I'm having a code design problem involving synchronization between the server and clients. I have a class
GameWorld that handles all of the simulation details (positions, velocities, projectiles, what have you) that exists in two places:
- on the server, it is contained in the class
GameRoomwhich handles all of the players connected to a specific lobby, reads and sends messages.
- on the client, it's in the class
GameplayGameState(using the stack of states pattern).
The problem is that i want to include the simulation code in this single
GameWorld class to avoid the various bugs involved in duplicating the code in both sides (maybe i change something in one but forget to do the same in the other). However, there is one thing that needs to change between the two: on the server,
GameWorld needs to notify
GameRoom somehow about events (like "this player died", "this player shot in that direction"), so that
GameRoom can then broadcast the event to the clients. On the client, however, i need no such thing, but i do need a way for
GameWorld to handle similar events for other reasons (for example, a dying player doesn't need to notify any clients but it does need to play a sound or display a message or something).
Programmatically, how would I go about implementing different behaviors for different sides? Keep in mind the code for GameWorld is separate and can't have ANY knowledge of its outside overlords. How can two different programs get the information they need without
GameWorld knowing what to give them? keep in mind polling is off the table, because it would be ridiculously inefficient to scan the GameWorld every tick for changes, keeping the previous state for reference. GameWorld itself needs to generate the events.