diff --git a/src/constants.rs b/src/constants.rs index 2a90aec..c4028d7 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -2,7 +2,6 @@ /// 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; diff --git a/src/graphics/flashing_message.rs b/src/graphics/flashing_message.rs deleted file mode 100644 index 23d1d02..0000000 --- a/src/graphics/flashing_message.rs +++ /dev/null @@ -1,18 +0,0 @@ -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(), - ); - } -} diff --git a/src/graphics/mod.rs b/src/graphics/mod.rs index b587b48..c2ef34b 100644 --- a/src/graphics/mod.rs +++ b/src/graphics/mod.rs @@ -4,17 +4,15 @@ 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: &mut Resources, bterm: &mut BTerm) { +pub fn draw(world: &World, resources: &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); } diff --git a/src/input/mod.rs b/src/input/mod.rs index 0907753..15c2318 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -1,35 +1,11 @@ use bracket_lib::prelude::*; use hecs::World; -use crate::resources::{flashing_message::FlashingMessageIntent, *}; +use crate::resources::*; 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 { @@ -58,17 +34,7 @@ pub fn handle(world: &mut World, resources: &mut Resources, bterm: &mut BTerm) { resources.show_debug_info = !resources.show_debug_info; } VirtualKeyCode::Escape | VirtualKeyCode::Q => { - 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. ")); + bterm.quit(); } _ => { resources diff --git a/src/input/player.rs b/src/input/player.rs index df9906b..1d5c416 100644 --- a/src/input/player.rs +++ b/src/input/player.rs @@ -93,7 +93,7 @@ fn try_step(point: Point, _world: &World, resources: &mut Resources) -> bool { true } crate::tile_data::TileType::Stairs => { - resources.should_advance_level = true; + // TODO: Go to next level true } crate::tile_data::TileType::Chest => todo!(), diff --git a/src/main.rs b/src/main.rs index cd82959..317ae33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ fn main() -> BError { let mut world = World::new(); - let mut map = Map::from(levels::get_level(starting_level)); + let mut map = Map::from_level(levels::get_level(starting_level)); map.spawn_entities(&mut world); let resources = Resources { @@ -57,8 +57,6 @@ 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 = (20..100) diff --git a/src/resources/flashing_message.rs b/src/resources/flashing_message.rs deleted file mode 100644 index 6155c72..0000000 --- a/src/resources/flashing_message.rs +++ /dev/null @@ -1,45 +0,0 @@ -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, -} - -impl FlashingMessage { - pub fn new(message: &str, intent: Option) -> 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) - } -} diff --git a/src/resources/map.rs b/src/resources/map.rs index bef1dc5..76ad3b2 100644 --- a/src/resources/map.rs +++ b/src/resources/map.rs @@ -44,6 +44,42 @@ 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 @@ -210,58 +246,3 @@ impl Map { self.tiles[index] = tile; } } - -impl From 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!(), - } - } -} diff --git a/src/resources/mod.rs b/src/resources/mod.rs index ab7b5cd..0a3425d 100644 --- a/src/resources/mod.rs +++ b/src/resources/mod.rs @@ -1,6 +1,5 @@ pub mod clock; pub mod difficulty; -pub mod flashing_message; pub mod map; pub mod sound_effects; pub mod sound_output; @@ -9,7 +8,6 @@ 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; @@ -26,6 +24,4 @@ pub struct Resources { pub selected_difficulty: Option, pub sound_effects: SoundEffects, pub sound_output: SoundOutput, - pub flashing_message: Option, - pub should_advance_level: bool, } diff --git a/src/resources/sound_effects.rs b/src/resources/sound_effects.rs index f0b7267..f0779c1 100644 --- a/src/resources/sound_effects.rs +++ b/src/resources/sound_effects.rs @@ -38,7 +38,6 @@ pub struct SoundEffects { pub slow_hit: SoundSamples, pub medium_hit: SoundSamples, pub fast_hit: SoundSamples, - pub pause: SoundSamples, rng: RandomNumberGenerator, } @@ -154,19 +153,6 @@ 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(), } } diff --git a/src/state.rs b/src/state.rs index 101e160..b992e9f 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,6 +1,5 @@ -use crate::resources::flashing_message::FlashingMessage; -use crate::resources::{Map, Resources}; -use crate::{graphics, input, levels, systems}; +use crate::resources::Resources; +use crate::{graphics, input, systems}; use bracket_lib::prelude::*; use hecs::World; @@ -13,11 +12,7 @@ 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, &mut self.resources, bterm); - - if self.resources.should_advance_level { - self.next_level(); - } + graphics::draw(&self.world, &self.resources, bterm); } } @@ -25,17 +20,4 @@ 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; - } } diff --git a/src/systems/monster_ai.rs b/src/systems/monster_ai.rs index 8123234..32061dd 100644 --- a/src/systems/monster_ai.rs +++ b/src/systems/monster_ai.rs @@ -74,7 +74,6 @@ 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)); diff --git a/src/systems/time.rs b/src/systems/time.rs index e924f47..4686e31 100644 --- a/src/systems/time.rs +++ b/src/systems/time.rs @@ -5,7 +5,7 @@ use crate::constants::CLOCK_PERIOD; use crate::resources::{Clock, Resources}; pub fn run(resources: &mut Resources) { - if !resources.stop_clock && resources.flashing_message.is_none() { + if !resources.stop_clock { try_tick(&mut resources.clock); } else { reset(&mut resources.clock); diff --git a/src/tile_data.rs b/src/tile_data.rs index a6bc744..775889b 100644 --- a/src/tile_data.rs +++ b/src/tile_data.rs @@ -375,61 +375,3 @@ pub fn tile_data(tile: TileType) -> TileData { }, } } - -impl From 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), - } - } -}