Map out common key combinations to quit

This commit is contained in:
Timothy Warren 2020-07-27 16:19:32 -04:00
parent 86872aaaf9
commit 63ab324052
5 changed files with 36 additions and 23 deletions

View File

@ -1,5 +1,5 @@
use ggez::{Context, audio};
use ggez::audio::SoundSource; use ggez::audio::SoundSource;
use ggez::{audio, Context};
use specs::{World, WorldExt}; use specs::{World, WorldExt};
use std::collections::HashMap; use std::collections::HashMap;

View File

@ -4,8 +4,8 @@ use ggez::event::KeyMods;
use ggez::{conf, event, timer, Context, GameResult}; use ggez::{conf, event, timer, Context, GameResult};
use specs::{RunNow, World, WorldExt}; use specs::{RunNow, World, WorldExt};
use std::io::prelude::*;
use std::fs::File; use std::fs::File;
use std::io::prelude::*;
use std::path; use std::path;
mod audio; mod audio;
@ -69,14 +69,24 @@ impl event::EventHandler for Game {
fn key_down_event( fn key_down_event(
&mut self, &mut self,
_context: &mut Context, context: &mut Context,
keycode: KeyCode, keycode: KeyCode,
_keymod: KeyMods, keymod: KeyMods,
_repeat: bool, _repeat: bool,
) { ) {
// 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>(); let mut input_queue = self.world.write_resource::<InputQueue>();
input_queue.keys_pressed.push(keycode); input_queue.keys_pressed.push(keycode);
} }
}
}
} }
pub fn initialize_level(world: &mut World, level: u32) { pub fn initialize_level(world: &mut World, level: u32) {
@ -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 file = File::open(&map_file).expect("Failed to open map file");
let mut map = String::new(); 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); load_map(world, map);
} }

View File

@ -28,7 +28,7 @@ pub fn load_map(world: &mut World, map_string: String) {
create_floor(world, position); create_floor(world, position);
create_box(world, position, BoxColor::Blue); create_box(world, position, BoxColor::Blue);
} }
"RB" => { "+" => {
create_floor(world, position); create_floor(world, position);
create_box(world, position, BoxColor::Red); 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_floor(world, position);
create_box_spot(world, position, BoxColor::Blue); create_box_spot(world, position, BoxColor::Blue);
} }
"RS" => { "=" => {
create_floor(world, position); create_floor(world, position);
create_box_spot(world, position, BoxColor::Red); create_box_spot(world, position, BoxColor::Red);
} }
"N" | " " => (), "-" => (),
c => panic!("unrecognized map item {}", c), c => panic!("unrecognized map item {}", c),
} }
} }

View File

@ -28,7 +28,7 @@ impl<'a> System<'a> for EventSystem {
let mut new_events = Vec::new(); let mut new_events = Vec::new();
for event in event_queue.events.drain(..) { for event in event_queue.events.drain(..) {
println!("New event: {:?}", event); // println!("New event: {:?}", event);
match event { match event {
Event::PlayerHistObstacle => { Event::PlayerHistObstacle => {
@ -39,7 +39,8 @@ impl<'a> System<'a> for EventSystem {
// An entity was just moved, check if it was a box and fire // An entity was just moved, check if it was a box and fire
// more events if it's been moved on a spot. // more events if it's been moved on a spot.
if let Some(the_box) = boxes.get(entities.entity(id)) { if let Some(the_box) = boxes.get(entities.entity(id)) {
let box_spots_with_positions: HashMap<(u8, u8), &BoxSpot> = (&box_spots, &positions) let box_spots_with_positions: HashMap<(u8, u8), &BoxSpot> =
(&box_spots, &positions)
.join() .join()
.map(|t| ((t.1.x, t.1.y), t.0)) .map(|t| ((t.1.x, t.1.y), t.0))
.collect(); .collect();
@ -47,7 +48,9 @@ impl<'a> System<'a> for EventSystem {
if let Some(box_position) = positions.get(entities.entity(id)) { if let Some(box_position) = positions.get(entities.entity(id)) {
// Check if there is a spot on this position, and if there // Check if there is a spot on this position, and if there
// is, if it's the correct or incorrect type // 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 { new_events.push(Event::BoxPlacedOnSpot(BoxPlacedOnSpot {
is_correct_spot: (box_spot.color == the_box.color), is_correct_spot: (box_spot.color == the_box.color),
})); }));

View File

@ -1,8 +1,8 @@
use ggez::graphics::spritebatch::SpriteBatch;
use ggez::graphics::Image; use ggez::graphics::Image;
use ggez::graphics::{Color, DrawParam}; use ggez::graphics::{Color, DrawParam};
use ggez::{nalgebra as na, timer};
use ggez::{graphics, Context}; use ggez::{graphics, Context};
use ggez::graphics::spritebatch::SpriteBatch; use ggez::{nalgebra as na, timer};
use itertools::Itertools; use itertools::Itertools;
use specs::{Join, Read, ReadStorage, System}; 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 // Iterate spritebatches ordered by z and actually render each of them
for (_z, group) in rendering_batches for (_z, group) in rendering_batches
.iter() .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 { for (image_path, draw_params) in group {
let image = Image::new(self.context, image_path).expect("expected image"); let image = Image::new(self.context, image_path).expect("expected image");
let mut sprite_batch = SpriteBatch::new(image); let mut sprite_batch = SpriteBatch::new(image);