void SetActiveTile() { var tile = grid.GetTile(mousePosition); if (tile != activeTile) { if (activeTile != null) { activeTile.isSelected = false; } if (tile != null) { tile.isSelected = true; } activeTile = tile; RefreshButtons(); } if (activeTile == null && grid.CanBuild(mousePosition, TileType.Flat)) { emptyTileMarker.SetActive(true); emptyTileMarker.transform.position = grid.HexToWorld(grid.WorldToHex(mousePosition)); } else { emptyTileMarker.SetActive(false); } }
public static Mesh GenerateCollisionMesh(TileGrid map) { Vector3[] vertices = new Vector3[(int)(map.width * map.height * 4)]; //iterate through the y values for (int i = 0, y = 0; y < map.height; y++) { //iterate through the x values for (int x = 0; x < map.width; x++) { //check if the tile is "lit" if (map.GetTile(x, y) >= 0) { //add the four corners of the tilemap to the vertices array for (int c = 0; c < 4; c++, i++) { float worldTileCoordX = x * map.tileWidth - map.offsetX; float worldTileCoordY = y * map.tileHeight - map.offsetY; vertices [i] = new Vector3( -map.tileWidth / 2 + (worldTileCoordX + (c % 2) * map.tileWidth), -map.tileHeight / 2 + (worldTileCoordY + (c / 2) * map.tileHeight) + (!(map.TileIsLit(x, y + 1)) ? (-0.3f * ((c / 2))) : 0), 0 ); } } } } int[] triangles = new int [(int)(map.width * map.height * 6)]; for (int ti = 0, vi = 0, y = 0; y < map.height; y++) { for (int x = 0; x < map.width; x++, ti += 6, vi += 4) { triangles [ti] = triangles [ti + 3] = vi; triangles [ti + 2] = vi + 1; triangles [ti + 1] = triangles [ti + 5] = vi + 3; triangles [ti + 4] = vi + 2; } } Mesh collisionMesh = new Mesh(); collisionMesh.vertices = vertices; collisionMesh.triangles = triangles; collisionMesh.RecalculateNormals(); return(collisionMesh); }
//rename this private void Paint(Vector2 tilePosition, int brushValue, bool onDown = false) { //the last tile coordinate clicked on by the player is updated lastCoordinate = new Vector2((int)tilePosition.x, (int)tilePosition.y); //check if the current tile is not equal to the current brush value if (map.GetTile((int)tilePosition.x, (int)tilePosition.y) != brushValue) { //set it equal to the current brush value map.SetTile((int)tilePosition.x, (int)tilePosition.y, brushValue); //mirror logic if (mirrorX) { map.SetTile((int)((map.width - 1) - tilePosition.x), (int)tilePosition.y, brushValue); } if (mirrorY) { map.SetTile((int)(tilePosition.x), (int)((map.height - 1) - tilePosition.y), brushValue); } if (mirrorX && mirrorY) { map.SetTile((int)((map.width - 1) - tilePosition.x), (int)((map.height - 1) - tilePosition.y), brushValue); } if (onDown) { lastBrushValue = currentBrushValue; } } else { if (onDown) { //if it is equal to the current brush value then make it an empty tile map.SetTile((int)tilePosition.x, (int)tilePosition.y, -1); //mirror logic if (mirrorX) { map.SetTile((int)((map.width - 1) - tilePosition.x), (int)tilePosition.y, -1); } if (mirrorY) { map.SetTile((int)(tilePosition.x), (int)((map.height - 1) - tilePosition.y), brushValue); } if (mirrorX && mirrorY) { map.SetTile((int)((map.width - 1) - tilePosition.x), (int)((map.height - 1) - tilePosition.y), brushValue); } lastBrushValue = -1; } } if (brushValue >= 0) { lastClickPosition = tilePosition; } }
void Update() { GameObject hovering = null; RaycastHit hit; bool buildStage = (RoundManager.instance.stage == RoundManager.RoundStage.DAWN || RoundManager.instance.stage == RoundManager.RoundStage.DUSK); if (!ScoreTracker.instance.isSummaryShowing && buildStage && menuCamera.ScreenToWorldPoint(Input.mousePosition).x < menuLimiter.position.x && Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity, terrainLayer)) { hovering = hit.collider.gameObject; } if (currentHover != hovering) { // Remove old: if (currentHover != null) { if (currentTile != null) { HighlightEffect.RemoveHighlight(currentTile); BuildingPrice price = currentTile.GetComponent <BuildingPrice>(); CostDisplay display = currentTile.GetComponentInChildren <CostDisplay>(true); if (price != null && display != null && !price.alwaysShowCost) { display.gameObject.SetActive(false); } } else { HighlightEffect.RemoveHighlight(currentHover); } } // Add new: currentPoint = null; currentHover = null; currentTile = null; if (hovering != null) { currentPoint = world.GetTerrainPos(hovering); if (currentPoint != null) { GameObject tile = world.GetTile(currentPoint.x, currentPoint.y); if (tile != null) { HighlightEffect.AddHighlight(tile, blockHoverColor); currentTile = tile; BuildingPrice price = currentTile.GetComponent <BuildingPrice>(); CostDisplay display = currentTile.GetComponentInChildren <CostDisplay>(true); if (price != null && display != null && !price.alwaysShowCost) { display.gameObject.SetActive(true); } } else { HighlightEffect.AddHighlight(hovering, blockHoverColor); } currentHover = hovering; } } } if (!ScoreTracker.instance.isSummaryShowing && buildStage && Input.GetButtonDown("Click") && currentPoint != null) { if (currentTile == null) { if (BuildMenu.activeTool != null && BuildMenu.activeTool.isPlaceable && Resources.instance.TryBuy(BuildMenu.activeTool.buildCosts)) { if (BuildMenu.activeTool.construction != null) { world.SetTile(currentPoint.x, currentPoint.y, BuildMenu.activeTool.construction); } else { world.SetTile(currentPoint.x, currentPoint.y, BuildingPrice.selected.prefab); } HighlightEffect.RemoveHighlight(currentHover); currentHover = null; world.GetTile(currentPoint.x, currentPoint.y).GetComponent <GameTile>().audio.PlayOneShot(buildSound); SoundEffects.PlaySound(buildSound); foreach (BuildingPrice.Cost cost in BuildMenu.activeTool.buildCosts) { if (cost.type != Resources.Type.PERSON && cost.type != Resources.Type.TIME) { ScoreTracker.instance.AddExpenses(cost.type, cost.amount); } } } } else { currentTile.GetComponent <GameTile>().ClickTile(world, currentPoint.x, currentPoint.y); } } }
public Tile GetTile(Unit unit) { return(tileGrid.GetTile(unit.Id)); }