Component _CreateTileGO(GameObject prefab, int x, int y, PositionCallback callback) { GameObject tile = Object.Instantiate(prefab); tile.transform.SetParent(container.transform); tile.transform.localPosition = new Vector2(x + 0.5f, y + 0.5f); TileBehaviour tileBehaviour = tile.GetComponent <TileBehaviour> (); if (tileBehaviour != null) { tileBehaviour._x = x; tileBehaviour._y = y; x += tileBehaviour.areaBottomLeftXOffset; y += tileBehaviour.areaBottomLeftYOffset; bool[,] area = tileBehaviour.area; for (int i = 0; i < area.GetLength(0); i++) { for (int j = 0; j < area.GetLength(1); j++) { if (area [i, j]) { componentGrid.Set(x + i, y + j, tileBehaviour); callback(x + i, y + j); } } } componentGrid.Set( tileBehaviour._x, tileBehaviour._y, tileBehaviour ); return(tileBehaviour); } else { componentGrid.Set(x, y, tile.transform); return(tile.transform); } }
void Clear(GridTriangle triangle, PositionRegionRenderer currentRenderer, int x, int y) { int relX = x - currentRenderer.regionX * Grid.REGION_SIZE; int relY = y - currentRenderer.regionY * Grid.REGION_SIZE; if (triangle != null) { TileInfo info = grid.atlas [triangle.id]; int actualSize; if (triangle.isVertical) { int bottomRelY = triangle.bottomLeftY - currentRenderer.regionY * Grid.REGION_SIZE; if (y == triangle.bottomLeftY) { if (triangle.height == 1) { DestroyImmediate(triangle.gameObject); } else { triangle.height -= 1; triangle.bottomLeftY += 1; Sprite sprite = info.GetSprite(out actualSize, triangle.subId, triangle.height); RenderUpTriangle(currentRenderer, relX, bottomRelY + 1, triangle.height, actualSize, sprite); } } else if (y == triangle.bottomLeftY + triangle.height - 1) { triangle.height -= 1; Sprite sprite = info.GetSprite(out actualSize, triangle.subId, triangle.height); RenderDownTriangle(currentRenderer, relX, bottomRelY, triangle.height, actualSize, sprite); } else { GridTriangle other = GridTriangle.CreateTriangle( containerGO, grid, triangle.id, triangle.subId, true, triangle.bottomLeftX, y + 1, 1, triangle.height - (y + 1 - triangle.bottomLeftY) ); triangle.height = y - triangle.bottomLeftY; Sprite sprite = info.GetSprite(out actualSize, triangle.subId, triangle.height); RenderDownTriangle(currentRenderer, relX, bottomRelY, triangle.height, actualSize, sprite); sprite = info.GetSprite(out actualSize, other.subId, other.height); bottomRelY = other.bottomLeftY - currentRenderer.regionY * Grid.REGION_SIZE; for (int i = 0; i < other.height; i++) { triangles.Set(other.bottomLeftX, other.bottomLeftY + i, other); } RenderUpTriangle(currentRenderer, relX, bottomRelY, other.height, actualSize, sprite); } } else { int bottomRelX = triangle.bottomLeftX - currentRenderer.regionX * Grid.REGION_SIZE; if (x == triangle.bottomLeftX) { if (triangle.width == 1) { DestroyImmediate(triangle.gameObject); } else { triangle.width -= 1; triangle.bottomLeftX += 1; Sprite sprite = info.GetSprite(out actualSize, triangle.subId, triangle.width); RenderRightTriangle(currentRenderer, bottomRelX + 1, relY, triangle.width, actualSize, sprite); } } else if (x == triangle.bottomLeftX + triangle.width - 1) { triangle.width -= 1; Sprite sprite = info.GetSprite(out actualSize, triangle.subId, triangle.width); RenderLeftTriangle(currentRenderer, bottomRelX, relY, triangle.width, actualSize, sprite); } else { GridTriangle other = GridTriangle.CreateTriangle( containerGO, grid, triangle.id, triangle.subId, false, x + 1, triangle.bottomLeftY, triangle.width - (x + 1 - triangle.bottomLeftX), 1 ); triangle.width = x - triangle.bottomLeftX; Sprite sprite = info.GetSprite(out actualSize, triangle.subId, triangle.width); RenderLeftTriangle(currentRenderer, bottomRelX, relY, triangle.width, actualSize, sprite); sprite = info.GetSprite(out actualSize, other.subId, other.width); bottomRelX = other.bottomLeftX - currentRenderer.regionX * Grid.REGION_SIZE; for (int i = 0; i < other.width; i++) { triangles.Set(other.bottomLeftX + i, other.bottomLeftY, other); } RenderRightTriangle(currentRenderer, bottomRelX, relY, other.width, actualSize, sprite); } } triangles.Set(x, y, null); } currentRenderer.mesh.Clear(relX, relY); }
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); } }