Advertisement
Guest User

Untitled

a guest
May 24th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. enum Direction { LEFT, STRAIGHT, RIGHT }
  5.  
  6. public class LevelGenerator : MonoBehaviour
  7. {
  8.  
  9. private Queue<GameObject> _leftTurns = new Queue<GameObject>();
  10. private Queue<GameObject> _rightTurns = new Queue<GameObject>();
  11. private Queue<GameObject> _straightRoad = new Queue<GameObject>();
  12.  
  13. private const int _minDir = 0;
  14. private const int _maxDir = 3;
  15.  
  16. private const int _angleOffset = 90;
  17.  
  18. [SerializeField] private GameObject _oldRoad;
  19.  
  20. private GameObject _currentPrefab;
  21. private float _currentAngleZ = 0;
  22. private float _oldAngleZ = 0;
  23. private bool _generationTag = false;
  24.  
  25.  
  26. private GameObject GetRoad(int _way)
  27. {
  28. GameObject temp = null;
  29. switch ((Direction)_way)
  30. {
  31. case Direction.LEFT:
  32. temp = _leftTurns.Dequeue();
  33. break;
  34.  
  35. case Direction.STRAIGHT:
  36. temp = _straightRoad.Dequeue();
  37. break;
  38.  
  39. case Direction.RIGHT:
  40. temp = _rightTurns.Dequeue();
  41. break;
  42. }
  43. temp.SetActive(true);
  44. return temp;
  45. }
  46.  
  47. public void CreatePlatform()
  48. {
  49.  
  50. int way = new System.Random().Next(_minDir, _maxDir); ;
  51. _currentPrefab = GetRoad(way);
  52.  
  53. _currentPrefab.transform.position = _oldRoad.transform.GetChild(way).position;
  54.  
  55. if (way == (int)Direction.LEFT || way == (int)Direction.RIGHT)
  56. {
  57. _oldAngleZ = _currentAngleZ;
  58. SetCorrectAngle((Direction)way);
  59. _currentPrefab.transform.rotation = Quaternion.Euler(0, 0, _oldAngleZ);
  60. return;
  61. }
  62. _currentPrefab.transform.rotation = Quaternion.Euler(0, 0, _currentAngleZ);
  63.  
  64. _oldRoad = _currentPrefab;
  65.  
  66. }
  67.  
  68. private void SetCorrectAngle(Direction _dir)
  69. {
  70. switch (_dir)
  71. {
  72. case Direction.LEFT:
  73. _currentAngleZ += _angleOffset;
  74. break;
  75.  
  76. case Direction.RIGHT:
  77. _currentAngleZ -= _angleOffset;
  78. break;
  79. }
  80. }
  81.  
  82. private void OnTriggerExit2D(Collider2D collision)
  83. {
  84. if (_generationTag)
  85. {
  86. CreatePlatform();
  87. }
  88. if (collision.gameObject.tag == "LeftTurn")
  89. {
  90. collision.gameObject.SetActive(false);
  91. collision.gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
  92. _leftTurns.Enqueue(collision.gameObject);
  93.  
  94. }
  95. if(collision.gameObject.tag == "RightTurn")
  96. {
  97. collision.gameObject.SetActive(false);
  98. collision.gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
  99. _rightTurns.Enqueue(collision.gameObject);
  100.  
  101. }
  102. if(collision.gameObject.tag == "Straight")
  103. {
  104. collision.gameObject.SetActive(false);
  105. collision.gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
  106. _straightRoad.Enqueue(collision.gameObject);
  107.  
  108. }
  109. if(collision.gameObject.tag == "Generation Tag")
  110. {
  111. _generationTag = true;
  112. collision.gameObject.SetActive(false);
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement