From d949a952bb804b8423a8155d6be1503488d5ee9e Mon Sep 17 00:00:00 2001 From: Thom Bruce Date: Wed, 25 Oct 2023 03:02:04 +0100 Subject: [PATCH 1/4] show damage at ship position when hit --- CHANGELOG.md | 4 +++ src/ui/damage.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ src/ui/mod.rs | 3 +++ 3 files changed, 71 insertions(+) create mode 100644 src/ui/damage.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index de39dc6..7981bc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- UI damage indicators appear when a ship is hit + ### Changed - Camera scaling to 1.5, more zoomed out, see enemies on screen for longer diff --git a/src/ui/damage.rs b/src/ui/damage.rs new file mode 100644 index 0000000..24ca094 --- /dev/null +++ b/src/ui/damage.rs @@ -0,0 +1,64 @@ +use bevy::prelude::*; + +use crate::{ + core::resources::{assets::UiAssets, state::GameState}, + ships::{ + bullet::BulletShipContactEvent, + ship::{AttackSet, Ship}, + }, +}; + +pub struct UiDamagePlugin; +impl Plugin for UiDamagePlugin { + fn build(&self, app: &mut App) { + app.add_systems( + Update, + (ui_spawn_damage, ui_update_damage) + .after(AttackSet) + .run_if(in_state(GameState::Active)), + ); + } +} + +fn ui_spawn_damage( + mut commands: Commands, + mut bullet_ship_contact_events: EventReader, + ship_transform: Query<&Transform, With>, + camera_query: Query<(&Camera, &GlobalTransform)>, + ui: Res, +) { + for event in bullet_ship_contact_events.iter() { + if let Ok(transform) = ship_transform.get(event.ship) { + // Use camera.world_to_viewport() and camera GlobalTransform to translate + // a world position into UI coordinates + let (camera, camera_transform) = camera_query.single(); + let coords = camera + .world_to_viewport(camera_transform, transform.translation) + .unwrap(); + + commands.spawn(TextBundle { + text: Text::from_section( + "100", + TextStyle { + font: ui.font.clone(), + font_size: 25.0, + color: Color::RED, + ..default() + }, + ), + style: Style { + position_type: PositionType::Absolute, + top: Val::Px(coords.y), + left: Val::Px(coords.x), + ..default() + }, + ..default() + }); + } + } +} + +fn ui_update_damage() { + // TODO: Update damage text position + // TODO: Despawn damage on a timer (maybe reuse the bullet despawn timer for this?) +} diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 392c20d..832bc0c 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,11 +1,13 @@ use bevy::prelude::*; pub mod camera; +pub mod damage; pub mod hud; pub mod menus; use self::{ camera::CameraPlugin, + damage::UiDamagePlugin, hud::HudPlugin, menus::{credits::CreditsPlugin, pause::PausePlugin, start_menu::MenuPlugin}, }; @@ -19,6 +21,7 @@ impl Plugin for UiPlugin { CreditsPlugin, PausePlugin, CameraPlugin, + UiDamagePlugin, )); } } From 92347cfbe59839c2c1f8567eb884e3d78e4c8a67 Mon Sep 17 00:00:00 2001 From: Thom Bruce Date: Wed, 25 Oct 2023 03:20:43 +0100 Subject: [PATCH 2/4] despawn UI damage after half a second --- src/core/mod.rs | 10 ++++++-- src/core/resources/despawn_timer.rs | 26 +++++++++++++++++++++ src/core/resources/mod.rs | 1 + src/ships/bullet.rs | 26 ++++----------------- src/ui/damage.rs | 36 +++++++++++++++-------------- 5 files changed, 59 insertions(+), 40 deletions(-) create mode 100644 src/core/resources/despawn_timer.rs diff --git a/src/core/mod.rs b/src/core/mod.rs index e1b3993..fd54b3b 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -5,12 +5,18 @@ pub mod resources; use self::{ effects::{animate::AnimatePlugin, blink::EffectsPlugin}, - resources::{game_time::GameTimePlugin, state::StatePlugin}, + resources::{despawn_timer::DespawnTimerPlugin, game_time::GameTimePlugin, state::StatePlugin}, }; pub struct CorePlugin; impl Plugin for CorePlugin { fn build(&self, app: &mut App) { - app.add_plugins((GameTimePlugin, StatePlugin, EffectsPlugin, AnimatePlugin)); + app.add_plugins(( + GameTimePlugin, + StatePlugin, + EffectsPlugin, + AnimatePlugin, + DespawnTimerPlugin, + )); } } diff --git a/src/core/resources/despawn_timer.rs b/src/core/resources/despawn_timer.rs new file mode 100644 index 0000000..9ce31d0 --- /dev/null +++ b/src/core/resources/despawn_timer.rs @@ -0,0 +1,26 @@ +use bevy::prelude::*; + +use crate::core::resources::state::GameState; + +#[derive(Component)] +pub struct DespawnTimer(pub Timer); + +pub struct DespawnTimerPlugin; +impl Plugin for DespawnTimerPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Update, despawn_system.run_if(in_state(GameState::Active))); + } +} + +fn despawn_system( + mut commands: Commands, + time: Res