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
|
let drawn_triangles = self
|
||||||
.objects
|
.objects
|
||||||
.iter()
|
.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));
|
.map(|v| (v.triangle.project_2d(), v.outline_color, v.fill_color));
|
||||||
for (triangle, outline_color, fill_color) in drawn_triangles {
|
for (triangle, outline_color, fill_color) in drawn_triangles {
|
||||||
r.draw_triangle(triangle.clone(), fill_color);
|
r.draw_triangle(triangle.clone(), fill_color);
|
||||||
|
|||||||
@ -9,17 +9,33 @@ mod vermiparous;
|
|||||||
|
|
||||||
struct Game {
|
struct Game {
|
||||||
objects: Vec<Object>,
|
objects: Vec<Object>,
|
||||||
|
next_object_id: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Game {
|
impl Game {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
objects: Vec::new(),
|
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.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) {}
|
fn event(&mut self, event: engine::Event) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Object {
|
struct Object {
|
||||||
|
kind: ObjectKind,
|
||||||
|
id: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ObjectKind {
|
||||||
Player { pos: V3, vel: V3 },
|
Player { pos: V3, vel: V3 },
|
||||||
Obstacle { pos: V3, vel_z: f64 },
|
Obstacle { pos: V3, vel: V3 },
|
||||||
Ground { pos: V3, vel_z: f64 },
|
Ground { pos: V3, vel_z: f64 },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Object {
|
impl Object {
|
||||||
fn update(&mut self, delta_time: Duration) {
|
fn update(&mut self, delta_time: Duration) {
|
||||||
match self {
|
match &mut self.kind {
|
||||||
Object::Player { pos, vel } => {
|
ObjectKind::Player { pos, vel } => {
|
||||||
*pos += *vel * delta_time.as_secs_f64();
|
*pos += *vel * delta_time.as_secs_f64();
|
||||||
if pos.0 >= 2.1 {
|
if pos.0 >= 2.1 {
|
||||||
pos.0 = -2.0;
|
pos.0 = -2.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Object::Obstacle { pos, vel_z } => {
|
ObjectKind::Obstacle { pos, vel } => {
|
||||||
pos.2 += *vel_z * delta_time.as_secs_f64();
|
*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();
|
pos.2 += *vel_z * delta_time.as_secs_f64();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render(&mut self, scene: &mut Scene) {
|
fn render(&mut self, scene: &mut Scene) {
|
||||||
match self {
|
match &mut self.kind {
|
||||||
Object::Player { pos, .. } => {
|
ObjectKind::Player { pos, .. } => {
|
||||||
scene.draw_shape(
|
scene.draw_shape(
|
||||||
*pos + V3(0.0, 0.2, 0.0),
|
*pos,
|
||||||
&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),
|
|
||||||
&Shape::new_cube(V3(0.2, 0.2, 0.2))
|
&Shape::new_cube(V3(0.2, 0.2, 0.2))
|
||||||
.translate(V3(-0.1, -0.1, -0.1))
|
.translate(V3(-0.1, -0.1, -0.1))
|
||||||
.rotate(V3(pos.0 * 5.0, pos.0 * 5.0, pos.0 * 5.0)),
|
.rotate(V3(pos.0 * 5.0, pos.0 * 5.0, pos.0 * 5.0)),
|
||||||
@ -83,8 +99,15 @@ impl Object {
|
|||||||
Color::Black,
|
Color::Black,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Object::Obstacle { pos, vel_z } => todo!(),
|
ObjectKind::Obstacle { pos, .. } => {
|
||||||
Object::Ground { 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 z in 0..10 {
|
||||||
for x in -10..10 {
|
for x in -10..10 {
|
||||||
scene.draw_shape(
|
scene.draw_shape(
|
||||||
@ -103,17 +126,23 @@ impl Object {
|
|||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut sdl_io = engine::SdlIo::new()?;
|
let mut sdl_io = engine::SdlIo::new()?;
|
||||||
let mut game = Game::new();
|
let mut game = Game::new();
|
||||||
|
let mut objects: Vec<ObjectKind> = Vec::new();
|
||||||
let player = Object::Player {
|
objects.push(ObjectKind::Player {
|
||||||
pos: V3(-1.0, -0.1, 0.0),
|
pos: V3(-1.8, -0.3, 0.0),
|
||||||
vel: V3(0.4, 0.0, 0.0),
|
vel: V3(0.4, 0.0, 0.0),
|
||||||
};
|
});
|
||||||
game.spawn(player);
|
objects.push(ObjectKind::Ground {
|
||||||
let ground = Object::Ground {
|
|
||||||
pos: V3(0.0, -0.3, -0.5),
|
pos: V3(0.0, -0.3, -0.5),
|
||||||
vel_z: -0.1,
|
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);
|
sdl_io.run(&mut game);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user