Advertisement
Guest User

Untitled

a guest
May 27th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.19 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. //[RequireComponent(typeof(AudioSource))]
  5. public class Control : MonoBehaviour {
  6.  
  7.     void OnApplicationQuit()
  8.     {
  9.         PlayerPrefs.SetInt("Screenmanager Resolution Width", 1080);
  10.         PlayerPrefs.SetInt("Screenmanager Resolution Height", 1920);
  11.         PlayerPrefs.SetInt("Screenmanager Is Fullscreen mode", 0);
  12.     }
  13.     //snewpdoge dog
  14.     public bool isHigh = false;
  15.     public int whenHigh = 0;
  16.  
  17.     //Setting the GameObjects of the get ready and the tap to play sprites so I can delete them after we start playing
  18.     public GameObject getReadySign;
  19.     public GameObject tutorial;
  20.    
  21.     //Deleting the score in the middle once the bird is dead so its not as ugly
  22.     public GameObject text;
  23.     public GameObject textShadow;
  24.     bool destroyed = false;
  25.  
  26.     //So I don't add too much deaths when I hit the collision
  27.     bool added = false;
  28.  
  29.     //Really strange issues when drawing the NEW highscore sprite so I made a bool for it
  30.     public bool newHighScore = false;
  31.  
  32.     //Jump speed
  33.     public Vector2 jumpForce = new Vector2(0, 300);
  34.    
  35.     //initialising the game with a isDead = false birdie, when this goes to true stuff happens
  36.     public bool isDead = false;
  37.    
  38.     //I think it's obvious
  39.     public int score = 0;
  40.    
  41.     //Generating all the AudioClips for all the audios played in the game.
  42.     public AudioClip hitmarker;
  43.     public AudioClip trumpet;
  44.     public AudioClip wally;
  45.     public AudioClip sadViolin;
  46.     public AudioClip snewp;
  47.     public AudioClip point;
  48.     //////////////////////////////////////////////////////////////////////
  49.    
  50.     //Generating all the audiosources used to play them
  51.     AudioSource snewpAudio;
  52.     AudioSource hitmarkerAudio;
  53.     AudioSource trumpetAudio;
  54.     AudioSource wallyAudio;
  55.     AudioSource sadViolinAudio;
  56.     AudioSource pointAudio;
  57.     //////////////////////////////////////////////////////////////////////
  58.  
  59.     //I need these bools to check whether or not the music has been played so that it doesnt flood with sound.
  60.     private bool musicPlayed = false;
  61.     private bool hitPlayed = false;
  62.    
  63.     //i is for roundrobin-ing the sounds when you jump
  64.     private int i;
  65.  
  66.     void Start () {
  67.        
  68.         //The game is frozen while you don't tap.
  69.         Time.timeScale = 0;
  70.        
  71.         //Initializing all the audios with the AudioSource component of the object.
  72.         hitmarkerAudio = GetComponent<AudioSource>();
  73.         trumpetAudio = GetComponent<AudioSource>();
  74.         wallyAudio = GetComponent<AudioSource>();
  75.         sadViolinAudio = GetComponent<AudioSource>();
  76.         snewpAudio = GetComponent<AudioSource>();
  77.         pointAudio = GetComponent<AudioSource>();
  78.        
  79.         //Background music ftw- snewwwwwwwp
  80.         snewpAudio.PlayOneShot(snewp);
  81.     }
  82.  
  83.     // Update is called once per frame
  84.     void Update () {
  85.         //Quit the game if escape is pressed
  86.         if (Input.GetKeyDown(KeyCode.Escape)) Application.Quit();
  87.  
  88.         //Mouse left button click/space jump - we check every frame and if anyone of these are pressed -
  89.         if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) && !isDead)
  90.         {
  91.             //This is only ran the first time the game starts - we delete the tutorial and the get ready sign
  92.             if(Time.timeScale == 0)
  93.             {
  94.                 GetComponent<Rigidbody2D>().isKinematic = false;
  95.                 Time.timeScale = 1;
  96.                 Destroy(getReadySign);
  97.                 Destroy(tutorial);
  98.                 if (!PlayerPrefs.HasKey("TimesJumped")) PlayerPrefs.SetInt("TimesJumped", 0);
  99.                 if (!PlayerPrefs.HasKey("TotalPoints")) PlayerPrefs.SetInt("TotalPoints", 0);
  100.                 if (!PlayerPrefs.HasKey("TimesDied")) PlayerPrefs.SetInt("TimesDied", 0);
  101.                 //Ui ui = new Ui();
  102.                 //ui.instruction.text = "0";
  103.             }
  104.             Jump();
  105.  
  106.             //Stuff to make the sounds when jumping randomized - WIP
  107.             int randomNumber = Random.Range(0, 3);
  108.             if (randomNumber == 0) hitmarkerAudio.PlayOneShot(hitmarker, 0.4f);
  109.                 else if (randomNumber == 1) trumpetAudio.PlayOneShot(trumpet, 0.4f);
  110.                 else if (randomNumber == 2) wallyAudio.PlayOneShot(wally, 0.8f);
  111.  
  112.             //For some reason they are hella laggy - not synchronized with jumps but w/e THEY WORK!
  113.         }
  114.        
  115.         //Rotation of the bird... Dunno how it works, took it from Google.
  116.         if (GetComponent<Rigidbody2D>().velocity.y > 0)
  117.         {
  118.             float angle1 = Mathf.Lerp(0, 45, (GetComponent<Rigidbody2D>().velocity.y / 3f));
  119.             transform.rotation = Quaternion.Euler(0, 0, angle1);
  120.         }
  121.         else
  122.         {
  123.             float angle = Mathf.Lerp(0, -60, (-GetComponent<Rigidbody2D>().velocity.y / 3f));
  124.             transform.rotation = Quaternion.Euler(0, 0, angle);
  125.        
  126.         }
  127.         //if (sadViolinAudio.isPlaying && isDead) Application.LoadLevel(Application.loadedLevel);
  128.     }
  129.     //Normal Y speed increase function
  130.     void Jump()
  131.     {
  132.         //We add 1 to the total count of times jumped in the playerprefs key "TimesJumped"
  133.         int addJump = 1;
  134.         PlayerPrefs.SetInt("TimesJumped", (PlayerPrefs.GetInt("TimesJumped") + addJump));
  135.  
  136.         GetComponent<Rigidbody2D>().velocity = Vector2.zero;
  137.         GetComponent<Rigidbody2D>().AddForce(jumpForce);
  138.     }
  139.  
  140.     //Detection if we hit something, if we do, call Die();
  141.     void OnCollisionEnter2D()
  142.     {
  143.         isDead = true;
  144.         if (isDead && !hitPlayed) { hitmarkerAudio.PlayOneShot(hitmarker); hitPlayed = true; }
  145.         Die();
  146.     }
  147.  
  148.     //If we hit a box trigger with the tag "score", count score++
  149.     void OnTriggerEnter2D(Collider2D other)
  150.     {
  151.         //We check if the trigger is "score", if it is, we add 1 point to the total score of the player and play the sound.
  152.         if(other.gameObject.CompareTag("score") && !isDead)
  153.         {
  154.            
  155.             score++;
  156.             pointAudio.PlayOneShot(point, 0.5f);
  157.         }
  158.     }
  159.  
  160.     //This is the function which tells what happens when we die.
  161.     void Die()
  162.     {
  163.         //If we are dead and we have yet to play the violin music, stop snewp dogge dog and start the violin.
  164.         if (isDead && !musicPlayed)
  165.         {
  166.             snewpAudio.Stop();
  167.             sadViolinAudio.PlayOneShot(sadViolin);
  168.             musicPlayed = true;
  169.            
  170.         }
  171.  
  172.         int addDeath = 1;
  173.         int addScore = score;
  174.         if (!added && isDead)
  175.         {
  176.             PlayerPrefs.SetInt("TimesDied", (PlayerPrefs.GetInt("TimesDied") + addDeath));
  177.             PlayerPrefs.SetInt("TotalPoints", addScore + PlayerPrefs.GetInt("TotalPoints"));
  178.             added = true;
  179.         }
  180.         //Just make a key for the highscore so we can save it for later.
  181.         if(!PlayerPrefs.HasKey("HighScore"))
  182.         {
  183.             PlayerPrefs.SetInt("HighScore", 0);
  184.         }
  185.  
  186.         //Here we clear the score counter in the middle of the screen as we don't need it.
  187.         if(isDead && !destroyed)
  188.         {
  189.             Destroy(text);
  190.             Destroy(textShadow);
  191.             destroyed = true;          
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement