This tutorial covers how to add an Enemy to your project. To speed up the process we'll be importing an existing entity rather than building a new one ourselves. Once imported, we will modify the entity so it has the functionality we'll need for this tutorial - specifically adding the ability for the enemy to take damage.
To import the entity
Download the exported entity file from here: https://github.com/vchelaru/FlatRedBall/raw/NetStandard/Samples/Platformer/DealingDamage/Enemy.entz
In Glue, right-click on the Entities folder
Select Import Entity
Navigate to the location where you saved the Enemy file earlier and click OK
We now have a fully-functional enemy which has:
Collision shape named AxisAlignedRectangle
Sprite displaying a walking animation
Platformer values so that it can collide with solid collision
EnemyInput object which will keep the enemy from moving (does not use the gamepad or keyboard)
We will add a list to our GameScreen and an instance of our Enemy to Level1 so we can see the enemy in game. To add an enemy list to GameScreen:
Select the Enemy entity
Select the Quick Actions tab
Click the Add Enemy List to GameScreen button - this creates a list of enemies which we'll use to create collision relationships later
Click the Add Enemy Factory button - this allows us to create enemies in code and Tiled maps.
To add an enemy to your Level1:
Drag+drop the Enemy entity onto Level1\
Select Enemy1 and click on the Variables tab
Set X to 160
Set Y to -160
Now we have an enemy in the game, but it falls through the level. We can fix this by telling the enemies to collide against our GameScreen's SolidCollision object:
Expand GameScreen
Expand the Objects folder
Drag+drop the EnemyList onto the SolidCollision object
If we run the game now, the enemy will fall and land in the level next to the player.
We’ll begin this tutorial with an empty project. Mine will be called EnemyDamage.
The Glue Wizard can help us get a quick project set up. We will leave most of the options to their default, but we will change the following:
Change What kind of control will the player have? to Platformer
Uncheck Add Sprite to Player Entity unless you intend to add graphics to your Player entity. This tutorial will not cover how to add graphics to the Player entity.
Note that we will not be explicitly working with the Player entity in this game, but we still create one since most games need one eventually.
Change Number of levels to create to 1. We will only have one level in this game
Now our project is ready to go as a fully functional platformer. We can begin adding the Enemy entity in the next tutorial.
This set of tutorials covers how to create enemies which can receive damage from Bullets created by the Player. Many games include enemies which can take damage from the player such as Mega Man X and Super Metroid.
The following pages cover a number of topics related to dealing damage:
Dynamic entity creation and destruction (Bullets)
HP and damage amounts
Damage over time and area of effect
Friendly fire and teams
This tutorial adds a Bullet entity which the player can shoot. The Bullet entity has the following characteristics:
It will be visually represented by a circle
It moves left or right depending on which way the Player is facing when shooting
It will be destroyed when colliding with the SolidCollision
It will be destroyed when colliding with the Enemy
To create a Bullet:
Click the Quick Actions tab
Click the Add Entity button
Enter the name Bullet
Click the Circle checkbox under Collisions
Leave all of the rest of the values default and click OK
When a Bullet is created, it will move either left or right. We need to control the speed of the bullet. We will create a variable which we'll use in our code later:
Select the Bullet entity
Click on the Variables tab
Click the Add New Variable button
Verify that float type is selected
Enter the name BulletSpeed
Click OK
Enter a value of 300 for BulletSpeed
We will also want to change the radius of the Bullet's CircleInstance:
Expand the Bullet Objects folder
Select CircleInstance
Click the Variables tab
Change Radius to 6
The Bullet creation logic will be added to the Player entity. We need to detect if the shoot button has been pressed. If so, we'll create a new bullet and have it move in the direction that the player is facing. To do this, open Player.cs in Visual Studio and modify the code as shown in the following snippet:
Whenever the input device is set on a platformer entity, the CustomInitializePlatformerInput method is called. Since our entity has custom input for shooting, we add the CustomInitializePlatformerInput where we assign shootingInput according to our InputDevice type. In this case we assign shooting to the right ALT key if using a keyboard and the X button if using an Xbox360GamePad. Any re-assignment of input should be done in CustomInitializePlatformerInput rather than CustomInitialize. This is because the order in which code is executed. When considering input, assignment, the following is performed:
CustomInitialize
InputDevice is assigned (can be assigned in generated code or custom code)
CustomInitializePlatformerInput which is called whenever the input device is assigned.
CustomInitialize always runs before an InputDevice is assigned. We want our shootingInput-assigning code to run after the InputDevice is assigned, so we should put it in CustomInitializePlatformerInput.
Finally, we check our shootingInput.WasJustPressed to see if the user just pushed the input. If so, we create a bullet and set its XVelocity according to the direction that the Player is facing. If we run our game now, we can shoot bullets in the direction we're facing.
Currently, our bullets can move through walls and enemies. First we'll add collision between our GameScreen BulletList and SolidCollision:
Expand the GameScreen Objects folder
Drag BulletList onto SolidCollision to create a new collision relationship
Select the new BulletListVsSolidCollision relationship
Click the Collision tab
Click the Add Event button
Click OK to accept the defaults
Now we can destroy the bullet whenever the event occurs:
Open the project in Visual Studio
Go to GameScreen.Event.cs
Find the BulletListVsSolidCollisionCollisionOccurred method
Modify the code as shown in the following snippet:
Now we can shoot bullets and they will get destroyed when they hit the wall.
We end this tutorial with the ability to shoot bullets and destroy them when they hit the wall. The next tutorial will implement the ability to damage and destroy enemies.
This tutorial will implement dealing damage to an Enemy. Rather than immediately destroying the Enemy when it collides with a Bullet, we'll implement health points (HP) which can be reduced when an Enemy collides with a Bullet. Once an Enemy's HP is reduced to 0, it will be destroyed.
Typically enemies will have two values for HP:
MaxHP - the starting number of HP when an Enemy is first spawned
CurrentHP - the number of HP that the enemy currently has
The MaxHP is a designer variable - a variable which a game designer may change during the development of the game to adjust difficulty. We'll define this in Glue so it can be changed easily. By contrast, CurrentHP is a logic variable - a variable which is used in game logic and which should not be modified by a game designer. It will initially be assigned to MaxHP and will be reduced whenever the Player takes damage. To define a MaxHP variable:
Select the Enemy entity
Click on the Variables tab
Click the Add New Variable button
Select the int type
Enter the name MaxHP
Click OK
Set the new MaxHP variable to 6
Now we can add the CurrentHP value to the Enemy:
Open the project in Visual Studio
Open Enemy.cs
Add the CurrentHp variable and modify CustomInitialize as shown in the following code snippet
In a typical game, whenever an Enemy takes damage the game may perform many actions such as:
Flashing the Enemy or playing some visual effect
Playing a sound effect
Showing the amount of damage dealt (as a floating number)
Modifying how much health is shown in a health bar
Destroying the Enemy and showing a death effect if the Enemy has died
Keeping this logic in the Enemy.cs file can help keep code organized, so we'll be adding the following function to the Enemy.cs file:
Be sure to make the TakeDamage method public so it can be called from outside of the Enemy class.
Now we can create a collision relationship between the BulletList and EnemyList objects in the GameScreen:
Expand the GameScreen Objects folder
Drag+drop the BulletList object onto EnemyList\
Select the new BulletListVsEnemyList relationship
Click on the Collision tab
Click the Add Event button
Accept the defaults and click OK
Now we can fill in the event method. Open GameScreen.Event.cs and modify the BulletListVsEnemyListCollisionOccurred method as shown in the following snippet:
Now we can shoot at the enemy. After six shots, the enemy is destroyed.
Now we've created a game where we can destroy an enemy with 6 shots. A real game would have more than one enemy, but if we add additional enemies, our game will automatically collide bullets against each enemy, and each enemy will keep track of its own HP.
The first line of code in the Player class defines an IPressableInput. This is an object which can reference any pressable input hardware such as a keyboard key or an Xbox360GamePad button. We create this IPressableInput so that we can write code which will work regardless of input device. For more information on IPressableInput, see the .