Prepare for a texture handler.

This commit is contained in:
2015-11-18 07:08:12 +01:00
parent 578909c847
commit 2ba792a8fd
3 changed files with 18 additions and 26 deletions

View File

@@ -6,8 +6,6 @@ extern crate sdl2_image;
use std::path::Path;
use std::thread::sleep_ms;
//use sdl2::video::{Window, WindowPos, OPENGL};
//use sdl2::timer::{get_ticks, delay};
use sdl2_image::LoadTexture;
use sdl2::rect::Rect;
use sdl2::keyboard::Keycode;
@@ -226,6 +224,7 @@ fn main() {
};
player.dx = a * PLAYER_SPEED_X + (1.0 - a) * player.dx;
player_sprite.flip = (false, false);
} else if keyboard.is_held(Keycode::Left) && (player.dx <= 0.0 || player.on_ground) {
let a = if player.dx < 0.0 {
PLAYER_ACCELERATION_X_START
@@ -234,6 +233,7 @@ fn main() {
};
player.dx = a * -PLAYER_SPEED_X + (1.0 - a) * player.dx;
player_sprite.flip = (true, false);
} else if player.on_ground {
player.dx = (1.0 - PLAYER_ACCELERATION_X_STOP) * player.dx;
@@ -442,7 +442,6 @@ fn main() {
let player_rect = camera_relative_rect(&camera.to_rect(), &player.to_rect());
player_sprite.flip = (true, false);
player_sprite.render(&mut renderer, &player_rect);
renderer.present();

View File

@@ -1,9 +1,5 @@
use sdl2::rect::Rect;
pub enum PlayerFacing {
Left,
Right
}
pub struct Player {
pub x: f32,
@@ -13,8 +9,7 @@ pub struct Player {
pub dx: f32,
pub dy: f32,
pub gravity: f32,
pub on_ground: bool,
pub facing: PlayerFacing
pub on_ground: bool
}
impl Player {
@@ -27,25 +22,11 @@ impl Player {
dx: 0.0,
dy: 0.0,
gravity: 0.3,
on_ground: false,
facing: PlayerFacing::Right
on_ground: false
}
}
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;
if self.dy > 8.0 {

View File

@@ -1,3 +1,4 @@
use std::collections::HashMap;
use sdl2::render::{Texture, Renderer};
use sdl2::rect::Rect;
@@ -8,11 +9,22 @@ pub trait Sprite {
}
pub struct TextureManager<'a> {
textures: HashMap<String, &'a Texture>
}
impl<'a> TextureManager<'a> {
pub fn insert_texture(&mut self, key: String, texture: &'a Texture) {
self.textures.insert(key, texture);
}
}
pub struct StaticSprite<'a> {
texture: &'a Texture,
x: i32,
y: i32,
flip: (bool, bool)
pub flip: (bool, bool)
}
impl<'a> StaticSprite<'a> {
@@ -37,7 +49,7 @@ pub struct AnimatedSprite<'a> {
texture: &'a Texture,
x: i32,
y: i32,
flip: (bool, bool),
pub flip: (bool, bool),
frame: u32,
frames: u32,
time: u64,