From eb1a4830ca902bf5cf27d30f92f6da2027e40f62 Mon Sep 17 00:00:00 2001 From: Levi Pearson Date: Sat, 30 Jul 2016 19:55:30 -0600 Subject: [PATCH] Misc cleanup --- src/game/map.rs | 6 ++++++ src/game/mod.rs | 7 ++++--- src/game/ship.rs | 33 +++++++++++---------------------- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/game/map.rs b/src/game/map.rs index 8ac967f..b844eed 100644 --- a/src/game/map.rs +++ b/src/game/map.rs @@ -18,6 +18,12 @@ impl Map { [self.width / 2.0, self.height / 2.0] } + pub fn clamp(&self, other: Vec2d) -> Vec2d { + let x = other[0].max(0.0).min(self.width); + let y = other[1].max(0.0).min(self.height); + [x, y] + } + pub fn draw_grid(&self, cam_pos: Vec2d, cam_size: Vec2d, diff --git a/src/game/mod.rs b/src/game/mod.rs index a7cdb2c..26b345e 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -67,16 +67,17 @@ impl Game { let ds = context.draw_state.blend(Blend::Alpha); + /* Make the camera coordinate origin the center of the window */ let ct = context.transform.trans((args.width / 2) as Scalar, (args.height / 2) as Scalar); let cam_pos = self.camera.position; - let cam_width = self.camera.width; - let cam_height = self.camera.height; + let cam_size= [self.camera.width, self.camera.height]; + /* Transform world coordinates to camera-relative coordinates */ let cam_trans = ct.append_transform(translate(mul_scalar(cam_pos, -1.0))); - self.map.draw_grid(cam_pos, [cam_width, cam_height], &ds, cam_trans, gl); + self.map.draw_grid(cam_pos, cam_size, &ds, cam_trans, gl); for bullet in self.bullets.iter() { draw_bullet(&bullet, &ds, cam_trans, gl); diff --git a/src/game/ship.rs b/src/game/ship.rs index 4e2e155..6b01335 100644 --- a/src/game/ship.rs +++ b/src/game/ship.rs @@ -29,31 +29,20 @@ impl Ship { } } - pub fn update(&mut self, thr: Scalar, rot: Scalar, dt: Scalar, map: &Map) { - let thrust = thr * THRUST_RATE * dt; - let dv = [self.rotation.cos() * thrust, self.rotation.sin() * thrust]; - let dr = rot * TURN_RATE * dt; + pub fn update(&mut self, thr: Scalar, rot: Scalar, d_t: Scalar, map: &Map) { + let thrust = thr * THRUST_RATE * d_t; + let d_vel = [self.rotation.cos() * thrust, self.rotation.sin() * thrust]; + let d_rot = rot * TURN_RATE * d_t; - self.rotation += dr; - self.velocity = add(self.velocity, dv); + self.rotation += d_rot; + self.velocity = add(self.velocity, d_vel); self.position = add(self.position, self.velocity); - if self.position[0] > map.width { - self.velocity[0] = 0.0; - self.position[0] = map.width; - } - if self.position[0] < 0.0 { - self.velocity[0] = 0.0; - self.position[0] = 0.0; - } - if self.position[1] > map.height { - self.velocity[1] = 0.0; - self.position[1] = map.height; - } - if self.position[1] < 0.0 { - self.velocity[1] = 0.0; - self.position[1] = 0.0; - } + let oldpos = self.position; + self.position = map.clamp(oldpos); + + if self.position[0] != oldpos[0] { self.velocity[0] = 0.0; } + if self.position[1] != oldpos[1] { self.velocity[1] = 0.0; } } pub fn trajectory(&self) -> (Vec2d, Vec2d, Vec2d) {