コード例 #1
0
        public override void OnShowRegion(int regionX, int regionY)
        {
            FiniteGrid             region         = grid.GetRegion(regionX, regionY);
            FiniteComponentGrid    regionTriangle = triangles.GetRegion(regionX, regionY);
            PositionRegionRenderer renderer       = GetRegionRenderer(regionX, regionY);

            ClearRenderers();
            renderer.mesh.PrepareUV();

            int bx = regionX * Grid.REGION_SIZE;
            int by = regionY * Grid.REGION_SIZE;

            for (int i = 0; i < Grid.REGION_SIZE; i++)
            {
                for (int j = 0; j < Grid.REGION_SIZE; j++)
                {
                    Tile tile = region.Get(i, j);

                    if (tile != null && tile.id != 0)
                    {
                        if (regionTriangle == null)
                        {
                            _OnSet(null, renderer, i + bx, j + by, tile);
                        }
                        else
                        {
                            _OnSet(regionTriangle.Get(i, j) as GridTriangle, renderer, i + bx, j + by, tile);
                        }
                    }
                }
            }

            renderer.mesh.ApplyUV();
            ApplyRenderers();
        }
コード例 #2
0
        public override void OnSet(int x, int y, int width, int height)
        {
            int minRegionX = Mathf.FloorToInt(x / (float)Grid.REGION_SIZE);
            int minRegionY = Mathf.FloorToInt(y / (float)Grid.REGION_SIZE);
            int maxRegionX = Mathf.FloorToInt((x + width) / (float)Grid.REGION_SIZE);
            int maxRegionY = Mathf.FloorToInt((y + height) / (float)Grid.REGION_SIZE);

            for (int regionX = minRegionX; regionX <= maxRegionX; regionX++)
            {
                for (int regionY = minRegionY; regionY <= maxRegionY; regionY++)
                {
                    FiniteGrid region = grid.GetRegion(regionX, regionY);

                    if (region.presented)
                    {
                        PositionRegionRenderer renderer       = GetRegionRenderer(regionX, regionY);
                        FiniteComponentGrid    triangleRegion = triangles.GetRegion(regionX, regionY);

                        int startX = regionX * Grid.REGION_SIZE;
                        int startY = regionY * Grid.REGION_SIZE;

                        int minX = Mathf.Max(x, startX);
                        int minY = Mathf.Max(y, startY);
                        int maxX = Mathf.Min(x + width, (regionX + 1) * Grid.REGION_SIZE);
                        int maxY = Mathf.Min(y + height, (regionY + 1) * Grid.REGION_SIZE);

                        renderer.mesh.PrepareUV();

                        for (int i = minX; i < maxX; i++)
                        {
                            for (int j = minY; j < maxY; j++)
                            {
                                Tile tile = region.Get(i - startX, j - startY);

                                if (tile != null)
                                {
                                    if (triangleRegion != null)
                                    {
                                        _OnSet(triangleRegion.Get(x - startX, y - startY) as GridTriangle, renderer, i, j, tile);
                                    }
                                    else
                                    {
                                        _OnSet(null, renderer, i, j, tile);
                                    }
                                }
                            }
                        }

                        renderer.mesh.ApplyUV();
                    }
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Gets the specified region and creates it if there is none yet.
        /// </summary>
        /// <returns>The or create region.</returns>
        /// <param name="X">The X region coordinate.</param>
        /// <param name="Y">The Y region coordinate.</param>
        public FiniteComponentGrid GetOrCreateRegion(int X, int Y)
        {
            FiniteComponentGrid region = GetRegion(X, Y);

            if (region == null)
            {
                region = new FiniteComponentGrid(X, Y, _regionSize);

                regions.Add(region);
            }

            return(region);
        }
コード例 #4
0
        /// <summary>
        /// Sets the component at the specified coordinates.
        /// </summary>
        /// <param name="x">The x tile coordinate.</param>
        /// <param name="y">The y tile coordinate.</param>
        /// <param name="value">The new value.</param>
        public void Set(int x, int y, Component value)
        {
            FiniteComponentGrid region = GetContainingRegion(x, y);

            if (region == null)
            {
                int X = Mathf.FloorToInt(((float)x) / _regionSize);
                int Y = Mathf.FloorToInt(((float)y) / _regionSize);

                region = new FiniteComponentGrid(X, Y, _regionSize);

                regions.Add(region);
            }

            region.Set(x - region.x * _regionSize, y - region.y * _regionSize, value);
        }
コード例 #5
0
        /// <summary>
        /// Gets the component at the specified coordinates.
        /// </summary>
        /// <param name="x">The x tile coordinate.</param>
        /// <param name="y">The y tile coordinate.</param>
        public Component Get(int x, int y)
        {
            FiniteComponentGrid region = GetContainingRegion(x, y);

            return(region != null?region.Get(x - region.x *_regionSize, y - region.y *_regionSize) : null);
        }
コード例 #6
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);
                }
            }
        }
コード例 #7
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);
            }
        }