Designing a Movement Transaction System for a Sokoban Game
Context My multiplayer game Lights Out is based on a 2D grid. Entities can only ever be in exactly one grid tile. This makes the rule evaluation really simple and understandable. However, it doesn't really feel nice to play (which you know if you've ever played any of the PuzzleScript games). At the same time, the more content is in the game, the more complex and arbitrary the game rules become. I therefore introduced the Movement Transaction System into the code base to deal with this. This includes two sides: - The gameplay code on server side deals with transactions. This bundles all movement code (including rule evaluation) into a single system. - The visualization & prediction code on client side deals with visual interpolation for moves (introducing some juice into the gameplay feel), based on the transactions managed by the server. The Transaction A single transaction includes the movement delta, a list of entities that it has affected and some flags. A transaction then undergoes several stages: - Queued : Gameplay code has requested an entity to move - Issued : The visual interpolation for the transaction has started in the client, but the entities have not been moved from a gameplay perspective - Committed : The entities have now been moved onto their new tiles, the visual interpolation is finishing - Aborted : The transaction couldn't be committed as it would've violated gameplay rules. Visual interpolation is reversed. The Visual Interpolation Whenever a transaction is issued on server-side, the server tells the clients to start a visual interpolation based on the transaction. This information includes the desired duration of the interpolation, as well as some flags (like whether to use acceleration or do a linear interpolation). The client then updates the visual interpolation every frame, until the transaction is either aborted or the target position has been reached. Simplifying Gameplay Code This new system has made the gameplay code much simpler. I c