Enemy Entity
Introduction
This tutorial will create the Enemy entity which we'll use in the remainder of the tutorials. This enemy will be similar to a Player entity - it has collision and will use Platformer physics, but it will not use input from a keyboard or gamepad - instead its movement will be controlled purely in code.
Creating the Enemy Entity
We'll be creating an Enemy entity ourselves since it wasn't automatically created for us by the Glue Wizard. To do this:
Click the Quick Actions tab
Click the Add Entity button
Enter the name Enemy
Check AxisAlignedRectangle
Check Platformer for the Input Movement Type
Leave the rest of the defaults and click OK
We will also change the color of the enemy rectangle so we can tell it apart from the Player:
Expand the Enemy Object folder
Select AxisAlignedRectangleInstance
Select the Variables tab
Change Width to 16
Change Height to 16
Change Color to Red
Adding an Enemy to Level1
Normally entities like Enemies are added through Tiled files, as shown in the breaking blocks tutorial. To keep the tutorial shorter, we will be directly adding Enemy instances through Glue. We could add enemies to GameScreen, but it's more common for each level to specify its own enemies, so we'll be adding the enemy instance to level1. First we will modify the EnemyList in GameScreen (which was automatically added as one of the default options when we created our Enemy entity) so it can be accessed in the levels. To do this:
Expand GameScreen Objects folder
Select EnemyList
Click the Properties tab
Set ExposedInDerived to True
Now that this is true, the EnemyList appears in all of the Level screens (which are derived from the GameScreen), and we can add instances to these lists.
To add an enemy to Level 1, drag+drop the Enemy entity onto the Level1 Screen.
We also need to modify the Enemy so it is positioned inside of the solid boundary of our game screen. To do this:
Select the newly-created Enemy1 in Level1
Select the Variables tab
Change X to 160
Change Y to -160
EnemyList vs SolidCollision
Now we have a fully-functional enemy, but it falls through the solid collision since we haven't yet set up an EnemyList vs SolidCollision relationship. To do this:
Expand GameScreen Objects folder
Drag+drop the EnemyList onto SolidCollision. Notice that we are doing this in the GameScreen rather than Level1 because we want all enemies to collide with the SolidCollision regardless of level.
Glue automatically sets the Collision Physics to Platformer Solid Collision since the Enemy entity is marked as a Platformer.
Conclusion
Now we have an Enemy entity and an instance of this Enemy in Level1. This instance collides with the game's SolidCollision and has full support for platformer physics. Currently both the Player and Enemy are controlled by the keyboard (or gamepad if one is plugged in). We will remove this input control from the Enemy and replace it with logic-based movement in the next tutorial.
Last updated