After witnessing the firepower of your fully armed and operational battleship, it is time to give it something to shoot at! We are going to add some enemies and, as always, we are going to start adding these spritesheets to spritesheets.xml:
We have nicknamed the 3 kind of enemies ‘cannon’, ‘triangle’ and ‘kamikaze’ (Sidequest: try to guess which is which in the spritesheet and then read the coordinates of the frames to check if you were right!).
The triangle and cannon spritesheets are pretty straight forward, but we are going to use a few tricks in the other: we have three different states which will be used depending on the health of the enemy to display the damage caused to the ship, and when the health is low we are adding random offsets to the position of the layer to show how its warp engine collapses into a singularity (Pro tip: this is a videogame, so just add cool stuff and then justify it with sciency words!).
Before programming the enemies, we have one problem to solve first: how can we detect when a ship collides with another? Or when a ship collides with a projectile? In order to easily let you solve that problem, Clockwork lets you define collision detectors. These will be functions that decide whether two objects with an specific shape collide, and can be implemented as you want depending on the requirements of your game. For example, you could have a game that happens in a one-dimensional universe, so to check if two objects collide you only need to know if their position in the only axis is the same, but you could also have a game that plays in 4 dimensions (if you manage to imagine that, please let us know, our brains still hurt from trying) or a game where objects collide if they occupy the same position in space and have the same color.
Luckily, we are just going to see if a point is inside a rectangle in the 2D space. Create a file called collisions.js inside the gameFiles folder and paste this content inside:
Here we are registering two collision detectors, one for telling if an enemy fire (which will be a point) is inside the player hitbox, and another one for checking the same but for player fire and enemy hitboxes. Actually, you can see that the code for both is the same (replacing the parameter names), so why define two? That way, the engine will only check collisions of enemy fire against the player and not its fellow enemies, and vice versa, meaning it will be more efficient and you won’t need to filter those false positives later. The code itself is very straightforward, it returns true if the point is at the right of the left side of the box, below the top side, at the left of the right side and above the lower side.
Now it is time to let Clockwork know you want to use this code file. Go to the manifest and update the components property:
You can add as many (existent) files as you want, so you could actually divide your components on several files to have your codebase more organized. For the sake of simplicity (and also because we are slightly lazy, though we won’t admit that openly), we will just keep piling components in components.js.
Now is where we would tell you to create a component that inherits from ship and adds some logic for the baddies, but we have a problem here. That logic would go inside the #setup and #loop events, and if you declare them inside your enemy you will overwrite the ones from the parent (ship), but we would still like our enemy to follow the laws of physics.
Don’t worry, confused programmer, cause we have you covered! We are going to create a component called kamikazeLogic, and then create another one called kamikazeShip that inherits from both kamikazeLogic and ship, so it will inherit both events. This would also potentially allow us to add those “brains” to other objects aside from ships.
This are the components you need to add to components.js:
KamikazeLogic will use the setup event to set a permanent acceleration in the y axis, overwrite the friction value to limit its speed and set up some variables of its own. In order to choose which state is used to render the object (from the ones defined in the spritesheet), it sets the value of the $state variable. Each frame, it will use this.engine.find(objectName) to get a reference to the player and thrust left or right to get closer to it, and when it is past the lower side of the screen it will silently destroy itself.
In the #collide event, we write the code that will be executed when it crashes against something. Since this ship might crash against the player in a suicidal attack or be reached by the player fire, we have to differentiate depending on the type of colliding object, found on shape2kind (if you want to know more about the information received in the collision events, feel free to have a look at the documentation on the Clockwork website). When it crashes with the player it will destroy itself and display an explosion, but when is reached by the player fire it will add points to the score and decrease its health, updating its state. If its health reaches 0, it will destroy itself.
The most interesting bit is the collision property, which sets a collider of type enemy, that defines a bounding box that covers the ship (its coordinates are relative to the object position), and a point of type enemyfire in the front of the ship. We are defining two different colliders because we want this ship to behave both as a target for enemy fire and as a bullet itself that can damage the player.
Now we just need to add the explosion spritesheet:
And the explosion component:
Finally, don’t forget to update the playerFire component so it has a playerFire collider:
And add a collider to playerShip as well:
And we will now have all the necessary pieces in place, add this object to your level
Deploy your game and try to destroy the ship before it reaches you!
Now we are going to add the other two kinds of enemies. They don’t use any new clockwork features, they are just enemies that can be killed on one shot, don’t damage the enemy on contact and shoot either one or two projectiles once in a while. To speed up things a little, here you have all the components needed to implement them (now you should be able to easily understand what they do!):
And you will also need to add the spritesheets for the enemy fire:
Here you can notice a useful technique, we can use t (the number of milliseconds passed inside the current animation) as a variable to calculate the layer position, leading to some interesting effects.
Let’s add the new ships to the level file to spawn them:
Try deploying the game and seeing how their behavior is defined by their code, each of them behaves differently and thus the player will need to follow a different strategy to defeat them.
You were probably able to defeat the enemy without taking a single hit, weren’t you? Well, don’t get too confident, remember that we haven’t coded the lives system yet, head to part 6 of this tutorial to learn how to implement it!