Prepare to flip player.

This commit is contained in:
2015-11-17 16:41:42 +01:00
parent a9d90d8c23
commit 578909c847
3 changed files with 30 additions and 6 deletions

View File

@@ -442,6 +442,7 @@ fn main() {
let player_rect = camera_relative_rect(&camera.to_rect(), &player.to_rect()); let player_rect = camera_relative_rect(&camera.to_rect(), &player.to_rect());
player_sprite.flip = (true, false);
player_sprite.render(&mut renderer, &player_rect); player_sprite.render(&mut renderer, &player_rect);
renderer.present(); renderer.present();

View File

@@ -1,5 +1,9 @@
use sdl2::rect::Rect; use sdl2::rect::Rect;
pub enum PlayerFacing {
Left,
Right
}
pub struct Player { pub struct Player {
pub x: f32, pub x: f32,
@@ -9,7 +13,8 @@ pub struct Player {
pub dx: f32, pub dx: f32,
pub dy: f32, pub dy: f32,
pub gravity: f32, pub gravity: f32,
pub on_ground: bool pub on_ground: bool,
pub facing: PlayerFacing
} }
impl Player { impl Player {
@@ -22,11 +27,25 @@ impl Player {
dx: 0.0, dx: 0.0,
dy: 0.0, dy: 0.0,
gravity: 0.3, gravity: 0.3,
on_ground: false on_ground: false,
facing: PlayerFacing::Right
} }
} }
pub fn update(&mut self) { pub fn update(&mut self) {
match self.facing {
PlayerFacing::Right => {
if self.dx < 0.0 {
self.facing = PlayerFacing::Left;
}
},
PlayerFacing::Left => {
if self.dx > 0.0 {
self.facing = PlayerFacing::Right;
}
}
}
self.dy += self.gravity; self.dy += self.gravity;
if self.dy > 8.0 { if self.dy > 8.0 {

View File

@@ -11,7 +11,8 @@ pub trait Sprite {
pub struct StaticSprite<'a> { pub struct StaticSprite<'a> {
texture: &'a Texture, texture: &'a Texture,
x: i32, x: i32,
y: i32 y: i32,
flip: (bool, bool)
} }
impl<'a> StaticSprite<'a> { impl<'a> StaticSprite<'a> {
@@ -19,14 +20,15 @@ impl<'a> StaticSprite<'a> {
StaticSprite { StaticSprite {
texture: texture, texture: texture,
x: x, x: x,
y: y y: y,
flip: (false, false)
} }
} }
} }
impl<'a> Sprite for StaticSprite<'a> { impl<'a> Sprite for StaticSprite<'a> {
fn render(&self, drawer: &mut Renderer, destination: &Rect) { fn render(&self, drawer: &mut Renderer, destination: &Rect) {
drawer.copy(self.texture, Some(Rect::new_unwrap(self.x, self.y, 16, 16)), Some(*destination)); drawer.copy_ex(self.texture, Some(Rect::new_unwrap(self.x, self.y, 16, 16)), Some(*destination), 0.0, None, self.flip);
} }
} }
@@ -35,6 +37,7 @@ pub struct AnimatedSprite<'a> {
texture: &'a Texture, texture: &'a Texture,
x: i32, x: i32,
y: i32, y: i32,
flip: (bool, bool),
frame: u32, frame: u32,
frames: u32, frames: u32,
time: u64, time: u64,
@@ -47,6 +50,7 @@ impl<'a> AnimatedSprite<'a> {
texture: texture, texture: texture,
x: x, x: x,
y: y, y: y,
flip: (false, false),
frame: 0, frame: 0,
frames: frames, frames: frames,
time: 0, time: 0,
@@ -74,6 +78,6 @@ impl<'a> Sprite for AnimatedSprite<'a> {
} }
fn render(&self, drawer: &mut Renderer, destination: &Rect) { fn render(&self, drawer: &mut Renderer, destination: &Rect) {
drawer.copy_ex(self.texture, Some(Rect::new_unwrap(self.x, self.y, 16, 16)), Some(*destination), 0.0, None, (false, false)); drawer.copy_ex(self.texture, Some(Rect::new_unwrap(self.x, self.y, 16, 16)), Some(*destination), 0.0, None, self.flip);
} }
} }