From 4f1ab9556a303f9b0aa2283f48f44f72883cdc0b Mon Sep 17 00:00:00 2001 From: Mikkel Troels Kongsted Date: Mon, 30 Mar 2026 10:04:10 +0200 Subject: [PATCH] add despawn and don't render objects behind camera --- game/src/engine/scene.rs | 3 ++ game/src/main.rs | 83 +++++++++++++++++++++++++++------------- 2 files changed, 59 insertions(+), 27 deletions(-) diff --git a/game/src/engine/scene.rs b/game/src/engine/scene.rs index 1f53856..31e97fe 100644 --- a/game/src/engine/scene.rs +++ b/game/src/engine/scene.rs @@ -39,6 +39,9 @@ impl Scene { let drawn_triangles = self .objects .iter() + .filter(|v| { + v.triangle.0 .2 >= -1.0 && v.triangle.1 .2 >= -1.0 && v.triangle.2 .2 >= -1.0 + }) .map(|v| (v.triangle.project_2d(), v.outline_color, v.fill_color)); for (triangle, outline_color, fill_color) in drawn_triangles { r.draw_triangle(triangle.clone(), fill_color); diff --git a/game/src/main.rs b/game/src/main.rs index 75ab0fb..1ddc692 100644 --- a/game/src/main.rs +++ b/game/src/main.rs @@ -9,17 +9,33 @@ mod vermiparous; struct Game { objects: Vec, + next_object_id: u32, } impl Game { fn new() -> Self { Self { objects: Vec::new(), + next_object_id: 0, } } - fn spawn(&mut self, object: Object) { + fn spawn(&mut self, object_kind: ObjectKind) { + let object = Object { + kind: object_kind, + id: self.next_object_id, + }; self.objects.push(object); + self.next_object_id += 1 + } + + fn despawn(&mut self, id: u32) { + let index = self + .objects + .iter() + .position(|o| o.id == id) + .expect("doesn't exist"); + self.objects.remove(index); } } @@ -41,41 +57,41 @@ impl engine::Game for Game { fn event(&mut self, event: engine::Event) {} } -enum Object { +struct Object { + kind: ObjectKind, + id: u32, +} + +enum ObjectKind { Player { pos: V3, vel: V3 }, - Obstacle { pos: V3, vel_z: f64 }, + Obstacle { pos: V3, vel: V3 }, Ground { pos: V3, vel_z: f64 }, } impl Object { fn update(&mut self, delta_time: Duration) { - match self { - Object::Player { pos, vel } => { + match &mut self.kind { + ObjectKind::Player { pos, vel } => { *pos += *vel * delta_time.as_secs_f64(); if pos.0 >= 2.1 { pos.0 = -2.0; } } - Object::Obstacle { pos, vel_z } => { - pos.2 += *vel_z * delta_time.as_secs_f64(); + ObjectKind::Obstacle { pos, vel } => { + *pos += *vel * delta_time.as_secs_f64(); } - Object::Ground { pos, vel_z } => { + ObjectKind::Ground { pos, vel_z } => { + println!("z pos {}", pos.2); pos.2 += *vel_z * delta_time.as_secs_f64(); } } } fn render(&mut self, scene: &mut Scene) { - match self { - Object::Player { pos, .. } => { + match &mut self.kind { + ObjectKind::Player { pos, .. } => { scene.draw_shape( - *pos + V3(0.0, 0.2, 0.0), - &Shape::new_cube(V3(0.2, 0.1, 0.2)), - Color::Green, - Color::Black, - ); - scene.draw_shape( - *pos + V3(-0.8, -0.2, 0.0), + *pos, &Shape::new_cube(V3(0.2, 0.2, 0.2)) .translate(V3(-0.1, -0.1, -0.1)) .rotate(V3(pos.0 * 5.0, pos.0 * 5.0, pos.0 * 5.0)), @@ -83,8 +99,15 @@ impl Object { Color::Black, ); } - Object::Obstacle { pos, vel_z } => todo!(), - Object::Ground { pos, .. } => { + ObjectKind::Obstacle { pos, .. } => { + scene.draw_shape( + *pos, + &Shape::new_cube(V3(0.2, 0.1, 0.2)), + Color::Green, + Color::Black, + ); + } + ObjectKind::Ground { pos, .. } => { for z in 0..10 { for x in -10..10 { scene.draw_shape( @@ -103,17 +126,23 @@ impl Object { fn main() -> Result<(), Box> { let mut sdl_io = engine::SdlIo::new()?; let mut game = Game::new(); - - let player = Object::Player { - pos: V3(-1.0, -0.1, 0.0), + let mut objects: Vec = Vec::new(); + objects.push(ObjectKind::Player { + pos: V3(-1.8, -0.3, 0.0), vel: V3(0.4, 0.0, 0.0), - }; - game.spawn(player); - let ground = Object::Ground { + }); + objects.push(ObjectKind::Ground { pos: V3(0.0, -0.3, -0.5), vel_z: -0.1, - }; - game.spawn(ground); + }); + objects.push(ObjectKind::Obstacle { + pos: V3(0.5, -0.2, 0.0), + vel: V3(0.0, 0.0, -0.1), + }); + + for object in objects { + game.spawn(object) + } sdl_io.run(&mut game); Ok(())