fix test sample

This commit is contained in:
SepComet 2026-04-02 12:52:23 +08:00
parent ffcd4e6b54
commit af1fb7bf9d
3 changed files with 37 additions and 24 deletions

View File

@ -22,11 +22,8 @@ namespace Simulation
} }
var enemyManager = GameEntry.EnemyManager; var enemyManager = GameEntry.EnemyManager;
if (enemyManager == null || enemyManager.Enemies == null) if (enemyManager != null && enemyManager.Enemies != null)
{ {
return;
}
var enemies = enemyManager.Enemies; var enemies = enemyManager.Enemies;
foreach (var enemy in enemies) foreach (var enemy in enemies)
{ {
@ -42,6 +39,7 @@ namespace Simulation
ApplyEnemyPresentation(enemyEntity, enemyData); ApplyEnemyPresentation(enemyEntity, enemyData);
} }
}
var projectiles = _world._projectiles; var projectiles = _world._projectiles;
for (int i = 0; i < projectiles.Count; i++) for (int i = 0; i < projectiles.Count; i++)

View File

@ -16,6 +16,7 @@ namespace Simulation
private const string EnemyProjectileGroupName = "EnemyProjectile"; private const string EnemyProjectileGroupName = "EnemyProjectile";
private readonly SimulationWorld _world; private readonly SimulationWorld _world;
private bool _isEventSubscribed;
public EntitySync(SimulationWorld world) public EntitySync(SimulationWorld world)
{ {
@ -24,14 +25,32 @@ namespace Simulation
public void OnStart() public void OnStart()
{ {
GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess); var eventComponent = GameEntry.Event;
GameEntry.Event.Subscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete); if (eventComponent == null)
{
return;
}
eventComponent.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
eventComponent.Subscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete);
_isEventSubscribed = true;
} }
public void OnDestroy() public void OnDestroy()
{ {
GameEntry.Event.Unsubscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess); if (!_isEventSubscribed)
GameEntry.Event.Unsubscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete); {
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) private void OnShowEntitySuccess(object sender, GameEventArgs e)

View File

@ -133,11 +133,7 @@ namespace Simulation.Tests.Editor
_world.Tick(new SimulationTickContext(0.5f, 0.5f, Vector3.zero)); _world.Tick(new SimulationTickContext(0.5f, 0.5f, Vector3.zero));
Assert.That(_world.Projectiles.Count, Is.EqualTo(1)); Assert.That(_world.Projectiles, Is.Empty);
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));
} }
[Test] [Test]