C++ & Cocos2d-x, but I think the issue is language-independent.
I just recently finished up writing the foundational logic for my game. The way I've decided to keep my logic and my graphics decoupled is by simply having two base objects: LogicBody and GraphicsBody. Any object that needs to show up in the game needs to be comprised of both. So far, so good.
However, I am struggling with the initialization and interaction framework. I'll explain:
1) How should I make sure the logic body and graphics body stay together (in hopes of maintaining code readability).
Should they? Take the case of object Monster. It will need a logic body and a graphics body. But should both of these be contained in one overarching Monster class? Won't accessing either internal structure become unnecessarily verbose (i.e. Monster->getGraphicsBody() every time). However, this makes initialization easy:
Alternatively, it is possible to initialize both logic and graphics separately:
How can I then keep them abreast/aware of each other though? This will create clutter too, as I will likely need to call setters on each at initialization to make sure they reference each other:
2) What is the appropriate way to change graphics: polling, logic-driven, or both?
In the simplest of cases, updating graphics is as easy as polling direction, isMoving, health, etc. However, let's say that I want to play a particular animation when the Monster gets hit. The easiest way seems to be to call the "getHitAnimation" of the graphics body from within the logic body at the time of being hit. The alternative is to poll, but it seems that a certain point you will be polling twenty different variables, which has the potential to make the graphics implementation indecipherable without reference to the logic class anyways.
3) Should a logic body have reference to its graphics body at all?
I understand how clean it would be to simply have the graphics body reference the logic body for rendering purposes, in which case the logic body has no business with the graphics body. But what about the case where logic body A has met logic body B, and graphics bodies A and B need to interact. How do I let graphics bodies A and B know where to look for another?
I want to know the best way to implement these parallel systems with the least amount of coupling. Pseudo code welcomed.