rust-pilot/src/game/map.rs

62 lines
1.8 KiB
Rust

use piston_window::*;
use piston_window::math::*;
const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 0.05];
pub struct Map {
pub width: Scalar,
pub height: Scalar
}
impl Map {
pub fn new(width: Scalar, height: Scalar) -> Self {
Map { width: width, height: height }
}
pub fn center(&self) -> Vec2d {
[self.width / 2.0, self.height / 2.0]
}
pub fn draw_grid<G>(&self,
cam_pos: Vec2d,
cam_width: Scalar,
cam_height: Scalar,
draw_state: &DrawState,
transform: Matrix2d,
g: &mut G)
where G: Graphics
{
let xcount = (cam_width / 100.0).ceil() as u32 + 2;
let ycount = (cam_height / 100.0).ceil() as u32 + 2;
let min_x = cam_pos[0] - cam_width / 2.0;
let min_y = cam_pos[1] - cam_height / 2.0;
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;
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);
Line::new(GREEN, 1.0)
.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;
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);
Line::new(GREEN, 1.0)
.draw([p1[0], p1[1], p2[0], p2[1]],
draw_state, transform, g);
}
}
}