bekovski

unity_pickupManager

Aug 12th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.88 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PickUpManager : MonoBehaviour {
  6.  
  7.     // singleton
  8.     public static PickUpManager instance;
  9.  
  10.     int maxNumPickUps = 5;
  11.  
  12.     float rate = 0.01f;
  13.     float maxTimeSpawned = 10f;
  14.     float maxTimeActive = 5f;
  15.  
  16.     class PickUp {
  17.         public GameObject pickUp;
  18.         public float timeLeftDespawn;
  19.         public float timeLeftActive;
  20.         public bool isCollected;
  21.         public bool isGreen;
  22.     }
  23.  
  24.     class GreenPickUp : PickUp {
  25.         // buffs
  26.         public bool isLaserBeam;
  27.         public bool isUmbrella;
  28.         public bool isDestroyedSpawnedMeteors;
  29.     }
  30.  
  31.     class RedPickUp : PickUp {
  32.         // debuffs
  33.         public bool isSlowPlayerDown;
  34.         public bool isIncreaseMeteorSpawnRate;
  35.         public bool isIncreaseMeteorSize;
  36.     }
  37.  
  38.     List<PickUp> spawnedPickUps = new List<PickUp>();
  39.     List<PickUp> collectedPickUps = new List<PickUp>();
  40.  
  41.     void Awake() {
  42.         instance = this;
  43.     }
  44.  
  45.     public void SpawnPickUp(List<GameObject> levelCubes) {
  46.         if (!(Random.value < rate) || spawnedPickUps.Count == maxNumPickUps - 1)
  47.             return;
  48.  
  49.  
  50.         // select random cube
  51.         GameObject cubePickUp = levelCubes[Random.Range(0, levelCubes.Count)];
  52.         GameObject pickUp = cubePickUp.transform.GetChild (Random.Range (0, 2)).gameObject;
  53.         // if it's already active, return
  54.         if (pickUp.activeSelf)
  55.             return;
  56.         else
  57.             pickUp.SetActive (true);
  58.  
  59.         // create PickUp struct
  60.         PickUp pu = new PickUp();
  61.         /*if(pickUp.CompareTag ("GreenPickUp")) {
  62.             pu = new GreenPickUp ();
  63.         }
  64.         else {
  65.             pu = new RedPickUp ();
  66.         }*/
  67.         pu.pickUp = pickUp;
  68.         pu.timeLeftDespawn = maxTimeSpawned;
  69.         pu.timeLeftActive = maxTimeActive;
  70.         pu.isGreen = pickUp.CompareTag ("GreenPickUp");
  71.  
  72.         spawnedPickUps.Add (pu);
  73.     } // SpawnPickUp()
  74.  
  75.     public void RemoveSpawnedPickUp(GameObject pickUp) {
  76.         for(int i=0; i < spawnedPickUps.Count; ++i) {
  77.             PickUp pu = spawnedPickUps [i];
  78.             spawnedPickUps.Remove (pu);
  79.         }
  80.     }
  81.  
  82.     public void CollectPickUp(GameObject pickUp) {
  83.         for(int i=0; i < spawnedPickUps.Count; ++i) {
  84.             PickUp pu = spawnedPickUps [i];
  85.             spawnedPickUps.Remove (pu);
  86.             collectedPickUps.Add (pu);
  87.         }
  88.     }
  89.  
  90.     public void UpdatePickUps(float deltaTime) {
  91.         UpdateSpawnedPickUps (deltaTime);
  92.         UpdateCollectedPickUps (deltaTime);
  93.     } // UpdatePickUps()
  94.  
  95.     public void UpdateSpawnedPickUps(float deltaTime) {
  96.         for(int i=0; i < spawnedPickUps.Count; ++i) {
  97.             PickUp pu = spawnedPickUps [i];
  98.  
  99.             pu.timeLeftDespawn -= deltaTime;
  100.  
  101.             // despawn if time is up
  102.             if(pu.timeLeftDespawn <= 0) {
  103.                 pu.pickUp.SetActive (false);
  104.                 spawnedPickUps.Remove (pu);
  105.             }
  106.         } // for()
  107.     } // UpdateSpawnedPickUps()
  108.  
  109.     public void UpdateCollectedPickUps(float deltaTime) {
  110.         for(int i=0; i < collectedPickUps.Count; ++i) {
  111.             PickUp pu = collectedPickUps [i];
  112.  
  113.             pu.timeLeftActive -= deltaTime;
  114.  
  115.             // deactivate if time is up
  116.             if(pu.timeLeftActive <= 0) {
  117.                 collectedPickUps.Remove (pu);
  118.  
  119.                 if(pu.isGreen) {
  120.                     pu.pickUp.GetComponent<PickUpBuffs> ().Deactivate ();
  121.                 }
  122.                 else {
  123.                     pu.pickUp.GetComponent<PickUpDebuffs> ().Deactivate ();
  124.                 }
  125.             }
  126.         } // for()
  127.     } // UpdateCollectedPickUps()
  128.  
  129.     public void ClearPickUpList() {
  130.         spawnedPickUps.Clear ();
  131.         collectedPickUps.Clear ();
  132.     }
  133.  
  134.  
  135.  
  136.  
  137.     // ========================
  138.     // buff/debuff methods including reverts
  139.     // ========================
  140.  
  141.     public void BuffLaserBeam() {
  142.        
  143.     }
  144.  
  145.     public void BuffUmbrella() {
  146.        
  147.     }
  148.  
  149.     public void BuffDestroyedSpawnedMeteors() {
  150.        
  151.     }
  152.        
  153.     public void DebuffSlowPlayerDown() {
  154.         float changeVal = PlayerController.instance.GetSpeed () / (-2f);
  155.         PlayerController.instance.ChangeSpeedBy (changeVal);
  156.     }
  157.  
  158.     public void DebuffSlowPlayerDownRevert() {
  159.         float changeVal = PlayerController.instance.GetSpeed () / (2f);
  160.         PlayerController.instance.ChangeSpeedBy (changeVal);
  161.     }
  162.  
  163.  
  164.     public void DebuffIncreaseMeteorSpawnRate() {
  165.        
  166.     }
  167.  
  168.     public void DebuffIncreaseMeteorSize() {
  169.        
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment