コード例 #1
0
    /// <summary>
    /// Update function when state is select and move
    /// </summary>
    private void SelectAndMoveUpdate()
    {
        //SELECT
        //check if left click was pressed
        if (Input.GetButtonDown("Select"))
        {
            //get collider clicked on
            Collider clickedOnCollider = ClickOnCollider();

            //check if player clicked on collider
            if (clickedOnCollider != null)
            {
                //check if clicked on friendly unit AND unit has action
                if (clickedOnCollider.gameObject.CompareTag("Friendly Unit"))
                {
                    //get unitcontroller clicked on
                    UnitController unitController = clickedOnCollider.gameObject.GetComponent <UnitController>();

                    //check if unit has action or not. If it has no action, don't select anything
                    if (!unitController.GetHasAction())
                    {
                        return;
                    }

                    //select unit clicked on
                    SelectUnit(unitController);
                }
                //clicked on ground
                else
                {
                    //unset selected unit
                    DeselectUnit();
                }
            }
        }

        //MOVE
        //check if right click was pressed and there is a selected unit
        else if (Input.GetButtonDown("Alt Select") && _selectedUnit != null)
        {
            //get collider clicked on
            Collider clickedOnCollider = ClickOnCollider();

            //check if player clicked on collider
            if (clickedOnCollider != null)
            {
                Coords oldCoords = _selectedUnit.GetUnitCoords();                                 //the current coords of selected unit
                Coords toCoords  = Grid.WorldSpaceToCoords(clickedOnCollider.transform.position); //the coordinates player clicked on (to move to)

                //check if grid space clicked is selectedunit's own space. Skip movement in that case
                if (grid.GetGridArray(toCoords) == _selectedUnit.gameObject)
                {
                    _selectedUnit.UnhighlightTiles();
                    SetStateAttacking();
                }

                //check and move unit
                else if (_selectedUnit.Move(toCoords))
                {
                    //move was successful, update grid
                    grid.SetGridArray(toCoords, _selectedUnit.gameObject);
                    grid.SetGridArray(oldCoords, null);

                    //state becomes attacking(with selectedunit)
                    SetStateAttacking();
                }
                else
                {
                    //move failed
                    DeselectUnit();
                }
            }
        }
    }
コード例 #2
0
 /// <summary>
 /// Get the coordinates of this unit on a grid
 /// </summary>
 /// <returns>the coordinates of this unit</returns>
 public Coords GetUnitCoords()
 {
     return(Grid.WorldSpaceToCoords(transform.position));
 }