diff --git a/src/card/playing.rs b/src/card/playing.rs index 84e745b..3561ad6 100644 --- a/src/card/playing.rs +++ b/src/card/playing.rs @@ -1,41 +1,29 @@ use bevy::prelude::*; use crate::{ - animation::transform::AnimatedTransform, card::{CardInHand, InsertCard, drag::CardDropped}, grid::Grid, mouse_position::MousePosition, - turns::TurnUnit, - unit::UnitTextures, + unit::{doh, fosma, zlosma}, }; pub fn card_playing_system( event: On, mut commands: Commands, - mut grid: Single<&mut Grid>, + grid: Single<&mut Grid>, mouse_position: Res, - textures: Res, ) { - let bundle = match event.card_type { - super::CardType::Unit(unit_type) => match unit_type { - crate::unit::UnitType::Fosma => Sprite::from_image(textures.fosma_texture()), - crate::unit::UnitType::Zlosma => Sprite::from_image(textures.zlosma_texture()), - crate::unit::UnitType::Doh => Sprite::from_image(textures.doh_texture()), - }, - }; - let unit = commands - .spawn(( - AnimatedTransform::identity(), - Transform::from_translation(mouse_position.position), - bundle, - TurnUnit, - )) - .id(); - if grid.try_set(mouse_position.position.truncate(), unit) { - commands.entity(event.entity).despawn(); - } else { - commands.entity(unit).despawn(); + if grid.is_occupied(mouse_position.position.truncate()) { commands.entity(event.entity).insert(CardInHand); commands.trigger(InsertCard(event.entity)); + return; } + match event.card_type { + super::CardType::Unit(unit_type) => match unit_type { + crate::unit::UnitType::Fosma => commands.run_system_cached(fosma::spawn_fosma), + crate::unit::UnitType::Zlosma => commands.run_system_cached(zlosma::spawn_zlosma), + crate::unit::UnitType::Doh => commands.run_system_cached(doh::spawn_doh), + }, + } + commands.entity(event.entity).despawn(); } diff --git a/src/grid.rs b/src/grid.rs index 9c1bfd9..4dd3b03 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -67,6 +67,22 @@ impl Grid { } } + pub fn get_entity(&self, position: Vec2) -> Option { + if self.is_in_grid(position) == false { + return None; + } + + match self.elements[self.indexify_position(position)] { + CellState::Filled(entity) => Some(entity.clone()), + CellState::Part(entity) => Some(entity.clone()), + CellState::Empty => None, + } + } + + pub fn get_entity_in_direction(&self, position: Vec2, direction: Vec2) -> Option { + self.get_entity(position + direction * self.cell_size()) + } + pub fn try_set(&mut self, position: Vec2, entity: Entity) -> bool { if self.is_occupied(position) { return false; diff --git a/src/unit/doh.rs b/src/unit/doh.rs new file mode 100644 index 0000000..0f51e62 --- /dev/null +++ b/src/unit/doh.rs @@ -0,0 +1,30 @@ +use crate::{ + animation::transform::AnimatedTransform, + grid::Grid, + mouse_position::MousePosition, + turns::{TurnBusy, TurnState, TurnUnit}, + unit::{UnitTextures, health::Health}, +}; +use bevy::prelude::*; + +#[derive(Component)] +pub struct Doh; + +pub fn spawn_doh( + mut commands: Commands, + mut grid: Single<&mut Grid>, + mouse_position: Res, + unit_textures: Res, +) { + let doh = commands + .spawn(( + Doh, + Sprite::from(unit_textures.doh_texture()), + Health::new(10), + Transform::from_xyz(mouse_position.position.x, mouse_position.position.y, 1.), + AnimatedTransform::from_xyz(mouse_position.position.x, mouse_position.position.y, 1.), + TurnUnit, + )) + .id(); + grid.try_set(mouse_position.position.truncate(), doh); +} diff --git a/src/unit/fosma.rs b/src/unit/fosma.rs new file mode 100644 index 0000000..532d243 --- /dev/null +++ b/src/unit/fosma.rs @@ -0,0 +1,30 @@ +use crate::{ + animation::transform::AnimatedTransform, + grid::Grid, + mouse_position::MousePosition, + turns::{TurnBusy, TurnState, TurnUnit}, + unit::{UnitTextures, health::Health}, +}; +use bevy::prelude::*; + +#[derive(Component)] +pub struct Fosma; + +pub fn spawn_fosma( + mut commands: Commands, + mut grid: Single<&mut Grid>, + mouse_position: Res, + unit_textures: Res, +) { + let fosma = commands + .spawn(( + Fosma, + Sprite::from(unit_textures.fosma_texture()), + Health::new(10), + Transform::from_xyz(mouse_position.position.x, mouse_position.position.y, 1.), + AnimatedTransform::from_xyz(mouse_position.position.x, mouse_position.position.y, 1.), + TurnUnit, + )) + .id(); + grid.try_set(mouse_position.position.truncate(), fosma); +} diff --git a/src/unit/health.rs b/src/unit/health.rs new file mode 100644 index 0000000..ac85595 --- /dev/null +++ b/src/unit/health.rs @@ -0,0 +1,13 @@ +use bevy::prelude::*; + +#[derive(Component)] +pub struct Health { + pub max_hp: u32, + pub hp: u32, +} + +impl Health { + pub fn new(max_hp: u32) -> Self { + Self { max_hp, hp: max_hp } + } +} diff --git a/src/unit/mod.rs b/src/unit/mod.rs index e075b7c..88e2257 100644 --- a/src/unit/mod.rs +++ b/src/unit/mod.rs @@ -1,5 +1,10 @@ use bevy::prelude::*; +pub mod doh; +pub mod fosma; +pub mod health; +pub mod zlosma; + pub struct UnitPlugin; impl Plugin for UnitPlugin { fn build(&self, app: &mut App) { diff --git a/src/unit/zlosma.rs b/src/unit/zlosma.rs new file mode 100644 index 0000000..4c93849 --- /dev/null +++ b/src/unit/zlosma.rs @@ -0,0 +1,30 @@ +use crate::{ + animation::transform::AnimatedTransform, + grid::Grid, + mouse_position::MousePosition, + turns::{TurnBusy, TurnState, TurnUnit}, + unit::{UnitTextures, health::Health}, +}; +use bevy::prelude::*; + +#[derive(Component)] +pub struct Zlosma; + +pub fn spawn_zlosma( + mut commands: Commands, + mut grid: Single<&mut Grid>, + mouse_position: Res, + unit_textures: Res, +) { + let zlosma = commands + .spawn(( + Zlosma, + Sprite::from(unit_textures.zlosma_texture()), + Health::new(10), + Transform::from_xyz(mouse_position.position.x, mouse_position.position.y, 1.), + AnimatedTransform::from_xyz(mouse_position.position.x, mouse_position.position.y, 1.), + TurnUnit, + )) + .id(); + grid.try_set(mouse_position.position.truncate(), zlosma); +}