Advertisement
Redee

По Людски через делегата.... ))

Jan 30th, 2017
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. /////////////
  2. // По Людски через делегата....
  3. /////
  4.  
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8.  
  9. public class GameController : MonoBehaviour
  10. {
  11.     delegate void DoWork();
  12.     DoWork doMyWork;
  13.  
  14.     public GameObject hazard;
  15.     public Vector3 spawnValues = new Vector3(6, 0, 16);
  16.     public float spawnWait = .5f;
  17.     public float wait = 1;
  18.     public int hazardCount = 10;
  19.  
  20.     float nextTime;
  21.  
  22.     // Use this for initialization
  23.     void Start ()
  24.     {
  25.         nextTime = Time.time + wait;
  26.         doMyWork = SpawnWaves;
  27.     }
  28.  
  29.     void SpawnWaves()
  30.     {
  31.         if (hazardCount > 0 && Time.time > nextTime)
  32.         {
  33.             hazardCount--;
  34.             nextTime = Time.time + spawnWait;
  35.  
  36.             Vector3 spawnPosition = new Vector3(
  37.                 Random.Range(-spawnValues.x, spawnValues.x),
  38.                 spawnValues.y, spawnValues.z);
  39.  
  40.             Quaternion spawnRotation = Quaternion.identity;
  41.             Instantiate(hazard, spawnPosition, spawnRotation);
  42.         }
  43.         else if (hazardCount < 1)
  44.         {
  45.             doMyWork = EmptyDo;
  46.         }
  47.     }
  48.  
  49.     void EmptyDo()
  50.     {
  51.        
  52.     }
  53.  
  54.     // Update is called once per frame
  55.     void Update()
  56.     {
  57.         // делегат
  58.         doMyWork();
  59.     }
  60. }
  61.  
  62.  
  63. ///////////////////////
  64.  
  65.  
  66. /////////
  67. // место вот этого что предлагают в уроке - через Абстрактное АПИ
  68. // и хитрые конструкции....
  69. ///
  70.  
  71. void Start ()
  72. {
  73.     StartCoroutine (SpawnWaves ());
  74. }
  75.  
  76. IEnumerator SpawnWaves ()
  77. {
  78.     yield return new WaitForSeconds (startWait);
  79.     while (true)
  80.     {
  81.         for (int i = 0; i < hazardCount; i++)
  82.         {
  83.             Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
  84.             Quaternion spawnRotation = Quaternion.identity;
  85.             Instantiate (hazard, spawnPosition, spawnRotation);
  86.             yield return new WaitForSeconds (spawnWait);
  87.         }
  88.         yield return new WaitForSeconds (waveWait);
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement