public void UpdateTileAsset(Int2 posGrid) { ShipGrid.Tile tile = ShipGrid.GetInstance().GetTile(posGrid); Int2 posSpriteSheet = AssetManager.GetInstance().GetAssetForTile(tile, sorting); Vector2 posTexture = new Vector2(posSpriteSheet.x * PIXELS_PER_UNIT, posSpriteSheet.y * PIXELS_PER_UNIT); Vector2 spriteSheetTilesSize = AssetManager.GetInstance().GetSpriteSheetTilesSize(); Vector2 assetUVMin = new Vector2(); assetUVMin.x = posTexture.x / spriteSheetTilesSize.x; assetUVMin.y = posTexture.y / spriteSheetTilesSize.y; Vector2 assetUVMax = new Vector2(); assetUVMax.x = (posTexture.x + SIZE_TILE * PIXELS_PER_UNIT) / spriteSheetTilesSize.x; assetUVMax.y = (posTexture.y + SIZE_TILE * PIXELS_PER_UNIT) / spriteSheetTilesSize.y; Vector2 assetUVCenter = new Vector2(); assetUVCenter.x = Mathf.Lerp(assetUVMin.x, assetUVMax.x, 0.5f); assetUVCenter.y = Mathf.Lerp(assetUVMin.y, assetUVMax.y, 0.5f); int vertexIndex = GetVertexIndex(posGrid); uvs[vertexIndex + VERTEX_INDEX_BOTTOM_LEFT] = assetUVMin; uvs[vertexIndex + VERTEX_INDEX_TOP_LEFT] = new Vector2(assetUVMin.x, assetUVMax.y); uvs[vertexIndex + VERTEX_INDEX_TOP_RIGHT] = assetUVMax; uvs[vertexIndex + VERTEX_INDEX_BOTTOM_RIGHT] = new Vector2(assetUVMax.x, assetUVMin.y); uvs[vertexIndex + VERTEX_INDEX_CENTER] = assetUVCenter; RotateTile(posGrid, tile.GetRotation(shouldGetTemporary: tile.HasTemporarySettings())); }
void ClearDraggedOutTiles() { for (int i = 0; i < affectedTileCount; i++) { Int2 tilePosGrid = affectedTiles[i]; ShipGrid.Tile tile = ShipGrid.GetInstance().GetTile(tilePosGrid); tile.ClearRoomTypeTemporary(); } }
void DrawDraggedOutTiles(bool isTemporary) { if (ShipGrid.GetInstance().GetTile(posGridStart).GetIsRoom()) { return; } Int2 posGridRoomStart = posGridStart; Int2 posGridRoomEnd = posGridEnd; TileAssetBlock.BlockType typeBlock = TileAssetBlock.BlockType.None; TileAssetBlock tileAssetBlock = AssetManager.GetInstance().GetTileAssetBlockForRoomType(roomTypeCurrent); // Block if (posGridEnd.y - posGridStart.y != 0 && tileAssetBlock.HasAnyValueInBlock()) { rotation = Direction.None; typeBlock = TileAssetBlock.BlockType.Block; } // Line else if (posGridEnd.x - posGridStart.x != 0 && tileAssetBlock.HasAnyValueInLine()) { rotation = Direction.None; typeBlock = TileAssetBlock.BlockType.Line; posGridRoomEnd.y = posGridRoomStart.y; } // Single else if (tileAssetBlock.HasAnyValueInSingle()) { typeBlock = TileAssetBlock.BlockType.Single; posGridRoomEnd = posGridRoomStart; } // Fail else { Debug.LogError(roomTypeCurrent + "'s TileAssetBlock doesn't contain any data!"); } Int2 newPosGridRoomBottomLeft = new Int2(Mathf.Min(posGridRoomStart.x, posGridRoomEnd.x), Mathf.Min(posGridRoomStart.y, posGridRoomEnd.y)); Int2 newPosGridRoomTopRight = new Int2(Mathf.Max(posGridRoomStart.x, posGridRoomEnd.x), Mathf.Max(posGridRoomStart.y, posGridRoomEnd.y)); Int2 newCoveredTileCount = new Int2(); newCoveredTileCount.x = (newPosGridRoomTopRight.x - newPosGridRoomBottomLeft.x) + 1; newCoveredTileCount.y = (newPosGridRoomTopRight.y - newPosGridRoomBottomLeft.y) + 1; if (newCoveredTileCount.y > 1) { if (posGridEnd.y > posGridStart.y) { newPosGridRoomBottomLeft.y -= (newCoveredTileCount.y - 1); } else { newPosGridRoomTopRight.y += (newCoveredTileCount.y - 1); } newCoveredTileCount.y = newCoveredTileCount.y * 2 - 1; } Int2 gridSize = ShipGrid.GetInstance().GetSize(); bool isColliding = false; for (int x = 0; x < newCoveredTileCount.x; x++) { if (isColliding) { break; } for (int y = 0; y < newCoveredTileCount.y; y++) { Int2 tilePosGrid = newPosGridRoomBottomLeft + new Int2(x, y); tilePosGrid.x = Mathf.Clamp(tilePosGrid.x, 0, gridSize.x); tilePosGrid.y = Mathf.Clamp(tilePosGrid.y, 0, gridSize.y); ShipGrid.Tile tile = ShipGrid.GetInstance().GetTile(tilePosGrid); isColliding = tile.GetIsRoom(); Vector2 size = new Vector2(gridSize.x, gridSize.y); Vector2 worldPos = (Vector2)ShipGrid.GetInstance().transform.position - (size / 2) + new Vector2(tilePosGrid.x, tilePosGrid.y) + new Vector2(0.5f, 0.5f); float duration = 100.0f; float length = 0.25f; Debug.DrawLine(worldPos + length * Vector2.up, worldPos + length * Vector2.right, Color.magenta, duration); Debug.DrawLine(worldPos + length * Vector2.right, worldPos + length * Vector2.down, Color.magenta, duration); Debug.DrawLine(worldPos + length * Vector2.down, worldPos + length * Vector2.left, Color.magenta, duration); Debug.DrawLine(worldPos + length * Vector2.left, worldPos + length * Vector2.up, Color.magenta, duration); } } if (isColliding) { newCoveredTileCount = lastCoveredTileCount; newPosGridRoomBottomLeft = lastPosGridRoomBottomLeft; newPosGridRoomTopRight = lastPosGridRoomTopRight; } lastPosGridRoomBottomLeft = newPosGridRoomBottomLeft; lastPosGridRoomTopRight = newPosGridRoomTopRight; lastCoveredTileCount = newCoveredTileCount; affectedTileCount = newCoveredTileCount.x * newCoveredTileCount.y; if (affectedTiles == null || affectedTiles.Length < affectedTileCount) { affectedTiles = new Int2[affectedTileCount]; } int roomID = ShipGrid.Tile.GetUniqueRoomID(newPosGridRoomBottomLeft); for (int x = 0; x < newCoveredTileCount.x; x++) { for (int y = 0; y < newCoveredTileCount.y; y++) { Int2 tilePosGrid = newPosGridRoomBottomLeft + new Int2(x, y); tilePosGrid.x = Mathf.Clamp(tilePosGrid.x, 0, gridSize.x); tilePosGrid.y = Mathf.Clamp(tilePosGrid.y, 0, gridSize.y); ShipGrid.Tile tile = ShipGrid.GetInstance().GetTile(tilePosGrid); tile.SetRotation(rotation, isTemporary); tile.CreateRoom(roomTypeCurrent, roomID, typeBlock, newPosGridRoomBottomLeft, newPosGridRoomTopRight, isTemporary); affectedTiles[y * newCoveredTileCount.x + x] = tilePosGrid; } } }