diff --git a/GFramework.Core.Abstractions/scene/ISceneLoader.cs b/GFramework.Game.Abstractions/scene/ISceneRoot.cs similarity index 59% rename from GFramework.Core.Abstractions/scene/ISceneLoader.cs rename to GFramework.Game.Abstractions/scene/ISceneRoot.cs index b55eb71..4947608 100644 --- a/GFramework.Core.Abstractions/scene/ISceneLoader.cs +++ b/GFramework.Game.Abstractions/scene/ISceneRoot.cs @@ -11,29 +11,21 @@ // See the License for the specific language governing permissions and // limitations under the License. -using GFramework.Core.Abstractions.utility; - -namespace GFramework.Core.Abstractions.scene; +namespace GFramework.Game.Abstractions.scene; /// -/// 定义场景加载器的接口,用于管理场景的加载、替换和卸载操作。 +/// 定义场景根接口,用于管理场景的加载和卸载操作。 /// -public interface ISceneLoader : IUtility +public interface ISceneRoot { - /// - /// 获取当前加载的场景对象。 - /// - /// 当前场景对象,如果未加载任何场景则返回 null。 - T? Current { get; } - /// /// 替换当前场景为指定键对应的场景。 /// - /// 场景的唯一标识符或键值。 + /// 场景的唯一标识符,用于定位要加载的场景。 void Replace(string key); /// - /// 卸载当前加载的场景。 + /// 卸载当前场景。 /// void Unload(); } \ No newline at end of file diff --git a/GFramework.Game.Abstractions/scene/ISceneRouter.cs b/GFramework.Game.Abstractions/scene/ISceneRouter.cs new file mode 100644 index 0000000..8e6076c --- /dev/null +++ b/GFramework.Game.Abstractions/scene/ISceneRouter.cs @@ -0,0 +1,43 @@ +// 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. + +namespace GFramework.Game.Abstractions.scene; + +/// +/// 定义场景路由接口,用于管理场景的切换、卸载以及根节点绑定。 +/// +public interface ISceneRouter +{ + /// + /// 获取当前场景的唯一标识符(键)。 + /// + /// 当前场景的键,如果未加载任何场景则返回 null。 + string? CurrentKey { get; } + + /// + /// 替换当前场景为指定键对应的场景。 + /// + /// 目标场景的唯一标识符(键)。 + void Replace(string sceneKey); + + /// + /// 卸载当前场景。 + /// + void Unload(); + + /// + /// 将指定的场景根节点与当前路由进行绑定。 + /// + /// 需要绑定的场景根节点。 + void BindRoot(ISceneRoot root); +} \ No newline at end of file diff --git a/GFramework.Game/scene/SceneRouterBase.cs b/GFramework.Game/scene/SceneRouterBase.cs new file mode 100644 index 0000000..88af0c2 --- /dev/null +++ b/GFramework.Game/scene/SceneRouterBase.cs @@ -0,0 +1,92 @@ +// 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.Core.system; +using GFramework.Game.Abstractions.scene; + +namespace GFramework.Game.scene; + +/// +/// 场景路由基类,提供场景切换和卸载的基础功能。 +/// 实现了 接口,用于管理场景的加载、替换和卸载操作。 +/// +public abstract class SceneRouterBase + : AbstractSystem, ISceneRouter +{ + /// + /// 当前绑定的场景根节点。 + /// + protected ISceneRoot? Root; + + /// + /// 当前激活场景的键值。 + /// + public string? CurrentKey { get; private set; } + + /// + /// 绑定场景根节点。 + /// + /// 要绑定的场景根节点。 + public void BindRoot(ISceneRoot root) + { + Root = root; + } + + /// + /// 替换当前场景为指定键值的新场景。 + /// 在替换前后会调用相应的虚方法以支持扩展逻辑。 + /// + /// 目标场景的键值。 + public void Replace(string sceneKey) + { + // 调用替换前的钩子方法 + OnBeforeReplace(sceneKey); + + // 执行场景替换操作 + Root!.Replace(sceneKey); + + // 更新当前场景键值 + CurrentKey = sceneKey; + + // 调用替换后的钩子方法 + OnAfterReplace(sceneKey); + } + + /// + /// 卸载当前场景,并将当前场景键值置为空。 + /// + public void Unload() + { + // 执行场景卸载操作 + Root!.Unload(); + + // 清空当前场景键值 + CurrentKey = null; + } + + /// + /// 场景替换前的虚方法,可在子类中重写以实现自定义逻辑。 + /// + /// 即将被替换的场景键值。 + protected virtual void OnBeforeReplace(string key) + { + } + + /// + /// 场景替换后的虚方法,可在子类中重写以实现自定义逻辑。 + /// + /// 已替换的场景键值。 + protected virtual void OnAfterReplace(string key) + { + } +} \ No newline at end of file