// Copyright (c) 2026 GeWuYou // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. using GFramework.Game.Abstractions.scene; using GFramework.Godot.extensions; using Godot; namespace GFramework.Godot.scene; /// /// 场景行为基类,封装通用的场景生命周期管理逻辑。 /// 提供对 Node 类型场景节点的统一管理,包括加载、进入、暂停、恢复、退出、卸载等操作。 /// /// Node 类型的场景节点。 public abstract class SceneBehaviorBase : ISceneBehavior where T : Node { /// /// 场景的唯一标识键。 /// private readonly string _key; /// /// IScene 接口引用(如果节点实现了该接口)。 /// private readonly IScene? _scene; /// /// 场景节点的所有者实例。 /// protected readonly T Owner; /// /// 场景是否处于活跃状态。 /// private bool _isActive; /// /// 场景是否已加载。 /// private bool _isLoaded; /// /// 场景是否正在过渡中。 /// private bool _isTransitioning; /// /// 初始化 SceneBehaviorBase 实例。 /// /// 场景节点的所有者实例。 /// 场景的唯一标识键。 protected SceneBehaviorBase(T owner, string key) { Owner = owner; _key = key; _scene = owner as IScene; } #region ISceneBehavior 实现 /// /// 获取场景的唯一标识键。 /// public string Key => _key; /// /// 获取场景是否已加载完成的状态。 /// public bool IsLoaded => _isLoaded; /// /// 获取场景是否处于活跃运行状态。 /// public bool IsActive => _isActive; /// /// 获取场景是否正在进行切换操作。 /// public bool IsTransitioning => _isTransitioning; /// /// 获取场景节点是否有效。 /// public bool IsAlive => Owner.IsValidNode(); /// /// 异步加载场景所需资源。 /// 在场景正式进入前调用,负责预加载场景所需的各类资源。 /// /// 场景进入参数,可能包含初始化数据或上下文信息。 /// 表示加载操作完成的ValueTask。 public virtual async ValueTask OnLoadAsync(ISceneEnterParam? param) { _isTransitioning = true; // 调用可选接口 if (_scene != null) await _scene.OnLoadAsync(param); _isLoaded = true; _isTransitioning = false; } /// /// 异步处理场景正式进入逻辑。 /// 在资源加载完成后调用,启动场景的主要运行逻辑。 /// /// 表示进入操作完成的ValueTask。 public virtual async ValueTask OnEnterAsync() { _isTransitioning = true; if (_scene != null) await _scene.OnEnterAsync(); _isActive = true; _isTransitioning = false; } /// /// 异步处理场景暂停逻辑。 /// 当场景被其他场景覆盖或失去焦点时调用。 /// /// 表示暂停操作完成的ValueTask。 public virtual async ValueTask OnPauseAsync() { if (_scene != null) await _scene.OnPauseAsync(); // 暂停处理 Owner.SetProcess(false); Owner.SetPhysicsProcess(false); Owner.SetProcessInput(false); _isActive = false; } /// /// 异步处理场景恢复逻辑。 /// 当场景重新获得焦点或从暂停状态恢复时调用。 /// /// 表示恢复操作完成的ValueTask。 public virtual async ValueTask OnResumeAsync() { if (Owner.IsInvalidNode()) return; if (_scene != null) await _scene.OnResumeAsync(); // 恢复处理 Owner.SetProcess(true); Owner.SetPhysicsProcess(true); Owner.SetProcessInput(true); _isActive = true; } /// /// 异步处理场景退出逻辑。 /// 在场景即将被替换或关闭时调用,执行清理工作。 /// /// 表示退出操作完成的ValueTask。 public virtual async ValueTask OnExitAsync() { _isTransitioning = true; if (_scene != null) await _scene.OnExitAsync(); _isActive = false; } /// /// 异步卸载场景资源。 /// 在场景完全退出后调用,释放占用的内存和资源。 /// /// 表示卸载操作完成的ValueTask。 public virtual async ValueTask OnUnloadAsync() { if (_scene != null) await _scene.OnUnloadAsync(); // 释放节点 Owner.QueueFreeX(); _isLoaded = false; _isTransitioning = false; } #endregion }