Play background sound #19

Merged
msrd0 merged 3 commits from play-sound into main 2024-07-07 16:13:39 +00:00
5 changed files with 54 additions and 15 deletions
Showing only changes of commit eb2505f0db - Show all commits

View file

@ -3,7 +3,11 @@ mod grid;
mod player;
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 log::error;
use player::Player;
@ -23,7 +27,9 @@ pub struct HouseState {
/// The energy level remaining in the house. Should decrease by itself, and much
/// faster when inhabited by the ghost.
pub charge: f32,
pub max_charge: f32
pub max_charge: f32,
pub sound_playing: bool
}
impl HouseState {
@ -43,14 +49,17 @@ impl HouseState {
rooms,
player,
human_layer: false,
// TODO this should be lower depending on the time elapsed
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 {
error!("How can I render a house when I'm not in one?!?");
return;
@ -70,14 +79,27 @@ pub fn draw(state: &crate::State, _ctx: &comfy::EngineContext<'_>) {
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();
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();
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.current_room_id < (house.room_count - 1) {
house.current_room_id += 1;

View file

@ -1,11 +1,13 @@
use crate::{
activities::Activity,
assets::ASSETS,
game::{ZLayer, GHOST_DISCHARGE_RATE, GHOST_DISCHARGE_RATE_MOVEMENT},
State
};
use comfy::{
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 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();
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
// the pending updates.
if state.ghost.overworld_movement_pending != Vec2::ZERO {

View file

@ -422,7 +422,9 @@ impl Chunk {
#[derive(Debug, Default)]
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) {

View file

@ -1,9 +1,9 @@
use crate::{
activities::{house, overworld, Activity},
assets::Assets,
assets::{Assets, ASSETS},
State
};
use comfy::{EngineContext, Vec2};
use comfy::{play_sound, play_sound_id, EngineContext, Vec2};
use std::{
ops::{Add, Sub},
time::Instant

View file

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