public void should_execute_system_without_predicate()
        {
            var fakeEntities = new List <IEntity>
            {
                Substitute.For <IEntity>(),
                Substitute.For <IEntity>()
            };

            var mockObservableGroup = Substitute.For <IObservableGroup>();

            mockObservableGroup.GetEnumerator().Returns(fakeEntities.GetEnumerator());

            var mockCollectionManager = Substitute.For <IEntityCollectionManager>();
            var threadHandler         = Substitute.For <IThreadHandler>();

            var fakeGroup = new Group();

            mockCollectionManager.GetObservableGroup(Arg.Is(fakeGroup), Arg.Any <int[]>()).Returns(mockObservableGroup);

            var observableSubject = new Subject <IObservableGroup>();
            var mockSystem        = Substitute.For <IReactToGroupSystem>();

            mockSystem.Group.Returns(fakeGroup);
            mockSystem.ReactToGroup(Arg.Is(mockObservableGroup)).Returns(observableSubject);

            var systemHandler = new ReactToGroupSystemHandler(mockCollectionManager, threadHandler);

            systemHandler.SetupSystem(mockSystem);

            observableSubject.OnNext(mockObservableGroup);

            mockSystem.ReceivedWithAnyArgs(2).Process(Arg.Any <IEntity>());
            Assert.Equal(1, systemHandler._systemSubscriptions.Count);
            Assert.NotNull(systemHandler._systemSubscriptions[mockSystem]);
        }
Example #2
0
        public void AddSystem(ISystem system)
        {
            _systems.Add(system);

            if (system is ISetupSystem)
            {
                SetupSystemHandler.Setup(system as ISetupSystem);
            }

            if (system is IReactToGroupSystem)
            {
                var subscription = ReactToGroupSystemHandler.Setup(system as IReactToGroupSystem);
                _systemSubscriptions.Add(system, new List <SubscriptionToken> {
                    subscription
                });
            }

            if (system is IReactToEntitySystem)
            {
                var subscriptions = ReactToEntitySystemHandler.Setup(system as IReactToEntitySystem);
                _systemSubscriptions.Add(system, new List <SubscriptionToken>(subscriptions));
            }

            if (system.IsReactiveDataSystem())
            {
                var subscriptions = ReactToDataSystemHandler.SetupWithoutType(system);
                _systemSubscriptions.Add(system, new List <SubscriptionToken>(subscriptions));
            }
        }
        public void should_execute_system_without_predicate()
        {
            var fakeEntities = new[]
            {
                Substitute.For <IEntity>(),
                Substitute.For <IEntity>()
            };

            var mockObservableGroup = Substitute.For <IObservableGroup>();

            mockObservableGroup.Entities.Returns(fakeEntities);

            var mockCollectionManager = Substitute.For <IEntityCollectionManager>();

            var fakeGroup = new Group();

            mockCollectionManager.CreateObservableGroup(Arg.Is(fakeGroup)).Returns(mockObservableGroup);

            var observableSubject = new Subject <IObservableGroup>();
            var mockSystem        = Substitute.For <IReactToGroupSystem>();

            mockSystem.TargetGroup.Returns(fakeGroup);
            mockSystem.ReactToGroup(Arg.Is(mockObservableGroup)).Returns(observableSubject);

            var systemHandler = new ReactToGroupSystemHandler(mockCollectionManager);

            systemHandler.SetupSystem(mockSystem);

            observableSubject.OnNext(mockObservableGroup);

            mockSystem.ReceivedWithAnyArgs(2).Execute(Arg.Any <IEntity>());
            Assert.Equal(1, systemHandler._systemSubscriptions.Count);
            Assert.NotNull(systemHandler._systemSubscriptions[mockSystem]);
        }
Example #4
0
        public void AddSystem(ISystem system)
        {
            _systems.Add(system);
            var subscriptionList = new List <SubscriptionToken>();

            if (system is ISetupSystem)
            {
                var subscriptions = SetupSystemHandler.Setup(system as ISetupSystem);
                subscriptionList.AddRange(subscriptions);
            }

            if (system is IReactToGroupSystem)
            {
                var subscription = ReactToGroupSystemHandler.Setup(system as IReactToGroupSystem);
                subscriptionList.Add(subscription);
            }

            if (system is IReactToEntitySystem)
            {
                var subscriptions = ReactToEntitySystemHandler.Setup(system as IReactToEntitySystem);
                subscriptionList.AddRange(subscriptions);
            }

            if (system is IManualSystem)
            {
                ManualSystemHandler.Start(system as IManualSystem);
            }

            _systemSubscriptions.Add(system, subscriptionList);
        }
Example #5
0
        private SystemExecutor CreateExecutor()
        {
            var messageBroker         = new MessageBroker();
            var identityGenerator     = new SequentialIdentityGenerator();
            var poolManager           = new PoolManager(identityGenerator, messageBroker);
            var reactsToEntityHandler = new ReactToEntitySystemHandler(poolManager);
            var reactsToGroupHandler  = new ReactToGroupSystemHandler(poolManager);
            var reactsToDataHandler   = new ReactToDataSystemHandler(poolManager);
            var setupHandler          = new SetupSystemHandler(poolManager);

            return(new SystemExecutor(poolManager, messageBroker, reactsToEntityHandler, reactsToGroupHandler, setupHandler, reactsToDataHandler));
        }
Example #6
0
        public void should_correctly_handle_systems()
        {
            var mockCollectionManager      = Substitute.For <IEntityCollectionManager>();
            var reactToEntitySystemHandler = new ReactToGroupSystemHandler(mockCollectionManager);

            var fakeMatchingSystem     = Substitute.For <IReactToGroupSystem>();
            var fakeNonMatchingSystem1 = Substitute.For <ISetupSystem>();
            var fakeNonMatchingSystem2 = Substitute.For <ISystem>();

            Assert.True(reactToEntitySystemHandler.CanHandleSystem(fakeMatchingSystem));
            Assert.False(reactToEntitySystemHandler.CanHandleSystem(fakeNonMatchingSystem1));
            Assert.False(reactToEntitySystemHandler.CanHandleSystem(fakeNonMatchingSystem2));
        }
Example #7
0
        public void should_destroy_and_dispose_system()
        {
            var mockCollectionManager = Substitute.For <IEntityCollectionManager>();
            var mockSystem            = Substitute.For <IReactToGroupSystem>();
            var mockDisposable        = Substitute.For <IDisposable>();

            var systemHandler = new ReactToGroupSystemHandler(mockCollectionManager);

            systemHandler._systemSubscriptions.Add(mockSystem, mockDisposable);
            systemHandler.DestroySystem(mockSystem);

            mockDisposable.Received(1).Dispose();
            Assert.Equal(0, systemHandler._systemSubscriptions.Count);
        }
        public void should_return_valid_subscription_token_when_processing()
        {
            var mockPoolManager  = Substitute.For <IPoolManager>();
            var mockSystem       = Substitute.For <IReactToGroupSystem>();
            var mockSubscription = Substitute.For <IObservable <GroupAccessor> >();

            mockSystem.ReactToGroup(Arg.Any <GroupAccessor>()).Returns(mockSubscription);

            var handler           = new ReactToGroupSystemHandler(mockPoolManager);
            var subscriptionToken = handler.Setup(mockSystem);

            Assert.That(subscriptionToken, Is.Not.Null);
            Assert.That(subscriptionToken.AssociatedObject, Is.Null);
            Assert.That(subscriptionToken.Disposable, Is.Not.Null);
        }
Example #9
0
        public void should_correctly_handle_systems()
        {
            var observableGroupManager     = Substitute.For <IObservableGroupManager>();
            var threadHandler              = Substitute.For <IThreadHandler>();
            var reactToEntitySystemHandler = new ReactToGroupSystemHandler(observableGroupManager, threadHandler);

            var fakeMatchingSystem     = Substitute.For <IReactToGroupSystem>();
            var fakeMatchingSystem2    = Substitute.For <IReactToGroupExSystem>();
            var fakeNonMatchingSystem1 = Substitute.For <ISetupSystem>();
            var fakeNonMatchingSystem2 = Substitute.For <IGroupSystem>();

            Assert.True(reactToEntitySystemHandler.CanHandleSystem(fakeMatchingSystem));
            Assert.True(reactToEntitySystemHandler.CanHandleSystem(fakeMatchingSystem2));
            Assert.False(reactToEntitySystemHandler.CanHandleSystem(fakeNonMatchingSystem1));
            Assert.False(reactToEntitySystemHandler.CanHandleSystem(fakeNonMatchingSystem2));
        }
Example #10
0
        private SystemExecutor CreateExecutor()
        {
            var messageBroker         = new EventSystem(new MessageBroker());
            var entityFactory         = new DefaultEntityFactory(messageBroker);
            var poolFactory           = new DefaultPoolFactory(entityFactory, messageBroker);
            var groupAccessorFactory  = new DefaultGroupAccessorFactory(messageBroker);
            var poolManager           = new PoolManager(messageBroker, poolFactory, groupAccessorFactory);
            var reactsToEntityHandler = new ReactToEntitySystemHandler(poolManager);
            var reactsToGroupHandler  = new ReactToGroupSystemHandler(poolManager);
            var reactsToDataHandler   = new ReactToDataSystemHandler(poolManager);
            var manualSystemHandler   = new ManualSystemHandler(poolManager);
            var setupHandler          = new SetupSystemHandler(poolManager);

            return(new SystemExecutor(poolManager, messageBroker, reactsToEntityHandler,
                                      reactsToGroupHandler, setupHandler, reactsToDataHandler, manualSystemHandler));
        }
Example #11
0
        private SystemExecutor CreateExecutor(IEntityCollectionManager entityCollectionManager)
        {
            var reactsToEntityHandler = new ReactToEntitySystemHandler(entityCollectionManager);
            var reactsToGroupHandler  = new ReactToGroupSystemHandler(entityCollectionManager);
            var reactsToDataHandler   = new ReactToDataSystemHandler(entityCollectionManager);
            var manualSystemHandler   = new ManualSystemHandler(entityCollectionManager);
            var setupHandler          = new SetupSystemHandler(entityCollectionManager);

            var conventionalSystems = new List <IConventionalSystemHandler>
            {
                setupHandler,
                reactsToEntityHandler,
                reactsToGroupHandler,
                reactsToDataHandler,
                manualSystemHandler
            };

            return(new SystemExecutor(conventionalSystems));
        }
Example #12
0
        protected EcsRxApplication()
        {
            // For sending events around
            EventSystem = new EventSystem(new MessageBroker());

            // For mapping component types to underlying indexes
            var componentTypeAssigner = new DefaultComponentTypeAssigner();
            var allComponents         = componentTypeAssigner.GenerateComponentLookups();

            var componentLookup = new ComponentTypeLookup(allComponents);
            // For interacting with the component databases
            var componentDatabase   = new ComponentDatabase(componentLookup);
            var componentRepository = new ComponentRepository(componentLookup, componentDatabase);

            // For creating entities, collections, observable groups and managing Ids
            var entityFactory           = new DefaultEntityFactory(new IdPool(), componentRepository);
            var entityCollectionFactory = new DefaultEntityCollectionFactory(entityFactory);
            var observableGroupFactory  = new DefaultObservableObservableGroupFactory();

            EntityCollectionManager = new EntityCollectionManager(entityCollectionFactory, observableGroupFactory, componentLookup);

            // All system handlers for the system types you want to support
            var reactsToEntityHandler = new ReactToEntitySystemHandler(EntityCollectionManager);
            var reactsToGroupHandler  = new ReactToGroupSystemHandler(EntityCollectionManager);
            var reactsToDataHandler   = new ReactToDataSystemHandler(EntityCollectionManager);
            var manualSystemHandler   = new ManualSystemHandler(EntityCollectionManager);
            var setupHandler          = new SetupSystemHandler(EntityCollectionManager);
            var teardownHandler       = new TeardownSystemHandler(EntityCollectionManager);

            var conventionalSystems = new List <IConventionalSystemHandler>
            {
                setupHandler,
                teardownHandler,
                reactsToEntityHandler,
                reactsToGroupHandler,
                reactsToDataHandler,
                manualSystemHandler
            };

            // The main executor which manages how systems are given information
            SystemExecutor = new SystemExecutor(conventionalSystems);
        }
Example #13
0
        public void should_only_execute_system_when_predicate_met()
        {
            var entityToMatch = Substitute.For <IEntity>();
            var idToMatch     = 1;

            entityToMatch.Id.Returns(idToMatch);

            var fakeEntities = new List <IEntity>
            {
                entityToMatch,
                Substitute.For <IEntity>()
            };


            var mockObservableGroup = Substitute.For <IObservableGroup>();

            mockObservableGroup.GetEnumerator().Returns(fakeEntities.GetEnumerator());

            var observableGroupManager = Substitute.For <IObservableGroupManager>();
            var threadHandler          = Substitute.For <IThreadHandler>();

            var fakeGroup = new GroupWithPredicate(x => x.Id == idToMatch);

            observableGroupManager.GetObservableGroup(Arg.Is(fakeGroup), Arg.Any <int[]>()).Returns(mockObservableGroup);

            var observableSubject = new Subject <IObservableGroup>();
            var mockSystem        = Substitute.For <IReactToGroupSystem>();

            mockSystem.Group.Returns(fakeGroup);
            mockSystem.ReactToGroup(Arg.Is(mockObservableGroup)).Returns(observableSubject);

            var systemHandler = new ReactToGroupSystemHandler(observableGroupManager, threadHandler);

            systemHandler.SetupSystem(mockSystem);

            observableSubject.OnNext(mockObservableGroup);

            mockSystem.ReceivedWithAnyArgs(1).Process(Arg.Is(entityToMatch));
            Assert.Equal(1, systemHandler._systemSubscriptions.Count);
            Assert.NotNull(systemHandler._systemSubscriptions[mockSystem]);
        }
        public void should_only_execute_system_when_predicate_met()
        {
            var entityToMatch = Substitute.For <IEntity>();
            var guidToMatch   = Guid.NewGuid();

            entityToMatch.Id.Returns(guidToMatch);

            var fakeEntities = new[]
            {
                entityToMatch,
                Substitute.For <IEntity>()
            };


            var mockObservableGroup = Substitute.For <IObservableGroup>();

            mockObservableGroup.Entities.Returns(fakeEntities);

            var mockCollectionManager = Substitute.For <IEntityCollectionManager>();

            var fakeGroup = new Group(x => x.Id == guidToMatch);

            mockCollectionManager.CreateObservableGroup(Arg.Is(fakeGroup)).Returns(mockObservableGroup);

            var observableSubject = new Subject <IObservableGroup>();
            var mockSystem        = Substitute.For <IReactToGroupSystem>();

            mockSystem.TargetGroup.Returns(fakeGroup);
            mockSystem.ReactToGroup(Arg.Is(mockObservableGroup)).Returns(observableSubject);

            var systemHandler = new ReactToGroupSystemHandler(mockCollectionManager);

            systemHandler.SetupSystem(mockSystem);

            observableSubject.OnNext(mockObservableGroup);

            mockSystem.ReceivedWithAnyArgs(1).Execute(Arg.Is(entityToMatch));
            Assert.Equal(1, systemHandler._systemSubscriptions.Count);
            Assert.NotNull(systemHandler._systemSubscriptions[mockSystem]);
        }
Example #15
0
        private SystemExecutor CreateExecutor(IObservableGroupManager observableGroupManager)
        {
            var threadHandler         = new DefaultThreadHandler();
            var reactsToEntityHandler = new ReactToEntitySystemHandler(observableGroupManager);
            var reactsToGroupHandler  = new ReactToGroupSystemHandler(observableGroupManager, threadHandler);
            var reactsToDataHandler   = new ReactToDataSystemHandler(observableGroupManager);
            var manualSystemHandler   = new ManualSystemHandler(observableGroupManager);
            var setupHandler          = new SetupSystemHandler(observableGroupManager);
            var teardownHandler       = new TeardownSystemHandler(observableGroupManager);

            var conventionalSystems = new List <IConventionalSystemHandler>
            {
                setupHandler,
                reactsToEntityHandler,
                reactsToGroupHandler,
                reactsToDataHandler,
                manualSystemHandler,
                teardownHandler
            };

            return(new SystemExecutor(conventionalSystems));
        }