I'm wondering what is the best way to resolve complex game interactions each having their own graphical effect that takes some time to complete.
For example if you hit a monster, then they get knocked back and then cause an explosion that damages each adjacent tile. I'd want to calculate everything that happens here in one algorithm but then play out the different effects visually one by one.
I could store the original game state then calculate all the interactions and then send them back to be played one by one, but it seems a little bit unwieldy. How do other people do it?
Hi. My game is
www.dungeonbash.com and I implemented it the exact wrong way. For whatever reason , it was decided that the game model or engine would be turn based, and these events and their completion callbacks would be communicated to the presentaton layer as a stream of events and let the presentation layer queue them in an animation queue to play out.
I cant stress strongly enough that this was a
bad idea and if you have started down this path, pull our now before its too late.
It means you have a bunch of state that is turn based in your model and another bunch of state that is realtime in your presentation layer (making it much more complicated than it needs to be), and these two systems have to communicate in a more complex way than needs be, and there is quite an impedance mismatch there there as well between these two complex systems.
If you have a game with realtime animations and such then make
a real time model. Sure, the player and creatures might take turns, but in this context, they are real time turns where an action just happens to be walking from one tile to another, or making an attack or whatever.
If I wrote DungeonBash again, it would be so much easier to construct and maintain if I:
1) made each and every entity in the game model (creatures, explosions, whatever) its own
rigourous formal state machine that independently and asynchronously responds to events from other entities in real time. the fact that a lot of these events tend to happen on what the player perceives as 'turn' boundaries is irrelevent to the model. The model is a collection of indepdent entities that communicate with each other via realtime events no concept of turns at this level.
(consider the pros and cons of using an event bus which is another discussion in itself)
2) make the presentation layer as thin and dumb as possible. Each presenter just reflects the state of the entity it represents in realtime with as little state in itself as you can get away with. None would be ideal.
3) state transitions in the entities in your model that require time to play out are timed in the model - it doesnt require a callback from the UI to tell it that an animation has completed. Rather, the UI dumbly mirrors the state of the entity and configures the animations to complete at the specified time. i.e. the model fires an event for its UI that says - I have transitioned to this state, and I will be in it for exactly 800ms. The UI then dumbly takes that event, and starts playing the appropriate animation to complete in 800ms