Complete Section 5.9

This commit is contained in:
Timothy Warren 2022-01-04 13:54:57 -05:00
parent 8824faff24
commit 0e9de911ce
2 changed files with 78 additions and 14 deletions

View File

@ -1,3 +1,5 @@
use std::cmp::Ordering;
use ::rltk::{Point, Rltk, VirtualKeyCode, RGB}; use ::rltk::{Point, Rltk, VirtualKeyCode, RGB};
use ::specs::prelude::*; 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"); let attr_gray = RGB::from_hex("#CCCCCC").expect("Oops");
ctx.print_color(50, y, attr_gray, black, name); ctx.print_color(50, y, attr_gray, black, name);
let color = if attribute.modifiers < 0 { let color = match attribute.modifiers.cmp(&0) {
RGB::from_f32(1.0, 0.0, 0.0) Ordering::Less => RGB::from_f32(1.0, 0.0, 0.0),
} else if attribute.modifiers == 0 { Ordering::Equal => RGB::named(rltk::WHITE),
RGB::named(rltk::WHITE) Ordering::Greater => RGB::from_f32(0.0, 1.0, 0.0),
} else {
RGB::from_f32(0.0, 1.0, 0.0)
}; };
ctx.print_color( ctx.print_color(
67, 67,
y, y,
@ -267,28 +268,28 @@ fn draw_tooltips(ecs: &World, ctx: &mut Rltk) {
let mut s = String::new(); let mut s = String::new();
if attr.might.bonus < 0 { if attr.might.bonus < 0 {
s += "Weak. " s += "Weak. "
} else if attr.might.bonus > 0 { }
if attr.might.bonus > 0 {
s += "String. " s += "String. "
} }
if attr.quickness.bonus < 0 { if attr.quickness.bonus < 0 {
s += "Clumsy. " s += "Clumsy. "
} else if attr.quickness.bonus > 0 { }
if attr.quickness.bonus > 0 {
s += "Agile. " s += "Agile. "
} }
if attr.fitness.bonus < 0 { if attr.fitness.bonus < 0 {
s += "Unhealthy. " s += "Unhealthy. "
} else if attr.fitness.bonus > 0 { }
if attr.fitness.bonus > 0 {
s += "Healthy. " s += "Healthy. "
} }
if attr.intelligence.bonus < 0 { if attr.intelligence.bonus < 0 {
s += "Unintelligent. " s += "Unintelligent. "
} else if attr.intelligence.bonus > 0 { }
if attr.intelligence.bonus > 0 {
s += "Smart. " s += "Smart. "
} }
if s.is_empty() { if s.is_empty() {
s = "Quite Average".to_string(); s = "Quite Average".to_string();
} }

View File

@ -193,7 +193,70 @@ fn skip_turn(ecs: &mut World) -> RunState {
RunState::PlayerTurn 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::<Consumable>();
let backpack = gs.ecs.read_storage::<InBackpack>();
let player_entity = gs.ecs.fetch::<Entity>();
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::<Ranged>()
.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::<WantsToUseItem>();
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 { pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
// Hotkeys
if ctx.shift && ctx.key.is_some() {
let key: Option<i32> = 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 // 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 // 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. // codes. The quick fix is to match on both types of number scan codes.