Scaffolding for dice game in place

main
Elijah C. Voigt 1 year ago
parent ae5bfb45d0
commit 59b767a48e

@ -1,7 +1,6 @@
use crate::prelude::*;
/// Menu Plugin; empty struct for Plugin impl
pub(crate) struct DicePlugin;
impl Plugin for DicePlugin {
@ -13,10 +12,32 @@ impl Plugin for DicePlugin {
/// Create UI for the Dice game at startup
fn init_dice_ui(mut commands: Commands) {
commands
.spawn((
GameChoice::Dice,
SetState(GameChoice::Dice),
SetState(MenuState::Closed),
))
.add(UiTitle { text: "Dice" });
.spawn((GameChoice::Dice, MenuState::Closed))
.add(UiContainer)
.with_children(|parent| {
parent
.spawn_empty()
.add(UiTitle { text: "Dice" })
.add(UiStyle(Style {
position_type: PositionType::Absolute,
top: Val::Px(0.0),
left: Val::Px(0.0),
margin: UiRect::all(Val::Px(5.0)),
border: UiRect::all(Val::Px(1.0)),
..default()
}));
parent
.spawn((SetState(GameChoice::None), SetState(MenuState::Open)))
.add(UiButton { label: "Menu" })
.add(UiStyle(Style {
position_type: PositionType::Absolute,
top: Val::Px(0.0),
right: Val::Px(0.0),
margin: UiRect::all(Val::Px(5.0)),
padding: UiRect::all(Val::Px(5.0)),
border: UiRect::all(Val::Px(1.0)),
..default()
}));
});
}

@ -11,6 +11,14 @@ impl Plugin for GamePlugin {
app.init_state::<GameChoice>();
app.add_systems(Startup, init_camera);
app.add_plugins(dice::DicePlugin);
app.add_systems(
Update,
manage_visibility::<GameChoice>.run_if(state_changed::<GameChoice>),
);
app.add_systems(
Update,
button_state_action::<GameChoice>.run_if(any_component_changed::<Interaction>),
);
}
}

@ -1,4 +1,4 @@
use crate::{manage_visibility, prelude::*};
use crate::prelude::*;
/// Menu Plugin; empty struct for Plugin impl
pub(crate) struct MenuPlugin;
@ -11,6 +11,10 @@ impl Plugin for MenuPlugin {
Update,
manage_visibility::<MenuState>.run_if(state_changed::<MenuState>),
);
app.add_systems(
Update,
button_state_action::<MenuState>.run_if(any_component_changed::<Interaction>),
);
}
}
@ -25,12 +29,14 @@ pub(crate) enum MenuState {
/// Initialize menu UI nodes at startup
fn init_menu_ui(mut commands: Commands) {
commands
.spawn(MenuState::Open)
.spawn((MenuState::Open, GameChoice::None))
.add(UiContainer)
.with_children(|parent| {
parent.spawn_empty().add(UiTitle {
text: "Game Jam Casino",
});
parent.spawn_empty().add(UiButton { label: "Dice" });
parent
.spawn(SetState(GameChoice::Dice))
.add(UiButton { label: "Dice" });
});
}

@ -1,3 +1,5 @@
pub(crate) use std::fmt::Debug;
/// Bevy imports
pub(crate) use bevy::{ecs::system::EntityCommand, prelude::*};
@ -5,7 +7,10 @@ pub(crate) use bevy::{ecs::system::EntityCommand, prelude::*};
pub(crate) use crate::ecs::schedule::common_conditions::*;
pub(crate) use crate::game::GameChoice;
pub(crate) use crate::manage_visibility;
pub(crate) use crate::menu::MenuState;
pub(crate) use crate::ui::button::UiButton;
pub(crate) use crate::ui::button_state_action;
pub(crate) use crate::ui::container::UiContainer;
pub(crate) use crate::ui::style::UiStyle;
pub(crate) use crate::ui::title::UiTitle;
pub(crate) use crate::ui::SetState;

@ -22,14 +22,19 @@ fn button_interaction(mut query: Query<(&mut BackgroundColor, &Interaction)>) {
/// Component for marking a button as a "Set State" action
#[derive(Component)]
pub(crate) struct SetState<S: States>(S);
pub(crate) struct SetState<S: States + Clone + Debug>(pub S);
fn button_state_actions<S: States>(
q: Query<(&mut S, &SetState<S>, &Interaction), Changed<Interaction>>,
s: Res<State<S>>,
n: Res<NextState<S>>,
pub(crate) fn button_state_action<S: States + Clone + Debug>(
q: Query<(&SetState<S>, &Interaction), Changed<Interaction>>,
mut n: ResMut<NextState<S>>,
) {
todo!("Change state when button is clicked")
q.iter()
.for_each(|(SetState(s), interaction)| match interaction {
Interaction::Pressed => {
n.set(s.clone());
}
_ => (),
});
}
pub(crate) mod button {
@ -113,8 +118,23 @@ pub(crate) mod container {
position_type: PositionType::Absolute,
..default()
},
border_color: BorderColor(Color::BLACK),
..default()
});
}
}
}
pub(crate) mod style {
use super::*;
pub(crate) struct UiStyle(pub Style);
impl EntityCommand for UiStyle {
fn apply(self, id: Entity, world: &mut World) {
if let Some(mut s) = world.entity_mut(id).get_mut::<Style>() {
*s = self.0.clone();
}
}
}
}

Loading…
Cancel
Save