Ejemplo n.º 1
0
        public static GridColliderPart TriangleGridColliderPart(GameObject parent, Grid grid, TileInfo tile, int x, int y, int w, int h)
        {
            GameObject obj = new GameObject("triangle");

            obj.transform.SetParent(parent.transform);

            obj.tag   = tile.tag;
            obj.layer = tile.layer;

            GridColliderPart part = obj.AddComponent <GridColliderPart> ();

            part.triangle = obj.AddComponent <PolygonCollider2D> ();

            part.shape      = tile.shape;
            part.isVertical = tile.isVertical;

            part.id = tile.id;
            part.triangle.isTrigger = tile.isTrigger;

            part.bottomLeftX = x;
            part.bottomLeftY = y;

            part.transform.localPosition = grid.TileSpaceToTransform(x + w / 2f, y + h / 2f);

            part.SetSize(w, h);

            return(part);
        }
Ejemplo n.º 2
0
        void ClearTile(int x, int y)
        {
            GridColliderPart wrapper = components.Get(x, y) as GridColliderPart;

            _ClearTile(wrapper, x, y);

            components.Set(x, y, null);
        }
Ejemplo n.º 3
0
        public static GridColliderPart LineGridColliderPart(GameObject parent, Grid grid, TileInfo tile, int x, int y, int w, int h)
        {
            GameObject obj = new GameObject("line");

            obj.transform.SetParent(parent.transform);

            obj.tag   = tile.tag;
            obj.layer = tile.layer;

            GridColliderPart part = obj.AddComponent <GridColliderPart> ();

            part.line = obj.AddComponent <EdgeCollider2D> ();
            part.line.usedByEffector = true;
            switch (tile.shape)
            {
            case TileShape.DOWN_ONEWAY: part.transform.Rotate(Vector3.forward * 180); break;

            case TileShape.LEFT_ONEWAY: part.transform.Rotate(Vector3.forward * 90); break;

            case TileShape.RIGHT_ONEWAY: part.transform.Rotate(-Vector3.forward * 90); break;
            }

            PlatformEffector2D effector = part.gameObject.AddComponent <PlatformEffector2D> ();

            effector.surfaceArc = 5;

            part.shape      = tile.shape;
            part.isVertical = tile.isVertical;

            part.id             = tile.id;
            part.line.isTrigger = tile.isTrigger;

            part.bottomLeftX = x;
            part.bottomLeftY = y;

            part.SetSize(w, h);

            return(part);
        }
Ejemplo n.º 4
0
 public bool Compatible(GridColliderPart other)
 {
     return(shape == other.shape &&
            (bottomLeftX != other.bottomLeftX && height == 1 && other.height == 1 ||
             bottomLeftY != other.bottomLeftY && width == 1 && other.width == 1));
 }
Ejemplo n.º 5
0
        public override void OnSet(int x, int y, Tile tile)
        {
            // clear previous
            ClearTile(x, y);

            TileInfo info = grid.atlas [tile.id];

            // add new
            if (info.shape != TileShape.EMPTY)
            {
                GridColliderPart wrapper = null;

                // HORIZONTAL GROWTH
                if (info.shape != TileShape.LEFT_ONEWAY && info.shape != TileShape.RIGHT_ONEWAY)
                {
                    bool expanded = false;

                    GridColliderPart left = components.Get(x - 1, y) as GridColliderPart;
                    if (left != null && left.Compatible(info) && !left.isVertical && !info.isVertical)
                    {
                        left.SetSize(left.width + 1, 1);
                        wrapper = left;

                        components.Set(x, y, left);

                        expanded = true;
                    }

                    GridColliderPart right = components.Get(x + 1, y) as GridColliderPart;
                    if (right != null && right.Compatible(info) && !right.isVertical && !info.isVertical)
                    {
                        if (!expanded)
                        {
                            right.bottomLeftX -= 1;
                            right.SetSize(right.width + 1, 1);
                            wrapper = right;

                            components.Set(x, y, right);

                            wrapper.ResetSizeAndPosition(grid);
                            return;
                        }
                        else
                        {
                            left.SetSize(left.width + right.width, 1);

                            for (int i = right.bottomLeftX; i < right.bottomLeftX + right.width; i++)
                            {
                                components.Set(i, y, left);
                            }

                            if (Application.isPlaying)
                            {
                                Destroy(right.gameObject);
                            }
                            else
                            {
                                DestroyImmediate(right.gameObject);
                            }

                            wrapper.ResetSizeAndPosition(grid);
                            return;
                        }
                    }

                    if (expanded)
                    {
                        wrapper.ResetSizeAndPosition(grid);
                        return;
                    }
                }

                // VERTICAL GROWTH
                if (info.shape != TileShape.FULL && info.shape != TileShape.UP_ONEWAY && info.shape != TileShape.DOWN_ONEWAY)
                {
                    bool expanded = false;

                    GridColliderPart down = components.Get(x, y - 1) as GridColliderPart;
                    if (down != null && down.Compatible(info) && down.isVertical && info.isVertical)
                    {
                        down.SetSize(1, down.height + 1);
                        wrapper = down;

                        components.Set(x, y, down);

                        expanded = true;
                    }

                    GridColliderPart up = components.Get(x, y + 1) as GridColliderPart;

                    if (up != null && up.Compatible(info) && up.isVertical && info.isVertical)
                    {
                        if (!expanded)
                        {
                            up.bottomLeftY -= 1;
                            up.SetSize(1, up.height + 1);
                            wrapper = up;

                            components.Set(x, y, up);

                            wrapper.ResetSizeAndPosition(grid);
                            return;
                        }
                        else
                        {
                            down.SetSize(1, down.height + up.height);

                            for (int i = up.bottomLeftY; i < up.bottomLeftY + up.width; i++)
                            {
                                components.Set(i, y, down);
                            }

                            if (Application.isPlaying)
                            {
                                Destroy(up.gameObject);
                            }
                            else
                            {
                                DestroyImmediate(up.gameObject);
                            }

                            wrapper.ResetSizeAndPosition(grid);
                            return;
                        }
                    }

                    if (expanded)
                    {
                        wrapper.ResetSizeAndPosition(grid);
                        return;
                    }
                }

                // NO EXPANSE, CREATE NEW
                wrapper = GridColliderPart.CreateColliderPart(containerGO, grid, info, x, y, 1, 1);
                components.Set(x, y, wrapper);

                wrapper.ResetSizeAndPosition(grid);
            }
        }
Ejemplo n.º 6
0
        void _ClearTile(GridColliderPart wrapper, int x, int y)
        {
            if (wrapper != null)
            {
                if (wrapper.width == 1)
                {
                    if (wrapper.height == 1)
                    {
                        if (Application.isPlaying)
                        {
                            Destroy(wrapper.gameObject);
                        }
                        else
                        {
                            DestroyImmediate(wrapper.gameObject);
                        }
                    }
                    else
                    {
                        if (wrapper.bottomLeftY == y)
                        {
                            wrapper.bottomLeftY += 1;

                            wrapper.SetSize(wrapper.width, wrapper.height - 1);
                            wrapper.ResetSizeAndPosition(grid);
                        }
                        else
                        {
                            int endY = wrapper.bottomLeftY + wrapper.height - 1;

                            wrapper.SetSize(wrapper.width, y - wrapper.bottomLeftY);
                            wrapper.ResetSizeAndPosition(grid);

                            if (endY != y)
                            {
                                GridColliderPart part = GridColliderPart.CreateColliderPart(containerGO, grid, grid.atlas[wrapper.id], x, y + 1, 1, endY - y);
                                part.ResetSizeAndPosition(grid);

                                for (int i = y + 1; i <= endY; i++)
                                {
                                    components.Set(x, i, part);
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (wrapper.bottomLeftX == x)
                    {
                        wrapper.bottomLeftX += 1;

                        wrapper.SetSize(wrapper.width - 1, wrapper.height);
                        wrapper.ResetSizeAndPosition(grid);
                    }
                    else
                    {
                        int endX = wrapper.bottomLeftX + wrapper.width - 1;

                        wrapper.SetSize(x - wrapper.bottomLeftX, wrapper.height);
                        wrapper.ResetSizeAndPosition(grid);

                        if (endX != x)
                        {
                            GridColliderPart part = GridColliderPart.CreateColliderPart(containerGO, grid, grid.atlas[wrapper.id], x + 1, y, endX - x, 1);
                            part.ResetSizeAndPosition(grid);

                            for (int i = x + 1; i <= endX; i++)
                            {
                                components.Set(i, y, part);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public override void OnShowRegion(int regionX, int regionY)
        {
            int bx = regionX * Grid.REGION_SIZE;
            int by = regionY * Grid.REGION_SIZE;

            FiniteGrid          region           = grid.GetRegion(regionX, regionY);
            FiniteComponentGrid regionComponents = components.GetOrCreateRegion(regionX, regionY);

            for (int y = 0; y < Grid.REGION_SIZE; y++)
            {
                GridColliderPart currentWrapper = components.Get(bx - 1, y + by) as GridColliderPart;

                for (int x = 0; x < Grid.REGION_SIZE - 1; x++)
                {
                    Tile tile = region.Get(x, y);

                    if (tile != null)
                    {
                        TileInfo info = grid.atlas [tile.id];

                        if (currentWrapper != null && currentWrapper.Compatible(info) && !info.isVertical && !currentWrapper.isVertical)
                        {
                            currentWrapper.width++;
                        }
                        else
                        {
                            if (currentWrapper != null)
                            {
                                currentWrapper.ResetSizeAndPosition(grid);
                            }

                            if (info.shape != TileShape.EMPTY && !info.isVertical)
                            {
                                currentWrapper = GridColliderPart.CreateColliderPart(containerGO, grid, info, bx + x, by + y, 1, 1);
                            }
                            else
                            {
                                currentWrapper = null;
                            }
                        }

                        regionComponents.Set(x, y, currentWrapper);
                    }
                    else
                    {
                        if (currentWrapper != null)
                        {
                            currentWrapper.ResetSizeAndPosition(grid);
                            currentWrapper = null;
                        }

                        regionComponents.Set(x, y, null);
                    }
                }

                if (currentWrapper != null)
                {
                    currentWrapper.ResetSizeAndPosition(grid);
                }

                Tile edgeTile = region.Get(Grid.REGION_SIZE - 1, y);
                if (edgeTile != null)
                {
                    OnSet(bx + Grid.REGION_SIZE - 1, by + y, edgeTile);
                }
            }

            for (int x = 0; x < Grid.REGION_SIZE; x++)
            {
                GridColliderPart currentWrapper = components.Get(bx + x, by - 1) as GridColliderPart;

                for (int y = 0; y < Grid.REGION_SIZE - 1; y++)
                {
                    Tile tile = region.Get(x, y);

                    if (tile != null)
                    {
                        TileInfo info = grid.atlas [tile.id];

                        if (currentWrapper != null && currentWrapper.Compatible(info) && info.isVertical && currentWrapper.isVertical)
                        {
                            currentWrapper.height++;

                            regionComponents.Set(x, y, currentWrapper);
                        }
                        else
                        {
                            if (currentWrapper != null)
                            {
                                currentWrapper.ResetSizeAndPosition(grid);
                            }

                            if (info.shape != TileShape.EMPTY && info.isVertical)
                            {
                                currentWrapper = GridColliderPart.CreateColliderPart(containerGO, grid, info, bx + x, by + y, 1, 1);

                                regionComponents.Set(x, y, currentWrapper);
                            }
                            else
                            {
                                currentWrapper = null;
                            }
                        }
                    }
                    else
                    {
                        if (currentWrapper != null)
                        {
                            currentWrapper.ResetSizeAndPosition(grid);
                            currentWrapper = null;
                        }
                    }
                }

                if (currentWrapper != null)
                {
                    currentWrapper.ResetSizeAndPosition(grid);
                }

                Tile edgeTile = region.Get(x, Grid.REGION_SIZE - 1);
                if (edgeTile != null && grid.atlas[edgeTile.id].isVertical)
                {
                    OnSet(bx + x, by + Grid.REGION_SIZE - 1, edgeTile);
                }
            }
        }
Ejemplo n.º 8
0
        public override void OnHideRegion(int X, int Y)
        {
            int bx = X * Grid.REGION_SIZE;
            int by = Y * Grid.REGION_SIZE;

            FiniteComponentGrid regionComponents = components.GetRegion(X, Y);

            if (regionComponents != null)
            {
                for (int i = 0; i < Grid.REGION_SIZE; i++)
                {
                    for (int j = 0; j < Grid.REGION_SIZE; j++)
                    {
                        GridColliderPart wrapper = regionComponents.Get(i, j) as GridColliderPart;

                        if (wrapper != null)
                        {
                            if (wrapper.bottomLeftX >= bx && wrapper.bottomLeftX + wrapper.width <= bx + Grid.REGION_SIZE &&
                                wrapper.bottomLeftY >= by && wrapper.bottomLeftY + wrapper.height <= by + Grid.REGION_SIZE)
                            {
                                if (Application.isPlaying)
                                {
                                    Destroy(wrapper.gameObject);
                                }
                                else
                                {
                                    DestroyImmediate(wrapper.gameObject);
                                }
                            }
                            else if (wrapper.isVertical)
                            {
                                if (wrapper.bottomLeftY < by)
                                {
                                    int topY = wrapper.bottomLeftY + wrapper.height;

                                    wrapper.SetSize(1, by - wrapper.bottomLeftY);
                                    wrapper.ResetSizeAndPosition(grid);

                                    if (topY > by + Grid.REGION_SIZE)
                                    {
                                        GridColliderPart topWrapper = GridColliderPart.CreateColliderPart(
                                            containerGO, grid, grid.atlas [wrapper.id],
                                            wrapper.bottomLeftX, by + Grid.REGION_SIZE, 1, topY - (by + Grid.REGION_SIZE)
                                            );
                                        topWrapper.ResetSizeAndPosition(grid);

                                        for (int k = topWrapper.bottomLeftY; k < topWrapper.bottomLeftY + topWrapper.height; k++)
                                        {
                                            components.Set(topWrapper.bottomLeftX, k, topWrapper);
                                        }
                                    }
                                }
                                else if (wrapper.bottomLeftY + wrapper.height > by + Grid.REGION_SIZE)
                                {
                                    wrapper.SetSize(1, wrapper.bottomLeftY + wrapper.height - (by + Grid.REGION_SIZE));
                                    wrapper.bottomLeftY = by + Grid.REGION_SIZE;
                                    wrapper.ResetSizeAndPosition(grid);
                                }
                            }
                            else if (!wrapper.isVertical)
                            {
                                if (wrapper.bottomLeftX < bx)
                                {
                                    int rightX = wrapper.bottomLeftX + wrapper.width;

                                    wrapper.SetSize(bx - wrapper.bottomLeftX, 1);
                                    wrapper.ResetSizeAndPosition(grid);

                                    if (rightX > bx + Grid.REGION_SIZE)
                                    {
                                        GridColliderPart rightWrapper = GridColliderPart.CreateColliderPart(
                                            containerGO, grid, grid.atlas [wrapper.id],
                                            bx + Grid.REGION_SIZE, wrapper.bottomLeftY, rightX - (bx + Grid.REGION_SIZE), 1
                                            );
                                        rightWrapper.ResetSizeAndPosition(grid);

                                        for (int k = rightWrapper.bottomLeftX; k < rightWrapper.bottomLeftX + rightWrapper.width; k++)
                                        {
                                            components.Set(k, rightWrapper.bottomLeftY, rightWrapper);
                                        }
                                    }
                                }
                                else if (wrapper.bottomLeftX + wrapper.width > bx + Grid.REGION_SIZE)
                                {
                                    wrapper.SetSize(wrapper.bottomLeftX + wrapper.width - (bx + Grid.REGION_SIZE), 1);
                                    wrapper.bottomLeftX = bx + Grid.REGION_SIZE;
                                    wrapper.ResetSizeAndPosition(grid);
                                }
                            }
                        }
                    }
                }

                components.ClearRegion(X, Y);
            }
        }