forked from tutorials/rust-sokoban
Map out common key combinations to quit
This commit is contained in:
parent
86872aaaf9
commit
63ab324052
@ -1,5 +1,5 @@
|
||||
use ggez::{Context, audio};
|
||||
use ggez::audio::SoundSource;
|
||||
use ggez::{audio, Context};
|
||||
use specs::{World, WorldExt};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
24
src/main.rs
24
src/main.rs
@ -4,8 +4,8 @@ use ggez::event::KeyMods;
|
||||
use ggez::{conf, event, timer, Context, GameResult};
|
||||
use specs::{RunNow, World, WorldExt};
|
||||
|
||||
use std::io::prelude::*;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path;
|
||||
|
||||
mod audio;
|
||||
@ -69,13 +69,23 @@ impl event::EventHandler for Game {
|
||||
|
||||
fn key_down_event(
|
||||
&mut self,
|
||||
_context: &mut Context,
|
||||
context: &mut Context,
|
||||
keycode: KeyCode,
|
||||
_keymod: KeyMods,
|
||||
keymod: KeyMods,
|
||||
_repeat: bool,
|
||||
) {
|
||||
let mut input_queue = self.world.write_resource::<InputQueue>();
|
||||
input_queue.keys_pressed.push(keycode);
|
||||
// println!("Key pressed: {:?}, {:?}", keycode, _keymod);
|
||||
|
||||
match (keycode, keymod) {
|
||||
// Map common keys to quit
|
||||
(KeyCode::Escape, KeyMods::NONE)
|
||||
| (KeyCode::Q, KeyMods::CTRL)
|
||||
| (KeyCode::Q, KeyMods::LOGO) => ggez::event::quit(context),
|
||||
_ => {
|
||||
let mut input_queue = self.world.write_resource::<InputQueue>();
|
||||
input_queue.keys_pressed.push(keycode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,8 +94,8 @@ pub fn initialize_level(world: &mut World, level: u32) {
|
||||
|
||||
let mut file = File::open(&map_file).expect("Failed to open map file");
|
||||
let mut map = String::new();
|
||||
file.read_to_string(&mut map).expect("failed to read map file");
|
||||
|
||||
file.read_to_string(&mut map)
|
||||
.expect("failed to read map file");
|
||||
|
||||
load_map(world, map);
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ pub fn load_map(world: &mut World, map_string: String) {
|
||||
create_floor(world, position);
|
||||
create_box(world, position, BoxColor::Blue);
|
||||
}
|
||||
"RB" => {
|
||||
"+" => {
|
||||
create_floor(world, position);
|
||||
create_box(world, position, BoxColor::Red);
|
||||
}
|
||||
@ -36,11 +36,11 @@ pub fn load_map(world: &mut World, map_string: String) {
|
||||
create_floor(world, position);
|
||||
create_box_spot(world, position, BoxColor::Blue);
|
||||
}
|
||||
"RS" => {
|
||||
"=" => {
|
||||
create_floor(world, position);
|
||||
create_box_spot(world, position, BoxColor::Red);
|
||||
}
|
||||
"N" | " " => (),
|
||||
"-" => (),
|
||||
c => panic!("unrecognized map item {}", c),
|
||||
}
|
||||
}
|
||||
|
@ -28,26 +28,29 @@ impl<'a> System<'a> for EventSystem {
|
||||
let mut new_events = Vec::new();
|
||||
|
||||
for event in event_queue.events.drain(..) {
|
||||
println!("New event: {:?}", event);
|
||||
// println!("New event: {:?}", event);
|
||||
|
||||
match event {
|
||||
Event::PlayerHistObstacle => {
|
||||
// play sound here
|
||||
// play sound here
|
||||
audio_store.play_sound(&"wall".to_string());
|
||||
}
|
||||
Event::EntityMoved(EntityMoved { id }) => {
|
||||
// An entity was just moved, check if it was a box and fire
|
||||
// more events if it's been moved on a spot.
|
||||
if let Some(the_box) = boxes.get(entities.entity(id)) {
|
||||
let box_spots_with_positions: HashMap<(u8, u8), &BoxSpot> = (&box_spots, &positions)
|
||||
.join()
|
||||
.map(|t| ((t.1.x, t.1.y), t.0))
|
||||
.collect();
|
||||
let box_spots_with_positions: HashMap<(u8, u8), &BoxSpot> =
|
||||
(&box_spots, &positions)
|
||||
.join()
|
||||
.map(|t| ((t.1.x, t.1.y), t.0))
|
||||
.collect();
|
||||
|
||||
if let Some(box_position) = positions.get(entities.entity(id)) {
|
||||
// Check if there is a spot on this position, and if there
|
||||
// is, if it's the correct or incorrect type
|
||||
if let Some(box_spot) = box_spots_with_positions.get(&(box_position.x, box_position.y)) {
|
||||
if let Some(box_spot) =
|
||||
box_spots_with_positions.get(&(box_position.x, box_position.y))
|
||||
{
|
||||
new_events.push(Event::BoxPlacedOnSpot(BoxPlacedOnSpot {
|
||||
is_correct_spot: (box_spot.color == the_box.color),
|
||||
}));
|
||||
|
@ -1,8 +1,8 @@
|
||||
use ggez::graphics::spritebatch::SpriteBatch;
|
||||
use ggez::graphics::Image;
|
||||
use ggez::graphics::{Color, DrawParam};
|
||||
use ggez::{nalgebra as na, timer};
|
||||
use ggez::{graphics, Context};
|
||||
use ggez::graphics::spritebatch::SpriteBatch;
|
||||
use ggez::{nalgebra as na, timer};
|
||||
use itertools::Itertools;
|
||||
use specs::{Join, Read, ReadStorage, System};
|
||||
|
||||
@ -94,8 +94,8 @@ impl<'a> System<'a> for RenderingSystem<'a> {
|
||||
// Iterate spritebatches ordered by z and actually render each of them
|
||||
for (_z, group) in rendering_batches
|
||||
.iter()
|
||||
.sorted_by(|a, b| Ord::cmp(&a.0, &b.0)) {
|
||||
|
||||
.sorted_by(|a, b| Ord::cmp(&a.0, &b.0))
|
||||
{
|
||||
for (image_path, draw_params) in group {
|
||||
let image = Image::new(self.context, image_path).expect("expected image");
|
||||
let mut sprite_batch = SpriteBatch::new(image);
|
||||
|
Loading…
Reference in New Issue
Block a user