Episode 12: AI

In addition to building entities, the entity factory also sets the AI (artificial intelligence) of said entities.

I have to admit that I knew nothing about AI programming heading into this, and by the end of it I still didn’t know anything, but I did have a working AI system!

The way I implemented AI was to simply write functions for each type of entity that checks the AI settings in its preset to see how it should behave. Here’s an example of the AI settings of an enemy:

The AI function of the entity is executed by the play state’s update function (once per frame). The function looks like this:

Using nested conditionals and switch statements usually isn’t a good way to do anything, but it was the most obvious solution. Plus the behavior is pretty basic, so I didn’t want to waste too much time on it.

Here’s how the preset at the top of the page would end up working:

  1. The entity moves laterally to line up with the player
  2. The entity moves forward
  3. Once the entity is close to the player, it stops moving forward and starts firing

Pretty neat, huh?

The obstacles and drops just scroll across the screen, but the enemy AI seems to work pretty well. I added a few more features, like having enemies try to use their modules or turn toward (or away from) the player.

I’m also pondering whether or not to have enemies use their secondary weapons. Enemies are vessels, and vessels have both sets of weapons, but how would they decide what set to use? I guess they could alternate between the two, but what would determine when to alternate? So many questions.

Leave a Reply