States
Every game will consist of multiple states (phases); every state defines a single part of your game, like "Waiting for more players", "Starting", "In game", "Ending", etc.
A state will always be its own class.
@GlobalMinigameState
public class MyState extends VitalGlobalMinigameState {
}
Every state will essentially be a listener that will only receive events when the state is active.
@GlobalMinigameState
public class MyState extends VitalGlobalMinigameState {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
// …
}
}
There can only be one state active at a given time.
Since we are building a global game, the state is handled globally by the VitalGlobalMinigameService.
Use this as a bean to switch between states, Vital will automatically handle event registration and all of that complicated stuff for you.
vitalGlobalMinigameService.setState(MyState.class);
When switching states, Vital automatically unregisters all of your event handlers from your old states.
Your new state is activated, and all of your event handlers for your new state will be registered and can now receive events.
If you are building a global game, you might want to store all of your game-related data, like player kills, stats, etc. in a separate Spring bean, or a service class that you can inject everywhere.
Lifecycles
| Lifecycle | Usage |
|---|---|
onEnable() | Perform an action when this state is enabled. |
onDisable() | Perform an action when this state is disabled. |