Compare commits
3 Commits
b2ca08ce3f
...
ae5bfb45d0
| Author | SHA1 | Date |
|---|---|---|
|
|
ae5bfb45d0 | 1 year ago |
|
|
3b7f626394 | 1 year ago |
|
|
520df6405e | 1 year ago |
@ -0,0 +1,19 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
/// Module mirroring bevy::ecs::schedule
|
||||
pub(crate) mod schedule {
|
||||
use super::*;
|
||||
|
||||
/// Module mirroring bevy::ecs::schedule::common_conditions
|
||||
pub(crate) mod common_conditions {
|
||||
use super::*;
|
||||
|
||||
/// Missing (in my opinion) condition for checking if any of a given component have changed
|
||||
pub(crate) fn any_component_changed<T>(q: Query<Entity, Changed<T>>) -> bool
|
||||
where
|
||||
T: Component,
|
||||
{
|
||||
!q.is_empty()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
/// Menu Plugin; empty struct for Plugin impl
|
||||
|
||||
pub(crate) struct DicePlugin;
|
||||
|
||||
impl Plugin for DicePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, init_dice_ui);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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" });
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
/// Dice game module
|
||||
pub(crate) mod dice;
|
||||
|
||||
/// Menu Plugin; empty struct for Plugin impl
|
||||
pub(crate) struct GamePlugin;
|
||||
|
||||
impl Plugin for GamePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_state::<GameChoice>();
|
||||
app.add_systems(Startup, init_camera);
|
||||
app.add_plugins(dice::DicePlugin);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(States, Debug, Clone, PartialEq, Eq, Hash, Default, Component)]
|
||||
pub(crate) enum GameChoice {
|
||||
#[default]
|
||||
None,
|
||||
Dice,
|
||||
}
|
||||
|
||||
/// Main game camera
|
||||
fn init_camera(mut commands: Commands) {
|
||||
commands.spawn(Camera2dBundle {
|
||||
camera: Camera {
|
||||
clear_color: ClearColorConfig::Custom(Color::WHITE),
|
||||
..default()
|
||||
},
|
||||
..default()
|
||||
});
|
||||
}
|
||||
@ -1,7 +1,44 @@
|
||||
// TODO: Should we use wee_alloc?
|
||||
// Use `wee_alloc` as the global allocator.
|
||||
#[global_allocator]
|
||||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
||||
extern crate wee_alloc;
|
||||
|
||||
/// Game Menu
|
||||
pub(crate) mod menu;
|
||||
|
||||
/// Generic UI elements
|
||||
pub(crate) mod ui;
|
||||
|
||||
/// Module mirroring bevy::ecs
|
||||
pub(crate) mod ecs;
|
||||
|
||||
/// Imports used across the project
|
||||
pub(crate) mod prelude;
|
||||
|
||||
pub(crate) mod game;
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.run();
|
||||
let mut app = App::new();
|
||||
app.add_plugins(DefaultPlugins);
|
||||
app.add_plugins(menu::MenuPlugin);
|
||||
app.add_plugins(ui::UiPlugin);
|
||||
app.add_plugins(game::GamePlugin);
|
||||
app.run();
|
||||
}
|
||||
|
||||
/// Hide entities with a given component
|
||||
pub(crate) fn manage_visibility<SC: States + Component>(
|
||||
mut q: Query<(&mut Visibility, &SC)>,
|
||||
r: Res<State<SC>>,
|
||||
) {
|
||||
q.iter_mut().for_each(|(mut v, sc)| {
|
||||
if *sc == *r.get() {
|
||||
*v = Visibility::Inherited;
|
||||
} else {
|
||||
*v = Visibility::Hidden;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -0,0 +1,36 @@
|
||||
use crate::{manage_visibility, prelude::*};
|
||||
|
||||
/// Menu Plugin; empty struct for Plugin impl
|
||||
pub(crate) struct MenuPlugin;
|
||||
|
||||
impl Plugin for MenuPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_state::<MenuState>();
|
||||
app.add_systems(Startup, init_menu_ui);
|
||||
app.add_systems(
|
||||
Update,
|
||||
manage_visibility::<MenuState>.run_if(state_changed::<MenuState>),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// State tracking if the menu is open or closed
|
||||
#[derive(States, Debug, Clone, PartialEq, Eq, Hash, Default, Component)]
|
||||
pub(crate) enum MenuState {
|
||||
#[default]
|
||||
Open,
|
||||
Closed,
|
||||
}
|
||||
|
||||
/// Initialize menu UI nodes at startup
|
||||
fn init_menu_ui(mut commands: Commands) {
|
||||
commands
|
||||
.spawn(MenuState::Open)
|
||||
.add(UiContainer)
|
||||
.with_children(|parent| {
|
||||
parent.spawn_empty().add(UiTitle {
|
||||
text: "Game Jam Casino",
|
||||
});
|
||||
parent.spawn_empty().add(UiButton { label: "Dice" });
|
||||
});
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
/// Bevy imports
|
||||
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;
|
||||
@ -0,0 +1,120 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
/// Menu Plugin; empty struct for Plugin impl
|
||||
pub(crate) struct UiPlugin;
|
||||
|
||||
impl Plugin for UiPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(
|
||||
Update,
|
||||
button_interaction.run_if(any_component_changed::<Interaction>),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn button_interaction(mut query: Query<(&mut BackgroundColor, &Interaction)>) {
|
||||
query.iter_mut().for_each(|(mut bg, i)| match i {
|
||||
Interaction::Hovered => bg.0 = Color::ORANGE,
|
||||
Interaction::Pressed => bg.0 = Color::GREEN,
|
||||
Interaction::None => bg.0 = Color::WHITE,
|
||||
});
|
||||
}
|
||||
|
||||
/// Component for marking a button as a "Set State" action
|
||||
#[derive(Component)]
|
||||
pub(crate) struct SetState<S: States>(S);
|
||||
|
||||
fn button_state_actions<S: States>(
|
||||
q: Query<(&mut S, &SetState<S>, &Interaction), Changed<Interaction>>,
|
||||
s: Res<State<S>>,
|
||||
n: Res<NextState<S>>,
|
||||
) {
|
||||
todo!("Change state when button is clicked")
|
||||
}
|
||||
|
||||
pub(crate) mod button {
|
||||
use super::*;
|
||||
|
||||
pub(crate) struct UiButton {
|
||||
pub label: &'static str,
|
||||
}
|
||||
|
||||
impl EntityCommand for UiButton {
|
||||
fn apply(self, id: Entity, world: &mut World) {
|
||||
let button_text_style = TextStyle {
|
||||
color: Color::BLACK,
|
||||
font_size: 16.0,
|
||||
..default()
|
||||
};
|
||||
world
|
||||
.entity_mut(id)
|
||||
.insert(ButtonBundle {
|
||||
style: Style {
|
||||
margin: UiRect::all(Val::Px(5.0)),
|
||||
padding: UiRect::all(Val::Px(5.0)),
|
||||
border: UiRect::all(Val::Px(1.0)),
|
||||
..default()
|
||||
},
|
||||
border_color: BorderColor(Color::BLACK),
|
||||
..default()
|
||||
})
|
||||
.with_children(|parent| {
|
||||
parent.spawn(TextBundle {
|
||||
text: Text::from_section(self.label, button_text_style),
|
||||
..default()
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue