Compare commits
8 commits
ecaf448e90
...
fe7eef7770
Author | SHA1 | Date | |
---|---|---|---|
fe7eef7770 | |||
f38a80b0c7 | |||
2b484a4ad7 | |||
e948ad1d93 | |||
094d8e8617 | |||
550713aaf8 | |||
0e5e823b0b | |||
def124cd71 |
14 changed files with 261 additions and 45 deletions
|
@ -2,6 +2,7 @@
|
|||
/// Fast PC mode at 400 cycles/ms.
|
||||
pub const CLOCK_PERIOD: f32 = 0.184;
|
||||
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_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),
|
||||
MAP_Y + MAP_HEIGHT,
|
||||
RGB::named(vga::get_by_index(flashing_message.color)),
|
||||
RGB::named(vga::BLACK),
|
||||
flashing_message.message.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -4,15 +4,17 @@ use hecs::World;
|
|||
use crate::resources::Resources;
|
||||
|
||||
mod entities;
|
||||
mod flashing_message;
|
||||
mod map;
|
||||
mod sidebar;
|
||||
pub mod vga_color;
|
||||
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();
|
||||
map::draw(resources, bterm);
|
||||
entities::draw(world, bterm);
|
||||
whip::draw(world, resources, bterm);
|
||||
flashing_message::draw(resources, bterm);
|
||||
sidebar::draw(resources, bterm);
|
||||
}
|
||||
|
|
|
@ -1,11 +1,35 @@
|
|||
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() && bterm.key.is_some() {
|
||||
handle_intent(bterm, world, resources);
|
||||
resources.flashing_message = None;
|
||||
}
|
||||
|
||||
match bterm.key {
|
||||
None => {}
|
||||
Some(key) => match key {
|
||||
|
@ -34,7 +58,17 @@ 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
|
||||
.sound_output
|
||||
.play_sound(resources.sound_effects.pause.clone());
|
||||
resources.flashing_message =
|
||||
Some(FlashingMessage::from(" Press any key to resume game. "));
|
||||
}
|
||||
_ => {
|
||||
resources
|
||||
|
|
|
@ -93,7 +93,7 @@ fn try_step(point: Point, _world: &World, resources: &mut Resources) -> bool {
|
|||
true
|
||||
}
|
||||
crate::tile_data::TileType::Stairs => {
|
||||
// TODO: Go to next level
|
||||
resources.should_advance_level = true;
|
||||
true
|
||||
}
|
||||
crate::tile_data::TileType::Chest => todo!(),
|
||||
|
|
|
@ -32,7 +32,7 @@ fn main() -> BError {
|
|||
|
||||
let mut world = World::new();
|
||||
|
||||
let mut map = Map::from_level(levels::get_level(starting_level));
|
||||
let mut map = Map::from(levels::get_level(starting_level));
|
||||
map.spawn_entities(&mut world);
|
||||
|
||||
let resources = Resources {
|
||||
|
@ -57,6 +57,8 @@ fn main() -> BError {
|
|||
sound_effects,
|
||||
sound_output,
|
||||
selected_difficulty: Some(selected_difficulty),
|
||||
flashing_message: Some(FlashingMessage::from("Press any key to begin this level.")),
|
||||
should_advance_level: false,
|
||||
};
|
||||
|
||||
// let descent_sounds: Vec<Sound> = (20..100)
|
||||
|
|
45
src/resources/flashing_message.rs
Normal file
45
src/resources/flashing_message.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use std::time::{Duration, Instant};
|
||||
|
||||
use crate::constants::*;
|
||||
|
||||
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) {
|
||||
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::new(message, None)
|
||||
}
|
||||
}
|
|
@ -44,42 +44,6 @@ impl Map {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_level(level: Level) -> Self {
|
||||
match level {
|
||||
Level::Normal(_data) => todo!(),
|
||||
Level::Randomized(data) => {
|
||||
let mut rng = RandomNumberGenerator::new();
|
||||
let mut map = Self::new();
|
||||
for (tile, count) in data {
|
||||
for _ in 0..count {
|
||||
loop {
|
||||
let point = Point {
|
||||
x: rng.range(0, MAP_WIDTH as i32),
|
||||
y: rng.range(0, MAP_HEIGHT as i32),
|
||||
};
|
||||
if map.get_tile_at(point) == TileType::Floor {
|
||||
map.set_tile_at(point, tile);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
loop {
|
||||
let point = Point {
|
||||
x: rng.range(0, MAP_WIDTH as i32),
|
||||
y: rng.range(0, MAP_HEIGHT as i32),
|
||||
};
|
||||
if map.get_tile_at(point) == TileType::Floor {
|
||||
map.set_tile_at(point, TileType::Player);
|
||||
break;
|
||||
}
|
||||
}
|
||||
map
|
||||
}
|
||||
Level::End => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spawn_entities(&mut self, world: &mut World) {
|
||||
for (index, tile) in self
|
||||
.tiles
|
||||
|
@ -246,3 +210,58 @@ impl Map {
|
|||
self.tiles[index] = tile;
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Level> for Map {
|
||||
fn from(level: Level) -> Self {
|
||||
match level {
|
||||
Level::Normal(data) => {
|
||||
assert_eq!(data.len(), MAP_HEIGHT, "Level data is incorrect height!");
|
||||
|
||||
let mut map = Self::new();
|
||||
for (y, line) in data.iter().enumerate() {
|
||||
assert_eq!(line.len(), MAP_WIDTH, "Level data is incorrect width!");
|
||||
for (x, c) in line.chars().enumerate() {
|
||||
map.set_tile_at(
|
||||
Point {
|
||||
x: x as i32,
|
||||
y: y as i32,
|
||||
},
|
||||
TileType::from(c),
|
||||
);
|
||||
}
|
||||
}
|
||||
map
|
||||
}
|
||||
Level::Randomized(data) => {
|
||||
let mut rng = RandomNumberGenerator::new();
|
||||
let mut map = Self::new();
|
||||
for (tile, count) in data {
|
||||
for _ in 0..count {
|
||||
loop {
|
||||
let point = Point {
|
||||
x: rng.range(0, MAP_WIDTH as i32),
|
||||
y: rng.range(0, MAP_HEIGHT as i32),
|
||||
};
|
||||
if map.get_tile_at(point) == TileType::Floor {
|
||||
map.set_tile_at(point, tile);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
loop {
|
||||
let point = Point {
|
||||
x: rng.range(0, MAP_WIDTH as i32),
|
||||
y: rng.range(0, MAP_HEIGHT as i32),
|
||||
};
|
||||
if map.get_tile_at(point) == TileType::Floor {
|
||||
map.set_tile_at(point, TileType::Player);
|
||||
break;
|
||||
}
|
||||
}
|
||||
map
|
||||
}
|
||||
Level::End => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
pub mod clock;
|
||||
pub mod difficulty;
|
||||
pub mod flashing_message;
|
||||
pub mod map;
|
||||
pub mod sound_effects;
|
||||
pub mod sound_output;
|
||||
|
@ -8,6 +9,7 @@ pub mod stats;
|
|||
use bracket_lib::prelude::*;
|
||||
pub use clock::{Clock, StopClock};
|
||||
pub use difficulty::Difficulty;
|
||||
pub use flashing_message::FlashingMessage;
|
||||
pub use map::Map;
|
||||
pub use sound_effects::SoundEffects;
|
||||
pub use sound_output::SoundOutput;
|
||||
|
@ -24,4 +26,6 @@ pub struct Resources {
|
|||
pub selected_difficulty: Option<Difficulty>,
|
||||
pub sound_effects: SoundEffects,
|
||||
pub sound_output: SoundOutput,
|
||||
pub flashing_message: Option<FlashingMessage>,
|
||||
pub should_advance_level: bool,
|
||||
}
|
||||
|
|
|
@ -38,6 +38,7 @@ pub struct SoundEffects {
|
|||
pub slow_hit: SoundSamples,
|
||||
pub medium_hit: SoundSamples,
|
||||
pub fast_hit: SoundSamples,
|
||||
pub pause: SoundSamples,
|
||||
rng: RandomNumberGenerator,
|
||||
}
|
||||
|
||||
|
@ -153,6 +154,19 @@ impl SoundEffects {
|
|||
duration: Duration::from_millis(25),
|
||||
}],
|
||||
}),
|
||||
pause: sound_output.render_sound_effect(&SoundEffect {
|
||||
sounds: {
|
||||
let mut sounds = vec![Sound {
|
||||
sound_type: SoundType::Tone(500),
|
||||
duration: Duration::from_millis(100),
|
||||
}];
|
||||
sounds.extend((100..=200).rev().step_by(10).map(|x| Sound {
|
||||
sound_type: SoundType::Tone(x),
|
||||
duration: Duration::from_millis(20),
|
||||
}));
|
||||
sounds
|
||||
},
|
||||
}),
|
||||
rng: RandomNumberGenerator::new(),
|
||||
}
|
||||
}
|
||||
|
|
24
src/state.rs
24
src/state.rs
|
@ -1,5 +1,6 @@
|
|||
use crate::resources::Resources;
|
||||
use crate::{graphics, input, systems};
|
||||
use crate::resources::flashing_message::FlashingMessage;
|
||||
use crate::resources::{Map, Resources};
|
||||
use crate::{graphics, input, levels, systems};
|
||||
use bracket_lib::prelude::*;
|
||||
use hecs::World;
|
||||
|
||||
|
@ -12,7 +13,11 @@ impl GameState for State {
|
|||
fn tick(&mut self, bterm: &mut BTerm) {
|
||||
input::handle(&mut self.world, &mut self.resources, bterm);
|
||||
systems::run(&mut self.world, &mut self.resources);
|
||||
graphics::draw(&self.world, &self.resources, bterm);
|
||||
graphics::draw(&self.world, &mut self.resources, bterm);
|
||||
|
||||
if self.resources.should_advance_level {
|
||||
self.next_level();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,4 +25,17 @@ impl State {
|
|||
pub fn new(world: World, resources: Resources) -> Self {
|
||||
State { world, resources }
|
||||
}
|
||||
|
||||
fn next_level(&mut self) {
|
||||
self.resources.level_number += 1;
|
||||
|
||||
self.world.clear();
|
||||
self.resources.map = Map::from(levels::get_level(self.resources.level_number));
|
||||
self.resources.map.spawn_entities(&mut self.world);
|
||||
|
||||
self.resources.flashing_message =
|
||||
Some(FlashingMessage::from("Press any key to begin this level."));
|
||||
|
||||
self.resources.should_advance_level = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,6 +74,7 @@ pub fn run(world: &mut World, resources: &mut Resources) {
|
|||
resources.stats.add_score(1);
|
||||
*tile = TileType::Floor;
|
||||
to_kill.push(entity);
|
||||
resources.map.clear_tile_content_at(Point::from(*position));
|
||||
}
|
||||
_ => {
|
||||
resources.map.clear_tile_content_at(Point::from(*position));
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::constants::CLOCK_PERIOD;
|
|||
use crate::resources::{Clock, 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);
|
||||
} else {
|
||||
reset(&mut resources.clock);
|
||||
|
|
|
@ -375,3 +375,61 @@ pub fn tile_data(tile: TileType) -> TileData {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
impl From<char> for TileType {
|
||||
fn from(c: char) -> Self {
|
||||
match c {
|
||||
' ' => TileType::Floor,
|
||||
'1' => TileType::Slow,
|
||||
'2' => TileType::Medium,
|
||||
'3' => TileType::Fast,
|
||||
'X' => TileType::Block,
|
||||
'W' => TileType::Whip,
|
||||
'L' => TileType::Stairs,
|
||||
'C' => TileType::Chest,
|
||||
'S' => TileType::SlowTime,
|
||||
'+' => TileType::Gem,
|
||||
'I' => TileType::Invisible,
|
||||
'T' => TileType::Teleport,
|
||||
'K' => TileType::Key,
|
||||
'D' => TileType::Door,
|
||||
'#' => TileType::Wall,
|
||||
'F' => TileType::SpeedTime,
|
||||
'.' => TileType::Trap,
|
||||
'R' => TileType::River,
|
||||
'Q' => TileType::Power,
|
||||
'/' => TileType::Forest,
|
||||
'\\' => TileType::Tree,
|
||||
'B' => TileType::Bomb,
|
||||
'V' => TileType::Lava,
|
||||
'=' => TileType::Pit,
|
||||
'A' => TileType::Tome,
|
||||
'U' => TileType::Tunnel,
|
||||
'Z' => TileType::Freeze,
|
||||
'*' => TileType::Nugget,
|
||||
'E' => TileType::Quake,
|
||||
';' => TileType::InvisibleBlock,
|
||||
':' => TileType::InvisibleWall,
|
||||
'`' => TileType::InvisibleDoor,
|
||||
'-' => TileType::Stop,
|
||||
'%' => TileType::Zap,
|
||||
']' => TileType::Create,
|
||||
'G' => TileType::Generator,
|
||||
'@' => TileType::Trap2,
|
||||
')' => TileType::Trap3,
|
||||
'(' => TileType::Trap4,
|
||||
'$' => TileType::Trap5,
|
||||
'α' => TileType::Trap6,
|
||||
'ß' => TileType::Trap7,
|
||||
'Γ' => TileType::Trap8,
|
||||
'π' => TileType::Trap9,
|
||||
'Σ' => TileType::Trap10,
|
||||
'σ' => TileType::Trap11,
|
||||
'µ' => TileType::Trap12,
|
||||
'τ' => TileType::Trap13,
|
||||
'P' => TileType::Player,
|
||||
'!' => TileType::Punctuation,
|
||||
_ => TileType::Letter(c),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue