Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SetSpawner : MonoBehaviour {
  6.  
  7. //Object pools
  8. //Easy objectpool
  9. //Normal objectpool
  10. //Hard objectpool
  11.  
  12. public bool canSpawn;
  13. public Transform spawnTransform;
  14. private HighScore difficultySetter;
  15. private SetObjectPools objectPool;
  16.  
  17. private float _easyChance;
  18. private float _normalChance;
  19. private float _hardChance;
  20. private float _totalChance;
  21.  
  22. // Percentage spawn chance
  23. private float _easySpawnChance;
  24. private float _normalSpawnChance;
  25. private float _hardSpawnChance;
  26.  
  27. private float _randomValue;
  28. private Vector3 _endOfSet;
  29. private Vector3 spawnPosition;
  30.  
  31.  
  32. void Awake()
  33. {
  34. difficultySetter = GameObject.Find("GameController").GetComponent<HighScore>();
  35. objectPool = GameObject.Find("SetObjectPooler").GetComponent<SetObjectPools>();
  36. spawnPosition = new Vector3(spawnTransform.position.x, spawnTransform.position.y, spawnTransform.position.z);
  37. canSpawn = true;
  38. }
  39.  
  40.  
  41. // Update is called once per frame
  42. void Update()
  43. {
  44. if (_endOfSet.x < spawnPosition.x)
  45. {
  46. canSpawn = true;
  47. Debug.Log(" first if statement, should be true " + canSpawn);
  48. Debug.Log("_endofSet.x :" + _endOfSet.x + ", spawnPosition.x:" + spawnPosition.x);
  49. }
  50.  
  51. if (canSpawn)
  52. {
  53. switch (difficultySetter.currentDifficulty)
  54. {
  55. case 0: //easy
  56. _easyChance = 100;
  57. _normalChance = 0;
  58. _hardChance = 0;
  59. break;
  60. case 1: // normal
  61. _easyChance = 100;
  62. _normalChance = 500;
  63. _hardChance = 0;
  64. break;
  65. case 2: // hard
  66. _easyChance = 100;
  67. _normalChance = 500;
  68. _hardChance = 1500;
  69. break;
  70. }
  71.  
  72. // Calculate percentages
  73. _totalChance = _easyChance + _normalChance + _hardChance;
  74. _easySpawnChance = _easyChance / _totalChance;
  75. _normalSpawnChance = _normalChance / _totalChance;
  76. _hardSpawnChance = _hardChance / _totalChance;
  77.  
  78. // Spawns the set based on a random value and the spawn percentages.
  79. // TODO: Break after if statement is done
  80.  
  81. _randomValue = Random.value;
  82. if (_randomValue <= _easySpawnChance)
  83. {
  84. objectPool.chosenSets = objectPool.easySets;
  85. SpawnSet();
  86. }
  87.  
  88. _randomValue -= _easySpawnChance;
  89. if (_randomValue <= _normalSpawnChance)
  90. {
  91. objectPool.chosenSets = objectPool.normalSets;
  92. SpawnSet();
  93. }
  94.  
  95. _randomValue -= _normalSpawnChance;
  96. if (_randomValue <= _hardSpawnChance)
  97. {
  98. objectPool.chosenSets = objectPool.hardSets;
  99. SpawnSet();
  100. }
  101.  
  102. }
  103. }
  104.  
  105. void SpawnSet()
  106. {
  107. Debug.Log("Spawn Set is just called, canSpawnshould be true, it is: " + canSpawn);
  108. GameObject set = objectPool.GetPooledSet();
  109. set.transform.position = spawnPosition;
  110. //newItem.transform.rotation = transform.rotation;
  111. set.SetActive(true);
  112. _endOfSet = set.gameObject.transform.GetChild(0).localPosition;
  113. canSpawn = false;
  114.  
  115. Debug.Log(_endOfSet + " this is the end of the (just spawned) set position...");
  116. Debug.Log(" Spawn set is doner, canSpawn should be false, it is " + canSpawn);
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement