diff --git a/src/gameplay/characters/components.rs b/src/gameplay/characters/components.rs index 57c9f87..cc617b8 100644 --- a/src/gameplay/characters/components.rs +++ b/src/gameplay/characters/components.rs @@ -1,11 +1,10 @@ use crate::animations::Animatable; -use crate::gameplay::items::CharacterEquips; -use crate::gameplay::items::weapons::BARE_FISTS; use crate::gameplay::characters::stats::CharacterStats; +use crate::gameplay::items::weapons::BARE_FISTS; +use crate::gameplay::items::CharacterEquips; use bevy::prelude::*; use bevy_rapier2d::prelude::*; - #[derive(Component, Default, Clone, Eq, PartialEq, Debug, Hash)] pub enum GroundStatus { #[default] @@ -25,7 +24,7 @@ pub struct CharacterPhysics; #[derive(Component)] pub struct CharacterSprite { - pub centering_transform: Vec3 + pub centering_transform: Vec3, } #[derive(Bundle)] @@ -56,10 +55,18 @@ impl Default for CharacterPhysicsBundle { active_events: ActiveEvents::COLLISION_EVENTS, solver_group: SolverGroups::new(Group::GROUP_1, Group::GROUP_1.complement()), // markers to access rigidbody attributes - external_force: ExternalForce { ..Default::default() }, - external_impulse: ExternalImpulse { ..Default::default() }, - damping: Damping { ..Default::default() }, - velocity: Velocity { ..Default::default() }, + external_force: ExternalForce { + ..Default::default() + }, + external_impulse: ExternalImpulse { + ..Default::default() + }, + damping: Damping { + ..Default::default() + }, + velocity: Velocity { + ..Default::default() + }, colliding_entities: CollidingEntities::default(), ground_status: GroundStatus::default(), character_physics: CharacterPhysics, @@ -92,12 +99,20 @@ impl CharacterSpriteBundle { transform: transform, ..Default::default() }, - texture_atlas: TextureAtlas { ..Default::default() }, + texture_atlas: TextureAtlas { + ..Default::default() + }, animatable: animatable, facing: Facing::default(), - character_sprite: CharacterSprite { centering_transform: centering_transform }, + character_sprite: CharacterSprite { + centering_transform: centering_transform, + }, character_equips: CharacterEquips { weapon: BARE_FISTS }, - character_stats: CharacterStats { ..Default::default() }, + character_stats: CharacterStats { + health: 50, + mana: 100, + max_health: 100, + }, } } -} \ No newline at end of file +} diff --git a/src/gameplay/characters/stats/mod.rs b/src/gameplay/characters/stats/mod.rs index c4899da..38e5626 100644 --- a/src/gameplay/characters/stats/mod.rs +++ b/src/gameplay/characters/stats/mod.rs @@ -1,5 +1,7 @@ +use crate::gameplay::ui::elements::party_status::PlayerHealthBar; +use crate::ui::bars::Bar; use bevy::prelude::*; - +use blake2::digest::Update; pub struct StatsPlugin; @@ -7,6 +9,7 @@ pub struct StatsPlugin; /// Player logic is only active during the State `GameState::Playing` impl Plugin for StatsPlugin { fn build(&self, app: &mut App) { + app.add_systems(Update, sync_health_bar); } } @@ -14,4 +17,14 @@ impl Plugin for StatsPlugin { pub struct CharacterStats { pub health: u32, pub mana: u32, -} \ No newline at end of file + pub max_health: u32, +} + +fn sync_health_bar( + mut bar_query: Query<&mut Bar, With>, + player_stat_query: Query<&CharacterStats>, +) { + let player_stat = player_stat_query.single(); + let mut bar = bar_query.single_mut(); + bar.set_progress(player_stat.health as f32 / player_stat.max_health as f32); +} diff --git a/src/gameplay/ui/elements/party_status.rs b/src/gameplay/ui/elements/party_status.rs index 2901da4..4d08634 100644 --- a/src/gameplay/ui/elements/party_status.rs +++ b/src/gameplay/ui/elements/party_status.rs @@ -1,10 +1,6 @@ -use crate::ui::bars::{ - Bar, - spawn_bar, -}; +use crate::ui::bars::{spawn_bar, Bar}; use bevy::prelude::*; - const STATUS_BAR_LENGTH: f32 = 300.0; const STATUS_BAR_HEIGHT: f32 = 16.0; const HEALTH_BAR_COLOR: Color = Color::linear_rgb(1., 0., 0.); @@ -22,21 +18,26 @@ pub struct CharacterStatusUi; #[derive(Component)] pub struct HealthBar; +#[derive(Component)] +pub struct PlayerHealthBar; + pub fn setup_player_status_group(commands: &mut Commands, parent: Entity) -> Entity { - let party_status_group = commands.spawn(( - NodeBundle { - style: Style { - width: Val::Percent(100.0), - height: Val::Percent(100.0), - flex_direction: FlexDirection::Column, - align_items: AlignItems::Start, - justify_content: JustifyContent::Start, + let party_status_group = commands + .spawn(( + NodeBundle { + style: Style { + width: Val::Percent(100.0), + height: Val::Percent(100.0), + flex_direction: FlexDirection::Column, + align_items: AlignItems::Start, + justify_content: JustifyContent::Start, + ..default() + }, ..default() }, - ..default() - }, - PartyStatusUi, - )).id(); + PartyStatusUi, + )) + .id(); commands.entity(parent).add_child(party_status_group); @@ -61,26 +62,24 @@ pub fn spawn_character_status(commands: &mut Commands, parent_node: Entity) { health_bar.set_progress(0.7); mana_bar.set_progress(0.3); - let character_name = commands.spawn( - character_status_text("Character Name", Color::linear_rgb(255., 255., 255.)) - ).id(); + let character_name = commands + .spawn(character_status_text( + "Character Name", + Color::linear_rgb(255., 255., 255.), + )) + .id(); commands.entity(parent_node).add_child(character_name); - - spawn_bar( + + let player_health_bar = spawn_bar( commands, health_bar, parent_node, Val::Px(5.0), Val::Px(0.0), ); + commands.entity(player_health_bar).insert(PlayerHealthBar); - spawn_bar( - commands, - mana_bar, - parent_node, - Val::Px(5.0), - Val::Px(0.0), - ); + spawn_bar(commands, mana_bar, parent_node, Val::Px(5.0), Val::Px(0.0)); } fn character_status_text(text: &str, color: Color) -> TextBundle { @@ -90,6 +89,6 @@ fn character_status_text(text: &str, color: Color) -> TextBundle { font_size: STATUS_TEXT_SIZE, color: color, ..default() - } + }, ) -} \ No newline at end of file +} diff --git a/src/gameplay/ui/mod.rs b/src/gameplay/ui/mod.rs index f7a86c9..c75e17f 100644 --- a/src/gameplay/ui/mod.rs +++ b/src/gameplay/ui/mod.rs @@ -1,6 +1,6 @@ mod top_bar; mod bottom_bar; -mod elements; +pub mod elements; mod windows; use crate::gameplay::ui::bottom_bar::{ GameUiBottomBar, BottomBarPlugin}; diff --git a/src/ui/bars.rs b/src/ui/bars.rs index 8840c88..2e6356f 100644 --- a/src/ui/bars.rs +++ b/src/ui/bars.rs @@ -10,14 +10,14 @@ impl Plugin for UiBarPlugin { fn update_bar( bar_query: Query<(&Children, &Bar), Changed>, - mut active_bar_area_query: Query<(&mut Style, &ActiveBarArea)> + mut active_bar_area_query: Query<(&mut Style, &ActiveBarArea)>, ) { for (children, bar) in bar_query.iter() { let mut drawn_area: f32 = 0.0; for &child in children.iter() { let (mut style, active_bar_area) = active_bar_area_query.get_mut(child).unwrap(); let (size, c) = bar.sections[active_bar_area.0]; - + let current_area = size as f32 / bar.total_size as f32; if current_area + drawn_area <= bar.progress { @@ -30,7 +30,7 @@ fn update_bar( drawn_area += current_area; } } - } + } } pub fn spawn_bar( @@ -40,35 +40,37 @@ pub fn spawn_bar( margin_vert: Val, margin_hor: Val, ) -> Entity { - let bar = commands.spawn(( - NodeBundle { - style: Style { - width: Val::Px(bar.dimensions.x), - height: Val::Px(bar.dimensions.y), - margin: UiRect::axes(margin_hor, margin_vert), + let bar = commands + .spawn(( + NodeBundle { + style: Style { + width: Val::Px(bar.dimensions.x), + height: Val::Px(bar.dimensions.y), + margin: UiRect::axes(margin_hor, margin_vert), + ..default() + }, + background_color: bar.empty_color.into(), ..default() }, - background_color: bar.empty_color.into(), - ..default() - }, - bar.clone() - )).with_children(|parent| { - - for (index, (_, color)) in bar.sections.iter().enumerate() { - parent.spawn(( - NodeBundle { - style: Style { - width: Val::Px(30.0), - height: Val::Px(bar.dimensions.y), + bar.clone(), + )) + .with_children(|parent| { + for (index, (_, color)) in bar.sections.iter().enumerate() { + parent.spawn(( + NodeBundle { + style: Style { + width: Val::Px(30.0), + height: Val::Px(bar.dimensions.y), + ..default() + }, + background_color: (*color).into(), ..default() }, - background_color: (*color).into(), - ..default() - }, - ActiveBarArea(index), - )); - } - }).id(); + ActiveBarArea(index), + )); + } + }) + .id(); commands.entity(parent).add_child(bar); return bar; @@ -100,7 +102,7 @@ impl Bar { sections: sections, total_size: total_size, empty_color: empty_color, - dimensions: dimensions + dimensions: dimensions, } } @@ -155,4 +157,5 @@ impl Bar { self.total_size += amount; self } -} \ No newline at end of file +} +