diff --git a/GFramework.Core.Abstractions/architecture/IArchitecture.cs b/GFramework.Core.Abstractions/architecture/IArchitecture.cs index d191b54..eaf0a22 100644 --- a/GFramework.Core.Abstractions/architecture/IArchitecture.cs +++ b/GFramework.Core.Abstractions/architecture/IArchitecture.cs @@ -10,7 +10,7 @@ namespace GFramework.Core.Abstractions.architecture; /// 架构接口,专注于生命周期管理,包括系统、模型、工具的注册和获取 /// 业务操作通过 ArchitectureRuntime 提供 /// -public interface IArchitecture : IAsyncInitializable +public interface IArchitecture : IAsyncInitializable, IAsyncDestroyable, IInitializable, IDestroyable { /// /// 获取架构上下文 @@ -25,22 +25,6 @@ public interface IArchitecture : IAsyncInitializable /// Action? Configurator { get; } - /// - /// 初始化方法,用于执行对象的初始化操作 - /// - /// - /// 该方法通常用于设置初始状态、初始化成员变量或执行必要的预处理操作 - /// - void Initialize(); - - /// - /// 销毁方法,用于执行对象的清理和销毁操作 - /// - /// - /// 该方法通常用于释放资源、清理内存或执行必要的清理操作 - /// - void Destroy(); - /// /// 注册系统实例到架构中 /// diff --git a/GFramework.Core.Abstractions/lifecycle/IInitializable.cs b/GFramework.Core.Abstractions/lifecycle/IInitializable.cs index 6e389e9..1275840 100644 --- a/GFramework.Core.Abstractions/lifecycle/IInitializable.cs +++ b/GFramework.Core.Abstractions/lifecycle/IInitializable.cs @@ -8,5 +8,5 @@ public interface IInitializable /// /// 初始化组件 /// - void Init(); + void Initialize(); } \ No newline at end of file diff --git a/GFramework.Core.Tests/architecture/ArchitectureContextTests.cs b/GFramework.Core.Tests/architecture/ArchitectureContextTests.cs index d1a208f..f1b54be 100644 --- a/GFramework.Core.Tests/architecture/ArchitectureContextTests.cs +++ b/GFramework.Core.Tests/architecture/ArchitectureContextTests.cs @@ -321,7 +321,7 @@ public class TestSystemV2 : ISystem return _context; } - public void Init() + public void Initialize() { } @@ -349,7 +349,7 @@ public class TestModelV2 : IModel return _context; } - public void Init() + public void Initialize() { } diff --git a/GFramework.Core.Tests/architecture/AsyncTestArchitecture.cs b/GFramework.Core.Tests/architecture/AsyncTestArchitecture.cs index 620eda2..69caddc 100644 --- a/GFramework.Core.Tests/architecture/AsyncTestArchitecture.cs +++ b/GFramework.Core.Tests/architecture/AsyncTestArchitecture.cs @@ -11,12 +11,12 @@ public class AsyncTestArchitecture : TestArchitectureBase /// /// 异步初始化架构 /// - protected override void Init() + protected override void OnInitialize() { // 注册模型 RegisterModel(new AsyncTestModel()); // 注册系统 RegisterSystem(new AsyncTestSystem()); - base.Init(); + base.OnInitialize(); } } \ No newline at end of file diff --git a/GFramework.Core.Tests/architecture/GameContextTests.cs b/GFramework.Core.Tests/architecture/GameContextTests.cs index 3089ed1..88b3dc7 100644 --- a/GFramework.Core.Tests/architecture/GameContextTests.cs +++ b/GFramework.Core.Tests/architecture/GameContextTests.cs @@ -222,7 +222,7 @@ public class TestArchitecture : Architecture /// /// 初始化方法,当前为空实现 /// - protected override void Init() + protected override void OnInitialize() { } } diff --git a/GFramework.Core.Tests/architecture/SyncTestArchitecture.cs b/GFramework.Core.Tests/architecture/SyncTestArchitecture.cs index 3a40f0a..27f0f41 100644 --- a/GFramework.Core.Tests/architecture/SyncTestArchitecture.cs +++ b/GFramework.Core.Tests/architecture/SyncTestArchitecture.cs @@ -11,10 +11,10 @@ public sealed class SyncTestArchitecture : TestArchitectureBase /// /// 初始化架构组件,注册模型、系统并设置事件监听器 /// - protected override void Init() + protected override void OnInitialize() { RegisterModel(new TestModel()); RegisterSystem(new TestSystem()); - base.Init(); + base.OnInitialize(); } } \ No newline at end of file diff --git a/GFramework.Core.Tests/architecture/TestArchitectureBase.cs b/GFramework.Core.Tests/architecture/TestArchitectureBase.cs index 5ffffbe..8b96d40 100644 --- a/GFramework.Core.Tests/architecture/TestArchitectureBase.cs +++ b/GFramework.Core.Tests/architecture/TestArchitectureBase.cs @@ -37,7 +37,7 @@ public abstract class TestArchitectureBase : Architecture /// /// 初始化架构组件,注册模型、系统并设置事件监听器 /// - protected override void Init() + protected override void OnInitialize() { InitCalled = true; _postRegistrationHook?.Invoke(this); diff --git a/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs b/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs index f223938..64ac626 100644 --- a/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs +++ b/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs @@ -53,7 +53,7 @@ public class EcsAdvancedTests { var system = (IEcsSystem)Activator.CreateInstance(systemType)!; ((IContextAware)system).SetContext(_context!); - system.Init(); + system.Initialize(); systems.Add(system); _container.RegisterPlurality(system); } @@ -65,7 +65,7 @@ public class EcsAdvancedTests { var runner = new EcsSystemRunner(); ((IContextAware)runner).SetContext(_context!); - runner.Init(); + runner.Initialize(); return runner; } @@ -220,7 +220,7 @@ public class EcsAdvancedTests foreach (var system in new[] { systemA, systemB, systemC }) { ((IContextAware)system).SetContext(_context!); - system.Init(); + system.Initialize(); _container.RegisterPlurality(system); } @@ -247,7 +247,7 @@ public class EcsAdvancedTests var movementSystem = new MovementSystem(); ((IContextAware)movementSystem).SetContext(_context!); - movementSystem.Init(); + movementSystem.Initialize(); _container.RegisterPlurality(movementSystem); _container.Register(new List { movementSystem } as IReadOnlyList); diff --git a/GFramework.Core.Tests/ecs/EcsBasicTests.cs b/GFramework.Core.Tests/ecs/EcsBasicTests.cs index ca7d030..021aa68 100644 --- a/GFramework.Core.Tests/ecs/EcsBasicTests.cs +++ b/GFramework.Core.Tests/ecs/EcsBasicTests.cs @@ -68,7 +68,7 @@ public class EcsBasicTests { var system = (IEcsSystem)Activator.CreateInstance(systemType)!; system.SetContext(_context!); - system.Init(); + system.Initialize(); systems.Add(system); _container.RegisterPlurality(system); } diff --git a/GFramework.Core.Tests/ecs/EcsIntegrationTests.cs b/GFramework.Core.Tests/ecs/EcsIntegrationTests.cs index dde6c2d..17cb4dc 100644 --- a/GFramework.Core.Tests/ecs/EcsIntegrationTests.cs +++ b/GFramework.Core.Tests/ecs/EcsIntegrationTests.cs @@ -68,7 +68,7 @@ public class EcsIntegrationTests { var system = (IEcsSystem)Activator.CreateInstance(systemType)!; system.SetContext(_context!); - system.Init(); + system.Initialize(); systems.Add(system); _container.RegisterPlurality(system); } diff --git a/GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs b/GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs index 2d86feb..9362987 100644 --- a/GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs +++ b/GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs @@ -214,7 +214,7 @@ public class MediatorComprehensiveTests var results = new List(); // 流应该在100ms后被取消(TaskCanceledException 继承自 OperationCanceledException) - Assert.ThrowsAsync(async () => + Assert.CatchAsync(async () => { await foreach (var item in stream) { diff --git a/GFramework.Core.Tests/model/AsyncTestModel.cs b/GFramework.Core.Tests/model/AsyncTestModel.cs index b4f0844..d414125 100644 --- a/GFramework.Core.Tests/model/AsyncTestModel.cs +++ b/GFramework.Core.Tests/model/AsyncTestModel.cs @@ -30,8 +30,8 @@ public sealed class AsyncTestModel : AbstractModel, IAsyncInitializable /// 当该方法被调用时抛出异常 public void Init() { - // sync Init 不应该被调用 - throw new InvalidOperationException("Sync Init should not be called"); + // sync OnInitialize 不应该被调用 + throw new InvalidOperationException("Sync OnInitialize should not be called"); } /// diff --git a/GFramework.Core.Tests/model/FailingModel.cs b/GFramework.Core.Tests/model/FailingModel.cs index 65008d1..4fa50a4 100644 --- a/GFramework.Core.Tests/model/FailingModel.cs +++ b/GFramework.Core.Tests/model/FailingModel.cs @@ -15,7 +15,7 @@ public sealed class FailingModel : IModel /// 该方法会故意抛出InvalidOperationException异常 /// /// 总是抛出此异常以模拟初始化失败 - public void Init() + public void Initialize() { throw new InvalidOperationException("Model init failed intentionally"); } diff --git a/GFramework.Core.Tests/model/TestModel.cs b/GFramework.Core.Tests/model/TestModel.cs index 59fc9ad..1cf267f 100644 --- a/GFramework.Core.Tests/model/TestModel.cs +++ b/GFramework.Core.Tests/model/TestModel.cs @@ -18,7 +18,7 @@ public sealed class TestModel : AbstractModel, ITestModel /// /// 初始化模型 /// - public void Init() + public void Initialize() { Initialized = true; } diff --git a/GFramework.Core.Tests/state/StateMachineSystemTests.cs b/GFramework.Core.Tests/state/StateMachineSystemTests.cs index 35e6309..8cb698a 100644 --- a/GFramework.Core.Tests/state/StateMachineSystemTests.cs +++ b/GFramework.Core.Tests/state/StateMachineSystemTests.cs @@ -114,7 +114,7 @@ public class StateMachineSystemTests Assert.Throws(() => state1.GetContext()); Assert.Throws(() => state2.GetContext()); - _stateMachine.Init(); + _stateMachine.Initialize(); Assert.That(state1.GetContext(), Is.SameAs(_context)); Assert.That(state2.GetContext(), Is.SameAs(_context)); @@ -129,7 +129,7 @@ public class StateMachineSystemTests var state = new TestStateV5(); _stateMachine!.Register(state); - _stateMachine.Init(); + _stateMachine.Initialize(); } /// @@ -173,7 +173,7 @@ public class StateMachineSystemTests _stateMachine!.Register(state1); _stateMachine.Register(state2); - _stateMachine.Init(); + _stateMachine.Initialize(); await _stateMachine.ChangeToAsync(); Assert.That(eventReceived, Is.True); @@ -203,7 +203,7 @@ public class StateMachineSystemTests _stateMachine!.Register(state1); _stateMachine.Register(state2); - _stateMachine.Init(); + _stateMachine.Initialize(); await _stateMachine.ChangeToAsync(); eventReceived = false; @@ -230,7 +230,7 @@ public class StateMachineSystemTests _stateMachine!.Register(state1); _stateMachine.Register(state2); - _stateMachine.Init(); + _stateMachine.Initialize(); await _stateMachine.ChangeToAsync(); await _stateMachine.ChangeToAsync(); await _stateMachine.ChangeToAsync(); diff --git a/GFramework.Core.Tests/system/AsyncTestSystem.cs b/GFramework.Core.Tests/system/AsyncTestSystem.cs index 87e64f0..bec03e5 100644 --- a/GFramework.Core.Tests/system/AsyncTestSystem.cs +++ b/GFramework.Core.Tests/system/AsyncTestSystem.cs @@ -30,10 +30,10 @@ public sealed class AsyncTestSystem : ISystem, IAsyncInitializable return _context; } - public void Init() + public void Initialize() { - // 同步 Init 不应该被调用 - throw new InvalidOperationException("Sync Init should not be called"); + // 同步 OnInitialize 不应该被调用 + throw new InvalidOperationException("Sync OnInitialize should not be called"); } public void Destroy() diff --git a/GFramework.Core.Tests/system/TestSystem.cs b/GFramework.Core.Tests/system/TestSystem.cs index 2aa6dad..f06c455 100644 --- a/GFramework.Core.Tests/system/TestSystem.cs +++ b/GFramework.Core.Tests/system/TestSystem.cs @@ -45,7 +45,7 @@ public sealed class TestSystem : ISystem /// /// 初始化系统 /// - public void Init() + public void Initialize() { Initialized = true; } diff --git a/GFramework.Core.Tests/utility/AbstractContextUtilityTests.cs b/GFramework.Core.Tests/utility/AbstractContextUtilityTests.cs index b970c31..7466f61 100644 --- a/GFramework.Core.Tests/utility/AbstractContextUtilityTests.cs +++ b/GFramework.Core.Tests/utility/AbstractContextUtilityTests.cs @@ -61,11 +61,11 @@ public class AbstractContextUtilityTests { var utility = new TestContextUtilityV1(); - Assert.That(utility.Initialized, Is.False, "Utility should not be initialized before Init"); + Assert.That(utility.Initialized, Is.False, "Utility should not be initialized before OnInitialize"); - utility.Init(); + utility.Initialize(); - Assert.That(utility.Initialized, Is.True, "Utility should be initialized after Init"); + Assert.That(utility.Initialized, Is.True, "Utility should be initialized after OnInitialize"); } /// @@ -76,11 +76,11 @@ public class AbstractContextUtilityTests { var utility = new TestContextUtilityV1(); - Assert.That(utility.GetLogger(), Is.Null, "Logger should be null before Init"); + Assert.That(utility.GetLogger(), Is.Null, "Logger should be null before OnInitialize"); - utility.Init(); + utility.Initialize(); - Assert.That(utility.GetLogger(), Is.Not.Null, "Logger should be set after Init"); + Assert.That(utility.GetLogger(), Is.Not.Null, "Logger should be set after OnInitialize"); } /// @@ -91,11 +91,11 @@ public class AbstractContextUtilityTests { var utility = new TestContextUtilityV1(); - Assert.That(utility.InitCalled, Is.False, "InitCalled should be false before Init"); + Assert.That(utility.InitCalled, Is.False, "InitCalled should be false before OnInitialize"); - utility.Init(); + utility.Initialize(); - Assert.That(utility.InitCalled, Is.True, "InitCalled should be true after Init"); + Assert.That(utility.InitCalled, Is.True, "InitCalled should be true after OnInitialize"); } /// @@ -106,7 +106,7 @@ public class AbstractContextUtilityTests { var utility = new TestContextUtilityV1(); - utility.Init(); + utility.Initialize(); Assert.That(utility.Destroyed, Is.False, "Utility should not be destroyed before Destroy"); utility.Destroy(); @@ -156,7 +156,7 @@ public class AbstractContextUtilityTests Assert.That(utility.Initialized, Is.False); Assert.That(utility.CustomInitializationDone, Is.False); - utility.Init(); + utility.Initialize(); Assert.That(utility.Initialized, Is.True); Assert.That(utility.CustomInitializationDone, Is.True); @@ -175,7 +175,7 @@ public class AbstractContextUtilityTests Assert.That(utility.Destroyed, Is.False); // 初始化 - utility.Init(); + utility.Initialize(); Assert.That(utility.Initialized, Is.True); Assert.That(utility.Destroyed, Is.False); @@ -194,7 +194,7 @@ public class AbstractContextUtilityTests var utility = new TestContextUtilityV1(); // 第一次初始化和销毁 - utility.Init(); + utility.Initialize(); Assert.That(utility.Initialized, Is.True); utility.Destroy(); Assert.That(utility.Destroyed, Is.True); @@ -203,7 +203,7 @@ public class AbstractContextUtilityTests utility.Destroyed = false; // 第二次初始化和销毁 - utility.Init(); + utility.Initialize(); Assert.That(utility.Initialized, Is.True); utility.Destroy(); Assert.That(utility.Destroyed, Is.True); diff --git a/GFramework.Core/architecture/Architecture.cs b/GFramework.Core/architecture/Architecture.cs index 0e60ee7..33c4740 100644 --- a/GFramework.Core/architecture/Architecture.cs +++ b/GFramework.Core/architecture/Architecture.cs @@ -261,15 +261,11 @@ public abstract class Architecture( } // 处理销毁(支持 IDestroyable 或 IAsyncDestroyable) - if (component is IDestroyable or IAsyncDestroyable) - { - // 原子去重:HashSet.Add 返回 true 表示添加成功(之前不存在) - if (_disposableSet.Add(component)) - { - _disposables.Add(component); - _logger.Trace($"Registered {component.GetType().Name} for destruction"); - } - } + if (component is not (IDestroyable or IAsyncDestroyable)) return; + // 原子去重:HashSet.Add 返回 true 表示添加成功(之前不存在) + if (!_disposableSet.Add(component)) return; + _disposables.Add(component); + _logger.Trace($"Registered {component.GetType().Name} for destruction"); } /// @@ -354,13 +350,13 @@ public abstract class Architecture( if (asyncMode && component is IAsyncInitializable asyncInit) await asyncInit.InitializeAsync(); else - component.Init(); + component.Initialize(); } /// /// 抽象初始化方法,由子类重写以进行自定义初始化操作 /// - protected abstract void Init(); + protected abstract void OnInitialize(); /// /// 异步销毁架构及所有组件 @@ -663,10 +659,10 @@ public abstract class Architecture( // 执行服务钩子 Container.ExecuteServicesHook(Configurator); - // === 用户 Init === - _logger.Debug("Calling user Init()"); - Init(); - _logger.Debug("User Init() completed"); + // === 用户 OnInitialize === + _logger.Debug("Calling user OnInitialize()"); + OnInitialize(); + _logger.Debug("User OnInitialize() completed"); // === 组件初始化阶段 === await InitializeAllComponentsAsync(asyncMode); diff --git a/GFramework.Core/architecture/ArchitectureContext.cs b/GFramework.Core/architecture/ArchitectureContext.cs index 27a347f..0b89e27 100644 --- a/GFramework.Core/architecture/ArchitectureContext.cs +++ b/GFramework.Core/architecture/ArchitectureContext.cs @@ -90,7 +90,7 @@ public class ArchitectureContext(IIocContainer container) : IArchitectureContext var mediator = Mediator; if (mediator == null) throw new InvalidOperationException( - "Mediator not registered. Call EnableMediator() in your Architecture.Init() method."); + "Mediator not registered. Call EnableMediator() in your Architecture.OnInitialize() method."); return await mediator.Send(request, cancellationToken); } diff --git a/GFramework.Core/ecs/EcsSystemBase.cs b/GFramework.Core/ecs/EcsSystemBase.cs index b22ad76..b978bfe 100644 --- a/GFramework.Core/ecs/EcsSystemBase.cs +++ b/GFramework.Core/ecs/EcsSystemBase.cs @@ -1,5 +1,6 @@ using Arch.Core; using GFramework.Core.Abstractions.ecs; +using GFramework.Core.extensions; using GFramework.Core.system; namespace GFramework.Core.ecs; @@ -34,7 +35,7 @@ public abstract class EcsSystemBase : AbstractSystem, IEcsSystem /// protected override void OnInit() { - EcsWorld = Context.GetService() ?? throw new InvalidOperationException( + EcsWorld = this.GetService() ?? throw new InvalidOperationException( "EcsWorld not found in context. Make sure ECS is properly initialized."); OnEcsInit(); diff --git a/GFramework.Core/ecs/EcsSystemRunner.cs b/GFramework.Core/ecs/EcsSystemRunner.cs index b08fbc0..3820a09 100644 --- a/GFramework.Core/ecs/EcsSystemRunner.cs +++ b/GFramework.Core/ecs/EcsSystemRunner.cs @@ -1,4 +1,5 @@ using GFramework.Core.Abstractions.ecs; +using GFramework.Core.extensions; using GFramework.Core.system; namespace GFramework.Core.ecs; @@ -17,8 +18,8 @@ public sealed class EcsSystemRunner : AbstractSystem protected override void OnInit() { // 从容器获取所有已注册的ECS系统 - var systemsList = Context.GetService>(); - if (systemsList != null && systemsList.Count > 0) + var systemsList = this.GetService>(); + if (systemsList is { Count: > 0 }) { // 按优先级排序 _systems.AddRange(systemsList.OrderBy(s => s.Priority)); diff --git a/GFramework.Core/ecs/EcsWorld.cs b/GFramework.Core/ecs/EcsWorld.cs index a0241cf..92651b3 100644 --- a/GFramework.Core/ecs/EcsWorld.cs +++ b/GFramework.Core/ecs/EcsWorld.cs @@ -8,25 +8,24 @@ namespace GFramework.Core.ecs; /// public sealed class EcsWorld : IEcsWorld { - private readonly World _world = World.Create(); private bool _disposed; /// /// 获取内部的Arch World实例 /// - public World InternalWorld => _world; + public World InternalWorld { get; } = World.Create(); /// /// 当前实体数量 /// - public int EntityCount => _world.Size; + public int EntityCount => InternalWorld.Size; /// /// 创建一个新实体 /// public Entity CreateEntity(params ComponentType[] types) { - return _world.Create(types); + return InternalWorld.Create(types); } /// @@ -34,7 +33,7 @@ public sealed class EcsWorld : IEcsWorld /// public void DestroyEntity(Entity entity) { - _world.Destroy(entity); + InternalWorld.Destroy(entity); } /// @@ -42,7 +41,7 @@ public sealed class EcsWorld : IEcsWorld /// public bool IsAlive(Entity entity) { - return _world.IsAlive(entity); + return InternalWorld.IsAlive(entity); } /// @@ -50,7 +49,7 @@ public sealed class EcsWorld : IEcsWorld /// public void Clear() { - _world.Clear(); + InternalWorld.Clear(); } /// @@ -60,7 +59,7 @@ public sealed class EcsWorld : IEcsWorld { if (_disposed) return; - World.Destroy(_world); + World.Destroy(InternalWorld); _disposed = true; } } \ No newline at end of file diff --git a/GFramework.Core/ecs/components/Position.cs b/GFramework.Core/ecs/components/Position.cs index 1743454..472679f 100644 --- a/GFramework.Core/ecs/components/Position.cs +++ b/GFramework.Core/ecs/components/Position.cs @@ -1,16 +1,19 @@ namespace GFramework.Core.ecs.components; /// -/// 位置组件 +/// 位置组件,用于表示实体在二维空间中的坐标位置。 /// -public struct Position +/// X轴坐标值 +/// Y轴坐标值 +public struct Position(float x, float y) { - public float X; - public float Y; + /// + /// 获取X轴坐标值。 + /// + public float X { get; set; } = x; - public Position(float x, float y) - { - X = x; - Y = y; - } + /// + /// 获取Y轴坐标值。 + /// + public float Y { get; set; } = y; } \ No newline at end of file diff --git a/GFramework.Core/ecs/components/Velocity.cs b/GFramework.Core/ecs/components/Velocity.cs index c277724..51e8892 100644 --- a/GFramework.Core/ecs/components/Velocity.cs +++ b/GFramework.Core/ecs/components/Velocity.cs @@ -1,16 +1,19 @@ namespace GFramework.Core.ecs.components; /// -/// 速度组件 +/// 速度组件,用于表示实体在二维空间中的运动速度。 /// -public struct Velocity +/// X轴方向的速度分量 +/// Y轴方向的速度分量 +public struct Velocity(float x, float y) { - public float X; - public float Y; + /// + /// X轴方向的速度分量 + /// + public float X { get; set; } = x; - public Velocity(float x, float y) - { - X = x; - Y = y; - } + /// + /// Y轴方向的速度分量 + /// + public float Y { get; set; } = y; } \ No newline at end of file diff --git a/GFramework.Core/ecs/examples/EcsUsageExample.cs b/GFramework.Core/ecs/examples/EcsUsageExample.cs deleted file mode 100644 index 9048076..0000000 --- a/GFramework.Core/ecs/examples/EcsUsageExample.cs +++ /dev/null @@ -1,67 +0,0 @@ -using Arch.Core; -using GFramework.Core.Abstractions.architecture; -using GFramework.Core.ecs.components; - -namespace GFramework.Core.ecs.examples; - -/// -/// ECS使用示例 - 演示如何创建和管理实体 -/// -public static class EcsUsageExample -{ - /// - /// 示例1: 创建移动的敌人实体 - /// - public static void CreateMovingEnemies(IArchitectureContext context, int count) - { - var ecsWorld = context.GetEcsWorld(); - var world = ecsWorld.InternalWorld; - - for (int i = 0; i < count; i++) - { - // 创建实体 - var entity = ecsWorld.CreateEntity( - new ComponentType[] - { - typeof(Position), - typeof(Velocity) - } - ); - - // 设置初始位置和速度 - world.Set(entity, new Position(i * 10, 0)); - - var random = new Random(); - world.Set(entity, new Velocity( - (float)(random.NextDouble() * 100 - 50), - (float)(random.NextDouble() * 100 - 50) - )); - } - } - - /// - /// 示例2: 批量清理实体 - /// - public static void ClearAllEntities(IArchitectureContext context) - { - var ecsWorld = context.GetEcsWorld(); - ecsWorld.Clear(); - } - - /// - /// 示例3: 查询特定实体 - /// - public static int CountMovingEntities(IArchitectureContext context) - { - var ecsWorld = context.GetEcsWorld(); - var world = ecsWorld.InternalWorld; - - int count = 0; - var query = new QueryDescription() - .WithAll(); - - world.Query(in query, (Entity entity) => { count++; }); - - return count; - } -} \ No newline at end of file diff --git a/GFramework.Core/ecs/systems/MovementSystem.cs b/GFramework.Core/ecs/systems/MovementSystem.cs index 20206b5..c0fe4cc 100644 --- a/GFramework.Core/ecs/systems/MovementSystem.cs +++ b/GFramework.Core/ecs/systems/MovementSystem.cs @@ -4,14 +4,22 @@ using GFramework.Core.ecs.components; namespace GFramework.Core.ecs.systems; /// -/// 移动系统示例 - 根据速度更新位置 +/// 移动系统,负责更新具有位置和速度组件的实体的位置。 +/// 根据速度和时间增量计算实体的新位置。 /// public class MovementSystem : EcsSystemBase { private QueryDescription _query; + /// + /// 获取系统的优先级,数值越小优先级越高。 + /// public override int Priority => 0; + /// + /// ECS初始化回调方法,在系统初始化时调用。 + /// 创建查询描述符,用于查找同时拥有Position和Velocity组件的实体。 + /// protected override void OnEcsInit() { // 创建查询:查找所有同时拥有Position和Velocity组件的实体 @@ -19,6 +27,10 @@ public class MovementSystem : EcsSystemBase .WithAll(); } + /// + /// 系统更新方法,每帧调用一次。 + /// + /// 帧间隔时间,用于计算位置变化量 public override void Update(float deltaTime) { // 查询并更新所有符合条件的实体 diff --git a/GFramework.Core/model/AbstractModel.cs b/GFramework.Core/model/AbstractModel.cs index 3968d03..4a619d6 100644 --- a/GFramework.Core/model/AbstractModel.cs +++ b/GFramework.Core/model/AbstractModel.cs @@ -13,7 +13,7 @@ public abstract class AbstractModel : ContextAwareBase, IModel /// /// 初始化模型,调用抽象方法OnInit执行具体初始化逻辑 /// - void IInitializable.Init() + void IInitializable.Initialize() { OnInit(); } diff --git a/GFramework.Core/state/StateMachineSystem.cs b/GFramework.Core/state/StateMachineSystem.cs index fe853ca..6ce8a4c 100644 --- a/GFramework.Core/state/StateMachineSystem.cs +++ b/GFramework.Core/state/StateMachineSystem.cs @@ -48,7 +48,7 @@ public class StateMachineSystem : StateMachine, IStateMachineSystem /// 初始化方法,在系统启动时调用 /// 遍历所有状态实例,为实现了IContextAware接口的状态设置上下文 /// - public virtual void Init() + public virtual void Initialize() { foreach (var state in States.Values.OfType()) state.SetContext(_context); } diff --git a/GFramework.Core/system/AbstractSystem.cs b/GFramework.Core/system/AbstractSystem.cs index da1d4bf..1efae50 100644 --- a/GFramework.Core/system/AbstractSystem.cs +++ b/GFramework.Core/system/AbstractSystem.cs @@ -17,7 +17,7 @@ public abstract class AbstractSystem : ContextAwareBase, ISystem /// /// 系统初始化方法,调用抽象初始化方法 /// - public void Init() + public void Initialize() { var name = GetType().Name; _logger = LoggerFactoryResolver.Provider.CreateLogger(name); diff --git a/GFramework.Core/utility/AbstractContextUtility.cs b/GFramework.Core/utility/AbstractContextUtility.cs index f694643..80697e1 100644 --- a/GFramework.Core/utility/AbstractContextUtility.cs +++ b/GFramework.Core/utility/AbstractContextUtility.cs @@ -19,7 +19,7 @@ public abstract class AbstractContextUtility : ContextAwareBase, IContextUtility /// /// 初始化上下文工具类 /// - public void Init() + public void Initialize() { var name = GetType().Name; // 获取上下文中的日志记录器 diff --git a/GFramework.Game/setting/SettingsModel.cs b/GFramework.Game/setting/SettingsModel.cs index e6df8d0..41d9eaf 100644 --- a/GFramework.Game/setting/SettingsModel.cs +++ b/GFramework.Game/setting/SettingsModel.cs @@ -240,7 +240,7 @@ public class SettingsModel(IDataLocationProvider? locationProvider, : null; } // ========================= - // Init + // OnInitialize // ========================= /// diff --git a/GFramework.Godot/architecture/AbstractArchitecture.cs b/GFramework.Godot/architecture/AbstractArchitecture.cs index 757f294..8150f22 100644 --- a/GFramework.Godot/architecture/AbstractArchitecture.cs +++ b/GFramework.Godot/architecture/AbstractArchitecture.cs @@ -53,7 +53,7 @@ public abstract class AbstractArchitecture( /// 初始化架构,按顺序注册模型、系统和工具。 /// 包括将架构绑定到Godot生命周期并调用模块安装逻辑。 /// - protected override void Init() + protected override void OnInitialize() { _architectureAnchorName = $"__{GFrameworkConstants.FrameworkName}__{GetType().Name}__{GetHashCode()}__ArchitectureAnchor__"; @@ -76,7 +76,7 @@ public abstract class AbstractArchitecture( if (Engine.GetMainLoop() is not SceneTree tree) return; - // 防止重复挂载(热重载 / 多次 Init) + // 防止重复挂载(热重载 / 多次 OnInitialize) if (tree.Root.GetNodeOrNull(_architectureAnchorName) != null) return;