diff --git a/src/animation/transform.rs b/src/animation/transform.rs index b42c8a7..189f2c0 100644 --- a/src/animation/transform.rs +++ b/src/animation/transform.rs @@ -1,6 +1,7 @@ use bevy::prelude::*; #[derive(Component)] +#[require(Transform)] pub struct AnimatedTransform { pub translation: Vec3, } @@ -11,6 +12,11 @@ impl AnimatedTransform { translation: Vec3 { x, y, z }, } } + pub fn identity() -> Self { + AnimatedTransform { + translation: Vec3::ZERO, + } + } } pub fn transform_animation(query: Query<(&mut Transform, &AnimatedTransform)>) { diff --git a/src/card/drag.rs b/src/card/drag.rs index 31489fd..4a783b4 100644 --- a/src/card/drag.rs +++ b/src/card/drag.rs @@ -5,6 +5,9 @@ use crate::{ card::{CardInHand, Hand, InsertCard}, }; +#[derive(EntityEvent)] +pub struct CardDropped(pub Entity); + #[derive(Component)] pub struct DraggableCard; @@ -69,6 +72,7 @@ pub fn card_drag_end( dragged: Single>, ) { commands.trigger(InsertCard(dragged.entity())); + commands.trigger(CardDropped(dragged.entity())); commands .entity(dragged.entity()) .remove::() diff --git a/src/card/mod.rs b/src/card/mod.rs index 28d5309..594ac40 100644 --- a/src/card/mod.rs +++ b/src/card/mod.rs @@ -3,8 +3,10 @@ use std::f32; use crate::animation::transform::AnimatedTransform; use bevy::prelude::*; use drag::*; +use playing::*; pub mod drag; +pub mod playing; pub struct CardPlugin; @@ -35,12 +37,14 @@ impl Plugin for CardPlugin { .add_systems(Update, card_arrange) .add_observer(card_add) .add_observer(card_clear) - .add_observer(insert_card); + .add_observer(insert_card) + .add_observer(despawn_card); app.insert_resource(MousePosition(Vec3::new(0., 0., 0.))) .add_systems(PreUpdate, mouse_position_system) .add_observer(card_drag_start) .add_observer(card_drag) .add_observer(card_drag_end); + app.add_observer(card_playing_system); } } @@ -133,3 +137,17 @@ fn insert_card( } hand_query.1.0.insert(decided_index, event.0); } +fn despawn_card(event: On, mut hand_query: Single<&mut Hand>, cards_query: Query<&Card>) { + if let Err(_) = cards_query.get(event.entity) { + return; + } + + if hand_query.0.contains(&event.entity) { + let index = hand_query + .0 + .iter() + .position(|x| *x == event.entity) + .unwrap(); + hand_query.0.remove(index); + } +} diff --git a/src/card/playing.rs b/src/card/playing.rs new file mode 100644 index 0000000..209a029 --- /dev/null +++ b/src/card/playing.rs @@ -0,0 +1,26 @@ +use bevy::prelude::*; + +use crate::{ + animation::transform::AnimatedTransform, + card::drag::{CardDropped, MousePosition}, + grid::Grid, + unit::UnitTextures, +}; + +pub fn card_playing_system( + event: On, + mut commands: Commands, + mut grid: Single<&mut Grid>, + mouse_position: Res, + textures: Res, +) { + let fosma = commands + .spawn(( + AnimatedTransform::identity(), + Sprite::from_image(textures.fosma_texture()), + )) + .id(); + grid.try_set(mouse_position.0.truncate(), fosma); + + commands.entity(event.0).despawn(); +} diff --git a/src/grid.rs b/src/grid.rs index 1aeb2df..809b897 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -12,7 +12,7 @@ impl Plugin for GridPlugin { } #[derive(Component)] -struct Grid { +pub struct Grid { bounds: Rect, columns: usize, rows: usize, @@ -42,9 +42,43 @@ impl Grid { .extend(0.) } + pub fn is_in_grid(&self, position: Vec2) -> bool { + self.bounds.contains(position) + } + + pub fn is_occupied(&self, position: Vec2) -> bool { + if self.is_in_grid(position) == false { + return true; + } + + match self.elements[self.indexify_position(position)] { + Some(_) => true, + None => false, + } + } + + pub fn is_free(&self, position: Vec2) -> bool { + self.is_occupied(position) == false + } + + pub fn try_set(&mut self, position: Vec2, entity: Entity) -> bool { + if self.is_occupied(position) { + return false; + } + let index = self.indexify_position(position); + + self.elements[index] = Some(entity); + true + } + fn cell_size(&self) -> Vec2 { self.bounds.size() / vec2(self.columns as f32, -(self.rows as f32)) } + + fn indexify_position(&self, position: Vec2) -> usize { + let indexified_position = (position / self.cell_size()).floor(); + indexified_position.x as usize + indexified_position.y as usize * self.columns + } } fn setup_grid(mut commands: Commands) { diff --git a/src/main.rs b/src/main.rs index f6a8d8a..057fd94 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod card; mod dev_tools; mod grid; mod turns; +mod unit; fn main() { App::new() @@ -17,6 +18,7 @@ fn main() { dev_tools::DevToolsPlugin, grid::GridPlugin, turns::TurnSystemPlugin, + unit::UnitPlugin, )) .add_systems(Startup, setup) .run(); diff --git a/src/unit/mod.rs b/src/unit/mod.rs new file mode 100644 index 0000000..eb3d1eb --- /dev/null +++ b/src/unit/mod.rs @@ -0,0 +1,25 @@ +use bevy::prelude::*; + +pub struct UnitPlugin; +impl Plugin for UnitPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Startup, unit_setup); + } +} + +fn unit_setup(mut commands: Commands, asset_server: Res) { + commands.insert_resource(UnitTextures { + fosma: asset_server.load("sprites/units/fosma.png"), + }); +} + +#[derive(Resource)] +pub struct UnitTextures { + fosma: Handle, +} + +impl UnitTextures { + pub fn fosma_texture(&self) -> Handle { + self.fosma.clone() + } +}