WaterShield.cs 1.36 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
using UnityEngine;
using System.Collections;

public class WaterShield : MonoBehaviour {

	private float timerOfShield = 0.0f;
	public float durationOfShield = 2.0f;
	public GameObject projectilePrefab;
	public int projectileSpeed = 5;
	public int numberOfProjectiles = 5;
	private GameObject player;
	[HideInInspector]
	public Vector2 playerDirection;

    void Start() {
        player = GameObject.FindGameObjectWithTag("Player");
    }

    void Update () {
		playerDirection = player.GetComponent<Actions>().GetLastDirection();
		transform.position = player.transform.position;

		timerOfShield += Time.deltaTime;

		if(timerOfShield >= durationOfShield) {
			Explode();
			Destroy(this.gameObject);
		}
	}



	void Explode() {
		for(int i = 0; i < numberOfProjectiles; i++) {
			GameObject waterShieldProjectile = (GameObject)Instantiate(projectilePrefab, transform.position, Quaternion.identity);
			waterShieldProjectile.GetComponent<WaterShieldProjectile>().direction = Rotate(playerDirection, 20 - (i * 10));
			waterShieldProjectile.GetComponent<WaterShieldProjectile>().speed = projectileSpeed;
		}
	}



	public Vector2 Rotate(Vector2 v, float degrees) {
		float sin = Mathf.Sin(degrees * Mathf.Deg2Rad);
		float cos = Mathf.Cos(degrees * Mathf.Deg2Rad);

		float tx = v.x;
		float ty = v.y;
		v.x = (cos * tx) - (sin * ty);
		v.y = (sin * tx) + (cos * ty);
		return v;
	}
}