mod controls; mod bullet; mod ship; mod camera; mod map; use piston_window::*; use piston_window::math::*; use piston_window::draw_state::Blend; use self::ship::Ship; use self::bullet::Bullet; use self::camera::Camera; use self::map::Map; use self::controls::Controls; const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0]; const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0]; const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 1.0]; pub struct Game { player: Ship, bullets: Vec, map: Map, camera: Camera, pub controls: Controls } fn draw_background(g: &mut G) where G: Graphics { clear(BLACK, g); } fn draw_player(player: &Ship, ds: &DrawState, trans: Matrix2d, g: &mut G) where G: Graphics { let trans = trans .append_transform(translate(player.position)) .rot_rad(player.rotation); Polygon::new(RED).draw(player.geometry, ds, trans, g); } fn draw_bullet(bullet: &Bullet, ds: &DrawState, trans: Matrix2d, g: &mut G) where G: Graphics { let trans = trans .append_transform(translate(bullet.position)); Polygon::new(WHITE).draw(bullet.geometry, &ds, trans, g); } impl Game { pub fn new(width: Scalar, height: Scalar) -> Self { let new_map = Map::new(10000.0, 10000.0); let center = new_map.center(); Game { player: Ship::new(center), bullets: Vec::new(), map: new_map, camera: Camera::new(center, width, height), controls: Controls::new() } } pub fn render(&mut self, args: RenderArgs, w: PistonWindow) { w.draw_2d(|context, gl| { draw_background(gl); let ds = context.draw_state.blend(Blend::Alpha); 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_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); for bullet in self.bullets.iter() { draw_bullet(&bullet, &ds, cam_trans, gl); } draw_player(&self.player, &ds, cam_trans, gl); }); } pub fn update(&mut self, args: UpdateArgs) { 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); 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); self.bullets.push(bul); } self.camera.follow(self.player.position, &self.map); } pub fn input(&mut self, inp: Input) { self.controls.update(inp); } }