Finish 2.2, moving the player

This commit is contained in:
Timothy Warren 2020-07-23 15:42:35 -04:00
parent e94defda89
commit 8c37bacafb

View File

@ -1,10 +1,13 @@
use ggez;
use ggez::event::{KeyCode, KeyMods};
use ggez::graphics; use ggez::graphics;
use ggez::graphics::DrawParam; use ggez::graphics::DrawParam;
use ggez::graphics::Image; use ggez::graphics::Image;
use ggez::nalgebra as na; use ggez::nalgebra as na;
use ggez::{conf, event, Context, GameResult}; use ggez::{conf, event, Context, GameResult};
use specs::{ 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; use std::path;
@ -22,11 +25,7 @@ pub struct Position {
impl Position { impl Position {
pub fn new(x: u8, y: u8) -> Self { pub fn new(x: u8, y: u8) -> Self {
Position { Position { x, y, z: 0 }
x,
y,
z: 0,
}
} }
} }
@ -52,6 +51,12 @@ pub struct Box {}
#[storage(VecStorage)] #[storage(VecStorage)]
pub struct BoxSpot {} pub struct BoxSpot {}
// Resources
#[derive(Default)]
pub struct InputQueue {
pub keys_pressed: Vec<KeyCode>,
}
// Systems // Systems
pub struct RenderingSystem<'a> { pub struct RenderingSystem<'a> {
context: &'a mut Context, 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 // All the game state
struct Game { struct Game {
world: World, world: World,
@ -97,6 +131,12 @@ struct Game {
impl event::EventHandler for Game { impl event::EventHandler for Game {
fn update(&mut self, _context: &mut Context) -> GameResult { fn update(&mut self, _context: &mut Context) -> GameResult {
// Run input system
{
let mut is = InputSystem {};
is.run_now(&self.world);
}
Ok(()) Ok(())
} }
@ -109,6 +149,19 @@ impl event::EventHandler for Game {
Ok(()) 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) { pub fn register_components(world: &mut World) {
@ -120,6 +173,10 @@ pub fn register_components(world: &mut World) {
world.register::<BoxSpot>(); world.register::<BoxSpot>();
} }
pub fn register_resources(world: &mut World) {
world.insert(InputQueue::default())
}
pub fn create_wall(world: &mut World, position: Position) { pub fn create_wall(world: &mut World, position: Position) {
world world
.create_entity() .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) { pub fn load_map(world: &mut World, map_string: String) {
// read all lines // read all lines
let rows: Vec<&str> = map_string let rows: Vec<&str> = map_string.trim().split('\n').map(|x| x.trim()).collect();
.trim()
.split('\n')
.map(|x| x.trim())
.collect();
for (y, row) in rows.iter().enumerate() { for (y, row) in rows.iter().enumerate() {
let columns: Vec<&str> = row.split(' ').collect(); let columns: Vec<&str> = row.split(' ').collect();
@ -206,7 +259,7 @@ pub fn load_map(world: &mut World, map_string: String) {
"S" => { "S" => {
create_floor(world, position); create_floor(world, position);
create_box_spot(world, position); create_box_spot(world, position);
}, }
"N" => (), "N" => (),
c => panic!("unrecognized map item {}", c), c => panic!("unrecognized map item {}", c),
} }
@ -230,10 +283,10 @@ pub fn initialize_level(world: &mut World) {
load_map(world, MAP.to_string()); load_map(world, MAP.to_string());
} }
pub fn main() -> GameResult { pub fn main() -> GameResult {
let mut world = World::new(); let mut world = World::new();
register_components(&mut world); register_components(&mut world);
register_resources(&mut world);
initialize_level(&mut world); initialize_level(&mut world);
// Create a game context and event loop // Create a game context and event loop