From ad332fb309f17b26facbeb142c4a7ff05b3adbe6 Mon Sep 17 00:00:00 2001 From: Simon From Jakobsen Date: Mon, 30 Mar 2026 15:52:23 +0200 Subject: [PATCH] draw skateboard --- game/src/engine/math.rs | 8 ++++ game/src/main.rs | 89 +++++++++++++++++++++++++++++++++-------- game/src/player.rs | 0 3 files changed, 81 insertions(+), 16 deletions(-) delete mode 100644 game/src/player.rs diff --git a/game/src/engine/math.rs b/game/src/engine/math.rs index ca249d7..129aedb 100644 --- a/game/src/engine/math.rs +++ b/game/src/engine/math.rs @@ -72,6 +72,14 @@ impl Mul for V3 { } } +impl Mul for V3 { + type Output = Self; + + fn mul(self, rhs: Self) -> Self::Output { + Self(self.0 * rhs.0, self.1 * rhs.1, self.2 * rhs.2) + } +} + impl Add<&Self> for V3 { type Output = Self; diff --git a/game/src/main.rs b/game/src/main.rs index 8b33b39..26f849c 100644 --- a/game/src/main.rs +++ b/game/src/main.rs @@ -1,6 +1,7 @@ #![allow(dead_code)] use std::{ + f64::consts::PI, sync::{Arc, Mutex}, time::Duration, }; @@ -58,7 +59,7 @@ impl engine::Game for Game { fn render(&mut self, r: &mut R) { let mut scene = Scene::new(); - for object in &mut self.objects { + for object in &self.objects { object.render(&mut scene); } scene.render(r, V3(0.0, 0.0, -1.0)); @@ -73,19 +74,63 @@ struct Object { } enum ObjectKind { - Player { pos: V3, vel: V3 }, + SkateBoard { pos: V3, vel: V3, rot: V3 }, Obstacle { pos: V3, vel: V3 }, Ground { pos: V3, vel_z: f64 }, } +struct MultiShapeShape { + shape: Shape, + offset: V3, +} + +struct MultiShape { + shapes: Vec, +} + +impl MultiShape { + 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 { - ObjectKind::Player { pos, vel } => { + ObjectKind::SkateBoard { pos, vel, rot } => { *pos += *vel * delta_time.as_secs_f64(); if pos.0 >= 2.1 { pos.0 = -2.0; } + // rot.0 += delta_time.as_secs_f64() * PI * 1.0; + rot.1 += delta_time.as_secs_f64() * PI * 1.0; + // rot.2 += delta_time.as_secs_f64() * PI * 2.0; } ObjectKind::Obstacle { pos, vel } => { *pos += *vel * delta_time.as_secs_f64(); @@ -96,19 +141,30 @@ impl Object { } } - fn render(&mut self, scene: &mut Scene) { - match &mut self.kind { - ObjectKind::Player { pos, .. } => { - scene.draw_shape( - *pos, - &Shape::new_cube(V3(0.2, 0.2, 0.2)), - Color::Green, - Color::Black, - ); + fn render(&self, scene: &mut Scene) { + match self.kind { + ObjectKind::SkateBoard { pos, rot, .. } => { + let board = MultiShape::new(vec![ + MultiShapeShape { + shape: Shape::new_cube(V3(0.2, 0.01, 0.05)), + offset: V3(0.0, 0.0, 0.0), + }, + MultiShapeShape { + shape: Shape::new_cube(V3(0.02, 0.02, 0.05)), + offset: V3(0.04, -0.02, 0.0), + }, + MultiShapeShape { + shape: Shape::new_cube(V3(0.02, 0.02, 0.05)), + offset: V3(0.14, -0.02, 0.0), + }, + ]) + .translate(V3(-0.1, -0.005, -0.0025)) + .rotate(rot); + board.draw(pos, scene, Color::Green, Color::Black); } ObjectKind::Obstacle { pos, .. } => { scene.draw_shape( - *pos, + pos, &Shape::new_cube(V3(0.2, 0.1, 0.2)), Color::Green, Color::Black, @@ -135,13 +191,14 @@ fn main() -> Result<(), Box> { let event_queue = Arc::new(Mutex::new(EventQueue::new())); let mut game = Game::new(event_queue.clone()); std::thread::spawn(move || { - let server = Server::bind("10.133.51.127:42069"); + let server = Server::bind("10.133.51.127:5000"); server.start(event_queue); }); let mut objects: Vec = Vec::new(); - objects.push(ObjectKind::Player { - pos: V3(-0.3, -0.25, 0.0), + objects.push(ObjectKind::SkateBoard { + pos: V3(0.0, -0.1, 0.0), vel: V3(0.0, 0.0, 0.0), + rot: V3(0.0, 0.0, 0.0), }); objects.push(ObjectKind::Ground { pos: V3(0.0, -0.3, -0.5), diff --git a/game/src/player.rs b/game/src/player.rs deleted file mode 100644 index e69de29..0000000