Newer
Older
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 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;
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()))
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()
canSkill = false;
rb.velocity = new Vector2(1, currentHand) * speed;
timePassed += Time.deltaTime;
yield return null;
}
timePassed = 0;
canMove = true;
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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);