OpponentController.cs 4.73 KiB
using System.Collections;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(BlockScript))]
public class OpponentController : Controller
{
private float offsetDistance;
private Rigidbody2D rb;
private GameManager gameManager;
private float elapsedTime;
void Start()
{
setup();
inputs = new Inputs ();
currentHand = 1;
myTurn = false;
canMove = true;
canSkill = true;
changeTurn();
elapsedTime = 0;
currentStamina = maxStamina;
}
private void setup()
{
animatorController = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
gameManager = FindObjectOfType<GameManager>();
maxStamina = new Easy().getMaxStamina();
speed = new Easy().getSpeed();
jumpForce = new Easy().getJumpForce();
}
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;
elapsedTime = 0;
disableBallCollider();
threePoint();
StartCoroutine(atkTurn());
} else
{
foreach (DefSkills script in GetComponents<DefSkills>())
{
script.enabled = true;
}
ForceBarController.shootAction += block;
BehindBackScript.action += steal;
animatorController.SetTrigger("DefTurn");
myTurn = true;
enableBallCollider();
twoPoint();
StopCoroutine(atkTurn());
}
//do something
}
private IEnumerator atkTurn()
{
while (true)
{
if (elapsedTime > 0.5f)
{
elapsedTime = 0;
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;
}
}
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.resetInputs();
inputs.stepBack = 1;
yield return new WaitUntil (() => rb.velocity == Vector2.zero);
break;
//K - BehindBack
case 5:
inputs.resetInputs();
inputs.behindBack = 1;
yield return null;
break;
//L - CrossOver
case 6:
inputs.resetInputs();
inputs.crossOver = 1;
yield return new WaitUntil (() => rb.velocity == Vector2.zero);
break;
//Space - Shoot
default:
inputs.resetInputs();
gameManager.shoot(Random.Range(0f, 1f), findThrowPoint(), target);
yield return null;
break;
}
}
public virtual void OnDisable()
{
StopAllCoroutines();
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;
}
}