Boom Booom Boooooom
Yes exactly all those bullets and not effect, not hit, no explostion, how boring is that. Let's implement a collision system and let's damage those invaders with the ships bullets. For this we need a couple of things.
In this article series I left out that the invaders as well can shoot at us, but I guess after you are through this article you should have an idea how to solve this.
What is my size
Let's introduce a simple collistion shape component where we just store a radius, this is by far good enough for the use case at hand.
Now handle the colliders
Further more we need a system to handle the collision, let's start first with a simple one, we will improve this system step by step.
To calculate if we have a collision I implemented a helper method. The method just calculates the distance and the threshold of the two involved radius and compares if the distance is inside the threshold. Pretty straight forward isn't it.
Further more I have define two sets of entities which come in handy later. For the moment this sets contains both the same, so I calculate the collision between all involved enties except the ship. I'm aware that this is not very performant if you have tousands of entities to check. We will improve this a litther further down.
One important thing, we now have to get every invader and every bullet a collision shape, else the collision system will never find them! In the GameAppState.class you have to replace the old code for setting the invader with the following one with the new collision shape component.
As well in the ControlAppState, where you create those nice bullets above your ship, you have to add a collision shape component
Dont forget to add that new system to your Main.class
Fire up your game with F6. Yeah I know there is no new behaviour visible. We have to do a little more, at least the TODO we should fill with life, but even that is not enough.
Might the force be with you
My idea to deal with only the interested entities is to have an attack and a defence component. The bullet only have a attack component and the invaders only have a defense component. On collision both component will be calculated against each other. If either the attack or the defense is used up that component will be deleted. Let's say our bullet do have attack of 1 and the invader do have defense of 2 on collision. After calculation the bullet do have an attack of 0 and the invader do have a defense of 1. The bullet disappears. That easy.
We need an attack component...
... and a defense component.
Looks quite straight forward and holds just the attack respective defense power.
We are nearly done. We need to adjust the collistion system to be able to deal with attack and defense component and react if one or the other is used up.
Lets first change the query of the attackParts and the defendParts by Attack.class and Defense.class to get only the interessted parties and not everything.
This as well reduce the amount of elements we have to compare against each other.
This alone is not sufficent, as you track now the attack and defense component as well we have to improve the creation of the invaders and the bullets once again with the following change
Finally we replace the TODO in the collision app state with the following snippet
And we are done. We now calculate the defense and the attack against each other, if one of them is equal or below zero we remove it. Looks simple, right. It is simple.
Now fire up your game and see what happens when you shoot.
I'm sure you are now able to improve this game a little bit and make the invaders shoot at our ship. For this our ship needs as well a collision shape of 1 and a defense of at least 3. And of course the invaders need some bombs to drop. Hint: the invaders can shoot them self with our approach, so make sure that an invader only shoot if no other invaders is below. Further more you will need some component for reloading with some random in it.
That's it for now. In the next article we have a closer look how we could add some nice effects and how this decay system comes in to play once again. You will love it.