This post shows you how to add a little AI control to the way enemies attack your player character.
After reading a tutorial here, by Kenneth Backus, I thought I’d implement what he did using Wade. I suggest you give his tutorial a quick read before you jump into the details of this post.
A few notes before I get started
- This demo uses Wade 3.8.1
- The ant and beetle assets in the demo are the intellectual property of Robert Brooks
- The background tile art assets are the intellectual property of kenney
Final Results preview
Using the keyboard (WASD) you can move the beetle around (you may have to click the demo to give it focus first). Click the ant button in the lower left of the demo to add some ants to the screen. The ants will seek out and attack the beetle. If an ant bites the beetle, you see an effect. Press the up arrow for the beetle to shoot and fend off the ants.
Note: The focus is given to the iframe on load, so if you click elsewhere on the page the key events won’t work. Also, if your browser is intercepting your keyboard input, you won’t be able to control the beetle. You will have to disable that for the demo to work. If you click the ant button and wait about four seconds, you should still be able to watch the ants attack and change state
The demo can be summarized in three parts
The beetle is controlled by the player, so from a programming point of view it is not very interesting. Wade provides a lot of stuff to help you get user input, show the correct graphics, object rotation and camera following . Look through this if you need some help figuring out how to control a character using the keyboard.
The ants as computer controlled characters are more interesting from a programming point of view. The graphic below summarizes the ant’s state machine.
- Hunt is the default state, seek out the beetle.
- If the ant is hit by a beetle bullet then flee. Once a safe distance from the beetle, revert to hunt.
- If in attack range, then change state to strafe and ask for permission to attack from the battle manager.
- If attacking, after you make contact with the beetle (bite – smoke effect), then retreat and allow another ant to attack.
There are some additional subtletiesin the code, but that is the gist of it.
The ant uses and bulds on the steering functionality developed in previous posts.
The ant’s state is displayed via a show states variable in the ant behavior file. Set this to false to hide the state text.
The battle manager controls a queue that determines which ant gets to attack next. When an ant goes into the ‘strafe‘ state, it sends an attack request to the battle manager. The battle manager then puts that ant into the battle queue.
In the onUpdate() function of the battle manager, it walks the queue and gives an ant permission to attack if there is one available on the queue in the ‘strafe’ state. Ants that have left the ‘strafe’ state are kicked out of the queue.
To tune how often an ant can attack, set the attack interval (this is the minum number of seconds between attack grants).
In the aforementioned tutorial, the communication between game objects is handled via a messaging queue. This is a much cleaner implementation and is how the attack request/grant should be handled. I didn’t do that here because I didn’t have any message queue code lying around (probably a future post) and it’s not built into Wade. For a demo, what I have done is adequate, but this woudl be an obvious refinement.
Download and extract the zip file, load battleCircle.html in your browser to see it in action.
There is a lot of code here, so it may take a bit to digest everything that is going on. I think it is pretty well commented, so hopefully that will make it a little easier to understand
One final note: no actual insects were harmed in the production of this post.
cheers – Shri