refactor: utilise a shared struct for common stats between entities
This commit is contained in:
parent
61cf4b9f74
commit
91d5deca3e
5 changed files with 41 additions and 39 deletions
11
src/enemy.rs
11
src/enemy.rs
|
@ -2,22 +2,15 @@ use bevy::prelude::*;
|
||||||
use std::f32::consts::PI;
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
use crate::player::*;
|
use crate::player::*;
|
||||||
|
use crate::helpers::*;
|
||||||
|
|
||||||
// Define the enemy component
|
// Define the enemy component
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Enemy {
|
pub struct Enemy {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub movement_speed: f32,
|
pub movement_speed: f32,
|
||||||
/*
|
|
||||||
pub rotation_speed: f32,
|
|
||||||
|
|
||||||
pub health: f32,
|
pub stats: CommonStats,
|
||||||
pub health_max: f32,
|
|
||||||
pub stamina: f32,
|
|
||||||
pub stamina_max: f32,
|
|
||||||
pub mana: f32,
|
|
||||||
pub mana_max: f32,
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define the enemy movement system
|
// Define the enemy movement system
|
||||||
|
|
|
@ -5,3 +5,14 @@ pub fn titlecase(s: &str) -> String {
|
||||||
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
19
src/main.rs
19
src/main.rs
|
@ -100,12 +100,14 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
movement_speed: 512.,
|
movement_speed: 512.,
|
||||||
rotation_speed: f32::to_radians(360.),
|
rotation_speed: f32::to_radians(360.),
|
||||||
|
|
||||||
|
stats: CommonStats {
|
||||||
health: 10.,
|
health: 10.,
|
||||||
health_max: 10.,
|
health_max: 10.,
|
||||||
stamina: 10.,
|
stamina: 10.,
|
||||||
stamina_max: 10.,
|
stamina_max: 10.,
|
||||||
mana: 100.,
|
mana: 100.,
|
||||||
mana_max: 100.,
|
mana_max: 100.,
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Spawn an enemy
|
// Spawn an enemy
|
||||||
|
@ -121,15 +123,14 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
.insert(Enemy {
|
.insert(Enemy {
|
||||||
name: "Goblin".to_string(),
|
name: "Goblin".to_string(),
|
||||||
movement_speed: 256.,
|
movement_speed: 256.,
|
||||||
/*
|
|
||||||
rotation_speed: f32::to_radians(360.),
|
|
||||||
|
|
||||||
health: 10.,
|
stats: CommonStats {
|
||||||
health_max: 10.,
|
health: 5.,
|
||||||
stamina: 10.,
|
health_max: 5.,
|
||||||
stamina_max: 10.,
|
stamina: 8.,
|
||||||
mana: 100.,
|
stamina_max: 8.,
|
||||||
mana_max: 100.,
|
mana: 50.,
|
||||||
*/
|
mana_max: 50.,
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,14 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::helpers::*;
|
||||||
|
|
||||||
// Define the player component
|
// Define the player component
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
pub movement_speed: f32,
|
pub movement_speed: f32,
|
||||||
pub rotation_speed: f32,
|
pub rotation_speed: f32,
|
||||||
|
|
||||||
pub health: f32,
|
pub stats: CommonStats,
|
||||||
pub health_max: f32,
|
|
||||||
pub stamina: f32,
|
|
||||||
pub stamina_max: f32,
|
|
||||||
pub mana: f32,
|
|
||||||
pub mana_max: f32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define the attacking component
|
// Define the attacking component
|
||||||
|
@ -50,9 +47,9 @@ pub fn player_movement(
|
||||||
let mut is_dashing = false;
|
let mut is_dashing = false;
|
||||||
|
|
||||||
// Dash on space key press if the player has the stamina
|
// 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;
|
is_dashing = true;
|
||||||
player.stamina -= 1.;
|
player.stats.stamina -= 1.;
|
||||||
movement_distance = 256.;
|
movement_distance = 256.;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,7 +98,7 @@ pub fn player_attack(
|
||||||
damage: 20.,
|
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>) {
|
pub fn player_regen(mut player_query: Query<&mut Player, With<Player>>, time: Res<Time>) {
|
||||||
let mut player = player_query.single_mut();
|
let mut player = player_query.single_mut();
|
||||||
if (player.stamina / player.stamina_max) < 1. {
|
if (player.stats.stamina / player.stats.stamina_max) < 1. {
|
||||||
player.stamina += 0.1 * time.delta_seconds();
|
player.stats.stamina += 0.1 * time.delta_seconds();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,16 +117,16 @@ pub fn render_ui(
|
||||||
.spacing(egui::Vec2::new(20., 10.))
|
.spacing(egui::Vec2::new(20., 10.))
|
||||||
.show(ui, |ui| {
|
.show(ui, |ui| {
|
||||||
let health_bar =
|
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.);
|
.desired_width(window_width / 10.);
|
||||||
let mut stamina_bar =
|
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.);
|
.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.);
|
.desired_width(window_width / 10.);
|
||||||
|
|
||||||
// Show the stamina bar to be empty if the player has no stamina
|
// 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 =
|
||||||
stamina_bar.fill(egui::Color32::from_rgba_unmultiplied(0, 0, 0, 0));
|
stamina_bar.fill(egui::Color32::from_rgba_unmultiplied(0, 0, 0, 0));
|
||||||
};
|
};
|
||||||
|
|
Reference in a new issue