307 lines
10 KiB
GDScript
307 lines
10 KiB
GDScript
class_name GroundLoader extends Node2D
|
||
|
||
@export_group("Scene")
|
||
@export var ignore_archive := false
|
||
@export var current_scene := "c02_s01"
|
||
@export var entrance_portal := "left"
|
||
@export var debug_reload := false:
|
||
set(new_val):
|
||
debug_reload = false
|
||
if is_node_ready() and current_scene and entrance_portal:
|
||
transition_to_scene(current_scene, entrance_portal, 0.0)
|
||
# 强制覆盖 archive 记录
|
||
@export var force_archive_scene := ""
|
||
@export var force_archive_portal := ""
|
||
|
||
@onready var mask_layer := %MaskLayer as CanvasLayer
|
||
@onready var mask := %Mask as ColorRect
|
||
|
||
var has_entered := false
|
||
var ground: Ground2D
|
||
|
||
# 场景名字映射到路径
|
||
static var GROUND_SCENE_PATH_DICT = {
|
||
"c01_s05": "uid://dlx5xxbg53rb8",
|
||
"c01_s06": "uid://bx16c8nn32f40",
|
||
"c01_s07": "uid://ds2iyfndwamiy",
|
||
"c01_s08": "uid://cwu4dhayra8pg",
|
||
"c01_s09": "uid://c777lv8mjojcw",
|
||
"c01_s10": "uid://be57l2o3vxxtm",
|
||
"c01_s11": "uid://coiumaaenimbc",
|
||
"c01_s12": "uid://bol5hl68pbpgq",
|
||
"c02_s01": "uid://bbs7yy5aofw1v",
|
||
"c02_s02": "uid://brck77w81fhvc",
|
||
"c02_s03": "uid://djc2uaefhmu7",
|
||
"c02_s04": "uid://bivc5cdap370p",
|
||
"c02_s05": "uid://cp8d3ag5nbjq0",
|
||
"c02_s06": "uid://cootarwb44vvh",
|
||
"c02_s07": "uid://t4xjt774ngwh",
|
||
"c02_s08": "uid://ce2vyyg2reg52",
|
||
"c02_s09": "uid://ryups1dnwdto",
|
||
"c02_s10": "uid://dny21yhtuteap",
|
||
"c02_s11": "uid://dq41rvwl5hyrk", # 注:该场景合并在了 c02_s03 院子中
|
||
"c02_s12": "uid://da4cuf2i3nwpj",
|
||
"c02_s13": "uid://bvjutch6jex0v",
|
||
"c02_s14": "uid://d0p4x5st2r315",
|
||
"c02_s15": "uid://b21p53g42j2nt",
|
||
"c02_s16": "uid://22hc3oe8t0id",
|
||
"c02_s17": "uid://cbr6gbgrl2wb1",
|
||
"c02_s18": "uid://d27gv3pbkn4b8",
|
||
"c03_s01": "uid://dlrbhfvnd3cs0", # 三楼
|
||
"c03_s02": "uid://rkro7u5wd3t1", # 三楼内侧
|
||
"c03_s03": "uid://bsqt2c061fmin", # 瞎子理发店
|
||
}
|
||
|
||
|
||
func _ready() -> void:
|
||
mask.visible = true
|
||
mask.color.a = 0.0
|
||
mask_layer.layer = GlobalConfig.CANVAS_LAYER_GROUND_MASK
|
||
# mask layer 独立的 always 处理模式,可以保证转场正常运行
|
||
# toggle_mask = mask_layer.toggle_mask
|
||
ground = get_node_or_null("Ground") as Ground2D
|
||
if ground:
|
||
print("GroundLoader remove old ground:", ground.scene_name)
|
||
# remove_child(ground)
|
||
ground.queue_free()
|
||
# load save
|
||
if not ignore_archive:
|
||
_load_save()
|
||
if current_scene and entrance_portal:
|
||
# 首次进入渐隐效果
|
||
transition_to_scene(current_scene, entrance_portal)
|
||
# transition_to_scene(current_scene, entrance_portal, 0.0)
|
||
|
||
|
||
# # var toggle_mask:Callable
|
||
# func toggle_mask(display: bool, mask_color: Color, wait_time: float) -> Tween:
|
||
# return mask_layer.toggle_mask(display, mask_color, wait_time)
|
||
|
||
var display_start_sec = 0.0
|
||
|
||
|
||
# wait_time 包含 ease in + wait + ease out 完整时长
|
||
# ease duration = min(ease_min_duration, wait_time * 0.5)
|
||
func toggle_mask(
|
||
display: bool, wait_time: float, ease_min_duration := 0.3, mask_color := Color.BLACK
|
||
) -> Tween:
|
||
var tween = get_tree().create_tween()
|
||
mask_color.a = mask.color.a
|
||
mask.color = mask_color
|
||
var duration = min(ease_min_duration, wait_time * 0.5)
|
||
if display:
|
||
display_start_sec = Time.get_ticks_msec() * 0.001
|
||
tween.tween_property(mask, "color:a", 1.0, duration).set_trans(Tween.TRANS_CUBIC)
|
||
else:
|
||
# 转场至少 0.6s, 除去 0.3s 最后的淡出,需要 0.3s 的等待时间(包含 mask 的淡入)
|
||
if wait_time:
|
||
var time = Time.get_ticks_msec() * 0.001
|
||
wait_time = max(wait_time + display_start_sec - time - 0.3, 0.0)
|
||
if wait_time:
|
||
tween.tween_interval(wait_time)
|
||
tween.tween_property(mask, "color:a", 0.0, duration).set_trans(Tween.TRANS_CUBIC)
|
||
return tween
|
||
|
||
|
||
func _load_save():
|
||
# 强制覆盖 archive 记录
|
||
if force_archive_scene or force_archive_portal:
|
||
current_scene = force_archive_scene
|
||
entrance_portal = force_archive_portal
|
||
return
|
||
if not Engine.is_editor_hint():
|
||
if ArchiveManager.archive.current_scene:
|
||
current_scene = ArchiveManager.archive.current_scene
|
||
if ArchiveManager.archive.entrance_portal:
|
||
entrance_portal = ArchiveManager.archive.entrance_portal
|
||
|
||
|
||
func transition_to_scene(scene_name: String, portal: String, wait_time := 1.4) -> void:
|
||
if ground:
|
||
print("GroundLoader transition_to_scene: pause prev ground.")
|
||
# 先发送,再暂停,允许 sfx 等节点执行 ease out
|
||
SceneManager.ground_transition_pre_paused.emit()
|
||
ground.process_mode = Node.PROCESS_MODE_DISABLED
|
||
if GlobalConfig.DEBUG:
|
||
# print reenter lock status
|
||
print("GroundLoader transition_to_scene: reenter lock status: ", ground.reenter_lock)
|
||
var scene_path = GROUND_SCENE_PATH_DICT.get(scene_name)
|
||
if scene_path:
|
||
current_scene = scene_name
|
||
entrance_portal = portal
|
||
# 优先更新 archive,使 ground 可以访问自己的 current_scene 键值
|
||
if not Engine.is_editor_hint():
|
||
_update_archive()
|
||
if wait_time > 0.0:
|
||
# 转场效果,在 _load_ground_node 之前播放
|
||
var tween = toggle_mask(true, wait_time)
|
||
tween.tween_callback(_do_transition.call_deferred.bind(scene_name))
|
||
_allow_ground_start = false
|
||
# 等到 toggle_mask 结束,再重置 freeze 状态
|
||
toggle_mask(false, wait_time).tween_callback(func(): _allow_ground_start = true)
|
||
else:
|
||
_allow_ground_start = true
|
||
_do_transition.call_deferred(scene_name)
|
||
else:
|
||
print("Scene not found: " + scene_name)
|
||
|
||
|
||
var _frozen_start_time_ms: int
|
||
var _allow_ground_start := false:
|
||
set(val):
|
||
_allow_ground_start = val
|
||
if ground:
|
||
if val:
|
||
if ground.process_mode != Node.PROCESS_MODE_INHERIT:
|
||
# ground_start 信号
|
||
SceneManager.ground_start.emit()
|
||
ground.process_mode = Node.PROCESS_MODE_INHERIT
|
||
print(
|
||
"GroundLoader _allow_ground_start: unfrozen. frozen duration(ms):",
|
||
Time.get_ticks_msec() - _frozen_start_time_ms
|
||
)
|
||
# else:
|
||
# print("GroundLoader _allow_ground_start: frozen")
|
||
# ground.process_mode = Node.PROCESS_MODE_DISABLED
|
||
# _frozen_start_time_ms = Time.get_ticks_msec()
|
||
|
||
|
||
func _update_archive():
|
||
ArchiveManager.archive.current_scene = current_scene
|
||
ArchiveManager.archive.entrance_portal = entrance_portal
|
||
|
||
|
||
func _do_transition(scene_name: String) -> void:
|
||
# SceneManager.freeze_player(0)
|
||
print("GroundLoader Transition to scene:", scene_name, "portal:", entrance_portal)
|
||
ground = get_node_or_null("Ground") as Ground2D
|
||
if ground:
|
||
# 防止命名冲突
|
||
remove_child(ground)
|
||
ground.queue_free()
|
||
# 先设置 ground,再添加到场景中
|
||
# 因为 ground 在 enter_tree 时会用到 SceneManager 的方法
|
||
# 其中间接用到了 GroundLoader 的 ground
|
||
ground = _load_ground_node(scene_name)
|
||
if not _allow_ground_start:
|
||
ground.process_mode = Node.PROCESS_MODE_DISABLED
|
||
print("GroundLoader not _allow_ground_start: frozen (delayed)")
|
||
_frozen_start_time_ms = Time.get_ticks_msec()
|
||
_add_ground()
|
||
if _allow_ground_start:
|
||
# 如果不阻塞,直接 ground_start 信号
|
||
SceneManager.ground_start.emit()
|
||
# 预先加载邻居场景
|
||
_post_transition()
|
||
if GlobalConfig.DEBUG and not Engine.is_editor_hint():
|
||
_watch_scene_update()
|
||
|
||
|
||
func _add_ground():
|
||
ground.ready.connect(SceneManager.ground_ready.emit.bind(ground))
|
||
ground.name = "Ground"
|
||
# 在 add child 之前,调整 ground 内部元素属性,在 on ground ready 前设置完成
|
||
if not Engine.is_editor_hint():
|
||
# 更新玩家位置
|
||
if not has_entered:
|
||
_update_player_position_from_archive()
|
||
else:
|
||
# move player to portal
|
||
ground.move_player_to_portal(entrance_portal)
|
||
if GlobalConfig.DEBUG:
|
||
print(
|
||
"GroundLoader add_ground finished:",
|
||
ground.scene_name,
|
||
" player.pos=",
|
||
ground.get_player().global_position
|
||
)
|
||
add_child(ground)
|
||
# ready 后,再整体重置 camera 位置
|
||
if not Engine.is_editor_hint():
|
||
ground.get_camera().reset_position_immediately()
|
||
has_entered = true
|
||
|
||
|
||
func _update_player_position_from_archive():
|
||
if ignore_archive or Engine.is_editor_hint():
|
||
return
|
||
var player = ground.get_player() as MainPlayer
|
||
player.global_position.x = ArchiveManager.archive.player_global_position_x
|
||
player.set_facing_direction(ArchiveManager.archive.player_direction)
|
||
ground.reset_player_y()
|
||
|
||
|
||
func _load_ground_node(scene_name: String) -> Ground2D:
|
||
if not GROUND_SCENE_PATH_DICT.has(scene_name):
|
||
return null
|
||
var path = GROUND_SCENE_PATH_DICT[scene_name]
|
||
var scene: PackedScene
|
||
if ResourceLoader.load_threaded_get_status(path) == ResourceLoader.THREAD_LOAD_LOADED:
|
||
scene = ResourceLoader.load_threaded_get(path) as PackedScene
|
||
else:
|
||
scene = ResourceLoader.load(path) as PackedScene
|
||
if scene:
|
||
var instance = scene.instantiate() as Node2D
|
||
var ground_node = instance.get_child(0)
|
||
instance.remove_child(ground_node)
|
||
ground_node.owner = null
|
||
instance.queue_free()
|
||
return ground_node
|
||
return null
|
||
|
||
|
||
# 读取 portals,预加载邻居场景
|
||
func _post_transition():
|
||
if ground:
|
||
var scene_names = []
|
||
var deploy_layer = ground.get_node("DeployLayer")
|
||
if deploy_layer:
|
||
for node in deploy_layer.get_children():
|
||
var portal = node as Portal2D
|
||
if not portal or not portal.target_scene:
|
||
continue
|
||
if GROUND_SCENE_PATH_DICT.has(portal.target_scene):
|
||
scene_names.append(portal.target_scene)
|
||
if scene_names:
|
||
for scene_name in scene_names:
|
||
ResourceLoader.load_threaded_request(GROUND_SCENE_PATH_DICT[scene_name])
|
||
if GlobalConfig.DEBUG:
|
||
print("preload neighbor scenes:", scene_names)
|
||
|
||
|
||
var update_watcher: Timer
|
||
var last_modify_time = 0
|
||
|
||
|
||
# DEBUG 时重新加载资源
|
||
func _watch_scene_update():
|
||
var scene_path = GROUND_SCENE_PATH_DICT[current_scene]
|
||
if scene_path:
|
||
last_modify_time = FileAccess.get_modified_time(scene_path)
|
||
if not update_watcher:
|
||
update_watcher = Timer.new()
|
||
update_watcher.wait_time = 1
|
||
update_watcher.one_shot = false
|
||
add_child(update_watcher)
|
||
update_watcher.start()
|
||
else:
|
||
# remove all connections
|
||
for c in update_watcher.timeout.get_connections():
|
||
update_watcher.timeout.disconnect(c.callable)
|
||
update_watcher.timeout.connect(_check_scene_update.bind(scene_path))
|
||
|
||
|
||
func _check_scene_update(scene_path):
|
||
var modify = FileAccess.get_modified_time(scene_path)
|
||
if modify != last_modify_time:
|
||
last_modify_time = modify
|
||
_on_resources_reload(scene_path)
|
||
|
||
|
||
func _on_resources_reload(res):
|
||
print("resources_reload processing:", res)
|
||
if not Engine.is_editor_hint() and res.ends_with(".tscn"):
|
||
ArchiveManager.save_all()
|
||
has_entered = false
|
||
transition_to_scene.call_deferred(current_scene, entrance_portal, 0.0)
|