alexandrheathen

Untitled

Dec 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PipeSpawner : MonoBehaviour {
  6.  
  7.     [System.Serializable]
  8.     public struct SpawnHeight {
  9.         public float min;
  10.         public float max;
  11.     }
  12.  
  13.     public GameObject PipePrefab;
  14.     public float shiftSpeed;
  15.     public float spawnRate;
  16.     public SpawnHeight spawnHeight;
  17.     public Vector3 spawnPos;
  18.     public Vector2 targetAspectRatio;
  19.     public bool beginInScreenCenter;
  20.     List<Transform> pipes;
  21.     float spawnTimer;
  22.     GameManager game;
  23.     float targetAspect;
  24.     Vector3 dynamicSpawnPos;
  25.  
  26.     void Start() {
  27.         pipes = new List<Transform>();
  28.         game = GameManager.Instance;
  29.         if (beginInScreenCenter)
  30.             SpawnPipe();
  31.     }
  32.  
  33.     void OnEnable() {
  34.         GameManager.OnGameOverConfirmed += OnGameOverConfirmed;
  35.     }
  36.  
  37.     void OnDisable() {
  38.         GameManager.OnGameOverConfirmed -= OnGameOverConfirmed;
  39.     }
  40.  
  41.     void OnGameOverConfirmed() {
  42.         for (int i = pipes.Count - 1; i >= 0; i--) {
  43.             GameObject temp = pipes[i].gameObject;
  44.             pipes.RemoveAt(i);
  45.             Destroy(temp);
  46.         }
  47.         if (beginInScreenCenter) SpawnPipe();
  48.     }
  49.  
  50.     void Update() {
  51.         if (game.GameOver) return;
  52.         targetAspect = (float)targetAspectRatio.x / targetAspectRatio.y;
  53.         dynamicSpawnPos.x = (spawnPos.x * Camera.main.aspect) / targetAspect;
  54.         spawnTimer += Time.deltaTime;
  55.         if (spawnTimer >= spawnRate) {
  56.             SpawnPipe();
  57.             spawnTimer = 0;
  58.         }
  59.  
  60.         ShiftPipes();
  61.     }
  62.  
  63.     void SpawnPipe() {
  64.         GameObject pipe = Instantiate(PipePrefab) as GameObject;
  65.         pipe.transform.SetParent(transform);
  66.         pipe.transform.localPosition = dynamicSpawnPos;
  67.         if (beginInScreenCenter && pipes.Count == 0) pipe.transform.localPosition = Vector3.zero;
  68.         float randomYPos = Random.Range(spawnHeight.min, spawnHeight.max);
  69.         pipe.transform.position += Vector3.up * randomYPos;
  70.         pipes.Add(pipe.transform);
  71.     }
  72.  
  73.     void ShiftPipes() {
  74.         for (int i = pipes.Count - 1; i >= 0; i--) {
  75.             pipes[i].position -= Vector3.right * shiftSpeed * Time.deltaTime;
  76.             if (pipes[i].position.x < (-dynamicSpawnPos.x * Camera.main.aspect) / targetAspect) {
  77.                 GameObject temp = pipes[i].gameObject;
  78.                 pipes.RemoveAt(i);
  79.                 Destroy(temp);
  80.             }
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment