Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using Random = UnityEngine.Random;
- public class GenerationScript : MonoBehaviour
- {
- [SerializeField] private LevelScript _levelScript = null;
- [SerializeField] private List<GeneratedObjects> _traps=new List<GeneratedObjects>();
- [SerializeField]private List<GeneratedObjects> _powerUps = new List<GeneratedObjects>();
- private GameObject _trapsAndPowerups;
- private int _amountTiles;
- private bool _finishInOneTick = false;
- private const float TrapValueMin = 0.175f;
- private const float TrapValueMax = 0.25f;
- private const float PowerUpValue = 0.925f;
- private float _trapValue = 0.0f;
- private const float EmptyZone = 5.0f;
- private const float HeightAnvil = 1.5f;
- private const int AmountRowsGeneratedPerTick = 30;
- void Awake ()
- {
- var amountTilesFloat = ((_levelScript.Radius*2)/_levelScript.WidthObject);
- _amountTiles = (int)amountTilesFloat;
- if (Mathf.Approximately(_amountTiles + 1, amountTilesFloat)) _amountTiles++;
- NormalizeGeneratedObjects(_traps);
- NormalizeGeneratedObjects(_powerUps);
- }
- //Normalize chances on objects
- void NormalizeGeneratedObjects(List<GeneratedObjects> generatedObjects)
- {
- float totalChance = 0;
- for (int i = 0; i < generatedObjects.Count;)
- {
- if (generatedObjects[i].Chance <= 0) generatedObjects.RemoveAt(i);
- else
- {
- totalChance += generatedObjects[i].Chance;
- i++;
- }
- }
- if (Mathf.Approximately(totalChance, 0))
- {
- Debug.LogError("List has a total chance of 0");
- }
- float chanceDivision = 1.000000f / totalChance;
- for (int i = 0; i < generatedObjects.Count; i++)
- {
- var generatedObject = generatedObjects[i];
- var newChance = generatedObject.Chance * chanceDivision;
- generatedObject.Chance = newChance;
- generatedObjects[i] = generatedObject;
- }
- }
- //Generate level
- public void Generate(float trapChanceT)
- {
- _trapValue = Mathf.Lerp(TrapValueMin, TrapValueMax, trapChanceT);
- StartCoroutine(GenerateUpdate());
- }
- //Make that the level automaticly finishes next tick
- public void ForceFinishNextTick()
- {
- _finishInOneTick = true;
- }
- //Generation Update
- private IEnumerator GenerateUpdate()
- {
- _finishInOneTick = false;
- if (_trapsAndPowerups)
- {
- //Put previous traps and powerups back in their pools
- var poolableItems = _trapsAndPowerups.GetComponentsInChildren<PoolableItem>();
- var poolItemsCount = poolableItems.Count();
- for (int i = 0; i < poolItemsCount; i++)
- {
- if (!poolableItems[i].PutInPool())
- {
- Destroy(poolableItems[i].gameObject);
- }
- }
- Destroy(_trapsAndPowerups);
- }
- //Generate new level
- _trapsAndPowerups = new GameObject();
- int amount = 0;
- var seed=new Vector2(Random.Range(0.0f,1000.0f), Random.Range(0.0f, 1000.0f));
- for (int m = 0; m < PickAChestScript.ActivePickAChestScript.AmountOfLevels; m++)
- {
- float startHeight = 100*m;
- float endHeight = PickAChestScript.ActivePickAChestScript.SizeTunnelPiece - EmptyZone;
- for (float i = EmptyZone; i <= endHeight; i += HeightAnvil)
- {
- //Limit amount of lines that get generated every tick to improve performance
- if (!_finishInOneTick)
- {
- amount++;
- if (amount > AmountRowsGeneratedPerTick)
- {
- yield return null;
- amount -= AmountRowsGeneratedPerTick;
- }
- }
- GenerateLine(-(i+ startHeight),seed);
- }
- }
- }
- //Generate one line
- void GenerateLine(float height,Vector2 seed)
- {
- var xStart = -_levelScript.Radius + _levelScript.WidthObject * 0.5f;
- var xDelta = ((_levelScript.Radius * 2) / _amountTiles);
- for (int i = 0; i < _amountTiles; i++)
- {
- var position = new Vector3(xStart + xDelta * i, height, 0);
- //Get value to see if trap, powerup or empty
- var value = Mathf.PerlinNoise(position.x+seed.x, position.y+seed.y);
- if (value < _trapValue)
- {
- //Get random trap
- var trap = SelectGeneratedObjects(_traps).MakePoolItem(position, Quaternion.identity);
- if (trap != null) trap.transform.parent = _trapsAndPowerups.transform;
- }
- else if (value > PowerUpValue)
- {
- //Get random powerup
- var powerup = SelectGeneratedObjects(_powerUps).MakePoolItem(position, Quaternion.identity);
- if (powerup != null) powerup.transform.parent = _trapsAndPowerups.transform;
- }
- }
- }
- //Get random object
- private Pool SelectGeneratedObjects(List<GeneratedObjects> _generatedObjects)
- {
- var chance = Random.Range(0.0f, 1.0f);
- for (int i = 0; i < _generatedObjects.Count; i++)
- {
- chance -= _generatedObjects[i].Chance;
- if (chance <= 0) return _generatedObjects[i].Pool;
- }
- if (_generatedObjects.Count == 0) return null;
- return _generatedObjects[_generatedObjects.Count - 1].Pool;
- }
- }
- [Serializable]
- public struct GeneratedObjects
- {
- public Pool Pool;
- public float Chance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement