Now we can control our ship and even the invaders move around, but nobody shoots. Let's change that in our example game. We want to shoot those invaders. There is quite a couple of things involved by that. We need a bullet blender model first. To spare you some time you can have mine from my google drive as usual. Place this beside the ship and the invaders models. Don't forget to convert it to j3o by right clicking the blend file.
Let's fire then
First add the model name to the model component Model.class
Now we need some additions in the control app state. Add the following private variable to the ControlAppState.class
As well add the following line to the initialize method
We add first a mapping for the shooting to the space bar and the left mouse pointer to generate a SHOOT event. Further more we need an actionListener instead of an analogListener to detect your key press. To make it work you have to add as well a private actionListener instance
I think the purpose of this action listener is cleaer: It sets a bullet 3.5 units above our ship. Hit F6 and see what happens if you fire your bullets. Yes they are all motionless and freezed on the screen.
Move move move
What do we need to make those bullest move upwards? We need at least a speed component. This speed comonent gives the bullet as well a direction.
And we have to add a speed component to the generated bullet above in the action listener.
Our bullet will reach the top most invader within 2 seconds to damage them. But still this won't move. So what shall we do? You guess it, we need a system for this. I call it BulletAppState.
And now register that new BulletAppState to our Main
But what happens with those bullets? The do never disappear from the game if they do not hit anything. And at the moment they are not programmed to hit anything. So what shall we do with them? We could have some guards in the bullet app state. If you are new to this entity system then I guess you would do this in the BulletAppState. But there is a much more elegant way to handle that. It is time for the second desgin pattern in entity system programming. The decay system. This decay system can be used for all kind of garbage we want to remove from entity system and with that from the visual area as well. The decay system can be used not only for the bullets but for many other things, it is like magic, just need a decay component to mark something as a decay and the decay system will take care of it. this is my favorit design pattern.
Get ride of it
So what do we need to get ride of those bullets we fired at the invaders. We need a decay component
The decay component holds the percentage of the decay, you can handover how long the decay should last until gone. So the bullet generation also needs a decay component, so the decay system comming soon can get ride of the bullets not needed any more.
The bullet shall life for 2 seconds, so we are sure it will reach the last invader before getting removed. And here comes the decay system
and register the decay system
You see we are only interested in all entities which do have a decay system, no matter if it is a ship, a bullet or something else you could think of. If we want to get ride of something, we just have to add a decay component to that entity and we are done, the rest is handled by this amazing decay system, which is very small and easy to understand.
I like this cleanness which can be found in many places in entity system driven code.