Initial commit
This commit is contained in:
@@ -14,6 +14,7 @@ public class NPC : MonoBehaviour
|
||||
|
||||
private Resource.ResourceType resourceToWorkWith, carryingResource;
|
||||
public Dictionary<Resource.ResourceType, int> inventory = new Dictionary<Resource.ResourceType, int>();
|
||||
public Dictionary<Skills.SkillType, Skills> skills = new Dictionary<Skills.SkillType, Skills>();
|
||||
|
||||
[SerializeField] int gatherAmount, inventoryCapacity;
|
||||
[SerializeField] NavMeshAgent navMeshAgent;
|
||||
@@ -74,7 +75,8 @@ public class NPC : MonoBehaviour
|
||||
}
|
||||
else
|
||||
{
|
||||
float distanceToTarget = Vector3.Distance(transform.position, currentTargetResource.transform.position);
|
||||
Vector3 targetPosition = GetResourceTargetPosition();
|
||||
float distanceToTarget = Vector3.Distance(transform.position, targetPosition);
|
||||
|
||||
if (distanceToTarget <= interactionRadius)
|
||||
{
|
||||
@@ -82,11 +84,21 @@ public class NPC : MonoBehaviour
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveToLocation(currentTargetResource.transform.position);
|
||||
MoveToLocation(targetPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 GetResourceTargetPosition()
|
||||
{
|
||||
if (currentTargetResource != null && job == Job.Miner && currentTargetResource.mineLocation != null)
|
||||
{
|
||||
return currentTargetResource.mineLocation.position;
|
||||
}
|
||||
|
||||
return currentTargetResource != null ? currentTargetResource.transform.position : transform.position;
|
||||
}
|
||||
|
||||
private void HandleDeliveryPhase()
|
||||
{
|
||||
// 1. Find a TownHall if we haven't cached one yet
|
||||
@@ -191,7 +203,7 @@ public class NPC : MonoBehaviour
|
||||
foreach (Resource res in resources)
|
||||
{
|
||||
if (res.resourceType == resourceToWorkWith && res.resourceState == Resource.ResourceState.Full)
|
||||
{
|
||||
{
|
||||
float distance = Vector3.Distance(transform.position, res.transform.position);
|
||||
if (distance < closestDistance)
|
||||
{
|
||||
@@ -204,7 +216,10 @@ public class NPC : MonoBehaviour
|
||||
if (closestResource != null)
|
||||
{
|
||||
currentTargetResource = closestResource;
|
||||
MoveToLocation(currentTargetResource.transform.position);
|
||||
if(this.job == Job.Miner)
|
||||
MoveToLocation(currentTargetResource.mineLocation.transform.position);
|
||||
else
|
||||
MoveToLocation(currentTargetResource.transform.position);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -250,7 +265,40 @@ public class NPC : MonoBehaviour
|
||||
}
|
||||
|
||||
currentTargetResource.RemoveResource(gatherAmount);
|
||||
AddToInventory(resourceToWorkWith, gatherAmount);
|
||||
|
||||
Skills.SkillType currentSkillType = resourceToWorkWith switch
|
||||
{
|
||||
Resource.ResourceType.Wood => Skills.SkillType.Woodcutting,
|
||||
Resource.ResourceType.Stone => Skills.SkillType.Stonecutting,
|
||||
Resource.ResourceType.Metal => Skills.SkillType.Mining,
|
||||
Resource.ResourceType.Food => Skills.SkillType.Farming,
|
||||
_ => Skills.SkillType.Woodcutting
|
||||
};
|
||||
|
||||
int currentSkillLevel = 1;
|
||||
if (skills.TryGetValue(currentSkillType, out Skills currentSkill))
|
||||
{
|
||||
currentSkillLevel = currentSkill.GetLevel(currentSkill.skillType);
|
||||
}
|
||||
|
||||
int bonusHarvest = Mathf.Max(0, currentSkillLevel - 1);
|
||||
AddToInventory(resourceToWorkWith, gatherAmount + bonusHarvest);
|
||||
|
||||
switch (resourceToWorkWith)
|
||||
{
|
||||
case Resource.ResourceType.Wood:
|
||||
GainExperience(Skills.SkillType.Woodcutting, 10);
|
||||
break;
|
||||
case Resource.ResourceType.Stone:
|
||||
GainExperience(Skills.SkillType.Stonecutting, 10);
|
||||
break;
|
||||
case Resource.ResourceType.Metal:
|
||||
GainExperience(Skills.SkillType.Mining, 10);
|
||||
break;
|
||||
case Resource.ResourceType.Food:
|
||||
GainExperience(Skills.SkillType.Farming, 10);
|
||||
break;
|
||||
}
|
||||
|
||||
if (currentTargetResource.GetResourceState() == false)
|
||||
{
|
||||
@@ -350,6 +398,18 @@ public class NPC : MonoBehaviour
|
||||
{
|
||||
return GetTotalInventoryAmount() + amountToAdd <= inventoryCapacity;
|
||||
}
|
||||
private void GainExperience(Skills.SkillType type, int amount)
|
||||
{
|
||||
// 1. Check if the dictionary does NOT contain the skill yet
|
||||
if (!skills.ContainsKey(type))
|
||||
{
|
||||
// The NPC learns the skill for the first time, using our new Constructor
|
||||
skills.Add(type, new Skills(type));
|
||||
}
|
||||
|
||||
// 2. Add the XP to that specific skill in the dictionary
|
||||
skills[type].AddExperience(amount);
|
||||
}
|
||||
|
||||
// Efficiency: Created a helper method so we aren't writing out the dictionary loop constantly
|
||||
private int GetTotalInventoryAmount()
|
||||
|
||||
Reference in New Issue
Block a user