AI PART 2

It’s worth noting at this point that my approach to AI is inspired by this guide by David Silverman. When I wrote my first beat’em up engine in Gamemaker a few years ago, this was the guide I followed. In C the way it is coded is different but many of the concepts remain the same.

So today we are showing off those hit boxes I keep talking about and using them to collide with the player, drain health and eventually end their green rectangular life.

First of all we are expanding our ChooseMove function:

We have added a check here to see if the Enemy is close enough to the player to think about attacking. We check this on the X and Z axis.

If close enough then we run the CheckAttackChance function, this is where the enemy will have some randomising AI that controls how aggressively they attack. Eg. the grunt enemies will not attack as frequently as the big boss enemies.

So currently each frame the enemy’s aggressiveness will increase by 1. It then checks to see if the aggressiveness value is higher than a randomly picked number between 0-100.

If so it will trigger the enemy’s Punch1 state (incorrectly labelled as PLAYER STATE PUNCH1 but I’ll clean that up later)

Eventually I will add more code here to choose from a variety of attacks.

So in the SwitchState function we do some one off things like setting the length of the attack and eventually triggering animation and SFX like so:

With the state now set to PUNCH1 the code then comes back into the UpdateEnemyState function where it will run the EnemyAttack function each frame until it is finished.

EnemyAttackTimer simply reduces the attack_timer value by 1 each frame.

We then trigger the hitbox and collision detection some time after the attack has started as we want it to sync with the frame of animation where the arm is extended. We use a hitboxTime argument to control which frame we want this to happen. So if attack_timer is less than say 3 it then triggers the hitbox and collision detection.

When the attack_timer is 0 we then turn of the hitbox, reset their aggressiveness and set the enemy’s state back to Idle (so they can start again).

All of this will be expanded in the future to allow for enemy combos and other things but for now we can do some simple attacking like this.

And what we get may look like a primitive sex game but it provides the basics of our beat’em up engine :)

Share