From 63ab324052f9d201c00e336391904dd5d4888da2 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 27 Jul 2020 16:19:32 -0400 Subject: [PATCH] Map out common key combinations to quit --- src/audio.rs | 4 ++-- src/main.rs | 24 +++++++++++++++++------- src/map.rs | 6 +++--- src/systems/event_system.rs | 17 ++++++++++------- src/systems/rendering_system.rs | 8 ++++---- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/audio.rs b/src/audio.rs index 36c51e5..360fa4d 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -1,5 +1,5 @@ -use ggez::{Context, audio}; use ggez::audio::SoundSource; +use ggez::{audio, Context}; use specs::{World, WorldExt}; use std::collections::HashMap; @@ -30,4 +30,4 @@ pub fn initialize_sounds(world: &mut World, context: &mut Context) { audio_store.sounds.insert(sound_name, sound_source); } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 4fe8749..9a848ca 100644 --- a/src/main.rs +++ b/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::(); - 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::(); + 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); } diff --git a/src/map.rs b/src/map.rs index cac5362..9e4b7ab 100644 --- a/src/map.rs +++ b/src/map.rs @@ -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), } } diff --git a/src/systems/event_system.rs b/src/systems/event_system.rs index c1689b6..da4bbb4 100644 --- a/src/systems/event_system.rs +++ b/src/systems/event_system.rs @@ -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), })); diff --git a/src/systems/rendering_system.rs b/src/systems/rendering_system.rs index d19a34f..a775a25 100644 --- a/src/systems/rendering_system.rs +++ b/src/systems/rendering_system.rs @@ -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);