From bb3c850c95f0206c935a2db3d31a3f207bbcf867 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Fri, 26 Jul 2024 22:54:05 +0930 Subject: [PATCH] [BGM] Make the debug menus play appropriate music :) --- main.gd | 9 +++++++-- scripts/managers/MusicManager.gd | 13 +++++++++---- test/debug_menu.gd | 3 +++ test/worldmap_system.gd | 6 ++++++ widgets/BattleScene.gd | 4 ++++ widgets/FileSelectWeb.gd | 6 ++++++ 6 files changed, 35 insertions(+), 6 deletions(-) diff --git a/main.gd b/main.gd index f5eca3f..be8c346 100644 --- a/main.gd +++ b/main.gd @@ -54,16 +54,21 @@ func push_menu(menu_type): return remove_child(menus[active_menu_stack[-1]]) active_menu_stack.append(menu_type) - add_child(menus[menu_type]) + self._present_menu(menus[menu_type]) func pop_menu(): if len(active_menu_stack) > 0: remove_child(menus[active_menu_stack.pop_back()]) if len(active_menu_stack) > 0: - add_child(menus[active_menu_stack[-1]]) + self._present_menu(menus[active_menu_stack[-1]]) else: push_menu(Menu.LOADER) +func _present_menu(menu) -> void: + add_child(menu) + if menu.has_method('_on_menu_presented'): + menu._on_menu_presented() + func _input(event: InputEvent) -> void: if event is InputEventKey and event.pressed: match event.scancode: diff --git a/scripts/managers/MusicManager.gd b/scripts/managers/MusicManager.gd index 1bafe8f..746a64d 100644 --- a/scripts/managers/MusicManager.gd +++ b/scripts/managers/MusicManager.gd @@ -45,20 +45,25 @@ func stop() -> void: self.queued_awaiting_render_bgm = '' self.current_bgm = '' -func play_bgm(key: String) -> void: +func play_bgm(key: String, target_time: float = -1.0) -> void: print('@%dms - Playing %s' % [get_ms(), key]) + _evict_pcm_cache() var bgm_id := int(key.substr(3, 2)) if not (bgm_id in audio_renderer.cached_midis): self.queue_prerender_bgm(bgm_id) # TODO: crossfade - var target_time := 0.0 if self.music_player_1.playing and self.current_bgm.substr(0, 5) == key.substr(0, 5): # Try live transition var tempo_thou := int(key.substr(6)) var old_tempo_thou := int(self.current_bgm.substr(6)) var old_playback_pos: float = self.music_player_1.get_playback_position() - target_time = old_playback_pos * old_tempo_thou / tempo_thou - print('Old temposcale %d, New temposcale %d, Old pos %.2f, New pos %.2f' % [old_tempo_thou, tempo_thou, old_playback_pos, target_time]) + if target_time < 0.0: # Negative (default) time means automatic continuation + if old_tempo_thou == tempo_thou: + return # Don't even need to refresh LRU since it can only be most recent anyway + target_time = old_playback_pos * old_tempo_thou / tempo_thou + print('Old temposcale %d, New temposcale %d, Old pos %.2f, New pos %.2f' % [old_tempo_thou, tempo_thou, old_playback_pos, target_time]) + if target_time < 0.0: + target_time = 0.0 if key in self.cached_bgms: self.music_player_1.stream = self.cached_bgms[key] self.music_player_1.play(target_time) diff --git a/test/debug_menu.gd b/test/debug_menu.gd index 422fe88..94d64b5 100644 --- a/test/debug_menu.gd +++ b/test/debug_menu.gd @@ -14,5 +14,8 @@ func _ready() -> void: btn.connect('pressed', self, '_on_btn_pressed', [key]) buttons.add_child(btn) +func _on_menu_presented() -> void: # Called by main.gd when a menu is presented in front of the rest + MusicManager.play_bgm('BGM08-1000') # Prelude + func _on_btn_pressed(menu) -> void: emit_signal('button_pressed', menu) diff --git a/test/worldmap_system.gd b/test/worldmap_system.gd index 0f2422a..0863251 100644 --- a/test/worldmap_system.gd +++ b/test/worldmap_system.gd @@ -5,6 +5,7 @@ var worldmap_shader_mat := preload('res://worldmap_palette_mat.tres') onready var minimap := $tr_minimap var current_map_id := 0 +const MAP_BGM_KEYS := ['BGM35-1000', 'BGM39-1000', 'BGM63-1000', 'BGM30-1000', 'BGM30-1000'] var map_images := [null, null, null, null, null] var map_textures := [null, null, null, null, null] var map_regional_replacement_amounts := [0, 0, 0, 0, 0] @@ -36,6 +37,7 @@ func _set_map(id: int) -> void: print_debug('Invalid map ID %d' % id) return self.current_map_id = id + MusicManager.play_bgm(MAP_BGM_KEYS[self.current_map_id]) var tileset = map_tilesets[id] self.current_texture = self.map_textures[self.current_map_id] self.material.set_shader_param('palette', SpriteLoader.worldmap_palette_textures[tileset]) @@ -99,8 +101,12 @@ func _ready() -> void: minimap.modulate.a8 = 192 minimap.rect_scale = Vector2.ONE * 0.125 minimap.rect_position = Vector2(8, 168) + self.pos = Vector2(156, 150) _set_map(0) +func _on_menu_presented() -> void: + MusicManager.play_bgm(MAP_BGM_KEYS[self.current_map_id], 0.0) + func handle_movement_input(delta: float, quantize:=8.0) -> void: delta = round(delta*quantize)/quantize if Input.is_physical_key_pressed(KEY_RIGHT): diff --git a/widgets/BattleScene.gd b/widgets/BattleScene.gd index 9b68aeb..92679a2 100644 --- a/widgets/BattleScene.gd +++ b/widgets/BattleScene.gd @@ -13,6 +13,10 @@ func _ready() -> void: background.material = battle_bg_mat set_bg(1) +const RANDOM_BGMS_PLAYLIST = [34, 43, 1, 9, 64] +func _on_menu_presented() -> void: + MusicManager.play_bgm('BGM%02d-1000'%RANDOM_BGMS_PLAYLIST.pick_random(), 0) + func set_bg(index: int) -> void: self.bg = SpriteLoader.battle_backgrounds[index] self.pal_l = len(self.bg.palette_texs) diff --git a/widgets/FileSelectWeb.gd b/widgets/FileSelectWeb.gd index 175b940..b78fb8e 100644 --- a/widgets/FileSelectWeb.gd +++ b/widgets/FileSelectWeb.gd @@ -150,6 +150,11 @@ func _upload_file(filename: String, file_type: String, data): # data is Javascr func _files_dropped(filenames: PoolStringArray, screen_idx: int) -> void: print('Files Dropped signal: screen=%d, files='%screen_idx, filenames) for filename in filenames: + var dir := Directory.new() + dir.open('user://') + var new_filename = 'user://' + filename.rsplit('/', true, 1)[1] + var err = dir.copy(filename, new_filename) + print('Attempted to copy %s to %s - error code %s' % [filename, new_filename, globals.ERROR_CODE_STRINGS[err]]) var ext: String = filename.rsplit('.', true, 1)[1] if ext in ALLOWED_EXTS: var file := File.new() @@ -168,6 +173,7 @@ func _files_dropped(filenames: PoolStringArray, screen_idx: int) -> void: func _ready() -> void: if OS.get_name() != 'HTML5' or !OS.has_feature('JavaScript'): return + print('is_userfs_persistent() == %s'%OS.is_userfs_persistent()) init_labels() update_view() btn_load.connect('pressed', self, 'activate_current_entry')