move sprite to a separate file
This commit is contained in:
80
src/main.rs
80
src/main.rs
@@ -15,11 +15,14 @@ use tile::Layer;
|
|||||||
use camera::Camera;
|
use camera::Camera;
|
||||||
use player::Player;
|
use player::Player;
|
||||||
use keyboard::KeyboardHandler;
|
use keyboard::KeyboardHandler;
|
||||||
|
use sprite::{Sprite, StaticSprite, AnimatedSprite};
|
||||||
|
|
||||||
|
|
||||||
mod tile;
|
mod tile;
|
||||||
mod camera;
|
mod camera;
|
||||||
mod player;
|
mod player;
|
||||||
mod keyboard;
|
mod keyboard;
|
||||||
|
mod sprite;
|
||||||
|
|
||||||
|
|
||||||
const SCREEN_WIDTH : i32 = 960;
|
const SCREEN_WIDTH : i32 = 960;
|
||||||
@@ -46,83 +49,6 @@ enum Tile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
trait Sprite {
|
|
||||||
fn update(&mut self, usize) {}
|
|
||||||
fn render(&self, &sdl2::render::Renderer, &Rect);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
struct StaticSprite<'a> {
|
|
||||||
texture: &'a sdl2::render::Texture,
|
|
||||||
x: i32,
|
|
||||||
y: i32
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> StaticSprite<'a> {
|
|
||||||
fn new(texture: &'a sdl2::render::Texture, x: i32, y: i32) -> StaticSprite<'a> {
|
|
||||||
StaticSprite {
|
|
||||||
texture: texture,
|
|
||||||
x: x,
|
|
||||||
y: y
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Sprite for StaticSprite<'a> {
|
|
||||||
fn render(&self, renderer: &sdl2::render::Renderer, destination: &Rect) {
|
|
||||||
let _ = renderer.copy(self.texture, Some(Rect::new(80 + (16 * self.x), 16 * self.y, 16, 16)), Some(*destination));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
struct AnimatedSprite<'a> {
|
|
||||||
texture: &'a sdl2::render::Texture,
|
|
||||||
x: i32,
|
|
||||||
y: i32,
|
|
||||||
frame: i32,
|
|
||||||
frames: i32,
|
|
||||||
time: usize,
|
|
||||||
frame_time: usize
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> AnimatedSprite<'a> {
|
|
||||||
fn new(texture: &'a sdl2::render::Texture, x: i32, y: i32, frames: i32, fps: i32) -> AnimatedSprite<'a> {
|
|
||||||
AnimatedSprite {
|
|
||||||
texture: texture,
|
|
||||||
x: x,
|
|
||||||
y: y,
|
|
||||||
frame: 0,
|
|
||||||
frames: frames,
|
|
||||||
time: 0,
|
|
||||||
frame_time: 1000 / fps as usize
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Sprite for AnimatedSprite<'a> {
|
|
||||||
fn update(&mut self, elapsed: usize) {
|
|
||||||
self.time += elapsed;
|
|
||||||
|
|
||||||
if self.time > self.frame_time {
|
|
||||||
self.frame += 1;
|
|
||||||
self.time = 0;
|
|
||||||
|
|
||||||
if self.frame < self.frames {
|
|
||||||
self.x += 16;
|
|
||||||
} else {
|
|
||||||
self.x -= 16 * (self.frames - 1) as i32;
|
|
||||||
|
|
||||||
self.frame = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render(&self, renderer: &sdl2::render::Renderer, destination: &Rect) {
|
|
||||||
let _ = renderer.copy(self.texture, Some(Rect::new(self.x, self.y, 16, 16)), Some(*destination));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
sdl2::init(sdl2::INIT_EVERYTHING);
|
sdl2::init(sdl2::INIT_EVERYTHING);
|
||||||
sdl2_image::init(sdl2_image::INIT_PNG);
|
sdl2_image::init(sdl2_image::INIT_PNG);
|
||||||
|
|||||||
79
src/sprite.rs
Normal file
79
src/sprite.rs
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
use sdl2::render::{Texture, Renderer};
|
||||||
|
use sdl2::rect::Rect;
|
||||||
|
|
||||||
|
|
||||||
|
pub trait Sprite {
|
||||||
|
fn update(&mut self, usize) {}
|
||||||
|
fn render(&self, &Renderer, &Rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub struct StaticSprite<'a> {
|
||||||
|
texture: &'a Texture,
|
||||||
|
x: i32,
|
||||||
|
y: i32
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> StaticSprite<'a> {
|
||||||
|
pub fn new(texture: &'a Texture, x: i32, y: i32) -> StaticSprite<'a> {
|
||||||
|
StaticSprite {
|
||||||
|
texture: texture,
|
||||||
|
x: x,
|
||||||
|
y: y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Sprite for StaticSprite<'a> {
|
||||||
|
fn render(&self, renderer: &Renderer, destination: &Rect) {
|
||||||
|
let _ = renderer.copy(self.texture, Some(Rect::new(80 + (16 * self.x), 16 * self.y, 16, 16)), Some(*destination));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub struct AnimatedSprite<'a> {
|
||||||
|
texture: &'a Texture,
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
frame: i32,
|
||||||
|
frames: i32,
|
||||||
|
time: usize,
|
||||||
|
frame_time: usize
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> AnimatedSprite<'a> {
|
||||||
|
pub fn new(texture: &'a Texture, x: i32, y: i32, frames: i32, fps: i32) -> AnimatedSprite<'a> {
|
||||||
|
AnimatedSprite {
|
||||||
|
texture: texture,
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
|
frame: 0,
|
||||||
|
frames: frames,
|
||||||
|
time: 0,
|
||||||
|
frame_time: 1000 / fps as usize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Sprite for AnimatedSprite<'a> {
|
||||||
|
fn update(&mut self, elapsed: usize) {
|
||||||
|
self.time += elapsed;
|
||||||
|
|
||||||
|
if self.time > self.frame_time {
|
||||||
|
self.frame += 1;
|
||||||
|
self.time = 0;
|
||||||
|
|
||||||
|
if self.frame < self.frames {
|
||||||
|
self.x += 16;
|
||||||
|
} else {
|
||||||
|
self.x -= 16 * (self.frames - 1) as i32;
|
||||||
|
|
||||||
|
self.frame = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render(&self, renderer: &Renderer, destination: &Rect) {
|
||||||
|
let _ = renderer.copy(self.texture, Some(Rect::new(self.x, self.y, 16, 16)), Some(*destination));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user