diff --git a/src/game/dice.rs b/src/game/dice.rs index 2bbe3e7..76dfa08 100644 --- a/src/game/dice.rs +++ b/src/game/dice.rs @@ -1,6 +1,7 @@ use crate::prelude::*; /// Menu Plugin; empty struct for Plugin impl + pub(crate) struct DicePlugin; impl Plugin for DicePlugin { @@ -9,6 +10,13 @@ impl Plugin for DicePlugin { } } +/// Create UI for the Dice game at startup fn init_dice_ui(mut commands: Commands) { - todo!() + commands + .spawn(( + GameChoice::Dice, + SetState(GameChoice::Dice), + SetState(MenuState::Closed), + )) + .add(UiTitle { text: "Dice" }); } diff --git a/src/game/mod.rs b/src/game/mod.rs index d66954c..112d9e6 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -10,11 +10,12 @@ impl Plugin for GamePlugin { fn build(&self, app: &mut App) { app.init_state::(); app.add_systems(Startup, init_camera); + app.add_plugins(dice::DicePlugin); } } -#[derive(States, Debug, Clone, PartialEq, Eq, Hash, Default)] -enum GameChoice { +#[derive(States, Debug, Clone, PartialEq, Eq, Hash, Default, Component)] +pub(crate) enum GameChoice { #[default] None, Dice, diff --git a/src/menu.rs b/src/menu.rs index 4ee9374..586ae9d 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -16,7 +16,7 @@ impl Plugin for MenuPlugin { /// State tracking if the menu is open or closed #[derive(States, Debug, Clone, PartialEq, Eq, Hash, Default, Component)] -enum MenuState { +pub(crate) enum MenuState { #[default] Open, Closed, @@ -25,36 +25,11 @@ enum MenuState { /// Initialize menu UI nodes at startup fn init_menu_ui(mut commands: Commands) { commands - .spawn(( - MenuState::Open, - NodeBundle { - style: Style { - width: Val::Percent(100.0), - height: Val::Percent(100.0), - align_items: AlignItems::Center, - align_content: AlignContent::Center, - justify_items: JustifyItems::Center, - justify_content: JustifyContent::Center, - flex_direction: FlexDirection::Column, - ..default() - }, - ..default() - }, - )) + .spawn(MenuState::Open) + .add(UiContainer) .with_children(|parent| { - let title_text_style = TextStyle { - color: Color::BLACK, - font_size: 24.0, - ..default() - }; - - parent.spawn(TextBundle { - text: Text::from_section("Game Jam Casino", title_text_style), - style: Style { - margin: UiRect::all(Val::Px(5.0)), - ..default() - }, - ..default() + parent.spawn_empty().add(UiTitle { + text: "Game Jam Casino", }); parent.spawn_empty().add(UiButton { label: "Dice" }); }); diff --git a/src/prelude.rs b/src/prelude.rs index 1aa70bf..26f344c 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -3,5 +3,9 @@ pub(crate) use bevy::{ecs::system::EntityCommand, prelude::*}; /// Intra-project imports pub(crate) use crate::ecs::schedule::common_conditions::*; +pub(crate) use crate::game::GameChoice; pub(crate) use crate::manage_visibility; pub(crate) use crate::ui::button::UiButton; +pub(crate) use crate::ui::container::UiContainer; +pub(crate) use crate::ui::title::UiTitle; +pub(crate) use crate::ui::SetState; diff --git a/src/ui.rs b/src/ui.rs index a852732..3c64b3a 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -20,6 +20,18 @@ 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); + +fn button_state_actions( + q: Query<(&mut S, &SetState, &Interaction), Changed>, + s: Res>, + n: Res>, +) { + todo!("Change state when button is clicked") +} + pub(crate) mod button { use super::*; @@ -55,3 +67,54 @@ pub(crate) mod button { } } } + +pub(crate) mod title { + use super::*; + + pub(crate) struct UiTitle { + pub text: &'static str, + } + + impl EntityCommand for UiTitle { + fn apply(self, id: Entity, world: &mut World) { + let title_text_style = TextStyle { + color: Color::BLACK, + font_size: 24.0, + ..default() + }; + world.entity_mut(id).insert(TextBundle { + text: Text::from_section(self.text, title_text_style), + style: Style { + margin: UiRect::all(Val::Px(5.0)), + ..default() + }, + ..default() + }); + } + } +} + +pub(crate) mod container { + use super::*; + + pub(crate) struct UiContainer; + + impl EntityCommand for UiContainer { + fn apply(self, id: Entity, world: &mut World) { + world.entity_mut(id).insert(NodeBundle { + style: Style { + width: Val::Percent(100.0), + height: Val::Percent(100.0), + align_items: AlignItems::Center, + align_content: AlignContent::Center, + justify_items: JustifyItems::Center, + justify_content: JustifyContent::Center, + flex_direction: FlexDirection::Column, + position_type: PositionType::Absolute, + ..default() + }, + ..default() + }); + } + } +}