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); } }
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); } } }