AI Implementation
![]() I have finished coding and implemented the AI that I planned and structured last week. I will attempt to summarise what code went into its behaviours and managements of states, and how they were implemented into an enemy Entity.
In the Enemy objects Class, an AIStates pointer is located and in the Enemy class’ Update function it sets this pointer to a new AIState by first deleting the memory allocated to the current state and then switching the pointer to a different state and allocating memory for this state instead. Each state of the AI inherits a HandleState function that returns true or false. The Update function of the Enemy object checks HandleState for the AIStates pointer meaning that it checks the HandleState function defined in whatever inheritor class is pointing to . If the conditions set within HandleState would cause it to return false, the update function in the Enemy object will switch the AIStates pointer to a new state, depending on what state it was currently in when it returned false. The result is that the AIStates pointer can only point to a single different state at any given time. The same results might have been possible if the HandleEvents code of each state and all necessary variables would have been placed within the Enemy entity and checked with conditional statements, but by utilizing the AIStates pointer structure the code can be separated from the Entity, providing a more clear code structure and requiring the program to check less conditions that might have otherwise eaten up memory. The behaviours of the AI are a combination of the code within each states HandleState function and criterias for switching defined in the Enemy Update function: For instance the HandleState of the ”Patrol” state creates a waypoint and then calls upon the Enemy MoveTowards function, causing the enemy to turn in relation to the waypoints angle. The Enemy already has defined within its Update to continuosly move towards which way it is facing. If the Enemy encounters a Player entity on its way by checking its sight-radius to see if any Player is within it, the Patrol states HandleState will return false, causing the Enemy update to check which state it should enter. It gets the Enum for the current state, which is Patrol, a conditional statement then tells it to switch its AIStates pointer to the ”Chase” state, starting then to check the HandleState function of the new state.
|
