States
Description
States represent discrete units of logic designed to work with a StateMachine.
Each state encapsulates behavior that is active only while the state is current, allowing complex logic to be decomposed into small, focused, and reusable components.
States:
define behavior for a specific phase or mode
are managed exclusively by a
StateMachinefollow a unified lifecycle (
Enter / Exit / Update)do not control transitions directly unless explicitly required
This approach ensures clear separation of responsibilities, predictable execution flow, and high extensibility.
State Lifecycle
Each state follows a standardized lifecycle:
Enter — called once when the state becomes active
Update — called continuously while the state is active
Exit — called once when the state is deactivated
This lifecycle guarantees consistent behavior across all state implementations.
Usage
States are created and registered in the Zenject container via Installers and are instantiated and managed by their corresponding StateMachine.
Adding new states does not require modifying existing ones, allowing the system to scale without increasing complexity.
Built-in State Implementations
The template includes the following state implementations for specific entities:
1. Game States
Defined states:
BootstrapState — Initializes core game systems and services; sets up the DI container and prepares the game for further loading.
LoadProgressState — Loads player progress, saved data, and persistent game state before transitioning to main gameplay or menu.
2. Gameplay States
Defined states:
GameplayBootstrapState — Initializes gameplay systems and sets up the scene at the start of a level.
InitGameplayState — Prepares player, enemies, and environment; loads required assets and configurations.
LaunchState — Starts actual gameplay; triggers player control and game mechanics.
TutorialState — Guides the player through tutorials or introductory instructions.
GameLoopState — Main gameplay loop; handles ongoing game logic, player interactions, and AI behavior.
WinGameState — Triggered when victory conditions are met; handles win animations, rewards, and progression.
LoseGameState — Triggered when defeat conditions are met; handles failure animations, retry logic, or game over UI.
3. Player States
Defined states:
PlayerIdleState — Player stands still; no movement or actions performed.
PlayerRunState — Player moves or runs; handles input-driven locomotion.
PlayerFinishState — Player has completed the level or objective; triggers victory animations or end-of-level logic.
PlayerDeadState — Player has died; triggers death animations, disables input, and may initiate respawn or game over.
4. Tentacle States
Defined states:
TentacleIdleState — Tentacle is idle; not attacking or moving.
TentacleAttackState — Tentacle performs attacks or offensive actions against the player.
TentacleDeadState — Tentacle is destroyed or defeated; triggers death animations and disables interactions.
5. Enemy States
Defined states:
EnemyIdleState — Enemy stands still; minimal or passive behavior.
EnemyInactiveState — Enemy is fully inactive or disabled, e.g., before spawn or after death.
EnemyHitState — Enemy takes damage; plays hit animations and checks for death.
EnemyChaseState — Enemy pursues the player, navigating obstacles to reach the target.
EnemyAttackState — Enemy attacks the player using abilities or weapons when in range.
EnemyWanderState — Enemy moves randomly or along a patrol route when the player is not visible.
EnemyDeadState — Enemy is dead; triggers death animations, disables components, and may drop loot.
How to Add a New Entity State
Create a new class and implement the
IStateinterface.Optionally, if the state requires per-frame updates, implement
IUpdatableState.
Register the new state in the appropriate
StateMachinevia an Installer.
References
See the StateMachine section for details on working with state transitions.
See the Installers section for details on registering states in the Zenject container.
Last updated