diff --git a/game/src/editor.rs b/game/src/editor.rs index d70ddcd..1153610 100644 --- a/game/src/editor.rs +++ b/game/src/editor.rs @@ -1,16 +1,24 @@ #![allow(dead_code)] +use std::f64::consts::PI; use std::{collections::HashSet, time::Duration}; use crate::engine::{Color, Key, Renderer, Scene, Shape, V3}; use crate::engine; +enum MutExcKey { + Lhs, + Rhs, + None, +} + struct Editor { objects: Vec, next_object_id: u32, keys_pressed: HashSet, camera_pos: V3, + camera_rot: V3, } impl Editor { @@ -20,6 +28,7 @@ impl Editor { next_object_id: 0, keys_pressed: HashSet::new(), camera_pos: V3(0.0, 0.0, -1.0), + camera_rot: V3(0.0, 0.0, 0.0), } } @@ -40,6 +49,17 @@ impl Editor { .expect("doesn't exist"); self.objects.remove(index); } + + pub fn mut_exc_keys(&self, lhs: Key, rhs: Key) -> MutExcKey { + match ( + self.keys_pressed.contains(&lhs), + self.keys_pressed.contains(&rhs), + ) { + (true, false) => MutExcKey::Lhs, + (false, true) => MutExcKey::Rhs, + _ => MutExcKey::None, + } + } } impl engine::Game for Editor { @@ -47,10 +67,56 @@ impl engine::Game for Editor { for object in &mut self.objects { object.update(delta_time); } - if self.keys_pressed.contains(&Key::Left) { - self.camera_pos.0 += delta_time.as_secs_f64() * 1.0; - } else if self.keys_pressed.contains(&Key::Right) { - self.camera_pos.0 -= delta_time.as_secs_f64() * 1.0; + match self.mut_exc_keys(Key::A, Key::D) { + MutExcKey::Lhs => { + self.camera_pos += + V3(-delta_time.as_secs_f64() * 2.0, 0.0, 0.0).rotate(self.camera_rot * -1.0); + } + MutExcKey::Rhs => { + self.camera_pos += + V3(delta_time.as_secs_f64() * 2.0, 0.0, 0.0).rotate(self.camera_rot * -1.0); + } + _ => {} + } + match self.mut_exc_keys(Key::LShift, Key::LCtrl) { + MutExcKey::Lhs => { + self.camera_pos += + V3(0.0, delta_time.as_secs_f64() * 2.0, 0.0).rotate(self.camera_rot * -1.0); + } + MutExcKey::Rhs => { + self.camera_pos += + V3(0.0, -delta_time.as_secs_f64() * 2.0, 0.0).rotate(self.camera_rot * -1.0); + } + _ => {} + } + match self.mut_exc_keys(Key::W, Key::S) { + MutExcKey::Lhs => { + self.camera_pos += + V3(0.0, 0.0, delta_time.as_secs_f64() * 2.0).rotate(self.camera_rot * -1.0); + } + MutExcKey::Rhs => { + self.camera_pos += + V3(0.0, 0.0, -delta_time.as_secs_f64() * 2.0).rotate(self.camera_rot * -1.0); + } + _ => {} + } + match self.mut_exc_keys(Key::Up, Key::Down) { + MutExcKey::Lhs => { + self.camera_rot.0 += PI * delta_time.as_secs_f64() * 1.0; + } + MutExcKey::Rhs => { + self.camera_rot.0 -= PI * delta_time.as_secs_f64() * 1.0; + } + _ => {} + } + match self.mut_exc_keys(Key::Left, Key::Right) { + MutExcKey::Lhs => { + self.camera_rot.1 += PI * delta_time.as_secs_f64() * 1.0; + } + MutExcKey::Rhs => { + self.camera_rot.1 -= PI * delta_time.as_secs_f64() * 1.0; + } + _ => {} } } @@ -59,7 +125,7 @@ impl engine::Game for Editor { for object in &self.objects { object.render(&mut scene); } - scene.render(r, self.camera_pos); + scene.render(r, self.camera_pos, self.camera_rot); } fn event(&mut self, event: engine::Event) { @@ -79,47 +145,6 @@ enum ObjectKind { Box { pos: V3, vel: V3 }, } -struct ShapeGroupShape { - shape: Shape, - offset: V3, -} - -struct ShapeGroup { - pub shapes: Vec, -} - -impl ShapeGroup { - pub fn new(shapes: Vec) -> Self { - Self { shapes } - } - pub fn rotate(mut self, rot: V3) -> Self { - for shape in &mut self.shapes { - shape.shape = shape - .shape - .translate(shape.offset) - .rotate(rot) - .translate(shape.offset * -1.0); - } - self - } - pub fn translate(mut self, offset: V3) -> Self { - for shape in &mut self.shapes { - shape.shape = shape.shape.translate(offset); - } - self - } - pub fn draw(self, pos: V3, scene: &mut Scene, outline_color: Color, fill_color: Color) { - for shape in self.shapes { - scene.draw_shape( - pos, - &shape.shape.translate(shape.offset), - outline_color, - fill_color, - ); - } - } -} - impl Object { fn update(&mut self, delta_time: Duration) { match &mut self.kind { diff --git a/game/src/engine/game.rs b/game/src/engine/game.rs index 0136a86..2960304 100644 --- a/game/src/engine/game.rs +++ b/game/src/engine/game.rs @@ -32,6 +32,14 @@ pub enum Color { pub enum Key { Left, Right, + Up, + Down, + W, + A, + S, + D, + LShift, + LCtrl, } pub enum Event { diff --git a/game/src/engine/math.rs b/game/src/engine/math.rs index c006232..cc358d3 100644 --- a/game/src/engine/math.rs +++ b/game/src/engine/math.rs @@ -122,6 +122,10 @@ pub struct Triangle2(pub V2, pub V2, pub V2); pub struct Triangle3(pub V3, pub V3, pub V3); impl Triangle3 { + pub fn map V3>(&self, f: F) -> Self { + Self(f(self.0), f(self.1), f(self.2)) + } + pub fn normal(&self) -> V3 { (self.1 - self.0).cross(self.2 - self.1) } @@ -130,6 +134,10 @@ impl Triangle3 { Self(self.0 + offset, self.1 + offset, self.2 + offset) } + pub fn rotate(&self, rot: V3) -> Self { + self.map(|v| v.rotate(rot)) + } + pub fn project_2d(&self, camera_pos: V3) -> Triangle2 { Triangle2( self.0.project_2d(camera_pos), diff --git a/game/src/engine/scene.rs b/game/src/engine/scene.rs index 2497038..dd0e460 100644 --- a/game/src/engine/scene.rs +++ b/game/src/engine/scene.rs @@ -17,7 +17,7 @@ impl Scene { } } - pub fn render(&mut self, r: &mut R, camera_pos: V3) { + pub fn render(&mut self, r: &mut R, camera_pos: V3, camera_rot: V3) { let mut indices_with_scores = self .objects .iter() @@ -35,24 +35,28 @@ impl Scene { for (i, _) in indices_with_scores { let object = &self.objects[i]; + let tri3 = object + .triangle + .translate(camera_pos * -1.0) + .rotate(camera_rot); // check if behind camera - if !(object.triangle.0 .2 >= -1.0 - && object.triangle.1 .2 >= -1.0 - && object.triangle.2 .2 >= -1.0) - { + // TODO: doesn't take camera_pos or camera_rot into accout + if !(tri3.0 .2 >= 0.0 && tri3.1 .2 >= 0.0 && tri3.2 .2 >= 0.0) { continue; } - if object.triangle.normal().dot(object.triangle.0 - camera_pos) >= 0.0 { + if tri3.normal().dot(tri3.0 - camera_pos) >= 0.0 { continue; } - let triangle = object.triangle.project_2d(camera_pos); + let tri2 = tri3 + .translate(V3(0.0, 0.0, -1.0)) + .project_2d(V3(0.0, 0.0, -1.0)); - r.draw_triangle(triangle.clone(), object.fill_color); - r.draw_line(triangle.0, triangle.1, object.outline_color); - r.draw_line(triangle.0, triangle.2, object.outline_color); + r.draw_triangle(tri2.clone(), object.fill_color); + r.draw_line(tri2.0, tri2.1, object.outline_color); + r.draw_line(tri2.0, tri2.2, object.outline_color); } } diff --git a/game/src/engine/sdl_io.rs b/game/src/engine/sdl_io.rs index 6db0b89..89e91a3 100644 --- a/game/src/engine/sdl_io.rs +++ b/game/src/engine/sdl_io.rs @@ -66,35 +66,16 @@ impl SdlIo { } => break 'running, Event::KeyDown { keycode: Some(key), .. - } => match key { - Keycode::Left => { - game.event(game::Event::KeyDown { - key: game::Key::Left, - }); - } - Keycode::Right => { - game.event(game::Event::KeyDown { - key: game::Key::Right, - }); - } - _ => {} + } => match TryInto::::try_into(key).ok() { + Some(key) => game.event(game::Event::KeyDown { key }), + None => {} }, Event::KeyUp { keycode: Some(key), .. - } => match key { - Keycode::Left => { - game.event(game::Event::KeyUp { - key: game::Key::Left, - }); - } - Keycode::Right => { - game.event(game::Event::KeyUp { - key: game::Key::Right, - }); - } - _ => {} + } => match TryInto::::try_into(key).ok() { + Some(key) => game.event(game::Event::KeyUp { key }), + None => {} }, - _ => {} } } @@ -230,3 +211,23 @@ impl From for FColor { FColor::from(>::into(value)) } } + +impl TryFrom for game::Key { + type Error = (); + + fn try_from(value: Keycode) -> Result { + Ok(match value { + Keycode::Left => game::Key::Left, + Keycode::Right => game::Key::Right, + Keycode::Up => game::Key::Up, + Keycode::Down => game::Key::Down, + Keycode::W => game::Key::W, + Keycode::A => game::Key::A, + Keycode::S => game::Key::S, + Keycode::D => game::Key::D, + Keycode::LShift => game::Key::LShift, + Keycode::LCtrl => game::Key::LCtrl, + _ => return Err(()), + }) + } +} diff --git a/game/src/game.rs b/game/src/game.rs index ce59754..3079f17 100644 --- a/game/src/game.rs +++ b/game/src/game.rs @@ -86,7 +86,7 @@ impl engine::Game for Game { for object in &self.objects { object.render(&mut scene); } - scene.render(r, V3(0.0, 0.0, -1.0)); + scene.render(r, V3(0.0, 0.0, -1.0), V3(0.0, 0.0, 0.0)); } fn event(&mut self, event: engine::Event) {