Skip to content
Snippets Groups Projects
MovementController.cs 3.53 KiB
Newer Older
william's avatar
william committed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Animator))]
william's avatar
william committed
public class MovementController : MonoBehaviour
{
    private Rigidbody2D rb;
    private Animator animatorController;
    private float timePassed;
    private bool canMove;
william's avatar
william committed
    private bool canShoot;
    private float currentHand;

    private ControlStrategy controlType;
william's avatar
william committed

    [SerializeField] private float speed;

    // Use this for initialization
    void Start ()
william's avatar
william committed
    {
        //fazer dinamico
        controlType = new KeyBoardControl();

william's avatar
william committed
        rb = GetComponent<Rigidbody2D>();
        animatorController = GetComponent<Animator>();
william's avatar
william committed
        timePassed = 0;
        canMove = true;
william's avatar
william committed
        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()))
william's avatar
william committed
        {
            StopCoroutine("move");
william's avatar
william committed
        }
        else if (canMove)
        {
            StartCoroutine("move");
        }
    }

    private IEnumerator move()
    {
        rb.velocity = new Vector2(Input.GetAxisRaw(controlType.getHorizontalInput()), Input.GetAxisRaw(controlType.getVerticalInput())) * speed;
william's avatar
william committed
        transform.rotation = rb.velocity.x <= 0 ? Quaternion.AngleAxis(180, Vector3.up) : Quaternion.AngleAxis(0, Vector3.up);
        yield return null;
    }

william's avatar
william committed
    {
        canMove = false;
william's avatar
william committed
        while (timePassed < 1)
        {
            rb.velocity = new Vector2(1, currentHand) * speed;
william's avatar
william committed
            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);
william's avatar
william committed
    }
}