Cool, bright and beautiful
Entity Component System short ECS or even shorter ES is one of the coolest, brightest and most beautiful paradigm in programming I have ever met. It is data driven and completly different to object oriented programming most people know nowadays. The entity system approach brought me back the feeling of the old days hacking games on a ZX81 with a 4KByte RAM extension. Really.
There are many good articles about entity systems, therefor I will not waste my time to write yet another one, just google and you get enough information. I'd like to write a series of blog articles to show you the beauty of the entity system by programming an example game with it. I will show you some of the design patterns I know with that example game.
The game I have in mind is a pretty simple invader like game with the famous jMonkeEngine and Zay ES. I'm currently not sure how far I will go and to how many parts this article will grow.
I will try to show all code in this series, so you you just can copy paste it and try out. Like in the old days where you did buy magazins and type those hex number rows in your comodore 64 and see what kind a game will resulting from it - or none at all because you made a typo somewhere in those 3 pages of hex digits.
Use a monkey
First you have to download JME 3.1 and make it work, it is pretty straight forward. Further more download as well Zay ES and its dependencies guava and slf4j-api. After that start a new game project in JME with the following steps: "New Project -> JME3 -> BasicGame"
I did simply add the jars with: Right click your project "Properties -> Libraries -> Add JAR/Folder". You can have them where every you want, I personally have them in a lib folder in my JME3 project.
Hint: Maybe you want to play through the Examples of JME3 first. I guess it is helpfull to understand the basics. But it is not really mandatory. Up to you, I don't care.
So now we are prepared for the next steps.
The boring part
Let's start with the main part...
Hit F6 and start your game. After selecting the resolution you only see a black screen. Not very chilling indeed.
It is all about the the entity system so lets setup and initialize Zay ES before we head into the game itself.
There is not much to say about this application state. As it is brain dead simple.
Black screen? I mean... hello?!
Indeed a game without visual representation is not much fun. Furthermore it always helps to see something on the screen and gives feedback to keep us happy and motivated.
A star ship
We need models which we could do with blender and import it into JME3.
First we need a space ship, so fire up your blender and do that. Well, I do not want to waste your time so I have one on my google drive to share with you. Doesn't mean you can not come up with our own model though.
If you have your model, place the blend file in the assets "Models" folder, then just right click your blender model in JME3 and select "Convert to j3o Binary".
After you took that hurdle with blender a factory to get the spatial in JME3 would be greate, it makes things simpler.
This implies that we have our models unter "Project Assets" (in real it the folder's name is "assets") and there in the folder "Models". I think needs not much explanation I let speak the code for itself. Of course you can do your factory better, more OO or not at all, I don't care.
Quick and dirty
So now we want to see that ship on the screen. For this we start with the visual system, I called it VisualAppState and it looks as follow
I assume your space ship is stored in the assets/Models/SpaceShip.j3o file.
If your model do have material then you will need a light, I took a directional light here.
And register that in the Main class as follow.
That's it, fire up your game with F6 and see. I agree there is not much about a ES so far but we are now prepared to do the first real entity system, actually we have to put some flesh on the VisualAppState and add some kind of a GameAppState. Stay tuned the next part will comming soon.