Actions.cs 3.5 KB
Newer Older
Luke Smith's avatar
Luke Smith committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Actions : MonoBehaviour {

	public GameObject[] abilities = new GameObject[4];		// The array full of the character's abilities
	public GameObject currentAbility = null;				// Which ability is currently active
	private int abilitiesCounter = 0;						// A counter which lets you cycle through the abilities
	private Image abilitiesImage;							// Picture which shows which ability it active

	private Color abilityUnusable;
	private Color abilityUsable;

	public int fireballSpeed = 5;

	public bool waterShieldUsable = true;
	private float waterShieldTimer = 0.0f;
	public float waterShieldCooldown = 5.0f;

	private Vector3 LastInput = new Vector2(0, -1);

	void Start () {

		abilitiesImage = GameObject.FindGameObjectWithTag ("Cycle").GetComponent<Image>();
		// Set the initial ability to something so that it's not null
		CycleAbility();

		abilityUnusable = new Color(255.0f,255.0f,255.0f,0.5f);
		abilityUsable = new Color(255.0f,255.0f,255.0f,1.0f);
	}



	void Update () {

		RaycastHit2D hit = Physics2D.Raycast(transform.position, GetLastDirection());
		Debug.DrawRay(transform.position, GetLastDirection(), Color.red);

		// Constantly check where the player is facing
		GetLastDirection();

		if(Input.GetButtonDown("Fire1")) {
			UseAbility(currentAbility.name);
		}

		if(waterShieldUsable == false) {
			waterShieldTimer += Time.deltaTime;
			if(waterShieldTimer > waterShieldCooldown) {
				waterShieldUsable = true;
				waterShieldTimer = 0.0f;
			}

			abilitiesImage.GetComponent<Image>().color = abilityUnusable;
		} else {
			abilitiesImage.GetComponent<Image>().color = abilityUsable;
		}

		// If the player presses the button to choose an ability...
		if(Input.GetButtonDown("Fire2")) {
			
			// Increment the counter
			abilitiesCounter++;

			// Call the ChooseAbility() function
			CycleAbility();
		}
	}



	void CycleAbility() {
		
		// If the counter goes out of range of the array's size, reset it to 0
		if(abilitiesCounter > abilities.Length - 1) {
			abilitiesCounter = 0;
		}

		// Set the currentAbility to the one chosen by the abilitiesCounter
		currentAbility = abilities[abilitiesCounter];

		// Set the image of the gameObject of the ability equal to the picture of the currentAbility
		abilitiesImage.GetComponent<Image>().sprite = currentAbility.GetComponent<SpriteRenderer>().sprite;
	}



	public void UseAbility(string myCurrentAbility) {

		switch(myCurrentAbility) {

		case "Ability - Fireball":
			GameObject clone = Instantiate (currentAbility, this.transform.position, Quaternion.identity) as GameObject;
			clone.GetComponent<FireballProjectile>().move = LastInput;
			clone.GetComponent<FireballProjectile>().speed = fireballSpeed;
			break;

		case "Ability - Water Shield":
			if(waterShieldUsable == true) {
				GameObject myShield = (GameObject)Instantiate(currentAbility, transform.position, Quaternion.identity);
				WaterShield waterShield = myShield.GetComponent<WaterShield>();

				waterShieldUsable = false;
			}
			break;

		case "Ability - Rock":
			Debug.Log("Throwing a rock!");
			break;

		case "Ability - Breath of Air":
			Debug.Log("Casting breath of air!");
			break;
		}
	}



	public Vector2 GetLastDirection() {

		Vector3 input = new Vector3 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"), 0);

		if (input.x == 0 && input.y == 0) {
			//do nothing	
		} else if (input.y != 0) {
			LastInput = new Vector2 (0, input.y);
		} else {
			LastInput = new Vector2 (input.x, 0);
		}

		return LastInput;
	}
}