GaelVanhalst

Armadilla: Level Generation

Jan 25th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 29.87 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Random = UnityEngine.Random;
  7.  
  8. public class PieceBasedLevelGeneration : MonoBehaviour {
  9.  
  10.     //Other scripts
  11.     public LevelManager LevelManagerScript = null;
  12.     public EnemySpawn EnemySpawnScript = null;
  13.     public Player PlayerScript = null;
  14.     public SecondLane SecondLaneScript = null;
  15.  
  16.     //Source objects grounds
  17.     public List<GameObject>RiverStarts=new List<GameObject>();
  18.     public List<GameObject> BaseGrounds = null;
  19.     public GameObject BasicWater = null;
  20.     public GameObject StartWater = null;
  21.     public GameObject EndWater = null;
  22.     public GameObject PureBaseGround = null;
  23.  
  24.     //Source objects index+pools
  25.     public MultiPool PieceMultipool = null;
  26.     public int BasicWaterIndex = 0;
  27.     public int StartWaterIndex = 0;
  28.     public int EndWaterIndex = 0;
  29.     public int PureBaseGroundIndex = 0;
  30.     public List<int> SetPieceBaseGroundIndexes = new List<int>();
  31.     public List<int> SetPieceRiverIndexes = new List<int>();
  32.     public List<LevelPieceInformation> LevelPieceInfoPerPiece = new List<LevelPieceInformation>();
  33.  
  34.     //Source cliffs
  35.     public List<GameObject> BottomCliffs = null;
  36.     public GameObject BasicWaterCliffBottom = null;
  37.     public List<GameObject>Cliffs =new List<GameObject>();
  38.     public List<GameObject> WaterCliffs = new List<GameObject>();
  39.  
  40.     //Source cliffs index+pools
  41.     public MultiPool CliffMultiPool = null;
  42.     public List<int> BottomCliffsIndexes = new List<int>();
  43.     public int BasicWaterCliffIndex = 0;
  44.     public List<int> CliffsIndexes = new List<int>();
  45.     public List<int> WaterCliffsIndexes = new List<int>();
  46.     public List<CliffPieceInformation> CliffInfoPerPiece = new List<CliffPieceInformation>();
  47.  
  48.     //Bottom + Decoration
  49.     public Pool BottomPiece = null;
  50.     public Pool SideScene = null;
  51.    
  52.     //Cliff positions + bottom positions
  53.     public Vector3 LeftCliffPos=new Vector3(7.5f,0,0);
  54.     public Vector3 RightCliffPos = new Vector3(-7.25f, -8.0f, 0);
  55.     public Vector3 BottomPartPos=new Vector3(-17.5f,-8,0);
  56.  
  57.     //Obstacles and bonus
  58.     public Pool WaterSheep = null;
  59.     public Pool Billboard = null;
  60.     public Pool Ramp = null;
  61.     public Pool Rail = null;
  62.     public Pool Hole = null;
  63.     public Pool Cannon = null;
  64.     public Pool BossLaunchPad = null;
  65.    
  66.     //Internal objects
  67.     private Queue<GameObject> _rows=new Queue<GameObject>();
  68.     private Queue<GameObject> _bottomParts = new Queue<GameObject>();
  69.     private Queue<GameObject> _sideScenery=new Queue<GameObject>();
  70.     private GameObject _currentPiece = null;
  71.     private GameObject _currentCliff = null;
  72.  
  73.     //Generation objects
  74.     private GameObject _generationNeedle = null;
  75.     private GameObject _generationNeedleSideScene = null;
  76.  
  77.     //Sizes and speed
  78.     public float LevelStart = 20;
  79.     public float LevelEnd = -100;
  80.  
  81.     public const float PieceLength = 10.0f;
  82.     public const float PieceWidth = 10.0f;
  83.     public const float SideLength = 60.0f;
  84.  
  85.     //Extra numbers
  86.     public float BillboardDistanceFromRiver = 25.0f;
  87.  
  88.     //Wait rows
  89.     private float _riverWaitRows = 0;
  90.     private float _rampWaitRows = 0;
  91.     private float _railWaitRows = 0;
  92.     private float _cannonWaitRows = 0;
  93.     private float _seaWaitRows = 0;
  94.     private float _holeWaitRows = 0;
  95.  
  96.     //Special Pieces Of land
  97.     private bool _firstSpecialRow = false;
  98.     private bool _specialLand = false;
  99.     private SpecialLands _specialLandKind;
  100.     private float _sizeLeftSpecialLand = 50;
  101.     private float _nextEnemyInSpecialLand = 0;
  102.     private const float HowManySpaceForOneEnemy = 3;
  103.  
  104.     //Spawning grazing sheep
  105.     private int _amountOfRowsToSpawnGrazingSheep = 0;
  106.     private float _nextGrazingSheep = 0;
  107.     private const float HowManySpaceForGrazingSheep = 4.5f;
  108.     private const float GrazingSheepSpawnHeight = 20.0f;
  109.     private const int AmountOfGrazeRowsAfterSea = 8;
  110.     private bool _shouldWaitBlockBeforeSpawningGrazeSheep = false;
  111.  
  112.     //Clouds
  113.     public GameObject CloudsLayer = null;
  114.  
  115.     public static float DistancePassed { get; private set; }
  116.  
  117.  
  118.     void Start () {
  119.         LevelManager.DeactivateSpecialPropsSpawning = GameManager.BossRunMode;
  120.  
  121.         _rows=new Queue<GameObject>();
  122.         _generationNeedle = new GameObject("GenerationNeedle");
  123.         _generationNeedleSideScene=new GameObject("GenerationSideNeedle");
  124.         _generationNeedleSideScene.transform.position=_generationNeedle.transform.position = new Vector3(0, 0, LevelEnd);
  125.  
  126.         CheckAllInputFiles();
  127.         GenerateThePoolIndexes();
  128.  
  129.         SyncAllWaitRows();
  130.  
  131.         _currentPiece = PlaceBasicPiece();
  132.         _rows.Enqueue(_currentPiece);
  133.         DestroyAndRegenerateLevel(true);
  134.  
  135.  
  136.         StayInBounds.LeftWall =  - PieceWidth * 0.5f;
  137.         StayInBounds.RightWall = PieceWidth * 0.5f;
  138.  
  139.         DistancePassed = 0;
  140.     }
  141.  
  142.     private void GenerateThePoolIndexes()
  143.     {
  144.         BasicWaterCliffIndex = CliffMultiPool.GetIndex(BasicWaterCliffBottom);
  145.         BottomCliffsIndexes=new List<int>();
  146.         foreach (var cliff in BottomCliffs)
  147.         {
  148.             BottomCliffsIndexes.Add(CliffMultiPool.GetIndex(cliff));
  149.         }
  150.         CliffsIndexes = new List<int>();
  151.         foreach (var cliff in Cliffs)
  152.         {
  153.             CliffsIndexes.Add(CliffMultiPool.GetIndex(cliff));
  154.         }
  155.  
  156.         WaterCliffsIndexes = new List<int>();
  157.         foreach (var cliff in WaterCliffs)
  158.         {
  159.             WaterCliffsIndexes.Add(CliffMultiPool.GetIndex(cliff));
  160.         }
  161.  
  162.         CliffInfoPerPiece = new List<CliffPieceInformation>();
  163.         foreach (var cliff in CliffMultiPool.PoolItems)
  164.         {
  165.             var cliffScript = cliff.PoolObject.GetComponent<CliffPieces>();
  166.             if (cliffScript != null)
  167.             {
  168.                 CliffInfoPerPiece.Add(new CliffPieceInformation(cliffScript, CliffMultiPool));
  169.             }
  170.         }
  171.  
  172.         BasicWaterIndex = PieceMultipool.GetIndex(BasicWater);
  173.         StartWaterIndex = PieceMultipool.GetIndex(StartWater);
  174.         EndWaterIndex = PieceMultipool.GetIndex(EndWater);
  175.         PureBaseGroundIndex = PieceMultipool.GetIndex(PureBaseGround);
  176.         SetPieceBaseGroundIndexes = new List<int>();
  177.         foreach (var ground in BaseGrounds)
  178.         {
  179.             SetPieceBaseGroundIndexes.Add(PieceMultipool.GetIndex(ground));
  180.         }
  181.  
  182.         SetPieceRiverIndexes = new List<int>();
  183.  
  184.         foreach (var ground in RiverStarts)
  185.         {
  186.             SetPieceRiverIndexes.Add(PieceMultipool.GetIndex(ground));
  187.         }
  188.  
  189.         LevelPieceInfoPerPiece=new List<LevelPieceInformation>();
  190.         foreach (var ground in PieceMultipool.PoolItems)
  191.         {
  192.             var groundScript = ground.PoolObject.GetComponent<LevelPieces>();
  193.             if (groundScript != null)
  194.             {
  195.                 LevelPieceInfoPerPiece.Add(new LevelPieceInformation(groundScript, PieceMultipool));
  196.             }
  197.         }
  198.     }
  199.  
  200.     //Check if all prefabs are valid
  201.     void CheckAllInputFiles()
  202.     {
  203.         CheckAllGrounds();
  204.         CheckAllRiverStarts();
  205.         if (_generationNeedle == null)
  206.         {
  207.             Debug.LogError("GenerationNeedle is empty");
  208.             Destroy(this);
  209.         }
  210.         if (BasicWater == null)
  211.         {
  212.             Debug.LogError("Basic water is empty");
  213.             Destroy(this);
  214.         }
  215.         if (EndWater == null)
  216.         {
  217.             Debug.LogError("End water is empty");
  218.             Destroy(this);
  219.         }
  220.         if (StartWater == null)
  221.         {
  222.             Debug.LogError("Start water is empty");
  223.             Destroy(this);
  224.         }
  225.         if (PureBaseGround == null)
  226.         {
  227.             Debug.LogError("Purebase ground is empty");
  228.             Destroy(this);
  229.         }
  230.         if (WaterSheep == null)
  231.         {
  232.             Debug.LogError("WaterSheep is empty");
  233.             Destroy(this);
  234.         }
  235.         if (Billboard == null)
  236.         {
  237.             Debug.LogError("Billboard is empty");
  238.             Destroy(this);
  239.         }
  240.         if (Ramp == null)
  241.         {
  242.             Debug.LogError("Ramp is empty");
  243.             Destroy(this);
  244.         }
  245.         if (Rail == null)
  246.         {
  247.             Debug.LogError("Rail is empty");
  248.             Destroy(this);
  249.         }
  250.         if (Cannon == null)
  251.         {
  252.             Debug.LogError("Cannon is empty");
  253.             Destroy(this);
  254.         }
  255.         if (Hole == null)
  256.         {
  257.             Debug.LogError("Hole is empty");
  258.             Destroy(this);
  259.         }
  260.         if (BossLaunchPad == null)
  261.         {
  262.             Debug.LogError("BossLaunchPad is empty");
  263.             Destroy(this);
  264.         }
  265.         if (BottomCliffs.Count <= 0)
  266.         {
  267.             Debug.LogError("Cliff is empty");
  268.             Destroy(this);
  269.         }
  270.         if (BasicWaterCliffBottom == null)
  271.         {
  272.             Debug.LogError("WaterCliff is empty");
  273.             Destroy(this);
  274.         }
  275.  
  276.         if (Cliffs.Count <= 0)
  277.         {
  278.             Debug.LogError("Cliffs is empty");
  279.             Destroy(this);
  280.         }
  281.  
  282.         if (WaterCliffs.Count <= 0)
  283.         {
  284.             Debug.LogError("WaterCliffs is empty");
  285.             Destroy(this);
  286.         }
  287.         if (BottomPiece == null)
  288.         {
  289.             Debug.LogError("BottomPiece is missing");
  290.             Destroy(this);
  291.         }
  292.         if (EnemySpawnScript == null)
  293.         {
  294.             Debug.LogError("EnemySpawnScript is missing");
  295.             Destroy(this);
  296.         }
  297.  
  298.         if (CliffMultiPool == null)
  299.         {
  300.             Debug.LogError("MultiPool is missing");
  301.             Destroy(this);
  302.         }
  303.     }
  304.  
  305.     void CheckAllRiverStarts()
  306.     {
  307.         RiverStarts.RemoveAll(item => item == null);
  308.  
  309.         if (RiverStarts.Count <= 0)
  310.         {
  311.             Debug.LogError("RiverStarts are empty");
  312.             Destroy(this);
  313.         }
  314.     }
  315.  
  316.     void CheckAllGrounds()
  317.     {
  318.         BaseGrounds.RemoveAll(item => item == null);
  319.         if (BaseGrounds.Count <= 0)
  320.         {
  321.             Debug.LogError("BasicGrounds are empty");
  322.             Destroy(this);
  323.         }
  324.     }
  325.  
  326.     //Set all the rows for spawning props
  327.     void SyncAllWaitRows()
  328.     {
  329.         _riverWaitRows = LevelManagerScript.HowLongBeforeFirstRiver;
  330.         _rampWaitRows = LevelManagerScript.RampDistance;
  331.         _railWaitRows = LevelManagerScript.RailDistance;
  332.         _cannonWaitRows = LevelManagerScript.CannonDistance;
  333.         _seaWaitRows = LevelManagerScript.SeaDistance;
  334.         _holeWaitRows = LevelManagerScript.HoleDistance;
  335.     }
  336.  
  337.     private void Update()
  338.     {
  339.         MoveLevel();
  340.         DestroyAndRegenerateLevel();
  341.     }
  342.  
  343.     //Lower the waiting for props
  344.     void LowerAllWaitRows(float amount)
  345.     {
  346.         if (_riverWaitRows > 0) _riverWaitRows-=amount;
  347.         if (_rampWaitRows > 0) _rampWaitRows -= amount;
  348.         if (_cannonWaitRows > 0) _cannonWaitRows -= amount;
  349.         if (_holeWaitRows > 0) _holeWaitRows -= amount;
  350.         if (_seaWaitRows > 0) _seaWaitRows -= amount;
  351.         if (_railWaitRows > 0) _railWaitRows -= amount;
  352.  
  353.         CheckForSpecialLands();
  354.     }
  355.  
  356.     //Check if a special kind of land needs to be placed
  357.     void CheckForSpecialLands()
  358.     {
  359.         if (_seaWaitRows <= 0)
  360.         {
  361.             _specialLandKind = SpecialLands.Sea;
  362.             _specialLand = true;
  363.             _firstSpecialRow = true;
  364.             _sizeLeftSpecialLand = LevelManagerScript.SeaLenght;
  365.             _nextEnemyInSpecialLand = WaterSheepScript.Radius;
  366.             _seaWaitRows = LevelManagerScript.SeaDistance;
  367.         }
  368.     }
  369.  
  370.     //Give a piece out of the basic list. Give default flat piece if needed
  371.     GameObject PlaceBasicPiece(bool normalLands=false)
  372.     {
  373.         if (normalLands) return PlaceGivenPiece(PureBaseGroundIndex);
  374.         var chance = SetPieceBaseGroundIndexes.Sum(ground => PieceMultipool.GetPrefab(ground).GetComponent<LevelPieces>().SpawnChance);
  375.         int chosenPiece=0;
  376.         var chosenNumber = Random.Range(0, chance);
  377.         foreach (var ground in SetPieceBaseGroundIndexes)
  378.         {
  379.             chosenNumber -= PieceMultipool.GetPrefab(ground).GetComponent<LevelPieces>().SpawnChance;
  380.             if (chosenNumber >= 0) continue;
  381.             chosenPiece = ground;
  382.             break;
  383.         }
  384.  
  385.         return PlaceGivenPiece(chosenPiece);
  386.     }
  387.  
  388.     //Place a river piece that connects
  389.     GameObject PlaceRiverPiece()
  390.     {
  391.         var chance = SetPieceRiverIndexes.Sum(ground => PieceMultipool.GetPrefab(ground).GetComponent<LevelPieces>().SpawnChance);
  392.         int chosenPiece=0;
  393.         var chosenNumber = Random.Range(0, chance);
  394.         foreach (var ground in SetPieceRiverIndexes)
  395.         {
  396.             chosenNumber -=  PieceMultipool.GetPrefab(ground).GetComponent<LevelPieces>().SpawnChance;
  397.             if (chosenNumber >= 0) continue;
  398.             chosenPiece = ground;
  399.             break;
  400.         }
  401.  
  402.         var riverPiece=PlaceGivenPiece(chosenPiece);
  403.  
  404.         PlaceWaterBillBoard(riverPiece);
  405.         return riverPiece;
  406.     }
  407.  
  408.     //Place the chosen piece
  409.     GameObject PlaceGivenPiece(int index)
  410.     {
  411.         var position = _generationNeedle.transform.position;
  412.         position.z += PieceLength * 0.5f;
  413.  
  414.         var piece = PieceMultipool.MakePoolItem(position, Quaternion.identity, index);
  415.         PlaceCliffs(position,piece);
  416.         PlaceBottomPart(position);
  417.  
  418.         _generationNeedle.transform.Translate(0, 0, PieceLength);
  419.         var script = piece.GetComponent<LevelPieces>();
  420.         if (script != null)
  421.         {
  422.             script.IndexInPool = index;
  423.         }
  424.  
  425.         return piece;
  426.     }
  427.  
  428.     //Add a cliff to the side, based on previous cliffs and if there is a connection with a river
  429.     void PlaceCliffs(Vector3 position,GameObject parent)
  430.     {
  431.         var requirements = parent.GetComponent<LevelPieces>().ExtraRequirements;
  432.  
  433.         var pos = LeftCliffPos;
  434.         pos.z = position.z;
  435.  
  436.         GameObject piece = null;
  437.  
  438.         int indexCliff = -1;
  439.  
  440.         CliffPieces currentPieceCliffCode = null;
  441.         if (_currentCliff != null)
  442.         {
  443.             currentPieceCliffCode = _currentCliff.GetComponent<CliffPieces>();
  444.         }
  445.        
  446.         if (requirements.WaterFallUp)
  447.         {
  448.             if (currentPieceCliffCode != null)
  449.             {
  450.  
  451.                 indexCliff = CliffInfoPerPiece[currentPieceCliffCode.IndexInPool].GetNextPiece(true, CliffMultiPool);
  452.             }
  453.             if (indexCliff < 0)
  454.             {
  455.                 indexCliff = CliffPieces.GiveCliffPiece(WaterCliffsIndexes,CliffMultiPool);
  456.             }
  457.             piece=CliffMultiPool.MakePoolItem(pos, Quaternion.identity, indexCliff);
  458.         }
  459.         else
  460.         {
  461.             if (currentPieceCliffCode != null)
  462.             {
  463.                 indexCliff = CliffInfoPerPiece[currentPieceCliffCode.IndexInPool].GetNextPiece(false, CliffMultiPool);
  464.             }
  465.             if (indexCliff < 0)
  466.             {
  467.                 indexCliff = CliffPieces.GiveCliffPiece(CliffsIndexes,CliffMultiPool);
  468.             }
  469.             piece = CliffMultiPool.MakePoolItem(pos, Quaternion.identity, indexCliff);
  470.         }
  471.  
  472.         if (piece != null)
  473.         {
  474.             _currentCliff = piece;
  475.             piece.transform.parent = parent.transform;
  476.             var scale = piece.transform.localScale;
  477.             scale.x = 1;
  478.             piece.transform.localScale = scale;
  479.             var script = piece.GetComponent<CliffPieces>();
  480.             if (script != null)
  481.             {
  482.                 script.IndexInPool = indexCliff;
  483.             }
  484.         }
  485.         else
  486.         {
  487.             Debug.LogError("Faulty cliff");
  488.         }
  489.  
  490.  
  491.         pos = RightCliffPos;
  492.         pos.z = position.z;
  493.  
  494.         if (requirements.WateFallDown)
  495.             piece = CliffMultiPool.MakePoolItem(pos, Quaternion.identity, BasicWaterCliffIndex);
  496.         else
  497.         {
  498.             indexCliff = CliffPieces.GiveCliffPiece(BottomCliffsIndexes, CliffMultiPool);
  499.             piece = CliffMultiPool.MakePoolItem(pos, Quaternion.identity, indexCliff);
  500.         }
  501.  
  502.         if (piece != null)
  503.         {
  504.             piece.transform.parent = parent.transform;
  505.             var scale = piece.transform.localScale;
  506.             scale.x = 1;
  507.             piece.transform.localScale = scale;
  508.             var script = piece.GetComponent<CliffPieces>();
  509.             if (script != null)
  510.             {
  511.                 script.IndexInPool = indexCliff;
  512.             }
  513.         }
  514.         else
  515.         {
  516.             Debug.LogError("Faulty cliff");
  517.  
  518.         }
  519.     }
  520.  
  521.     //Place land and river at the bottom of the cliffs
  522.     void PlaceBottomPart(Vector3 position)
  523.     {
  524.         var pos = BottomPartPos;
  525.         pos.z = position.z;
  526.         var piece = BottomPiece.MakePoolItem(pos, Quaternion.identity);
  527.         if (piece != null) _bottomParts.Enqueue(piece);
  528.     }
  529.  
  530.     //Place the water tiles
  531.     GameObject PlaceWaterLand()
  532.     {
  533.         if (_sizeLeftSpecialLand <= 0)
  534.         {
  535.             _specialLand = false;
  536.             _amountOfRowsToSpawnGrazingSheep = AmountOfGrazeRowsAfterSea;
  537.             _shouldWaitBlockBeforeSpawningGrazeSheep = true;
  538.         }
  539.  
  540.         GameObject waterpiece = null;
  541.         if (_firstSpecialRow)
  542.         {
  543.             waterpiece = PlaceGivenPiece(StartWaterIndex);
  544.             _sizeLeftSpecialLand -= PieceLength*0.5f;
  545.         }
  546.         else if (!_specialLand)
  547.         {
  548.             waterpiece = PlaceGivenPiece(EndWaterIndex);
  549.         }
  550.         else
  551.         {
  552.             waterpiece = PlaceGivenPiece(BasicWaterIndex);
  553.             PlaceWaterSheepInSea(waterpiece);
  554.             _sizeLeftSpecialLand -= PieceLength;
  555.         }
  556.        
  557.  
  558.         if (!_firstSpecialRow) return waterpiece;
  559.         PlaceWaterBillBoard(waterpiece);
  560.         _firstSpecialRow = false;
  561.  
  562.         return waterpiece;
  563.     }
  564.  
  565.     //Add swimming sheep to the water
  566.     void PlaceWaterSheepInSea(GameObject row)
  567.     {
  568.         var radiusWaterSheep = WaterSheepScript.Radius;
  569.         var posPiece = row.transform.position;
  570.         var startposZ = posPiece.z- 0.5f * PieceLength;
  571.  
  572.         var nextSpawnZ = _nextEnemyInSpecialLand;
  573.         const float borders = PieceWidth*0.35f;
  574.         while (nextSpawnZ + radiusWaterSheep <= PieceLength)
  575.         {
  576.             var pos = new Vector3(0, posPiece.y+0.35f, startposZ+nextSpawnZ) { x = Random.Range(-borders, borders) };
  577.  
  578.  
  579.             var waterSheep = WaterSheep.MakePoolItem(pos, WaterSheep.transform.rotation);
  580.             if (waterSheep != null)
  581.             {
  582.                 waterSheep.transform.parent = row.transform;
  583.                 var script = waterSheep.GetComponent<WaterSheepScript>();
  584.                 EnemySpawnScript.Enemies.Add(script);
  585.                 script.SpawnScript = EnemySpawnScript;
  586.             }
  587.             nextSpawnZ += HowManySpaceForOneEnemy;
  588.         }
  589.         _nextEnemyInSpecialLand = nextSpawnZ - PieceLength;
  590.     }
  591.  
  592.     //Choose and spawn the next piece of land
  593.     GameObject SpawnPiece(bool normalLands)
  594.     {
  595.         int index = -1;
  596.         LevelPieces currentPieceCode = null;
  597.         if (_currentPiece != null)
  598.         {
  599.             currentPieceCode=_currentPiece.GetComponent<LevelPieces>();
  600.         }
  601.  
  602.         if (currentPieceCode != null)
  603.         {
  604.             index=LevelPieceInfoPerPiece[currentPieceCode.IndexInPool].GetNextPiece(PieceMultipool);
  605.         }
  606.  
  607.         if (index < 0)
  608.         {
  609.             if (_specialLand && !normalLands)
  610.             {
  611.                 switch (_specialLandKind)
  612.                 {
  613.                     case SpecialLands.Sea:
  614.                         return PlaceWaterLand();
  615.                     default:
  616.                         throw new ArgumentOutOfRangeException();
  617.                 }
  618.             }
  619.  
  620.             LowerAllWaitRows(PieceLength);
  621.  
  622.             if ((_riverWaitRows > 0) || normalLands) return PlaceBasicPiece(normalLands);
  623.             _riverWaitRows = LevelManagerScript.RiverDistance;
  624.             return PlaceRiverPiece();
  625.         }
  626.         else
  627.         {
  628.             return PlaceGivenPiece(index);
  629.         }
  630.     }
  631.  
  632.     //Place the billboard to show that there will be water soon
  633.     void PlaceWaterBillBoard(GameObject row)
  634.     {
  635.         var position = new Vector3(-6, 0, -BillboardDistanceFromRiver+row.transform.position.z);
  636.         Billboard.MakePoolItem(position, Quaternion.identity);
  637.     }
  638.  
  639.     //Movement of the level
  640.     void MoveLevel()
  641.     {
  642.         DistancePassed += GameManager.LevelMoveSpeedStatic * Time.deltaTime;
  643.     }
  644.  
  645.     //Destroy land that is out of reach and build new coming land
  646.     void DestroyAndRegenerateLevel(bool normalGrounds=false)
  647.     {
  648.         while (_generationNeedle.transform.position.z < LevelStart + DistancePassed + PieceLength * 0.5)
  649.         {
  650.             _currentPiece = SpawnPiece(normalGrounds);
  651.             if (!CheckIfShouldSpawnGrazingSheep(_currentPiece)&&!GameManager.BossRunMode&&!normalGrounds)
  652.             {
  653.                 CheckIfCanAddAnyExtras(_currentPiece);
  654.             }
  655.  
  656.             _rows.Enqueue(_currentPiece);
  657.         }
  658.  
  659.         GameObject piece = null;
  660.         if (_rows.Count > 0)
  661.         {
  662.             piece = _rows.Peek();
  663.             if (piece != null)
  664.             {
  665.                 while (piece.transform.position.z - 0.5f*PieceLength < LevelEnd + DistancePassed)
  666.                 {
  667.                     var row = _rows.Dequeue();
  668.                     var poolableItems = row.GetComponentsInChildren<PoolableItemMultiPool>().ToList();
  669.                     var piecePool = row.GetComponent<PoolableItemMultiPool>();
  670.                     poolableItems.Remove(piecePool);
  671.                     foreach (var poolItem in poolableItems)
  672.                     {
  673.                         if (!poolItem.PutInPool())
  674.                             Destroy(poolItem.gameObject);
  675.                     }
  676.  
  677.                     if (piecePool != null)
  678.                     {
  679.                         if (!piecePool.PutInPool())
  680.                         {
  681.                             Destroy(piecePool.gameObject);
  682.                         }
  683.  
  684.                     }
  685.                     else Destroy(row);
  686.                     if (_rows.Count <= 0) break;
  687.                     piece = _rows.Peek();
  688.                     if (piece == null) break;
  689.                 }
  690.             }
  691.             else _rows.Dequeue();
  692.         }
  693.  
  694.         if (_bottomParts.Count > 0)
  695.         {
  696.             piece = _bottomParts.Peek();
  697.             if (piece != null)
  698.             {
  699.                 while (piece.transform.position.z - 0.5f*PieceLength < LevelEnd + DistancePassed)
  700.                 {
  701.                     var row = _bottomParts.Dequeue();
  702.                     var piecePool = row.GetComponent<PoolableItem>();
  703.                     if (piecePool != null)
  704.                     {
  705.                         if (!piecePool.PutInPool())
  706.                         {
  707.                             Destroy(piecePool.gameObject);
  708.                         }
  709.  
  710.                     }
  711.                     else Destroy(row);
  712.                     if (_bottomParts.Count <= 0) return;
  713.                     piece = _bottomParts.Peek();
  714.                     if (piece == null) break;
  715.                 }
  716.             }
  717.             else _bottomParts.Dequeue();
  718.         }
  719.  
  720.  
  721.         while (_generationNeedleSideScene.transform.position.z < LevelStart + DistancePassed + SideLength * 0.5)
  722.         {
  723.             var pos = _generationNeedleSideScene.transform.position;
  724.             pos.y = 0;
  725.             pos.x = 30;
  726.            var sidePiece = SideScene.MakePoolItem(pos, Quaternion.identity);
  727.             _sideScenery.Enqueue(sidePiece);
  728.  
  729.             pos.x = -65;
  730.             var sidePiece2 = SideScene.MakePoolItem(pos, Quaternion.identity);
  731.             _sideScenery.Enqueue(sidePiece2);
  732.  
  733.             pos.x = 0;
  734.  
  735.             pos.z += SideLength;
  736.  
  737.             _generationNeedleSideScene.transform.position = pos;
  738.  
  739.         }
  740.  
  741.         if (_sideScenery.Count > 0)
  742.         {
  743.             piece = _sideScenery.Peek();
  744.             if (piece != null)
  745.             {
  746.                 while (piece.transform.position.z + 0.5f * SideLength < LevelEnd + DistancePassed)
  747.                 {
  748.                     var row = _sideScenery.Dequeue();
  749.                     var poolableItems = row.GetComponentsInChildren<PoolableItem>().ToList();
  750.                     var piecePool = row.GetComponent<PoolableItem>();
  751.                     poolableItems.Remove(piecePool);
  752.                     foreach (var poolItem in poolableItems)
  753.                     {
  754.                         if (!poolItem.PutInPool())
  755.                             Destroy(poolItem.gameObject);
  756.                     }
  757.  
  758.                     if (piecePool != null)
  759.                     {
  760.                         if (!piecePool.PutInPool())
  761.                         {
  762.                             Destroy(piecePool.gameObject);
  763.                         }
  764.  
  765.                     }
  766.                     else Destroy(row);
  767.                     if (_rows.Count <= 0) break;
  768.                     piece = _sideScenery.Peek();
  769.                     if (piece == null) break;
  770.                 }
  771.             }
  772.             else _sideScenery.Dequeue();
  773.         }
  774.  
  775.     }
  776.  
  777.     //Check to add props
  778.     void CheckIfCanAddAnyExtras(GameObject row)
  779.     {
  780.         if(LevelManager.DeactivateSpecialPropsSpawning)return;
  781.         if (_rampWaitRows <= 0)
  782.         {
  783.             if (RampScript.CheckIfCanSpawnRampAndSpawnIt(row,Ramp))
  784.             {
  785.                 _rampWaitRows = LevelManagerScript.RampDistance;
  786.                 return;
  787.             }
  788.         }
  789.         if (_railWaitRows <= 0)
  790.         {
  791.  
  792.             if (GrindPoleScript.CheckIfCanSpawnRailAndSpawnIt(row, PlayerScript, Rail))
  793.            {
  794.                _railWaitRows = LevelManagerScript.RailDistance;
  795.                return;
  796.            }
  797.         }
  798.         if (_holeWaitRows <= 0)
  799.         {
  800.             if (HoleScript.CheckIfCanSpawnAndSpawnIt(row,Hole))
  801.             {
  802.                 _holeWaitRows = LevelManagerScript.HoleDistance;
  803.                 return;
  804.             }
  805.         }
  806.         if (_cannonWaitRows <= 0)
  807.         {
  808.             if (HitLauncher.CheckIfCanSpawnAndSpawnIt(row,Cannon))
  809.             {
  810.                 _cannonWaitRows = LevelManagerScript.CannonDistance;
  811.                 return;
  812.             }
  813.         }
  814.     }
  815.  
  816.     //Add grazing sheep after water
  817.     bool CheckIfShouldSpawnGrazingSheep(GameObject row)
  818.     {
  819.         if(_amountOfRowsToSpawnGrazingSheep<=0)return false;
  820.         if (_shouldWaitBlockBeforeSpawningGrazeSheep)
  821.         {
  822.             _shouldWaitBlockBeforeSpawningGrazeSheep = false;
  823.             return false;
  824.         }
  825.         _amountOfRowsToSpawnGrazingSheep--;
  826.  
  827.         var posPiece = row.transform.position;
  828.         var startposZ = posPiece.z- 0.5f * PieceLength;
  829.  
  830.         var nextSpawnZ = _nextGrazingSheep;
  831.         const float borders = PieceWidth*0.5f;
  832.         while (nextSpawnZ + SheepMovementScript.SheepRadius <= PieceLength)
  833.         {
  834.             var pos = new Vector3(0, posPiece.y + GrazingSheepSpawnHeight, startposZ + nextSpawnZ) { x = Random.Range(-borders, borders) };
  835.  
  836.             var rotation = Random.Range(0.0f, 360.0f);
  837.             EnemySpawnScript.SpawnGrazingSheep(pos,rotation);
  838.             nextSpawnZ += HowManySpaceForGrazingSheep;
  839.         }
  840.         _nextGrazingSheep = nextSpawnZ - PieceLength;
  841.  
  842.         return true;
  843.     }
  844.  
  845.     //Handle the clouds (for cannon)
  846.     public void ShowCloudLayer(bool show)
  847.     {
  848.         var clouds = CloudsLayer.GetComponentsInChildren<Renderer>();
  849.         if (show)
  850.         {
  851.             foreach (var cloud in clouds)
  852.             {
  853.                 cloud.enabled = true;
  854.             }
  855.  
  856.         }
  857.         else
  858.         {
  859.             foreach (var cloud in clouds)
  860.             {
  861.                 cloud.enabled = false;
  862.             }
  863.         }
  864.     }
  865.     void OnDestroy()
  866.     {
  867.         DistancePassed = 0;
  868.     }
  869.  
  870.  
  871.     public enum SpecialLands
  872.     {
  873.         Sea,
  874.     }
  875. }
  876.  
  877.  
  878. public class CliffPieceInformation
  879. {
  880.     private List<int> _nextPieces=new List<int>();
  881.     private List<int> _nextRiverPieces = new List<int>();
  882.  
  883.     public CliffPieceInformation(CliffPieces script,MultiPool cliffPool)
  884.     {
  885.         _nextPieces=new List<int>();
  886.         foreach (var nextPiece in script.ForcedNextPieces)
  887.         {
  888.             _nextPieces.Add(cliffPool.GetIndex(nextPiece));
  889.         }
  890.  
  891.         _nextRiverPieces = new List<int>();
  892.         foreach (var nextPiece in script.ForcedNextPiecesRiver)
  893.         {
  894.             _nextRiverPieces.Add(cliffPool.GetIndex(nextPiece));
  895.         }
  896.  
  897.     }
  898.  
  899.     public int GetNextPiece(bool river, MultiPool cliffMultiPool)
  900.     {
  901.         if (river)
  902.         {
  903.             if (_nextRiverPieces.Count > 0)
  904.             {
  905.                 return CliffPieces.GiveCliffPiece(_nextRiverPieces,cliffMultiPool);
  906.             }
  907.             else return -1;
  908.         }
  909.         else
  910.         {
  911.             if (_nextPieces.Count > 0)
  912.             {
  913.                 return CliffPieces.GiveCliffPiece(_nextPieces,cliffMultiPool);
  914.             }
  915.             else return -1;
  916.         }
  917.         //If there is no next piece forced, because the manager is free to choose what piece they want, than it will return null
  918.     }
  919. }
  920.  
  921. public class LevelPieceInformation
  922. {
  923.     private List<int> _nextPieces = new List<int>();
  924.  
  925.     public LevelPieceInformation(LevelPieces script, MultiPool piecePool)
  926.     {
  927.         _nextPieces = new List<int>();
  928.         foreach (var nextPiece in script.ForcedNextSetPieces)
  929.         {
  930.             _nextPieces.Add(piecePool.GetIndex(nextPiece));
  931.         }
  932.     }
  933.  
  934.     public int GetNextPiece(MultiPool pieceMultiPool)
  935.     {
  936.         if (_nextPieces.Count > 0)
  937.         {
  938.             return LevelPieces.GiveLevelPiece(_nextPieces,pieceMultiPool);
  939.         }
  940.         return -1;
  941.         //If there is no next piece forced, because the manager is free to choose what piece they want, than it will return null
  942.     }
  943. }
Advertisement
Add Comment
Please, Sign In to add comment