SHARE
TWEET

bird

a guest Sep 22nd, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Parallaxer : MonoBehaviour
  6. {
  7.     class PoolObject
  8.     {
  9.         public Transform transform;
  10.         public bool inUse;
  11.         public PoolObject(Transform t) { transform = t; }
  12.         public void Dispose() { inUse = false; }
  13.     }
  14.  
  15.     [System.Serializable]
  16.     public struct YSpawnRange
  17.     {
  18.         public float min;
  19.         public float max;
  20.  
  21.     }
  22.  
  23.     public GameObject Prefab;
  24.     public int poolSize;
  25.     public float shiftSpeed;
  26.     public float spawnRate;
  27.  
  28.     public YSpawnRange ySpawnRange;
  29.     public Vector3 defaultSpawnPos;
  30.     public bool spawnImmediate;
  31.     public Vector3 immediateSpawnPos;
  32.     public Vector2 targetAspectRatio;
  33.  
  34.     float spawnTimer;
  35.     float targetAspect;
  36.     PoolObject[] poolObjects;
  37.     GameManager game;
  38.  
  39.     void Awake()
  40.     {
  41.         Configure();
  42.     }
  43.     void Start()
  44.     {
  45.         game = GameManager.Instance;
  46.     }
  47.  
  48.     void OnEnable()
  49.     {
  50.         GameManager.OnGameOverConfirmed += OnGameOverConfirmed;
  51.     }
  52.  
  53.     void OnDisable()
  54.     {
  55.         GameManager.OnGameOverConfirmed -= OnGameOverConfirmed;
  56.     }
  57.  
  58.     void OnGameOverConfirmed()
  59.     {
  60.         for (int i = 0; i < poolObjects.Length; i++)
  61.         {
  62.             poolObjects[i].Dispose();
  63.             poolObjects[i].transform.position = Vector3.one * 1000;
  64.         }
  65.         if (spawnImmediate)
  66.         {
  67.             SpawnImmediate();
  68.         }
  69.     }
  70.  
  71.     void Update()
  72.     {
  73.         if (game.GameOver) return;
  74.  
  75.         Shift();
  76.         spawnTimer += Time.deltaTime;
  77.         if (spawnTimer > spawnRate)
  78.         {
  79.             Spawn();
  80.             spawnTimer = 0;
  81.         }
  82.     }
  83.  
  84.     void Configure()
  85.     {
  86.         targetAspect = targetAspectRatio.x / targetAspectRatio.y;
  87.         poolObjects = new PoolObject[poolSize];
  88.         for (int i = 0; i < poolObjects.Length; i++)
  89.         {
  90.             GameObject go = Instantiate(Prefab) as GameObject;
  91.             Transform t = go.transform;
  92.             t.SetParent(transform);
  93.             t.position = Vector3.one * 1000;
  94.             poolObjects[i] = new PoolObject(t);
  95.         }
  96.         if (spawnImmediate)
  97.         {
  98.             SpawnImmediate();
  99.         }
  100.     }
  101.  
  102.     void Spawn()
  103.     {
  104.         Transform t = GetPoolObject();
  105.         if (t == null) return;
  106.         Vector3 pos = Vector3.zero;
  107.         pos.x = defaultSpawnPos.x;
  108.         pos.y = Random.Range(ySpawnRange.min, ySpawnRange.max);
  109.         t.position = pos;
  110.     }
  111.  
  112.     void SpawnImmediate()
  113.     {
  114.         Transform t = GetPoolObject();
  115.         if (t == null) return;
  116.         Vector3 pos = Vector3.zero;
  117.         pos.x = immediateSpawnPos.x;
  118.         pos.y = Random.Range(ySpawnRange.min, ySpawnRange.max);
  119.         t.position = pos;
  120.         Spawn();
  121.     }
  122.  
  123.     void Shift()
  124.     {
  125.         for (int i = 0; i < poolObjects.Length; i++)
  126.         {
  127.             poolObjects[i].transform.position += -Vector3.right * shiftSpeed * Time.deltaTime;
  128.             CheckDisposeObject(poolObjects[i]);
  129.         }
  130.     }
  131.  
  132.     void CheckDisposeObject(PoolObject poolObject)
  133.     {
  134.         if (poolObject.transform.position.x < -defaultSpawnPos.x)
  135.         {
  136.             poolObject.Dispose();
  137.             poolObject.transform.position = Vector3.one * 1000;
  138.         }
  139.     }
  140.  
  141.     Transform GetPoolObject()
  142.     {
  143.         for (int i = 0; i < poolObjects.Length; i++)
  144.         {
  145.             if (!poolObjects[i].inUse)
  146.             {
  147.                 poolObjects[i].Use();
  148.                 return poolObjects[i].transform;
  149.             }
  150.         }
  151.         return null;
  152.     }
  153. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top