Browse Source

Factor out keyboard code

master
Noelle 4 years ago
parent
commit
68bee6490d
3 changed files with 65 additions and 12 deletions
  1. 4
    0
      src/components.rs
  2. 34
    0
      src/keyboard.rs
  3. 27
    12
      src/main.rs

+ 4
- 0
src/components.rs View File

Right, Right,
} }


#[derive(Component, Debug, Default)]
#[storage(NullStorage)]
pub struct KeyboardControlled;

#[derive(Component, Debug)] #[derive(Component, Debug)]
#[storage(VecStorage)] #[storage(VecStorage)]
pub struct Position(pub Point); pub struct Position(pub Point);

+ 34
- 0
src/keyboard.rs View File

use specs::prelude::*;

use crate::components::*;

use super::MovementCommand;

const PLAYER_MOVEMENT_SPEED: i32 = 20;

pub struct Keyboard;

impl<'a> System<'a> for Keyboard {
type SystemData = (
ReadExpect<'a, Option<MovementCommand>>,
ReadStorage<'a, KeyboardControlled>,
WriteStorage<'a, Velocity>,
);

fn run(&mut self, mut data: Self::SystemData) {
let movement_command = match &*data.0 {
Some(movement_command) => movement_command,
None => return,
};

for (_, vel) in (&data.1, &mut data.2).join() {
match movement_command {
&MovementCommand::Move(direction) => {
vel.speed = PLAYER_MOVEMENT_SPEED;
vel.direction = direction;
},
MovementCommand::Stop => vel.speed = 0,
}
}
}
}

+ 27
- 12
src/main.rs View File



use crate::components::*; use crate::components::*;


const PLAYER_MOVEMENT_SPEED: i32 = 20;
pub enum MovementCommand {
Stop,
Move(Direction),
}


fn direction_spritesheet_row(direction: Direction) -> i32 { fn direction_spritesheet_row(direction: Direction) -> i32 {
use self::Direction::*; use self::Direction::*;


let texture_creator = canvas.texture_creator(); 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 = [ let textures = [
texture_creator.load_texture("assets/bardo.png")?, texture_creator.load_texture("assets/bardo.png")?,
]; ];
right_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Right), right_frames: character_animation_frames(player_spritesheet, player_top_left_frame, Direction::Right),
}; };


let mut world = World::new();


world.create_entity() world.create_entity()
.with(KeyboardControlled)
.with(Position(Point::new(0, 0))) .with(Position(Point::new(0, 0)))
.with(Velocity {speed: 0, direction: Direction::Right}) .with(Velocity {speed: 0, direction: Direction::Right})
.with(player_animation.right_frames[0].clone()) .with(player_animation.right_frames[0].clone())
let mut event_pump = sdl_context.event_pump()?; let mut event_pump = sdl_context.event_pump()?;
let mut i = 0; let mut i = 0;
'running: loop { 'running: loop {
let mut movement_command = None;
for event in event_pump.poll_iter() { for event in event_pump.poll_iter() {
match event { match event {
Event::Quit {..} | Event::Quit {..} |
break 'running; break 'running;
}, },
Event::KeyDown { keycode: Some(Keycode::Left), repeat: false, .. } => { 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, .. } => { 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, .. } => { 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, .. } => { 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::Left), repeat: false, .. } |
Event::KeyUp { keycode: Some(Keycode::Right), repeat: false, .. } | Event::KeyUp { keycode: Some(Keycode::Right), repeat: false, .. } |
Event::KeyUp { keycode: Some(Keycode::Up), repeat: false, .. } | Event::KeyUp { keycode: Some(Keycode::Up), repeat: false, .. } |
Event::KeyUp { keycode: Some(Keycode::Down), 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; 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)?; render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, &player)?;



Loading…
Cancel
Save