|
|
@@ -16,7 +16,10 @@ use std::time::Duration; |
|
|
|
|
|
|
|
use crate::components::*; |
|
|
|
|
|
|
|
const PLAYER_MOVEMENT_SPEED: i32 = 20; |
|
|
|
pub enum MovementCommand { |
|
|
|
Stop, |
|
|
|
Move(Direction), |
|
|
|
} |
|
|
|
|
|
|
|
fn direction_spritesheet_row(direction: Direction) -> i32 { |
|
|
|
use self::Direction::*; |
|
|
@@ -112,6 +115,18 @@ fn main() -> Result<(), String> { |
|
|
|
|
|
|
|
let texture_creator = canvas.texture_creator(); |
|
|
|
|
|
|
|
let mut dispatcher = DispatcherBuilder::new() |
|
|
|
.with(keyboard::Keyboard, "Keyboard", &[]) |
|
|
|
.with(physics::Physics, "Physics", &["Keyboard"]) |
|
|
|
.with(animator::Animator, "Animator", &["Keyboard"]) |
|
|
|
.build(); |
|
|
|
|
|
|
|
let mut world = World::new(); |
|
|
|
dispatcher.setup(&mut world.res); |
|
|
|
|
|
|
|
let movement_command: Option<MovementCommand> = None; |
|
|
|
world.add_resource(movement_command); |
|
|
|
|
|
|
|
let textures = [ |
|
|
|
texture_creator.load_texture("assets/bardo.png")?, |
|
|
|
]; |
|
|
@@ -127,9 +142,9 @@ fn main() -> Result<(), String> { |
|
|
|
right_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Right), |
|
|
|
}; |
|
|
|
|
|
|
|
let mut world = World::new(); |
|
|
|
|
|
|
|
world.create_entity() |
|
|
|
.with(KeyboardControlled) |
|
|
|
.with(Position(Point::new(0, 0))) |
|
|
|
.with(Velocity {speed: 0, direction: Direction::Right}) |
|
|
|
.with(player_animation.right_frames[0].clone()) |
|
|
@@ -139,6 +154,7 @@ fn main() -> Result<(), String> { |
|
|
|
let mut event_pump = sdl_context.event_pump()?; |
|
|
|
let mut i = 0; |
|
|
|
'running: loop { |
|
|
|
let mut movement_command = None; |
|
|
|
for event in event_pump.poll_iter() { |
|
|
|
match event { |
|
|
|
Event::Quit {..} | |
|
|
@@ -146,33 +162,32 @@ fn main() -> Result<(), String> { |
|
|
|
break 'running; |
|
|
|
}, |
|
|
|
Event::KeyDown { keycode: Some(Keycode::Left), repeat: false, .. } => { |
|
|
|
player.speed = PLAYER_MOVEMENT_SPEED; |
|
|
|
player.direction = Direction::Left; |
|
|
|
movement_command = Some(MovementCommand::Move(Direction::Left)); |
|
|
|
}, |
|
|
|
Event::KeyDown { keycode: Some(Keycode::Right), repeat: false, .. } => { |
|
|
|
player.speed = PLAYER_MOVEMENT_SPEED; |
|
|
|
player.direction = Direction::Right; |
|
|
|
movement_command = Some(MovementCommand::Move(Direction::Right)); |
|
|
|
}, |
|
|
|
Event::KeyDown { keycode: Some(Keycode::Up), repeat: false, .. } => { |
|
|
|
player.speed = PLAYER_MOVEMENT_SPEED; |
|
|
|
player.direction = Direction::Up; |
|
|
|
movement_command = Some(MovementCommand::Move(Direction::Up)); |
|
|
|
}, |
|
|
|
Event::KeyDown { keycode: Some(Keycode::Down), repeat: false, .. } => { |
|
|
|
player.speed = PLAYER_MOVEMENT_SPEED; |
|
|
|
player.direction = Direction::Down; |
|
|
|
movement_command = Some(MovementCommand::Move(Direction::Down)); |
|
|
|
}, |
|
|
|
Event::KeyUp { keycode: Some(Keycode::Left), repeat: false, .. } | |
|
|
|
Event::KeyUp { keycode: Some(Keycode::Right), repeat: false, .. } | |
|
|
|
Event::KeyUp { keycode: Some(Keycode::Up), repeat: false, .. } | |
|
|
|
Event::KeyUp { keycode: Some(Keycode::Down), repeat: false, .. } => { |
|
|
|
player.speed = 0; |
|
|
|
movement_command = Some(MovementCommand::Stop); |
|
|
|
}, |
|
|
|
_ => {} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
*world.write_resource() = movement_command; |
|
|
|
|
|
|
|
i = (i + 1) % 255; |
|
|
|
// TODO: Do with specs! |
|
|
|
dispatcher.dispatch(&mut world.res); |
|
|
|
world.maintain(); |
|
|
|
|
|
|
|
render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, &player)?; |
|
|
|
|