Files
mtgodot-poc/project/app_lifecycle.gd
T
shenleiandClaude Sonnet 5 9732f2799e 40250 classic: increment 50 (W4) — map BGM selection + background volume save/restore
§9.1 map BGM selection (bgm_director.gd):
- server bgm_changed(name, volume): non-empty name plays it as field music;
  empty name falls back to musicInfo.fieldMusic (METIN2THEMA = "M2BG.mp3")
- volume < 0 (server unspecified, GC_MAIN_CHARACTER3_BGM) -> default
  1/5*0.1 = 0.02; otherwise server volume; volume applied before play_bgm()
  fade-in (matches game.py:190 SetMusicVolume/SetSoundVolume then FadeInMusic)
- connect M2Client.world_reset -> re-run selection on map change
- new musicinfo.gd: METIN2THEMA/loginMusic/createMusic/selectMusic/fieldMusic
  constants + save/load_last_play_field_music -> user://BGM/lastplay.inf

§9.2 background/foreground volume save-restore:
- audio.gd: save_volume()/restore_volume() (backup fields echo reference
  m_fBackupMusicVolume / m_fBackupSoundVolume) + set_music_volume()
- app_lifecycle.gd: _enter_background() uses save_volume() + mute instead of
  stop_bgm(); _exit_background() calls restore_volume(); _bgm_name never cleared

§9.3 lifecycle ownership: DOC ONLY — design confirmed, deferred to Phase 2 / W0
(AppFlow sole lifecycle owner; M2Client drops NOTIFICATION_* keeps suspend/resume).
game_scene.gd / app_flow.gd / m2_client.* left untouched per workflow constraints.

Tests: bgm_test.gd (new) PASS; gamescene_test / netplay_test / p9_test green.
Docs: CLIENT-GAP.md increment 50 bullet + W4 progress row; CLIENT-GAP-FIX.md
§9.1 §9.2 状态/改动/测试/验收, §9.3 deferred note, C.5 batch record.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yvAPqPivoY7vmBbzgqK4W
2026-09-02 15:31:58 +09:00

95 lines
3.3 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# AppLifecycle (F5) —— 移动端应用生命周期集中处理。
#
# 把 SceneTree 转发来的 MainLoop 通知收成信号,并做默认动作:后台时暂停游戏
# 逻辑 / 降帧 / 停 BGM,恢复时还原;iOS 内存告警时广播让各缓存自清。渲染上下文
# 丢失(Android Vulkan surface)由 Godot 自己重建,我们持有的 GPU 资源都由
# RenderingServer 托管,无需干预。
#
# 用法:
# var life := preload("res://app_lifecycle.gd").new()
# add_child(life) # 越早越好(autoload 亦可)
# life.bind(m2client, audio) # 可选:自动接常见消费方
# life.paused.connect(_on_bg); life.resumed.connect(_on_fg)
extends Node
signal paused # 进入后台(NOTIFICATION_APPLICATION_PAUSED
signal resumed # 回到前台
signal focus_changed(focused: bool)
signal memory_warning # iOS 内存压力
signal back_requested # Android 返回键
signal close_requested # 窗口关闭请求(桌面)
## 后台时是否 get_tree().paused = true(默认开;纯观战/录像可关)
var pause_tree_on_background := true
## 后台时把 max_fps 压到这个值省电(0 = 不改)
var background_max_fps := 8
var _bound_client: Node
var _bound_audio: Node
var _saved_max_fps := 0
var _bg := false
func _ready() -> void:
# 通知在暂停树时也要能收到
process_mode = Node.PROCESS_MODE_ALWAYS
func bind(m2client: Node = null, audio: Node = null) -> void:
_bound_client = m2client
_bound_audio = audio
func is_backgrounded() -> bool:
return _bg
func _notification(what: int) -> void:
match what:
NOTIFICATION_APPLICATION_PAUSED:
_enter_background()
NOTIFICATION_APPLICATION_RESUMED:
_exit_background()
NOTIFICATION_APPLICATION_FOCUS_IN, NOTIFICATION_WM_WINDOW_FOCUS_IN:
focus_changed.emit(true)
NOTIFICATION_APPLICATION_FOCUS_OUT, NOTIFICATION_WM_WINDOW_FOCUS_OUT:
focus_changed.emit(false)
NOTIFICATION_OS_MEMORY_WARNING:
memory_warning.emit()
NOTIFICATION_WM_GO_BACK_REQUEST:
back_requested.emit()
NOTIFICATION_WM_CLOSE_REQUEST:
close_requested.emit()
func _enter_background() -> void:
if _bg:
return
_bg = true
if background_max_fps > 0:
_saved_max_fps = Engine.max_fps
Engine.max_fps = background_max_fps
# §9.2:不停 BGM,改为「保存音量 + 静音」(对标 CSoundManager::SaveVolume 后
# SetMusicVolume(0))。_bgm_name 不清空,回前台恢复音量即可同一首继续播。
if _bound_audio and _bound_audio.has_method("save_volume"):
_bound_audio.save_volume()
if _bound_audio.has_method("set_music_volume"):
_bound_audio.set_music_volume(0.0)
elif _bound_audio and _bound_audio.has_method("stop_bgm"):
_bound_audio.stop_bgm()
if _bound_client and _bound_client.has_method("suspend"):
_bound_client.suspend()
if pause_tree_on_background:
get_tree().paused = true
paused.emit()
func _exit_background() -> void:
if not _bg:
return
_bg = false
if pause_tree_on_background:
get_tree().paused = false
if background_max_fps > 0:
Engine.max_fps = _saved_max_fps
# §9.2:还原后台前保存的音量。
if _bound_audio and _bound_audio.has_method("restore_volume"):
_bound_audio.restore_volume()
if _bound_client and _bound_client.has_method("resume"):
_bound_client.resume()
resumed.emit()