feat: improved movement

Former-commit-id: bda1eca205f49d3b5c6e90fa5cf1a5ec9decb1cc
This commit is contained in:
Muhammad Nauman Raza 2023-12-05 13:51:40 +00:00
parent b24372284d
commit 76e3a0cc5d
Signed by: devraza
GPG key ID: 91EAD6081011574B
3 changed files with 63 additions and 14 deletions

View file

@ -1,4 +1,4 @@
use bevy::{core_pipeline::tonemapping::Tonemapping, prelude::*, window::*, winit::WinitSettings};
use bevy::{core_pipeline::tonemapping::Tonemapping, prelude::*, window::*};
use bevy_egui::EguiPlugin;
@ -54,8 +54,6 @@ fn main() {
))
.init_resource::<UiState>()
.init_resource::<OpenWindows>()
// Only run the app when there is user input, reducing resource usage
.insert_resource(WinitSettings::desktop_app())
.add_systems(Startup, (setup, setup_ui))
.add_systems(Update, (render_ui, movement))
.run();
@ -73,12 +71,23 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
});
commands.spawn((
SpriteBundle {
texture: asset_server.load("player/player.png"),
texture: asset_server.load("player/player-4x.png"),
transform: Transform {
scale: Vec3::splat(0.2),
..default()
},
..default()
},
Player {
movement_speed: 1024.,
movement_speed: 512.,
rotation_speed: f32::to_radians(360.),
health: 0.4,
health_max: 10.,
stamina: 0.1,
stamina_max: 10.,
defence: 40.,
},
));
}

View file

@ -5,6 +5,13 @@ use bevy::prelude::*;
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 defence: f32,
}
// Define the player movement system
@ -22,22 +29,36 @@ pub fn movement(
if keys.pressed(KeyCode::W) {
movement_factor += 1.;
}
if keys.pressed(KeyCode::S) {
} else if keys.pressed(KeyCode::S) {
movement_factor -= 1.;
}
if keys.pressed(KeyCode::A) {
rotation_factor += 1.;
}
if keys.pressed(KeyCode::D) {
} else if keys.pressed(KeyCode::D) {
rotation_factor -= 1.;
}
if keys.pressed(KeyCode::Space) {
blink_factor += 4.;
}
if keys.pressed(KeyCode::Space) && keys.just_released(KeyCode::Right) {
blink_factor += 4.;
};
if keys.pressed(KeyCode::Up) {
transform.rotation = Quat::from_rotation_z((0_f32).to_radians());
movement_factor = 1.;
}
if keys.pressed(KeyCode::Down) {
transform.rotation = Quat::from_rotation_z((180_f32).to_radians());
movement_factor = 1.;
}
if keys.pressed(KeyCode::Left) {
transform.rotation = Quat::from_rotation_z((90_f32).to_radians());
movement_factor = 1.;
}
if keys.pressed(KeyCode::Right) {
transform.rotation = Quat::from_rotation_z((270_f32).to_radians());
movement_factor = 1.;
}
// Get the player's *forward* vector
let movement_direction = transform.rotation * Vec3::Y;

View file

@ -3,6 +3,7 @@ use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts};
use crate::HYPERNOVA;
use crate::Player;
// Define UI resources
#[derive(Default, Resource)]
@ -38,7 +39,11 @@ pub fn render_ui(
mut windows: Query<&mut Window>,
mut ui_state: ResMut<UiState>,
mut open_windows: ResMut<OpenWindows>,
mut query: Query<&Player>,
) {
// Query the player information and put it into scope
let player = query.single_mut();
let window = windows.single_mut();
let window_width = window.resolution.width();
let window_height = window.resolution.height();
@ -96,4 +101,18 @@ pub fn render_ui(
ui.add_space(window_height / 22.);
});
});
egui::CentralPanel::default()
.frame(egui::containers::Frame {
fill: egui::Color32::TRANSPARENT,
inner_margin: egui::style::Margin::same(10.),
..default()
})
.show(ctx, |ui| {
let health = egui::widgets::ProgressBar::new(player.health)
.desired_width(window_width / 10.);
let defence = egui::widgets::ProgressBar::new(player.defence)
.desired_width(window_width / 10.);
ui.add(health);
ui.add(defence);
});
}