bevy_gearbox
A Bevy plugin for managing entity states with support for hierarchical state machines.
Why Use This?
Perfect for game entities that need complex state management:
- Characters with abilities that can interrupt normal behavior
- AI systems with layered decision-making
- Interactive objects with multiple operational modes
Quick Example
use *;
use *;
;
;
;
// Define the state machine
state_machine!;
Core Concepts
State Machine Definition (generated by state_machine!())
- Owner Component: A Bevy component that identifies the state machine
- State Components: Components representing individual states (first state must implement
Default) state_machine!Macro: Connects the owner to its possible states and generates:- A
<OwnerName>StateEnumto track current state - A
<OwnerName>Pluginwith necessary systems
- A
State Transitions
Use the StateTransitionCommandsExt trait:
commands.entity.transition;
The generated systems handle removing old state components and adding new ones automatically.
Hierarchical State Machines
For advanced use cases, bevy_gearbox supports parent-child state machine relationships using special states:
RestingState: Default inactive state for child state machines (supports "blockers")WorkingState: Active state when controlled by parentInChildSMState(Entity): Parent state when delegating to childFinishedChildSMState(Entity): Parent state when child completesFizzledState: Child state when parent exits prematurely
Setup
- Add to your project:
- Add plugins to your app:
use *;
use GearboxPlugin;
- Spawn entities:
// Starts in IdleState automatically
commands.spawn;
Querying States
// Query by state component
// Query by state enum
Advanced Features
- Hierarchical State Machines: Parent state machines can delegate control to child state machines
- State Blocking: Prevent transitions based on conditions (e.g., cooldowns, resource requirements)
- Automatic State Management: Handle complex parent-child state transitions automatically
TODO
- Add comprehensive HSM examples (character abilities, AI behavior trees)
- Add state blocking examples (cooldowns, resource management)
- Add complex nested state machine example