Beispiel #1
0
    public override void AI_RunActiveUnit()
    {
        Example1Unit myActiveUnit = (Example1Unit)_activeUnit;

        if (myActiveUnit.State == Example1Unit.CustomState.ReadyToMove)
        {
            int minDist = int.MaxValue;
            _targetUnit = null;
            foreach (SrpgUnit unit in _playerUnits)
            {
                int dist = SrpgGameUtils.CalcSimpleDistance(myActiveUnit, unit);
                if (dist < minDist)
                {
                    minDist     = dist;
                    _targetUnit = (Example1Unit)unit;
                }
            }

            if (minDist == 1)
            {
                myActiveUnit.SetState(Example1Unit.CustomState.ReadyToAttack);
                ClearActiveUnitCells();
                CreateActiveUnitAttackCells();
            }
            else
            {
                minDist = int.MaxValue;
                SrpgCell targetCell = null;
                foreach (SrpgCell cell in _activeUnitCells)
                {
                    int dist1   = SrpgGameUtils.CalcSimpleDistance(cell.x, cell.y, _activeUnit.x, _activeUnit.y);
                    int dist2   = SrpgGameUtils.CalcSimpleDistance(cell.x, cell.y, _targetUnit.x, _targetUnit.y);
                    int distSum = dist1 + dist2;
                    if (distSum < minDist)
                    {
                        minDist    = distSum;
                        targetCell = cell;
                    }
                }

                MoveActiveUnit(targetCell);
            }
        }
        else if (myActiveUnit.State == Example1Unit.CustomState.ReadyToAttack)
        {
            int dist = SrpgGameUtils.CalcSimpleDistance(myActiveUnit, _targetUnit);
            if (dist <= myActiveUnit.attackRange)
            {
                Attack(myActiveUnit, _targetUnit);
            }
            else
            {
                myActiveUnit.SetState(Example1Unit.CustomState.End);
                ClearActiveUnit();
            }
        }
    }
Beispiel #2
0
    IEnumerator Attack_Coroutine(Example1Unit attackUnit, Example1Unit damageUnit)
    {
        _activeUnitAnimating = true;

        Vector3 attackVec        = damageUnit.transform.position - attackUnit.transform.position;
        Vector3 attackDir        = attackVec.normalized;
        float   attackMaxDist    = attackVec.magnitude / 2.0f;
        Vector3 attackUnitOrigin = attackUnit.transform.position;

        int   halfFrame     = _unitMoveFrame / 2;
        float eachFrameTime = _unitMoveTime / _unitMoveFrame;

        for (int framei = 0; framei < _unitMoveFrame; framei++)
        {
            float ratio = (halfFrame - Math.Abs(halfFrame - framei)) / (float)halfFrame;
            if (ratio == 1.0f)
            {
                StartCoroutine(Damage_Coroutine(attackUnit, damageUnit));
            }
            float attackDist = attackMaxDist * ratio;
            attackUnit.transform.position = attackUnitOrigin + attackDir * attackDist;
            yield return(new WaitForSeconds(eachFrameTime));
        }

        if ((attackUnit.transform.position - attackUnitOrigin).magnitude > 0.0f)
        {
            attackUnit.transform.position = attackUnitOrigin;
        }

        _activeUnitAnimating = false;

        attackUnit.SetState(Example1Unit.CustomState.End);
        ClearActiveUnit();
    }
Beispiel #3
0
    public override void OnActiveUnitMoveEnd()
    {
        Example1Unit myActiveUnit = (Example1Unit)_activeUnit;

        myActiveUnit.SetState(Example1Unit.CustomState.ReadyToAttack);
        CreateActiveUnitAttackCells();
    }
Beispiel #4
0
    protected override bool OnTileClicked(object sender, EventArgs e)
    {
        if (!base.OnTileClicked(sender, e))
        {
            return(false);
        }

        if (_activeUnit == null || _activeUnitAnimating)
        {
            return(false);
        }

        Example1Unit myActiveUnit = (Example1Unit)_activeUnit;

        if (_activeUnit.player)
        {
            if (myActiveUnit.State == Example1Unit.CustomState.ReadyToMove)
            {
                myActiveUnit.SetState(Example1Unit.CustomState.Idle);
                ClearActiveUnit();
            }
        }
        else
        {
            ClearActiveUnit();
        }

        return(true);
    }
Beispiel #5
0
 public override void AI_ChooseEnemyUnit()
 {
     foreach (SrpgUnit unit in _enemyUnits)
     {
         if (!unit.IsEnd())
         {
             Example1Unit myUnit = (Example1Unit)unit;
             myUnit.SetState(Example1Unit.CustomState.ReadyToMove);
             ChangeActiveUnit(myUnit);
             break;
         }
     }
 }
Beispiel #6
0
    protected override bool OnUnitClicked(object sender, EventArgs e)
    {
        if (!base.OnUnitClicked(sender, e))
        {
            return(false);
        }

        Example1Unit clickedUnit = (Example1Unit)sender;

        if (clickedUnit == null)
        {
            Debug.LogError("unit is null");
            return(false);
        }

        if (_activeUnit == null)
        {
            if (clickedUnit.player)
            {
                Debug.Assert(clickedUnit.State != Example1Unit.CustomState.ReadyToMove &&
                             clickedUnit.State != Example1Unit.CustomState.ReadyToAttack);

                if (clickedUnit.State == Example1Unit.CustomState.Idle)
                {
                    ChangeActiveUnit(clickedUnit);
                    clickedUnit.SetState(Example1Unit.CustomState.ReadyToMove);
                }
            }
            else
            {
                ChangeActiveUnit(clickedUnit);
            }
        }
        else if (_activeUnit == clickedUnit)
        {
            if (_activeUnitAnimating)
            {
                return(false);
            }

            if (_activeUnit.player)
            {
                if (clickedUnit.State == Example1Unit.CustomState.ReadyToMove)
                {
                    clickedUnit.SetState(Example1Unit.CustomState.ReadyToAttack);
                    ClearActiveUnitCells();
                    CreateActiveUnitAttackCells();
                }
                else if (clickedUnit.State == Example1Unit.CustomState.ReadyToAttack)
                {
                    clickedUnit.SetState(Example1Unit.CustomState.End);
                    ClearActiveUnit();
                }
            }
        }
        else // myActiveUnit != null
        {
            if (_activeUnitAnimating)
            {
                return(false);
            }

            if (_activeUnit.player)
            {
                Example1Unit myActiveUnit = (Example1Unit)_activeUnit;
                if (myActiveUnit.State == Example1Unit.CustomState.ReadyToMove)
                {
                    if (clickedUnit.player)
                    {
                        clickedUnit.SetState(Example1Unit.CustomState.ReadyToMove);
                    }
                    myActiveUnit.SetState(Example1Unit.CustomState.Idle);
                    ChangeActiveUnit(clickedUnit);
                }
                else if (myActiveUnit.State == Example1Unit.CustomState.ReadyToAttack)
                {
                    if (!clickedUnit.player)
                    {
                        if (SrpgGameUtils.CalcSimpleDistance(_activeUnit, clickedUnit) <= myActiveUnit.attackRange)
                        {
                            Attack(myActiveUnit, clickedUnit);
                        }
                    }
                }
            }
            else
            {
                clickedUnit.SetState(Example1Unit.CustomState.ReadyToMove);
                ChangeActiveUnit(clickedUnit);
            }
        }

        return(true);
    }