get_tile now returns an Option<&Tile>

This commit is contained in:
2015-01-12 14:26:26 +01:00
parent b481a4f3bb
commit b02f487e18
2 changed files with 42 additions and 26 deletions

View File

@@ -87,7 +87,9 @@ fn main() {
match poll_event() {
Event::Quit(_) => break 'main,
Event::KeyDown(_, _, key, _, _, repeat) => {
if repeat == false {
keyboard.key_down(key);
}
},
Event::KeyUp(_, _, key, _, _, _) => {
keyboard.key_up(key);
@@ -155,11 +157,14 @@ fn main() {
break;
}
// @todo get_tile should return Option<Tile>
d = match *layer.get_tile(x, y) {
if let Some(tile) = layer.get_tile(x, y) {
d = match *tile {
Tile::Empty => d,
Tile::Floor(_) => d.min(t)
};
} else {
break;
}
x += 1;
}
@@ -184,11 +189,14 @@ fn main() {
break;
}
// @todo get_tile should return Option<Tile>
d = match *layer.get_tile(x, y) {
if let Some(tile) = layer.get_tile(x, y) {
d = match *tile {
Tile::Empty => d,
Tile::Floor(_) => d.max(t)
};
} else {
break;
}
x -= 1;
}
@@ -215,11 +223,14 @@ fn main() {
break;
}
// @todo get_tile should return Option<Tile>
d = match *layer.get_tile(x, y) {
if let Some(tile) = layer.get_tile(x, y) {
d = match *tile {
Tile::Empty => d,
Tile::Floor(_) => d.min(t)
};
} else {
break;
}
y += 1;
}
@@ -242,17 +253,18 @@ fn main() {
loop {
let t = (y * TILE_HEIGHT + TILE_HEIGHT) as f32 - p;
println!("x={}, y={}, d={}, t={}", x, y, d, t);
if t < d {
break;
}
// @todo get_tile should return Option<Tile>
d = match *layer.get_tile(x, y) {
if let Some(tile) = layer.get_tile(x, y) {
d = match *tile {
Tile::Empty => d,
Tile::Floor(_) => d.max(t)
};
} else {
break;
}
y -= 1;
}

View File

@@ -23,10 +23,14 @@ impl<T> Layer<T> where T: Clone {
}
}
pub fn get_tile(&self, x: i32, y: i32) -> &T {
pub fn get_tile(&self, x: i32, y: i32) -> Option<&T> {
let offset = (x + y * self.width) as usize;
&self.tiles[offset]
if offset >= 0 && offset < self.tiles.len() {
Some(&self.tiles[offset])
} else {
None
}
}
pub fn set_tile(&mut self, x: i32, y: i32, tile: T) {
@@ -58,7 +62,7 @@ impl<T> Layer<T> where T: Clone {
for x in range(intersect.x, intersect.x + intersect.w + 1) {
let position = Rect::new(x * self.tile_width, y * self.tile_height, self.tile_width, self.tile_height);
f(self.get_tile(x, y), &position);
f(self.get_tile(x, y).unwrap(), &position);
}
}
}