示例#1
0
    private void UnitOptionsFlow()
    {
        Field fieldHit;

        // 1) Selecting
        if (fieldHit = ray.FieldWithUnitSelector(MouseAction.Down))    // Button Down
        {
            unitSelected = fieldHit.unit;
            unitValidity = CheckUnitValidity(unitSelected);
            if (unitValidity.Valid)
            {
                HighlightUnits(player, false);
                Invoke("HighlightLegalMoves", 0.0f);
                drag.SetArrowStart(fieldHit);
                OnUnitSelected?.Invoke(unitSelected);
            }
            return;
        }

        // 2) Holding -> Only for Drag animation
        if (fieldHit = ray.Selector <Field>(MouseAction.Held))
        {
            if (unitSelected == null)
            {
                return;
            }
            if (!unitValidity.Valid)
            {
                return;
            }
            drag.SetArrowTarget(fieldHit);
            return;
        }
        else if (Input.GetMouseButton(0))
        {
            drag.Pause();
            return;
        }

        // 3) Releasing
        if (Input.GetMouseButtonUp(0) && unitSelected != null)
        {
            UnhighlightLegalMoves();
        }
        if (fieldHit = ray.Selector <Field>(MouseAction.Up))
        {
            if (fieldHit != null && unitSelected != null)
            {
                Action action = new Action(unitSelected.field, fieldHit);
                if (unitValidity.Valid && IsMoveLegal(unitSelected, fieldHit))
                {
                    // Making the actual Move
                    player.MakeAction(action);
                }
                else if (fieldHit != unitSelected.field)
                {
                    // Error message printing
                    Validity worseValidity = unitValidity.GetIfWorse(action.Move.CheckValidity(board));
                    if (worseValidity.PrintIfInvalid())
                    {
                        InfoText.Show(unitSelected.unitName + " can't move like that");
                    }
                    HighlightUnits(player, true);
                }
                else
                {
                    HighlightUnits(player, true);
                }
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            drag.Stop();
            OnUnitDeselected?.Invoke(unitSelected);
            unitSelected = null;
            unitValidity = null;
            if (fieldHit == null)
            {
                HighlightUnits(player, true);
            }
        }
    }