mirror of
https://github.com/Mercantec-GHC/h5-projekt-mst.git
synced 2026-08-27 05:37:39 +02:00
add despawn and don't render objects behind camera
This commit is contained in:
parent
b8b9f284f4
commit
4f1ab9556a
@ -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);
|
||||
|
||||
@ -9,17 +9,33 @@ mod vermiparous;
|
||||
|
||||
struct Game {
|
||||
objects: Vec<Object>,
|
||||
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<R: Renderer> engine::Game<R> 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<dyn std::error::Error>> {
|
||||
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<ObjectKind> = 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(())
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user