예제 #1
0
        // system execution
        void IECSSystem.Execute(ECSEngine engine, ECSEntity[] entities)
        {
            for (int i = 0; i < entities.Length; i++)
            {
                //get entity
                ECSEntity entity = entities[i];

                // get data
                PositionComponent pc = engine.GetComponent <PositionComponent>(entity);
                VelocityComponent vc = engine.GetComponent <VelocityComponent>(entity);

                // manipulate data
                pc.Position += vc.Velocity * Time.DeltaTime;

                // set data
                engine.SetComponent(entity, pc);

                Debug.Log("Entity: " + entity + " - Pos: " + pc.Position, ConsoleColor.Green, entity);

                // other operations
                if (pc.Position.y < -10)
                {
                    Console.Clear();
                    Debug.Log("Destroyed: " + entity, ConsoleColor.Red, entity);

                    // destroy entities
                    engine.DestroyEntity(entity);
                }
            }
        }
예제 #2
0
    void IECSSystem.Execute(ECSEngine engine, ECSEntity[] entities)
    {
        for (int i = 0; i < entities.Length; i++)
        {
            ECSEntity entity = entities[i];

            PositionComponent pc = engine.GetComponent <PositionComponent>(entity);
            VelocityComponent vc = engine.GetComponent <VelocityComponent>(entity);

            if (Input.IsKeyPressed(KeyCode.W))
            {
                pc.Position.Y -= vc.Velocity.Y * Time.DeltaTime;
            }

            if (Input.IsKeyPressed(KeyCode.S))
            {
                pc.Position.Y += vc.Velocity.Y * Time.DeltaTime;
            }

            if (Input.IsKeyPressed(KeyCode.A))
            {
                pc.Position.X -= vc.Velocity.X * Time.DeltaTime;
            }

            if (Input.IsKeyPressed(KeyCode.D))
            {
                pc.Position.X += vc.Velocity.X * Time.DeltaTime;
            }

            engine.SetComponent <PositionComponent>(entity, pc);
        }
    }
예제 #3
0
    void IECSSystem.Execute(ECSEngine engine, ECSEntity[] entities)
    {
        for (int i = 0; i < entities.Length; i++)
        {
            ECSEntity entity = entities[i];

            TimeComponent tc = engine.GetComponent <TimeComponent>(entity);

            tc.CurrentTime -= Time.DeltaTime;

            if (tc.CurrentTime <= 0f)
            {
                tc.CurrentTime += tc.Time;

                SpawnPointComponent pc = engine.GetComponent <SpawnPointComponent>(entity);

                var enemy = engine.CreateEntity();

                engine.AddComponent <NameComponent>(enemy,
                                                    new NameComponent()
                {
                    Name = "ENEMY"
                });

                engine.AddComponent <PositionComponent>(enemy,
                                                        new PositionComponent()
                {
                    Position = pc.SpawnPoint
                });

                engine.AddComponent <VelocityComponent>(enemy,
                                                        new VelocityComponent()
                {
                    Velocity = new Vector2(0f, 5f)
                });

                engine.AddComponent(enemy, new BoxColliderComponent()
                {
                    Size = new Vector2(1f, 1f)
                });

                engine.AddComponent <SpriteRendererComponent>(enemy,
                                                              new SpriteRendererComponent()
                {
                    RenderOffset = RenderOffset.Player,
                    Texture      = TextureManager.GetTexture("EnemyBlack2"),
                    Sprite       = new Sprite(1f, 1f)
                    {
                        pivot = Vector2.One * 0.5f
                    }
                });

                engine.AddComponent <AIComponent>(enemy, new AIComponent());
            }

            engine.SetComponent <TimeComponent>(entity, tc);
        }
    }
예제 #4
0
    void IECSSystem.Execute(ECSEngine engine, ECSEntity[] entities)
    {
        for (int i = 0; i < entities.Length; i++)
        {
            ECSEntity entity = entities[i];

            if (Input.IsKeyDown(KeyCode.Space))
            {
                WeaponComponent   wc = engine.GetComponent <WeaponComponent>(entity);
                PositionComponent pc = engine.GetComponent <PositionComponent>(entity);

                ECSEntity bullet = engine.CreateEntity();

                engine.AddComponent <NameComponent>(bullet,
                                                    new NameComponent()
                {
                    Name = "BULLET"
                });

                engine.AddComponent <PositionComponent>(bullet,
                                                        new PositionComponent()
                {
                    Position = pc.Position + wc.BulletSpawnOffset
                });

                engine.AddComponent <VelocityComponent>(bullet,
                                                        new VelocityComponent()
                {
                    Velocity = new Vector2(0f, -10f)
                });

                engine.AddComponent(bullet, new BoxColliderComponent()
                {
                    Size = new Vector2(1f, 1f)
                });

                engine.AddComponent <SpriteRendererComponent>(bullet,
                                                              new SpriteRendererComponent()
                {
                    RenderOffset = RenderOffset.Player,
                    Texture      = TextureManager.GetTexture("LaserRed01"),
                    Sprite       = new Sprite(0.1f, 1f)
                    {
                        pivot = new Vector2(0.1f, 0.5f)
                    }
                });

                engine.AddComponent <AIComponent>(bullet, new AIComponent());
            }
        }
    }
예제 #5
0
    void IECSSystem.Execute(ECSEngine engine, ECSEntity[] entities)
    {
        for (int i = 0; i < entities.Length; i++)
        {
            ECSEntity entity = entities[i];

            PositionComponent pc = engine.GetComponent <PositionComponent>(entity);
            VelocityComponent vc = engine.GetComponent <VelocityComponent>(entity);

            pc.Position += vc.Velocity * Time.DeltaTime;

            engine.SetComponent <PositionComponent>(entity, pc);
        }
    }
예제 #6
0
    void IECSSystem.Execute(ECSEngine engine, ECSEntity[] entities)
    {
        for (int i = 0; i < entities.Length; i++)
        {
            ECSEntity entity = entities[i];

            PositionComponent       pc  = engine.GetComponent <PositionComponent>(entity);
            SpriteRendererComponent src = engine.GetComponent <SpriteRendererComponent>(entity);

            src.Sprite.position = pc.Position;
            engine.SetComponent <SpriteRendererComponent>(entity, src);

            src.Sprite.DrawTexture(src.Texture);
        }
    }
예제 #7
0
    void IECSSystem.Execute(ECSEngine engine, ECSEntity[] entities)
    {
        for (int i = 0; i < entities.Length; i++)
        {
            ECSEntity entity = entities[i];

            CollisionDataComponent cdc = engine.GetComponent <CollisionDataComponent>(entity);

            ECSEntity entity1 = cdc.Entity1;
            ECSEntity entity2 = cdc.Entity2;

            engine.DestroyEntity(entity);

            if (!this.entitiesToDestroy.Contains(entity1))
            {
                this.entitiesToDestroy.Add(entity1);
            }

            if (!this.entitiesToDestroy.Contains(entity2))
            {
                this.entitiesToDestroy.Add(entity2);
            }
        }

        this.entitiesToDestroy.Sort((x1, x2) => - x1.CompareTo(x2));

        for (int i = 0; i < this.entitiesToDestroy.Count; i++)
        {
            ECSEntity entity = this.entitiesToDestroy[i];
            engine.DestroyEntity(entity);
        }

        this.entitiesToDestroy.Clear();
    }
예제 #8
0
    void IECSSystem.Execute(ECSEngine engine, ECSEntity[] entities)
    {
        for (int i = 0; i < entities.Length; i++)
        {
            ECSEntity entity = entities[i];

            PositionComponent pc = engine.GetComponent <PositionComponent>(entity);

            if (this.IsOutOfScreen(pc.Position))
            {
                engine.DestroyEntity(entity);
            }
        }
    }
예제 #9
0
    void IECSSystem.Execute(ECSEngine engine, ECSEntity[] entities)
    {
        for (int i = 0; i < entities.Length - 1; i++)
        {
            ECSEntity entity1 = entities[i];

            for (int j = i + 1; j < entities.Length; j++)
            {
                ECSEntity entity2 = entities[j];

                if (entity1 != entity2)
                {
                    PositionComponent    pc1  = engine.GetComponent <PositionComponent>(entity1);
                    BoxColliderComponent box1 = engine.GetComponent <BoxColliderComponent>(entity1);

                    Vector2 box1ExtentMin = this.GetExtentMin(pc1, box1);
                    Vector2 box1ExtentMax = this.GetExtentMax(pc1, box1);

                    PositionComponent    pc2  = engine.GetComponent <PositionComponent>(entity2);
                    BoxColliderComponent box2 = engine.GetComponent <BoxColliderComponent>(entity2);

                    Vector2 box2ExtentMin = this.GetExtentMin(pc2, box2);
                    Vector2 box2ExtentMax = this.GetExtentMax(pc2, box2);

                    if (this.Intersect(box1ExtentMin, box1ExtentMax, box2ExtentMin, box2ExtentMax))
                    {
                        ECSEntity collisionDataEntity = engine.CreateEntity();

                        engine.AddComponent(collisionDataEntity, new CollisionDataComponent()
                        {
                            Entity1 = entity1, Entity2 = entity2
                        });
                    }
                }
            }
        }
    }