IEnumerator DecideAction() { while (true) { if (stateHandler.IsCurrentState(EnemyStateHandler.States.Patrolling) || stateHandler.IsCurrentState(EnemyStateHandler.States.Chasing)) { if (playerDetection.IsPlayerInView()) { stateHandler.SwitchToDetectedPlayerState(); movement.StopRotating(); Vector3 rotateDir = (GameManager.instance.player.transform.position - transform.position).normalized; yield return(StartCoroutine(movement.RotateTowardsDirection(rotateDir))); if (!stateHandler.IsCurrentState(EnemyStateHandler.States.Dead)) { yield return(StartCoroutine(playerDetection.CheckIfHeldLineOfSight())); } } else { HandleWaypoint(waypointHandler.GetCurrentWaypoint()); } } else if (stateHandler.IsCurrentState(EnemyStateHandler.States.Shooting)) { gunHandler.Shoot(); transform.forward = (GameManager.instance.player.transform.position - transform.position).normalized; if (!playerDetection.IsPlayerInView()) { stateHandler.SwitchToChasingState(); } } else if (stateHandler.IsCurrentState(EnemyStateHandler.States.LookAround)) { if (!movement.isLookingAround) { StartCoroutine(movement.LookAroundForPlayer()); } } yield return(new WaitForFixedUpdate()); } }
/// <summary> /// Checks after secondsToWait if the player is still in line of sight, then switches to shooting/patrol state /// </summary> public IEnumerator CheckIfHeldLineOfSight() { float secondsToWait = 1; float timeWaited = 0; Coroutine ColorCoroutine = colorController.SwitchToHostileColour(); while (timeWaited < secondsToWait) { timeWaited += Time.deltaTime; if (!IsPlayerInView()) { colorController.StopCoroutine(ColorCoroutine); stateHandler.SwitchToLookAroundState(); colorController.SwitchToAwareColour(true); yield break; } yield return(new WaitForFixedUpdate()); } if (stateHandler.IsCurrentState(EnemyStateHandler.States.DetectedPlayer)) { stateHandler.SwitchToShootingState(); } }