refactor: utilise a shared struct for common stats between entities

This commit is contained in:
Muhammad Nauman Raza 2024-07-02 12:47:58 +01:00
parent 61cf4b9f74
commit 91d5deca3e
Signed by: devraza
GPG key ID: 91EAD6081011574B
5 changed files with 41 additions and 39 deletions

View file

@ -2,22 +2,15 @@ use bevy::prelude::*;
use std::f32::consts::PI;
use crate::player::*;
use crate::helpers::*;
// Define the enemy component
#[derive(Component)]
pub struct Enemy {
pub name: String,
pub movement_speed: f32,
/*
pub rotation_speed: f32,
pub health: f32,
pub health_max: f32,
pub stamina: f32,
pub stamina_max: f32,
pub mana: f32,
pub mana_max: f32,
*/
pub stats: CommonStats,
}
// Define the enemy movement system

View file

@ -5,3 +5,14 @@ pub fn titlecase(s: &str) -> String {
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
}
}
// Struct for common values between all entities (players and enemies)
pub struct CommonStats {
pub health: f32,
pub health_max: f32,
pub stamina: f32,
pub stamina_max: f32,
pub mana: f32,
pub mana_max: f32,
}

View file

@ -100,12 +100,14 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
movement_speed: 512.,
rotation_speed: f32::to_radians(360.),
health: 10.,
health_max: 10.,
stamina: 10.,
stamina_max: 10.,
mana: 100.,
mana_max: 100.,
stats: CommonStats {
health: 10.,
health_max: 10.,
stamina: 10.,
stamina_max: 10.,
mana: 100.,
mana_max: 100.,
}
});
// Spawn an enemy
@ -121,15 +123,14 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
.insert(Enemy {
name: "Goblin".to_string(),
movement_speed: 256.,
/*
rotation_speed: f32::to_radians(360.),
health: 10.,
health_max: 10.,
stamina: 10.,
stamina_max: 10.,
mana: 100.,
mana_max: 100.,
*/
stats: CommonStats {
health: 5.,
health_max: 5.,
stamina: 8.,
stamina_max: 8.,
mana: 50.,
mana_max: 50.,
}
});
}

View file

@ -1,17 +1,14 @@
use bevy::prelude::*;
use crate::helpers::*;
// Define the player component
#[derive(Component)]
pub struct Player {
pub movement_speed: f32,
pub rotation_speed: f32,
pub health: f32,
pub health_max: f32,
pub stamina: f32,
pub stamina_max: f32,
pub mana: f32,
pub mana_max: f32,
pub stats: CommonStats,
}
// Define the attacking component
@ -50,9 +47,9 @@ pub fn player_movement(
let mut is_dashing = false;
// Dash on space key press if the player has the stamina
if keys.just_pressed(KeyCode::Space) && player.stamina >= 1. {
if keys.just_pressed(KeyCode::Space) && player.stats.stamina >= 1. {
is_dashing = true;
player.stamina -= 1.;
player.stats.stamina -= 1.;
movement_distance = 256.;
}
@ -101,7 +98,7 @@ pub fn player_attack(
damage: 20.,
});
player.mana -= 1.;
player.stats.mana -= 1.;
}
}
@ -128,7 +125,7 @@ fn camera_follow(
pub fn player_regen(mut player_query: Query<&mut Player, With<Player>>, time: Res<Time>) {
let mut player = player_query.single_mut();
if (player.stamina / player.stamina_max) < 1. {
player.stamina += 0.1 * time.delta_seconds();
if (player.stats.stamina / player.stats.stamina_max) < 1. {
player.stats.stamina += 0.1 * time.delta_seconds();
}
}

View file

@ -117,16 +117,16 @@ pub fn render_ui(
.spacing(egui::Vec2::new(20., 10.))
.show(ui, |ui| {
let health_bar =
egui::widgets::ProgressBar::new(player.health / player.health_max)
egui::widgets::ProgressBar::new(player.stats.health / player.stats.health_max)
.desired_width(window_width / 10.);
let mut stamina_bar =
egui::widgets::ProgressBar::new(player.stamina / player.stamina_max)
egui::widgets::ProgressBar::new(player.stats.stamina / player.stats.stamina_max)
.desired_width(window_width / 10.);
let mana_bar = egui::widgets::ProgressBar::new(player.mana / player.mana_max)
let mana_bar = egui::widgets::ProgressBar::new(player.stats.mana / player.stats.mana_max)
.desired_width(window_width / 10.);
// Show the stamina bar to be empty if the player has no stamina
if player.stamina <= 0. {
if player.stats.stamina <= 0. {
stamina_bar =
stamina_bar.fill(egui::Color32::from_rgba_unmultiplied(0, 0, 0, 0));
};