-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into lunacys/metaprogression
- Loading branch information
Showing
11 changed files
with
188 additions
and
6 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,16 @@ | ||
use bevy::prelude::*; | ||
|
||
pub mod game_timer; | ||
pub mod health_bar; | ||
|
||
use game_timer::GameTimerPlugin; | ||
use health_bar::HealthBarPlugin; | ||
|
||
pub struct UiPlugin; | ||
|
||
impl Plugin for UiPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_plugins(GameTimerPlugin) | ||
.add_plugins(HealthBarPlugin); | ||
} | ||
} |
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,61 @@ | ||
use bevy::prelude::*; | ||
use bevy::text::*; | ||
use bevy::time::Stopwatch; | ||
use std::time::Duration; | ||
use bevy::sprite::Anchor; | ||
|
||
pub struct GameTimerPlugin; | ||
|
||
impl Plugin for GameTimerPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems(Startup, spawn) | ||
.insert_resource(GameTimer(Stopwatch::default())) | ||
.add_systems(Update, update_timer); | ||
} | ||
} | ||
|
||
#[derive(Resource)] | ||
struct GameTimer(Stopwatch); | ||
|
||
#[derive(Component)] | ||
struct GameTimerText; | ||
|
||
fn spawn (mut commands: Commands) { | ||
commands.spawn(NodeBundle { | ||
style: Style { | ||
flex_direction: FlexDirection::Column, | ||
justify_self: JustifySelf::Center, | ||
..Default::default() | ||
}, | ||
z_index: ZIndex::Global(2), | ||
..Default::default() | ||
}).with_children(|parent| { | ||
parent.spawn(( | ||
TextBundle::from_section( | ||
"00:00", | ||
TextStyle { | ||
font_size: 50.0, | ||
color: Color::BLACK, | ||
..default() | ||
}, | ||
).with_text_justify(JustifyText::Center), | ||
GameTimerText, | ||
)); | ||
}); | ||
} | ||
|
||
fn update_timer( | ||
time: Res<Time>, | ||
mut timer: ResMut<GameTimer>, | ||
mut query: Query<&mut Text, With<GameTimerText>>, | ||
) { | ||
timer.0.tick(time.delta()); | ||
|
||
let elapsed_secs = timer.0.elapsed_secs(); | ||
let minutes = (elapsed_secs / 60.0).floor() as u32; | ||
let seconds = (elapsed_secs % 60.0).floor() as u32; | ||
|
||
for mut text in query.iter_mut() { | ||
text.sections[0].value = format!("{:02}:{:02}", minutes, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use bevy::prelude::*; | ||
use bevy::sprite::Anchor; | ||
use crate::stats::*; | ||
use crate::player::*; | ||
use vs_assets::plugin::UiAssets; | ||
|
||
pub struct HealthBarPlugin; | ||
|
||
impl Plugin for HealthBarPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems(Update, update); | ||
} | ||
} | ||
|
||
const WIDTH: f32 = 75.; | ||
const HEIGHT: f32 = 10.; | ||
|
||
#[derive(Component)] | ||
struct HealthBar; | ||
|
||
pub fn spawn_health_bar( | ||
child_builder: &mut ChildBuilder, | ||
assets: Res<UiAssets>, | ||
) { | ||
let bar_texture: Handle<Image> = assets.health_bar.clone(); | ||
let bar_outline_texture: Handle<Image> = assets.health_bar_outline.clone(); | ||
|
||
let sprite = Sprite { | ||
custom_size: Some(Vec2::new(WIDTH, HEIGHT)), | ||
anchor: Anchor::CenterLeft, | ||
color: Color::rgb(0.8, 0.1, 0.1), | ||
..Default::default() | ||
}; | ||
|
||
let transform = Transform::from_translation(Vec3::new(-WIDTH / 2., -35., 100.)); | ||
|
||
let health_bar_bundle = SpriteBundle { | ||
texture: bar_texture, | ||
sprite: sprite.clone(), | ||
transform: transform.clone(), | ||
..Default::default() | ||
}; | ||
child_builder.spawn((health_bar_bundle, HealthBar)); | ||
|
||
let health_bar_outline_bundle = SpriteBundle { | ||
texture: bar_outline_texture, | ||
transform, | ||
sprite, | ||
..Default::default() | ||
}; | ||
child_builder.spawn(health_bar_outline_bundle); | ||
} | ||
|
||
fn update( | ||
mut hp_bar: Query<&mut Sprite, With<HealthBar>>, | ||
health: Query<(&Health, &MaxHealth), With<Player>>, | ||
) { | ||
if let Ok(mut sprite) = hp_bar.get_single_mut() { | ||
if let Ok((health, max_health)) = health.get_single() { | ||
let percent = f32::clamp(health.0 as f32 / max_health.0 as f32, 0., 1.); | ||
sprite.custom_size = Some(Vec2::new(WIDTH * percent, HEIGHT)); | ||
} | ||
} | ||
} |