From 952818c5be6440f8f6c067a5e01ebd562213b321 Mon Sep 17 00:00:00 2001 From: rendo Date: Sun, 1 Feb 2026 19:07:50 +0500 Subject: [PATCH] Fosma --- src/grid.rs | 26 +++++++++++++++++++++++++- src/music_player.rs | 2 +- src/unit/doh.rs | 6 +++--- src/unit/fosma.rs | 25 +++++++++++++++++++++++-- src/unit/health.rs | 21 ++++++++++++++++++--- src/unit/mod.rs | 9 ++++++++- src/unit/zlosma.rs | 6 +++--- 7 files changed, 81 insertions(+), 14 deletions(-) diff --git a/src/grid.rs b/src/grid.rs index 4dd3b03..8bbb9af 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -7,10 +7,16 @@ pub struct GridPlugin; impl Plugin for GridPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, setup_grid) - .add_systems(Update, snap_grid_elements); + .add_systems(Update, snap_grid_elements) + .add_observer(grid_despawn_check); } } +#[derive(EntityEvent)] +pub struct GridDespawn { + pub entity: Entity, +} + #[derive(Component)] pub struct Grid { bounds: Rect, @@ -131,3 +137,21 @@ fn snap_grid_elements( } } } + +pub fn grid_despawn_check(event: On, mut grid_query: Single<&mut Grid>) { + for i in 0..grid_query.elements.len() { + match grid_query.elements[i] { + CellState::Empty => continue, + CellState::Filled(entity) => { + if entity == event.entity { + grid_query.elements[i] = CellState::Empty + } + } + CellState::Part(entity) => { + if entity == event.entity { + grid_query.elements[i] = CellState::Empty + } + } + } + } +} diff --git a/src/music_player.rs b/src/music_player.rs index 15be05f..610a20e 100644 --- a/src/music_player.rs +++ b/src/music_player.rs @@ -36,7 +36,7 @@ fn try_play( commands.entity(music_player.entity()).insert(( AudioPlayer::new(soundtrack.0[random_range(0..soundtrack.0.len())].clone()), PlaybackSettings { - volume: Volume::Linear(0.2), + volume: Volume::Linear(2.0), mode: bevy::audio::PlaybackMode::Remove, ..default() }, diff --git a/src/unit/doh.rs b/src/unit/doh.rs index 0f51e62..6df76ac 100644 --- a/src/unit/doh.rs +++ b/src/unit/doh.rs @@ -1,8 +1,8 @@ use crate::{ animation::transform::AnimatedTransform, - grid::Grid, + grid::{Grid, grid_despawn_check}, mouse_position::MousePosition, - turns::{TurnBusy, TurnState, TurnUnit}, + turns::{TurnBusy, TurnUnit}, unit::{UnitTextures, health::Health}, }; use bevy::prelude::*; @@ -23,7 +23,7 @@ pub fn spawn_doh( 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, + //TurnUnit, )) .id(); grid.try_set(mouse_position.position.truncate(), doh); diff --git a/src/unit/fosma.rs b/src/unit/fosma.rs index 532d243..48a2916 100644 --- a/src/unit/fosma.rs +++ b/src/unit/fosma.rs @@ -1,8 +1,8 @@ use crate::{ animation::transform::AnimatedTransform, - grid::Grid, + grid::{Grid, grid_despawn_check}, mouse_position::MousePosition, - turns::{TurnBusy, TurnState, TurnUnit}, + turns::{TurnBusy, TurnUnit}, unit::{UnitTextures, health::Health}, }; use bevy::prelude::*; @@ -28,3 +28,24 @@ pub fn spawn_fosma( .id(); grid.try_set(mouse_position.position.truncate(), fosma); } + +pub fn fosma_action( + mut commands: Commands, + grid: Single<&Grid>, + 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.)) + else { + commands.entity(entity).remove::(); + return; + }; + + if let Ok(mut health) = health_query.get_mut(unit) { + health.damage(10); + } + commands.entity(entity).remove::(); + } +} diff --git a/src/unit/health.rs b/src/unit/health.rs index ac85595..d410ea3 100644 --- a/src/unit/health.rs +++ b/src/unit/health.rs @@ -1,13 +1,28 @@ use bevy::prelude::*; +use crate::grid::GridDespawn; + #[derive(Component)] pub struct Health { - pub max_hp: u32, - pub hp: u32, + pub max_hp: i32, + pub hp: i32, } impl Health { - pub fn new(max_hp: u32) -> Self { + pub fn new(max_hp: i32) -> Self { Self { max_hp, hp: max_hp } } + + pub fn damage(&mut self, damage: i32) { + self.hp -= damage; + } +} + +pub fn health_despawn_system(mut commands: Commands, query: Query<(Entity, &Health)>) { + for (entity, health) in query { + if health.hp <= 0 { + commands.trigger(GridDespawn { entity }); + commands.entity(entity).despawn(); + } + } } diff --git a/src/unit/mod.rs b/src/unit/mod.rs index 88e2257..aa0315c 100644 --- a/src/unit/mod.rs +++ b/src/unit/mod.rs @@ -1,5 +1,10 @@ use bevy::prelude::*; +use crate::{ + turns::TurnState, + unit::{fosma::fosma_action, health::health_despawn_system}, +}; + pub mod doh; pub mod fosma; pub mod health; @@ -8,7 +13,9 @@ pub mod zlosma; pub struct UnitPlugin; impl Plugin for UnitPlugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, unit_setup); + app.add_systems(Startup, unit_setup) + .add_systems(OnEnter(TurnState::Turn), health_despawn_system) + .add_systems(Update, fosma_action); } } diff --git a/src/unit/zlosma.rs b/src/unit/zlosma.rs index 4c93849..2c88412 100644 --- a/src/unit/zlosma.rs +++ b/src/unit/zlosma.rs @@ -1,8 +1,8 @@ use crate::{ animation::transform::AnimatedTransform, - grid::Grid, + grid::{Grid, grid_despawn_check}, mouse_position::MousePosition, - turns::{TurnBusy, TurnState, TurnUnit}, + turns::{TurnBusy, TurnUnit}, unit::{UnitTextures, health::Health}, }; use bevy::prelude::*; @@ -23,7 +23,7 @@ pub fn spawn_zlosma( 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, + //TurnUnit, )) .id(); grid.try_set(mouse_position.position.truncate(), zlosma);