Euras

Spawner

Feb 1st, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5. public class Spawner : MonoBehaviour {
  6.  
  7.     public GameObject Bubble;
  8.     GameObject _Bubble;
  9.     public GameObject Fish;
  10.     public GameObject BlueFish;
  11.     public GameObject GoldFish;
  12.     public GameObject Shark;
  13.  
  14.     public static bool doSpawn = true;
  15.     public static bool doSpawnFish = true;
  16.     public static bool doSpawnBlueFish = true;
  17.     public static bool doSpawnGoldFish = true;
  18.     public static bool doSpawnShark = true;
  19.  
  20.     float spawnSpeed = 0.25F;
  21.  
  22.     void Update()
  23.     {
  24.         if(!MainGlobals.isGameOver)
  25.         {
  26.             if (doSpawn)
  27.             {
  28.                 StartCoroutine(spawnBubbles());
  29.             }
  30.  
  31.             if (doSpawnFish)
  32.             {
  33.                 StartCoroutine(spawnFishes());
  34.             }
  35.  
  36.             if (doSpawnBlueFish)
  37.             {
  38.                 StartCoroutine(spawnBlueFishes());
  39.             }
  40.  
  41.             if (doSpawnGoldFish)
  42.             {
  43.                 StartCoroutine(spawnGoldFishes());
  44.             }
  45.  
  46.             if (doSpawnShark)
  47.             {
  48.                 StartCoroutine(spawnSharks());
  49.             }
  50.         }
  51.     }
  52.  
  53.     IEnumerator spawnBubbles()
  54.     {
  55.         doSpawn = false;
  56.         yield return new WaitForSeconds(spawnSpeed);
  57.         spawnBubble();
  58.         doSpawn = true;
  59.     }
  60.  
  61.     IEnumerator spawnFishes()
  62.     {
  63.         doSpawnFish = false;
  64.         yield return new WaitForSeconds(UnityEngine.Random.Range(3, 7));
  65.         spawnFish();
  66.         doSpawnFish = true;
  67.     }
  68.  
  69.     IEnumerator spawnBlueFishes()
  70.     {
  71.         doSpawnBlueFish = false;
  72.         yield return new WaitForSeconds(UnityEngine.Random.Range(8, 15));
  73.         spawnBlueFish();
  74.         doSpawnBlueFish = true;
  75.     }
  76.  
  77.     IEnumerator spawnGoldFishes()
  78.     {
  79.         doSpawnGoldFish = false;
  80.         yield return new WaitForSeconds(UnityEngine.Random.Range(8, 15));
  81.         spawnGoldFish();
  82.         doSpawnGoldFish = true;
  83.     }
  84.  
  85.     IEnumerator spawnSharks()
  86.     {
  87.         doSpawnShark = false;
  88.         yield return new WaitForSeconds(UnityEngine.Random.Range(8, 15));
  89.         spawnShark();
  90.         doSpawnShark = true;
  91.     }
  92.  
  93.     public GameObject spawnBubble()
  94.     {
  95.         Vector3 pos = new Vector3(UnityEngine.Random.Range(-3.0F, 3.0F), -6.5F, -5);
  96.         _Bubble = (GameObject)Instantiate(Bubble, pos, Quaternion.identity);
  97.         float bubbleSize = UnityEngine.Random.Range(1.0F, 1.5F);
  98.         _Bubble.transform.localScale = new Vector3(bubbleSize, bubbleSize, _Bubble.transform.localScale.z);
  99.         _Bubble.transform.tag = "bubble";
  100.         return _Bubble;
  101.     }
  102.  
  103.     public void spawnFish()
  104.     {
  105.         if(_Bubble.transform.childCount <= 0)
  106.         {
  107.             spawnFishes(Fish);
  108.         }
  109.     }
  110.  
  111.     public void spawnBlueFish()
  112.     {
  113.         if (_Bubble.transform.childCount <= 0)
  114.         {
  115.             spawnFishes(BlueFish);
  116.         }
  117.     }
  118.  
  119.     public void spawnGoldFish()
  120.     {
  121.         if (_Bubble.transform.childCount <= 0)
  122.         {
  123.             spawnFishes(GoldFish);
  124.         }
  125.     }
  126.  
  127.     public void spawnShark()
  128.     {
  129.         if (_Bubble.transform.childCount <= 0)
  130.         {
  131.             spawnFishes(Shark);
  132.         }
  133.     }
  134.  
  135.     public void spawnFishes(GameObject fishType)
  136.     {
  137.         GameObject newFish = (GameObject)Instantiate(fishType, _Bubble.transform.position, Quaternion.identity);
  138.         newFish.transform.SetParent(_Bubble.transform);
  139.         _Bubble.GetComponent<Rigidbody2D>().gravityScale = UnityEngine.Random.Range(-0.03F, -0.5F); ;
  140.         newFish.transform.localScale = _Bubble.transform.localScale / 2;
  141.         if (newFish.transform.position.x > 0)
  142.         {
  143.             newFish.transform.rotation = Quaternion.Euler(0, 180, 0);
  144.         }
  145.         newFish.transform.Rotate(0.0f, 0.0f, UnityEngine.Random.Range(-45.0f, 45.0f));
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment