-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from thombruce/feat/ui-damage
Feat/UI damage
- Loading branch information
Showing
10 changed files
with
172 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Time>, | ||
mut query: Query<(Entity, &mut DespawnTimer)>, | ||
) { | ||
for (entity, mut bullet) in query.iter_mut() { | ||
bullet.0.tick(time.delta()); | ||
if bullet.0.finished() { | ||
commands.entity(entity).despawn(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod assets; | ||
pub mod despawn_timer; | ||
pub mod game_time; | ||
pub mod state; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use bevy::prelude::*; | ||
|
||
use crate::{ | ||
core::resources::{assets::UiAssets, despawn_timer::DespawnTimer, state::GameState}, | ||
ships::{ | ||
bullet::BulletShipContactEvent, | ||
ship::{AttackSet, Ship}, | ||
}, | ||
}; | ||
|
||
use super::resources::top::Top; | ||
|
||
#[derive(Component)] | ||
struct UiTextFadeOut; | ||
|
||
pub struct UiDamagePlugin; | ||
impl Plugin for UiDamagePlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems( | ||
Update, | ||
(ui_spawn_damage, ui_text_fade_out) | ||
.after(AttackSet) | ||
.run_if(in_state(GameState::Active)), | ||
); | ||
} | ||
} | ||
|
||
fn ui_spawn_damage( | ||
mut commands: Commands, | ||
mut bullet_ship_contact_events: EventReader<BulletShipContactEvent>, | ||
ship_transform: Query<&Transform, With<Ship>>, | ||
camera_query: Query<(&Camera, &GlobalTransform)>, | ||
ui: Res<UiAssets>, | ||
) { | ||
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() | ||
}, | ||
DespawnTimer(Timer::from_seconds(0.5, TimerMode::Once)), | ||
UiTextFadeOut, | ||
Top(coords.y), | ||
)); | ||
} | ||
} | ||
} | ||
|
||
fn ui_text_fade_out( | ||
time: Res<Time>, | ||
mut text: Query<(&mut Text, &mut Style, &mut Top), With<UiTextFadeOut>>, | ||
) { | ||
for (mut txt, mut style, mut top) in text.iter_mut() { | ||
// Moves the damage text upwards every frame | ||
top.0 -= 100.0 * time.delta_seconds(); | ||
style.top = Val::Px(top.0); | ||
|
||
// Fades the damage text out every frame | ||
for section in txt.sections.iter_mut() { | ||
let current_alpha = section.style.color.a(); | ||
section | ||
.style | ||
.color | ||
.set_a(current_alpha - (2.0 * time.delta_seconds())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod top; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use bevy::prelude::*; | ||
|
||
// TODO: This is actually just some arbitrary value we wanted to be able to change. | ||
// It was originally created for credits scrolling and ported from there, | ||
// and is now also being used in the ui/damage.rs system. | ||
// Both systems wanted to be able to change the "top" value of a UI | ||
// element, set as a Val::[Px, Percent, VMax, VMin, VH, VW](f32) which | ||
// it seems we can't otherwise perform calculations on. | ||
// So what this is, really, is a wrapper around some f32 intended to be used | ||
// in some other UI element. | ||
// | ||
// We might therefore think about renaming it to something like Uif32 so that it | ||
// could be used more generally for Top, Bottom, Left, Right, Margin, Padding, etc. | ||
// | ||
// But what if we wanted to change two or more such values? An entity can't have | ||
// more than one of a single component, can it? | ||
// | ||
// No, but... we should create more of these and determine what's common about them, | ||
// see if we can implement some kind of inheritence, and also generalise the system(s) | ||
// that are responsible for converting these values into Val::* ones. | ||
// | ||
// Though, I'm not certain how possible that is... Are these values always in a | ||
// Style component? And how do we determine what kind of Val to set? | ||
// | ||
// Perhaps 'Top'/'Uif32Top'/* should have a secondary value indicating type? | ||
// Following this, the conversions could be handled in a single system here. | ||
#[derive(Component)] | ||
pub struct Top(pub f32); |