Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions src/gameplay/characters/components.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -25,7 +24,7 @@ pub struct CharacterPhysics;

#[derive(Component)]
pub struct CharacterSprite {
pub centering_transform: Vec3
pub centering_transform: Vec3,
}

#[derive(Bundle)]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
}
}
}
}
17 changes: 15 additions & 2 deletions src/gameplay/characters/stats/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
use crate::gameplay::ui::elements::party_status::PlayerHealthBar;
use crate::ui::bars::Bar;
use bevy::prelude::*;

use blake2::digest::Update;

pub struct StatsPlugin;

/// This plugin handles player related stuff like movement
/// 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);
}
}

#[derive(Component, Default)]
pub struct CharacterStats {
pub health: u32,
pub mana: u32,
}
pub max_health: u32,
}

fn sync_health_bar(
mut bar_query: Query<&mut Bar, With<PlayerHealthBar>>,
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);
}
61 changes: 30 additions & 31 deletions src/gameplay/ui/elements/party_status.rs
Original file line number Diff line number Diff line change
@@ -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.);
Expand All @@ -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);

Expand All @@ -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 {
Expand All @@ -90,6 +89,6 @@ fn character_status_text(text: &str, color: Color) -> TextBundle {
font_size: STATUS_TEXT_SIZE,
color: color,
..default()
}
},
)
}
}
2 changes: 1 addition & 1 deletion src/gameplay/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
63 changes: 33 additions & 30 deletions src/ui/bars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ impl Plugin for UiBarPlugin {

fn update_bar(
bar_query: Query<(&Children, &Bar), Changed<Bar>>,
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 {
Expand All @@ -30,7 +30,7 @@ fn update_bar(
drawn_area += current_area;
}
}
}
}
}

pub fn spawn_bar(
Expand All @@ -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;
Expand Down Expand Up @@ -100,7 +102,7 @@ impl Bar {
sections: sections,
total_size: total_size,
empty_color: empty_color,
dimensions: dimensions
dimensions: dimensions,
}
}

Expand Down Expand Up @@ -155,4 +157,5 @@ impl Bar {
self.total_size += amount;
self
}
}
}