From 44a343ff717b926ad63ab4cd0d7f89a60524ee67 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Wed, 10 Dec 2025 22:16:38 -0800 Subject: [PATCH] I belive things are loading correctly... --- justfile | 2 +- tetris/src/blocks.rs | 23 +++++++++++--- tetris/src/main.rs | 75 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 87 insertions(+), 13 deletions(-) diff --git a/justfile b/justfile index 66bd279..67dfd4d 100644 --- a/justfile +++ b/justfile @@ -3,7 +3,7 @@ VERSION := `git rev-parse --short HEAD` check: cargo check -p tetris -play: +run: cargo run -p tetris bindgen profile name: diff --git a/tetris/src/blocks.rs b/tetris/src/blocks.rs index 998c8e9..b67d05f 100644 --- a/tetris/src/blocks.rs +++ b/tetris/src/blocks.rs @@ -65,16 +65,31 @@ impl AssetLoader for ShapeAssetLoader { /// Initialize camera and block game area fn load_assets(server: Res, mut all_assets: ResMut) { - all_assets.handles.push(server.load::("t.shape.toml").untyped()); + all_assets + .handles + .push(server.load::("t.shape.toml").untyped()); } +/// Spawn the tetris 2d camera fn setup_camera(mut commands: Commands) { commands.spawn((Camera2d, Camera::default())); } -fn setup_blocks(mut commands: Commands) { - error!("Setup blocks"); - error!("Once all steps are setup, move on to Run"); +/// Spawn a block +/// This system is temporary +fn setup_blocks( + mut commands: Commands, + all_assets: Res, + server: Res, + mut checklist: ResMut, +) { + let h: Handle = server + .get_handle(all_assets.handles[0].path().unwrap()) + .unwrap(); + + commands.spawn(AssetComponent::new(h)); + + checklist.spawn_shape = true; } /// Event handler for transforming a handle component into a thing diff --git a/tetris/src/main.rs b/tetris/src/main.rs index 4ce3aef..8923ac9 100644 --- a/tetris/src/main.rs +++ b/tetris/src/main.rs @@ -17,15 +17,23 @@ fn main() { .init_state::() .init_state::() .init_resource::() - .add_systems(Update, load) + .init_resource::() + // Check if assets were added to loading queue + .add_systems( + Update, + loading_check + .run_if(in_state(Loading::Active)) + .run_if(resource_changed::), + ) + // Wait for pending assets to be loaded + .add_systems(Update, loading_wait.run_if(in_state(Loading::Active))) + // Once done loading, move to the setup state + .add_systems(OnEnter(Loading::Idle), setup_game) + // Check if the game is ready to progress + .add_systems(Update, setup_wait.run_if(in_state(GameState::Setup))) .run(); } -#[derive(Default, Resource, Debug)] -struct AllAssets { - handles: Vec, -} - /// Reports if the game is loading assets #[derive(States, Default, Clone, Eq, Debug, PartialEq, Hash)] enum Loading { @@ -46,16 +54,67 @@ enum Debugger { #[derive(States, Default, Clone, Eq, Debug, PartialEq, Hash)] enum GameState { #[default] + Boot, Setup, Run, } -fn load(mut next: ResMut>, server: Res, assets: Res) { - if assets.handles.iter().all(|h| matches!(server.get_load_state(h.id()), Some(LoadState::Loaded))) { +/// A list of all assets so we don't lose them +#[derive(Default, Resource, Debug)] +struct AllAssets { + handles: Vec, +} + +/// A "checklist" to know if we can progress from setup to the game +#[derive(Default, Resource)] +struct SetupChecklist { + spawn_shape: bool, +} + +/// Sends the game into Loading::Active if assets are added to the AllAssets list +fn loading_check(mut next: ResMut>, all_assets: Res) { + debug_assert!(all_assets.is_changed()); + + next.set(Loading::Active); +} + +/// Waits in Loading::Active until all assets are loaded then move to Loading::Idle +fn loading_wait( + curr: Res>, + mut next: ResMut>, + server: Res, + all_assets: Res, +) { + debug_assert!(*curr.get() == Loading::Active); + if all_assets + .handles + .iter() + .all(|h| matches!(server.get_load_state(h.id()), Some(LoadState::Loaded))) + { next.set(Loading::Idle); } } +/// Moves the game from Boot to Setup +fn setup_game(curr: Res>, mut next: ResMut>) { + debug_assert!(*curr.get() == GameState::Boot); + + next.set(GameState::Setup); +} + +fn setup_wait( + curr: Res>, + mut next: ResMut>, + checklist: Res, +) { + debug_assert!(*curr.get() == GameState::Setup); + + // If all checks pass, move on to the run state + if checklist.spawn_shape { + next.set(GameState::Run); + } +} + /// A wrapper around a handle for assigning an arbitrary Handle to an entity #[derive(Debug, Component)] struct AssetComponent {