Пример #1
0
        //---------------------------------------------------------------------------

        public void CreateCollisionFromMap(IEntity entity, Map map)
        {
            MultiPathColliderComponent path = entity.AddComponent <MultiPathColliderComponent>();

            if (path != null)
            {
                for (int x = 0; x < map.Width - 1; x++)
                {
                    Vector2 start  = new Vector2((x + 1) * 32, 0);
                    int     length = 0;
                    for (int y = 0; y < map.Height; y++)
                    {
                        if (map[x, y].IsBlocked != map[x + 1, y].IsBlocked)
                        {
                            length++;
                        }
                        else
                        {
                            if (length > 0)
                            {
                                path.AddPath(start, new Vector2((x + 1) * 32, y * 32));
                                length = 0;
                            }
                            start = new Vector2((x + 1) * 32, (y + 1) * 32);
                        }
                    }
                }

                for (int y = 0; y < map.Height - 1; y++)
                {
                    Vector2 start  = new Vector2(0, (y + 1) * 32);
                    int     length = 0;
                    for (int x = 0; x < map.Width; x++)
                    {
                        if (map[x, y].IsBlocked != map[x, y + 1].IsBlocked)
                        {
                            length++;
                        }
                        else
                        {
                            if (length > 0)
                            {
                                path.AddPath(start, new Vector2(x * 32, (y + 1) * 32));
                                length = 0;
                            }
                            start = new Vector2((x + 1) * 32, (y + 1) * 32);
                        }
                    }
                }
            }
        }
Пример #2
0
        public IEntity CreateCollision()
        {
            IEntity entity = EntityManager.Get().Find(m_CollisionEntity);

            if (entity == null)
            {
                entity = EntityFactory.Create <Entity>(string.Format("Chunk[{0}|{0}]", X, Y));
                entity.AddComponent <TransformComponent>();
                entity.AddComponent <PhysicsComponent>();
                entity.AddComponent <MultiPathColliderComponent>().SetCollisionCategory(ECollisionCategory.Stage);
            }
            MultiPathColliderComponent path = entity.GetComponent <MultiPathColliderComponent>();

            if (path != null)
            {
                path.Reset();
                path.SetCollisionCategory(ECollisionCategory.Stage);

                for (int x = -1; x < Width - 1; x++)
                {
                    Vector2 start  = new Vector2((GlobalX(x) + 1) * 64, GlobalY(0) * 64);
                    int     length = 0;
                    for (int y = 0; y < Height; y++)
                    {
                        ChunkCell cell = null;
                        if (x < 0 && LeftChunk != null)
                        {
                            cell = LeftChunk[Width + x, y];
                        }
                        else if (x >= 0)
                        {
                            cell = m_Cells[x, y];
                        }

                        if (cell != null)
                        {
                            if (cell.IsBlocked != m_Cells[x + 1, y].IsBlocked)
                            {
                                length++;
                            }
                            else
                            {
                                if (length > 0)
                                {
                                    Vector2 end = new Vector2((GlobalX(x) + 1) * 64, GlobalY(y) * 64);
                                    path.AddPath(start, end);
                                    AddCorners(start, end);
                                    length = 0;
                                }
                                start = new Vector2((GlobalX(x) + 1) * 64, (GlobalY(y) + 1) * 64);
                            }
                        }
                    }
                    if (length > 0)
                    {
                        Vector2 end = new Vector2((GlobalX(x) + 1) * 64, GlobalY(Height) * 64);
                        path.AddPath(start, end);
                        AddCorners(start, end);
                    }
                }

                for (int y = -1; y < Height - 1; y++)
                {
                    Vector2 start  = new Vector2(GlobalX(0) * 64, (GlobalY(y) + 1) * 64);
                    int     length = 0;
                    for (int x = 0; x < Width; x++)
                    {
                        ChunkCell cell = null;
                        if (y < 0 && TopChunk != null)
                        {
                            cell = TopChunk[x, Height + y];
                        }
                        else if (y >= 0)
                        {
                            cell = m_Cells[x, y];
                        }

                        if (cell != null)
                        {
                            if (cell.IsBlocked != m_Cells[x, y + 1].IsBlocked)
                            {
                                length++;
                            }
                            else
                            {
                                if (length > 0)
                                {
                                    Vector2 end = new Vector2(GlobalX(x) * 64, (GlobalY(y) + 1) * 64);
                                    path.AddPath(start, end);
                                    AddCorners(start, end);
                                    length = 0;
                                }
                                start = new Vector2((GlobalX(x) + 1) * 64, (GlobalY(y) + 1) * 64);
                            }
                        }
                    }
                    if (length > 0)
                    {
                        Vector2 end = new Vector2(GlobalX(Width) * 64, (GlobalY(y) + 1) * 64);
                        path.AddPath(start, end);
                        AddCorners(start, end);
                    }
                }
            }
            return(entity);
        }