grid units

This commit is contained in:
rendo 2026-02-04 18:38:05 +05:00
commit fab5e1c41e
5 changed files with 51 additions and 14 deletions

View file

@ -32,6 +32,16 @@ pub enum CellState {
Part(Entity),
}
impl CellState {
pub fn option(&self) -> Option<Entity> {
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<CellState> = Vec::new();
@ -98,6 +108,16 @@ impl Grid {
self.elements[index] = CellState::Filled(entity);
true
}
pub fn by_index(&self, index: usize) -> Option<Entity> {
if self.elements.len() <= index {
None
} else {
self.elements[index].option()
}
}
pub fn get_elements(&self) -> &Vec<CellState> {
&self.elements
}
#[inline]
fn cell_size(&self) -> Vec2 {

View file

@ -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::<TurnState>()
.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<Entity, With<TurnPreEff
commands.entity(effect).insert(TurnBusy);
}
}
fn unit_setup(mut commands: Commands, query: Query<Entity, With<TurnUnit>>) {
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<Entity, With<TurnPostEffect>>) {
for effect in query {
@ -58,7 +75,7 @@ pub fn try_confirm_turn(mut commands: Commands, turn_state: Res<State<TurnState>
}
}
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);
}

View file

@ -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::*;

View file

@ -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<Fosma>, With<TurnBusy>)>,
fosma_query: Query<(Entity, &Transform), (With<Fosma>, With<GridTurnBusy>)>,
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::<TurnBusy>();
commands.entity(entity).remove::<GridTurnBusy>();
return;
};
if let Ok(mut health) = health_query.get_mut(unit) {
health.damage(10);
}
commands.entity(entity).remove::<TurnBusy>();
commands.entity(entity).remove::<GridTurnBusy>();
}
}

View file

@ -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::*;