Advertisement
Placido_GDD

MVC CubeManager

Jan 30th, 2022 (edited)
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.30 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CubeManager : MonoBehaviour
  6. {
  7.    
  8.     public SpawnerData spawnerData;
  9.     public CubeDataContainer container;
  10.     public CubeData cubeData;
  11.     public CubeData tempCubeData;
  12.     public ScoreKeeper scoreKeeper;
  13.     public Player player;
  14.     public float spawnDelay;
  15.     private GameObject tempCube;
  16.     private bool gameOver;
  17.     public GameObject GameOverTxt;
  18.     // Start is called before the first frame update
  19.     void Start()
  20.     {        
  21.         spawnerData = GameObject.Find("CubeSpawnerData").GetComponent<SpawnerData>();
  22.         container = GameObject.Find("CubeDataHolder").GetComponent<CubeDataContainer>();
  23.         scoreKeeper = GameObject.Find("ScoreKeeper").GetComponent<ScoreKeeper>();
  24.         player = GameObject.Find("PlayerControl").GetComponent<Player>();
  25.         InvokeRepeating("SpawnCubes", 0.0f, spawnDelay);
  26.         InvokeRepeating("ActivateCubes", 0.0f, spawnDelay + 2.0f);
  27.         gameOver = false;
  28.     }
  29.     // Update is called once per frame
  30.     void Update()
  31.     {
  32.         if (gameOver == false)
  33.         {
  34.           CubeReactions();
  35.           GameOver();
  36.           ActivateCubes();
  37.         }
  38.        
  39.     }
  40.  
  41.     void GameOver()
  42.     {
  43.         if (spawnerData.activeCubeAmt >= spawnerData.maxActiveCubeAmt)
  44.         {
  45.             gameOver = true;
  46.             player.playerGameOver = true;
  47.             GameOverTxt.SetActive(true);
  48.             scoreKeeper.endScoreTxt.text = scoreKeeper.scoreTxt.text;
  49.         }
  50.  
  51.     }
  52.  
  53.     void SpawnCubes()
  54.     {
  55.         if ((spawnerData.CubeAmt < (spawnerData.MaxCubeAmt + 1) || spawnerData.activeCubeAmt < (spawnerData.maxActiveCubeAmt + 1)) && gameOver != true)
  56.         {
  57.             for (int i = 0; i < spawnerData.cubesPerRow - 1; i++)
  58.             {
  59.                 if (spawnerData.cubesInCol[i] < spawnerData.maxCubesInCol)
  60.                 {
  61.                     tempCube = Instantiate(spawnerData.Cube, spawnerData.spawnPoints[i] + new Vector2(0, 1.0f), Quaternion.identity);
  62.                     container.CubeList.Add(tempCube);
  63.                     cubeData = tempCube.GetComponent<CubeData>();
  64.                     cubeData.spawnerID = i;
  65.                     spawnerData.activeCubeAmt++;
  66.                     spawnerData.CubeAmt++;
  67.                     spawnerData.cubesInCol[i]++;
  68.                 }
  69.                
  70.             }
  71.         }        
  72.     }
  73.  
  74.     void ActivateCubes()
  75.     {
  76.         if (!gameOver)
  77.         {
  78.             //Debug.Log("Activating inactive cubes");
  79.             foreach (GameObject cube in container.CubeList)
  80.             {
  81.                 if (cube.activeSelf == false)
  82.                 {                    
  83.                     cube.SetActive(true);
  84.                     tempCubeData = cube.GetComponent<CubeData>();
  85.                     cube.transform.position = spawnerData.spawnPoints[tempCubeData.spawnerID] + new Vector2(0.0f, 3.0f);
  86.                     cubeData.SetSprite();
  87.                     cubeData.CleanBoxList();
  88.                     cubeData.isMoving = true;
  89.                     spawnerData.activeCubeAmt++;
  90.                 }
  91.             }
  92.  
  93.         }
  94.        
  95.     }
  96.     void CubeReactions()
  97.     {
  98.         foreach (GameObject cube in container.CubeList)
  99.         {
  100.             if (cube.activeSelf == true) //code only runs if the cube is active.
  101.             {
  102.                 //Use BoxList Index 0-1 for Red cubes
  103.                 //0 for left 1 for right
  104.                 //Red cubes only connect sideways
  105.                 if (cube.gameObject.tag == "Red" && cube.GetComponent<CubeData>().isMoving == false)
  106.                 {
  107.                     cubeData = cube.GetComponent<CubeData> ();
  108.                     if (cubeData.BoxList[0] != null && cubeData.BoxList[1] != null)
  109.                     {
  110.                        
  111.                         tempCubeData = cubeData.BoxList[0].GetComponent<CubeData>();
  112.                         tempCubeData.CleanBoxList ();
  113.                         tempCubeData.isMoving = true;
  114.                         cubeData.BoxList[0].SetActive(false);                      
  115.                         spawnerData.activeCubeAmt--;
  116.                         //
  117.                         tempCubeData = cubeData.BoxList[1].GetComponent<CubeData>();
  118.                         tempCubeData.CleanBoxList();
  119.                         tempCubeData.isMoving = true;
  120.                         cubeData.BoxList[1].SetActive(false);
  121.                         spawnerData.activeCubeAmt--;
  122.                         //
  123.                         tempCubeData = cube.GetComponent <CubeData>();
  124.                         tempCubeData.isMoving = true;
  125.                         spawnerData.activeCubeAmt--;
  126.                         scoreKeeper.score++;
  127.                         cube.SetActive(false);
  128.                     }
  129.                 }
  130.                 //Use BoxList Index 2-3 for Blue cubes
  131.                 //2 for up 3 for down
  132.                 //Blue cubes only connect vertically
  133.                 else if (cube.gameObject.tag == "Blue" && cube.GetComponent<CubeData>().isMoving == false)
  134.                 {
  135.                     cubeData = cube.GetComponent<CubeData>();
  136.                     if (cubeData.BoxList[2] != null && cubeData.BoxList[3] != null)
  137.                     {
  138.                        
  139.                         tempCubeData = cubeData.BoxList[2].GetComponent<CubeData>();
  140.                         tempCubeData.CleanBoxList();
  141.                         cubeData.BoxList[2].SetActive(false);
  142.                         spawnerData.activeCubeAmt--;
  143.                         scoreKeeper.score++;
  144.                         //
  145.                         tempCubeData = cubeData.BoxList[3].GetComponent<CubeData>();
  146.                         tempCubeData.CleanBoxList();
  147.                         cubeData.BoxList[3].SetActive(false);
  148.                         spawnerData.activeCubeAmt--;
  149.                         scoreKeeper.score++;
  150.                         //
  151.                         tempCubeData = cube.GetComponent<CubeData> ();
  152.                         tempCubeData.isMoving = true;
  153.                         spawnerData.activeCubeAmt--;
  154.                         scoreKeeper.score++;
  155.                         cube.SetActive(false);
  156.                        
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.     }
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement