Two common patterns for implementing simple AI behaviors like you saw them in the classic 2D Zelda games are:
- State Machines. State machines are a general architectural pattern in software development with great potential for senseless overengineering. So if you ask 10 programmers to implement a state machine, you get 20 different solutions. But a simple one is to give every enemy a variable that says in which state it currently is (moving, turning, attacking, etc.). Your
_process/_physics_processwould then be aif/elifchain with separate code for each possible state to perform whatever the enemy is supposed to do during that state and checking the conditions to switch to a different state. - Behavior Trees. Another common pattern for simple enemy AI. The main difference between BT and SM is that a behavior tree doesn't remember which state itsit's in but rather determines the state again on each frame. You can express that logic using a data-driven approach as explained in the article I just linked. Or you can do it the simple way using an
if/elsetree checking all the relevant information and then behaves according to the result.