IEnumerator MoveToRandomDestination() { while (state == EnemyState.RANDOM_MOVING) { GameObject hq = GameObject.Find("HQ"); Destination destination = null; IndividualMovement movement = gameObject.GetComponent <IndividualMovement>(); int count = 0; while (destination == null || !movement.DestinationReachable(destination) && count < 50) { int threat = (int)Time.fixedTime / 30; int seekHqChance = Random.Range(0, 50) + threat; if (seekHqChance > 40) { destination = new Destination(hq); } else { destination = new Destination(new Vector3(Random.Range(-500, 500), 0, Random.Range(-500, 500))); } if (Vector3.Distance(destination.Position, hq.transform.position) < 200.0f && Time.fixedTime < 120) { destination = null; } count++; } if (count == 50 || destination == null) { ChangeEnemyState(EnemyState.IDLE); } movement.MoveTo(destination, null); while (movement.moving) { yield return(new WaitForSeconds(0.5f)); } } }
public void ChangeEnemyState(EnemyState newState, GameObject target = null) { if (newState != state || currentRoutine == null) { if (currentRoutine != null) { StopCoroutine(currentRoutine); currentRoutine = null; } IndividualMovement movement = GetComponent <IndividualMovement>(); if (movement != null) { if (movement.moving) { movement.CancelMovement(); } } animator.SetBool("Attacking", false); state = newState; switch (newState) { case EnemyState.RANDOM_MOVING: currentRoutine = MoveToRandomDestination(); StartCoroutine(currentRoutine); break; case EnemyState.ATTACKING: currentRoutine = EnemyAttack(target); StartCoroutine(currentRoutine); break; case EnemyState.FLEEING: currentRoutine = MoveToNearestSpawner(); StartCoroutine(currentRoutine); break; default: break; } } }
IEnumerator AnimatedDeath() { IndividualMovement movement = GetComponent <IndividualMovement>(); if (movement != null) { movement.CancelMovement(); } if (animator) { animator.SetInteger("Health", health); yield return(new WaitForSeconds(1.5f)); } //Check if we have a crawler mother, we will have to spawn units upon death if (gameObject.GetComponent <AlienCrawlerMother>() != null) { gameObject.GetComponent <AlienCrawlerMother>().SpawnChildren(); } Destroy(gameObject); yield return(null); }
IEnumerator MoveToNearestSpawner() { while (state == EnemyState.FLEEING) { Destination destination = null; IndividualMovement movement = gameObject.GetComponent <IndividualMovement>(); while (destination == null || !movement.DestinationReachable(destination)) { GameObject[] aliens = GameObject.FindGameObjectsWithTag("Enemy"); List <GameObject> hives = new List <GameObject>(); for (var i = 0; i < aliens.Length; i++) { if (aliens[i].name.Contains("Hive")) { int nearbyHumans = DetectWithTag("Ally", aliens[i].transform.position, SENSE_RANGE); if (nearbyHumans < FLEE_NUM_HUMANS_THRESHOLD) { hives.Add(aliens[i]); } } } if (hives.Count > 0) { List <GameObject> reachableHives = new List <GameObject>(); foreach (GameObject hive in hives) { if (movement.DestinationReachable(new Destination(hive))) { reachableHives.Add(hive); } } if (reachableHives.Count > 0) { destination = new Destination(GetNearestObj(reachableHives)); } else { canFlee = false; ChangeEnemyState(EnemyState.RANDOM_MOVING); break; } } else { canFlee = false; ChangeEnemyState(EnemyState.RANDOM_MOVING); break; } } movement.MoveTo(destination, () => ChangeEnemyState(EnemyState.DEFENDING)); while (movement.moving) { yield return(new WaitForSeconds(0.5f)); } } }
public void Defend() { IndividualMovement movement = gameObject.GetComponent <IndividualMovement>(); if (movement.moving && currentRoutine != null) { ChangeState(AttackUnitState.DEFENDING); } // If we are not currently running a defend routine, we should start one now else if (currentRoutine == null && movement != null && movement.moving == false) { Collider[] nearbyColliders = Physics.OverlapSphere(transform.position, weapon.GetRange()); List <GameObject> nearbyEnemies = new List <GameObject>(); foreach (Collider col in nearbyColliders) { GameObject colObj = col.gameObject; if (colObj.tag == "Enemy") { nearbyEnemies.Add(colObj); } } if (nearbyEnemies.Count > 0) { List <GameObject> enemyUnits = new List <GameObject>(); List <GameObject> spawners = new List <GameObject>(); foreach (GameObject enemy in nearbyEnemies) { if (enemy.GetComponent <Hive>() != null) { spawners.Add(enemy); } else { enemyUnits.Add(enemy); } } List <GameObject> priorityEnemies; if (spawners.Count > 0) { priorityEnemies = spawners; } else { priorityEnemies = enemyUnits; } GameObject nearestEnemy = null; float nearestDist = 999999; foreach (GameObject enemy in priorityEnemies) { if (Vector3.Distance(enemy.transform.position, transform.position) < nearestDist) { if (enemy.GetComponent <AttackTarget>().GetHealth() > 0) { nearestEnemy = enemy; nearestDist = Vector3.Distance(enemy.transform.position, transform.position); } } } if (nearestEnemy != null) { currentRoutine = DefendingAttack(nearestEnemy); StartCoroutine(currentRoutine); } } } }