From fab5e1c41e34c265e2c28fe5cdf8acb2d677ad2c Mon Sep 17 00:00:00 2001 From: rendo Date: Wed, 4 Feb 2026 18:38:05 +0500 Subject: [PATCH] grid units --- src/grid.rs | 20 ++++++++++++++++++++ src/turns.rs | 29 +++++++++++++++++++++++------ src/unit/doh.rs | 2 +- src/unit/fosma.rs | 12 ++++++------ src/unit/zlosma.rs | 2 +- 5 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/grid.rs b/src/grid.rs index 8bbb9af..4cd161f 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -32,6 +32,16 @@ pub enum CellState { Part(Entity), } +impl CellState { + pub fn option(&self) -> Option { + match self { + CellState::Empty => None, + CellState::Filled(entity) => Some(entity.clone()), + CellState::Part(entity) => Some(entity.clone()), + } + } +} + impl Grid { pub fn square(start: Vec2, side: f32, cells: usize) -> Self { let mut elements: Vec = Vec::new(); @@ -98,6 +108,16 @@ impl Grid { self.elements[index] = CellState::Filled(entity); true } + pub fn by_index(&self, index: usize) -> Option { + if self.elements.len() <= index { + None + } else { + self.elements[index].option() + } + } + pub fn get_elements(&self) -> &Vec { + &self.elements + } #[inline] fn cell_size(&self) -> Vec2 { diff --git a/src/turns.rs b/src/turns.rs index 9e49bff..9704e34 100644 --- a/src/turns.rs +++ b/src/turns.rs @@ -1,12 +1,14 @@ use bevy::prelude::*; +use crate::grid::Grid; + pub struct TurnSystemPlugin; impl Plugin for TurnSystemPlugin { fn build(&self, app: &mut App) { app.init_state::() .add_systems(OnEnter(TurnState::PreEffectProcess), pre_effect_setup) - .add_systems(OnEnter(TurnState::UnitProcess), unit_setup) + .add_systems(OnEnter(TurnState::UnitProcess), grid_unit_setup) .add_systems(OnEnter(TurnState::PostEffectProcess), post_effect_setup) .add_systems( Update, @@ -16,6 +18,11 @@ impl Plugin for TurnSystemPlugin { } #[derive(Component)] +#[require(TurnBusy)] +pub struct GridTurnBusy { + pub grid_index: usize, +} +#[derive(Component, Default)] pub struct TurnBusy; #[derive(Component)] @@ -41,10 +48,20 @@ fn pre_effect_setup(mut commands: Commands, query: Query>) { - for unit in query { - commands.entity(unit).insert(TurnBusy); - } +fn grid_unit_setup(mut commands: Commands, grid_q: Single<&Grid>) { + let grid = grid_q.into_inner(); + let (entity, entity_index): (Entity, usize) = 'search_block: { + for i in 0..grid.get_elements().len() { + if let Some(e) = grid.by_index(i) { + break 'search_block (e, i); + } + } + return; + }; + + commands.entity(entity).insert(GridTurnBusy { + grid_index: entity_index, + }); } fn post_effect_setup(mut commands: Commands, query: Query>) { for effect in query { @@ -58,7 +75,7 @@ pub fn try_confirm_turn(mut commands: Commands, turn_state: Res } } -fn process_busy_turnables(mut commands: Commands, query: Query<(Entity, &TurnBusy)>) { +fn process_busy_turnables(mut commands: Commands, query: Query<(Entity, &GridTurnBusy)>) { if query.iter().len() == 0 { commands.run_system_cached(try_advance); } diff --git a/src/unit/doh.rs b/src/unit/doh.rs index 6df76ac..19876ca 100644 --- a/src/unit/doh.rs +++ b/src/unit/doh.rs @@ -2,7 +2,7 @@ use crate::{ animation::transform::AnimatedTransform, grid::{Grid, grid_despawn_check}, mouse_position::MousePosition, - turns::{TurnBusy, TurnUnit}, + turns::{GridTurnBusy, TurnUnit}, unit::{UnitTextures, health::Health}, }; use bevy::prelude::*; diff --git a/src/unit/fosma.rs b/src/unit/fosma.rs index 48a2916..c890d64 100644 --- a/src/unit/fosma.rs +++ b/src/unit/fosma.rs @@ -1,8 +1,8 @@ use crate::{ animation::transform::AnimatedTransform, - grid::{Grid, grid_despawn_check}, + grid::Grid, mouse_position::MousePosition, - turns::{TurnBusy, TurnUnit}, + turns::{GridTurnBusy, TurnUnit}, unit::{UnitTextures, health::Health}, }; use bevy::prelude::*; @@ -32,20 +32,20 @@ pub fn spawn_fosma( pub fn fosma_action( mut commands: Commands, grid: Single<&Grid>, - fosma_query: Query<(Entity, &Transform), (With, With)>, + fosma_query: Query<(Entity, &Transform), (With, With)>, mut health_query: Query<&mut Health>, ) { for (entity, transform) in fosma_query { let Some(unit) = - grid.get_entity_in_direction(transform.translation.truncate(), Vec2::new(1., 0.)) + grid.get_entity_in_direction(transform.translation.truncate(), Vec2::new(0., -1.)) else { - commands.entity(entity).remove::(); + commands.entity(entity).remove::(); return; }; if let Ok(mut health) = health_query.get_mut(unit) { health.damage(10); } - commands.entity(entity).remove::(); + commands.entity(entity).remove::(); } } diff --git a/src/unit/zlosma.rs b/src/unit/zlosma.rs index 2c88412..ba72d9d 100644 --- a/src/unit/zlosma.rs +++ b/src/unit/zlosma.rs @@ -2,7 +2,7 @@ use crate::{ animation::transform::AnimatedTransform, grid::{Grid, grid_despawn_check}, mouse_position::MousePosition, - turns::{TurnBusy, TurnUnit}, + turns::{GridTurnBusy, TurnUnit}, unit::{UnitTextures, health::Health}, }; use bevy::prelude::*;