This commit is contained in:
Theis Pieter Hollebeek 2026-04-01 15:37:33 +02:00
parent 7fced21e23
commit 479e3cf990
4 changed files with 67 additions and 83 deletions

View File

@ -64,8 +64,9 @@ impl SdlIo {
keycode: Some(Keycode::Escape),
..
} => break 'running,
Event::KeyDown { keycode, .. } => match keycode {
Some(key) => match key {
Event::KeyDown {
keycode: Some(key), ..
} => match key {
Keycode::Left => {
game.event(game::Event::KeyDown {
key: game::Key::Left,
@ -78,10 +79,9 @@ impl SdlIo {
}
_ => {}
},
None => {}
},
Event::KeyUp { keycode, .. } => match keycode {
Some(key) => match key {
Event::KeyUp {
keycode: Some(key), ..
} => match key {
Keycode::Left => {
game.event(game::Event::KeyUp {
key: game::Key::Left,
@ -94,8 +94,6 @@ impl SdlIo {
}
_ => {}
},
None => {}
},
_ => {}
}

View File

@ -1,4 +1,4 @@
use std::{collections::VecDeque, sync::Mutex};
use std::collections::VecDeque;
pub enum Event {
Skateboard {

View File

@ -57,41 +57,26 @@ impl<R: Renderer> engine::Game<R> for Game {
fn update(&mut self, delta_time: Duration) {
for object in &mut self.objects {
if self.keys_pressed.contains(&Key::Left) == self.keys_pressed.contains(&Key::Right) {
match &mut object.kind {
ObjectKind::SkateBoard {
pivot_deg: pivot_factor,
..
} => {
if let ObjectKind::SkateBoard { pivot_deg, .. } = &mut object.kind {
let decay_rate = 1.0 - (2.0 * delta_time.as_secs_f64());
*pivot_factor *= decay_rate;
}
_ => {}
*pivot_deg *= decay_rate;
}
}
if self.keys_pressed.contains(&Key::Left) {
match &mut object.kind {
ObjectKind::SkateBoard { pivot_deg, .. } => {
if let ObjectKind::SkateBoard { pivot_deg, .. } = &mut object.kind {
*pivot_deg -= 18.0 * delta_time.as_secs_f64();
if *pivot_deg < -12.5 {
*pivot_deg = -12.5;
}
}
_ => {}
}
}
if self.keys_pressed.contains(&Key::Right) {
match &mut object.kind {
ObjectKind::SkateBoard {
pivot_deg: pivot_factor,
..
} => {
*pivot_factor += 18.0 * delta_time.as_secs_f64();
if *pivot_factor > 12.5 {
*pivot_factor = 12.5;
if let ObjectKind::SkateBoard { pivot_deg, .. } = &mut object.kind {
*pivot_deg += 18.0 * delta_time.as_secs_f64();
if *pivot_deg > 12.5 {
*pivot_deg = 12.5;
}
}
_ => {}
}
}
object.update(delta_time);
@ -214,8 +199,8 @@ impl Object {
} => {
*pos += *vel * delta_time.as_secs_f64();
*vel += V3(vel.0, vel.1, vel.0 - 0.01 * delta_time.as_secs_f64());
if pos.2 <= original_pos.2 - *grid_depth as f64 * *grid_item_size as f64 {
pos.2 += *grid_depth as f64 * *grid_item_size as f64
if pos.2 <= original_pos.2 - *grid_depth as f64 * *grid_item_size {
pos.2 += *grid_depth as f64 * *grid_item_size
}
}
}
@ -385,15 +370,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let server = Server::bind("10.133.51.127:5000");
server.start(event_queue);
});
let mut objects: Vec<ObjectKind> = Vec::new();
objects.push(ObjectKind::SkateBoard {
let objects: Vec<ObjectKind> = vec![
ObjectKind::SkateBoard {
pos: V3(0.0, -0.1, -0.7),
vel: V3(0.0, 0.0, 0.0),
rot: V3(0.0, PI * 0.5, 0.0),
nyoom_factor: 0.0,
pivot_deg: 0.0,
});
objects.push(ObjectKind::Ground {
},
ObjectKind::Ground {
original_pos: V3(0.0, -0.25, -0.6),
pos: V3(0.0, -0.25, -0.6),
vel: V3(0.0, 0.0, -0.1),
@ -401,11 +386,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
grid_item_size: 0.1,
grid_width: 10,
grid_depth: 10,
});
objects.push(ObjectKind::Obstacle {
},
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)

View File

@ -28,7 +28,7 @@ impl Server {
fn get_fallible(data: &[f64], idx: usize) -> Result<f64, String> {
data.get(idx)
.map(|idx| *idx)
.cloned()
.ok_or_else(|| format!("protocol error: {idx}"))
}
@ -37,12 +37,12 @@ impl Server {
let mut data = String::new();
reader
.read_line(&mut data)
.map_err(|err| format!("io error: {}", err.to_string()))?;
.map_err(|err| format!("io error: {err}"))?;
let data = data
.split(",")
.map(|x| x.parse::<f64>())
.collect::<Result<Vec<f64>, _>>();
let data = data.map_err(|err| format!("protocol error: {}", err.to_string()))?;
let data = data.map_err(|err| format!("protocol error: {err}"))?;
Ok(Event::Skateboard {
x: Self::get_fallible(&data, 0)?,