From c7a137c58d1bfe7752fe2228bae4120a73e36dfc Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Tue, 8 Jul 2025 12:28:44 -0700 Subject: [PATCH] web output for trees --- .gitignore | 3 +++ Makefile | 7 +++++++ flake.nix | 3 +++ rust-toolchain.toml | 2 +- src/base_game.rs | 18 ++++++++++++------ src/debug.rs | 30 ++++++++++++++++++------------ trees.css | 4 ++++ trees.html | 10 ++++++++++ 8 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 Makefile create mode 100644 trees.css create mode 100644 trees.html diff --git a/.gitignore b/.gitignore index 245b501..af864be 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ # home-manager stuff result + +# Web output +dist/* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4eb0837 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +web/trees: + mkdir -p dist/trees + trunk build --release true trees.html --dist dist/trees + ln -sf `pwd`/assets dist/trees/assets + +web/trees/serve: web/trees + cd dist/trees && simple-http-server diff --git a/flake.nix b/flake.nix index 5ed92ab..3c125ae 100644 --- a/flake.nix +++ b/flake.nix @@ -49,6 +49,9 @@ wayland # To use the wayland feature # Bug tracking git-bug + # wasm builder + trunk + simple-http-server ]; LD_LIBRARY_PATH = lib.makeLibraryPath [ alsa-lib diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 5139e6e..0281a60 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] channel = "nightly-2025-06-16" components = [ "rustfmt", "rustc-dev", "cargo", "clippy", "rust-analyzer", "rustc-codegen-cranelift" ] -targets = [ "x86_64-unknown-linux-gnu" ] +targets = [ "x86_64-unknown-linux-gnu", "wasm32-unknown-unknown" ] diff --git a/src/base_game.rs b/src/base_game.rs index 836b7a0..0e67dfb 100644 --- a/src/base_game.rs +++ b/src/base_game.rs @@ -5,12 +5,18 @@ pub struct BaseGamePlugin; impl Plugin for BaseGamePlugin { fn build(&self, app: &mut App) { - app.add_plugins(DefaultPlugins) - .add_plugins(DebuggingPlugin) - .add_plugins(MeshPickingPlugin) - .add_plugins(RapierPhysicsPlugin::::default()) - .add_plugins(LoadingPlugin) - .add_systems(Startup, setup_camera); + app.add_plugins(DefaultPlugins.set(WindowPlugin { + primary_window: Some(Window { + fit_canvas_to_parent: true, + ..default() + }), + ..default() + })) + .add_plugins(DebuggingPlugin) + .add_plugins(MeshPickingPlugin) + .add_plugins(RapierPhysicsPlugin::::default()) + .add_plugins(LoadingPlugin) + .add_systems(Startup, setup_camera); } } diff --git a/src/debug.rs b/src/debug.rs index b6d10e9..5211a50 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -14,21 +14,11 @@ impl Plugin for DebuggingPlugin { app.init_state::() .init_resource::() .add_plugins(RapierDebugRenderPlugin::default().disabled()) - .add_plugins(WireframePlugin::default()) - .insert_resource(WireframeConfig { - global: false, - default_color: MAGENTA.into(), - }) .add_systems(Startup, init_debug_ui) - .add_systems(OnEnter(DebuggingState::On), enable_wireframe) - .add_systems(OnExit(DebuggingState::On), disable_wireframe) .add_systems( Update, ( - ( - toggle_state_visibility::, - toggle_rapier_debug_render, - ) + (toggle_state_visibility::,) .run_if(state_changed::), toggle_debug_state.run_if(on_keyboard_press(KeyCode::F12)), ( @@ -40,6 +30,22 @@ impl Plugin for DebuggingPlugin { .run_if(in_state(DebuggingState::On)), ), ); + + // WebGL2-incompatible systems + #[cfg(not(target_arch = "wasm32"))] + { + app.add_plugins(WireframePlugin::default()) + .insert_resource(WireframeConfig { + global: false, + default_color: MAGENTA.into(), + }) + .add_systems( + Update, + toggle_rapier_debug_render.run_if(state_changed::), + ) + .add_systems(OnEnter(DebuggingState::On), enable_wireframe) + .add_systems(OnExit(DebuggingState::On), disable_wireframe); + } } } @@ -99,7 +105,7 @@ fn toggle_debug_state( On => Off, Off => On, }); - debug!("Toggling debug state: {:?} -> {:?}", curr, next); + info!("Toggling debug state: {:?} -> {:?}", curr, next); } /// Simple system that enables/disables rapier debug visuals when the debugging state changes diff --git a/trees.css b/trees.css new file mode 100644 index 0000000..547c492 --- /dev/null +++ b/trees.css @@ -0,0 +1,4 @@ +canvas { + width: 90%; + height: 90%; +} diff --git a/trees.html b/trees.html new file mode 100644 index 0000000..f315427 --- /dev/null +++ b/trees.html @@ -0,0 +1,10 @@ + + + + + +