Pārlūkot izejas kodu

Factor out keyboard code

master
Noelle pirms 4 gadiem
vecāks
revīzija
68bee6490d
3 mainītis faili ar 65 papildinājumiem un 12 dzēšanām
  1. 4
    0
      src/components.rs
  2. 34
    0
      src/keyboard.rs
  3. 27
    12
      src/main.rs

+ 4
- 0
src/components.rs Parādīt failu

@@ -10,6 +10,10 @@ pub enum Direction {
Right,
}

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

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

+ 34
- 0
src/keyboard.rs Parādīt failu

@@ -0,0 +1,34 @@
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 Parādīt failu

@@ -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)?;


Notiek ielāde…
Atcelt
Saglabāt