82 lines
2.3 KiB
Rust
82 lines
2.3 KiB
Rust
use bevy::prelude::*;
|
|
|
|
pub struct TurnSystemPlugin;
|
|
|
|
impl Plugin for TurnSystemPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.init_state::<TurnState>()
|
|
.add_systems(OnEnter(TurnState::PreEffectProcess), pre_effect_setup)
|
|
.add_systems(OnEnter(TurnState::UnitProcess), unit_setup)
|
|
.add_systems(OnEnter(TurnState::PostEffectProcess), post_effect_setup)
|
|
.add_systems(
|
|
Update,
|
|
process_busy_turnables.run_if(not(in_state(TurnState::Turn))),
|
|
);
|
|
}
|
|
}
|
|
|
|
#[derive(Component)]
|
|
pub struct TurnBusy;
|
|
|
|
#[derive(Component)]
|
|
pub struct TurnPreEffect;
|
|
|
|
#[derive(Component)]
|
|
pub struct TurnUnit;
|
|
|
|
#[derive(Component)]
|
|
pub struct TurnPostEffect;
|
|
|
|
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
|
|
pub enum TurnState {
|
|
#[default]
|
|
Turn,
|
|
PreEffectProcess,
|
|
UnitProcess,
|
|
PostEffectProcess,
|
|
}
|
|
|
|
fn pre_effect_setup(mut commands: Commands, query: Query<Entity, With<TurnPreEffect>>) {
|
|
for effect in query {
|
|
commands.entity(effect).insert(TurnBusy);
|
|
}
|
|
}
|
|
fn unit_setup(mut commands: Commands, query: Query<Entity, With<TurnUnit>>) {
|
|
for unit in query {
|
|
commands.entity(unit).insert(TurnBusy);
|
|
}
|
|
}
|
|
fn post_effect_setup(mut commands: Commands, query: Query<Entity, With<TurnPostEffect>>) {
|
|
for effect in query {
|
|
commands.entity(effect).insert(TurnBusy);
|
|
}
|
|
}
|
|
|
|
pub fn try_confirm_turn(mut commands: Commands, turn_state: Res<State<TurnState>>) {
|
|
if let TurnState::Turn = turn_state.get() {
|
|
commands.set_state(TurnState::PreEffectProcess);
|
|
}
|
|
}
|
|
|
|
fn process_busy_turnables(mut commands: Commands, query: Query<(Entity, &TurnBusy)>) {
|
|
if query.iter().len() == 0 {
|
|
commands.run_system_cached(try_advance);
|
|
}
|
|
}
|
|
|
|
fn try_advance(mut commands: Commands, state: Res<State<TurnState>>) {
|
|
match state.get() {
|
|
TurnState::Turn => {
|
|
commands.set_state(TurnState::PreEffectProcess);
|
|
}
|
|
TurnState::PreEffectProcess => {
|
|
commands.set_state(TurnState::UnitProcess);
|
|
}
|
|
TurnState::UnitProcess => {
|
|
commands.set_state(TurnState::PostEffectProcess);
|
|
}
|
|
TurnState::PostEffectProcess => {
|
|
commands.set_state(TurnState::Turn);
|
|
}
|
|
}
|
|
}
|