Enemy AI – range of sight
Let's now start developing enemy AI by thinking about our functional requirements. The enemies in the scene will begin in patrol mode, wandering the level from place to place searching for the player character. If the player is spotted, the enemy will change from patrolling and begin chasing the player, attempting to move closer to them for an attack. If the enemy reaches within attacking range of the player, the enemy will change from chasing to attacking. If the player outruns the enemy and successfully loses them, the enemy should stop chasing and return to patrolling again, searching for the player as they were doing initially. This, in sum, describes our needed enemy AI behavior.
To achieve this behavior, we'll need to code the line of sight functionality for the enemy. The enemy relies on being able to see the player character or determining whether the player is visible to the enemy at any one moment. This helps the enemy decide whether...