Initial web support

This commit is contained in:
Alex Page 2022-02-04 19:09:59 -05:00
parent 4cd9f5acc3
commit cbcda28719
17 changed files with 238 additions and 143 deletions

View file

@ -1,4 +1,4 @@
use std::time::Instant;
use instant::Instant;
pub mod monster;
pub mod player;

View file

@ -1,4 +1,4 @@
use std::time::Instant;
use instant::Instant;
#[derive(Debug)]
pub struct Player {

View file

@ -10,6 +10,13 @@ pub fn handle_intent(bterm: &mut BTerm, _world: &mut World, resources: &mut Reso
if let Some(flashing_message) = &resources.flashing_message {
if let Some(intent) = &flashing_message.intent {
match intent {
FlashingMessageIntent::Start => {
if resources.sound_output.is_none() || resources.sound_effects.is_none() {
let sound_output = SoundOutput::new();
resources.sound_effects = Some(SoundEffects::new(&sound_output));
resources.sound_output = Some(sound_output);
}
}
FlashingMessageIntent::Quit => {
if key == VirtualKeyCode::Y {
bterm.quit()
@ -70,16 +77,20 @@ pub fn handle(world: &mut World, resources: &mut Resources, bterm: &mut BTerm) {
));
}
VirtualKeyCode::P => {
resources
.sound_output
.play_sound(resources.sound_effects.pause.clone());
if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
sound_output.play_sound(sound_effects.pause.clone());
}
resources.flashing_message =
Some(FlashingMessage::from(" Press any key to resume game. "));
}
_ => {
resources
.sound_output
.play_sound(resources.sound_effects.bad_key.clone());
if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
sound_output.play_sound(sound_effects.bad_key.clone());
}
}
},
}

View file

@ -1,4 +1,5 @@
use std::time::{Duration, Instant};
use instant::Instant;
use std::time::Duration;
use bracket_lib::prelude::*;
use hecs::{Entity, With, Without, World};
@ -29,10 +30,12 @@ pub fn try_move(delta_x: i32, delta_y: i32, world: &mut World, resources: &mut R
if let Ok(monster) = world.get::<Monster>(monster_entity) {
resources.stats.add_score(damage_for_kind(monster.kind));
resources.stats.take_gems(damage_for_kind(monster.kind));
resources.sound_output.play_sound(sound_effect_for_kind(
monster.kind,
&resources.sound_effects,
));
if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
sound_output
.play_sound(sound_effect_for_kind(monster.kind, sound_effects));
}
to_kill.push(monster_entity);
}
}
@ -48,15 +51,17 @@ pub fn try_move(delta_x: i32, delta_y: i32, world: &mut World, resources: &mut R
systems::time::force_tick(&mut resources.clock);
resources
.sound_output
.play_sound(resources.sound_effects.step.clone());
if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
sound_output.play_sound(sound_effects.step.clone());
}
}
} else {
let static_sound = resources
.sound_effects
.get_new_static_effect(&resources.sound_output);
resources.sound_output.play_sound(static_sound);
} else if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
let static_sound = sound_effects.get_new_static_effect(sound_output);
sound_output.play_sound(static_sound);
}
player.last_moved = now;
@ -75,18 +80,22 @@ fn try_step(point: Point, _world: &World, resources: &mut Resources) -> bool {
| crate::tile_data::TileType::Medium
| crate::tile_data::TileType::Fast => true,
crate::tile_data::TileType::Block | crate::tile_data::TileType::Wall => {
resources
.sound_output
.play_sound(resources.sound_effects.blocked.clone());
if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
sound_output.play_sound(sound_effects.blocked.clone());
}
if resources.stats.score > 2 {
resources.stats.take_score(2);
}
false
}
crate::tile_data::TileType::Whip => {
resources
.sound_output
.play_sound(resources.sound_effects.grab.clone());
if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
sound_output.play_sound(sound_effects.grab.clone());
}
resources.stats.give_whips(1);
resources.stats.add_score(1);
resources.map.set_tile_at(point, TileType::Floor);
@ -99,9 +108,11 @@ fn try_step(point: Point, _world: &World, resources: &mut Resources) -> bool {
crate::tile_data::TileType::Chest => todo!(),
crate::tile_data::TileType::SlowTime => todo!(),
crate::tile_data::TileType::Gem => {
resources
.sound_output
.play_sound(resources.sound_effects.grab.clone());
if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
sound_output.play_sound(sound_effects.grab.clone());
}
resources.stats.give_gems(1);
resources.stats.add_score(1);
resources.map.set_tile_at(point, TileType::Floor);
@ -109,9 +120,11 @@ fn try_step(point: Point, _world: &World, resources: &mut Resources) -> bool {
}
crate::tile_data::TileType::Invisible => todo!(),
crate::tile_data::TileType::Teleport => {
resources
.sound_output
.play_sound(resources.sound_effects.grab.clone());
if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
sound_output.play_sound(sound_effects.grab.clone());
}
resources.stats.give_teleports(1);
resources.stats.add_score(1);
resources.map.set_tile_at(point, TileType::Floor);
@ -173,11 +186,15 @@ pub fn whip(world: &mut World, resources: &mut Resources) {
WantsToWhip {
frame: 0,
last_frame: Instant::now(),
sound: Some(
resources
.sound_output
.play_sound(resources.sound_effects.whipping.clone()),
),
sound: {
if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
Some(sound_output.play_sound(sound_effects.whipping.clone()))
} else {
None
}
},
},
);
resources.stats.whips -= 1;

View file

@ -10,74 +10,29 @@ mod state;
pub mod systems;
pub mod tile_data;
use std::time::Instant;
use std::panic;
use bracket_lib::prelude::*;
use hecs::World;
use resources::{difficulty::SECRET, *};
use state::State;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
#[wasm_bindgen(start)]
pub fn main_js() -> Result<(), JsValue> {
panic::set_hook(Box::new(console_error_panic_hook::hook));
let _ = main_common(false);
Ok(())
}
fn main() -> BError {
main_common(true)
}
fn main_common(initialize_sound: bool) -> BError {
let context = BTermBuilder::vga(80, 25)
.with_fps_cap(60.0)
.with_title("Kroz")
.build()?;
.build()
.unwrap();
let mut sound_output = SoundOutput::new();
let sound_effects = SoundEffects::new(&sound_output);
sound_output.play_sound(sound_effects.startup.clone());
let starting_level = 0;
let selected_difficulty = SECRET;
let mut world = World::new();
let mut map = Map::from(levels::get_level(starting_level));
map.spawn_entities(&mut world);
let resources = Resources {
level_number: starting_level,
show_debug_info: false,
player_input: None,
stats: Stats {
score: 0,
gems: selected_difficulty.starting_gems,
whips: selected_difficulty.starting_whips,
whip_power: selected_difficulty.starting_whip_power,
teleports: selected_difficulty.starting_teleports,
keys: selected_difficulty.starting_keys,
},
clock: Clock {
last_ticked: Instant::now(),
has_ticked: false,
ticks: 0,
},
stop_clock: false,
map,
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)
// .rev()
// .flat_map(|x| {
// (1..10).rev().flat_map(move |y| {
// vec![Sound {
// sound_type: SoundType::Tone(x * y * y),
// duration: Duration::from_millis((y as f64 / 1.5) as u64),
// }]
// })
// })
// .collect();
// let descent_effect = gs.sound_system.render_sound_effect(SoundEffect {
// sounds: descent_sounds,
// });
// let _ = gs.sound_system.play_sound(descent_effect);
main_loop(context, State::new(world, resources))
main_loop(context, State::new(initialize_sound))
}

View file

@ -1,4 +1,4 @@
use std::time::Instant;
use instant::Instant;
pub struct Clock {
pub last_ticked: Instant,

View file

@ -1,8 +1,10 @@
use std::time::{Duration, Instant};
use instant::Instant;
use std::time::Duration;
use crate::constants::*;
pub enum FlashingMessageIntent {
Start,
Quit,
Save,
Restore,

View file

@ -1,4 +1,4 @@
use std::time::Instant;
use instant::Instant;
use crate::{
components::{monster::*, Monster, Player, Position, Renderable},

View file

@ -24,8 +24,8 @@ pub struct Resources {
pub stop_clock: bool,
pub map: Map,
pub selected_difficulty: Option<Difficulty>,
pub sound_effects: SoundEffects,
pub sound_output: SoundOutput,
pub sound_effects: Option<SoundEffects>,
pub sound_output: Option<SoundOutput>,
pub flashing_message: Option<FlashingMessage>,
pub should_advance_level: bool,
}

View file

@ -1,5 +1,11 @@
use crate::resources::flashing_message::FlashingMessage;
use crate::resources::{Map, Resources};
use instant::Instant;
use crate::resources::clock::Clock;
use crate::resources::difficulty::*;
use crate::resources::flashing_message::{FlashingMessage, FlashingMessageIntent};
use crate::resources::sound_effects::SoundEffects;
use crate::resources::stats::Stats;
use crate::resources::{Map, Resources, SoundOutput};
use crate::{graphics, input, levels, systems};
use bracket_lib::prelude::*;
use hecs::World;
@ -22,7 +28,56 @@ impl GameState for State {
}
impl State {
pub fn new(world: World, resources: Resources) -> Self {
pub fn new(initialize_sound: bool) -> Self {
let mut sound_output = if initialize_sound {
Some(SoundOutput::new())
} else {
None
};
let sound_effects = sound_output.as_ref().map(SoundEffects::new);
if let (Some(so), Some(se)) = (&mut sound_output, &sound_effects) {
so.play_sound(se.startup.clone());
}
let starting_level = 0;
let selected_difficulty = SECRET;
let mut world = World::new();
let mut map = Map::from(levels::get_level(starting_level));
map.spawn_entities(&mut world);
let resources = Resources {
level_number: starting_level,
show_debug_info: false,
player_input: None,
stats: Stats {
score: 0,
gems: selected_difficulty.starting_gems,
whips: selected_difficulty.starting_whips,
whip_power: selected_difficulty.starting_whip_power,
teleports: selected_difficulty.starting_teleports,
keys: selected_difficulty.starting_keys,
},
clock: Clock {
last_ticked: Instant::now(),
has_ticked: false,
ticks: 0,
},
stop_clock: false,
map,
sound_effects,
sound_output,
selected_difficulty: Some(selected_difficulty),
flashing_message: Some(FlashingMessage::new(
"Press any key to begin this level.",
Some(FlashingMessageIntent::Start),
)),
should_advance_level: false,
};
State { world, resources }
}

View file

@ -56,10 +56,12 @@ pub fn run(world: &mut World, resources: &mut Resources) {
// TODO: Sound
resources.map.clear_tile_content_at(Point::from(*position));
resources.stats.take_gems(damage_for_kind(monster.kind));
resources.sound_output.play_sound(sound_effect_for_kind(
monster.kind,
&resources.sound_effects,
));
if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
sound_output
.play_sound(sound_effect_for_kind(monster.kind, sound_effects));
}
to_kill.push(entity);
}
} else {
@ -67,10 +69,14 @@ pub fn run(world: &mut World, resources: &mut Resources) {
match tile {
TileType::Wall => {}
TileType::Block => {
resources.sound_output.play_sound(sound_effect_for_kind(
monster.kind,
&resources.sound_effects,
));
if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
sound_output.play_sound(sound_effect_for_kind(
monster.kind,
sound_effects,
));
}
resources.stats.add_score(1);
*tile = TileType::Floor;
to_kill.push(entity);

View file

@ -1,4 +1,5 @@
use std::time::{Duration, Instant};
use instant::Instant;
use std::time::Duration;
use crate::constants::CLOCK_PERIOD;

View file

@ -1,4 +1,5 @@
use std::time::{Duration, Instant};
use instant::Instant;
use std::time::Duration;
use bracket_lib::prelude::*;
use hecs::{Entity, World};
@ -77,15 +78,16 @@ pub fn run(world: &mut World, resources: &mut Resources) {
}
if wants_to_whip.frame == 7 {
wants_to_whip.sound = None;
resources
.sound_output
.play_sound(resources.sound_effects.whipping_hit_end.clone());
} else {
wants_to_whip.sound = Some(
resources
.sound_output
.play_sound(resources.sound_effects.whipping_hit.clone()),
);
if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
sound_output.play_sound(sound_effects.whipping_hit_end.clone());
}
} else if let (Some(sound_effects), Some(sound_output)) =
(&mut resources.sound_effects, &mut resources.sound_output)
{
wants_to_whip.sound =
Some(sound_output.play_sound(sound_effects.whipping_hit.clone()));
}
}
}