Small refactor of Layer.
This commit is contained in:
61
src/main.rs
61
src/main.rs
@@ -1,4 +1,3 @@
|
||||
#[deny(trivial_casts, trivial_numeric_casts)]
|
||||
extern crate sdl2;
|
||||
extern crate sdl2_image;
|
||||
|
||||
@@ -17,6 +16,7 @@ use timer::Timer;
|
||||
use game_object::GameObject;
|
||||
use player_components::{PlayerPhysicsComponent, PlayerGraphicsComponent};
|
||||
|
||||
|
||||
mod timer;
|
||||
mod tile;
|
||||
mod camera;
|
||||
@@ -26,6 +26,7 @@ mod game_object;
|
||||
mod component;
|
||||
mod player_components;
|
||||
|
||||
|
||||
const SCREEN_WIDTH : u32 = 960;
|
||||
const SCREEN_HEIGHT : u32 = 640;
|
||||
|
||||
@@ -41,6 +42,7 @@ const PLAYER_ACCELERATION_X_START : f32 = 0.02;
|
||||
const PLAYER_ACCELERATION_X_STOP : f32 = 0.15;
|
||||
const PLAYER_ACCELERATION_X_CHANGE : f32 = 0.06;
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
enum Tile<'a> {
|
||||
Empty,
|
||||
@@ -49,6 +51,7 @@ enum Tile<'a> {
|
||||
Floor(Rect)
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let sdl_context = sdl2::init().unwrap();
|
||||
let video_subsystem = sdl_context.video().unwrap();
|
||||
@@ -249,15 +252,11 @@ fn main() {
|
||||
break;
|
||||
}
|
||||
|
||||
if let Some(tile) = layer.get_tile(x, y) {
|
||||
d = match *tile {
|
||||
Tile::Floor(_) => d.min(t),
|
||||
Tile::Static(_, solid) => if solid { d.min(t) } else { d },
|
||||
_ => d
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
d = match *layer.get_tile(x, y) {
|
||||
Tile::Floor(_) => d.min(t),
|
||||
Tile::Static(_, solid) => if solid { d.min(t) } else { d },
|
||||
_ => d
|
||||
};
|
||||
|
||||
x += 1;
|
||||
}
|
||||
@@ -285,15 +284,11 @@ fn main() {
|
||||
break;
|
||||
}
|
||||
|
||||
if let Some(tile) = layer.get_tile(x, y) {
|
||||
d = match *tile {
|
||||
Tile::Floor(_) => d.max(t),
|
||||
Tile::Static(_, solid) => if solid { d.max(t) } else { d },
|
||||
_ => d
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
d = match *layer.get_tile(x, y) {
|
||||
Tile::Floor(_) => d.max(t),
|
||||
Tile::Static(_, solid) => if solid { d.max(t) } else { d },
|
||||
_ => d
|
||||
};
|
||||
|
||||
x -= 1;
|
||||
}
|
||||
@@ -323,15 +318,11 @@ fn main() {
|
||||
break;
|
||||
}
|
||||
|
||||
if let Some(tile) = layer.get_tile(x, y) {
|
||||
d = match *tile {
|
||||
Tile::Floor(_) => d.min(t),
|
||||
Tile::Static(_, solid) => if solid { d.min(t) } else { d },
|
||||
_ => d
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
d = match *layer.get_tile(x, y) {
|
||||
Tile::Floor(_) => d.min(t),
|
||||
Tile::Static(_, solid) => if solid { d.min(t) } else { d },
|
||||
_ => d
|
||||
};
|
||||
|
||||
y += 1;
|
||||
}
|
||||
@@ -363,15 +354,11 @@ fn main() {
|
||||
break;
|
||||
}
|
||||
|
||||
if let Some(tile) = layer.get_tile(x, y) {
|
||||
d = match *tile {
|
||||
Tile::Floor(_) => d.max(t),
|
||||
Tile::Static(_, solid) => if solid { d.max(t) } else { d },
|
||||
_ => d
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
d = match *layer.get_tile(x, y) {
|
||||
Tile::Floor(_) => d.max(t),
|
||||
Tile::Static(_, solid) => if solid { d.max(t) } else { d },
|
||||
_ => d
|
||||
};
|
||||
|
||||
y -= 1;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use sprite::{Sprite, StaticSprite, AnimatedSprite};
|
||||
use game_object::GameObject;
|
||||
use component::{Updatable, Renderable};
|
||||
|
||||
|
||||
pub struct PlayerPhysicsComponent {
|
||||
pub dx: f32,
|
||||
pub dy: f32,
|
||||
@@ -28,6 +29,7 @@ impl Updatable for PlayerPhysicsComponent {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct PlayerGraphicsComponent<'a> {
|
||||
flip_horizontal: Cell<bool>,
|
||||
sprite_standing: RefCell<StaticSprite<'a>>,
|
||||
|
||||
26
src/tile.rs
26
src/tile.rs
@@ -1,10 +1,9 @@
|
||||
use std::iter::repeat;
|
||||
|
||||
use sdl2::rect::Rect;
|
||||
|
||||
|
||||
pub struct Layer<T> {
|
||||
tiles: Vec<T>,
|
||||
tiles: Vec<Option<T>>,
|
||||
default: T,
|
||||
width: u32,
|
||||
height: u32,
|
||||
tile_width: u32,
|
||||
@@ -12,9 +11,11 @@ pub struct Layer<T> {
|
||||
}
|
||||
|
||||
impl<T> Layer<T> where T: Clone {
|
||||
// TODO Change u32 to usize for width and height?
|
||||
pub fn new(width: u32, height: u32, tile_width: u32, tile_height: u32, tile: T) -> Layer<T> {
|
||||
Layer {
|
||||
tiles: repeat(tile).take((width * height) as usize).collect(),
|
||||
tiles: vec![None; (width * height) as usize],
|
||||
default: tile,
|
||||
width: width,
|
||||
height: height,
|
||||
tile_width: tile_width,
|
||||
@@ -22,20 +23,19 @@ impl<T> Layer<T> where T: Clone {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_tile(&self, x: i32, y: i32) -> Option<&T> {
|
||||
pub fn get_tile(&self, x: i32, y: i32) -> &T {
|
||||
let offset = (x + y * self.width as i32) as usize;
|
||||
|
||||
if offset < self.tiles.len() {
|
||||
Some(&self.tiles[offset])
|
||||
} else {
|
||||
None
|
||||
match *self.tiles.get(offset).unwrap() {
|
||||
Some(ref tile) => tile,
|
||||
None => &self.default
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_tile(&mut self, x: i32, y: i32, tile: T) {
|
||||
let offset = (x + y * self.width as i32) as usize;
|
||||
|
||||
self.tiles[offset] = tile;
|
||||
self.tiles[offset] = Some(tile);
|
||||
}
|
||||
|
||||
pub fn find_intersecting(&self, rect: &Rect) -> Option<Rect> {
|
||||
@@ -61,7 +61,7 @@ impl<T> Layer<T> where T: Clone {
|
||||
for x in intersect.x()..(intersect.x() + intersect.width() as i32) {
|
||||
let position = Rect::new(x * self.tile_width as i32, y * self.tile_height as i32, self.tile_width, self.tile_height);
|
||||
|
||||
f(self.get_tile(x, y).unwrap(), &position);
|
||||
f(self.get_tile(x, y), &position);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,10 +74,10 @@ impl<T> Layer<T> where T: Clone {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Layer;
|
||||
|
||||
use sdl2::rect::Rect;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn layer_find_intersecting() {
|
||||
let layer = Layer::new(3, 3, 3, 3, ());
|
||||
|
||||
Reference in New Issue
Block a user