using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(Rigidbody2D))] [RequireComponent(typeof(Animator))] public class MovementController : MonoBehaviour { private Rigidbody2D rb; private Animator animatorController; private float timePassed; private bool canMove; private bool canSkill; private bool canShoot; private float currentHand; private ControlStrategy controlType; [SerializeField] private float speed; // Use this for initialization void Start () { //fazer dinamico controlType = new KeyBoardControl(); rb = GetComponent<Rigidbody2D>(); animatorController = GetComponent<Animator>(); currentHand = 1; timePassed = 0; canMove = true; canSkill = true; animatorController.SetBool("Moving", false); animatorController.SetBool("Shooting", false); animatorController.SetTrigger("AtkTurn"); } private void Update() { if (rb.velocity == Vector2.zero) { animatorController.SetBool("Moving", false); } else { animatorController.SetBool("Moving", true); } } // Update is called once per frame private void FixedUpdate() { if (canSkill && Input.GetKeyDown(controlType.getStepBackInput())) { StopCoroutine("move"); StartCoroutine("stepBack"); } else if (canSkill && Input.GetKeyDown(controlType.getBehindBackInput())) { StopCoroutine("move"); StartCoroutine("steal"); } else if (canSkill && Input.GetKeyDown(controlType.getCrossOverInput())) { StopCoroutine("move"); StartCoroutine("crossOver"); } else if (canMove) { StartCoroutine("move"); } } private IEnumerator move() { rb.velocity = new Vector2(Input.GetAxisRaw(controlType.getHorizontalInput()), Input.GetAxisRaw(controlType.getVerticalInput())) * speed; transform.rotation = rb.velocity.x <= 0 ? Quaternion.AngleAxis(180, Vector3.up) : Quaternion.AngleAxis(0, Vector3.up); yield return null; } private IEnumerator stepBack() { canMove = false; canSkill = false; while (timePassed < 1) { rb.velocity = new Vector2(1, currentHand) * speed; timePassed += Time.deltaTime; yield return null; } timePassed = 0; canMove = true; canSkill = true; } private IEnumerator crossOver() { canMove = false; canSkill = false; while (timePassed < 1) { rb.velocity = new Vector2(-1, currentHand) * speed; timePassed += Time.deltaTime; yield return null; } timePassed = 0; canMove = true; canSkill = true; } private void steal() { canMove = false; canSkill = false; animatorController.SetBool("Stealing", true); stealAttempt(); timePassed = 0; canMove = true; canSkill = true; } private void behindBack() { canMove = false; canSkill = false; changeHand(); canMove = true; canSkill = true; } private void changeHand() { currentHand *= -1; } private void stealAttempt() { animatorController.SetTrigger("DefTurn"); animatorController.SetBool("Stealing", false); } }