Exemple #1
0
        public void AllFilterTest()
        {
            List <IEcsEntity> entities = new List <IEcsEntity>
            {
                _entityABD, _entityBD0, _entityBD1, _entityBC, _entityAB
            };

            IEcsGroup group = _world.Filter(new EcsFilter().AllOf <ComponentB>());

            Assert.AreEqual(entities.Count, group.CalculateCount());
            group.ForEach((IEcsEntity entity, ComponentB compB) =>
            {
                Assert.IsTrue(entities.Contains(entity));
            });



            entities = new List <IEcsEntity>
            {
                _entityABD, _entityBD0, _entityBD1
            };

            group = _world.Filter(new EcsFilter().AllOf <ComponentB, ComponentD>());
            Assert.AreEqual(3, group.CalculateCount());
            group.ForEach((IEcsEntity entity, ComponentB compB, ComponentD compD) =>
            {
                Assert.IsTrue(entities.Contains(entity));
            });
        }
Exemple #2
0
        public void GroupIncVersionFilterTest()
        {
            EcsWorld   world  = new EcsWorld();
            IEcsEntity entity = world.CreateEntity(new ComponentA(), new ComponentB());

            Assert.AreEqual(1, world.Filter(new EcsFilter().AllOf <ComponentB>()).CalculateCount());

            entity.AddComponent(new ComponentC());
            world.CreateEntity(new ComponentC(), new ComponentD());

            Assert.AreEqual(1, world.Filter(new EcsFilter().AllOf <ComponentB>()).CalculateCount());
        }
    public void Update(float deltaTime, EcsWorld world)
    {
        world.Filter(_heroFilter).ForEach((IEcsEntity entity, TransformComponent transform, RigBodyComponent rigBody) =>
        {
            if (Input.GetKey(KeyCode.A))
            {
                transform.Rotation += 2 * deltaTime;
            }

            if (Input.GetKey(KeyCode.D))
            {
                transform.Rotation -= 2 * deltaTime;
            }

            rigBody.Velocity = float2.zero;

            if (!Input.GetKey(KeyCode.W))
            {
                return;
            }

            float rad        = transform.Rotation;
            float2 dir       = new float2(-math.sin(rad), math.cos(rad));
            rigBody.Velocity = 25 * dir;
        });
    }
 public void Update(float deltaTime, EcsWorld world)
 {
     world.Filter(_filter).ForEach((IEcsEntity entity, TransformComponent transform, RigBodyComponent rigBody) =>
     {
         transform.Position += rigBody.Velocity * deltaTime;
     });
 }
    private static void CreateOrDestroyEntities(EcsWorld world, EcsFilter filter, int count, Action <EcsWorld> createEntity)
    {
        IEcsGroup group = world.Filter(filter);

        if (group.CalculateCount() == count)
        {
            return;
        }

        IEcsEntity[] entities = group.ToEntityArray();
        for (int i = entities.Length; i < count; i++)
        {
            createEntity(world);
        }

        for (int i = count; i < entities.Length; i++)
        {
            IEcsEntity             entity    = entities[i];
            BroadphaseRefComponent brRef     = entity.GetComponent <BroadphaseRefComponent>();
            CharacterComponent     character = entity.GetComponent <CharacterComponent>();

            Object.Destroy(character.Ref.gameObject);

            foreach (SAPChunk chunk in brRef.Chunks)
            {
                BroadphaseHelper.RemoveFormChunk(chunk, entity.Id);
            }

            entity.Destroy();
        }
    }
        public unsafe void Update(float deltaTime, EcsWorld world)
        {
            BroadphaseSAPComponent bpChunks = world.GetOrCreateSingleton <BroadphaseSAPComponent>();

            IEcsEntity[] entities = world.Filter(_entitiesFilter).ToEntityArray();

            for (int i = 0; i < entities.Length; i++)
            {
                IEcsEntity entity   = entities[i];
                uint       entityId = entity.Id;

                TransformComponent tr  = entity.GetComponent <TransformComponent>();
                ColliderComponent  col = entity.GetComponent <ColliderComponent>();
                RigBodyComponent   rig = entity.GetComponent <RigBodyComponent>();

                AABB aabb     = new AABB(col.Size, tr.Position, col.ColliderType == ColliderType.Rect ? tr.Rotation : 0f);
                bool isStatic = MathHelper.Equal(rig.InvMass, 0);
                int  layer    = col.Layer;

                List <SAPChunk> chunks = new List <SAPChunk>(4);
                foreach (int chunkId in BroadphaseHelper.GetChunks(aabb))
                {
                    chunks.Add(BroadphaseHelper.GetOrCreateChunk(chunkId, bpChunks));
                }

                BroadphaseRefComponent bpRef = new BroadphaseRefComponent
                {
                    Chunks     = chunks,
                    ChunksHash = BroadphaseHelper.CalculateChunksHash(aabb),
                    AABB       = aabb
                };
                entity.AddComponent(bpRef);

                foreach (SAPChunk chunk in chunks)
                {
                    if (chunk.Length >= chunk.Items.Length)
                    {
                        Array.Resize(ref chunk.Items, 2 * chunk.Length);

                        fixed(AABB *pAABB = &bpRef.AABB)
                        {
                            chunk.Items[chunk.Length++] = new BroadphaseAABB
                            {
                                AABB     = pAABB,
                                Id       = entityId,
                                IsStatic = isStatic,
                                Layer    = layer,
                                Entity   = entity
                            };
                        }

                        if (!isStatic)
                            chunk.DynamicCounter++; }
                }
            }
        }
Exemple #7
0
    public void Update(float deltaTime, EcsWorld world)
    {
        world.Filter(_transformsFilter).ForEach((IEcsEntity entity, TransformComponent transform, CharacterComponent character) =>
        {
            character.Ref.Transform.position = new Vector3(transform.Position.x, 0, transform.Position.y);
            character.Ref.Transform.rotation = Quaternion.Euler(0, -Mathf.Rad2Deg * transform.Rotation, 0);
        });

        world.Filter(_rayFilter).ForEach((IEcsEntity entity, RayComponent ray, CharacterComponent character) =>
        {
            float2 target = ray.Hit ? ray.HitPoint : ray.Target;

            float distance = math.distance(ray.Source, target);
            character.Ref.Ray.localScale    = new Vector3(0.4f, 5, distance);
            character.Ref.Ray.localPosition = 0.5f * distance * Vector3.forward;
        });

        world.Filter(_heroFilter).ForEach((IEcsEntity entity, CharacterComponent character) =>
        {
            Transform camera = Camera.main.transform;
            camera.position  = Vector3.Lerp(camera.position, character.Ref.transform.position + 10 * Vector3.up,
                                            10 * Time.deltaTime);
        });
    }
    public void Update(float deltaTime, EcsWorld world)
    {
        CreateOrDestroyEntities(world, _staticRectFilter, _physicsScene.StaticRectCount, CreateStaticRect);
        CreateOrDestroyEntities(world, _staticCircleFilter, _physicsScene.StaticCircleCount, CreateStaticCircle);
        CreateOrDestroyEntities(world, _dynamicBlueCircleFilter, _physicsScene.DynamicBlueCircleCount, CreateDynamicBlueCircle);
        CreateOrDestroyEntities(world, _dynamicYellowCircleFilter, _physicsScene.DynamicYellowCircleCount, CreateDynamicYellowCircle);

        if (world.Filter(_heroFilter).CalculateCount() > 0)
        {
            return;
        }

        IEcsEntity heroEntity = CreateCircleEntity(world, Vector2.zero, 0, 5, 1, "Default", 150);

        heroEntity.AddComponent(new HeroComponent());
        Instantiate(_physicsScene.Hero, heroEntity);
    }
Exemple #9
0
        public unsafe void Update(float deltaTime, EcsWorld world)
        {
            BroadphaseSAPComponent bpChunks = world.GetOrCreateSingleton <BroadphaseSAPComponent>();

            world.Filter(_entitiesFilter).ForEach(
                (IEcsEntity entity, TransformComponent tr, ColliderComponent col, BroadphaseRefComponent bpRef) =>
            {
                AABB aabb = new AABB(col.Size, tr.Position,
                                     col.ColliderType == ColliderType.Rect ? tr.Rotation : 0f);

                fixed(AABB * pAABB = &bpRef.AABB)
                {
                    pAABB->Min = aabb.Min;
                    pAABB->Max = aabb.Max;
                }

                int chunksHash = BroadphaseHelper.CalculateChunksHash(aabb);
                if (bpRef.ChunksHash == chunksHash)
                {
                    return;
                }

                RigBodyComponent rig = entity.GetComponent <RigBodyComponent>();

                uint entityId = entity.Id;
                bool isStatic = MathHelper.Equal(rig.InvMass, 0);
                int layer     = col.Layer;

                List <SAPChunk> chunks    = bpRef.Chunks;
                List <SAPChunk> newChunks = new List <SAPChunk>(4);
                foreach (int chunkId in BroadphaseHelper.GetChunks(aabb))
                {
                    int index = -1;
                    for (int i = 0; i < chunks.Count; i++)
                    {
                        SAPChunk chunk = chunks[i];
                        if (chunk == null || chunk.Id != chunkId)
                        {
                            continue;
                        }

                        index = i;
                        break;
                    }

                    if (index >= 0)
                    {
                        SAPChunk chunk = chunks[index];
                        chunks[index]  = null;
                        newChunks.Add(chunk);
                    }
                    else
                    {
                        SAPChunk chunk = BroadphaseHelper.GetOrCreateChunk(chunkId, bpChunks);

                        if (chunk.Length >= chunk.Items.Length)
                        {
                            Array.Resize(ref chunk.Items, 2 * chunk.Length);
                        }

                        fixed(AABB * pAABB = &bpRef.AABB)
                        {
                            chunk.Items[chunk.Length++] = new BroadphaseAABB
                            {
                                AABB     = pAABB,
                                Id       = entityId,
                                IsStatic = isStatic,
                                Layer    = layer,
                                Entity   = entity
                            };
                        }

                        if (!isStatic)
                        {
                            chunk.DynamicCounter++;
                        }

                        newChunks.Add(chunk);
                    }
                }

                foreach (SAPChunk chunk in chunks)
                {
                    if (chunk == null)
                    {
                        continue;
                    }
                    BroadphaseHelper.RemoveFormChunk(chunk, entityId);
                }

                bpRef.Chunks     = newChunks;
                bpRef.ChunksHash = chunksHash;
            });
        }
Exemple #10
0
        public unsafe void Update(float deltaTime, EcsWorld world)
        {
            BroadphaseSAPComponent bpChunks = world.GetOrCreateSingleton <BroadphaseSAPComponent>();

            world.Filter(_rayFilter).ForEach((IEcsEntity entity, TransformComponent tr, RayComponent ray) =>
            {
                ray.Hit      = false;
                ray.Source   = tr.Position;
                ray.Rotation = tr.Rotation;

                float minDist = float.MaxValue;
                RayTrace(ray, ref _chunksBuffer, ref _pointsBuffer, out int length);

                for (int i = 0; i < length - 1; i++)
                {
                    SAPChunk chunk = BroadphaseHelper.GetOrCreateChunk(_chunksBuffer[i], bpChunks);
                    float2 p1      = _pointsBuffer[i];
                    float2 p2      = _pointsBuffer[i + 1];

                    AABB sAABB = new AABB
                    {
                        Min = new float2(math.min(p1.x, p2.x), math.min(p1.y, p2.y)),
                        Max = new float2(math.max(p1.x, p2.x), math.max(p1.y, p2.y))
                    };

                    for (int j = 0; j < chunk.Length; j++)
                    {
                        BroadphaseAABB item = chunk.Items[j];
                        if (!item.AABB->Overlap(sAABB))
                        {
                            continue;
                        }

                        if (!_collisionMatrix.Check(ray.Layer, item.Layer))
                        {
                            continue;
                        }

                        IEcsEntity targetEntity = item.Entity;
                        if (entity == targetEntity)
                        {
                            continue;
                        }

                        tr = targetEntity.GetComponent <TransformComponent>();
                        ColliderComponent col = targetEntity.GetComponent <ColliderComponent>();

                        float2 hitPoint = float2.zero;
                        if (col.ColliderType == ColliderType.Circle && !OnCircleIntersection(ray, col, tr, out hitPoint) ||
                            col.ColliderType == ColliderType.Rect && !OnRectIntersection(ray, col, tr, out hitPoint))
                        {
                            continue;
                        }

                        float dist = math.distancesq(p1, hitPoint);
                        if (!(dist < minDist))
                        {
                            continue;
                        }

                        minDist      = dist;
                        ray.HitPoint = hitPoint;
                    }

                    if (!(minDist < float.MaxValue))
                    {
                        continue;
                    }

                    ray.Hit = true;
                    break;
                }
            });
        }