feat: mana

This commit is contained in:
Muhammad Nauman Raza 2024-03-19 21:50:03 +00:00
parent 5778c9bf79
commit b42673076a
Signed by: devraza
GPG key ID: 91EAD6081011574B
3 changed files with 32 additions and 11 deletions

View file

@ -66,7 +66,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(SpriteBundle {
texture: asset_server.load("player/player-4x.png"),
transform: Transform {
scale: Vec3::splat(0.2),
scale: Vec3::splat(0.25),
..default()
},
..default()
@ -87,7 +87,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
.spawn(SpriteBundle {
texture: asset_server.load("player/player-4x.png"),
transform: Transform {
scale: Vec3::splat(0.2),
scale: Vec3::splat(0.25),
..default()
},
..default()
@ -96,9 +96,11 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
movement_speed: 512.,
rotation_speed: f32::to_radians(360.),
health: 1.,
health: 10.,
health_max: 10.,
stamina: 1.,
stamina: 10.,
stamina_max: 10.,
mana: 100.,
mana_max: 100.,
});
}

View file

@ -10,6 +10,8 @@ pub struct Player {
pub health_max: f32,
pub stamina: f32,
pub stamina_max: f32,
pub mana: f32,
pub mana_max: f32,
}
// Define the attacking component
@ -47,9 +49,9 @@ pub fn 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 >= 0.3 {
if keys.just_pressed(KeyCode::Space) && player.stamina >= 1. {
is_dashing = true;
player.stamina -= 0.3;
player.stamina -= 1.;
movement_distance = 256.;
}
@ -72,19 +74,21 @@ pub fn attack(
Query<&mut Transform, With<Attack>>,
Query<&Transform, With<Player>>
)>,
mut player_query: Query<&mut Player>,
mut commands: Commands,
asset_server: Res<AssetServer>,
) {
let mut player = player_query.single_mut();
for player_transform in set.p1().iter_mut() {
let attack_position = player_transform.translation + ((player_transform.rotation * Vec3::Y) * 50.);
let attack_position = player_transform.translation + ((player_transform.rotation * Vec3::Y) * 100.);
if keys.just_pressed(KeyCode::Enter) {
commands
.spawn(SpriteBundle {
texture: asset_server.load("attacks/stone_cannon.png"),
transform: Transform {
scale: Vec3::splat(0.2),
scale: Vec3::splat(0.3),
translation: attack_position,
rotation: player_transform.rotation,
},
@ -94,6 +98,8 @@ pub fn attack(
velocity: 10.,
damage: 20.,
});
player.mana -= 1.;
}
}
@ -119,7 +125,7 @@ pub 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 < 1. {
if (player.stamina / player.stamina_max) < 1. {
player.stamina += 0.1 * time.delta_seconds();
}
}

View file

@ -56,6 +56,12 @@ pub fn render_ui(
let black = KAGAYAKI.get("BLACK").unwrap();
let black = egui::Color32::from_rgb(black.0, black.1, black.2);
let blue = KAGAYAKI.get("BLUE").unwrap();
let blue = egui::Color32::from_rgb(blue.0, blue.1, blue.2);
let yellow = KAGAYAKI.get("YELLOW").unwrap();
let yellow = egui::Color32::from_rgb(yellow.0, yellow.1, yellow.2);
egui::Window::new("Login")
.anchor(egui::Align2::CENTER_CENTER, egui::Vec2::new(0., 0.))
.resizable(false)
@ -118,8 +124,11 @@ pub fn render_ui(
.show(ui, |ui| {
let health_bar = egui::widgets::ProgressBar::new(player.health)
.desired_width(window_width / 10.);
let mut stamina_bar = egui::widgets::ProgressBar::new(player.stamina)
let mut stamina_bar = egui::widgets::ProgressBar::new(player.stamina / player.stamina_max)
.desired_width(window_width / 10.);
let mana_bar = egui::widgets::ProgressBar::new(player.mana / player.mana_max)
.desired_width(window_width / 10.);
// Show the stamina bar to be empty if the player has no stamina
if player.stamina <= 0. {
@ -132,9 +141,13 @@ pub fn render_ui(
ui.add(health_bar);
ui.end_row();
ui.label(egui::RichText::new("Stamina").color(purple));
ui.label(egui::RichText::new("Stamina").color(yellow));
ui.add(stamina_bar);
ui.end_row();
ui.label(egui::RichText::new("Mana").color(blue));
ui.add(mana_bar);
ui.end_row();
});
});
}