Exemplo n.º 1
0
    //Update once per frame
    void Update()
    {
        //Debug.Log(agent.speed + " " +  agent.velocity);
        //FSM Code checks
        bool isMoving           = Input.GetButton("Moving");
        bool isInteractable     = Input.GetButton("Interactable");
        bool isDefaultAttack    = Input.GetButton("Default Attack");      //Rename this attack later to dash attack
        bool isProjectileAttack = Input.GetButton("Projectile Attack");
        bool isAOEAttack        = Input.GetButton("AOE Attack");
        bool isBuffAttack       = Input.GetButton("Buff Attack");
        bool isDebuffAttack     = Input.GetButton("Debuff Attack");

        if (EventSystem.current.IsPointerOverGameObject())
        {
            return;
        }

        /*
         *      Hot key weapon switching. Remove later
         */
        /*
         * switch (Input.inputString) {
         * case "0":
         *      WeaponState = 0;//unarmed
         *      equipStatus = false;
         *
         *      break;
         * case "1":
         *      WeaponState = 1;//longsword
         *      if (!equipStatus) {
         *              playSFX(weaponEquip);
         *              equipStatus = true;
         *      }
         *      break;
         * }
         */


        /*
         * State machine of the player. Will handle idle, moving, interacting and attacking states of the player
         * Waiting is not empty for future use
         * Creator: Kevin Ho
         */
        switch (state)
        {
        case TPC_State.IDLE:
            //Debug.Log("State: IDLE");
            //Attack was found
            if (isDefaultAttack | isProjectileAttack | isAOEAttack | isBuffAttack | isDebuffAttack)
            {
                state = TPC_State.ATTACKING;
                return;
            }
            //Destination mouse click was found
            if (isMoving)
            {
                state = TPC_State.MOVING;
                return;
            }
            //Interactable mouse click was found
            if (isInteractable)
            {
                state = TPC_State.INTERACTING;
                return;
            }
            break;

        case TPC_State.MOVING:
            //New location clicked
            if ((!isDefaultAttack | !isProjectileAttack | !isAOEAttack | !isBuffAttack | !isDebuffAttack) & isMoving)
            {
                //Debug.Log("State: NEW MOVING");
                MousePoint();
                if (agent.remainingDistance < 0.1f)
                {
                    state = TPC_State.IDLE;
                    return;
                }
            }
            //Called attack.
            if (isDefaultAttack | isProjectileAttack | isAOEAttack | isBuffAttack | isDebuffAttack)
            {
                motor.MoveToPoint(this.transform.position);
                state = TPC_State.IDLE;
            }
            //Interactable mouse click was found
            if (isInteractable)
            {
                state = TPC_State.INTERACTING;
                return;
            }
            //Still currently moving
            else
            {
                //Debug.Log("State: CURRENTLY MOVING");
                if (agent.remainingDistance < 0.1f)
                {
                    state = TPC_State.IDLE;
                    return;
                }
            }
            break;

        case TPC_State.INTERACTING:
            //Debug.Log("State: INTERACTING");
            //New location clicked. Override interactable
            if ((!isDefaultAttack | !isProjectileAttack | !isAOEAttack | !isBuffAttack | !isDebuffAttack) & isMoving)
            {
                state = TPC_State.MOVING;
                return;
            }
            //New interactable clicked
            if ((!isDefaultAttack | !isProjectileAttack | !isAOEAttack | !isBuffAttack | !isDebuffAttack) & isInteractable)
            {
                //Debug.Log("State: NEW INTERACTABLE");
                MousePoint();
                if (agent.remainingDistance < 1.5f)                  //Change value later when interactables range is less
                {
                    if (interactableTag == "Item" && Time.time > nextInteractable)
                    {
                        nextInteractable = Time.time + interactableRate;
                        interactableTag  = null;
                    }
                    else if (WeaponState == 0 && ((interactableTag == "Enemy") || (interactableTag == "EnemyArcher")) && Time.time > nextInteractable)
                    {
                        nextInteractable = Time.time + interactableRate;
                        playSFX(punchAttack);
                        interactableTag = null;
                    }
                    state = TPC_State.IDLE;
                    return;
                }
            }
            //Still currently moving  to interactable
            else
            {
                //Debug.Log("State: CURRENT INTERACTABLE");
                //Debug.Log(agent.remainingDistance.ToString("F4"));
                if (agent.remainingDistance < 1.5f)
                {
                    //Play pick up soiund
                    if (interactableTag == "Item" && Time.time > nextInteractable)
                    {
                        nextInteractable = Time.time + interactableRate;
                        interactableTag  = null;
                    }
                    else if (WeaponState == 0 && ((interactableTag == "Enemy") || (interactableTag == "EnemyArcher")) && Time.time > nextInteractable)
                    {
                        nextInteractable = Time.time + interactableRate;
                        playSFX(punchAttack);
                        interactableTag = null;
                    }
                    state = TPC_State.IDLE;
                    return;
                }
            }
            break;

        case TPC_State.ATTACKING:
            //Player is using default attack
            //Debug.Log("State: ATTACKING");
            if (isDefaultAttack)
            {
                RaycastHit hit            = getMousePosition();
                Quaternion targetRotation = rotationAngle(hit);
                motor.rotatePlayer(targetRotation);
                motor.fireDashAttack();
                StartCoroutine(AttackWait(dashAttackWait));
                if (spellSlots != null)
                {
                    spellSlots[2].GetComponent <UISlotCooldown>().StartCooldown(3, 5f);
                }
                //motor.fireDefaultAttack();
                //StartCoroutine(AttackWait(defaultAttackWait));
                //state = TPC_State.IDLE;
                return;
            }
            //Player is using projectile attack
            if (isProjectileAttack)
            {
                RaycastHit hit            = getMousePosition();
                Quaternion targetRotation = rotationAngle(hit);
                motor.rotatePlayer(targetRotation);
                motor.fireProjectileAttack();
                StartCoroutine(AttackWait(projectileAttackWait));
                if (spellSlots != null)
                {
                    spellSlots[1].GetComponent <UISlotCooldown>().StartCooldown(2, 1.5f);
                }
                //state = TPC_State.IDLE;
                return;
            }
            //Player is using AOE attack
            if (isAOEAttack)
            {
                RaycastHit hit            = getMousePosition();
                Quaternion targetRotation = rotationAngle(hit);
                motor.rotatePlayer(targetRotation);
                motor.fireAOEAttack();
                if (spellSlots != null)
                {
                    spellSlots[0].GetComponent <UISlotCooldown>().StartCooldown(1, 4f);
                }
                //StartCoroutine(AttackWait(AOEAttackWait));	//Add back to lock player down
                //state = TPC_State.IDLE;
                return;
            }
            //Player is using buff attack
            if (isBuffAttack)
            {
                RaycastHit hit            = getMousePosition();
                Quaternion targetRotation = rotationAngle(hit);
                motor.rotatePlayer(targetRotation);
                motor.fireBuffAttack();
                StartCoroutine(AttackWait(buffAttackWait));
                return;
            }
            if (isDebuffAttack)
            {
                RaycastHit hit            = getMousePosition();
                Quaternion targetRotation = rotationAngle(hit);
                motor.rotatePlayer(targetRotation);
                motor.fireDebuffAOEAttack(hit);
                StartCoroutine(AttackWait(debuffAttackWait));
                return;
            }
            state = TPC_State.IDLE;
            break;

        //Waiting state does nothing
        case TPC_State.WAITING:
            //Debug.Log("State: WAITING");
            break;
        }
    }