Misc cleanup

master
Levi Pearson 2016-07-30 19:55:30 -06:00
parent 2120fc51ed
commit eb1a4830ca
3 changed files with 21 additions and 25 deletions

View File

@ -18,6 +18,12 @@ impl Map {
[self.width / 2.0, self.height / 2.0] [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<G>(&self, pub fn draw_grid<G>(&self,
cam_pos: Vec2d, cam_pos: Vec2d,
cam_size: Vec2d, cam_size: Vec2d,

View File

@ -67,16 +67,17 @@ impl Game {
let ds = context.draw_state.blend(Blend::Alpha); 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, let ct = context.transform.trans((args.width / 2) as Scalar,
(args.height / 2) as Scalar); (args.height / 2) as Scalar);
let cam_pos = self.camera.position; let cam_pos = self.camera.position;
let cam_width = self.camera.width; let cam_size= [self.camera.width, self.camera.height];
let cam_height = self.camera.height;
/* Transform world coordinates to camera-relative coordinates */
let cam_trans = ct.append_transform(translate(mul_scalar(cam_pos, -1.0))); 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() { for bullet in self.bullets.iter() {
draw_bullet(&bullet, &ds, cam_trans, gl); draw_bullet(&bullet, &ds, cam_trans, gl);

View File

@ -29,31 +29,20 @@ impl Ship {
} }
} }
pub fn update(&mut self, thr: Scalar, rot: Scalar, dt: Scalar, map: &Map) { pub fn update(&mut self, thr: Scalar, rot: Scalar, d_t: Scalar, map: &Map) {
let thrust = thr * THRUST_RATE * dt; let thrust = thr * THRUST_RATE * d_t;
let dv = [self.rotation.cos() * thrust, self.rotation.sin() * thrust]; let d_vel = [self.rotation.cos() * thrust, self.rotation.sin() * thrust];
let dr = rot * TURN_RATE * dt; let d_rot = rot * TURN_RATE * d_t;
self.rotation += dr; self.rotation += d_rot;
self.velocity = add(self.velocity, dv); self.velocity = add(self.velocity, d_vel);
self.position = add(self.position, self.velocity); self.position = add(self.position, self.velocity);
if self.position[0] > map.width { let oldpos = self.position;
self.velocity[0] = 0.0; self.position = map.clamp(oldpos);
self.position[0] = map.width;
} if self.position[0] != oldpos[0] { self.velocity[0] = 0.0; }
if self.position[0] < 0.0 { if self.position[1] != oldpos[1] { self.velocity[1] = 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;
}
} }
pub fn trajectory(&self) -> (Vec2d, Vec2d, Vec2d) { pub fn trajectory(&self) -> (Vec2d, Vec2d, Vec2d) {