Some simple effects would be cool. But how to achive that? For example we may want a crackle when a bullet hit an invader or some cool 8 bit shot sounds when we fire. And for sure we want to see some nice explosion and hear an impressive blast. You got the point.
In this article we will look how we can get visual explosion. We need, yep exactly, yet another system. I like the fine granularity of those systems. This makes it as well easy to spot the bad guy if something is not working anymore.
The exploison system will deal with all kind of dying entities. We need a tagging die component and a position to display a nice explosion.
First some preparation. For the effect we need an explosion texture, you can take the one on my google drive as usual or design one your self, if you want to use my code 1:1 then your on one must be a 2x2 effect texture. Place that in the assets textures folder as flame.png
So let's start with the die component, pretty simple as it is only a tag.
And then we need as well the explosion system, which looks similiar to the visual system we implemented in the beginning.
So basically we hold all currently displaying effects in a map, like we did with the spatials in the visual state. This is kind a desing pattern you see often in entity systems. The updateEffects method is not really used in this example, but I leave it for the sake of completeness. The explanation for the createExplosion can be found in jME Hello Effects in the beginners section.
Of course you have to register that state in the Main.class like all the others.
If you start your game you will not see any new. Yes of course not, we have to add a die entity for this. And we add this die entity where we delete our destroied invaders in the collision state. There is maybe a cleaner way to do this, but this is the simplest way, so let's do it simple.
We add a die entity with the die component and the position of the ship we delete in the update method of the CollisionAppState.class
If done start your game again. You happily see the explosion... but you realize that it never ends. Not exactly what we wanted. The explosion doesn't need to last for ever it is enough to last for 1 second. So let's fix this.
Point of no return
The first idea you might have is to fix it in the explosion state, right? So there is the explosion so we do something with measuring time and stuff to get rid of it and handle this in the update method. But ... hold on, isn't there a simpler solution? A control maybe? Nope.
We can add a decay component and let handle this by the decay system. That is done with one single line! And exploring this simplicity and reusabiltiy was my personal point of no return. After that I could not think of implementing a game without an entity component system in hand.
So let's do it, add a decay of 1 seconds to this die entity