From 91d5deca3ea6cd73ded5d9439bfec1c52de35289 Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Tue, 2 Jul 2024 12:47:58 +0100 Subject: [PATCH] refactor: utilise a shared struct for common stats between entities --- src/enemy.rs | 11 ++--------- src/helpers.rs | 11 +++++++++++ src/main.rs | 31 ++++++++++++++++--------------- src/player.rs | 19 ++++++++----------- src/ui.rs | 8 ++++---- 5 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/enemy.rs b/src/enemy.rs index b8d96b4..a279d1f 100644 --- a/src/enemy.rs +++ b/src/enemy.rs @@ -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 diff --git a/src/helpers.rs b/src/helpers.rs index fe25221..7ae7ef7 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -5,3 +5,14 @@ pub fn titlecase(s: &str) -> String { Some(f) => f.to_uppercase().collect::() + 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, +} + diff --git a/src/main.rs b/src/main.rs index 26fd977..a3b6990 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,12 +100,14 @@ fn setup(mut commands: Commands, asset_server: Res) { 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) { .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., + } }); } diff --git a/src/player.rs b/src/player.rs index c9a82a2..e31fb42 100644 --- a/src/player.rs +++ b/src/player.rs @@ -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>, time: Res