From 12bf2ea96d03bbc85b0777227a187d1e06501683 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Fri, 17 Nov 2023 19:07:04 -0800 Subject: [PATCH] About to make a big refactor to selection --- TODO | 30 +++++++++++++++++++++--------- src/display2d.rs | 4 +++- src/display3d.rs | 21 +++++++++++---------- src/game.rs | 9 ++++++++- 4 files changed, 43 insertions(+), 21 deletions(-) diff --git a/TODO b/TODO index 8c959d5..8d97ad2 100644 --- a/TODO +++ b/TODO @@ -1,10 +1,22 @@ -Camera handling: -* Set all new cameras as "is_active = false" automatically. -* Set loading camera as "is_active = true" upon entering loading. -* Set menu camera as "is_active = true" upon entering menu, etc. -* Maybe this has to do with loading entry/de-entry? It goes away after entering main menu... +Selecting one entity at a time... +* Affects both 2d and 3d modes seemingly equally +* We seem to not be de-selecting something... +* Does it happen when you play the game fast/slow? + * Yes +* It only seems to happen when we capture a piece. -OK new idea: -* Have one enum with all states (no split states) -* Assign a state to each camera. -* On state change, assign the camera with that state. +New architecture: +* Instead of inserting a "Selected" marker, emit a "Selected" event. + * Contextually this acts differently based on: + * If no entities are selected, insert the selected marker on that entity. + * If an entity is already marked selected, we emit a move event and de-select all entities. +* Q: Should we "Select Event" a BoardIndex? + * Pros: + * Display agnostic. + * Easy to keep in sync between displays. + * Cons: + * Possibly umm... actually I can't think of any. +* Emit "Selected" event which includes the BoardIndex selected. + * If a piece exists at that index: + * If a piece is already selected, emit a "Move" event and de-select the selected piece. + * Else mark it as selected in 2d and 3d. diff --git a/src/display2d.rs b/src/display2d.rs index 162523a..9f49d1c 100644 --- a/src/display2d.rs +++ b/src/display2d.rs @@ -284,6 +284,7 @@ fn select( cameras: Query<(&Camera, &GlobalTransform), With>, atlases: Res>, mut commands: Commands, + selected: Query, With, With)>, ) { // For each window (there is only one) windows @@ -322,7 +323,8 @@ fn select( }) .iter() .for_each(|(entity, _)| { - info!("Selected entity {:?}", entity); + info!("Selected pieces: {:?}", selected.iter().len()); + assert!(selected.iter().len() <= 2, "Selected too many pieces!"); commands.entity(*entity).insert(game::Selected); }); }); diff --git a/src/display3d.rs b/src/display3d.rs index 2e1dde6..e8a3a5a 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -358,11 +358,11 @@ fn set_piece_texture( mut models: Query<(&Name, &mut Handle)>, ) { events.iter().for_each(|(entity, piece, side)| { - info!("Checking piece texture for {:?}", entity); + debug!("Checking piece texture for {:?}", entity); if let Some(gltf) = gltfs.get(&assets_map.models) { children.iter_descendants(entity).for_each(|child| { if let Ok((n, mut m)) = models.get_mut(child) { - info!("Setting piece texture for {:?}", child); + debug!("Setting piece texture for {:?}", child); match (*piece, side, n.as_str()) { (Piece::Queen, Side::A, "Queen.0") => { *m = gltf @@ -468,14 +468,7 @@ fn select( selectable: Query, With)>, children: Query<&Children>, mut commands: Commands, - _selected: Query< - Entity, - ( - With, - With, - With, - ), - >, + selected: Query, With, With)>, ) { events .iter() @@ -506,6 +499,14 @@ fn select( }) .iter() .for_each(|&e| { + info!( + "Selected pieces: {:?}", + selected.iter().len() + ); + assert!( + selected.iter().len() <= 2, + "Selected too many pieces selected!" + ); commands.entity(e).insert(game::Selected); }); Some(()) diff --git a/src/game.rs b/src/game.rs index 886a9da..5fb139e 100644 --- a/src/game.rs +++ b/src/game.rs @@ -503,9 +503,16 @@ fn capture_piece(mut events: Query<&mut Visibility, Added>) { fn asserts( selected_pieces: Query, With, With)>, selected_tiles: Query, With, With)>, + mut commands: Commands, ) { if selected_pieces.iter().len() > 2 { - panic!("More than two pieces selected"); + selected_pieces.iter().for_each(|e| { + info!("Too many pieces selected, one of which is: {:?}", e); + }); + panic!( + "More than two pieces selected {:?}", + selected_pieces.iter().len() + ); } if selected_tiles.iter().len() > 2 { panic!(