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;
|
||||||
extern crate sdl2_image;
|
extern crate sdl2_image;
|
||||||
|
|
||||||
@@ -17,6 +16,7 @@ use timer::Timer;
|
|||||||
use game_object::GameObject;
|
use game_object::GameObject;
|
||||||
use player_components::{PlayerPhysicsComponent, PlayerGraphicsComponent};
|
use player_components::{PlayerPhysicsComponent, PlayerGraphicsComponent};
|
||||||
|
|
||||||
|
|
||||||
mod timer;
|
mod timer;
|
||||||
mod tile;
|
mod tile;
|
||||||
mod camera;
|
mod camera;
|
||||||
@@ -26,6 +26,7 @@ mod game_object;
|
|||||||
mod component;
|
mod component;
|
||||||
mod player_components;
|
mod player_components;
|
||||||
|
|
||||||
|
|
||||||
const SCREEN_WIDTH : u32 = 960;
|
const SCREEN_WIDTH : u32 = 960;
|
||||||
const SCREEN_HEIGHT : u32 = 640;
|
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_STOP : f32 = 0.15;
|
||||||
const PLAYER_ACCELERATION_X_CHANGE : f32 = 0.06;
|
const PLAYER_ACCELERATION_X_CHANGE : f32 = 0.06;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
enum Tile<'a> {
|
enum Tile<'a> {
|
||||||
Empty,
|
Empty,
|
||||||
@@ -49,6 +51,7 @@ enum Tile<'a> {
|
|||||||
Floor(Rect)
|
Floor(Rect)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let sdl_context = sdl2::init().unwrap();
|
let sdl_context = sdl2::init().unwrap();
|
||||||
let video_subsystem = sdl_context.video().unwrap();
|
let video_subsystem = sdl_context.video().unwrap();
|
||||||
@@ -249,15 +252,11 @@ fn main() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(tile) = layer.get_tile(x, y) {
|
d = match *layer.get_tile(x, y) {
|
||||||
d = match *tile {
|
Tile::Floor(_) => d.min(t),
|
||||||
Tile::Floor(_) => d.min(t),
|
Tile::Static(_, solid) => if solid { d.min(t) } else { d },
|
||||||
Tile::Static(_, solid) => if solid { d.min(t) } else { d },
|
_ => d
|
||||||
_ => d
|
};
|
||||||
}
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
x += 1;
|
x += 1;
|
||||||
}
|
}
|
||||||
@@ -285,15 +284,11 @@ fn main() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(tile) = layer.get_tile(x, y) {
|
d = match *layer.get_tile(x, y) {
|
||||||
d = match *tile {
|
Tile::Floor(_) => d.max(t),
|
||||||
Tile::Floor(_) => d.max(t),
|
Tile::Static(_, solid) => if solid { d.max(t) } else { d },
|
||||||
Tile::Static(_, solid) => if solid { d.max(t) } else { d },
|
_ => d
|
||||||
_ => d
|
};
|
||||||
}
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
x -= 1;
|
x -= 1;
|
||||||
}
|
}
|
||||||
@@ -323,15 +318,11 @@ fn main() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(tile) = layer.get_tile(x, y) {
|
d = match *layer.get_tile(x, y) {
|
||||||
d = match *tile {
|
Tile::Floor(_) => d.min(t),
|
||||||
Tile::Floor(_) => d.min(t),
|
Tile::Static(_, solid) => if solid { d.min(t) } else { d },
|
||||||
Tile::Static(_, solid) => if solid { d.min(t) } else { d },
|
_ => d
|
||||||
_ => d
|
};
|
||||||
}
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
y += 1;
|
y += 1;
|
||||||
}
|
}
|
||||||
@@ -363,15 +354,11 @@ fn main() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(tile) = layer.get_tile(x, y) {
|
d = match *layer.get_tile(x, y) {
|
||||||
d = match *tile {
|
Tile::Floor(_) => d.max(t),
|
||||||
Tile::Floor(_) => d.max(t),
|
Tile::Static(_, solid) => if solid { d.max(t) } else { d },
|
||||||
Tile::Static(_, solid) => if solid { d.max(t) } else { d },
|
_ => d
|
||||||
_ => d
|
};
|
||||||
}
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
y -= 1;
|
y -= 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use sprite::{Sprite, StaticSprite, AnimatedSprite};
|
|||||||
use game_object::GameObject;
|
use game_object::GameObject;
|
||||||
use component::{Updatable, Renderable};
|
use component::{Updatable, Renderable};
|
||||||
|
|
||||||
|
|
||||||
pub struct PlayerPhysicsComponent {
|
pub struct PlayerPhysicsComponent {
|
||||||
pub dx: f32,
|
pub dx: f32,
|
||||||
pub dy: f32,
|
pub dy: f32,
|
||||||
@@ -28,6 +29,7 @@ impl Updatable for PlayerPhysicsComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct PlayerGraphicsComponent<'a> {
|
pub struct PlayerGraphicsComponent<'a> {
|
||||||
flip_horizontal: Cell<bool>,
|
flip_horizontal: Cell<bool>,
|
||||||
sprite_standing: RefCell<StaticSprite<'a>>,
|
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;
|
use sdl2::rect::Rect;
|
||||||
|
|
||||||
|
|
||||||
pub struct Layer<T> {
|
pub struct Layer<T> {
|
||||||
tiles: Vec<T>,
|
tiles: Vec<Option<T>>,
|
||||||
|
default: T,
|
||||||
width: u32,
|
width: u32,
|
||||||
height: u32,
|
height: u32,
|
||||||
tile_width: u32,
|
tile_width: u32,
|
||||||
@@ -12,9 +11,11 @@ pub struct Layer<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Layer<T> where T: Clone {
|
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> {
|
pub fn new(width: u32, height: u32, tile_width: u32, tile_height: u32, tile: T) -> Layer<T> {
|
||||||
Layer {
|
Layer {
|
||||||
tiles: repeat(tile).take((width * height) as usize).collect(),
|
tiles: vec![None; (width * height) as usize],
|
||||||
|
default: tile,
|
||||||
width: width,
|
width: width,
|
||||||
height: height,
|
height: height,
|
||||||
tile_width: tile_width,
|
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;
|
let offset = (x + y * self.width as i32) as usize;
|
||||||
|
|
||||||
if offset < self.tiles.len() {
|
match *self.tiles.get(offset).unwrap() {
|
||||||
Some(&self.tiles[offset])
|
Some(ref tile) => tile,
|
||||||
} else {
|
None => &self.default
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_tile(&mut self, x: i32, y: i32, tile: T) {
|
pub fn set_tile(&mut self, x: i32, y: i32, tile: T) {
|
||||||
let offset = (x + y * self.width as i32) as usize;
|
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> {
|
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) {
|
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);
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::Layer;
|
|
||||||
|
|
||||||
use sdl2::rect::Rect;
|
use sdl2::rect::Rect;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn layer_find_intersecting() {
|
fn layer_find_intersecting() {
|
||||||
let layer = Layer::new(3, 3, 3, 3, ());
|
let layer = Layer::new(3, 3, 3, 3, ());
|
||||||
|
|||||||
Reference in New Issue
Block a user