refactor states; separate display and play states

bevy0.12
Elijah C. Voigt 2 years ago
parent a5bb2aa385
commit 8e0254c7e8

@ -21,18 +21,16 @@ impl Plugin for Display2dPlugin {
.add_systems( .add_systems(
Update, Update,
( (
menu::exit_to_menu.run_if(in_state(GameState::Display2d)),
move_piece move_piece
.run_if(in_state(GameState::Display2d)) .run_if(in_state(GameState::Play))
.run_if(in_state(DisplayState::Display2d))
.run_if(any_with_component::<game::Selected>()), .run_if(any_with_component::<game::Selected>()),
select.run_if(in_state(GameState::Display2d)).run_if( select
|buttons: Res<Input<MouseButton>>| -> bool { .run_if(in_state(GameState::Play))
.run_if(in_state(DisplayState::Display2d))
.run_if(|buttons: Res<Input<MouseButton>>| -> bool {
buttons.just_pressed(MouseButton::Left) buttons.just_pressed(MouseButton::Left)
}, }),
),
snap_back_cancel
.run_if(in_state(GameState::Display2d))
.run_if(any_component_removed::<game::Selected>()),
update_background.run_if(on_event::<WindowResized>()), update_background.run_if(on_event::<WindowResized>()),
set_transform set_transform
.after(game::update_board::<Display2d>) .after(game::update_board::<Display2d>)
@ -41,8 +39,8 @@ impl Plugin for Display2dPlugin {
set_tile_sprite.run_if(any_component_added::<game::Tile>), set_tile_sprite.run_if(any_component_added::<game::Tile>),
), ),
) )
.add_systems(OnEnter(GameState::Display2d), activate::<Display2d>) .add_systems(OnEnter(DisplayState::Display2d), activate::<Display2d>)
.add_systems(OnExit(GameState::Display2d), deactivate::<Display2d>); .add_systems(OnExit(DisplayState::Display2d), deactivate::<Display2d>);
} }
} }
@ -114,6 +112,7 @@ fn set_background(
translation: Vec3::NEG_Z, translation: Vec3::NEG_Z,
..default() ..default()
}, },
visibility: Visibility::Hidden,
..default() ..default()
}, },
)); ));
@ -329,19 +328,3 @@ fn move_piece(
} }
}) })
} }
fn snap_back_cancel(
mut events: RemovedComponents<game::Selected>,
query: Query<&game::BoardIndex, (With<game::Piece>, With<Display2d>)>,
mut move_events: EventWriter<game::Move>,
) {
events.iter().for_each(|entity| {
if let Ok(idx) = query.get(entity) {
move_events.send(game::Move {
from: idx.clone(),
to: Some(idx.clone()),
..default()
});
}
})
}

@ -31,14 +31,14 @@ impl Plugin for Display3dPlugin {
Update, Update,
( (
hydrate_camera, // TODO: add run_if... hydrate_camera, // TODO: add run_if...
menu::exit_to_menu.run_if(in_state(GameState::Display3d)),
set_piece_model.run_if(any_component_added::<Piece>), set_piece_model.run_if(any_component_added::<Piece>),
set_board_model.run_if(any_component_added::<game::BoardComponent>), set_board_model.run_if(any_component_added::<game::BoardComponent>),
set_tile_hitbox.run_if(any_component_added::<game::Tile>), set_tile_hitbox.run_if(any_component_added::<game::Tile>),
set_piece_position.run_if(any_component_changed::<BoardIndex>), set_piece_position.run_if(any_component_changed::<BoardIndex>),
set_piece_texture.run_if(any_component_changed::<Side>), set_piece_texture.run_if(any_component_changed::<Side>),
select select
.run_if(in_state(GameState::Display3d)) .run_if(in_state(GameState::Play))
.run_if(in_state(DisplayState::Display3d))
.run_if(on_event::<MouseButtonInput>()), .run_if(on_event::<MouseButtonInput>()),
pick_up.run_if(any_component_added::<game::Selected>), pick_up.run_if(any_component_added::<game::Selected>),
put_down.run_if(any_component_removed::<game::Selected>()), put_down.run_if(any_component_removed::<game::Selected>()),
@ -49,23 +49,26 @@ impl Plugin for Display3dPlugin {
( (
move_camera move_camera
.run_if(resource_exists::<debug::DebugEnabled>()) .run_if(resource_exists::<debug::DebugEnabled>())
.run_if(in_state(GameState::Display3d)) .run_if(in_state(GameState::Play))
.run_if(in_state(DisplayState::Display3d))
.run_if(on_event::<MouseMotion>()), .run_if(on_event::<MouseMotion>()),
gizmo_system gizmo_system
.run_if(resource_exists::<debug::DebugEnabled>()) .run_if(resource_exists::<debug::DebugEnabled>())
.run_if(in_state(GameState::Display3d)), .run_if(in_state(GameState::Play))
.run_if(in_state(DisplayState::Display3d)),
mouse_zoom mouse_zoom
.run_if(resource_exists::<debug::DebugEnabled>()) .run_if(resource_exists::<debug::DebugEnabled>())
.run_if(in_state(GameState::Display3d)) .run_if(in_state(GameState::Play))
.run_if(in_state(DisplayState::Display3d))
.run_if(on_event::<MouseWheel>()), .run_if(on_event::<MouseWheel>()),
selected_gizmo.run_if(resource_exists::<debug::DebugEnabled>()), selected_gizmo.run_if(resource_exists::<debug::DebugEnabled>()),
), ),
) )
.add_systems( .add_systems(
OnEnter(GameState::Display3d), OnEnter(DisplayState::Display3d),
(activate::<Display3d>, set_piece_texture), (activate::<Display3d>, set_piece_texture),
) )
.add_systems(OnExit(GameState::Display3d), deactivate::<Display3d>); .add_systems(OnExit(DisplayState::Display3d), deactivate::<Display3d>);
} }
} }
@ -199,11 +202,7 @@ fn hydrate_camera(
UiCameraConfig { show_ui: true }, UiCameraConfig { show_ui: true },
FogSettings { FogSettings {
color: Color::WHITE, color: Color::WHITE,
falloff: FogFalloff::from_visibility_colors( falloff: FogFalloff::from_visibility_colors(100.0, Color::NONE, Color::NONE),
100.0,
Color::ORANGE_RED,
Color::NONE,
),
..default() ..default()
}, },
)); ));

@ -10,6 +10,7 @@ impl Plugin for GamePlugin {
.add_systems( .add_systems(
Update, Update,
( (
menu::exit_to_menu.run_if(in_state(GameState::Play)),
update_board::<display2d::Display2d>.run_if(on_event::<Move>()), update_board::<display2d::Display2d>.run_if(on_event::<Move>()),
update_board::<display3d::Display3d>.run_if(on_event::<Move>()), update_board::<display3d::Display3d>.run_if(on_event::<Move>()),
set_side.run_if(on_event::<Move>()), // TODO: correct run_if? set_side.run_if(on_event::<Move>()), // TODO: correct run_if?

@ -29,6 +29,7 @@ fn main() {
let mut app = App::new(); let mut app = App::new();
app.add_state::<GameState>(); app.add_state::<GameState>();
app.add_state::<DisplayState>();
app.add_systems(Update, state.run_if(resource_changed::<State<GameState>>())); app.add_systems(Update, state.run_if(resource_changed::<State<GameState>>()));
app.add_systems(Update, loading.run_if(in_state(GameState::Loading))); app.add_systems(Update, loading.run_if(in_state(GameState::Loading)));
app.add_systems( app.add_systems(
@ -69,6 +70,12 @@ pub enum GameState {
Loading, Loading,
Menu, Menu,
Credits, Credits,
Play,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States, Component)]
pub enum DisplayState {
#[default]
Display2d, Display2d,
Display3d, Display3d,
} }
@ -111,14 +118,15 @@ fn loading(
/// System for printing the current state /// System for printing the current state
/// ///
/// Only runs when state is modified. /// Only runs when state is modified.
fn state(state: Res<State<GameState>>) { fn state(game_state: Res<State<GameState>>, display_state: Res<State<DisplayState>>) {
info!("State is {:?}", *state); info!("Game State is {:?}", *game_state);
info!("Display State is {:?}", *display_state);
} }
fn toggle_display_mode( fn toggle_display_mode(
mut events: EventReader<KeyboardInput>, mut events: EventReader<KeyboardInput>,
state: Res<State<GameState>>, display_state: Res<State<DisplayState>>,
mut next_state: ResMut<NextState<GameState>>, mut next_state: ResMut<NextState<DisplayState>>,
) { ) {
events events
.iter() .iter()
@ -127,10 +135,9 @@ fn toggle_display_mode(
key_code, state, .. key_code, state, ..
}| (*key_code, *state) == (Some(KeyCode::Space), ButtonState::Pressed), }| (*key_code, *state) == (Some(KeyCode::Space), ButtonState::Pressed),
) )
.for_each(|_| match state.get() { .for_each(|_| match display_state.get() {
GameState::Display2d => next_state.set(GameState::Display3d), DisplayState::Display2d => next_state.set(DisplayState::Display3d),
GameState::Display3d => next_state.set(GameState::Display2d), DisplayState::Display3d => next_state.set(DisplayState::Display2d),
_ => (),
}) })
} }

@ -33,18 +33,6 @@ struct Menu;
struct Quit; struct Quit;
fn init_menu_ui(mut commands: Commands) { fn init_menu_ui(mut commands: Commands) {
commands.spawn((
Menu,
Camera2dBundle {
camera: Camera {
is_active: false,
..default()
},
..default()
},
UiCameraConfig { show_ui: true },
));
commands commands
.spawn(( .spawn((
Menu, Menu,
@ -66,7 +54,8 @@ fn init_menu_ui(mut commands: Commands) {
.with_children(|parent| { .with_children(|parent| {
parent parent
.spawn(( .spawn((
GameState::Display3d, GameState::Play,
DisplayState::Display3d,
ButtonBundle { ButtonBundle {
style: Style { style: Style {
padding: UiRect::all(Val::Px(5.0)), padding: UiRect::all(Val::Px(5.0)),
@ -79,7 +68,8 @@ fn init_menu_ui(mut commands: Commands) {
)) ))
.with_children(|parent| { .with_children(|parent| {
parent.spawn(( parent.spawn((
GameState::Display3d, GameState::Play,
DisplayState::Display3d,
TextBundle::from_section( TextBundle::from_section(
"Start", "Start",
TextStyle { TextStyle {
@ -148,13 +138,17 @@ fn init_menu_ui(mut commands: Commands) {
} }
fn handle_menu_button( fn handle_menu_button(
events: Query<(&Interaction, &GameState), (With<Button>, Changed<Interaction>)>, events: Query<(&Interaction, &GameState, &DisplayState), (With<Button>, Changed<Interaction>)>,
mut next_state: ResMut<NextState<GameState>>, mut next_gs: ResMut<NextState<GameState>>,
mut next_ds: ResMut<NextState<DisplayState>>,
) { ) {
events events
.iter() .iter()
.filter(|(&interaction, _)| interaction == Interaction::Pressed) .filter(|(&interaction, ..)| interaction == Interaction::Pressed)
.for_each(|(_, s)| next_state.set(s.clone())) .for_each(|(.., gs, ds)| {
next_gs.set(gs.clone());
next_ds.set(ds.clone());
});
} }
fn handle_menu_quit( fn handle_menu_quit(

Loading…
Cancel
Save