From 9161af2b835d9bb89b2ae262b950ee5378001269 Mon Sep 17 00:00:00 2001 From: Melik Houij Date: Fri, 31 Mar 2023 09:13:38 +0200 Subject: [PATCH] removed some code and made it move properly --- src/scene.rs | 54 ++++++++++++++++++++++++-------------------- src/scene/objects.rs | 38 ++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/src/scene.rs b/src/scene.rs index df412f9..cd97b3f 100644 --- a/src/scene.rs +++ b/src/scene.rs @@ -40,6 +40,7 @@ pub struct Scene { shaders: Vec>, time: std::time::Instant, DebugMode: bool, + input: objects::Input } impl Scene{ @@ -65,7 +66,8 @@ impl Scene{ debug, shaders: vec![shader], time, - DebugMode: false + DebugMode: false, + input: objects::Input::default() } } @@ -77,24 +79,6 @@ impl Scene{ self.gl.enable(glow::DEPTH_TEST); self.gl.clear(glow::COLOR_BUFFER_BIT | glow::DEPTH_BUFFER_BIT); } - /* - unsafe { - - self.shaders[0].Use(); - self.tempData.projection = perspective(Deg(self.camera.Zoom), SCR_WIDTH as f32 /SCR_HEIGHT as f32, 0.1, 100.0); - let view = self.camera.GetViewMatrix(); - self.shaders[0].setMat4("projection", &self.tempData.projection); - self.shaders[0].setMat4("view", &view); - - let mut model: Matrix4 = Matrix4::from_axis_angle(vec3(0.0, -1.0, 0.0).normalize(), - cgmath::Rad(1.0)); - - model = model * Matrix4::from_scale(0.2); - self.shaders[0].setMat4("model", &model); - } - */ - self.Car.update(); - self.Car.Draw(&self.shaders[0], &self.camera); if self.DebugMode{ @@ -105,6 +89,7 @@ impl Scene{ if self.DebugMode { self.debug.handle(&event); } + match event { Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { @@ -115,12 +100,31 @@ impl Scene{ Event::KeyDown {keycode: Some(Keycode::Q), ..} => { self.DebugMode = !self.DebugMode }, - Event::KeyDown {keycode: Some(Keycode::W), ..} => { - self.Car.forward(); + Event::KeyDown {keycode: Some(Keycode::W), repeat: false, ..} => { + self.input.Accel = true; + }, + Event::KeyUp {keycode: Some(Keycode::W), ..} => { + self.input.Accel = false; + }, + Event::KeyDown {keycode: Some(Keycode::S), repeat: false, ..} => { + self.input.Decel = true; + }, + Event::KeyUp {keycode: Some(Keycode::S), ..} => { + self.input.Decel = false; + }, + Event::KeyDown {keycode: Some(Keycode::D), repeat: false, ..} => { + self.input.Right = true; + }, + Event::KeyUp {keycode: Some(Keycode::D), ..} => { + self.input.Right = false; + }, + + Event::KeyDown {keycode: Some(Keycode::A), repeat: false, ..} => { + self.input.Left = true; + }, + Event::KeyUp {keycode: Some(Keycode::A), ..} => { + self.input.Left = false; }, - Event::KeyDown {keycode: Some(Keycode::A), .. }=> { - self.Car.turn(2.5); - } Event::KeyDown {keycode: Some(Keycode::M), ..} => { self.debug.addWindow(debug::OpenWindows::ModelInfo) }, @@ -129,6 +133,8 @@ impl Scene{ } + self.Car.update(&self.input); + self.Car.Draw(&self.shaders[0], &self.camera); ret } diff --git a/src/scene/objects.rs b/src/scene/objects.rs index c86300c..2272d2b 100644 --- a/src/scene/objects.rs +++ b/src/scene/objects.rs @@ -7,6 +7,23 @@ use cgmath::prelude::*; use crate::model::Model; use crate::camera::Camera; +pub struct Input { + pub Accel: bool, + pub Decel: bool, + pub Left: bool, + pub Right: bool +} + +impl Default for Input { + fn default() -> Input { + Input { + Accel: false, + Decel: false, + Left: false, + Right: false + } + } +} pub struct Transform { pub Position: Vector3, pub Rotation: Quaternion, @@ -27,6 +44,7 @@ impl Default for Transform { } } + impl Transform { pub fn setPos(&mut self, input: Vector3) { @@ -47,15 +65,29 @@ impl Player { } } - pub fn update(&mut self) + pub fn update(&mut self, input: &Input) { + if input.Accel { + self.forward(0.01); + } + if input.Decel { + self.forward(-0.01) + } + + if input.Left { + self.turn(1.5) + } + if input.Right { + self.turn(-1.5) + } + self.Transform.Position += self.Transform.Velocity; self.Transform.Rotation = self.Transform.Rotation.normalize(); } - pub fn forward(&mut self) + pub fn forward(&mut self, amount: f32) { let forward = self.Transform.Rotation.rotate_vector(cgmath::Vector3::unit_z()); - let distance = forward * 0.01; + let distance = forward * amount; self.Transform.Position += distance; }