From 2f1b6e915287fe7a888ce89e4c5e02518b8f4d3f Mon Sep 17 00:00:00 2001 From: Rendo Date: Thu, 29 Jan 2026 02:06:48 +0500 Subject: [PATCH] test turn --- src/dev_tools/mod.rs | 97 +++++++++++++++++++------------------------- src/grid.rs | 22 ++++++---- src/turns.rs | 53 +++++++++++++----------- 3 files changed, 86 insertions(+), 86 deletions(-) diff --git a/src/dev_tools/mod.rs b/src/dev_tools/mod.rs index 44ff836..403ea24 100644 --- a/src/dev_tools/mod.rs +++ b/src/dev_tools/mod.rs @@ -2,6 +2,7 @@ use bevy::prelude::*; use crate::buttons; use crate::card; +use crate::turns::try_confirm_turn; const PANEL_BORDER: Color = Color::srgb_u8(36, 36, 143); const PANEL_BACKGROUND: Color = Color::srgb_u8(0, 0, 52); @@ -55,60 +56,44 @@ fn setup_debug_menu(mut commands: Commands) { )) .id(); - commands - .spawn(( - Button, - Node { - min_height: px(32), - width: percent(100.), - justify_content: JustifyContent::Center, - align_items: AlignItems::Center, - margin: UiRect::all(px(4)), - border: UiRect::all(px(3)), - ..default() - }, - BorderColor::all(BUTTON_BORDER), - BackgroundColor(BUTTON_BACKGROUND), - ChildOf(panel), - children![( - Text::new("Add card"), - TextFont { - font_size: 11.0, - ..default() - }, - )], - )) - .observe( - |_: On, mut commands: Commands| { - commands.trigger(card::AddCard) - }, - ); - commands - .spawn(( - Button, - Node { - min_height: px(32), - width: percent(100.), - justify_content: JustifyContent::Center, - align_items: AlignItems::Center, - margin: UiRect::all(px(4)), - border: UiRect::all(px(3)), - ..default() - }, - BorderColor::all(BUTTON_BORDER), - BackgroundColor(BUTTON_BACKGROUND), - ChildOf(panel), - children![( - Text::new("Clear cards"), - TextFont { - font_size: 11.0, - ..default() - }, - )], - )) - .observe( - |_: On, mut commands: Commands| { - commands.trigger(card::ClearCards) - }, - ); + commands.spawn(button(panel, "AddCard")).observe( + |_: On, mut commands: Commands| { + commands.trigger(card::AddCard) + }, + ); + commands.spawn(button(panel, "ClearCards")).observe( + |_: On, mut commands: Commands| { + commands.trigger(card::ClearCards) + }, + ); + commands.spawn(button(panel, "ConfirmTurn")).observe( + |_: On, mut commands: Commands| { + commands.run_system_cached(try_confirm_turn); + }, + ); +} + +fn button(child_of: Entity, text: impl Into) -> impl Bundle { + ( + Button, + Node { + min_height: px(32), + width: percent(100.), + justify_content: JustifyContent::Center, + align_items: AlignItems::Center, + margin: UiRect::all(px(4)), + border: UiRect::all(px(3)), + ..default() + }, + BorderColor::all(BUTTON_BORDER), + BackgroundColor(BUTTON_BACKGROUND), + ChildOf(child_of), + children![( + Text::new(text), + TextFont { + font_size: 11.0, + ..default() + }, + )], + ) } diff --git a/src/grid.rs b/src/grid.rs index f19a112..9c1bfd9 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -16,13 +16,20 @@ pub struct Grid { bounds: Rect, columns: usize, rows: usize, - elements: Vec>, + elements: Vec, +} + +#[derive(PartialEq, Eq, Clone, Copy)] +pub enum CellState { + Empty, + Filled(Entity), + Part(Entity), } impl Grid { pub fn square(start: Vec2, side: f32, cells: usize) -> Self { - let mut elements: Vec> = Vec::new(); - elements.resize(cells * cells, None); + let mut elements: Vec = Vec::new(); + elements.resize(cells * cells, CellState::Empty); Grid { bounds: Rect::from_corners( start, @@ -54,8 +61,9 @@ impl Grid { } match self.elements[self.indexify_position(position)] { - Some(_) => true, - None => false, + CellState::Filled(_) => true, + CellState::Part(_) => true, + CellState::Empty => false, } } @@ -65,7 +73,7 @@ impl Grid { } let index = self.indexify_position(position); - self.elements[index] = Some(entity); + self.elements[index] = CellState::Filled(entity); true } @@ -96,7 +104,7 @@ fn snap_grid_elements( ) { for grid in grid_query { for (index, el) in grid.elements.iter().enumerate() { - let Some(entity) = el else { + let CellState::Filled(entity) = el else { continue; }; let Ok(mut transform) = transform_query.get_mut(entity.clone()) else { diff --git a/src/turns.rs b/src/turns.rs index c08b8c6..561c0c5 100644 --- a/src/turns.rs +++ b/src/turns.rs @@ -16,19 +16,19 @@ impl Plugin for TurnSystemPlugin { } #[derive(Component)] -struct TurnBusy(bool); +pub struct TurnBusy(pub bool); #[derive(Component)] -struct TurnPreEffect; +pub struct TurnPreEffect; #[derive(Component)] -struct TurnUnit; +pub struct TurnUnit; #[derive(Component)] -struct TurnPostEffect; +pub struct TurnPostEffect; #[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)] -enum TurnState { +pub enum TurnState { #[default] Turn, PreEffectProcess, @@ -51,11 +51,14 @@ fn post_effect_setup(mut commands: Commands, query: Query, - state: Res>, -) { + +pub fn try_confirm_turn(mut commands: Commands, turn_state: Res>) { + if let TurnState::Turn = turn_state.get() { + commands.set_state(TurnState::PreEffectProcess); + } +} + +fn process_busy_turnables(mut commands: Commands, query: Query<(Entity, &TurnBusy)>) { let mut advance_flag: bool = true; for (turnable, component) in query { if component.0 { @@ -66,19 +69,23 @@ fn process_busy_turnables( } if advance_flag { - match state.get() { - TurnState::Turn => { - commands.set_state(TurnState::PreEffectProcess); - } - TurnState::PreEffectProcess => { - commands.set_state(TurnState::UnitProcess); - } - TurnState::UnitProcess => { - commands.set_state(TurnState::PostEffectProcess); - } - TurnState::PostEffectProcess => { - commands.set_state(TurnState::Turn); - } + commands.run_system_cached(try_advance); + } +} + +fn try_advance(mut commands: Commands, state: Res>) { + match state.get() { + TurnState::Turn => { + commands.set_state(TurnState::PreEffectProcess); + } + TurnState::PreEffectProcess => { + commands.set_state(TurnState::UnitProcess); + } + TurnState::UnitProcess => { + commands.set_state(TurnState::PostEffectProcess); + } + TurnState::PostEffectProcess => { + commands.set_state(TurnState::Turn); } } }