State
States are a crucial part of any multiplayer game, storing data used during runtime. This State is shared between the server and the clients and is used to synchronize the game state between all clients. The advantage of storing the game state on the server being, that clients reconnecting, even during a level, poses no problem.
State classes implement the Schema class from Colyseus, which uses delta encoding to only send state changes to the clients. This reduces the bandwidth and therefore can reduce latency.
All variables that are marked as private are not shared with the clients and are only used by the server to store internal data. In the actual implementation, these variables are not really private, but don't have a @type annotation and are therefore not shared with the clients. For more information about the @type annotation and schemas, read the Colyseus Schema documentation.
The live state of all running games can be accessed under following url: http://localhost:2567/colyseus. Note that the address and port may vary depending on the configuration.
Game State
The game state is the central data structure of the game room. It holds data about the current game save, the current level, and the connected clients.
The following diagram shows the structure of the game state.
Client State
When a client connects to the server, a new client state is created and stored in the game state. If the client disconnects, the client state is removed from the game state.
The client state holds data about the client which persists over multiple levels. And is used to generate the level state.
Level Metadata
Each level has metadata that describes the level. This information is present in the game state, which holds a list of all available levels, with their metadata.
roundTileIndices and excludedCollisionTileIndices provide information for the client's tilemap loader about round corner tiles and non-collider tiles.
The biome property is used to determin which assets should be loaded for the level. The thumbnail is used in the level selection screen to show a preview of the level.
Level State
Overview
The level state holds the current state of the level. This state consists of metadata about the level and the entities. The metadata is described here.
Entities are stored in maps, where the key is the entity's id. The id is globally unique (within the current level). The id must be stored as a string, because Colyseus only supports string keys for maps.
Entities
Entities are game objects that have a certain functionality, such as moving or pressing buttons. This functionality needs to be synchronized with all clients, which is why the entities are stored in the level state. Each entity type extends the EntityState class, which holds fields all entities implement. Entities may have additional properties specific to their functionality.
Player
The player entity represents the player in the game. It has a session id, which is the id of the client that controls the player and can be used by the client to identify clients own player.
Players can vote for a gravity shift. If this property is true on both players, the gravity will be shifted and the property will be reset.
The receiving damage flag is used to indicate that the player is currently receiving damage and is used to sync animations between clients. The animation state is used to determine the current animation of the player and is - just as the damage flag - used to sync animations between clients.
If a player disconnects, the connected flag will be set to false and any new player that connects will take over the player entity of the disconnected player.
Box
The box entity represents a box in the game and has no additional properties.
Pressure Plate
The pressure plate entity represents a pressure plate in the game. It has a property that is true if the pressure plate is activated. Pressure plates can be connected to doors and are used to open them.
Door
The door entity represents a door in the game and is either a trapdoor or a door with a certain height. The door height is shortened to H3, H4 and H5.
Doors can be connected to pressure plates and will open if these pressure plates are activated. The logic type determines is all connected pressure plates must be activated or if only one of them is enough to open the door.
Collectible
The collectible entity represents a collectible in the game. It has a type and a collected property that is true if the collectible was collected. If levelEnding is true, the level will end upon picking up the collectible.
Breakable Floor
Breakable floors are entities that can be broken by the player when they are standing on them.
Storyteller
The storyteller entity represents a storyteller in the game. It has a list of story lines that are displayed in the game. Based on the current story line index, the current story line is displayed. If the index is out of bounds, no story line is displayed.