feat: begin on stamina

Former-commit-id: d4d8c9a8e6ed2fb5483465a70a10c612be2823dc
This commit is contained in:
Muhammad Nauman Raza 2023-12-06 22:45:32 +00:00
parent 6fb06a102e
commit f9fb6c21da
6 changed files with 69 additions and 38 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 715 B

After

Width:  |  Height:  |  Size: 756 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 375 B

After

Width:  |  Height:  |  Size: 403 B

View file

@ -55,12 +55,22 @@ fn main() {
.init_resource::<UiState>()
.init_resource::<OpenWindows>()
.add_systems(Startup, (setup, setup_ui))
.add_systems(Update, (render_ui, movement, camera_follow))
.add_systems(Update, (render_ui, movement, camera_follow, player_regen))
.run();
}
// Bevy engine setup
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),
..default()
},
..default()
});
// Spawn the 2D camera
commands.spawn(Camera2dBundle {
camera: Camera {
hdr: true,
@ -69,34 +79,26 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
tonemapping: Tonemapping::TonyMcMapface,
..default()
});
commands.spawn(
SpriteBundle {
// Spawn the player
commands
.spawn(SpriteBundle {
texture: asset_server.load("player/player-4x.png"),
transform: Transform {
scale: Vec3::splat(0.2),
..default()
},
..default()
});
commands.spawn((
SpriteBundle {
texture: asset_server.load("player/player-4x.png"),
transform: Transform {
scale: Vec3::splat(0.2),
..default()
},
..default()
},
Player {
})
.insert(Player {
movement_speed: 512.,
rotation_speed: f32::to_radians(360.),
health: 10.,
health: 1.,
health_max: 10.,
stamina: 10.,
stamina: 1.,
stamina_max: 10.,
defence: 40.,
},
));
});
}

View file

@ -18,13 +18,25 @@ pub struct Player {
pub fn movement(
time: Res<Time>,
keys: Res<Input<KeyCode>>,
mut player: Query<(&Player, &mut Transform)>,
mut player_query: Query<(&mut Player, &mut Transform), With<Player>>,
) {
let (player, mut transform) = player.single_mut();
let (mut player, mut transform) = player_query.single_mut();
let mut rotation_factor = 0.;
let mut movement_factor = 0.;
let mut blink_factor = 0.;
let mut lock: bool = false;
if keys.pressed(KeyCode::Space) && lock == false {
lock = true;
movement_factor = 5.;
if player.stamina > 0. {
player.stamina -= 0.05;
}
if player.stamina < 0. {
player.stamina = 0.;
}
}
if keys.pressed(KeyCode::W) {
movement_factor += 1.;
@ -38,9 +50,8 @@ pub fn movement(
rotation_factor -= 1.;
}
if keys.pressed(KeyCode::Space) {
blink_factor += 4.;
}
// Initialise the movement distance variable (to bring it into scope)
let movement_distance: f32;
if keys.pressed(KeyCode::Up) {
transform.rotation = Quat::from_rotation_z((0_f32).to_radians());
@ -62,15 +73,12 @@ pub fn movement(
// Get the player's *forward* vector
let movement_direction = transform.rotation * Vec3::Y;
// Initialise the movement distance variable (to bring it into scope)
let movement_distance: f32;
if blink_factor == 0. {
if lock != true {
movement_distance = movement_factor * player.movement_speed * time.delta_seconds();
// Change the player rotation around the Z-axis only if not blinking
transform.rotate_z(rotation_factor * player.rotation_speed * time.delta_seconds());
} else {
movement_distance = blink_factor * player.movement_speed * 0.01;
movement_distance = player.stamina * movement_factor * player.movement_speed * time.delta_seconds();
}
// Update the player translation with the translation
@ -90,3 +98,15 @@ pub fn camera_follow(
camera_transform.translation.y = pos.y;
}
}
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 < 0. {
player.stamina = 0.;
player.stamina += 0.1 * time.delta_seconds();
} else {
player.stamina += 0.1 * time.delta_seconds();
}
}
}

View file

@ -39,10 +39,10 @@ pub fn render_ui(
mut windows: Query<&mut Window>,
mut ui_state: ResMut<UiState>,
mut open_windows: ResMut<OpenWindows>,
mut query: Query<&Player>,
mut player_query: Query<&mut Player, With<Player>>,
) {
// Query the player information and put it into scope
let player = query.single_mut();
let player = player_query.single_mut();
let window = windows.single_mut();
let window_width = window.resolution.width();
@ -101,6 +101,7 @@ pub fn render_ui(
ui.add_space(window_height / 22.);
});
});
egui::CentralPanel::default()
.frame(egui::containers::Frame {
fill: egui::Color32::TRANSPARENT,
@ -111,20 +112,28 @@ pub fn render_ui(
ui.visuals_mut().menu_rounding = egui::Rounding::ZERO;
ui.visuals_mut().selection.bg_fill = purple;
let health =
egui::widgets::ProgressBar::new(player.health).desired_width(window_width / 10.);
let stamina =
egui::widgets::ProgressBar::new(player.stamina).desired_width(window_width / 10.);
// Show player stats
egui::Grid::new("Stats")
.spacing(egui::Vec2::new(20., 10.))
.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)
.desired_width(window_width / 10.);
// Show the stamina bar to be empty if the player has no stamina
if player.stamina <= 0. {
stamina_bar =
stamina_bar.fill(egui::Color32::from_rgba_unmultiplied(0, 0, 0, 0));
};
// Show the progress bars!
ui.label(egui::RichText::new("Health").color(purple));
ui.add(health);
ui.add(health_bar);
ui.end_row();
ui.label(egui::RichText::new("Stamina").color(purple));
ui.add(stamina);
ui.add(stamina_bar);
ui.end_row();
});
});