public void GlobalSetup()
        {
            var componentTypeAssigner = new DefaultComponentTypeAssigner();
            var allComponents         = componentTypeAssigner.GenerateComponentLookups();
            var componentLookup       = new ComponentTypeLookup(allComponents);

            _availableComponents = allComponents.Keys
                                   .Select(x => Activator.CreateInstance(x) as IComponent)
                                   .ToArray();

            var componentDatabase   = new ComponentDatabase(componentLookup);
            var componentRepository = new ComponentRepository(componentLookup, componentDatabase);

            var entityFactory          = new DefaultEntityFactory(new IdPool(), componentRepository);
            var poolFactory            = new DefaultEntityCollectionFactory(entityFactory);
            var observableGroupFactory = new DefaultObservableObservableGroupFactory();

            _entityCollectionManager = new EntityCollectionManager(poolFactory, observableGroupFactory, componentLookup);

            _availableComponents = _groupFactory.GetComponentTypes
                                   .Select(x => Activator.CreateInstance(x) as IComponent)
                                   .ToArray();

            _testGroups = _groupFactory.CreateTestGroups().ToArray();

            foreach (var group in _testGroups)
            {
                _entityCollectionManager.GetObservableGroup(group);
            }

            _defaultEntityCollection = _entityCollectionManager.GetCollection();
        }
Example #2
0
        public void Setup(IDependencyContainer container)
        {
            container.Bind <IMessageBroker, MessageBroker>();
            container.Bind <IEventSystem, EventSystem>();
            container.Bind <IIdPool, IdPool>();
            container.Bind <IThreadHandler, DefaultThreadHandler>();
            container.Bind <IEntityFactory, DefaultEntityFactory>();
            container.Bind <IEntityCollectionFactory, DefaultEntityCollectionFactory>();
            container.Bind <IObservableGroupFactory, DefaultObservableObservableGroupFactory>();
            container.Bind <IEntityCollectionManager, EntityCollectionManager>();
            container.Bind <IObservableGroupManager>(x => x.ToMethod(y => y.Resolve <IEntityCollectionManager>()));
            container.Bind <IConventionalSystemHandler, ManualSystemHandler>();
            container.Bind <ISystemExecutor, SystemExecutor>();

            var componentTypeAssigner = new DefaultComponentTypeAssigner();
            var allComponents         = componentTypeAssigner.GenerateComponentLookups();
            var componentLookup       = new ComponentTypeLookup(allComponents);

            container.Bind <IComponentTypeAssigner>(new BindingConfiguration {
                ToInstance = componentTypeAssigner
            });
            container.Bind <IComponentTypeLookup>(new BindingConfiguration {
                ToInstance = componentLookup
            });
            container.Bind <IComponentDatabase, ComponentDatabase>();
        }
Example #3
0
        public void Setup(IDependencyContainer container)
        {
            container.Bind <IMessageBroker, MessageBroker>();
            container.Bind <IEventSystem, EventSystem>();
            container.Bind <IIdPool, IdPool>();
            container.Bind <IEntityFactory, DefaultEntityFactory>();
            container.Bind <IEntityCollectionFactory, DefaultEntityCollectionFactory>();
            container.Bind <IObservableGroupFactory, DefaultObservableObservableGroupFactory>();
            container.Bind <IEntityCollectionManager, EntityCollectionManager>();
            container.Bind <IConventionalSystemHandler, ReactToEntitySystemHandler>();
            container.Bind <IConventionalSystemHandler, ReactToGroupSystemHandler>();
            container.Bind <IConventionalSystemHandler, ReactToDataSystemHandler>();
            container.Bind <IConventionalSystemHandler, ManualSystemHandler>();
            container.Bind <IConventionalSystemHandler, SetupSystemHandler>();
            container.Bind <IConventionalSystemHandler, TeardownSystemHandler>();
            container.Bind <ISystemExecutor, SystemExecutor>();

            var componentTypeAssigner = new DefaultComponentTypeAssigner();
            var allComponents         = componentTypeAssigner.GenerateComponentLookups();
            var componentLookup       = new ComponentTypeLookup(allComponents);

            container.Bind <IComponentTypeAssigner>(new BindingConfiguration {
                BindInstance = componentTypeAssigner
            });
            container.Bind <IComponentTypeLookup>(new BindingConfiguration {
                BindInstance = componentLookup
            });
            container.Bind <IComponentDatabase, ComponentDatabase>();
            container.Bind <IComponentRepository, ComponentRepository>();
        }
Example #4
0
        public void Initialize()
        {
            var componentTypes = ComponentTypeLookup.GetAllComponentTypes().ToArray();
            var componentCount = componentTypes.Length;

            ComponentData = new IComponentPool[componentCount];

            for (var i = 0; i < componentCount; i++)
            {
                ComponentData[i] = CreatePoolFor(componentTypes[i].Key, DefaultExpansionAmount);
            }
        }
Example #5
0
        public ref T AddComponent <T>(int componentTypeId) where T : IComponent, new()
        {
            var defaultComponent = ComponentTypeLookup.CreateDefault <T>();
            var allocationId     = ComponentDatabase.Allocate(componentTypeId);

            InternalComponentAllocations[componentTypeId] = allocationId;
            ComponentDatabase.Set(componentTypeId, allocationId, defaultComponent);

            _onComponentsAdded.OnNext(new [] { componentTypeId });

            return(ref ComponentDatabase.GetRef <T>(componentTypeId, allocationId));
        }
        public void should_corectly_get_matching_entities()
        {
            // easier to test with real stuff
            var componentLookups = new Dictionary <Type, int>
            {
                { typeof(TestComponentOne), 0 },
                { typeof(TestComponentTwo), 1 },
                { typeof(TestComponentThree), 2 }
            };
            var componentLookupType = new ComponentTypeLookup(componentLookups);
            var componentDatabase   = new ComponentDatabase(componentLookupType);
            var componentRepository = new ComponentRepository(componentLookupType, componentDatabase);

            var hasOneAndTwo = new Entity(1, componentRepository);

            hasOneAndTwo.AddComponent <TestComponentOne>();
            hasOneAndTwo.AddComponent <TestComponentTwo>();

            var hasAllComponents = new Entity(2, componentRepository);

            hasAllComponents.AddComponent <TestComponentOne>();
            hasAllComponents.AddComponent <TestComponentTwo>();
            hasAllComponents.AddComponent <TestComponentThree>();

            var hasOneAndThree = new Entity(3, componentRepository);

            hasOneAndThree.AddComponent <TestComponentOne>();
            hasOneAndThree.AddComponent <TestComponentThree>();

            var entityGroup = new [] { hasOneAndTwo, hasAllComponents, hasOneAndThree };

            var matchGroup1 = new Group(typeof(TestComponentOne), typeof(TestComponentTwo));
            var matchGroup2 = new Group(null, new [] { typeof(TestComponentOne), typeof(TestComponentTwo) }, new[] { typeof(TestComponentThree) });
            var matchGroup3 = new Group(null, new Type[0], new[] { typeof(TestComponentTwo) });


            var group1Results1 = entityGroup.MatchingGroup(matchGroup1).ToArray();

            Assert.Equal(2, group1Results1.Length);
            Assert.Contains(hasOneAndTwo, group1Results1);
            Assert.Contains(hasAllComponents, group1Results1);

            var group1Results2 = entityGroup.MatchingGroup(matchGroup2).ToArray();

            Assert.Equal(1, group1Results2.Length);
            Assert.Contains(hasOneAndTwo, group1Results2);

            var group1Results3 = entityGroup.MatchingGroup(matchGroup3).ToArray();

            Assert.Equal(1, group1Results3.Length);
            Assert.Contains(hasOneAndThree, group1Results3);
        }
        public void AddComponents(IReadOnlyList <IComponent> components)
        {
            var componentTypeIds = new int[components.Count];

            for (var i = 0; i < components.Count; i++)
            {
                var componentTypeId = ComponentTypeLookup.GetComponentType(components[i].GetType());
                var allocationId    = ComponentDatabase.Allocate(componentTypeId);
                InternalComponentAllocations[componentTypeId] = allocationId;
                ComponentDatabase.Set(componentTypeId, allocationId, components[i]);
            }

            _onComponentsAdded.OnNext(componentTypeIds);
        }
Example #8
0
        public void GlobalSetup()
        {
            var componentTypeAssigner = new DefaultComponentTypeAssigner();
            var allComponents         = componentTypeAssigner.GenerateComponentLookups();
            var componentLookup       = new ComponentTypeLookup(allComponents);

            _availableComponents = allComponents.Keys
                                   .Select(x => Activator.CreateInstance(x) as IComponent)
                                   .ToArray();

            var componentDatabase = new ComponentDatabase(componentLookup);

            _componentRepository = new ComponentRepository(componentLookup, componentDatabase);
        }
        protected override IDisposable ReactToPools()
        {
            var componentType1 = ComponentTypeLookup.GetComponentType(typeof(T1));
            var componentType2 = ComponentTypeLookup.GetComponentType(typeof(T2));
            var pool1          = ComponentDatabase.GetPoolFor <T1>(componentType1);
            var pool2          = ComponentDatabase.GetPoolFor <T2>(componentType2);

            var subscriptions = new CompositeDisposable();

            pool1.OnPoolExtending.Subscribe(_ => Refresh()).AddTo(subscriptions);
            pool2.OnPoolExtending.Subscribe(_ => Refresh()).AddTo(subscriptions);

            return(subscriptions);
        }
Example #10
0
        public void Setup(IDependencyContainer container)
        {
            container.Bind <IMessageBroker, MessageBroker>();
            container.Bind <IEventSystem, EventSystem>();
            container.Bind <IIdPool, IdPool>();
            container.Bind <IEntityFactory, DefaultEntityFactory>();
            container.Bind <IEntityCollectionFactory, DefaultEntityCollectionFactory>();
            container.Bind <IObservableGroupFactory, DefaultObservableObservableGroupFactory>();
            container.Bind <IEntityCollectionManager, EntityCollectionManager>();
            container.Bind <IConventionalSystemHandler, ReactToEntitySystemHandler>();
            container.Bind <IConventionalSystemHandler, ReactToGroupSystemHandler>();
            container.Bind <IConventionalSystemHandler, ReactToDataSystemHandler>();
            container.Bind <IConventionalSystemHandler, ManualSystemHandler>();
            container.Bind <IConventionalSystemHandler, SetupSystemHandler>();
            container.Bind <IConventionalSystemHandler, TeardownSystemHandler>();
            container.Bind <ISystemExecutor, SystemExecutor>();

            var explicitTypeLookups = new Dictionary <Type, int>
            {
                { typeof(Component1), 0 },
                { typeof(Component2), 1 },
                { typeof(Component3), 2 },
                { typeof(Component4), 3 },
                { typeof(Component5), 4 },
                { typeof(Component6), 5 },
                { typeof(Component7), 6 },
                { typeof(Component8), 7 },
                { typeof(Component9), 8 },
                { typeof(Component10), 9 },
                { typeof(Component11), 10 },
                { typeof(Component12), 11 },
                { typeof(Component13), 12 },
                { typeof(Component14), 13 },
                { typeof(Component15), 14 },
                { typeof(Component16), 15 },
                { typeof(Component17), 16 },
                { typeof(Component18), 17 },
                { typeof(Component19), 18 },
                { typeof(Component20), 19 }
            };

            var explicitComponentLookup = new ComponentTypeLookup(explicitTypeLookups);

            container.Bind <IComponentTypeLookup>(new BindingConfiguration {
                BindInstance = explicitComponentLookup
            });
            container.Bind <IComponentDatabase, ComponentDatabase>();
            container.Bind <IComponentRepository, ComponentRepository>();
        }
        public void Setup(IDependencyContainer container)
        {
            container.Unbind <IComponentTypeLookup>();
            var explicitTypeLookups = new Dictionary <Type, int>
            {
                { typeof(NameComponent), ComponentLookupTypes.NameComponentId },
                { typeof(PositionComponent), ComponentLookupTypes.PositionComponentId },
                { typeof(MovementSpeedComponent), ComponentLookupTypes.MovementSpeedComponentId }
            };
            var explicitComponentLookup = new ComponentTypeLookup(explicitTypeLookups);

            container.Bind <IComponentTypeLookup>(new BindingConfiguration {
                ToInstance = explicitComponentLookup
            });
        }
Example #12
0
        private IEntityCollectionManager CreateCollectionManager()
        {
            var componentLookups = new Dictionary <Type, int>
            {
                { typeof(TestComponentOne), 0 },
                { typeof(TestComponentTwo), 1 },
                { typeof(TestComponentThree), 2 }
            };
            var componentLookupType    = new ComponentTypeLookup(componentLookups);
            var componentDatabase      = new ComponentDatabase(componentLookupType);
            var componentRepository    = new ComponentRepository(componentLookupType, componentDatabase);
            var entityFactory          = new DefaultEntityFactory(new IdPool(), componentRepository);
            var collectionFactory      = new DefaultEntityCollectionFactory(entityFactory);
            var observableGroupFactory = new DefaultObservableObservableGroupFactory();

            return(new EntityCollectionManager(collectionFactory, observableGroupFactory, componentLookupType));
        }
Example #13
0
        public IReferenceBatchAccessor <T1, T2> GetReferenceAccessorFor <T1, T2>(IObservableGroup observableGroup)
            where T1 : class, IComponent
            where T2 : class, IComponent
        {
            var componentTypes = ComponentTypeLookup.GetComponentTypes(typeof(T1), typeof(T2));
            var token          = new AccessorToken(componentTypes, observableGroup);

            if (BatchAccessors.ContainsKey(token))
            {
                return((IReferenceBatchAccessor <T1, T2>)BatchAccessors[token]);
            }

            var batchBuilder  = ReferenceBatchBuilderFactory.Create <T1, T2>();
            var batchAccessor = new ReferenceBatchAccessor <T1, T2>(observableGroup, ComponentDatabase, batchBuilder, ComponentTypeLookup);

            BatchAccessors.Add(token, batchAccessor);

            return(batchAccessor);
        }
Example #14
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 #15
0
        public IBatchAccessor <T1, T2, T3> GetAccessorFor <T1, T2, T3>(IObservableGroup observableGroup)
            where T1 : unmanaged, IComponent
            where T2 : unmanaged, IComponent
            where T3 : unmanaged, IComponent
        {
            var componentTypes = ComponentTypeLookup.GetComponentTypes(typeof(T1), typeof(T2), typeof(T3));
            var token          = new AccessorToken(componentTypes, observableGroup);

            if (BatchAccessors.ContainsKey(token))
            {
                return((IBatchAccessor <T1, T2, T3>)BatchAccessors[token]);
            }

            var batchBuilder  = BatchBuilderFactory.Create <T1, T2, T3>();
            var batchAccessor = new BatchAccessor <T1, T2, T3>(observableGroup, ComponentDatabase, batchBuilder, ComponentTypeLookup);

            BatchAccessors.Add(token, batchAccessor);

            return(batchAccessor);
        }
Example #16
0
        private (IEntityCollectionManager, IComponentDatabase, IComponentTypeLookup) CreateFramework()
        {
            var componentLookups = new Dictionary <Type, int>
            {
                { typeof(TestComponentOne), 0 },
                { typeof(TestComponentTwo), 1 },
                { typeof(TestComponentThree), 2 },
                { typeof(ViewComponent), 3 },
                { typeof(TestStructComponentOne), 4 },
                { typeof(TestStructComponentTwo), 5 }
            };
            var componentLookupType    = new ComponentTypeLookup(componentLookups);
            var componentDatabase      = new ComponentDatabase(componentLookupType);
            var entityFactory          = new DefaultEntityFactory(new IdPool(), componentDatabase, componentLookupType);
            var collectionFactory      = new DefaultEntityCollectionFactory(entityFactory);
            var observableGroupFactory = new DefaultObservableObservableGroupFactory();
            var collectionManager      = new EntityCollectionManager(collectionFactory, observableGroupFactory, componentLookupType);

            return(collectionManager, componentDatabase, componentLookupType);
        }
Example #17
0
        public void Setup(IDependencyContainer container)
        {
            container.Bind <IMessageBroker, MessageBroker>();
            container.Bind <IEventSystem, EventSystem>();
            container.Bind <IIdPool, IdPool>();
            container.Bind <IEntityFactory, DefaultEntityFactory>();
            container.Bind <IEntityCollectionFactory, DefaultEntityCollectionFactory>();
            container.Bind <IObservableGroupFactory, DefaultObservableObservableGroupFactory>();
            container.Bind <IEntityCollectionManager, EntityCollectionManager>();
            container.Bind <IConventionalSystemHandler, ManualSystemHandler>();
            container.Bind <ISystemExecutor, SystemExecutor>();

            var componentNamespace      = typeof(Component1).Namespace;
            var componentTypes          = typeof(Component1).Assembly.GetTypes().Where(x => x.Namespace == componentNamespace);
            var explicitTypeLookups     = componentTypes.Select((type, i) => new { type, i }).ToDictionary(x => x.type, x => x.i);
            var explicitComponentLookup = new ComponentTypeLookup(explicitTypeLookups);

            container.Bind <IComponentTypeLookup>(new BindingConfiguration {
                ToInstance = explicitComponentLookup
            });
            container.Bind <IComponentDatabase, ComponentDatabase>();
        }
Example #18
0
        public IComponent GetComponent(Type componentType)
        {
            var componentTypeId = ComponentTypeLookup.GetComponentType(componentType);

            return(GetComponent(componentTypeId));
        }
Example #19
0
        public bool HasComponent(Type componentType)
        {
            var componentTypeId = ComponentTypeLookup.GetComponentType(componentType);

            return(HasComponent(componentTypeId));
        }
Example #20
0
        public void RemoveComponents(params Type[] componentTypes)
        {
            var componentTypeIds = ComponentTypeLookup.GetComponentTypes(componentTypes);

            RemoveComponents(componentTypeIds);
        }