Full LibGDX Game Tutorial – Enemy System

Sharing is caring!

Full LibGDX Game Tutorial – Enemy System

Welcome to part 13 of our Full LibGDX Game Tutorial. This part will focus on adding an enemy. We will add a mechanic to add patrolling enemies to some of our platforms. If you haven’t seen the earlier parts of this tutorial I advise you to start at Full LibGDX Game Tutorial – Project setup as this tutorial continues from these earlier parts. For those of you who have come from part 12, you can continue on.

In the last part, we created some mechanics to allow us to kill our player if the get hit by the ever increasing water zone. This time we will be adding enemies to our world on random platforms.

Creating our components for our enemy system.

The EnemyComponent simply needs to hold a few items relating to our enemy. We will hold the original position of the enemy so we can make them move around this point. We will also add a direction flag so we can tell which direction they’re going and flip them if they get too far and finally, we will add an isDead flag so later we can add the ability for our player to kill the enemies.

So let’s make our EnemyComponent:

With the component made we can now add a system to control the enemies. The system will be pretty much the same as other systems we have in place with some bespoke code for our enemies in the processEntity method.

In the code above you can see we have made it so enemies will move left and right until they get 1 unit away from their original position then flip direction.

Adding Enemy units to our level.

Now we have a system for controlling enemies we need to add enemies to our world. In order to do that we will add a method to our levelFactory class call createEnemy:

This again is pretty standard and similar to the other methods we already have in our levelFactory. The next thing we will do is update our generateLevel method to add enemies randomly based on our simplexNoise values:

Here you can see we make use of the noise7 and noise8 we added in the last part to decide whether to add an enemy on the pad or not. You will also notice we are now using a second noise called simRough. This is a second simplex noise with a higher roughness level. This allows us to have greater control of our level generation as we can now adjust the platforms and enemies separately instead of all using the same noise.

Previously we were using the same texture for a lot of our objects. Since we added the DFUtils in the last part we can now update our level factory to use the utilities in that class. So let’s update out LevelFactory constructor to use the DFUtils and create a second simplex noise.

After adding this code to your project you may notice that the makeTextureRegion method is showing an error. This is expected as an extra method has been added to the DFUtils to help keep the code readable. The new method is:

This method simply wraps the other makeTextureRegion with ints as parameters to using floats, which it then converts to ints. This saves us converting it in our levelFactory and makes it easier to read.

Finally to add our new enemy system to the game we update our MainScreen class and add the new system to the engine like this:

In part 14 we will be adding the ability to shoot. Adding functions to check if the player has pressed the mouse button, checking if the player is allowed to shoot, generating a bullet, killing enemies and removing dead objects from our game.


If you see any mistakes in this article or have some suggestions please feel free to leave a comment below.

← Game Mechanics Contents Shooting →

Sharing is caring!

One Reply to “Full LibGDX Game Tutorial – Enemy System”

  1. Nice tutorials. I have been following it kinly. But i have this issue. from the previous tutorial, you did not show this system: engine.addSystem(new WallSystem(player)); it was suddenly placed with the rest of the code. The implementation was not shown.

    Similarly, I discovered adding any new system causes a Null pointer exception and I have tried debugging but couldn’t make a head way. The new additions causing this error are: lvlFactory.createWaterFloor(wFloorRegion);, lvlFactory.createWalls(wallRegion); znc now the new one: engine.addSystem(new EnemySystem()); kindly help out.

    Thank you

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.