Compare commits

..

3 Commits

Author SHA1 Message Date
Elijah C. Voigt ae5bfb45d0 Mid set-state button action implementation 1 year ago
Elijah C. Voigt 3b7f626394 Adding some foundational UI elements 1 year ago
Elijah C. Voigt 520df6405e Got web to work 1 year ago

59
Cargo.lock generated

@ -84,7 +84,7 @@ version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 1.0.0",
"getrandom", "getrandom",
"once_cell", "once_cell",
"version_check", "version_check",
@ -1094,7 +1094,7 @@ dependencies = [
"arrayref", "arrayref",
"arrayvec", "arrayvec",
"cc", "cc",
"cfg-if", "cfg-if 1.0.0",
"constant_time_eq", "constant_time_eq",
] ]
@ -1233,6 +1233,12 @@ dependencies = [
"nom", "nom",
] ]
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -1328,7 +1334,7 @@ version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 1.0.0",
"wasm-bindgen", "wasm-bindgen",
] ]
@ -1454,7 +1460,7 @@ version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 1.0.0",
] ]
[[package]] [[package]]
@ -1727,6 +1733,7 @@ name = "game-jam-casino"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bevy", "bevy",
"wee_alloc",
] ]
[[package]] [[package]]
@ -1745,7 +1752,7 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 1.0.0",
"js-sys", "js-sys",
"libc", "libc",
"wasi", "wasi",
@ -2089,7 +2096,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97" checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97"
dependencies = [ dependencies = [
"cesu8", "cesu8",
"cfg-if", "cfg-if 1.0.0",
"combine", "combine",
"jni-sys", "jni-sys",
"log", "log",
@ -2183,7 +2190,7 @@ version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 1.0.0",
"winapi", "winapi",
] ]
@ -2193,7 +2200,7 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 1.0.0",
"windows-targets 0.52.5", "windows-targets 0.52.5",
] ]
@ -2273,6 +2280,12 @@ version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
[[package]]
name = "memory_units"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
[[package]] [[package]]
name = "metal" name = "metal"
version = "0.27.0" version = "0.27.0"
@ -2382,7 +2395,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4"
dependencies = [ dependencies = [
"bitflags 2.5.0", "bitflags 2.5.0",
"cfg-if", "cfg-if 1.0.0",
"cfg_aliases", "cfg_aliases",
"libc", "libc",
] ]
@ -2614,7 +2627,7 @@ version = "0.9.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 1.0.0",
"libc", "libc",
"redox_syscall 0.5.1", "redox_syscall 0.5.1",
"smallvec", "smallvec",
@ -2685,7 +2698,7 @@ version = "3.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e6a007746f34ed64099e88783b0ae369eaa3da6392868ba262e2af9b8fbaea1" checksum = "5e6a007746f34ed64099e88783b0ae369eaa3da6392868ba262e2af9b8fbaea1"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 1.0.0",
"concurrent-queue", "concurrent-queue",
"hermit-abi", "hermit-abi",
"pin-project-lite", "pin-project-lite",
@ -3053,7 +3066,7 @@ version = "0.30.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "732ffa00f53e6b2af46208fba5718d9662a421049204e156328b66791ffa15ae" checksum = "732ffa00f53e6b2af46208fba5718d9662a421049204e156328b66791ffa15ae"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 1.0.0",
"core-foundation-sys", "core-foundation-sys",
"libc", "libc",
"ntapi", "ntapi",
@ -3108,7 +3121,7 @@ version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 1.0.0",
"once_cell", "once_cell",
] ]
@ -3239,7 +3252,7 @@ version = "1.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 1.0.0",
"static_assertions", "static_assertions",
] ]
@ -3323,7 +3336,7 @@ version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 1.0.0",
"wasm-bindgen-macro", "wasm-bindgen-macro",
] ]
@ -3348,7 +3361,7 @@ version = "0.4.42"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0"
dependencies = [ dependencies = [
"cfg-if", "cfg-if 1.0.0",
"js-sys", "js-sys",
"wasm-bindgen", "wasm-bindgen",
"web-sys", "web-sys",
@ -3403,6 +3416,18 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "wee_alloc"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e"
dependencies = [
"cfg-if 0.1.10",
"libc",
"memory_units",
"winapi",
]
[[package]] [[package]]
name = "wgpu" name = "wgpu"
version = "0.19.4" version = "0.19.4"
@ -3410,7 +3435,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbd7311dbd2abcfebaabf1841a2824ed7c8be443a0f29166e5d3c6a53a762c01" checksum = "cbd7311dbd2abcfebaabf1841a2824ed7c8be443a0f29166e5d3c6a53a762c01"
dependencies = [ dependencies = [
"arrayvec", "arrayvec",
"cfg-if", "cfg-if 1.0.0",
"cfg_aliases", "cfg_aliases",
"js-sys", "js-sys",
"log", "log",

@ -5,3 +5,10 @@ edition = "2021"
[dependencies] [dependencies]
bevy = "0.13" bevy = "0.13"
wee_alloc = "*"
[profile.wasm-release]
inherits = "release"
opt-level = "z"
lto = "fat"
codegen-units = 1

@ -5,7 +5,7 @@ OUT_DIR="./platforms/web/"
NAME="game-jam-casino" NAME="game-jam-casino"
TARGET="wasm32-unknown-unknown" TARGET="wasm32-unknown-unknown"
cargo build --release --target $TARGET cargo build --profile wasm-release --target $TARGET
wasm-bindgen --target web \ wasm-bindgen --target web \
--out-dir $OUT_DIR \ --out-dir $OUT_DIR \

@ -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::*; use bevy::prelude::*;
fn main() { fn main() {
App::new() let mut app = App::new();
.add_plugins(DefaultPlugins) app.add_plugins(DefaultPlugins);
.run(); 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…
Cancel
Save