private void Awake() { agent = GetComponent <NavMeshAgent>(); compHealth = GetComponent <Health>(); compVillagerStatus = GetComponent <VillagerStatus>(); ts = GetComponent <TimeScale>(); }
// Event invocations. private void OnDied(VillagerStatus obj) { if (Died != null) { Died(obj); } }
// Event invocations. private void OnVillagerDied(VillagerStatus victim) { if (VillagerDied != null) { VillagerDied(victim); } }
// Event payload for when a villager dies. private void Village_VillagerDied(VillagerStatus victim) { // If the current target was removed from the villager list... if (victim == cmpEnemyMovement.target) { // Choose a new target. SetRandomTarget(); } }
// Villager death event payload. private void VillagerStatus_Died(VillagerStatus victim) { //Debug.Log("Whoa! A villager DIED!!!"); // Remove the villager from the villagers list. villagers.Remove(victim); // Notify subscribers about the villager's death. OnVillagerDied(victim); // If there are no villagers left, game over. if (villagers.Count == 0) { OnAllVillagersDied(); } }
// Get the closest villager to a position that's within a certain distance. // Returns null if no villager is found. public VillagerStatus GetClosestVillager(Vector3 position, float maxDistance) { // The closest villager so far. VillagerStatus closestVillager = null; // Iterate through all of the villagers. foreach (VillagerStatus villager in villagers) { // Calculate the distance between the villager and the point. float distance = Vector3.Distance(villager.transform.position, position); // If the distance is the closest one so far... if (distance < maxDistance) { // Update the smallest distance so far (between the villagers and the position). maxDistance = distance; // This villager is the closest one so far! closestVillager = villager; } } return(closestVillager); }
private void Start() { List <Transform> housePositions = kp.GetKeyPoints(); #if PRINT_HOUSE_COUNT Debug.Log("Number of houses: " + housePositions.Count); #endif // All of the villagers are spawned here. // Loop for each transform. foreach (Transform trans in housePositions) { // Instantiate a new villager. GameObject newVillager = Instantiate(villagerPrefab, trans.position, Quaternion.identity); // Get the villager's relevant components for assignment operations. VillagerMovement vm = newVillager.GetComponent <VillagerMovement>(); VillagerStatus vs = newVillager.GetComponent <VillagerStatus>(); TimeControllable tc = newVillager.GetComponent <TimeControllable>(); // Assign the house to the villager. vm.houseTransform = trans; // Pass the shrine to the villager. vm.shrine = shrine; // Pass the food controller reference to the villager. vs.foodController = foodController; // Pass village reference to the villager. //vs.village = this; // Subscribe to the villager's events. vs.Died += VillagerStatus_Died; vs.AttackedByEnemy += VillagerStatus_AttackedByEnemy; // Pass the time controller to the villager. tc.timeController = GetComponent <TimeControllable>().timeController; TimeScale.PassTimeScale(newVillager, ts); // Add the villager to the list of existing villagers. villagers.Add(vs); } }
// Return true if a villager is close enough to a pointer. public override bool AdditionalPointerChecks(Vector3 location) { target = car.gameController.village.GetClosestVillager(location, range); return(target != null); }
// Choose a certain villager to target. public void SetTarget(VillagerStatus newTarget) { cmpEnemyMovement.target = newTarget; }