//Abstract classes are never used by the parent, only the child //Chase the player void ChaseAndFire(AISense sense) { isSpotted = true; //This is used to get a direction from the player and our current position Vector2 dir = GameManager.instance.player.transform.position - transform.position; //Get the angle of the direction float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; //Angel axis Quaternion qto = Quaternion.AngleAxis(angle, Vector3.forward); //Euler Quaternion qto2 = Quaternion.Euler(qto.eulerAngles.x, qto.eulerAngles.y, qto.eulerAngles.z); //Set the rotation transform.rotation = qto2; //Move up, What ever direction the enemy is facing he will move towards it transform.position += transform.right * 2 * Time.deltaTime; //If we cannot see the player if (!sense.CanSee(GameManager.instance.player)) { //Set the current state to patrol currentState = AIStates.Patrol; } }
// Use this for initialization void Start() { //Set the curren state to patrolling currentState = AIStates.Patrol; //sense is the component of our enemy sense = GetComponent <AISense>(); }
//Patrol method for rotating void Patrol(Text text, AISense sense) { isSpotted = false; // Do Patrol transform.Rotate(0, 0, -Time.deltaTime * rotationSpeed); // Check for Transitions if (sense.CanSee(GameManager.instance.player) || sense.CanHear(GameManager.instance.player, GameManager.instance.noiseMaker.volume)) { //Since spotted, increase the counter text.text = "Times Spotted: " + ++timesSpotted; //currentstate is now Chasing currentState = AIStates.ChaseAndFire; } }
//Enemy State Machine Virtual method public virtual void StateMachine(GameObject spotted, Text text, AISense sense) { // AI States are based on enum value switch (currentState) { //If the currentstate is Patroling case AIStates.Patrol: // Do patrol work spotted.SetActive(false); Patrol(text, sense); break; //If the current state is chasing case AIStates.ChaseAndFire: // Do patrol work spotted.SetActive(true); ChaseAndFire(sense); break; } }
public void ChangeState(AISense newMode) { // Change our state aiSense = newMode; }