Actions.cs 3.5 KB
Newer Older
1 2 3 4 5 6
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Actions : MonoBehaviour {

7 8 9
	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
Jordan B Mckenney's avatar
Jordan B Mckenney committed
10
	private Image abilitiesImage;							// Picture which shows which ability it active
11

12 13
	private Color abilityUnusable;
	private Color abilityUsable;
14

15
	public int fireballSpeed = 5;
16 17 18 19 20

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

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

	void Start () {
Jordan B Mckenney's avatar
Jordan B Mckenney committed
24 25

		abilitiesImage = GameObject.FindGameObjectWithTag ("Cycle").GetComponent<Image>();
26 27 28 29 30 31 32 33 34 35 36
		// 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 () {

37 38 39 40 41 42
		RaycastHit2D hit = Physics2D.Raycast(transform.position, GetLastDirection());
		Debug.DrawRay(transform.position, GetLastDirection(), Color.red);

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

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
		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")) {
61
			
62 63
			// Increment the counter
			abilitiesCounter++;
64

65 66 67 68 69 70 71 72 73 74 75 76 77
			// 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;
		}
78

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

82 83 84 85 86 87 88 89 90 91
		// 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) {

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

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

				waterShieldUsable = false;
			}
			break;
106 107

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

		case "Ability - Breath of Air":
112 113 114 115
			Debug.Log("Casting breath of air!");
			break;
		}
	}
116 117 118



119
	public Vector2 GetLastDirection() {
120 121 122 123 124 125

		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) {
126
			LastInput = new Vector2 (0, input.y);
127
		} else {
128
			LastInput = new Vector2 (input.x, 0);
129 130 131 132
		}

		return LastInput;
	}
133
}