public override Subscription <WorldCommandSender> Subscribe(EntityId entityId) { if (entityIdToSenderSubscriptions == null) { entityIdToSenderSubscriptions = new Dictionary <EntityId, HashSet <Subscription <WorldCommandSender> > >(); } var subscription = new Subscription <WorldCommandSender>(this, entityId); if (!entityIdToSenderSubscriptions.TryGetValue(entityId, out var subscriptions)) { subscriptions = new HashSet <Subscription <WorldCommandSender> >(); entityIdToSenderSubscriptions.Add(entityId, subscriptions); } if (WorkerSystem.TryGetEntity(entityId, out var entity)) { subscription.SetAvailable(new WorldCommandSender(entity, World)); } subscriptions.Add(subscription); return(subscription); }
public WorldCommandSenderSubscriptionManager(World world) { this.world = world; // Check that these are there workerSystem = world.GetExistingManager <WorkerSystem>(); var constraintsSystem = world.GetExistingManager <ComponentConstraintsCallbackSystem>(); constraintsSystem.RegisterEntityAddedCallback(entityId => { if (!entityIdToSenderSubscriptions.TryGetValue(entityId, out var subscriptions)) { return; } workerSystem.TryGetEntity(entityId, out var entity); foreach (var subscription in subscriptions) { subscription.SetAvailable(new WorldCommandSender(entity, world)); } }); constraintsSystem.RegisterEntityAddedCallback(entityId => { if (!entityIdToSenderSubscriptions.TryGetValue(entityId, out var subscriptions)) { return; } foreach (var subscription in subscriptions) { ResetValue(subscription); subscription.SetUnavailable(); } }); }