From a9a397ba97e7b9ce67256ed3d12958498a5a83d4 Mon Sep 17 00:00:00 2001 From: Levi Pearson Date: Sat, 30 Jul 2016 16:18:34 -0600 Subject: [PATCH] Refactor grid drawing for greater clarity --- src/game/map.rs | 45 ++++++++++++++++++++++----------------------- src/game/mod.rs | 4 ++-- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/game/map.rs b/src/game/map.rs index 2a1846d..8ac967f 100644 --- a/src/game/map.rs +++ b/src/game/map.rs @@ -2,6 +2,7 @@ use piston_window::*; use piston_window::math::*; const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 0.05]; +const GRID_SIZE: Scalar = 100.0; pub struct Map { pub width: Scalar, @@ -19,43 +20,41 @@ impl Map { pub fn draw_grid(&self, cam_pos: Vec2d, - cam_width: Scalar, - cam_height: Scalar, + cam_size: Vec2d, draw_state: &DrawState, transform: Matrix2d, g: &mut G) where G: Graphics { + /* Camera coordinates will be centered in the screen, so find the + upper left and lower right coordinates */ + let min = sub(cam_pos, mul_scalar(cam_size, 0.5)); + let max = add(cam_pos, mul_scalar(cam_size, 0.5)); - let xcount = (cam_width / 100.0).ceil() as u32 + 2; - let ycount = (cam_height / 100.0).ceil() as u32 + 2; + /* Determine the number of grid lines to draw in each direction */ + let xcount = (cam_size[0] / GRID_SIZE).ceil() as u32 + 1; + let ycount = (cam_size[1] / GRID_SIZE).ceil() as u32 + 1; - let min_x = cam_pos[0] - cam_width / 2.0; - let min_y = cam_pos[1] - cam_height / 2.0; + /* Clamp the line coordinates to multiples of the grid size */ + let first_x = (min[0] / GRID_SIZE).trunc() * GRID_SIZE; + let first_y = (min[1] / GRID_SIZE).trunc() * GRID_SIZE; - let first_x = (min_x / 100.0).trunc() * 100.0; - let first_y = (min_y / 100.0).trunc() * 100.0; - - let start_y = cam_pos[1] - cam_height; - let end_y = cam_pos[1] + cam_height; + /* Draw vertical grid lines */ for x in 0..xcount { - let line_x = first_x + (100.0 * x as Scalar); - let p1 = sub([line_x, start_y], cam_pos); - let p2 = sub([line_x, end_y], cam_pos); + let line_x = first_x + (GRID_SIZE * x as Scalar); + let p1 = [line_x, min[1]]; + let p2 = [line_x, max[1]]; Line::new(GREEN, 1.0) - .draw([p1[0], p1[1], p2[0], p2[1]], - draw_state, transform, g); + .draw([p1[0], p1[1], p2[0], p2[1]], draw_state, transform, g); } - let start_x = cam_pos[0] - cam_width; - let end_x = cam_pos[0] + cam_width; + /* Draw horizontal grid lines */ for y in 0..ycount { - let line_y = first_y + (100.0 * y as Scalar); - let p1 = sub([start_x, line_y], cam_pos); - let p2 = sub([end_x, line_y], cam_pos); + let line_y = first_y + (GRID_SIZE * y as Scalar); + let p1 = [min[0], line_y]; + let p2 = [max[0], line_y]; Line::new(GREEN, 1.0) - .draw([p1[0], p1[1], p2[0], p2[1]], - draw_state, transform, g); + .draw([p1[0], p1[1], p2[0], p2[1]], draw_state, transform, g); } } } diff --git a/src/game/mod.rs b/src/game/mod.rs index 7eb8ca2..d64f3f3 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -74,10 +74,10 @@ impl Game { let cam_width = self.camera.width; let cam_height = self.camera.height; - self.map.draw_grid(cam_pos, cam_width, cam_height, &ds, ct, gl); - 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); }