Newer
Older
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();
StartCoroutine(atkTurn());
} else
{
foreach (DefSkills script in GetComponents<DefSkills>())
{
script.enabled = true;
}
ForceBarController.shootAction += block;
BehindBackScript.action += steal;
animatorController.SetTrigger("DefTurn");
myTurn = true;
enableBallCollider();
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;
}
}
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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;
}