From 520df6405ed2530f48a9c2fdf73c2fabb47241f7 Mon Sep 17 00:00:00 2001 From: "Elijah C. Voigt" Date: Wed, 5 Jun 2024 20:08:58 -0700 Subject: [PATCH] Got web to work --- Cargo.lock | 59 +++++++++++++++++++++++++++----------- Cargo.toml | 9 +++++- scripts/build-web.sh | 2 +- src/main.rs | 15 ++++++++-- src/menu.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+), 22 deletions(-) create mode 100644 src/menu.rs diff --git a/Cargo.lock b/Cargo.lock index 012537c..f66c862 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -84,7 +84,7 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "getrandom", "once_cell", "version_check", @@ -1094,7 +1094,7 @@ dependencies = [ "arrayref", "arrayvec", "cc", - "cfg-if", + "cfg-if 1.0.0", "constant_time_eq", ] @@ -1233,6 +1233,12 @@ dependencies = [ "nom", ] +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "cfg-if" version = "1.0.0" @@ -1328,7 +1334,7 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "wasm-bindgen", ] @@ -1454,7 +1460,7 @@ version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -1727,6 +1733,7 @@ name = "game-jam-casino" version = "0.1.0" dependencies = [ "bevy", + "wee_alloc", ] [[package]] @@ -1745,7 +1752,7 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "js-sys", "libc", "wasi", @@ -2089,7 +2096,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97" dependencies = [ "cesu8", - "cfg-if", + "cfg-if 1.0.0", "combine", "jni-sys", "log", @@ -2183,7 +2190,7 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "winapi", ] @@ -2193,7 +2200,7 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "windows-targets 0.52.5", ] @@ -2273,6 +2280,12 @@ version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" +[[package]] +name = "memory_units" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" + [[package]] name = "metal" version = "0.27.0" @@ -2382,7 +2395,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" dependencies = [ "bitflags 2.5.0", - "cfg-if", + "cfg-if 1.0.0", "cfg_aliases", "libc", ] @@ -2614,7 +2627,7 @@ version = "0.9.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "redox_syscall 0.5.1", "smallvec", @@ -2685,7 +2698,7 @@ version = "3.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e6a007746f34ed64099e88783b0ae369eaa3da6392868ba262e2af9b8fbaea1" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "concurrent-queue", "hermit-abi", "pin-project-lite", @@ -3053,7 +3066,7 @@ version = "0.30.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "732ffa00f53e6b2af46208fba5718d9662a421049204e156328b66791ffa15ae" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "core-foundation-sys", "libc", "ntapi", @@ -3108,7 +3121,7 @@ version = "1.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "once_cell", ] @@ -3239,7 +3252,7 @@ version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "static_assertions", ] @@ -3323,7 +3336,7 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "wasm-bindgen-macro", ] @@ -3348,7 +3361,7 @@ version = "0.4.42" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "js-sys", "wasm-bindgen", "web-sys", @@ -3403,6 +3416,18 @@ dependencies = [ "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]] name = "wgpu" version = "0.19.4" @@ -3410,7 +3435,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cbd7311dbd2abcfebaabf1841a2824ed7c8be443a0f29166e5d3c6a53a762c01" dependencies = [ "arrayvec", - "cfg-if", + "cfg-if 1.0.0", "cfg_aliases", "js-sys", "log", diff --git a/Cargo.toml b/Cargo.toml index fe3ae2f..4066cd4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,11 @@ version = "0.1.0" edition = "2021" [dependencies] -bevy = "0.13" \ No newline at end of file +bevy = "0.13" +wee_alloc = "*" + +[profile.wasm-release] +inherits = "release" +opt-level = "z" +lto = "fat" +codegen-units = 1 \ No newline at end of file diff --git a/scripts/build-web.sh b/scripts/build-web.sh index 0a24029..ba4b5a2 100644 --- a/scripts/build-web.sh +++ b/scripts/build-web.sh @@ -5,7 +5,7 @@ OUT_DIR="./platforms/web/" NAME="game-jam-casino" TARGET="wasm32-unknown-unknown" -cargo build --release --target $TARGET +cargo build --profile wasm-release --target $TARGET wasm-bindgen --target web \ --out-dir $OUT_DIR \ diff --git a/src/main.rs b/src/main.rs index d216c17..68b5308 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,16 @@ +extern crate wee_alloc; + +// Use `wee_alloc` as the global allocator. +#[global_allocator] +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + use bevy::prelude::*; +mod menu; + fn main() { - App::new() - .add_plugins(DefaultPlugins) - .run(); + let mut app = App::new(); + app.add_plugins(DefaultPlugins); + app.add_plugins(menu::MenuPlugin); + app.run(); } diff --git a/src/menu.rs b/src/menu.rs new file mode 100644 index 0000000..a821517 --- /dev/null +++ b/src/menu.rs @@ -0,0 +1,67 @@ +use bevy::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::(); + app.add_systems(Startup, init_menu); + app.add_systems(OnEnter(MenuState::Open), open_menu); + app.add_systems(OnExit(MenuState::Open), close_menu); + } +} + +/// State tracking if the menu is open or closed +#[derive(States, Debug, Clone, PartialEq, Eq, Hash, Default)] +enum MenuState { + #[default] + Closed, + Open, +} + +#[derive(Component)] +struct MenuUi; + +/// Initialize menu UI nodes at startup +fn init_menu( + mut commands: Commands, +) { + commands.spawn(Camera2dBundle { ..default() }); + + commands.spawn(( + MenuUi, + NodeBundle { + ..default() + } + )).with_children(|parent| { + parent.spawn( + ButtonBundle { + ..default() + } + ).with_children(|parent| { + parent.spawn(TextBundle { + text: Text::from_section("START", TextStyle { color: Color::BLACK, ..default() }), + ..default() + }); + }); + }); +} + +/// Make menu UI visible +fn open_menu( + mut query: Query<&mut Visibility, With>, +) { + query.iter_mut().for_each(|mut v| { + *v = Visibility::Hidden + }) +} + +/// Hide menu UI +fn close_menu( + mut query: Query<&mut Visibility, With>, +) { + query.iter_mut().for_each(|mut v| { + *v = Visibility::Inherited + }) +} \ No newline at end of file