Update crates.

Fmt.
Clippy.
This commit is contained in:
2017-11-21 09:50:59 +01:00
parent ec42cb33e0
commit ac57e9ee38
13 changed files with 320 additions and 194 deletions

View File

@@ -41,10 +41,12 @@ impl Camera {
}
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 {

View File

@@ -15,7 +15,9 @@ pub struct PlayerJumpCommand<'a> {
impl<'a> PlayerJumpCommand<'a> {
pub fn new(game_object: &'a mut GameObject<'a>) -> PlayerJumpCommand<'a> {
PlayerJumpCommand { game_object: RefCell::new(game_object) }
PlayerJumpCommand {
game_object: RefCell::new(game_object),
}
}
}

View File

@@ -1,5 +1,6 @@
use sdl2::rect::Rect;
use sdl2::render::Renderer;
use sdl2::render::Canvas;
use sdl2::video::Window;
use game_object::GameObject;
@@ -9,5 +10,5 @@ pub trait Updatable {
}
pub trait Renderable {
fn render(&self, object: &GameObject, f64, &mut Renderer, &Rect);
fn render(&self, object: &GameObject, f64, &mut Canvas<Window>, &Rect);
}

View File

@@ -1,7 +1,8 @@
use sdl2::rect::Rect;
use sdl2::render::Renderer;
use sdl2::render::Canvas;
use sdl2::video::Window;
use component::{Updatable, Renderable};
use component::{Renderable, Updatable};
pub struct GameObject<'a> {
@@ -9,8 +10,8 @@ pub struct GameObject<'a> {
pub y: f32,
pub w: u32,
pub h: u32,
pub dx: f32, // TODO moved to PlayerPhysicsComponent
pub dy: f32, // TODO moved to PlayerPhysicsComponent
pub dx: f32, // TODO moved to PlayerPhysicsComponent
pub dy: f32, // TODO moved to PlayerPhysicsComponent
pub gravity: f32, // TODO moved to PlayerPhysicsComponent
pub on_ground: bool,
physics: Box<Updatable + 'a>,
@@ -18,11 +19,12 @@ pub struct 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 {
x: x,
y: y,
@@ -43,7 +45,7 @@ impl<'a> GameObject<'a> {
}
#[inline]
pub fn render(&self, elapsed: f64, renderer: &mut Renderer, destination: &Rect) {
pub fn render(&self, elapsed: f64, renderer: &mut Canvas<Window>, destination: &Rect) {
self.graphics.render(self, elapsed, renderer, destination);
}

View File

@@ -20,11 +20,11 @@ impl KeyboardHandler {
pub fn process(&mut self, event: &Event) {
match *event {
Event::KeyDown { keycode, repeat, .. } => {
if !repeat {
self.key_down(keycode.unwrap());
}
}
Event::KeyDown {
keycode, repeat, ..
} => if !repeat {
self.key_down(keycode.unwrap());
},
Event::KeyUp { keycode, .. } => {
self.key_up(keycode.unwrap());
}

View File

@@ -13,7 +13,7 @@ use keyboard::KeyboardHandler;
use sprite::{Sprite, StaticSprite};
use timer::Timer;
use game_object::GameObject;
use player_components::{PlayerPhysicsComponent, PlayerGraphicsComponent};
use player_components::{PlayerGraphicsComponent, PlayerPhysicsComponent};
mod timer;
@@ -59,43 +59,46 @@ fn main() {
let _ = sdl2::image::init(sdl2::image::INIT_PNG).unwrap();
let window = video_subsystem.window("Super Matte Bros", SCREEN_WIDTH, SCREEN_HEIGHT)
let window = video_subsystem
.window("Super Matte Bros", SCREEN_WIDTH, SCREEN_HEIGHT)
.position_centered()
.build()
.unwrap();
let mut renderer = window.renderer()
.software()
.build()
.unwrap();
let mut canvas = window.into_canvas().software().build().unwrap();
let creator = canvas.texture_creator();
let world_sprites = renderer.load_texture(Path::new("gfx/world.png")).unwrap();
let world_sprites = creator.load_texture(Path::new("gfx/world.png")).unwrap();
let _foo = tiler::Set {
image: renderer.load_texture(Path::new("gfx/world.png")).unwrap(),
image: creator.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,
}],
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 brick_sprite = StaticSprite::new(&world_sprites, 16 * 1, 16 * 0);
let player_sprites = renderer.load_texture(Path::new("gfx/mario.png")).unwrap();
let player_sprites = creator.load_texture(Path::new("gfx/mario.png")).unwrap();
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();
@@ -279,7 +282,11 @@ fn main() {
d = match *layer.get_tile(x, y) {
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,
};
@@ -311,7 +318,11 @@ fn main() {
d = match *layer.get_tile(x, y) {
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,
};
@@ -345,7 +356,11 @@ fn main() {
d = match *layer.get_tile(x, y) {
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,
};
@@ -381,7 +396,11 @@ fn main() {
d = match *layer.get_tile(x, y) {
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,
};
@@ -407,17 +426,18 @@ fn main() {
lag -= MS_PER_UPDATE;
}
renderer.set_draw_color(Color::RGB(93, 148, 251));
renderer.clear();
canvas.set_draw_color(Color::RGB(93, 148, 251));
canvas.clear();
layer.for_each_intersecting(&camera.to_rect(), |tile: &Tile, position: &Rect| {
let object = camera.to_relative_rect(position);
match *tile {
Tile::Background(src) |
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)
Tile::Background(src) | Tile::Floor(src) => canvas
.copy(&world_sprites, Some(src), Some(object))
.unwrap(),
Tile::Static(sprite, _) => {
sprite.render(lag / MS_PER_UPDATE, &mut canvas, &object)
}
_ => (),
}
@@ -425,8 +445,8 @@ fn main() {
let player_rect = camera.to_relative_rect(&player.to_rect());
player.render(elapsed, &mut renderer, &player_rect);
player.render(elapsed, &mut canvas, &player_rect);
renderer.present();
canvas.present();
}
}

View File

@@ -1,11 +1,12 @@
use std::cell::{Cell, RefCell};
use sdl2::rect::Rect;
use sdl2::render::{Renderer, Texture};
use sdl2::render::{Canvas, Texture};
use sdl2::video::Window;
use sprite::{Sprite, StaticSprite, AnimatedSprite};
use sprite::{AnimatedSprite, Sprite, StaticSprite};
use game_object::GameObject;
use component::{Updatable, Renderable};
use component::{Renderable, Updatable};
pub struct PlayerPhysicsComponent {
@@ -48,11 +49,13 @@ impl<'a> 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 Canvas<Window>,
destination: &Rect,
) {
if !object.on_ground {
let mut sprite = self.sprite_jumping.borrow_mut();

View File

@@ -1,14 +1,16 @@
use std::cell::RefCell;
use sdl2::render::{Texture, Renderer};
use sdl2::render::{Canvas, Texture};
use sdl2::video::Window;
use sdl2::rect::Rect;
pub trait Sprite {
fn render(&self, f64, &mut Renderer, &Rect);
fn render(&self, f64, &mut Canvas<Window>, &Rect);
}
pub struct StaticSprite<'a> {
texture: &'a Texture,
texture: &'a Texture<'a>,
x: i32,
y: i32,
pub flip_horizontal: bool,
@@ -28,19 +30,21 @@ impl<'a> StaticSprite<'a> {
}
impl<'a> Sprite for StaticSprite<'a> {
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);
fn render(&self, _: f64, drawer: &mut Canvas<Window>, 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,
);
}
}
pub struct AnimatedSprite<'a> {
texture: &'a Texture,
texture: &'a Texture<'a>,
x: i32,
y: i32,
pub flip_horizontal: bool,
@@ -68,7 +72,7 @@ impl<'a> AnimatedSprite<'a> {
}
impl<'a> Sprite for AnimatedSprite<'a> {
fn render(&self, elapsed: f64, drawer: &mut Renderer, destination: &Rect) {
fn render(&self, elapsed: f64, drawer: &mut Canvas<Window>, destination: &Rect) {
let mut time = self.time.borrow_mut();
let mut frame = self.frame.borrow_mut();
@@ -79,12 +83,14 @@ impl<'a> Sprite for AnimatedSprite<'a> {
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

@@ -11,7 +11,8 @@ pub struct Layer<T> {
}
impl<T> Layer<T>
where T: Clone
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> {
@@ -28,7 +29,7 @@ impl<T> Layer<T>
pub fn get_tile(&self, x: i32, y: i32) -> &T {
let offset = (x + y * self.width as i32) as usize;
match *self.tiles.get(offset).unwrap() {
match self.tiles[offset] {
Some(ref tile) => tile,
None => &self.default,
}
@@ -41,12 +42,13 @@ impl<T> Layer<T>
}
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;
}
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;
}
@@ -56,17 +58,24 @@ impl<T> Layer<T>
let x2 = (rect.x() + rect.width() as i32 - 1) / self.tile_width as i32;
let y2 = (rect.y() + rect.height() as i32 - 1) / self.tile_height as i32;
Some(Rect::new(x1, y1, (x2 - x1 + 1) as u32, (y2 - y1 + 1) as u32))
Some(Rect::new(
x1,
y1,
(x2 - x1 + 1) as u32,
(y2 - y1 + 1) as u32,
))
}
pub fn for_each_intersecting<F: FnMut(&T, &Rect)>(&self, rect: &Rect, mut f: F) {
if let Some(intersect) = self.find_intersecting(rect) {
for y in intersect.y()..(intersect.y() + intersect.height() 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);
}
@@ -75,10 +84,12 @@ impl<T> Layer<T>
}
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,
)
}
}
@@ -102,71 +113,123 @@ mod tests {
assert_eq!(layer.find_intersecting(&Rect::new(9, 9, 1, 1)), None);
// 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(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, 1, 1, 1)),
Some(Rect::new(0, 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(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, 4, 1, 1)),
Some(Rect::new(0, 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(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)));
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(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.
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(5, 2, 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)));
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(5, 2, 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.
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(5, 1, 2, 1)),
Some(Rect::new(1, 0, 2, 1)));
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(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(5, 4, 2, 1)),
Some(Rect::new(1, 1, 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(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(5, 7, 2, 1)),
Some(Rect::new(1, 2, 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(5, 7, 2, 1)),
Some(Rect::new(1, 2, 2, 1))
);
// 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, 5, 1, 2)),
Some(Rect::new(0, 1, 1, 2)));
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, 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, 5, 1, 2)),
Some(Rect::new(1, 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, 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, 5, 1, 2)),
Some(Rect::new(2, 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, 5, 1, 2)),
Some(Rect::new(2, 1, 1, 2))
);
// 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))
);
}
}

View File

@@ -6,16 +6,16 @@ use sdl2::render::Texture;
type TileId = usize;
pub struct Map {
pub struct Map<'a> {
width: usize,
height: usize,
sets: Vec<Set>,
sets: Vec<Set<'a>>,
layers: Vec<Layer>,
}
pub struct Set {
pub image: Texture,
pub struct Set<'a> {
pub image: Texture<'a>,
pub width: usize,
pub height: usize,
pub tiles: Vec<Tile>,

View File

@@ -6,7 +6,9 @@ pub struct Timer {
impl Timer {
pub fn new() -> Timer {
Timer { time: Instant::now() }
Timer {
time: Instant::now(),
}
}
pub fn current_time(&self) -> f64 {