From 76e3a0cc5d3a85dfb9527203dee155ce5e724a6d Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Tue, 5 Dec 2023 13:51:40 +0000 Subject: [PATCH] feat: improved movement Former-commit-id: bda1eca205f49d3b5c6e90fa5cf1a5ec9decb1cc --- src/main.rs | 19 ++++++++++++++----- src/player.rs | 39 ++++++++++++++++++++++++++++++--------- src/ui.rs | 19 +++++++++++++++++++ 3 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 62c936f..5571d77 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::() .init_resource::() - // 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) { }); 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., }, )); } diff --git a/src/player.rs b/src/player.rs index 23cf0d5..15daa81 100644 --- a/src/player.rs +++ b/src/player.rs @@ -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; diff --git a/src/ui.rs b/src/ui.rs index c91d54f..24fd957 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -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, mut open_windows: ResMut, + 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); + }); }