Newer
Older
using System.Collections;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(BlockScript))]
public class OpponentController : Controller
{
private float offsetDistance;
private Rigidbody2D rb;
private float elapsedTime;
void Start()
{
setup();
currentHand = 1;
myTurn = false;
canMove = true;
canSkill = true;
changeTurn();

william
committed
currentStamina = maxStamina;
}
private void setup()
{
animatorController = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
gameManager = FindObjectOfType<GameManager>();
}
private void Update()
{
elapsedTime += Time.deltaTime;
if (rb.velocity == Vector2.zero)
{
animatorController.SetBool("Moving", false);
}
else
{
animatorController.SetBool("Moving", true);
}
}
public override void changeTurn()
{
if (myTurn)
{
foreach(AtkSkills script in GetComponents<AtkSkills>())
{
script.enabled = true;
}
GetComponent<BlockScript>().enabled = false;
ForceBarController.shootAction -= block;
BehindBackScript.action -= steal;
animatorController.SetTrigger("AtkTurn");
myTurn = false;
StartCoroutine(atkTurn());
}
else
{
foreach(DefSkills script in GetComponents<DefSkills>())
{
script.enabled = true;
}
ForceBarController.shootAction += block;
BehindBackScript.action += steal;
animatorController.SetTrigger("DefTurn");
myTurn = true;
StopCoroutine(atkTurn());
}
//do something
}
private IEnumerator atkTurn()
{
while (true)
{
if (elapsedTime > 0.5f)
{
elapsedTime = 0;

william
committed
if (gameManager.getCurrentTime() > gameManager.getTurnDuration() - 1f)
{
yield return movement(7);
}
if (gameManager.getTurnDuration() % gameManager.getCurrentTime() > 1.5f)
{
yield return movement(Random.Range(0, 8));
}
else
{
yield return movement(Random.Range(0, 6));
}
}
yield return null;
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
}
}
private IEnumerator movement(int choice)
{
switch (choice)
{
//W - Move
case 0:
rb.velocity = Vector2.up * speed;
transform.rotation = rb.velocity.x <= 0 ? Quaternion.AngleAxis(0, Vector3.up) : Quaternion.AngleAxis(180, Vector3.up);
yield return null;
break;
//A - Move
case 1:
rb.velocity = Vector2.left * speed;
transform.rotation = rb.velocity.x <= 0 ? Quaternion.AngleAxis(0, Vector3.up) : Quaternion.AngleAxis(180, Vector3.up);
yield return null;
break;
//S - Move
case 2:
rb.velocity = Vector2.down * speed;
transform.rotation = rb.velocity.x <= 0 ? Quaternion.AngleAxis(0, Vector3.up) : Quaternion.AngleAxis(180, Vector3.up);
yield return null;
break;
//D - Move
case 3:
rb.velocity = Vector2.right * speed;
transform.rotation = rb.velocity.x <= 0 ? Quaternion.AngleAxis(0, Vector3.up) : Quaternion.AngleAxis(180, Vector3.up);
yield return null;
break;
//J - StepBack
case 4:
inputs.stepBack = 1;
yield return new WaitUntil(() => rb.velocity == Vector2.zero);
break;
//K - BehindBack
case 5:
inputs.behindBack = 1;
yield return null;
break;
//L - CrossOver
case 6:

william
committed
inputs.resetInputs();
inputs.crossOver = 1;
yield return new WaitUntil(() => rb.velocity == Vector2.zero);
break;
//Space - Shoot
default:
gameManager.shoot(Random.Range(0f, 1f), findThrowPoint(), target);
}
public virtual void OnDisable()
{
disableSkills();
animatorController.SetTrigger("Idle");
}
private void block()
{
//do something
if (GetComponent<Animation>() != null)
{
GetComponent<Animation>().Stop();
}
StartCoroutine(new WaitForSecondsRealtime(UnityEngine.Random.Range(0, 1)));
inputs.block = 1;
private void steal()
{
//do something
if (GetComponent<Animation>() != null)
{
GetComponent<Animation>().Stop();
}
//StartCoroutine(new WaitForSecondsRealtime(UnityEngine.Random.Range(0, 1)));
inputs.steal = UnityEngine.Random.Range(0, 2);
}
public void moveToPosition(Transform atkPosition, Transform defPosition)
{
if (myTurn)
{
rb.MovePosition(atkPosition.position);
}
else
{
rb.MovePosition(defPosition.position);
}
}
private Transform findThrowPoint()
{
foreach (Transform t in GetComponentInChildren<Transform>())
{
if (t.CompareTag("ThrowPoint"))
{
return t;
}
}
return null;
}