Play background sound (#19)
All checks were successful
Rust / rustfmt (push) Successful in 25s
Rust / clippy (push) Successful in 2m33s
Rust / build (push) Successful in 3m44s

Reviewed-on: #19
Co-authored-by: Dominic <git@msrd0.de>
Co-committed-by: Dominic <git@msrd0.de>
This commit is contained in:
Dominic 2024-07-07 16:13:39 +00:00 committed by msrd0.dev - Forgejo
parent 8e04d74682
commit 6115ef4aa2
Signed by: msrd0.dev - Forgejo
GPG key ID: E2F16281CAA26E5F
5 changed files with 57 additions and 14 deletions

View file

@ -128,7 +128,11 @@ impl AssetsWriter {
writeln!(file, "{indent}\t\t_ctx.load_texture_from_bytes({asset_const_name:?}, {asset_const_name});")?; writeln!(file, "{indent}\t\t_ctx.load_texture_from_bytes({asset_const_name:?}, {asset_const_name});")?;
} }
for asset_const_name in root.sound_assets.values() { for asset_const_name in root.sound_assets.values() {
writeln!(file, "{indent}\t\tcomfy::load_sound_from_bytes({asset_const_name:?}, {asset_const_name}, Default::default());")?; writeln!(file, "{indent}\t\tcomfy::load_sound_from_bytes({asset_const_name:?}, {asset_const_name},")?;
writeln!(
file,
"{indent}\t\t\tcomfy::StaticSoundSettings::new().loop_region(..));"
)?;
} }
for group_name in root.groups.keys() { for group_name in root.groups.keys() {
writeln!(file, "{indent}\t\t{group_name}::Assets::load(_ctx);")?; writeln!(file, "{indent}\t\t{group_name}::Assets::load(_ctx);")?;

View file

@ -3,7 +3,11 @@ mod grid;
mod player; mod player;
mod room; mod room;
use comfy::{main_camera_mut, random_i32, vec2, EngineContext, RandomRange as _}; use crate::assets::ASSETS;
use comfy::{
main_camera_mut, play_sound_id, random_i32, stop_sound_id, vec2, EngineContext,
RandomRange as _
};
use grid::Grid; use grid::Grid;
use log::error; use log::error;
use player::Player; use player::Player;
@ -23,7 +27,9 @@ pub struct HouseState {
/// The energy level remaining in the house. Should decrease by itself, and much /// The energy level remaining in the house. Should decrease by itself, and much
/// faster when inhabited by the ghost. /// faster when inhabited by the ghost.
pub charge: f32, pub charge: f32,
pub max_charge: f32 pub max_charge: f32,
pub sound_playing: bool
} }
impl HouseState { impl HouseState {
@ -43,14 +49,17 @@ impl HouseState {
rooms, rooms,
player, player,
human_layer: false, human_layer: false,
// TODO this should be lower depending on the time elapsed // TODO this should be lower depending on the time elapsed
charge: max_charge, charge: max_charge,
max_charge max_charge,
sound_playing: false
} }
} }
} }
pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) { pub fn draw(state: &crate::State, _ctx: &EngineContext<'_>) {
let Some(house) = state.house() else { let Some(house) = state.house() else {
error!("How can I render a house when I'm not in one?!?"); error!("How can I render a house when I'm not in one?!?");
return; return;
@ -70,14 +79,27 @@ pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) {
house.player.draw(); house.player.draw();
} }
pub fn update(state: &mut crate::State, ctx: &mut comfy::EngineContext<'_>) { pub fn update(state: &mut crate::State, ctx: &mut EngineContext<'_>) {
if state.overworld.sound_playing {
stop_sound_id(ASSETS.music.galactic_rap_speedup);
state.overworld.sound_playing = false;
}
let mut camera = main_camera_mut(); let mut camera = main_camera_mut();
camera.center = vec2(0.0, 0.0); camera.center = vec2(0.0, 0.0);
let house = state.house_mut(ctx); let Some(house) = state.house_mut(ctx) else {
error!("WTF I cannot update a house without a house");
return;
};
let current_room = house.rooms.get(house.current_room_id).unwrap(); let current_room = house.rooms.get(house.current_room_id).unwrap();
house.player.update(&current_room.grid); house.player.update(&current_room.grid);
if !house.sound_playing {
play_sound_id(ASSETS.music.mesmerizing_galaxy_loop);
house.sound_playing = true;
}
if house.player.is_moving_to_right_room(current_room) { if house.player.is_moving_to_right_room(current_room) {
if house.current_room_id < (house.room_count - 1) { if house.current_room_id < (house.room_count - 1) {
house.current_room_id += 1; house.current_room_id += 1;

View file

@ -1,11 +1,13 @@
use crate::{ use crate::{
activities::Activity, activities::Activity,
assets::ASSETS,
game::{ZLayer, GHOST_DISCHARGE_RATE, GHOST_DISCHARGE_RATE_MOVEMENT}, game::{ZLayer, GHOST_DISCHARGE_RATE, GHOST_DISCHARGE_RATE_MOVEMENT},
State State
}; };
use comfy::{ use comfy::{
draw_rect_outline, draw_sprite, error, info, is_key_down, main_camera_mut, draw_rect_outline, draw_sprite, error, info, is_key_down, main_camera_mut,
texture_id, vec2, EngineContext, IVec2, KeyCode, Vec2, RED, WHITE play_sound_id, stop_sound_id, texture_id, vec2, EngineContext, IVec2, KeyCode, Vec2,
RED, WHITE
}; };
use std::time::Instant; use std::time::Instant;
use worldgen::MovementCost; use worldgen::MovementCost;
@ -50,9 +52,20 @@ pub fn draw(state: &State, _ctx: &EngineContext<'_>) {
); );
} }
fn update_move_player(state: &mut State, _ctx: &mut EngineContext<'_>) { fn update_move_player(state: &mut State, ctx: &mut EngineContext<'_>) {
let now = Instant::now(); let now = Instant::now();
if !state.overworld.sound_playing {
if let Some(house) = state.house_mut(ctx) {
if house.sound_playing {
stop_sound_id(ASSETS.music.mesmerizing_galaxy_loop);
house.sound_playing = false;
}
}
play_sound_id(ASSETS.music.galactic_rap_speedup);
state.overworld.sound_playing = true;
}
// Are there any pending position updates? If so, we ignore all user input and execute // Are there any pending position updates? If so, we ignore all user input and execute
// the pending updates. // the pending updates.
if state.ghost.overworld_movement_pending != Vec2::ZERO { if state.ghost.overworld_movement_pending != Vec2::ZERO {

View file

@ -422,7 +422,9 @@ impl Chunk {
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Overworld { pub struct Overworld {
chunks: HashMap<IVec2, Chunk> chunks: HashMap<IVec2, Chunk>,
pub sound_playing: bool
} }
fn world_to_chunk_and_local_coords(world_coords: IVec2) -> (IVec2, UVec2) { fn world_to_chunk_and_local_coords(world_coords: IVec2) -> (IVec2, UVec2) {

View file

@ -59,10 +59,12 @@ impl State {
self.houses.get(&self.get_house_pos()?) self.houses.get(&self.get_house_pos()?)
} }
fn house_mut(&mut self, ctx: &mut EngineContext<'_>) -> &mut HouseState { fn house_mut(&mut self, ctx: &mut EngineContext<'_>) -> Option<&mut HouseState> {
Some(
self.houses self.houses
.entry(self.get_house_pos().unwrap()) .entry(self.get_house_pos()?)
.or_insert_with(|| HouseState::generate_new_house(ctx)) .or_insert_with(|| HouseState::generate_new_house(ctx))
)
} }
} }