diff --git a/Assets/GameMain/Scripts/Simulation/Presentation/SimulationWorld.TransformSync.cs b/Assets/GameMain/Scripts/Simulation/Presentation/SimulationWorld.TransformSync.cs index 11b80ed..308d556 100644 --- a/Assets/GameMain/Scripts/Simulation/Presentation/SimulationWorld.TransformSync.cs +++ b/Assets/GameMain/Scripts/Simulation/Presentation/SimulationWorld.TransformSync.cs @@ -22,25 +22,23 @@ namespace Simulation } var enemyManager = GameEntry.EnemyManager; - if (enemyManager == null || enemyManager.Enemies == null) + if (enemyManager != null && enemyManager.Enemies != null) { - return; - } - - var enemies = enemyManager.Enemies; - foreach (var enemy in enemies) - { - if (enemy is not EnemyBase enemyEntity || !enemyEntity.Available) + var enemies = enemyManager.Enemies; + foreach (var enemy in enemies) { - continue; - } + if (enemy is not EnemyBase enemyEntity || !enemyEntity.Available) + { + continue; + } - if (!_world.TryGetEnemyData(enemyEntity.Id, out EnemySimData enemyData)) - { - continue; - } + if (!_world.TryGetEnemyData(enemyEntity.Id, out EnemySimData enemyData)) + { + continue; + } - ApplyEnemyPresentation(enemyEntity, enemyData); + ApplyEnemyPresentation(enemyEntity, enemyData); + } } var projectiles = _world._projectiles; diff --git a/Assets/GameMain/Scripts/Simulation/SimulationWorld.EntitySync.cs b/Assets/GameMain/Scripts/Simulation/SimulationWorld.EntitySync.cs index d8a3290..ef8bfde 100644 --- a/Assets/GameMain/Scripts/Simulation/SimulationWorld.EntitySync.cs +++ b/Assets/GameMain/Scripts/Simulation/SimulationWorld.EntitySync.cs @@ -16,6 +16,7 @@ namespace Simulation private const string EnemyProjectileGroupName = "EnemyProjectile"; private readonly SimulationWorld _world; + private bool _isEventSubscribed; public EntitySync(SimulationWorld world) { @@ -24,14 +25,32 @@ namespace Simulation public void OnStart() { - GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess); - GameEntry.Event.Subscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete); + var eventComponent = GameEntry.Event; + if (eventComponent == null) + { + return; + } + + eventComponent.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess); + eventComponent.Subscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete); + _isEventSubscribed = true; } public void OnDestroy() { - GameEntry.Event.Unsubscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess); - GameEntry.Event.Unsubscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete); + if (!_isEventSubscribed) + { + return; + } + + var eventComponent = GameEntry.Event; + if (eventComponent != null) + { + eventComponent.Unsubscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess); + eventComponent.Unsubscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete); + } + + _isEventSubscribed = false; } private void OnShowEntitySuccess(object sender, GameEventArgs e) diff --git a/Assets/Tests/Simulation/EditMode/SimulationWorldTickTests.cs b/Assets/Tests/Simulation/EditMode/SimulationWorldTickTests.cs index 6358291..2d77ba2 100644 --- a/Assets/Tests/Simulation/EditMode/SimulationWorldTickTests.cs +++ b/Assets/Tests/Simulation/EditMode/SimulationWorldTickTests.cs @@ -133,11 +133,7 @@ namespace Simulation.Tests.Editor _world.Tick(new SimulationTickContext(0.5f, 0.5f, Vector3.zero)); - Assert.That(_world.Projectiles.Count, Is.EqualTo(1)); - ProjectileSimData projectile = _world.Projectiles[0]; - Assert.That(projectile.Active, Is.False); - Assert.That(projectile.State, Is.EqualTo(1)); - Assert.That(projectile.Age, Is.GreaterThanOrEqualTo(projectile.LifeTime)); + Assert.That(_world.Projectiles, Is.Empty); } [Test]