Add support for bottom flashing messages
This commit is contained in:
parent
ecaf448e90
commit
def124cd71
9 changed files with 112 additions and 39 deletions
|
@ -2,6 +2,7 @@
|
||||||
/// Fast PC mode at 400 cycles/ms.
|
/// Fast PC mode at 400 cycles/ms.
|
||||||
pub const CLOCK_PERIOD: f32 = 0.184;
|
pub const CLOCK_PERIOD: f32 = 0.184;
|
||||||
pub const PLAYER_STEP_PERIOD: f32 = 1.0 / 7.5;
|
pub const PLAYER_STEP_PERIOD: f32 = 1.0 / 7.5;
|
||||||
|
pub const FLASHING_PERIOD: f32 = 0.02;
|
||||||
|
|
||||||
pub const SIDEBAR_POS_X: i32 = 66;
|
pub const SIDEBAR_POS_X: i32 = 66;
|
||||||
pub const SIDEBAR_POS_Y: i32 = 0;
|
pub const SIDEBAR_POS_Y: i32 = 0;
|
||||||
|
|
18
src/graphics/flashing_message.rs
Normal file
18
src/graphics/flashing_message.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
use crate::constants::*;
|
||||||
|
use crate::graphics::vga_color as vga;
|
||||||
|
use crate::resources::Resources;
|
||||||
|
use bracket_lib::prelude::*;
|
||||||
|
|
||||||
|
pub fn draw(resources: &mut Resources, bterm: &mut BTerm) {
|
||||||
|
if let Some(flashing_message) = &mut resources.flashing_message {
|
||||||
|
flashing_message.next_color();
|
||||||
|
|
||||||
|
bterm.print_color_centered_at(
|
||||||
|
MAP_X + (MAP_WIDTH / 2) - 1,
|
||||||
|
MAP_Y + MAP_HEIGHT,
|
||||||
|
RGB::named(vga::get_by_index(flashing_message.color)),
|
||||||
|
RGB::named(vga::BLACK),
|
||||||
|
format!(" {} ", &flashing_message.message),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,15 +4,17 @@ use hecs::World;
|
||||||
use crate::resources::Resources;
|
use crate::resources::Resources;
|
||||||
|
|
||||||
mod entities;
|
mod entities;
|
||||||
|
mod flashing_message;
|
||||||
mod map;
|
mod map;
|
||||||
mod sidebar;
|
mod sidebar;
|
||||||
pub mod vga_color;
|
pub mod vga_color;
|
||||||
mod whip;
|
mod whip;
|
||||||
|
|
||||||
pub fn draw(world: &World, resources: &Resources, bterm: &mut BTerm) {
|
pub fn draw(world: &World, resources: &mut Resources, bterm: &mut BTerm) {
|
||||||
bterm.cls();
|
bterm.cls();
|
||||||
map::draw(resources, bterm);
|
map::draw(resources, bterm);
|
||||||
entities::draw(world, bterm);
|
entities::draw(world, bterm);
|
||||||
whip::draw(world, resources, bterm);
|
whip::draw(world, resources, bterm);
|
||||||
|
flashing_message::draw(resources, bterm);
|
||||||
sidebar::draw(resources, bterm);
|
sidebar::draw(resources, bterm);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,17 +6,26 @@ use crate::resources::*;
|
||||||
mod player;
|
mod player;
|
||||||
|
|
||||||
pub fn handle(world: &mut World, resources: &mut Resources, bterm: &mut BTerm) {
|
pub fn handle(world: &mut World, resources: &mut Resources, bterm: &mut BTerm) {
|
||||||
|
if resources.flashing_message.is_some() {
|
||||||
|
if bterm.key.is_some() {
|
||||||
|
resources.flashing_message = None;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
match bterm.key {
|
match bterm.key {
|
||||||
None => {}
|
None => {}
|
||||||
Some(key) => match key {
|
Some(key) => match key {
|
||||||
VirtualKeyCode::Left | VirtualKeyCode::J => {
|
VirtualKeyCode::Left | VirtualKeyCode::J => {
|
||||||
player::try_move(-1, 0, world, resources);
|
player::try_move(-1, 0, world, resources);
|
||||||
}
|
}
|
||||||
VirtualKeyCode::U | VirtualKeyCode::Home => player::try_move(-1, -1, world, resources),
|
VirtualKeyCode::U | VirtualKeyCode::Home => {
|
||||||
|
player::try_move(-1, -1, world, resources)
|
||||||
|
}
|
||||||
VirtualKeyCode::Up | VirtualKeyCode::I => {
|
VirtualKeyCode::Up | VirtualKeyCode::I => {
|
||||||
player::try_move(0, -1, world, resources);
|
player::try_move(0, -1, world, resources);
|
||||||
}
|
}
|
||||||
VirtualKeyCode::O | VirtualKeyCode::PageUp => player::try_move(1, -1, world, resources),
|
VirtualKeyCode::O | VirtualKeyCode::PageUp => {
|
||||||
|
player::try_move(1, -1, world, resources)
|
||||||
|
}
|
||||||
VirtualKeyCode::Right | VirtualKeyCode::K => {
|
VirtualKeyCode::Right | VirtualKeyCode::K => {
|
||||||
player::try_move(1, 0, world, resources);
|
player::try_move(1, 0, world, resources);
|
||||||
}
|
}
|
||||||
|
@ -26,7 +35,9 @@ pub fn handle(world: &mut World, resources: &mut Resources, bterm: &mut BTerm) {
|
||||||
VirtualKeyCode::Down | VirtualKeyCode::M => {
|
VirtualKeyCode::Down | VirtualKeyCode::M => {
|
||||||
player::try_move(0, 1, world, resources);
|
player::try_move(0, 1, world, resources);
|
||||||
}
|
}
|
||||||
VirtualKeyCode::N | VirtualKeyCode::End => player::try_move(-1, 1, world, resources),
|
VirtualKeyCode::N | VirtualKeyCode::End => {
|
||||||
|
player::try_move(-1, 1, world, resources)
|
||||||
|
}
|
||||||
VirtualKeyCode::W => {
|
VirtualKeyCode::W => {
|
||||||
player::whip(world, resources);
|
player::whip(world, resources);
|
||||||
}
|
}
|
||||||
|
@ -36,6 +47,10 @@ pub fn handle(world: &mut World, resources: &mut Resources, bterm: &mut BTerm) {
|
||||||
VirtualKeyCode::Escape | VirtualKeyCode::Q => {
|
VirtualKeyCode::Escape | VirtualKeyCode::Q => {
|
||||||
bterm.quit();
|
bterm.quit();
|
||||||
}
|
}
|
||||||
|
VirtualKeyCode::P => {
|
||||||
|
resources.flashing_message =
|
||||||
|
Some(FlashingMessage::from("Press any key to resume game."));
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
resources
|
resources
|
||||||
.sound_output
|
.sound_output
|
||||||
|
@ -44,3 +59,4 @@ pub fn handle(world: &mut World, resources: &mut Resources, bterm: &mut BTerm) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -57,6 +57,7 @@ fn main() -> BError {
|
||||||
sound_effects,
|
sound_effects,
|
||||||
sound_output,
|
sound_output,
|
||||||
selected_difficulty: Some(selected_difficulty),
|
selected_difficulty: Some(selected_difficulty),
|
||||||
|
flashing_message: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
// let descent_sounds: Vec<Sound> = (20..100)
|
// let descent_sounds: Vec<Sound> = (20..100)
|
||||||
|
|
32
src/resources/flashing_message.rs
Normal file
32
src/resources/flashing_message.rs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use crate::constants::*;
|
||||||
|
|
||||||
|
pub struct FlashingMessage {
|
||||||
|
pub message: String,
|
||||||
|
pub color: usize,
|
||||||
|
pub last_changed_color: Instant,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FlashingMessage {
|
||||||
|
pub fn next_color(&mut self) {
|
||||||
|
let now = Instant::now();
|
||||||
|
if now - self.last_changed_color > Duration::from_secs_f32(FLASHING_PERIOD) {
|
||||||
|
self.color += 1;
|
||||||
|
if self.color > 15 {
|
||||||
|
self.color = 13
|
||||||
|
}
|
||||||
|
self.last_changed_color = now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&str> for FlashingMessage {
|
||||||
|
fn from(message: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
message: message.to_string(),
|
||||||
|
color: 14,
|
||||||
|
last_changed_color: Instant::now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
pub mod clock;
|
pub mod clock;
|
||||||
pub mod difficulty;
|
pub mod difficulty;
|
||||||
|
pub mod flashing_message;
|
||||||
pub mod map;
|
pub mod map;
|
||||||
pub mod sound_effects;
|
pub mod sound_effects;
|
||||||
pub mod sound_output;
|
pub mod sound_output;
|
||||||
|
@ -8,6 +9,7 @@ pub mod stats;
|
||||||
use bracket_lib::prelude::*;
|
use bracket_lib::prelude::*;
|
||||||
pub use clock::{Clock, StopClock};
|
pub use clock::{Clock, StopClock};
|
||||||
pub use difficulty::Difficulty;
|
pub use difficulty::Difficulty;
|
||||||
|
pub use flashing_message::FlashingMessage;
|
||||||
pub use map::Map;
|
pub use map::Map;
|
||||||
pub use sound_effects::SoundEffects;
|
pub use sound_effects::SoundEffects;
|
||||||
pub use sound_output::SoundOutput;
|
pub use sound_output::SoundOutput;
|
||||||
|
@ -24,4 +26,5 @@ pub struct Resources {
|
||||||
pub selected_difficulty: Option<Difficulty>,
|
pub selected_difficulty: Option<Difficulty>,
|
||||||
pub sound_effects: SoundEffects,
|
pub sound_effects: SoundEffects,
|
||||||
pub sound_output: SoundOutput,
|
pub sound_output: SoundOutput,
|
||||||
|
pub flashing_message: Option<FlashingMessage>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ impl GameState for State {
|
||||||
fn tick(&mut self, bterm: &mut BTerm) {
|
fn tick(&mut self, bterm: &mut BTerm) {
|
||||||
input::handle(&mut self.world, &mut self.resources, bterm);
|
input::handle(&mut self.world, &mut self.resources, bterm);
|
||||||
systems::run(&mut self.world, &mut self.resources);
|
systems::run(&mut self.world, &mut self.resources);
|
||||||
graphics::draw(&self.world, &self.resources, bterm);
|
graphics::draw(&self.world, &mut self.resources, bterm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ use crate::constants::CLOCK_PERIOD;
|
||||||
use crate::resources::{Clock, Resources};
|
use crate::resources::{Clock, Resources};
|
||||||
|
|
||||||
pub fn run(resources: &mut Resources) {
|
pub fn run(resources: &mut Resources) {
|
||||||
if !resources.stop_clock {
|
if !resources.stop_clock && resources.flashing_message.is_none() {
|
||||||
try_tick(&mut resources.clock);
|
try_tick(&mut resources.clock);
|
||||||
} else {
|
} else {
|
||||||
reset(&mut resources.clock);
|
reset(&mut resources.clock);
|
||||||
|
|
Loading…
Add table
Reference in a new issue