diff --git a/src/component.rs b/src/component.rs new file mode 100644 index 0000000..5f9ebd8 --- /dev/null +++ b/src/component.rs @@ -0,0 +1,12 @@ +use sdl2::rect::Rect; +use sdl2::render::Renderer; + +use game_object::GameObject; + +pub trait Updatable { + fn update(&self, &GameObject); +} + +pub trait Renderable { + fn render(&self, &GameObject, f64, &mut Renderer, &Rect); +} diff --git a/src/game_object.rs b/src/game_object.rs new file mode 100644 index 0000000..c100b9c --- /dev/null +++ b/src/game_object.rs @@ -0,0 +1,46 @@ +use sdl2::rect::Rect; +use sdl2::render::Renderer; + +use component::{Updatable, Renderable}; + +pub struct GameObject<'a> { + pub x: f32, + pub y: f32, + pub w: u32, + pub h: u32, + pub dx: f32, + pub dy: f32, + pub gravity: f32, + pub on_ground: bool, + physics: Box, + graphics: Box +} + +impl<'a> GameObject<'a> { + pub fn new(x: f32, y: f32, physics: Box, graphics: Box) -> GameObject<'a> { + GameObject { + x: x, + y: y, + w: 32, + h: 32, + dx: 0.0, + dy: 0.0, + gravity: 0.3, + on_ground: false, + physics: physics, + graphics: graphics + } + } + + pub fn update(&self) { + self.physics.update(self); + } + + pub fn render(&self, elapsed: f64, renderer: &mut Renderer, destination: &Rect) { + self.graphics.render(self, elapsed, renderer, destination); + } + + pub fn to_rect(&self) -> Rect { + Rect::new(self.x as i32, self.y as i32, 32, 32) + } +} diff --git a/src/main.rs b/src/main.rs index b1cbb24..61d714a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,19 +11,21 @@ use sdl2::keyboard::Keycode; use sdl2::pixels::Color; use sdl2::render::{Renderer, Texture}; - use tile::Layer; use camera::Camera; use keyboard::KeyboardHandler; use sprite::{Sprite, StaticSprite, AnimatedSprite}; use timer::Timer; - +use game_object::GameObject; +use component::{Updatable, Renderable}; mod timer; mod tile; mod camera; mod keyboard; mod sprite; +mod game_object; +mod component; const SCREEN_WIDTH : u32 = 960; const SCREEN_HEIGHT : u32 = 640; @@ -40,56 +42,6 @@ const PLAYER_ACCELERATION_X_START : f32 = 0.02; const PLAYER_ACCELERATION_X_STOP : f32 = 0.15; const PLAYER_ACCELERATION_X_CHANGE : f32 = 0.06; -struct GameObject<'a> { - pub x: f32, - pub y: f32, - pub w: u32, - pub h: u32, - pub dx: f32, - pub dy: f32, - pub gravity: f32, - pub on_ground: bool, - physics: Box, - graphics: Box -} - -impl<'a> GameObject<'a> { - pub fn new(x: f32, y: f32, physics: Box, graphics: Box) -> GameObject<'a> { - GameObject { - x: x, - y: y, - w: 32, - h: 32, - dx: 0.0, - dy: 0.0, - gravity: 0.3, - on_ground: false, - physics: physics, - graphics: graphics - } - } - - pub fn update(&self) { - self.physics.update(self); - } - - pub fn render(&self, elapsed: f64, renderer: &mut Renderer, destination: &Rect) { - self.graphics.render(self, elapsed, renderer, destination); - } - - pub fn to_rect(&self) -> Rect { - Rect::new(self.x as i32, self.y as i32, 32, 32) - } -} - -trait Updatable { - fn update(&self, &GameObject); -} - -trait Renderable { - fn render(&self, &GameObject, f64, &mut Renderer, &Rect); -} - struct PlayerPhysicsComponent; impl Updatable for PlayerPhysicsComponent { @@ -169,7 +121,6 @@ fn main() { let mut layer = Layer::new(212, 20, TILE_WIDTH, TILE_HEIGHT, Tile::Empty); - layer.set_tile(2, 15, Tile::Background(Rect::new(16 * 9, 16 * 8, 16, 16))); layer.set_tile(1, 16, Tile::Background(Rect::new(16 * 8, 16 * 8, 16, 16))); @@ -189,27 +140,22 @@ fn main() { layer.set_tile(14, 17, Tile::Background(Rect::new(16 * 12, 16 * 9, 16, 16))); layer.set_tile(15, 17, Tile::Background(Rect::new(16 * 13, 16 * 9, 16, 16))); - layer.set_tile(16, 14, Tile::Floor(Rect::new(16 * 24, 16 * 0, 16, 16))); - layer.set_tile(17, 16, Tile::Background(Rect::new(16 * 9, 16 * 8, 16, 16))); layer.set_tile(16, 17, Tile::Background(Rect::new(16 * 8, 16 * 8, 16, 16))); layer.set_tile(17, 17, Tile::Background(Rect::new(16 * 8, 16 * 9, 16, 16))); layer.set_tile(18, 17, Tile::Background(Rect::new(16 * 10, 16 * 8, 16, 16))); - layer.set_tile(20, 14, Tile::Static(&brick_sprite, true)); layer.set_tile(21, 14, Tile::Floor(Rect::new(16 * 24, 16 * 0, 16, 16))); layer.set_tile(22, 14, Tile::Static(&brick_sprite, true)); layer.set_tile(23, 14, Tile::Floor(Rect::new(16 * 24, 16 * 0, 16, 16))); layer.set_tile(24, 14, Tile::Static(&brick_sprite, true)); - layer.set_tile(22, 10, Tile::Floor(Rect::new(16 * 24, 16 * 0, 16, 16))); - layer.set_tile(19, 7, Tile::Floor(Rect::new(16 * 0, 16 * 20, 16, 16))); layer.set_tile(20, 7, Tile::Floor(Rect::new(16 * 1, 16 * 20, 16, 16))); layer.set_tile(21, 7, Tile::Floor(Rect::new(16 * 2, 16 * 20, 16, 16))); @@ -217,18 +163,15 @@ fn main() { layer.set_tile(20, 8, Tile::Floor(Rect::new(16 * 1, 16 * 21, 16, 16))); layer.set_tile(21, 8, Tile::Floor(Rect::new(16 * 2, 16 * 21, 16, 16))); - layer.set_tile(23, 17, Tile::Background(Rect::new(16 * 11, 16 * 9, 16, 16))); layer.set_tile(24, 17, Tile::Background(Rect::new(16 * 12, 16 * 9, 16, 16))); layer.set_tile(25, 17, Tile::Background(Rect::new(16 * 13, 16 * 9, 16, 16))); - layer.set_tile(28, 16, Tile::Floor(Rect::new(16 * 0, 16 * 8, 16, 16))); layer.set_tile(29, 16, Tile::Floor(Rect::new(16 * 1, 16 * 8, 16, 16))); layer.set_tile(28, 17, Tile::Floor(Rect::new(16 * 0, 16 * 9, 16, 16))); layer.set_tile(29, 17, Tile::Floor(Rect::new(16 * 1, 16 * 9, 16, 16))); - layer.set_tile(38, 15, Tile::Floor(Rect::new(16 * 0, 16 * 8, 16, 16))); layer.set_tile(39, 15, Tile::Floor(Rect::new(16 * 1, 16 * 8, 16, 16))); layer.set_tile(38, 16, Tile::Floor(Rect::new(16 * 0, 16 * 9, 16, 16))); @@ -236,13 +179,11 @@ fn main() { layer.set_tile(38, 17, Tile::Floor(Rect::new(16 * 0, 16 * 9, 16, 16))); layer.set_tile(39, 17, Tile::Floor(Rect::new(16 * 1, 16 * 9, 16, 16))); - layer.set_tile(41, 17, Tile::Background(Rect::new(16 * 11, 16 * 9, 16, 16))); layer.set_tile(42, 17, Tile::Background(Rect::new(16 * 12, 16 * 9, 16, 16))); layer.set_tile(43, 17, Tile::Background(Rect::new(16 * 12, 16 * 9, 16, 16))); layer.set_tile(44, 17, Tile::Background(Rect::new(16 * 13, 16 * 9, 16, 16))); - layer.set_tile(46, 14, Tile::Floor(Rect::new(16 * 0, 16 * 8, 16, 16))); layer.set_tile(47, 14, Tile::Floor(Rect::new(16 * 1, 16 * 8, 16, 16))); layer.set_tile(46, 15, Tile::Floor(Rect::new(16 * 0, 16 * 9, 16, 16))); @@ -252,7 +193,6 @@ fn main() { layer.set_tile(46, 17, Tile::Floor(Rect::new(16 * 0, 16 * 9, 16, 16))); layer.set_tile(47, 17, Tile::Floor(Rect::new(16 * 1, 16 * 9, 16, 16))); - layer.set_tile(50, 15, Tile::Background(Rect::new(16 * 9, 16 * 8, 16, 16))); layer.set_tile(49, 16, Tile::Background(Rect::new(16 * 8, 16 * 8, 16, 16))); @@ -265,13 +205,11 @@ fn main() { layer.set_tile(51, 17, Tile::Background(Rect::new(16 * 8, 16 * 9, 16, 16))); layer.set_tile(52, 17, Tile::Background(Rect::new(16 * 10, 16 * 8, 16, 16))); - for x in 0..212 { layer.set_tile(x, 18, Tile::Static(&floor_sprite, true)); layer.set_tile(x, 19, Tile::Static(&floor_sprite, true)); } - let mut camera = Camera::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, layer.to_rect()); let previous = timer.current_time(); diff --git a/src/sprite.rs b/src/sprite.rs index b764963..85ea9b8 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -3,12 +3,10 @@ use std::cell::RefCell; use sdl2::render::{Texture, Renderer}; use sdl2::rect::Rect; - pub trait Sprite { fn render(&self, f64, &mut Renderer, &Rect); } - pub struct StaticSprite<'a> { texture: &'a Texture, x: i32, @@ -35,7 +33,6 @@ impl<'a> Sprite for StaticSprite<'a> { } } - pub struct AnimatedSprite<'a> { texture: &'a Texture, x: i32,