Add flashing message intents

This commit is contained in:
Alex Page 2022-02-03 17:57:30 -05:00
parent def124cd71
commit 0e5e823b0b
3 changed files with 50 additions and 9 deletions

View file

@ -12,7 +12,7 @@ pub fn draw(resources: &mut Resources, bterm: &mut BTerm) {
MAP_Y + MAP_HEIGHT,
RGB::named(vga::get_by_index(flashing_message.color)),
RGB::named(vga::BLACK),
format!(" {} ", &flashing_message.message),
flashing_message.message.to_string(),
);
}
}

View file

@ -1,13 +1,33 @@
use bracket_lib::prelude::*;
use hecs::World;
use crate::resources::*;
use crate::resources::{flashing_message::FlashingMessageIntent, *};
mod player;
pub fn handle_intent(bterm: &mut BTerm, world: &mut World, resources: &mut Resources) {
if let Some(key) = bterm.key {
if let Some(flashing_message) = &resources.flashing_message {
if let Some(intent) = &flashing_message.intent {
match intent {
FlashingMessageIntent::Quit => {
if key == VirtualKeyCode::Y {
bterm.quit()
}
}
FlashingMessageIntent::Save => todo!(),
FlashingMessageIntent::Restore => todo!(),
FlashingMessageIntent::Restart => todo!(),
}
}
}
}
}
pub fn handle(world: &mut World, resources: &mut Resources, bterm: &mut BTerm) {
if resources.flashing_message.is_some() {
if bterm.key.is_some() {
handle_intent(bterm, world, resources);
resources.flashing_message = None;
}
} else {
@ -45,11 +65,14 @@ pub fn handle(world: &mut World, resources: &mut Resources, bterm: &mut BTerm) {
resources.show_debug_info = !resources.show_debug_info;
}
VirtualKeyCode::Escape | VirtualKeyCode::Q => {
bterm.quit();
resources.flashing_message = Some(FlashingMessage::new(
" Are you sure you want to quit (Y/N)? ",
Some(FlashingMessageIntent::Quit),
));
}
VirtualKeyCode::P => {
resources.flashing_message =
Some(FlashingMessage::from("Press any key to resume game."));
Some(FlashingMessage::from(" Press any key to resume game. "));
}
_ => {
resources

View file

@ -1,14 +1,36 @@
use std::time::{Duration, Instant};
use bracket_lib::prelude::{BTerm, VirtualKeyCode};
use hecs::World;
use crate::constants::*;
use super::Resources;
pub enum FlashingMessageIntent {
Quit,
Save,
Restore,
Restart,
}
pub struct FlashingMessage {
pub message: String,
pub color: usize,
pub last_changed_color: Instant,
pub intent: Option<FlashingMessageIntent>,
}
impl FlashingMessage {
pub fn new(message: &str, intent: Option<FlashingMessageIntent>) -> Self {
Self {
message: message.to_string(),
color: 14,
last_changed_color: Instant::now(),
intent,
}
}
pub fn next_color(&mut self) {
let now = Instant::now();
if now - self.last_changed_color > Duration::from_secs_f32(FLASHING_PERIOD) {
@ -23,10 +45,6 @@ impl FlashingMessage {
impl From<&str> for FlashingMessage {
fn from(message: &str) -> Self {
Self {
message: message.to_string(),
color: 14,
last_changed_color: Instant::now(),
}
Self::new(message, None)
}
}