private bool PlayerIsInRange() { if (CanHear.CanHear(GameManager.Instance.Players[0].gameObject)) { return(true); } else { return(false); } }
/* * Handle Movement */ public override void move() { PlayerPawn playerPawn = (PlayerPawn)pawn; // Pressing W will move up if (Input.GetKey(KeyCode.W)) { if (!senses.isChasing && senses.playerInRange) { senses.CanHear(playerPawn); } pawnTransform.Translate(0, Time.deltaTime * speed, 0); // Pressing A will move left } else if (Input.GetKey(KeyCode.A)) { if (!senses.isChasing && senses.playerInRange) { senses.CanHear(playerPawn); } pawnTransform.Translate(Time.deltaTime * -speed, 0, 0); // Pressing S will move down } else if (Input.GetKey(KeyCode.S)) { if (!senses.isChasing && senses.playerInRange) { senses.CanHear(playerPawn); } pawnTransform.Translate(0, Time.deltaTime * -speed, 0); // Pressing D will move right } else if (Input.GetKey(KeyCode.D)) { if (!senses.isChasing && senses.playerInRange) { senses.CanHear(playerPawn); } pawnTransform.Translate(Time.deltaTime * speed, 0, 0); } }
// Update is called once per frame void Update() { switch (currentState) { case AIStates.Idle: Idle(); //Gives the enemy directions on when to be idol and when not to be. if (senses.CanHear(GameManager.instance.player.gameObject)) { currentState = AIStates.LookAround; //allows lion to look around. } if (senses.CanSee(GameManager.instance.player.gameObject)) { currentState = AIStates.Chase; //allows lion to chase. } break; case AIStates.Chase: Chase(); //gives lion the abillity to chase depending on situation. if (!senses.CanSee(GameManager.instance.player.gameObject)) { currentState = AIStates.LookAround; //lion will look during chase. } if (Vector3.Distance(tf.position, GameManager.instance.player.tf.position) > giveUpChaseDistance) { currentState = AIStates.GoHome; //if player is lost lion will go back to start position. } break; case AIStates.LookAround: LookAround(); //Lion will scan area. if (senses.CanSee(GameManager.instance.player.gameObject)) { currentState = AIStates.Chase; //if player is spotted it will chase. } else if (Vector3.Distance(tf.position, GameManager.instance.player.tf.position) > giveUpChaseDistance) { currentState = AIStates.Chase; //if player is within distance it will chase. } else if (!senses.CanHear(GameManager.instance.player.gameObject)) { currentState = AIStates.GoHome; //if player is not within distance or is out of site lion will go home. } break; case AIStates.GoHome: GoHome(); //allows lion to move back to the starting position. if (senses.CanHear(GameManager.instance.player.gameObject)) { currentState = AIStates.LookAround; //will go home if sight lost. } if (senses.CanSee(GameManager.instance.player.gameObject)) { currentState = AIStates.Chase; } if (Vector3.Distance(tf.position, homePoint) <= closeEnough) { currentState = AIStates.Idle; } break; //Lion pawn's actions are managed in all code above. } heading = transform.position - playerPosition.position; distance = heading.magnitude; if (distance < 2)//sets distance. { transform.position = Vector2.MoveTowards(transform.position, playerPosition.position, speed * Time.deltaTime); }//if player is within distance lion will move towards player. }
//Update is called once per frame void Update() { tf = gameObject.transform; //AI states are based on enum values switch (currentState) { case AIStates.Idle: Idle(); //Check for transitions if (senses.CanHear(GameManager.instance.player.gameObject)) { currentState = AIStates.LookAround; } if (senses.CanSee(GameManager.instance.player.gameObject)) { currentState = AIStates.Chase; } break; case AIStates.Chase: Chase(); //Check for transitions if (!senses.CanSee(GameManager.instance.player.gameObject)) { currentState = AIStates.LookAround; } if (Vector3.Distance(tf.position, GameManager.instance.player.tf.position) > stopChaseDistance) { currentState = AIStates.GoHome; } if (Vector3.Distance(tf.position, GameManager.instance.player.tf.position) <= stopChaseDistance) { currentState = AIStates.Shoot; } break; case AIStates.LookAround: LookAround(); //Check for transitions if (senses.CanSee(GameManager.instance.player.gameObject)) { currentState = AIStates.Shoot; } else if (Vector3.Distance(tf.position, GameManager.instance.player.tf.position) > stopChaseDistance) { currentState = AIStates.GoHome; } else if (!senses.CanHear(GameManager.instance.player.gameObject)) { currentState = AIStates.GoHome; } break; case AIStates.GoHome: GoHome(); //Check for transitions if (senses.CanHear(GameManager.instance.player.gameObject)) { currentState = AIStates.LookAround; } if (senses.CanSee(GameManager.instance.player.gameObject)) { currentState = AIStates.Chase; } if (Vector3.Distance(tf.position, homePoint) <= closeEnough) { currentState = AIStates.Idle; } break; } }