Hello all,
I'm currently making a 2D game using libgdx. I have only one GameObject class that is used to instantiate every object in the game, it is kind of like entity component system, but components have variables as well as functions in them, so it's not 100% ECS.
My PlayerStateBehaviour class works okay but it is getting too fat because I handle all state changes as well as animation logic in there. That's why I want to decouple some stuff just for clarity of code. But of course the components and connected so they have to communicate in some way. I've read a lot of articles online and there are two widely used ways for small games. One is to just hold a pointer to the other component I need - I can put it as an argument to the constructor. I tried that way but I think it sucks because I can't figure out a way to sort all the dependencies automatically and the other reason is that I need multiple constructors for every different combination that I want. (am I missing something here?)
The other way is messaging. The simplest pattern that is already in java is Observer and this is what I decided to use.
1. I made every single behaviour/component an Observer as well as an Observable. (Is this ok?)
2.My idea is that I want every component to be able to exist on its own, without being dependent on another component. (I know messaging is still a sort of coupling, but at least every object can exist on its own.)
So what I want to do is just limit the PlayerStateComponent to transitioning between different states and sending messages about every state change, nothing else. And then the AnimationBehaviour will catch the message (STATE_CHANGE_ATTACK or something like that) and change the proper animation. (is it cool?)
3. Should I handle every single thing only by messages, or is it okay to sometimes hold pointers to other components, I really like the components to be directly referring to each other but the problem is that I can't automate the process because I don't know how to sort the dependencies....
4. How exactly should I automate the process of finding observers. Should add every component as an observer to everyone else, or just add observer when it's actually needed? The first one is easy to code, the second one, I can't come up with a way of doing it, so I chose the first way.
Can you give me some ideas, or at least tell me if I'm on the right track?