public void IndexGetsLastComponentThatTriggeredAddingEntityToGroup()
        {
            _context = new MyTestContext();

            IComponent receivedComponent = null;

            _group = _context.GetGroup(Matcher <MyTestEntity> .AllOf(CID.ComponentA, CID.ComponentB));
            _index = new EntityIndex <MyTestEntity, string>("TestIndex", _group, (e, c) =>
            {
                receivedComponent = c;
                return(((NameAgeComponent)c).name);
            });

            var nameAgeComponent1 = new NameAgeComponent();

            nameAgeComponent1.name = "Max";

            var nameAgeComponent2 = new NameAgeComponent();

            nameAgeComponent2.name = "Jack";

            var entity = _context.CreateEntity();

            entity.AddComponent(CID.ComponentA, nameAgeComponent1);
            entity.AddComponent(CID.ComponentB, nameAgeComponent2);

            Assert.AreEqual(nameAgeComponent2, receivedComponent);
        }
示例#2
0
        public void ThrowsWhenTargetAlreadyHasComponent()
        {
            _originalEntity.AddComponentA();
            _originalEntity.AddComponent(MyTestComponentsLookup.ComponentB, _nameAge);
            var component = new NameAgeComponent();

            _targetEntity.AddComponent(MyTestComponentsLookup.ComponentB, component);

            Assert.Throws <EntityAlreadyHasComponentException>(() => _originalEntity.CopyTo(_targetEntity));
        }
        public void DeactivatedPrimaryEntityIndexDoesNotAddEntitiesAnymore()
        {
            SetupDeactivatedPrimaryEntityIndex();

            var nameAgeComponent = new NameAgeComponent();

            nameAgeComponent.name = NAME;
            _context.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent);

            Assert.IsNull(_primaryIndex.GetEntity(NAME));
        }
        public void PrimaryEntityIndexThrowsWhenAddingEntityForTheSameKey()
        {
            SetupActivatedPrimaryEntityIndex();

            var nameAgeComponent = new NameAgeComponent();

            nameAgeComponent.name = NAME;
            _entity = _context.CreateEntity();

            Assert.Throws <EntityIndexException>(() => _entity.AddComponent(CID.ComponentA, nameAgeComponent));
        }
示例#5
0
        public void ComponentPoolGetsNonPoolCreatedInstanceWhenRemoved()
        {
            var component = new NameAgeComponent();

            _defaultEntity.AddComponent(1, component);

            _defaultEntity.RemoveComponent(1);

            var newComponent = (NameAgeComponent)_defaultEntity.CreateComponent(1, typeof(NameAgeComponent));

            Assert.AreEqual(component, newComponent);
        }
示例#6
0
 public void Setup()
 {
     _context        = new MyTestContext();
     _defaultEntity  = TestTools.CreateEntity();
     _originalEntity = _context.CreateEntity();
     _targetEntity   = _context.CreateEntity();
     _nameAge        = new NameAgeComponent
     {
         name = "Max", age = 42
     };
     _didDispatch = 0;
 }
        public void ReactivatedPrimaryEntityIndexAddsNewEntities()
        {
            SetupReactivatedPrimaryEntityIndex();

            var nameAgeComponent = new NameAgeComponent();

            nameAgeComponent.name = "Jack";
            _entity = _context.CreateEntity();
            _entity.AddComponent(CID.ComponentA, nameAgeComponent);

            Assert.AreEqual(_entity, _primaryIndex.GetEntity("Jack"));
        }
示例#8
0
        public void ReplacesExistingComponentWhenOverwriteIsSet()
        {
            _originalEntity.AddComponentA();
            _originalEntity.AddComponent(MyTestComponentsLookup.NameAge, _nameAge);
            var component = new NameAgeComponent();

            _targetEntity.AddComponent(MyTestComponentsLookup.NameAge, component);
            _originalEntity.CopyTo(_targetEntity, true);

            var copy = _targetEntity.GetComponent(MyTestComponentsLookup.NameAge);

            Assert.AreNotEqual(_nameAge, copy);
            Assert.AreNotEqual(component, copy);

            Assert.AreEqual(_nameAge.name, ((NameAgeComponent)copy).name);
            Assert.AreEqual(_nameAge.age, ((NameAgeComponent)copy).age);
        }
        public void SetupMultipleKeyPrimaryEntityIndex()
        {
            _context      = new MyTestContext();
            _group        = _context.GetGroup(Matcher <MyTestEntity> .AllOf(CID.ComponentA));
            _primaryIndex = new PrimaryEntityIndex <MyTestEntity, string>("TestIndex", _group, (e, c) =>
            {
                return(c is NameAgeComponent nameAge
                                        ? new[] { nameAge.name + "1", nameAge.name + "2" }
                                        : new[] { ((NameAgeComponent)e.GetComponent(CID.ComponentA)).name + "1", ((NameAgeComponent)e.GetComponent(CID.ComponentA)).name + "2" });
            });

            var nameAgeComponent = new NameAgeComponent();

            nameAgeComponent.name = NAME;
            _entity = _context.CreateEntity();
            _entity.AddComponent(CID.ComponentA, nameAgeComponent);
        }
        public void SetupActivatedSingleKeyEntityIndex()
        {
            _context = new MyTestContext();
            _group   = _context.GetGroup(Matcher <MyTestEntity> .AllOf(CID.ComponentA));
            _index   = new EntityIndex <MyTestEntity, string>("TestIndex", _group, (e, c) =>
            {
                return(c is NameAgeComponent nameAge
                                        ? nameAge.name
                                        : ((NameAgeComponent)e.GetComponent(CID.ComponentA)).name);
            });

            _nameAgeComponent      = new NameAgeComponent();
            _nameAgeComponent.name = NAME;
            _entity1 = _context.CreateEntity();
            _entity1.AddComponent(CID.ComponentA, _nameAgeComponent);
            _entity2 = _context.CreateEntity();
            _entity2.AddComponent(CID.ComponentA, _nameAgeComponent);
        }
        public void SetupActivatedPrimaryEntityIndex()
        {
            _context      = new MyTestContext();
            _group        = _context.GetGroup(Matcher <MyTestEntity> .AllOf(MyTestComponentsLookup.ComponentA));
            _primaryIndex = new PrimaryEntityIndex <MyTestEntity, string>(
                "TestIndex",
                _group,
                (e, c) =>
            {
                return(c is NameAgeComponent nameAge
                                                ? nameAge.name
                                                : ((NameAgeComponent)e.GetComponent(MyTestComponentsLookup.ComponentA)).name);
            });

            var nameAgeComponent = new NameAgeComponent();

            nameAgeComponent.name = NAME;

            _entity = _context.CreateEntity();
            _entity.AddComponent(MyTestComponentsLookup.ComponentA, nameAgeComponent);
        }
        public void IndexOfMultipleComponentsWorksWithNoneOf()
        {
            _context = new MyTestContext();

            var receivedComponents = new List <IComponent>();

            var nameAgeComponent1 = new NameAgeComponent();

            nameAgeComponent1.name = "Max";

            var nameAgeComponent2 = new NameAgeComponent();

            nameAgeComponent2.name = "Jack";

            _group = _context.GetGroup(Matcher <MyTestEntity> .AllOf(CID.ComponentA).NoneOf(CID.ComponentB));
            _index = new EntityIndex <MyTestEntity, string>("TestIndex", _group, (e, c) =>
            {
                receivedComponents.Add(c);

                if (c == nameAgeComponent1)
                {
                    return(((NameAgeComponent)c).name);
                }

                return(((NameAgeComponent)e.GetComponent(CID.ComponentA)).name);
            });

            var entity = _context.CreateEntity();

            entity.AddComponent(CID.ComponentA, nameAgeComponent1);
            entity.AddComponent(CID.ComponentB, nameAgeComponent2);

            Assert.AreEqual(2, receivedComponents.Count);
            Assert.AreEqual(nameAgeComponent1, receivedComponents[0]);
            Assert.AreEqual(nameAgeComponent2, receivedComponents[1]);
        }