Did stuff.

This commit is contained in:
2017-02-03 10:00:07 +01:00
parent 8178b415ae
commit ec42cb33e0
13 changed files with 277 additions and 111 deletions

44
Cargo.lock generated
View File

@@ -2,7 +2,7 @@
name = "super-matte-bros" name = "super-matte-bros"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"sdl2 0.27.2 (registry+https://github.com/rust-lang/crates.io-index)", "sdl2 0.28.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@@ -12,22 +12,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "0.2.1" version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.11" version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "num" name = "num"
version = "0.1.32" version = "0.1.36"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@@ -35,7 +35,7 @@ name = "num-integer"
version = "0.1.32" version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@@ -44,32 +44,32 @@ version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.1.32" version = "0.1.36"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.3.14" version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "sdl2" name = "sdl2"
version = "0.27.2" version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
"num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"sdl2-sys 0.27.2 (registry+https://github.com/rust-lang/crates.io-index)", "sdl2-sys 0.27.2 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@@ -78,17 +78,17 @@ name = "sdl2-sys"
version = "0.27.2" version = "0.27.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[metadata] [metadata]
"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
"checksum lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "49247ec2a285bb3dcb23cbd9c35193c025e7251bfce77c1d5da97e6362dffe7f" "checksum lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6abe0ee2e758cd6bc8a2cd56726359007748fbf4128da998b65d0b70f881e19b"
"checksum libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "c96061f0c8a2dc27482e394d82e23073569de41d73cd736672ccd3e5c7471bfd" "checksum libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)" = "9e030dc72013ed68994d1b2cbf36a94dd0e58418ba949c4b0db7eeb70a7a6352"
"checksum num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "c04bd954dbf96f76bab6e5bd6cef6f1ce1262d15268ce4f926d2b5b778fa7af2" "checksum num 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "bde7c03b09e7c6a301ee81f6ddf66d7a28ec305699e3d3b056d2fc56470e3120"
"checksum num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "fb24d9bfb3f222010df27995441ded1e954f8f69cd35021f6bef02ca9552fb92" "checksum num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "fb24d9bfb3f222010df27995441ded1e954f8f69cd35021f6bef02ca9552fb92"
"checksum num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "287a1c9969a847055e1122ec0ea7a5c5d6f72aad97934e131c83d5c08ab4e45c" "checksum num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "287a1c9969a847055e1122ec0ea7a5c5d6f72aad97934e131c83d5c08ab4e45c"
"checksum num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "51eab148f171aefad295f8cece636fc488b9b392ef544da31ea4b8ef6b9e9c39" "checksum num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "a16a42856a256b39c6d3484f097f6713e14feacd9bfb02290917904fae46c81c"
"checksum rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "2791d88c6defac799c3f20d74f094ca33b9332612d9aef9078519c82e4fe04a5" "checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
"checksum sdl2 0.27.2 (registry+https://github.com/rust-lang/crates.io-index)" = "55d39a8921d9cab08e229dccb574940c99b5819f872ff13335f7e087ffbbdf53" "checksum sdl2 0.28.0 (registry+https://github.com/rust-lang/crates.io-index)" = "29301273acc73295e4864a069bf9ad791ef0f819f4fc2a4653aa55818f1cbb05"
"checksum sdl2-sys 0.27.2 (registry+https://github.com/rust-lang/crates.io-index)" = "37cfe343db4cd2159cab098096fff52e92d513c2a5b1ee06abbfd5db5323a64d" "checksum sdl2-sys 0.27.2 (registry+https://github.com/rust-lang/crates.io-index)" = "37cfe343db4cd2159cab098096fff52e92d513c2a5b1ee06abbfd5db5323a64d"

View File

@@ -4,9 +4,12 @@ version = "0.0.1"
authors = ["logaritmisk <anders.e.olsson@gmail.com>"] authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies.sdl2] [dependencies.sdl2]
version = "0.27" version = "0.28"
default-features = false default-features = false
features = ["image"] features = ["image"]
[[bin]] [[bin]]
name = "super-matte-bros" name = "super-matte-bros"
[profile.release]
lto = true

View File

@@ -6,7 +6,7 @@ pub struct Camera {
y: i32, y: i32,
width: u32, width: u32,
height: u32, height: u32,
bounding: Rect bounding: Rect,
} }
impl Camera { impl Camera {
@@ -16,7 +16,7 @@ impl Camera {
y: y, y: y,
width: width, width: width,
height: height, height: height,
bounding: bounding bounding: bounding,
} }
} }
@@ -41,7 +41,10 @@ impl Camera {
} }
pub fn to_relative_rect(&self, rect: &Rect) -> Rect { pub fn to_relative_rect(&self, rect: &Rect) -> Rect {
Rect::new(rect.x() - self.x, rect.y() - self.y, rect.width(), rect.height()) Rect::new(rect.x() - self.x,
rect.y() - self.y,
rect.width(),
rect.height())
} }
pub fn to_rect(&self) -> Rect { pub fn to_rect(&self) -> Rect {

34
src/command.rs Normal file
View File

@@ -0,0 +1,34 @@
use std::cell::RefCell;
use game_object::GameObject;
pub trait Command {
fn apply(&mut self);
fn revert(&mut self);
}
pub struct PlayerJumpCommand<'a> {
game_object: RefCell<&'a mut GameObject<'a>>,
}
impl<'a> PlayerJumpCommand<'a> {
pub fn new(game_object: &'a mut GameObject<'a>) -> PlayerJumpCommand<'a> {
PlayerJumpCommand { game_object: RefCell::new(game_object) }
}
}
impl<'a> Command for PlayerJumpCommand<'a> {
fn apply(&mut self) {
let mut game_object = self.game_object.borrow_mut();
game_object.dx += 5.0;
}
fn revert(&mut self) {
let mut game_object = self.game_object.borrow_mut();
game_object.dx -= 5.0;
}
}

1
src/ecs.rs Normal file
View File

@@ -0,0 +1 @@
type Entity = usize;

View File

@@ -3,6 +3,7 @@ use sdl2::render::Renderer;
use component::{Updatable, Renderable}; use component::{Updatable, Renderable};
pub struct GameObject<'a> { pub struct GameObject<'a> {
pub x: f32, pub x: f32,
pub y: f32, pub y: f32,
@@ -13,11 +14,15 @@ pub struct GameObject<'a> {
pub gravity: f32, // TODO moved to PlayerPhysicsComponent pub gravity: f32, // TODO moved to PlayerPhysicsComponent
pub on_ground: bool, pub on_ground: bool,
physics: Box<Updatable + 'a>, physics: Box<Updatable + 'a>,
graphics: Box<Renderable + 'a> graphics: Box<Renderable + 'a>,
} }
impl<'a> GameObject<'a> { impl<'a> GameObject<'a> {
pub fn new(x: f32, y: f32, physics: Box<Updatable + 'a>, graphics: Box<Renderable + 'a>) -> GameObject<'a> { pub fn new(x: f32,
y: f32,
physics: Box<Updatable + 'a>,
graphics: Box<Renderable + 'a>)
-> GameObject<'a> {
GameObject { GameObject {
x: x, x: x,
y: y, y: y,
@@ -28,18 +33,21 @@ impl<'a> GameObject<'a> {
gravity: 0.3, gravity: 0.3,
on_ground: false, on_ground: false,
physics: physics, physics: physics,
graphics: graphics graphics: graphics,
} }
} }
#[inline]
pub fn update(&self) { pub fn update(&self) {
self.physics.update(self); self.physics.update(self);
} }
#[inline]
pub fn render(&self, elapsed: f64, renderer: &mut Renderer, destination: &Rect) { pub fn render(&self, elapsed: f64, renderer: &mut Renderer, destination: &Rect) {
self.graphics.render(self, elapsed, renderer, destination); self.graphics.render(self, elapsed, renderer, destination);
} }
#[inline]
pub fn to_rect(&self) -> Rect { pub fn to_rect(&self) -> Rect {
Rect::new(self.x as i32, self.y as i32, 32, 32) Rect::new(self.x as i32, self.y as i32, 32, 32)
} }

View File

@@ -20,14 +20,14 @@ impl KeyboardHandler {
pub fn process(&mut self, event: &Event) { pub fn process(&mut self, event: &Event) {
match *event { match *event {
Event::KeyDown {keycode, repeat, ..} => { Event::KeyDown { keycode, repeat, .. } => {
if !repeat { if !repeat {
self.key_down(keycode.unwrap()); self.key_down(keycode.unwrap());
} }
}, }
Event::KeyUp {keycode, ..} => { Event::KeyUp { keycode, .. } => {
self.key_up(keycode.unwrap()); self.key_up(keycode.unwrap());
}, }
_ => (), _ => (),
} }
} }
@@ -50,21 +50,21 @@ impl KeyboardHandler {
pub fn was_pressed(&self, keycode: Keycode) -> bool { pub fn was_pressed(&self, keycode: Keycode) -> bool {
match self.pressed_keys.get(&keycode) { match self.pressed_keys.get(&keycode) {
Some(state) => *state, Some(state) => *state,
None => false None => false,
} }
} }
pub fn was_released(&self, keycode: Keycode) -> bool { pub fn was_released(&self, keycode: Keycode) -> bool {
match self.released_keys.get(&keycode) { match self.released_keys.get(&keycode) {
Some(state) => *state, Some(state) => *state,
None => false None => false,
} }
} }
pub fn is_held(&self, keycode: Keycode) -> bool { pub fn is_held(&self, keycode: Keycode) -> bool {
match self.held_keys.get(&keycode) { match self.held_keys.get(&keycode) {
Some(state) => *state, Some(state) => *state,
None => false None => false,
} }
} }
} }

View File

@@ -24,22 +24,24 @@ mod sprite;
mod game_object; mod game_object;
mod component; mod component;
mod player_components; mod player_components;
mod tiler;
mod command;
const SCREEN_WIDTH : u32 = 960; const SCREEN_WIDTH: u32 = 960;
const SCREEN_HEIGHT : u32 = 640; const SCREEN_HEIGHT: u32 = 640;
const TILE_WIDTH : u32 = 32; const TILE_WIDTH: u32 = 32;
const TILE_HEIGHT : u32 = 32; const TILE_HEIGHT: u32 = 32;
const MS_PER_UPDATE : f64 = 10.0; const MS_PER_UPDATE: f64 = 10.0;
const PLAYER_SPEED_X : f32 = 4.0; const PLAYER_SPEED_X: f32 = 4.0;
const PLAYER_THRESHOLD_X : f32 = 0.2; const PLAYER_THRESHOLD_X: f32 = 0.2;
const PLAYER_ACCELERATION_X_START : f32 = 0.02; 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)]
@@ -47,7 +49,7 @@ enum Tile<'a> {
Empty, Empty,
Static(&'a StaticSprite<'a>, bool), Static(&'a StaticSprite<'a>, bool),
Background(Rect), Background(Rect),
Floor(Rect) Floor(Rect),
} }
@@ -57,11 +59,32 @@ fn main() {
let _ = sdl2::image::init(sdl2::image::INIT_PNG).unwrap(); let _ = sdl2::image::init(sdl2::image::INIT_PNG).unwrap();
let window = video_subsystem.window("Super Matte Bros", SCREEN_WIDTH, SCREEN_HEIGHT).position_centered().build().unwrap(); let window = video_subsystem.window("Super Matte Bros", SCREEN_WIDTH, SCREEN_HEIGHT)
let mut renderer = window.renderer().software().build().unwrap(); .position_centered()
.build()
.unwrap();
let mut renderer = window.renderer()
.software()
.build()
.unwrap();
let world_sprites = renderer.load_texture(Path::new("gfx/world.png")).unwrap(); let world_sprites = renderer.load_texture(Path::new("gfx/world.png")).unwrap();
let _foo = tiler::Set {
image: renderer.load_texture(Path::new("gfx/world.png")).unwrap(),
width: 16,
height: 16,
tiles: vec![tiler::Tile {
id: 0,
animation: None,
},
tiler::Tile {
id: 1,
animation: None,
}],
};
let floor_sprite = StaticSprite::new(&world_sprites, 16 * 0, 16 * 0); let floor_sprite = StaticSprite::new(&world_sprites, 16 * 0, 16 * 0);
let brick_sprite = StaticSprite::new(&world_sprites, 16 * 1, 16 * 0); let brick_sprite = StaticSprite::new(&world_sprites, 16 * 1, 16 * 0);
@@ -69,7 +92,10 @@ fn main() {
let timer = Timer::new(); let timer = Timer::new();
let mut player = GameObject::new(390.0, 390.0, Box::new(PlayerPhysicsComponent::new()), Box::new(PlayerGraphicsComponent::new(&player_sprites))); let mut player = GameObject::new(390.0,
390.0,
Box::new(PlayerPhysicsComponent::new()),
Box::new(PlayerGraphicsComponent::new(&player_sprites)));
let mut keyboard = KeyboardHandler::new(); let mut keyboard = KeyboardHandler::new();
@@ -183,8 +209,8 @@ fn main() {
use sdl2::event::Event; use sdl2::event::Event;
match event { match event {
Event::KeyDown {keycode, ..} if keycode == Some(Keycode::Escape) => break 'main, Event::KeyDown { keycode, .. } if keycode == Some(Keycode::Escape) => break 'main,
Event::Quit {..} => break 'main, Event::Quit { .. } => break 'main,
_ => (), _ => (),
} }
@@ -254,7 +280,7 @@ fn main() {
d = match *layer.get_tile(x, y) { d = match *layer.get_tile(x, y) {
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,
}; };
x += 1; x += 1;
@@ -286,7 +312,7 @@ fn main() {
d = match *layer.get_tile(x, y) { d = match *layer.get_tile(x, y) {
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,
}; };
x -= 1; x -= 1;
@@ -320,7 +346,7 @@ fn main() {
d = match *layer.get_tile(x, y) { d = match *layer.get_tile(x, y) {
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,
}; };
y += 1; y += 1;
@@ -356,7 +382,7 @@ fn main() {
d = match *layer.get_tile(x, y) { d = match *layer.get_tile(x, y) {
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,
}; };
y -= 1; y -= 1;
@@ -388,9 +414,12 @@ fn main() {
let object = camera.to_relative_rect(position); let object = camera.to_relative_rect(position);
match *tile { match *tile {
Tile::Background(src) | Tile::Floor(src) => renderer.copy(&world_sprites, Some(src), Some(object)).unwrap(), Tile::Background(src) |
Tile::Static(ref sprite, _) => sprite.render(lag / MS_PER_UPDATE, &mut renderer, &object), Tile::Floor(src) => renderer.copy(&world_sprites, Some(src), Some(object)).unwrap(),
_ => () Tile::Static(ref sprite, _) => {
sprite.render(lag / MS_PER_UPDATE, &mut renderer, &object)
}
_ => (),
} }
}); });

View File

@@ -25,8 +25,7 @@ impl PlayerPhysicsComponent {
} }
impl Updatable for PlayerPhysicsComponent { impl Updatable for PlayerPhysicsComponent {
fn update(&self, _: &GameObject) { fn update(&self, _: &GameObject) {}
}
} }
@@ -34,7 +33,7 @@ pub struct PlayerGraphicsComponent<'a> {
flip_horizontal: Cell<bool>, flip_horizontal: Cell<bool>,
sprite_standing: RefCell<StaticSprite<'a>>, sprite_standing: RefCell<StaticSprite<'a>>,
sprite_running: RefCell<AnimatedSprite<'a>>, sprite_running: RefCell<AnimatedSprite<'a>>,
sprite_jumping: RefCell<StaticSprite<'a>> sprite_jumping: RefCell<StaticSprite<'a>>,
} }
impl<'a> PlayerGraphicsComponent<'a> { impl<'a> PlayerGraphicsComponent<'a> {
@@ -43,13 +42,17 @@ impl<'a> PlayerGraphicsComponent<'a> {
flip_horizontal: Cell::new(false), flip_horizontal: Cell::new(false),
sprite_standing: RefCell::new(StaticSprite::new(texture, 80, 32)), sprite_standing: RefCell::new(StaticSprite::new(texture, 80, 32)),
sprite_running: RefCell::new(AnimatedSprite::new(texture, 96, 32, 3, 10.0)), sprite_running: RefCell::new(AnimatedSprite::new(texture, 96, 32, 3, 10.0)),
sprite_jumping: RefCell::new(StaticSprite::new(texture, 160, 32)) sprite_jumping: RefCell::new(StaticSprite::new(texture, 160, 32)),
} }
} }
} }
impl<'a> Renderable for PlayerGraphicsComponent<'a> { impl<'a> Renderable for PlayerGraphicsComponent<'a> {
fn render(&self, object: &GameObject, elapsed: f64, renderer: &mut Renderer, destination: &Rect) { fn render(&self,
object: &GameObject,
elapsed: f64,
renderer: &mut Renderer,
destination: &Rect) {
if !object.on_ground { if !object.on_ground {
let mut sprite = self.sprite_jumping.borrow_mut(); let mut sprite = self.sprite_jumping.borrow_mut();

View File

@@ -12,7 +12,7 @@ pub struct StaticSprite<'a> {
x: i32, x: i32,
y: i32, y: i32,
pub flip_horizontal: bool, pub flip_horizontal: bool,
pub flip_vertical: bool pub flip_vertical: bool,
} }
impl<'a> StaticSprite<'a> { impl<'a> StaticSprite<'a> {
@@ -22,14 +22,20 @@ impl<'a> StaticSprite<'a> {
x: x, x: x,
y: y, y: y,
flip_horizontal: false, flip_horizontal: false,
flip_vertical: false flip_vertical: false,
} }
} }
} }
impl<'a> Sprite for StaticSprite<'a> { impl<'a> Sprite for StaticSprite<'a> {
fn render(&self, _: f64, drawer: &mut Renderer, destination: &Rect) { fn render(&self, _: f64, drawer: &mut Renderer, destination: &Rect) {
let _ = drawer.copy_ex(self.texture, Some(Rect::new(self.x, self.y, 16, 16)), Some(*destination), 0.0, None, self.flip_horizontal, self.flip_vertical); let _ = drawer.copy_ex(self.texture,
Some(Rect::new(self.x, self.y, 16, 16)),
Some(*destination),
0.0,
None,
self.flip_horizontal,
self.flip_vertical);
} }
} }
@@ -42,7 +48,7 @@ pub struct AnimatedSprite<'a> {
frames: u32, frames: u32,
frame_time: f64, frame_time: f64,
frame: RefCell<u32>, frame: RefCell<u32>,
time: RefCell<f64> time: RefCell<f64>,
} }
impl<'a> AnimatedSprite<'a> { impl<'a> AnimatedSprite<'a> {
@@ -56,7 +62,7 @@ impl<'a> AnimatedSprite<'a> {
frame: RefCell::new(0), frame: RefCell::new(0),
frames: frames, frames: frames,
time: RefCell::new(0.0), time: RefCell::new(0.0),
frame_time: 1000.0 / fps as f64 frame_time: 1000.0 / fps as f64,
} }
} }
} }
@@ -73,6 +79,12 @@ impl<'a> Sprite for AnimatedSprite<'a> {
let x = self.x + (*frame * 16) as i32; let x = self.x + (*frame * 16) as i32;
let _ = drawer.copy_ex(self.texture, Some(Rect::new(x, self.y, 16, 16)), Some(*destination), 0.0, None, self.flip_horizontal, self.flip_vertical); let _ = drawer.copy_ex(self.texture,
Some(Rect::new(x, self.y, 16, 16)),
Some(*destination),
0.0,
None,
self.flip_horizontal,
self.flip_vertical);
} }
} }

View File

@@ -7,10 +7,12 @@ pub struct Layer<T> {
width: u32, width: u32,
height: u32, height: u32,
tile_width: u32, tile_width: u32,
tile_height: u32 tile_height: u32,
} }
impl<T> Layer<T> where T: Clone { impl<T> Layer<T>
where T: Clone
{
// TODO Change u32 to usize for width and height? // 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 {
@@ -19,7 +21,7 @@ impl<T> Layer<T> where T: Clone {
width: width, width: width,
height: height, height: height,
tile_width: tile_width, tile_width: tile_width,
tile_height: tile_height tile_height: tile_height,
} }
} }
@@ -28,7 +30,7 @@ impl<T> Layer<T> where T: Clone {
match *self.tiles.get(offset).unwrap() { match *self.tiles.get(offset).unwrap() {
Some(ref tile) => tile, Some(ref tile) => tile,
None => &self.default None => &self.default,
} }
} }
@@ -39,10 +41,12 @@ impl<T> Layer<T> where T: Clone {
} }
pub fn find_intersecting(&self, rect: &Rect) -> Option<Rect> { pub fn find_intersecting(&self, rect: &Rect) -> Option<Rect> {
if rect.x() + rect.width() as i32 <= 0 || rect.x() >= (self.width * self.tile_width) as i32 { if rect.x() + rect.width() as i32 <= 0 ||
rect.x() >= (self.width * self.tile_width) as i32 {
return None; return None;
} }
if rect.y() + rect.height() as i32 <= 0 || rect.y() >= (self.height * self.tile_height) as i32 { if rect.y() + rect.height() as i32 <= 0 ||
rect.y() >= (self.height * self.tile_height) as i32 {
return None; return None;
} }
@@ -59,7 +63,10 @@ impl<T> Layer<T> where T: Clone {
if let Some(intersect) = self.find_intersecting(rect) { if let Some(intersect) = self.find_intersecting(rect) {
for y in intersect.y()..(intersect.y() + intersect.height() as i32) { for y in intersect.y()..(intersect.y() + intersect.height() as i32) {
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), &position); f(self.get_tile(x, y), &position);
} }
@@ -68,7 +75,10 @@ impl<T> Layer<T> where T: Clone {
} }
pub fn to_rect(&self) -> Rect { pub fn to_rect(&self) -> Rect {
Rect::new(0, 0, self.width * self.tile_width, self.height * self.tile_height) Rect::new(0,
0,
self.width * self.tile_width,
self.height * self.tile_height)
} }
} }
@@ -84,7 +94,7 @@ mod tests {
// out of bounds. // out of bounds.
assert_eq!(layer.find_intersecting(&Rect::new(-1, 1, 1, 1)), None); assert_eq!(layer.find_intersecting(&Rect::new(-1, 1, 1, 1)), None);
assert_eq!(layer.find_intersecting(&Rect::new( 1, -1, 1, 1)), None); assert_eq!(layer.find_intersecting(&Rect::new(1, -1, 1, 1)), None);
assert_eq!(layer.find_intersecting(&Rect::new(-1, -1, 1, 1)), None); assert_eq!(layer.find_intersecting(&Rect::new(-1, -1, 1, 1)), None);
assert_eq!(layer.find_intersecting(&Rect::new(9, 7, 1, 1)), None); assert_eq!(layer.find_intersecting(&Rect::new(9, 7, 1, 1)), None);
@@ -92,45 +102,71 @@ mod tests {
assert_eq!(layer.find_intersecting(&Rect::new(9, 9, 1, 1)), None); assert_eq!(layer.find_intersecting(&Rect::new(9, 9, 1, 1)), None);
// middle of tile. // middle of tile.
assert_eq!(layer.find_intersecting(&Rect::new(1, 1, 1, 1)), Some(Rect::new(0, 0, 1, 1))); assert_eq!(layer.find_intersecting(&Rect::new(1, 1, 1, 1)),
assert_eq!(layer.find_intersecting(&Rect::new(4, 1, 1, 1)), Some(Rect::new(1, 0, 1, 1))); Some(Rect::new(0, 0, 1, 1)));
assert_eq!(layer.find_intersecting(&Rect::new(7, 1, 1, 1)), Some(Rect::new(2, 0, 1, 1))); assert_eq!(layer.find_intersecting(&Rect::new(4, 1, 1, 1)),
Some(Rect::new(1, 0, 1, 1)));
assert_eq!(layer.find_intersecting(&Rect::new(7, 1, 1, 1)),
Some(Rect::new(2, 0, 1, 1)));
assert_eq!(layer.find_intersecting(&Rect::new(1, 4, 1, 1)), Some(Rect::new(0, 1, 1, 1))); assert_eq!(layer.find_intersecting(&Rect::new(1, 4, 1, 1)),
assert_eq!(layer.find_intersecting(&Rect::new(4, 4, 1, 1)), Some(Rect::new(1, 1, 1, 1))); Some(Rect::new(0, 1, 1, 1)));
assert_eq!(layer.find_intersecting(&Rect::new(7, 4, 1, 1)), Some(Rect::new(2, 1, 1, 1))); assert_eq!(layer.find_intersecting(&Rect::new(4, 4, 1, 1)),
Some(Rect::new(1, 1, 1, 1)));
assert_eq!(layer.find_intersecting(&Rect::new(7, 4, 1, 1)),
Some(Rect::new(2, 1, 1, 1)));
assert_eq!(layer.find_intersecting(&Rect::new(1, 7, 1, 1)), Some(Rect::new(0, 2, 1, 1))); assert_eq!(layer.find_intersecting(&Rect::new(1, 7, 1, 1)),
assert_eq!(layer.find_intersecting(&Rect::new(4, 7, 1, 1)), Some(Rect::new(1, 2, 1, 1))); Some(Rect::new(0, 2, 1, 1)));
assert_eq!(layer.find_intersecting(&Rect::new(7, 7, 1, 1)), Some(Rect::new(2, 2, 1, 1))); assert_eq!(layer.find_intersecting(&Rect::new(4, 7, 1, 1)),
Some(Rect::new(1, 2, 1, 1)));
assert_eq!(layer.find_intersecting(&Rect::new(7, 7, 1, 1)),
Some(Rect::new(2, 2, 1, 1)));
// interlaps 4 tiles. // interlaps 4 tiles.
assert_eq!(layer.find_intersecting(&Rect::new(2, 2, 2, 2)), Some(Rect::new(0, 0, 2, 2))); assert_eq!(layer.find_intersecting(&Rect::new(2, 2, 2, 2)),
assert_eq!(layer.find_intersecting(&Rect::new(5, 2, 2, 2)), Some(Rect::new(1, 0, 2, 2))); Some(Rect::new(0, 0, 2, 2)));
assert_eq!(layer.find_intersecting(&Rect::new(2, 5, 2, 2)), Some(Rect::new(0, 1, 2, 2))); assert_eq!(layer.find_intersecting(&Rect::new(5, 2, 2, 2)),
assert_eq!(layer.find_intersecting(&Rect::new(5, 5, 2, 2)), Some(Rect::new(1, 1, 2, 2))); Some(Rect::new(1, 0, 2, 2)));
assert_eq!(layer.find_intersecting(&Rect::new(2, 5, 2, 2)),
Some(Rect::new(0, 1, 2, 2)));
assert_eq!(layer.find_intersecting(&Rect::new(5, 5, 2, 2)),
Some(Rect::new(1, 1, 2, 2)));
// interlaps 2 tiles horizontal. // interlaps 2 tiles horizontal.
assert_eq!(layer.find_intersecting(&Rect::new(2, 1, 2, 1)), Some(Rect::new(0, 0, 2, 1))); assert_eq!(layer.find_intersecting(&Rect::new(2, 1, 2, 1)),
assert_eq!(layer.find_intersecting(&Rect::new(5, 1, 2, 1)), Some(Rect::new(1, 0, 2, 1))); Some(Rect::new(0, 0, 2, 1)));
assert_eq!(layer.find_intersecting(&Rect::new(5, 1, 2, 1)),
Some(Rect::new(1, 0, 2, 1)));
assert_eq!(layer.find_intersecting(&Rect::new(2, 4, 2, 1)), Some(Rect::new(0, 1, 2, 1))); assert_eq!(layer.find_intersecting(&Rect::new(2, 4, 2, 1)),
assert_eq!(layer.find_intersecting(&Rect::new(5, 4, 2, 1)), Some(Rect::new(1, 1, 2, 1))); Some(Rect::new(0, 1, 2, 1)));
assert_eq!(layer.find_intersecting(&Rect::new(5, 4, 2, 1)),
Some(Rect::new(1, 1, 2, 1)));
assert_eq!(layer.find_intersecting(&Rect::new(2, 7, 2, 1)), Some(Rect::new(0, 2, 2, 1))); assert_eq!(layer.find_intersecting(&Rect::new(2, 7, 2, 1)),
assert_eq!(layer.find_intersecting(&Rect::new(5, 7, 2, 1)), Some(Rect::new(1, 2, 2, 1))); Some(Rect::new(0, 2, 2, 1)));
assert_eq!(layer.find_intersecting(&Rect::new(5, 7, 2, 1)),
Some(Rect::new(1, 2, 2, 1)));
// interlaps 2 tiles vertical. // interlaps 2 tiles vertical.
assert_eq!(layer.find_intersecting(&Rect::new(1, 2, 1, 2)), Some(Rect::new(0, 0, 1, 2))); assert_eq!(layer.find_intersecting(&Rect::new(1, 2, 1, 2)),
assert_eq!(layer.find_intersecting(&Rect::new(1, 5, 1, 2)), Some(Rect::new(0, 1, 1, 2))); Some(Rect::new(0, 0, 1, 2)));
assert_eq!(layer.find_intersecting(&Rect::new(1, 5, 1, 2)),
Some(Rect::new(0, 1, 1, 2)));
assert_eq!(layer.find_intersecting(&Rect::new(4, 2, 1, 2)), Some(Rect::new(1, 0, 1, 2))); assert_eq!(layer.find_intersecting(&Rect::new(4, 2, 1, 2)),
assert_eq!(layer.find_intersecting(&Rect::new(4, 5, 1, 2)), Some(Rect::new(1, 1, 1, 2))); Some(Rect::new(1, 0, 1, 2)));
assert_eq!(layer.find_intersecting(&Rect::new(4, 5, 1, 2)),
Some(Rect::new(1, 1, 1, 2)));
assert_eq!(layer.find_intersecting(&Rect::new(7, 2, 1, 2)), Some(Rect::new(2, 0, 1, 2))); assert_eq!(layer.find_intersecting(&Rect::new(7, 2, 1, 2)),
assert_eq!(layer.find_intersecting(&Rect::new(7, 5, 1, 2)), Some(Rect::new(2, 1, 1, 2))); Some(Rect::new(2, 0, 1, 2)));
assert_eq!(layer.find_intersecting(&Rect::new(7, 5, 1, 2)),
Some(Rect::new(2, 1, 1, 2)));
// exactly one tile. // exactly one tile.
assert_eq!(layer.find_intersecting(&Rect::new(0, 0, 3, 3)), Some(Rect::new(0, 0, 1, 1))); assert_eq!(layer.find_intersecting(&Rect::new(0, 0, 3, 3)),
Some(Rect::new(0, 0, 1, 1)));
} }
} }

39
src/tiler.rs Normal file
View File

@@ -0,0 +1,39 @@
use std::time::Duration;
use sdl2::render::Texture;
type TileId = usize;
pub struct Map {
width: usize,
height: usize,
sets: Vec<Set>,
layers: Vec<Layer>,
}
pub struct Set {
pub image: Texture,
pub width: usize,
pub height: usize,
pub tiles: Vec<Tile>,
}
pub struct Tile {
pub id: TileId,
pub animation: Option<Vec<Frame>>,
}
pub struct Frame {
tile: TileId,
duration: Duration,
}
pub struct Layer {
tiles: Vec<TileId>,
}

View File

@@ -1,14 +1,12 @@
use std::time::Instant; use std::time::Instant;
pub struct Timer { pub struct Timer {
time: Instant time: Instant,
} }
impl Timer { impl Timer {
pub fn new() -> Timer { pub fn new() -> Timer {
Timer { Timer { time: Instant::now() }
time: Instant::now()
}
} }
pub fn current_time(&self) -> f64 { pub fn current_time(&self) -> f64 {