Skip to content
Snippets Groups Projects
OpponentController.cs 4.73 KiB
Newer Older
using System.Collections;
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(BlockScript))]
public class OpponentController : Controller
william's avatar
william committed
	private float offsetDistance;
	private Rigidbody2D rb;
	private GameManager gameManager;
	private float elapsedTime;
william's avatar
william committed
	void Start()
	{
		setup();
		inputs = new Inputs ();
		currentHand = 1;
		myTurn = false;
		canMove = true;
		canSkill = true;
		changeTurn();
		elapsedTime = 0;
		currentStamina = maxStamina;
	}
william's avatar
william committed
	private void setup()
	{
		animatorController = GetComponent<Animator>();
		rb = GetComponent<Rigidbody2D>();
		gameManager = FindObjectOfType<GameManager>();
		maxStamina = new Easy().getMaxStamina();
		speed = new Easy().getSpeed();
		jumpForce = new Easy().getJumpForce();
william's avatar
william committed
	}
william's avatar
william committed
	private void Update()
	{
		elapsedTime += Time.deltaTime;
william's avatar
william committed
		if (rb.velocity == Vector2.zero)
		{
			animatorController.SetBool("Moving", false);
		} else
		{
			animatorController.SetBool("Moving", true);
		}
	}
william's avatar
william committed
	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();
Will Lucena's avatar
Will Lucena committed
            threePoint();
william's avatar
william committed
			StartCoroutine(atkTurn());    
		} else
		{
			foreach (DefSkills script in GetComponents<DefSkills>())
			{
				script.enabled = true;
			}
			ForceBarController.shootAction += block;
			BehindBackScript.action += steal;
			animatorController.SetTrigger("DefTurn");
			myTurn = true;
			enableBallCollider();
Will Lucena's avatar
Will Lucena committed
            twoPoint();
william's avatar
william committed
			StopCoroutine(atkTurn());  
		}
		//do something
	}
william's avatar
william committed
	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;          
		}
	}
william's avatar
william committed
	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;
		}
	}
william's avatar
william committed
	public virtual void OnDisable()
	{
		StopAllCoroutines();
		disableSkills();
		animatorController.SetTrigger("Idle");
	}
william's avatar
william committed
	private void block()
	{
		//do something
		if (GetComponent<Animation>() != null)
		{
			GetComponent<Animation>().Stop();
		}
		StartCoroutine(new WaitForSecondsRealtime (UnityEngine.Random.Range(0, 1)));
		inputs.block = 1;
	}
william's avatar
william committed
	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);
	}
william's avatar
william committed
	public void moveToPosition(Transform atkPosition, Transform defPosition)
	{
		if (myTurn)
		{
			rb.MovePosition(atkPosition.position);
		} else
		{
			rb.MovePosition(defPosition.position);
		}
	}
william's avatar
william committed
	private Transform findThrowPoint()
	{
		foreach (Transform t in GetComponentInChildren<Transform>())
		{
			if (t.CompareTag("ThrowPoint"))
			{
				return t;
			}
		}
		return null;
	}