/// <summary> /// Selects the tile at the given position /// </summary> public void SelectTile(Vector2 position) { if (tiles.ContainsKey(position)) { if (tiles[position].OccupyingUnit != null) { display.gameObject.SetActive(true); display.DisplayUnit(tiles[position].OccupyingUnit); } else { display.gameObject.SetActive(false); } switch (selectionState) { case SelectionState.Unit: DeselectTiles(); tiles[position].Highlight = HighlightState.Selected; if (tiles[position].OccupyingUnit != null && tiles[position].OccupyingUnit.Team == teams[turnStage].teamType) { selectedUnit = tiles[position].OccupyingUnit; selectedUnit.Select(); selectionState = SelectionState.Action; } if (tiles[position].OccupyingUnit == null) { switch (tiles[position].Terrain) { case TileState.Dirt: break; case TileState.Grass: if (teams[turnStage].teamType == TeamType.Plants) { selectionState = SelectionState.PurchaseUnit; selectedPosition = position; shop.DisplayShop(teams[turnStage]); } break; case TileState.Metal: if (teams[turnStage].teamType == TeamType.Robots) { selectionState = SelectionState.PurchaseUnit; selectedPosition = position; shop.DisplayShop(teams[turnStage]); } break; } } break; case SelectionState.Action: switch (tiles[position].Highlight) { case HighlightState.Selected: case HighlightState.UnSelected: selectedUnit = null; DeselectTiles(); selectionState = SelectionState.Unit; break; case HighlightState.Move: Vector2 distance = new Vector2(Mathf.Abs(position.x - selectedUnit.Position.x), Mathf.Abs(position.y - selectedUnit.Position.y)); if (distance.x < distance.y) { selectedUnit.currentMoveRange -= distance.x * 2 + (distance.y - distance.x); } else { selectedUnit.currentMoveRange -= distance.y * 2 + (distance.x - distance.y); } tiles[selectedUnit.Position].OccupyingUnit = null; selectedUnit.Position = position; tiles[selectedUnit.Position].OccupyingUnit = selectedUnit; if (selectedUnit.Team == TeamType.Plants) { tiles[position].Terrain = TileState.Grass; } else { tiles[position].Terrain = TileState.Metal; } DeselectTiles(); selectedUnit.Select(); tiles[selectedUnit.Position].Highlight = HighlightState.Selected; break; case HighlightState.Attack: selectionState = SelectionState.DisplayEnemy; selectedEnemy = tiles[position].OccupyingUnit; DeselectTiles(); tiles[selectedUnit.Position].Highlight = HighlightState.Selected; tiles[position].Highlight = HighlightState.Attack; attackButton.SetActive(true); break; } break; case SelectionState.DisplayEnemy: if (!(position == selectedUnit.Position || position == selectedEnemy.Position)) { selectionState = SelectionState.Action; attackButton.SetActive(false); selectedEnemy = null; selectedUnit.Select(); } break; case SelectionState.PurchaseUnit: DeselectTiles(); shop.HideShop(); selectionState = SelectionState.Unit; break; } } }