Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class TerrainSpawner : MonoBehaviour
  5. {
  6. public GameObject SOLID_BLOCK_PREFAB;
  7. public Vector2 roomSize;
  8. public Vector2 roomGenLocation;
  9. public int CHANCE_UP1, CHANCE_DOWN1;
  10.  
  11. public int startGen_offset, end_offset;
  12.  
  13. private enum PrevBlock
  14. {
  15. WENT_UP,
  16. WENT_DOWN,
  17. STAYED
  18. }
  19.  
  20. //next is used to force blocks when they have reached a limit
  21. private enum NextBlock
  22. {
  23. GO_UP,
  24. GO_DOWN,
  25. STAY,
  26. }
  27.  
  28. NextBlock next;
  29. PrevBlock prev;
  30.  
  31. private bool BoundsCheck(Vector3 a, Vector3 b)
  32. {
  33. return (a.y < b.y - 1 && a.y > b.y - (roomSize.y / 2) + 1);
  34. }
  35. private bool BoundsCheck2(Vector3 a, Vector3 b)
  36. {
  37. return (a.y > b.y + 1 && a.y < b.y + (roomSize.y / 2) - 1);
  38. }
  39.  
  40. void Start()
  41. {
  42. Random.seed = (int)System.DateTime.Now.Ticks;
  43. if (CHANCE_UP1 + CHANCE_DOWN1 != 100)
  44. {
  45. Debug.Log("Percentages must add up to 100%");
  46. return;
  47. }
  48. else if (startGen_offset >= roomSize.x)
  49. {
  50. Debug.Log("Your starting offset cannot be more than horizontal roomsize");
  51. return;
  52. }
  53. prev = PrevBlock.STAYED;
  54. next = NextBlock.STAY;
  55. GameObject upBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(roomGenLocation.x, roomGenLocation.y + roomSize.y / 2f), Quaternion.identity) as GameObject;
  56. upBlock.transform.localScale = new Vector3(roomSize.x - 1f, 1f, 1f);
  57. upBlock.gameObject.name = "UpBlock";
  58. GameObject downBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(roomGenLocation.x, roomGenLocation.y - roomSize.y / 2f), Quaternion.identity) as GameObject;
  59. downBlock.transform.localScale = new Vector3(roomSize.x - 1f, 1f, 1f);
  60. downBlock.gameObject.name = "DownBlock";
  61. GameObject rightBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(roomGenLocation.x + roomSize.x / 2f, roomGenLocation.y), Quaternion.identity) as GameObject;
  62. rightBlock.transform.localScale = new Vector3(1f, roomSize.y + 1f, 1f);
  63. rightBlock.gameObject.name = "RightBlock";
  64. GameObject leftBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(roomGenLocation.x - roomSize.x / 2f, roomGenLocation.y), Quaternion.identity) as GameObject;
  65. leftBlock.transform.localScale = new Vector3(1f, roomSize.y + 1f, 1f);
  66. leftBlock.gameObject.name = "LeftBlock";
  67.  
  68. Vector3 prevPos = new Vector3(leftBlock.transform.position.x + startGen_offset, downBlock.transform.position.y + 1);
  69. prev = PrevBlock.STAYED;
  70. next = NextBlock.STAY;
  71.  
  72. for (; prevPos.x < rightBlock.transform.position.x - end_offset; prevPos.x++)
  73. {
  74. for (float b = downBlock.transform.position.y + 1; b < (int)roomGenLocation.y + 1; b++)
  75. {
  76. if ((Random.Range(1, 101) < CHANCE_UP1 && BoundsCheck(prevPos, roomGenLocation) && (prev == PrevBlock.STAYED || prev == PrevBlock.WENT_UP))
  77. || next == NextBlock.GO_UP)
  78. {// nextblock goes up by 1
  79. GameObject tempBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(prevPos.x, prevPos.y + 1f), Quaternion.identity) as GameObject;
  80. prevPos = tempBlock.transform.position;
  81. tempBlock.gameObject.name = "wentUp";
  82. prev = PrevBlock.WENT_UP;
  83. next = NextBlock.STAY;
  84. break;
  85. }
  86. else if ((Random.Range(1, 101) < CHANCE_DOWN1 && BoundsCheck(prevPos, roomGenLocation) && (prev == PrevBlock.STAYED || prev == PrevBlock.WENT_DOWN))
  87. || next == NextBlock.GO_DOWN)
  88. { // nextblock goes down by 1
  89. GameObject tempBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(prevPos.x, prevPos.y - 1f), Quaternion.identity) as GameObject;
  90. prevPos = tempBlock.transform.position;
  91. tempBlock.gameObject.name = "wentDown";
  92. prev = PrevBlock.WENT_DOWN;
  93. next = NextBlock.STAY;
  94. break;
  95. }
  96. else
  97. {//height stays
  98. if (prevPos.y >= roomGenLocation.y - 1) // IF I HIT A LIMIT
  99. {
  100. next = NextBlock.GO_DOWN;
  101. }
  102. else if (prevPos.y <= roomGenLocation.y - (roomSize.y / 2) + 1) // IF I HIT A LIMIT
  103. {
  104. next = NextBlock.GO_UP;
  105. }
  106. else
  107. {
  108. prev = PrevBlock.STAYED;
  109. }
  110. GameObject tempBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(prevPos.x, prevPos.y), Quaternion.identity) as GameObject;
  111. tempBlock.gameObject.name = "stayed";
  112. prevPos = tempBlock.transform.position;
  113. break;
  114. }
  115. }
  116. }
  117.  
  118. prevPos = new Vector3(leftBlock.transform.position.x + startGen_offset, upBlock.transform.position.y - 1);
  119. for (; prevPos.x < rightBlock.transform.position.x - end_offset; prevPos.x++)
  120. {
  121. for (float b = downBlock.transform.position.y + 1; b < (int)roomGenLocation.y + 1; b++)
  122. {
  123. if ((Random.Range(1, 101) < CHANCE_UP1 && BoundsCheck2(prevPos, roomGenLocation) && (prev == PrevBlock.STAYED || prev == PrevBlock.WENT_UP))
  124. || next == NextBlock.GO_UP)
  125. {// nextblock goes up by 1
  126. GameObject tempBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(prevPos.x, prevPos.y + 1f), Quaternion.identity) as GameObject;
  127. prevPos = tempBlock.transform.position;
  128. tempBlock.gameObject.name = "wentUp";
  129. prev = PrevBlock.WENT_UP;
  130. next = NextBlock.STAY;
  131. break;
  132. }
  133. else if ((Random.Range(1, 101) < CHANCE_DOWN1 && BoundsCheck2(prevPos, roomGenLocation) && (prev == PrevBlock.STAYED || prev == PrevBlock.WENT_DOWN))
  134. || next == NextBlock.GO_DOWN)
  135. { // nextblock goes down by 1
  136. GameObject tempBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(prevPos.x, prevPos.y - 1f), Quaternion.identity) as GameObject;
  137. prevPos = tempBlock.transform.position;
  138. tempBlock.gameObject.name = "wentDown";
  139. prev = PrevBlock.WENT_DOWN;
  140. next = NextBlock.STAY;
  141. break;
  142. }
  143. else
  144. {//height stays
  145. if (prevPos.y >= upBlock.transform.position.y - 1) // IF I HIT A LIMIT
  146. {
  147. next = NextBlock.GO_DOWN;
  148. }
  149. else if (prevPos.y <= roomGenLocation.y + 1) // IF I HIT A LIMIT
  150. {
  151. next = NextBlock.GO_UP;
  152. }
  153. else
  154. {
  155. prev = PrevBlock.STAYED;
  156. }
  157. GameObject tempBlock = Instantiate(SOLID_BLOCK_PREFAB, new Vector3(prevPos.x, prevPos.y), Quaternion.identity) as GameObject;
  158. tempBlock.gameObject.name = "stayed";
  159. prevPos = tempBlock.transform.position;
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement