Sending Messages of Intent
Explore how to implement an intent-based messaging system in Rust using Legion ECS to manage entity movement in a turn-based game. Learn to create and process movement intents separately to simplify player and monster actions while supporting additional gameplay mechanics like stuns. This lesson guides you through building and integrating message components, handling movement commands, and refining player input for efficient game turn management.
Currently, the PlayerInput and RandomMovement systems load the map and check for tile access. As we add more gameplay elements, we needn’t remember to update both of them and any other system that can produce movement. Instead, we can share a lot of the functionality by having systems that produce movement produce a message indicating that an entity intends to move.
A later system can then process all movement requests and apply them if they’re possible. This is quite powerful. Suppose we add a stun mechanic—stunned monsters can’t move. Our system that handles stuns could remove movement intention from any entity, and we wouldn’t have to change a line of the other systems.
Messages as entities
Games often feature elaborate messaging systems. Message queueing and efficient message handling could easily fill its own book. We can build a simple, efficient messaging system inside an ECS by treating each message as its own entity. We’ll need a new component type for the message. For this, we create two new component structures in components.rs:
These are just like the ...