public ComponentNotificationsBuilder <TScope> AddRemovedNotifications <TComponent>()
        {
            // @formatter:off
            Parent
            .Components <TScope>()
            .Add <ComponentRemovedListeners <TScope, TComponent> >()
            .End()
            .Messages()
            .Add <ComponentRemoved <TScope, TComponent> >()
            .End()
            .Systems()
            .Setup((contexts, bus) => {
                var unused = new ComponentRemovedMessageProducer <TScope, TComponent>(contexts, bus);
            })
            .Update((contexts, bus) => {
                foreach (var message in bus.All <ComponentRemoved <TScope, TComponent> >())
                {
                    var entity = message.Entity;

                    if (!entity.Has <ComponentRemovedListeners <TScope, TComponent> >())
                    {
                        continue;
                    }

                    var listeners = entity.Get <ComponentRemovedListeners <TScope, TComponent> >().Listeners;

                    foreach (var listener in listeners)
                    {
                        listener.OnComponentRemoved(message.Entity, message.Component);
                    }
                }
            })
            .Cleanup((contexts, bus) =>
            {
                var context = contexts.Get <Game>();

                foreach (var entity in context.AllWith <Destroyed, ComponentRemovedListeners <TScope, TComponent> >())
                {
                    var list = entity.Get <ComponentRemovedListeners <TScope, TComponent> >().Listeners;

                    ListPool <IComponentRemovedListener <TScope, TComponent> > .Instance.Return(list);
                }
            })
            .End();
            // @formatter:on
            return(this);
        }
Exemple #2
0
        public GlobalComponentNotificationsBuilder <TScope> AddRemovedNotifications <TComponent>()
        {
            // @formatter:off
            Parent
            .Components <Singletons>()
            .Add <ComponentRemovedListeners <TScope, TComponent> >()
            .End()
            .Messages()
            .Add <ComponentRemoved <TScope, TComponent> >()
            .End()
            .Systems()
            .Setup((contexts, bus) => {
                var unused = new ComponentRemovedMessageProducer <TScope, TComponent>(contexts, bus);

                contexts.Singleton().Add(new ComponentRemovedListeners <TScope, TComponent>(new List <IComponentRemovedListener <TScope, TComponent> >()));
            })
            .Update((contexts, bus) => {
                var listeners = contexts.Singleton().Get <ComponentRemovedListeners <TScope, TComponent> >().Listeners;

                if (listeners.Count == 0)
                {
                    return;
                }

                foreach (var message in bus.All <ComponentRemoved <TScope, TComponent> >())
                {
                    foreach (var listener in listeners)
                    {
                        listener.OnComponentRemoved(message.Entity, message.Component);
                    }
                }
            })
            .End();
            // @formatter:on
            return(this);
        }