Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        if (!generatorScript.loaded)
        {
            return;
        }
        // Clear all tile colors.
        // If you want a tile to stay colored, you must set it every frame
        hexGrid.ClearTileColors();

        if (buyMenu)
        {
            return;
        }
        if (popup)
        {
            return;
        }

        Vector2    mousePos    = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2Int hovered     = hexGrid.GetClosestTileIndex(mousePos);
        TileInfo   hoveredTile = hexGrid.tiles[hovered.x, hovered.y];

        if (turnScript.playerTurn)
        {
            // Click event
            if (Input.GetMouseButtonDown(0))
            {
                Debug.Log("You clicked on: " + hovered.ToString());
                Debug.Log("Type: "
                          + hoveredTile.type.ToString());

                // Conditions for selection to be possible
                if (selected != hovered &&
                    hoveredTile.type != TileType.WATER &&
                    (hoveredTile.unit != null || hoveredTile.camp != null))
                {
                    // Select hovered tile
                    selected = hovered;
                    if (hoveredTile.unit != null)
                    {
                        UnitScript unitScript = hexGrid.tiles[selected.x, selected.y].unitScript;
                        float      range      = unitScript.range;
                        if (range > 0.0f)
                        {
                            ClearNeighbors();
                            neighbors = hexGrid.GetReachableTiles(selected, range, unitScript.type);
                        }
                        else
                        {
                            ClearNeighbors();
                        }
                    }
                    else if (hoveredTile.camp != null)
                    {
                        buyMenu = true;
                        ClearNeighbors();
                        UpdateBuyButtonColors();
                        buyUnitMenu.SetActive(true);
                        campSelection = selected;
                        selected      = noSelection;
                    }
                }
                else if (selected == hovered &&
                         hexGrid.tiles[selected.x, selected.y].unit != null)
                {
                    // Clear selection
                    selected = noSelection;
                    if (neighbors != null)
                    {
                        ClearNeighbors();
                    }
                }
            }
            if (selected != noSelection &&
                hexGrid.tiles[selected.x, selected.y].unit != null)
            {
                // Selection is a unit
                TileInfo unitTile = hexGrid.tiles[selected.x, selected.y];
                //UnitType unitType = unitTile.unitScript.type;

                if (Input.GetMouseButtonDown(0))
                {
                    // Determine whether the unit can move to the target tile
                    if (IsTileWalkable(hovered, selected))
                    {
                        // Move the unit
                        UnitCommand moveCommand;
                        moveCommand.type   = UnitCommandType.MOVE;
                        moveCommand.target = hovered;
                        unitTile.unitScript.SubmitCommand(moveCommand);
                    }
                }
                else if (Input.GetMouseButtonDown(1))
                {
                    // Determine whether the unit can dig the target tile
                    if (IsTileDiggable(hovered, selected))
                    {
                        if (hexGrid.tiles[hovered.x, hovered.y].type == TileType.CITY)
                        {
                            warningPopup.SetActive(true);
                            popup           = true;
                            popupDig.type   = UnitCommandType.DIG;
                            popupDig.target = hovered;
                            popupUnitScript = unitTile.unitScript;
                        }
                        else
                        {
                            UnitCommand digCommand;
                            digCommand.type   = UnitCommandType.DIG;
                            digCommand.target = hovered;
                            unitTile.unitScript.SubmitCommand(digCommand);
                        }
                    }
                    if (IsTileTruckable(hovered, selected))
                    {
                        UnitCommand extinguishCommand;
                        extinguishCommand.type   = UnitCommandType.EXTINGUISH;
                        extinguishCommand.target = hovered;
                        unitTile.unitScript.SubmitCommand(extinguishCommand);
                    }
                }

                if (Input.GetKeyDown(KeyCode.Escape))
                {
                    // Clear selected unit's commands
                    unitTile.unitScript.ClearCommands();
                }
            }

            if (neighbors != null)
            {
                foreach (TileNode tile in neighbors)
                {
                    hexGrid.SetTileColor(tile.coords, neighborsColor);
                    UnitScript unitScript = hexGrid.tiles[selected.x, selected.y].unitScript;
                    float      range      = unitScript.range;
                    UnitType   unitType   = unitScript.type;
                    if (unitType == UnitType.TEST)
                    {
                        if (tile.dist + UnitScript.GetCommandTypeCost(UnitCommandType.DIG) <= range)
                        {
                            hexGrid.SetIconActiveAlpha(tile.coords, UnitCommandType.DIG, true, 0.4f);
                        }
                    }
                }
            }
        }

        foreach (Vector2Int unitTile in hexGrid.unitTiles)
        {
            bool       isSelected = unitTile == selected;
            UnitScript unitScript = hexGrid.tiles[unitTile.x, unitTile.y].unitScript;
            unitScript.DrawCommands(isSelected);
        }
        //hexGrid.MultiplyTileColor(hovered, hoveredColor);
        hexGrid.AverageTileColorWith(hovered, hoveredColor);
        if (selected != noSelection)
        {
            //hexGrid.MultiplyTileColor(selected, selectedColor);
            hexGrid.SetTileColor(selected, selectedColor);
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            // TODO janky for now
            // Deselect everything
            selected = noSelection;
            ClearNeighbors();
        }
    }