Add Sprite to GameObject.
This commit is contained in:
31
src/main.rs
31
src/main.rs
@@ -10,7 +10,7 @@ use sdl2_image::LoadTexture;
|
|||||||
use sdl2::rect::Rect;
|
use sdl2::rect::Rect;
|
||||||
use sdl2::keyboard::Keycode;
|
use sdl2::keyboard::Keycode;
|
||||||
use sdl2::pixels::Color;
|
use sdl2::pixels::Color;
|
||||||
use sdl2::render::Renderer;
|
use sdl2::render::{Renderer, Texture};
|
||||||
|
|
||||||
|
|
||||||
use tile::Layer;
|
use tile::Layer;
|
||||||
@@ -44,12 +44,16 @@ const PLAYER_ACCELERATION_X_CHANGE : f32 = 0.06;
|
|||||||
|
|
||||||
|
|
||||||
struct GameObject {
|
struct GameObject {
|
||||||
|
x: f32,
|
||||||
|
y: f32,
|
||||||
graphics: Box<GraphicsComponent + 'static>
|
graphics: Box<GraphicsComponent + 'static>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GameObject {
|
impl GameObject {
|
||||||
pub fn new(graphics: Box<GraphicsComponent + 'static>) -> GameObject {
|
pub fn new(x: f32, y: f32, graphics: Box<GraphicsComponent + 'static>) -> GameObject {
|
||||||
GameObject {
|
GameObject {
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
graphics: graphics
|
graphics: graphics
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,21 +69,19 @@ trait GraphicsComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct PlayerGraphicsComponent {
|
struct PlayerGraphicsComponent<'a> {
|
||||||
x: f32,
|
sprite_running: Box<Sprite + 'a>
|
||||||
y: f32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PlayerGraphicsComponent {
|
impl<'a> PlayerGraphicsComponent<'a> {
|
||||||
pub fn new(x: f32, y: f32) -> PlayerGraphicsComponent {
|
pub fn new(texture: &'a Texture) -> PlayerGraphicsComponent<'a> {
|
||||||
PlayerGraphicsComponent {
|
PlayerGraphicsComponent {
|
||||||
x: x,
|
sprite_running: Box::new(AnimatedSprite::new(&texture, 96, 32, 3, 15))
|
||||||
y: y
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GraphicsComponent for PlayerGraphicsComponent {
|
impl<'a> GraphicsComponent for PlayerGraphicsComponent<'a> {
|
||||||
fn update(&self, object: &GameObject, renderer: &Renderer) {
|
fn update(&self, object: &GameObject, renderer: &Renderer) {
|
||||||
println!("epic shit!");
|
println!("epic shit!");
|
||||||
}
|
}
|
||||||
@@ -90,7 +92,6 @@ impl GraphicsComponent for PlayerGraphicsComponent {
|
|||||||
enum Tile<'a> {
|
enum Tile<'a> {
|
||||||
Empty,
|
Empty,
|
||||||
Static(&'a StaticSprite<'a>, bool),
|
Static(&'a StaticSprite<'a>, bool),
|
||||||
|
|
||||||
Background(Rect),
|
Background(Rect),
|
||||||
Floor(Rect)
|
Floor(Rect)
|
||||||
}
|
}
|
||||||
@@ -105,11 +106,6 @@ fn main() {
|
|||||||
let window = video_subsystem.window("Super Matte Bros", SCREEN_WIDTH, SCREEN_HEIGHT).position_centered().build().unwrap();
|
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 renderer = window.renderer().software().build().unwrap();
|
||||||
|
|
||||||
|
|
||||||
//let _player = GameObject::new(Box::new(PlayerGraphicsComponent::new(390.0, 390.0)));
|
|
||||||
//_player.update(&renderer);
|
|
||||||
|
|
||||||
|
|
||||||
let world_sprites = renderer.load_texture(&Path::new("gfx/world.png")).unwrap();
|
let world_sprites = renderer.load_texture(&Path::new("gfx/world.png")).unwrap();
|
||||||
|
|
||||||
let floor_sprite = StaticSprite::new(&world_sprites, 16 * 0, 16 * 0);
|
let floor_sprite = StaticSprite::new(&world_sprites, 16 * 0, 16 * 0);
|
||||||
@@ -117,6 +113,9 @@ fn main() {
|
|||||||
|
|
||||||
let player_sprites = renderer.load_texture(&Path::new("gfx/mario.png")).unwrap();
|
let player_sprites = renderer.load_texture(&Path::new("gfx/mario.png")).unwrap();
|
||||||
|
|
||||||
|
//let _player = GameObject::new(390.0, 390.0, Box::new(PlayerGraphicsComponent::new(&player_sprites)));
|
||||||
|
//_player.update(&renderer);
|
||||||
|
|
||||||
let mut player_sprite = AnimatedSprite::new(&player_sprites, 96, 32, 3, 15);
|
let mut player_sprite = AnimatedSprite::new(&player_sprites, 96, 32, 3, 15);
|
||||||
|
|
||||||
let mut keyboard = KeyboardHandler::new();
|
let mut keyboard = KeyboardHandler::new();
|
||||||
|
|||||||
Reference in New Issue
Block a user