Advertisement
l3enjamin

Chip's Tunes - Activators.cs

May 8th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Activators : MonoBehaviour {
  5.  
  6.     public KeyCode Key;
  7.     bool Active;
  8.     GameObject Note, GM;
  9.     SpriteRenderer SR;
  10.     Color OldColor;
  11.     public bool CreateMode;
  12.     public GameObject BasicNote;
  13.     public ParticleSystem Hit;
  14.     public AudioSource Failure, Miss;
  15.     public bool Inv = true;
  16.  
  17.     void Start () {
  18.         GM = GameObject.Find ("GameManager");
  19.         SR = GetComponent<SpriteRenderer> ();
  20.         OldColor = SR.color;
  21.     }
  22.  
  23.     void Update () {
  24.         //Creating a song by hand
  25.         if (CreateMode) {
  26.             if (Input.GetKeyDown (Key))
  27.                 Instantiate (BasicNote, transform.position, Quaternion.identity);
  28.         } else {
  29.             //Invincibility at the start of a song
  30.             if (Input.GetKeyDown (Key)) {
  31.                 StartCoroutine (pressed ());
  32.                 if (Inv == true) {
  33.                     Miss.Play ();
  34.                 }
  35.             }
  36.             //Hitting a note
  37.             if (Input.GetKeyDown (Key) && Active) {
  38.                 Destroy (Note);
  39.                 GM.GetComponent<gamemanager> ().addstreak ();
  40.                 addscore ();
  41.                 Active = false;
  42.                 explode ();
  43.             }
  44.             //Miss a note
  45.             else if (Input.GetKeyDown (Key) && !Active && Inv == false) {
  46.                 Miss.Play ();
  47.                 if (GM.GetComponent<gamemanager>().streak > 25) {
  48.                     Failure.Play ();
  49.                 }
  50.                 GM.GetComponent<gamemanager> ().resetstreak ();
  51.             }
  52.         }
  53.     }
  54.     void OnTriggerStay2D(Collider2D col) {
  55.         //Player wins
  56.         if (col.gameObject.tag == "WinNote") {
  57.             GM.GetComponent<gamemanager> ().Win ();
  58.         }
  59.         //Assigning the current note
  60.         if (col.gameObject.tag == "Note") {
  61.             Note = col.gameObject;
  62.             Active = true;
  63.         }
  64.         //Turning off invincibility
  65.         if (col.gameObject.tag == "Inv") {
  66.             Inv = false;
  67.         }
  68. }
  69.     void OnTriggerExit2D(Collider2D col) {
  70.         //If the player misses a note
  71.         GM.GetComponent<gamemanager> ().missnote ();
  72.         Active = false;
  73.         GM.GetComponent<gamemanager> ().resetstreak ();
  74.     }
  75.  
  76.     void addscore () {
  77.         //Calculate total score
  78.         PlayerPrefs.SetInt ("Score", PlayerPrefs.GetInt ("Score") + GM.GetComponent<gamemanager> ().getscore ());
  79.     }
  80.     void explode () {
  81.         //Particle effect when key is pressed
  82.         ParticleSystem explode = Instantiate (Hit) as ParticleSystem;
  83.         explode.transform.position = transform.position;
  84.         explode.Play ();
  85.     }
  86.  
  87.  
  88.     IEnumerator pressed () {
  89.         //Changes activator color when key is pressed
  90.         SR.color = new Color (0, 0, 0);
  91.         yield return new WaitForSeconds (0.2f);
  92.         SR.color = OldColor;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement