Beispiel #1
0
        public void Add_AddComponentToExistingEntity()
        {
            var entity = new EcsEntity();

            var component1 = new  TestComponent1();
            var component2 = new  TestComponent2();

            entity.AddComponent(component1);

            _ecsStoreService.AddEntity(entity);

            Assert.AreEqual(1, _ecsStore.EntitiesByComponents.Count);

            entity.AddComponent(component2);

            Assert.AreEqual(2, _ecsStore.EntitiesByComponents.Count);
            Assert.AreEqual(1, _ecsStore.EntitiesByComponents[typeof(TestComponent1)].Count);
            Assert.AreEqual(1, _ecsStore.EntitiesByComponents[typeof(TestComponent2)].Count);

            var entityByComponent1 = _ecsStore.EntitiesByComponents[typeof(TestComponent1)]?.FirstOrDefault();
            var entityByComponent2 = _ecsStore.EntitiesByComponents[typeof(TestComponent2)]?.FirstOrDefault();

            Assert.AreEqual(entity, entityByComponent1);
            Assert.AreEqual(entity, entityByComponent2);
        }
Beispiel #2
0
        private EcsEntity CreatePlayer()
        {
            var windowSize = new Vector3(this.GraphicsDevice.Viewport.Bounds.Size.ToVector2(), 0.0f);

            var player = new EcsEntity(_world);

            player.Name = "Player";

            var transform = player.AddComponent <EcsTransformComponent>();

            transform.Position = windowSize / 2.0f;
            transform.Scale    = Vector3.One / 2.0f;

            var sprite = player.AddComponent <EcsSpriteComponent>();

            sprite.Texture = this.Content.Load <Texture2D>("Box");

            var speed = player.AddComponent <EcsSpeedComponent>();

            speed.Speed = 200.0f;

            var shoot = player.AddComponent <ShootComponent>();

            shoot.ShootInterval = 0.2f;

            player.AddComponent <EcsGamePadInputComponent>();

            return(player);
        }
Beispiel #3
0
        public void Add_ManyComponents()
        {
            var entity = new EcsEntity();

            var component1 = new  TestComponent1();
            var component2 = new  TestComponent2();

            entity
            .AddComponent(component1)
            .AddComponent(component2);

            _ecsStoreService.AddEntity(entity);

            var actualEntity = _ecsStore.Entities.FirstOrDefault();

            Assert.AreEqual(_ecsStore.Entities.Count, 1);
            Assert.AreEqual(entity, actualEntity.Value);

            var entityByComponent1 = _ecsStore.EntitiesByComponents[typeof(TestComponent1)]?.FirstOrDefault();
            var entityByComponent2 = _ecsStore.EntitiesByComponents[typeof(TestComponent2)]?.FirstOrDefault();

            Assert.AreEqual(entity, entityByComponent1);
            Assert.AreEqual(entity, entityByComponent2);

            Assert.AreEqual(2, _ecsStore.EntitiesByComponents.Count);
            Assert.AreEqual(1, _ecsStore.EntitiesByComponents[typeof(TestComponent1)].Count);
            Assert.AreEqual(1, _ecsStore.EntitiesByComponents[typeof(TestComponent2)].Count);
        }
Beispiel #4
0
        private EcsEntity CreateBullet()
        {
            var bullet = new EcsEntity(_world);

            bullet.Name = "Bullet";

            var transform = bullet.AddComponent <EcsTransformComponent>();

            transform.Scale = Vector3.One / 10.0f;

            var sprite = bullet.AddComponent <EcsSpriteComponent>();

            sprite.Texture = this.Content.Load <Texture2D>("Box");

            var speed = bullet.AddComponent <EcsSpeedComponent>();

            speed.Speed = 800.0f;

            bullet.AddComponent <MoveDirectionComponent>();

            return(bullet);
        }
Beispiel #5
0
        public void Get_FewComponents_ByOneInterface_WithDifferentTimeCreatingOfComponent()
        {
            var entity1 = new EcsEntity()
                          .AddComponent(new TestComponent2());

            var entity2 = new EcsEntity()
                          .AddComponent(new TestComponent1());

            _ecsStoreService.AddEntity(entity1).AddEntity(entity2);

            entity2.AddComponent(new TestComponent2());

            var actualEntities = _ecsStoreService.GetEntities(typeof(TestComponent2));

            Assert.AreEqual(2, actualEntities.Count);
            Assert.AreEqual(entity1, actualEntities[0]);
            Assert.AreEqual(entity2, actualEntities[1]);
        }