[−][src]Module rust_action_heroes::system
Putting the "S" in ECS
Systems describe how entities with certain components interact.
Take for example you have two entities:
E1:
Component::Squishy: Flag
Component::Vulnerable: Bool
...
E2:
Component::Squisher: Flag
Component::SquishingMood: Bool
...
These two entities might interact via a SquishSystem
which says:
SquishSystem(
(squishies, squishers, vulnerables, squishingmood): (Squishy, Squisher, Vulnerable, SquishingMood)
):
for (squishy, vulnerable) in (squshies, vulnerables):
if vulnerable:
for (squisher, squishingmood) in (squishers, squishingmoods):
if squishingmood:
squisher.squish(squishy)
This collects all entities with the Squishy
, Vulnerable
, Squisher
, and SquishingMood
components and conveniently allows us to iterate over any slice of those four.
In the above example we are iterating over all entities with both the squishy
and
vulnerable
component, then if vulnerable
is true, we iterate over all squisher
and
squishingmood
entities and do some action.
Event Channels
One pattern I use a lot is to have a system either listening or writing an Event Channel. This means that systems are more interrupt driven as opposed to real-time.
Events don't scale to all problems, even in this game we have some systems which are real-time, but in a turn-based-like game events are very useful for minimizing unecessary compute and reasoning about what a sytem will do.
Modules
door | Doors are opened with keys |
grab | How does the Grabaron work? |
grid | This grid ain't free! |
level | How we progress from level N to N+1 |
locks | How to open locked doors |
move_sound | "Bloop" |
movement_solver | Movement was so complicated it has two systems |
mover | Movement was so complicated it has two systems |
process_input | Nobody has time to process raw keyboard input |
sprites | There's a system for managing sprites! |
switches | How the game knows if all the switches are down |