Move thrust/rotation calcs to ship module
parent
a9a397ba97
commit
2120fc51ed
|
@ -1,9 +1,6 @@
|
||||||
use piston_window::*;
|
use piston_window::*;
|
||||||
use piston_window::math::*;
|
use piston_window::math::*;
|
||||||
|
|
||||||
static TURN_RATE: Scalar = 4.0;
|
|
||||||
static THRUST_RATE: Scalar = 4.0;
|
|
||||||
|
|
||||||
pub struct Controls {
|
pub struct Controls {
|
||||||
up_d: bool,
|
up_d: bool,
|
||||||
down_d: bool,
|
down_d: bool,
|
||||||
|
@ -45,32 +42,27 @@ impl Controls {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn velocity_change(&self, rot: Scalar, dt: Scalar) -> Vec2d {
|
pub fn thrust_control(&self) -> Scalar {
|
||||||
let thrust = if self.up_d {
|
if self.up_d {
|
||||||
THRUST_RATE * dt
|
1.0
|
||||||
} else if self.down_d {
|
} else if self.down_d {
|
||||||
-THRUST_RATE * dt
|
-1.0
|
||||||
} 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
|
|
||||||
} else {
|
} else {
|
||||||
0.0
|
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;
|
let firing = self.fire;
|
||||||
self.fire = false;
|
self.fire = false;
|
||||||
firing
|
firing
|
||||||
|
|
|
@ -87,18 +87,20 @@ impl Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, args: UpdateArgs) {
|
pub fn update(&mut self, args: UpdateArgs) {
|
||||||
let dv = self.controls.velocity_change(self.player.rotation, args.dt);
|
let thr = self.controls.thrust_control();
|
||||||
let dr = self.controls.rotation_change(args.dt);
|
let rot = self.controls.rotation_control();
|
||||||
let firing = self.controls.fire_check();
|
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); }
|
for bullet in self.bullets.iter_mut() { bullet.update(args.dt); }
|
||||||
self.bullets.retain(|&ref bullet| bullet.ttl > 0.0);
|
self.bullets.retain(|&ref bullet| bullet.ttl > 0.0);
|
||||||
if firing {
|
if firing {
|
||||||
let (nose, dir, vel) = self.player.trajectory();
|
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.bullets.push(bul);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.camera.follow(self.player.position, &self.map);
|
self.camera.follow(self.player.position, &self.map);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,9 @@ use std::f64::consts::*;
|
||||||
|
|
||||||
use super::map::Map;
|
use super::map::Map;
|
||||||
|
|
||||||
|
static TURN_RATE: Scalar = 4.0;
|
||||||
|
static THRUST_RATE: Scalar = 4.0;
|
||||||
|
|
||||||
const SHIP_POLY: &'static [[Scalar; 2]] = &[
|
const SHIP_POLY: &'static [[Scalar; 2]] = &[
|
||||||
[-10.0, -8.0],
|
[-10.0, -8.0],
|
||||||
[10.0, 0.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.rotation += dr;
|
||||||
self.velocity = add(self.velocity, dv);
|
self.velocity = add(self.velocity, dv);
|
||||||
self.position = add(self.position, self.velocity);
|
self.position = add(self.position, self.velocity);
|
||||||
|
|
||||||
if self.position[0] > map.width {
|
if self.position[0] > map.width {
|
||||||
self.velocity[0] = 0.0;
|
self.velocity[0] = 0.0;
|
||||||
self.position[0] = map.width;
|
self.position[0] = map.width;
|
||||||
|
|
Loading…
Reference in New Issue