From 0e9de911cee7477febb436e2069264e9f0e52686 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 4 Jan 2022 13:54:57 -0500 Subject: [PATCH] Complete Section 5.9 --- src/gui.rs | 29 ++++++++++++------------ src/player.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 14 deletions(-) diff --git a/src/gui.rs b/src/gui.rs index 9648f06..e622a35 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,3 +1,5 @@ +use std::cmp::Ordering; + use ::rltk::{Point, Rltk, VirtualKeyCode, RGB}; use ::specs::prelude::*; @@ -164,13 +166,12 @@ fn draw_attribute(name: &str, attribute: &Attribute, y: i32, ctx: &mut Rltk) { let attr_gray = RGB::from_hex("#CCCCCC").expect("Oops"); ctx.print_color(50, y, attr_gray, black, name); - let color = if attribute.modifiers < 0 { - RGB::from_f32(1.0, 0.0, 0.0) - } else if attribute.modifiers == 0 { - RGB::named(rltk::WHITE) - } else { - RGB::from_f32(0.0, 1.0, 0.0) + let color = match attribute.modifiers.cmp(&0) { + Ordering::Less => RGB::from_f32(1.0, 0.0, 0.0), + Ordering::Equal => RGB::named(rltk::WHITE), + Ordering::Greater => RGB::from_f32(0.0, 1.0, 0.0), }; + ctx.print_color( 67, y, @@ -267,28 +268,28 @@ fn draw_tooltips(ecs: &World, ctx: &mut Rltk) { let mut s = String::new(); if attr.might.bonus < 0 { s += "Weak. " - } else if attr.might.bonus > 0 { + } + if attr.might.bonus > 0 { s += "String. " } - if attr.quickness.bonus < 0 { s += "Clumsy. " - } else if attr.quickness.bonus > 0 { + } + if attr.quickness.bonus > 0 { s += "Agile. " } - if attr.fitness.bonus < 0 { s += "Unhealthy. " - } else if attr.fitness.bonus > 0 { + } + if attr.fitness.bonus > 0 { s += "Healthy. " } - if attr.intelligence.bonus < 0 { s += "Unintelligent. " - } else if attr.intelligence.bonus > 0 { + } + if attr.intelligence.bonus > 0 { s += "Smart. " } - if s.is_empty() { s = "Quite Average".to_string(); } diff --git a/src/player.rs b/src/player.rs index 21c2049..539ebfa 100644 --- a/src/player.rs +++ b/src/player.rs @@ -193,7 +193,70 @@ fn skip_turn(ecs: &mut World) -> RunState { RunState::PlayerTurn } +fn use_consumable_hotkey(gs: &mut State, key: i32) -> RunState { + use crate::components::{Consumable, InBackpack, Ranged, WantsToUseItem}; + + let consumables = gs.ecs.read_storage::(); + let backpack = gs.ecs.read_storage::(); + let player_entity = gs.ecs.fetch::(); + let entities = gs.ecs.entities(); + let mut carried_consumables = Vec::new(); + + for (entity, carried_by, _consumable) in (&entities, &backpack, &consumables).join() { + if carried_by.owner == *player_entity { + carried_consumables.push(entity); + } + } + + if (key as usize) < carried_consumables.len() { + if let Some(ranged) = gs + .ecs + .read_storage::() + .get(carried_consumables[key as usize]) + { + return RunState::ShowTargeting { + range: ranged.range, + item: carried_consumables[key as usize], + }; + } + + let mut intent = gs.ecs.write_storage::(); + intent + .insert( + *player_entity, + WantsToUseItem { + item: carried_consumables[key as usize], + target: None, + }, + ) + .expect("Unable to insert intent to use item."); + + return RunState::PlayerTurn; + } + + RunState::PlayerTurn +} + pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState { + // Hotkeys + if ctx.shift && ctx.key.is_some() { + let key: Option = match ctx.key.unwrap() { + VirtualKeyCode::Key1 => Some(1), + VirtualKeyCode::Key2 => Some(2), + VirtualKeyCode::Key3 => Some(3), + VirtualKeyCode::Key4 => Some(4), + VirtualKeyCode::Key5 => Some(5), + VirtualKeyCode::Key6 => Some(6), + VirtualKeyCode::Key7 => Some(7), + VirtualKeyCode::Key8 => Some(8), + VirtualKeyCode::Key9 => Some(9), + _ => None, + }; + if let Some(key) = key { + return use_consumable_hotkey(gs, key - 1); + } + } + // Mac OS is special when it comes to the numpad. Instead of reporting // the keys as Numpad-specific numbers, it reports the number row scan // codes. The quick fix is to match on both types of number scan codes.