From 2120fc51edfe167d64332912b2b28b3dbff64684 Mon Sep 17 00:00:00 2001 From: Levi Pearson Date: Sat, 30 Jul 2016 16:19:31 -0600 Subject: [PATCH] Move thrust/rotation calcs to ship module --- src/game/controls.rs | 38 +++++++++++++++----------------------- src/game/mod.rs | 12 +++++++----- src/game/ship.rs | 10 +++++++++- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/game/controls.rs b/src/game/controls.rs index f1d2d78..32606d1 100644 --- a/src/game/controls.rs +++ b/src/game/controls.rs @@ -1,9 +1,6 @@ use piston_window::*; use piston_window::math::*; -static TURN_RATE: Scalar = 4.0; -static THRUST_RATE: Scalar = 4.0; - pub struct Controls { up_d: bool, down_d: bool, @@ -45,32 +42,27 @@ impl Controls { } } - pub fn velocity_change(&self, rot: Scalar, dt: Scalar) -> Vec2d { - let thrust = if self.up_d { - THRUST_RATE * dt + pub fn thrust_control(&self) -> Scalar { + if self.up_d { + 1.0 } else if self.down_d { - -THRUST_RATE * dt - } else { - 0.0 - }; - if thrust != 0.0 { - [rot.cos() * thrust, rot.sin() * thrust] - } else { - [0.0, 0.0] - } - } - - pub fn rotation_change(&self, dt: Scalar) -> Scalar { - if self.left_d { - -TURN_RATE * dt - } else if self.right_d { - TURN_RATE * dt + -1.0 } else { 0.0 } } - pub fn fire_check(&mut self) -> bool { + pub fn rotation_control(&self) -> Scalar { + if self.right_d { + 1.0 + } else if self.left_d { + -1.0 + } else { + 0.0 + } + } + + pub fn fire_control(&mut self) -> bool { let firing = self.fire; self.fire = false; firing diff --git a/src/game/mod.rs b/src/game/mod.rs index d64f3f3..a7cdb2c 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -87,18 +87,20 @@ impl Game { } pub fn update(&mut self, args: UpdateArgs) { - let dv = self.controls.velocity_change(self.player.rotation, args.dt); - let dr = self.controls.rotation_change(args.dt); - let firing = self.controls.fire_check(); + let thr = self.controls.thrust_control(); + let rot = self.controls.rotation_control(); + let firing = self.controls.fire_control(); + + self.player.update(thr, rot, args.dt, &self.map); - self.player.update(dv, dr, &self.map); for bullet in self.bullets.iter_mut() { bullet.update(args.dt); } self.bullets.retain(|&ref bullet| bullet.ttl > 0.0); if firing { let (nose, dir, vel) = self.player.trajectory(); - let bul = Bullet::new(nose, add(vel,mul_scalar(dir, 3.0)), 2.0); + let bul = Bullet::new(nose, add(vel, mul_scalar(dir, 3.0)), 2.0); self.bullets.push(bul); } + self.camera.follow(self.player.position, &self.map); } diff --git a/src/game/ship.rs b/src/game/ship.rs index 91d6e37..4e2e155 100644 --- a/src/game/ship.rs +++ b/src/game/ship.rs @@ -3,6 +3,9 @@ use std::f64::consts::*; use super::map::Map; +static TURN_RATE: Scalar = 4.0; +static THRUST_RATE: Scalar = 4.0; + const SHIP_POLY: &'static [[Scalar; 2]] = &[ [-10.0, -8.0], [10.0, 0.0], @@ -26,10 +29,15 @@ impl Ship { } } - pub fn update(&mut self, dv: Vec2d, dr: Scalar, map: &Map) { + 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; + self.rotation += dr; self.velocity = add(self.velocity, dv); self.position = add(self.position, self.velocity); + if self.position[0] > map.width { self.velocity[0] = 0.0; self.position[0] = map.width;