using SepCore.Entity; using GameFramework.Event; using UnityGameFramework.Runtime; namespace SepCore.Simulation { public sealed partial class SimulationWorld { // Bridges entity show/hide events into simulation state registration. public sealed class EntitySync { private static string EnemyGroupName => GameEntry.Luban.Global.EnemyGroupName; private static string DropGroupName => GameEntry.Luban.Global.DropGroupName; private static string ProjectileGroupName => GameEntry.Luban.Global.ProjectileGroupName; private readonly SimulationWorld _world; private bool _isEventSubscribed; public EntitySync(SimulationWorld world) { _world = world; } public void OnStart() { var eventComponent = GameEntry.Event; if (eventComponent == null) { return; } eventComponent.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess); eventComponent.Subscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete); _isEventSubscribed = true; } public void OnDestroy() { 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) { if (e is not ShowEntitySuccessEventArgs args) return; if (args.Entity == null) return; if (_world == null) return; string groupName = args.Entity.EntityGroup?.Name; if (string.IsNullOrEmpty(groupName)) return; if (groupName == EnemyGroupName && args.Entity.Logic is EnemyBase enemy) { _world.RegisterEnemyLifecycle(enemy, args.UserData); return; } if (groupName == DropGroupName && args.Entity.Logic is EntityBase pickupEntity) { _world.RegisterPickupLifecycle(pickupEntity); return; } if (groupName == ProjectileGroupName && args.Entity.Logic is EntityBase projectileEntity) { _world.RegisterProjectileLifecycle(projectileEntity, args.UserData); } } private void OnHideEntityComplete(object sender, GameEventArgs e) { if (e is not HideEntityCompleteEventArgs args) return; if (args.EntityGroup == null) return; if (_world == null) return; string groupName = args.EntityGroup.Name; if (groupName == EnemyGroupName) { _world.UnregisterEnemyLifecycle(args.EntityId); return; } if (groupName == DropGroupName) { _world.UnregisterPickupLifecycle(args.EntityId); return; } if (groupName == ProjectileGroupName) { _world.UnregisterProjectileLifecycle(args.EntityId); } } } } }