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

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

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

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

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

                    ListPool <IComponentAddedListener <TScope, TComponent> > .Instance.Return(list);
                }
            })
            .End();
            // @formatter:on
            return(this);
        }
Пример #2
0
        public GlobalComponentNotificationsBuilder <TScope> AddAddedNotifications <TComponent>()
        {
            // @formatter:off
            Parent
            .Components <Singletons>()
            .Add <ComponentAddedListeners <TScope, TComponent> >()
            .End()
            .Messages()
            .Add <ComponentAdded <TScope, TComponent> >()
            .End()
            .Systems()
            .Setup((contexts, bus) => {
                var unused = new ComponentAddedMessageProducer <TScope, TComponent>(contexts, bus);

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

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

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