Skip to content

Instantly share code, notes, and snippets.

@lilyball
Created April 9, 2014 17:12
Show Gist options
  • Save lilyball/10293280 to your computer and use it in GitHub Desktop.
Save lilyball/10293280 to your computer and use it in GitHub Desktop.
use std::iter;
static MAP_SIZE: uint = 10;
static TILE_SIZE: f32 = 10.;
pub struct Map {
tiles: [[u8,..MAP_SIZE],..MAP_SIZE],
view: Vec<(f32,f32,u8)>,
view_y: uint,
view_x: uint,
view_height: uint,
view_width: uint
}
impl Map {
fn new() -> Map {
Map {
tiles: [[0,..MAP_SIZE],..MAP_SIZE],
view: Vec::new(),
view_y: 0,
view_x: 0,
view_height: 0,
view_width: 0
}
}
fn set_view(&mut self, y: uint, x: uint, height: uint, width: uint) {
self.view_y = y;
self.view_x = x;
self.view_height = height;
self.view_width = width;
}
fn update_view(&mut self) {
self.view = self.tiles
.iter()
.skip(self.view_y)
.take(self.view_height)
.enumerate()
.flat_map(|(y, row)| row
.iter()
.skip(self.view_x)
.take(self.view_width)
.zip(iter::Repeat::new(y))
.enumerate()
.map(|(x, (tile, y))| (x as f32 * TILE_SIZE, y as f32 * TILE_SIZE, *tile)))
.collect()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment