I am new to game development, so please be gentle with me.
I digged through the Tuts+ 2D space shooter tutorials and tried to change the structure of this game to suit more the entity component system. (Just for your memory: they used enemies called Seekers and Wanderers and put one control on each of them)
I decided to change the approach to a more complex one: my Seekers consist of various controls (EnemyControl to be shootable, LootControl to let them drop some stuff, ExplosionControl to let them do some fancy explosion on death and the SeekerControl from the original)
My question now is: What is the best practice to put the desired behaviour of entities together?
Let me give an example to become more clear: ExplosionControl has a public method which does the fancy boom. This method is called in the EntityControl.die() method. I would like to separate these two parts, so that the ExplosionControl.explode() gets called automatically on the spatial.detachFromParent()… like it’s possible on the AppState.detach().
If I put my funcionality into the controlUpdate() method and check if the entity is still alive, the effect won’t happen, because it will all happen in one update cycle, where the entity is still alive.
I hope I could explain my problem in a more ore less understandable way.
I think you’re mixing up the Entity system approach with its “Components” and jME “Controls”. If you use an ES a Control would read the Components of an Entity to decide what to do with the actual 3D object on screen.
Okay, then let me explain how I understood this: I have some spatial S (my entity) to which I add the desired behaviour (the Controls). Some controls can be added to any other spatial, but will cause the same behauviour then.
In my example this would be the ExplosionControl. Any spatial to which this control is added will explode when… and this is my problem… when? The only possibility I see, is to call the .explode() method in some other control, whether its some generic EnemyControl or maybe the PlayerControl.
Can I separate this behaviour so that the explosion will happen at any cost on the spatials detachment?
Just make an explosion when you detach the spatial? You seem to lack basic control over what happens in your application… You could e.g. have an AppState that tracks the overall progress in your game and decides when to make explosions where and when to kill NPCs or whatever.
Then I got this whole thing wrong. From my understanding (at least from what I read in the documentation section) I am supposed to put several different behaviour to the various spatials via the Controls (inherited from AbstractControl). When some event happens during my running AppState (e.g. some bullet collision in case of the tutorial) these methods get called.
Won’t this lead to the AppStates being some blow, which we wanted to avoid?
Thinking of some enemy spawning: there’s some Node (enemyNode) to which we add some EnemySpawnControl, which keeps spawning Spatials, which have EnemyControls with them. Did I get the primary approach right?
It depends on what you want to do, this is not a game maker with rigid “rules”, its a programming framework. Technically you don’t need to do anything with Controls if you don’t want but they certainly make your code cleaner. An AppState is for global things that are not logically attached to a Spatial, I’d consider general game flows like spawning objects etc. that. A Control is for things specific to a Spatial, for example calling the correct animation and things like that. The over-arching logic is defined by your decisions however.
Thanks a lot for your answers. It helped me a bit more how to handle this framework. I suppose my problem was the fixed view on the states and controls. I thought of the AppStates as some higher level containers for application wide things (such as mentioned in the tutorials e.g. menu / game cycle)
So I would use the AppStates for possible different levels/stages, too, and connect the corresponding spatials such as scenes and objects in this levels/stages to them?
Yeah, you can just keep a list of “NPCs” in your AppState (a list of Spatials) and manage them from there, same with your “Level”. Thats what “game cycle” means in this case. After a while you’ll come up with more ways to intelligently use the building blocks. Just don’t fall into the trap that many people fall into and try and use the engine classes for things that your application should do. E.g. “there should be a list of all spatials I added in this AppState”. Just make your own list.