forked from tutorials/rust-sokoban
Finish 2.2, moving the player
This commit is contained in:
parent
e94defda89
commit
8c37bacafb
79
src/main.rs
79
src/main.rs
@ -1,10 +1,13 @@
|
||||
use ggez;
|
||||
use ggez::event::{KeyCode, KeyMods};
|
||||
use ggez::graphics;
|
||||
use ggez::graphics::DrawParam;
|
||||
use ggez::graphics::Image;
|
||||
use ggez::nalgebra as na;
|
||||
use ggez::{conf, event, Context, GameResult};
|
||||
use specs::{
|
||||
join::Join, Builder, Component, ReadStorage, RunNow, System, VecStorage, World, WorldExt,
|
||||
join::Join, Builder, Component, Read, ReadStorage, RunNow, System, VecStorage, World, WorldExt,
|
||||
Write, WriteStorage,
|
||||
};
|
||||
|
||||
use std::path;
|
||||
@ -22,11 +25,7 @@ pub struct Position {
|
||||
|
||||
impl Position {
|
||||
pub fn new(x: u8, y: u8) -> Self {
|
||||
Position {
|
||||
x,
|
||||
y,
|
||||
z: 0,
|
||||
}
|
||||
Position { x, y, z: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,6 +51,12 @@ pub struct Box {}
|
||||
#[storage(VecStorage)]
|
||||
pub struct BoxSpot {}
|
||||
|
||||
// Resources
|
||||
#[derive(Default)]
|
||||
pub struct InputQueue {
|
||||
pub keys_pressed: Vec<KeyCode>,
|
||||
}
|
||||
|
||||
// Systems
|
||||
pub struct RenderingSystem<'a> {
|
||||
context: &'a mut Context,
|
||||
@ -90,6 +95,35 @@ impl<'a> System<'a> for RenderingSystem<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct InputSystem {}
|
||||
|
||||
impl<'a> System<'a> for InputSystem {
|
||||
// Data
|
||||
type SystemData = (
|
||||
Write<'a, InputQueue>,
|
||||
WriteStorage<'a, Position>,
|
||||
ReadStorage<'a, Player>,
|
||||
);
|
||||
|
||||
fn run(&mut self, data: Self::SystemData) {
|
||||
let (mut input_queue, mut positions, players) = data;
|
||||
|
||||
for (position, _player) in (&mut positions, &players).join() {
|
||||
// Get the first key pressed
|
||||
if let Some(key) = input_queue.keys_pressed.pop() {
|
||||
// Apply the key to the position
|
||||
match key {
|
||||
KeyCode::Up => position.y -= 1,
|
||||
KeyCode::Down => position.y += 1,
|
||||
KeyCode::Left => position.x -= 1,
|
||||
KeyCode::Right => position.x += 1,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// All the game state
|
||||
struct Game {
|
||||
world: World,
|
||||
@ -97,6 +131,12 @@ struct Game {
|
||||
|
||||
impl event::EventHandler for Game {
|
||||
fn update(&mut self, _context: &mut Context) -> GameResult {
|
||||
// Run input system
|
||||
{
|
||||
let mut is = InputSystem {};
|
||||
is.run_now(&self.world);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -109,6 +149,19 @@ impl event::EventHandler for Game {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn key_down_event(
|
||||
&mut self,
|
||||
_context: &mut Context,
|
||||
keycode: KeyCode,
|
||||
_keymod: KeyMods,
|
||||
_repeat: bool,
|
||||
) {
|
||||
println!("Key pressed: {:?}", keycode);
|
||||
|
||||
let mut input_queue = self.world.write_resource::<InputQueue>();
|
||||
input_queue.keys_pressed.push(keycode);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register_components(world: &mut World) {
|
||||
@ -120,6 +173,10 @@ pub fn register_components(world: &mut World) {
|
||||
world.register::<BoxSpot>();
|
||||
}
|
||||
|
||||
pub fn register_resources(world: &mut World) {
|
||||
world.insert(InputQueue::default())
|
||||
}
|
||||
|
||||
pub fn create_wall(world: &mut World, position: Position) {
|
||||
world
|
||||
.create_entity()
|
||||
@ -175,11 +232,7 @@ pub fn create_player(world: &mut World, position: Position) {
|
||||
|
||||
pub fn load_map(world: &mut World, map_string: String) {
|
||||
// read all lines
|
||||
let rows: Vec<&str> = map_string
|
||||
.trim()
|
||||
.split('\n')
|
||||
.map(|x| x.trim())
|
||||
.collect();
|
||||
let rows: Vec<&str> = map_string.trim().split('\n').map(|x| x.trim()).collect();
|
||||
|
||||
for (y, row) in rows.iter().enumerate() {
|
||||
let columns: Vec<&str> = row.split(' ').collect();
|
||||
@ -206,7 +259,7 @@ pub fn load_map(world: &mut World, map_string: String) {
|
||||
"S" => {
|
||||
create_floor(world, position);
|
||||
create_box_spot(world, position);
|
||||
},
|
||||
}
|
||||
"N" => (),
|
||||
c => panic!("unrecognized map item {}", c),
|
||||
}
|
||||
@ -230,10 +283,10 @@ pub fn initialize_level(world: &mut World) {
|
||||
load_map(world, MAP.to_string());
|
||||
}
|
||||
|
||||
|
||||
pub fn main() -> GameResult {
|
||||
let mut world = World::new();
|
||||
register_components(&mut world);
|
||||
register_resources(&mut world);
|
||||
initialize_level(&mut world);
|
||||
|
||||
// Create a game context and event loop
|
||||
|
Loading…
Reference in New Issue
Block a user