grid units
This commit is contained in:
parent
952818c5be
commit
fab5e1c41e
5 changed files with 51 additions and 14 deletions
20
src/grid.rs
20
src/grid.rs
|
|
@ -32,6 +32,16 @@ pub enum CellState {
|
||||||
Part(Entity),
|
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 {
|
impl Grid {
|
||||||
pub fn square(start: Vec2, side: f32, cells: usize) -> Self {
|
pub fn square(start: Vec2, side: f32, cells: usize) -> Self {
|
||||||
let mut elements: Vec<CellState> = Vec::new();
|
let mut elements: Vec<CellState> = Vec::new();
|
||||||
|
|
@ -98,6 +108,16 @@ impl Grid {
|
||||||
self.elements[index] = CellState::Filled(entity);
|
self.elements[index] = CellState::Filled(entity);
|
||||||
true
|
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]
|
#[inline]
|
||||||
fn cell_size(&self) -> Vec2 {
|
fn cell_size(&self) -> Vec2 {
|
||||||
|
|
|
||||||
27
src/turns.rs
27
src/turns.rs
|
|
@ -1,12 +1,14 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::grid::Grid;
|
||||||
|
|
||||||
pub struct TurnSystemPlugin;
|
pub struct TurnSystemPlugin;
|
||||||
|
|
||||||
impl Plugin for TurnSystemPlugin {
|
impl Plugin for TurnSystemPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.init_state::<TurnState>()
|
app.init_state::<TurnState>()
|
||||||
.add_systems(OnEnter(TurnState::PreEffectProcess), pre_effect_setup)
|
.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(OnEnter(TurnState::PostEffectProcess), post_effect_setup)
|
||||||
.add_systems(
|
.add_systems(
|
||||||
Update,
|
Update,
|
||||||
|
|
@ -16,6 +18,11 @@ impl Plugin for TurnSystemPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
#[require(TurnBusy)]
|
||||||
|
pub struct GridTurnBusy {
|
||||||
|
pub grid_index: usize,
|
||||||
|
}
|
||||||
|
#[derive(Component, Default)]
|
||||||
pub struct TurnBusy;
|
pub struct TurnBusy;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
|
@ -41,11 +48,21 @@ fn pre_effect_setup(mut commands: Commands, query: Query<Entity, With<TurnPreEff
|
||||||
commands.entity(effect).insert(TurnBusy);
|
commands.entity(effect).insert(TurnBusy);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn unit_setup(mut commands: Commands, query: Query<Entity, With<TurnUnit>>) {
|
fn grid_unit_setup(mut commands: Commands, grid_q: Single<&Grid>) {
|
||||||
for unit in query {
|
let grid = grid_q.into_inner();
|
||||||
commands.entity(unit).insert(TurnBusy);
|
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>>) {
|
fn post_effect_setup(mut commands: Commands, query: Query<Entity, With<TurnPostEffect>>) {
|
||||||
for effect in query {
|
for effect in query {
|
||||||
commands.entity(effect).insert(TurnBusy);
|
commands.entity(effect).insert(TurnBusy);
|
||||||
|
|
@ -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 {
|
if query.iter().len() == 0 {
|
||||||
commands.run_system_cached(try_advance);
|
commands.run_system_cached(try_advance);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use crate::{
|
||||||
animation::transform::AnimatedTransform,
|
animation::transform::AnimatedTransform,
|
||||||
grid::{Grid, grid_despawn_check},
|
grid::{Grid, grid_despawn_check},
|
||||||
mouse_position::MousePosition,
|
mouse_position::MousePosition,
|
||||||
turns::{TurnBusy, TurnUnit},
|
turns::{GridTurnBusy, TurnUnit},
|
||||||
unit::{UnitTextures, health::Health},
|
unit::{UnitTextures, health::Health},
|
||||||
};
|
};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
animation::transform::AnimatedTransform,
|
animation::transform::AnimatedTransform,
|
||||||
grid::{Grid, grid_despawn_check},
|
grid::Grid,
|
||||||
mouse_position::MousePosition,
|
mouse_position::MousePosition,
|
||||||
turns::{TurnBusy, TurnUnit},
|
turns::{GridTurnBusy, TurnUnit},
|
||||||
unit::{UnitTextures, health::Health},
|
unit::{UnitTextures, health::Health},
|
||||||
};
|
};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
@ -32,20 +32,20 @@ pub fn spawn_fosma(
|
||||||
pub fn fosma_action(
|
pub fn fosma_action(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
grid: Single<&Grid>,
|
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>,
|
mut health_query: Query<&mut Health>,
|
||||||
) {
|
) {
|
||||||
for (entity, transform) in fosma_query {
|
for (entity, transform) in fosma_query {
|
||||||
let Some(unit) =
|
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 {
|
else {
|
||||||
commands.entity(entity).remove::<TurnBusy>();
|
commands.entity(entity).remove::<GridTurnBusy>();
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Ok(mut health) = health_query.get_mut(unit) {
|
if let Ok(mut health) = health_query.get_mut(unit) {
|
||||||
health.damage(10);
|
health.damage(10);
|
||||||
}
|
}
|
||||||
commands.entity(entity).remove::<TurnBusy>();
|
commands.entity(entity).remove::<GridTurnBusy>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use crate::{
|
||||||
animation::transform::AnimatedTransform,
|
animation::transform::AnimatedTransform,
|
||||||
grid::{Grid, grid_despawn_check},
|
grid::{Grid, grid_despawn_check},
|
||||||
mouse_position::MousePosition,
|
mouse_position::MousePosition,
|
||||||
turns::{TurnBusy, TurnUnit},
|
turns::{GridTurnBusy, TurnUnit},
|
||||||
unit::{UnitTextures, health::Health},
|
unit::{UnitTextures, health::Health},
|
||||||
};
|
};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue