Exemplo n.º 1
0
    private void StateAction()
    {
        switch (cState)
        {
        case StateMachine.CombatState.Defend:
            //TODO: - wander om ikke i range, seek om utenfor range, interpose om destroyer kommer, angripe om enemy kommer
            if (Vector3.Distance(transform.position, defenceTarget.transform.position) > defendRange)
            {
                if (RotateObject(defenceTarget.transform.position, 3))
                {
                    //speed = 0.1f;
                }
                mState = StateMachine.MovementState.Seek;
            }                    /*
                                  * if (targetObject.CompareTag()) //hvordan se at det er en kule når man er firkant?
                                  * {
                                  * cState = StateMachine.CombatState.Attack;
                                  * mState = StateMachine.MovementState.Seek;
                                  * }*/
            if (targetObject.CompareTag(destroyerTag))
            {
                mState = StateMachine.MovementState.Interpose;
            }
            else
            {
                mState = StateMachine.MovementState.Idle;
            }
            break;

        case StateMachine.CombatState.Attack:
            if (Vector3.Distance(transform.position, targetObject.transform.position) > attackRange)
            {
                if (!AvoidObstacle())
                {
                    RotateObject(targetObject.transform.position, 3);
                }
            }
            else
            {
                if (targetObject == null)
                {
                    cState = StateMachine.CombatState.Idle;
                    mState = StateMachine.MovementState.Wander;
                    break;
                }
                if (targetScript == null)
                {
                    targetScript = targetObject.GetComponent <FSMController>();
                    if (targetScript == null)
                    {
                        Debug.LogError("No target script found!");
                        return;
                    }
                }
                //Start attack animation if not running
                targetScript.DealDamage(dps * Time.deltaTime);
            }
            break;

        case StateMachine.CombatState.Death:
            Collider c = GetComponent <BoxCollider>();
            if (c == null)
            {
                GetComponent <SphereCollider>().enabled = false;
            }
            else
            {
                c.enabled = false;
            }

            if (defenceTarget != null)
            {
                defenceTarget.GetComponent <StatueScript>().ReduceDefenderCount();
            }
            //Start death animaiton
            isDead = true;
            Destroy(gameObject);
            break;

        case StateMachine.CombatState.Capture:

            if (statueScript == null)
            {
                statueScript = targetObject.GetComponent <StatueScript>();
                if (statueScript == null)
                {
                    Debug.LogError("No target script found!");
                    return;
                }
            }
            if (statueScript.Team == team)
            {
                targetObject = null;
                statueScript = null;
                cState       = StateMachine.CombatState.Idle;
                mState       = StateMachine.MovementState.Wander;
                break;
            }

            if (Vector3.Distance(transform.position, targetObject.transform.position) > 3f)
            {
                RotateObject(targetObject.transform.position, 3);

                //Walk forward with fixed update
            }
            else
            {
                statueScript.DoCapture(team, (int)hpC.MaxHp);
                cState = StateMachine.CombatState.Death;
            }
            break;
        }

        switch (mState)
        {
        case StateMachine.MovementState.Wander:
            //TODO: Check obstacle avoidance, then rotate else wander rotate
            if (!AvoidObstacle())
            {
                if (!wanderActive)
                {
                    break;
                }
                rotateDir = Wander.Direction(rotSpeed, rotateDir, 10);
            }
            break;

        case StateMachine.MovementState.Seek:
            //TODO: Check obstacle avoidance, then rotate or move along path?
            //TODO: Check if enemy is close, then attack or flee
            if (!AvoidObstacle())
            {
                if (defenceTarget != null)
                {
                    RotateObject(defenceTarget.transform.position, 4);
                    break;
                }
                Vector3 disVector = targetObject.transform.position - transform.position;
                if (HelperFunctions.VectorLength(disVector) < seekRange)
                {
                    if (cState != StateMachine.CombatState.Capture)
                    {
                        RotateObject(disVector, 1.5f);
                    }
                }
            }
            break;

        case StateMachine.MovementState.Flee:
            //TODO: Check distance to enemies close, move away or resume
            if (!AvoidObstacle())
            {
                //From target to self, important to calculate target direction to flee to
                Vector3 disVector = (targetObject.transform.position - transform.position);
                if (HelperFunctions.VectorLength(disVector) <= fleeDistance)
                {
                    RotateObject(transform.position - disVector, 4);
                }
            }
            break;

        case StateMachine.MovementState.Interpose:
            if (defenceTarget != null)
            {
                Vector3 intercept = ((defenceTarget.transform.position - targetObject.transform.position) / 2);
                RotateObject(intercept + defenceTarget.transform.position, 4);
            }
            break;
        }
    }
Exemplo n.º 2
0
    private void StateAction()
    {
        switch (cState)
        {
        case StateMachine.CombatState.Defend:

            break;

        case StateMachine.CombatState.Attack:
            if (Vector3.Distance(transform.position, targetObject.transform.position) > attackRange)
            {
                if (!AvoidObstacle())
                {
                    RotateObject(targetObject.transform.position);
                }
            }
            else
            {
                if (targetObject == null)
                {
                    cState = StateMachine.CombatState.Idle;
                    mState = StateMachine.MovementState.Wander;
                    break;
                }

                if (targetScript == null)
                {
                    targetScript = targetObject.GetComponent <FSMController>();
                    if (targetScript == null)
                    {
                        targetObject = null;
                        //Debug.LogError("No target script found!");
                        return;
                    }
                }

                //Start attack animation if not running
                targetScript.DealDamage(dps * Time.deltaTime);
            }

            break;

        case StateMachine.CombatState.Death:
            Collider c = GetComponent <BoxCollider>();
            if (c == null)
            {
                GetComponent <SphereCollider>().enabled = false;
            }
            else
            {
                c.enabled = false;
            }

            if (defenceTarget != null)
            {
                defenceTarget.GetComponent <StatueScript>().ReduceDefenderCount();
            }

            isDead = true;
            SpawnController.DestroyObject(gameObject);
            break;

        case StateMachine.CombatState.Capture:

            //TODO: BUG: Still not working
            if (statueScript == null)
            {
                statueScript = targetObject.GetComponent <StatueScript>();
                if (statueScript == null)
                {
                    //Debug.LogError("No target script found!");
                    return;
                }
            }

            if (statueScript.Team == team)
            {
                targetObject = null;
                statueScript = null;
                cState       = StateMachine.CombatState.Idle;
                mState       = StateMachine.MovementState.Wander;
                break;
            }

            if (Vector3.Distance(transform.position, targetObject.transform.position) > 3f)
            {
                RotateObject(targetObject.transform.position);

                //Walk forward with fixed update
            }
            else
            {
                statueScript.DoCapture(team, (int)hpC.MaxHp);
                cState = StateMachine.CombatState.Death;
            }

            break;
        }

        switch (mState)
        {
        case StateMachine.MovementState.Wander:
            //TODO: Check obstacle avoidance, then rotate else wander rotate
            if (!AvoidObstacle())
            {
                if (!wanderActive)
                {
                    break;
                }
                rotateDir = Wander.Direction(rotSpeed, rotateDir, 10);
            }

            break;

        case StateMachine.MovementState.Seek:
            //TODO: Check obstacle avoidance, then rotate or move along path?
            //TODO: Check if enemy is close, then attack or flee
            if (!AvoidObstacle())
            {
                if (defenceTarget != null)
                {
                    RotateObject(defenceTarget.transform.position);
                    break;
                }

                if (targetObject != null)
                {
                    Vector3 disVector = targetObject.transform.position - transform.position;
                    if (HelperFunctions.VectorLength(disVector) < seekRange)
                    {
                        if (cState != StateMachine.CombatState.Capture)
                        {
                            RotateObject(disVector);
                        }
                    }
                }
            }

            break;

        case StateMachine.MovementState.Flee:
            if (!AvoidObstacle())
            {
                Vector3 disVector = (targetObject.transform.position - transform.position);
                if (HelperFunctions.VectorLength(disVector) <= fleeDistance)
                {
                    RotateObject(transform.position - disVector);
                }
            }

            break;

        case StateMachine.MovementState.Interpose:
            if (targetObject == null)
            {
                mState = StateMachine.MovementState.Wander;
                break;
            }

            if (defenceTarget != null)
            {
                Vector3 intercept = ((defenceTarget.transform.position - targetObject.transform.position) / 2) +
                                    targetObject.transform.position;
                RotateObject(intercept);
            }

            break;
        }
    }