I am programming a turn-based game, 2D tile-based, overhead view. I have an update function and a render function running in separate threads. When it's the player's turn, the player can e.g.
- select a target tile from the map
- then a popup menu with the available interactions appears
- player selects interaction and the interaction gets executed
One tedious way is the following:
GameLoopUpdate(): switch( interaction_state ) case tile_selected: lookup_interactions_for_selected_tile() break case tile_and_interaction_selected() execute_interaction_at_tile() break default: break
Of course the above does not scale when the interactions get complex.
Conceptually, I would like it to be more like:
KeyListener(): if tile is clicked set_tile_selected() GameLoopUpdate(): if tile is selected: interaction = spawn_thread_interaction_selection() execute_interaction_at_tile( interaction )
where the spawned thread would somehow override the input listeners AND the rendering. But this looks bad as well.
Another idea (I'm starting to consider implementing this) is the following:
GameLoopUpdate(): if it's player's turn set next state as PlayerState_Base else run ai turn PlayerState_Base(): if tile is selected set next state as PlayerState_TileSelected PlayerState_TileSelected(): if interaction is selected execute interaction set player's turn as done
where each state above would have dedicated key listener and rendering functions
Any other ideas?