Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- enum Direction { LEFT, STRAIGHT, RIGHT }
- public class LevelGenerator : MonoBehaviour
- {
- private Queue<GameObject> _leftTurns = new Queue<GameObject>();
- private Queue<GameObject> _rightTurns = new Queue<GameObject>();
- private Queue<GameObject> _straightRoad = new Queue<GameObject>();
- private const int _minDir = 0;
- private const int _maxDir = 3;
- private const int _angleOffset = 90;
- [SerializeField] private GameObject _oldRoad;
- private GameObject _currentPrefab;
- private float _currentAngleZ = 0;
- private float _oldAngleZ = 0;
- private bool _generationTag = false;
- private GameObject GetRoad(int _way)
- {
- GameObject temp = null;
- switch ((Direction)_way)
- {
- case Direction.LEFT:
- temp = _leftTurns.Dequeue();
- break;
- case Direction.STRAIGHT:
- temp = _straightRoad.Dequeue();
- break;
- case Direction.RIGHT:
- temp = _rightTurns.Dequeue();
- break;
- }
- temp.SetActive(true);
- return temp;
- }
- public void CreatePlatform()
- {
- int way = new System.Random().Next(_minDir, _maxDir); ;
- _currentPrefab = GetRoad(way);
- _currentPrefab.transform.position = _oldRoad.transform.GetChild(way).position;
- if (way == (int)Direction.LEFT || way == (int)Direction.RIGHT)
- {
- _oldAngleZ = _currentAngleZ;
- SetCorrectAngle((Direction)way);
- _currentPrefab.transform.rotation = Quaternion.Euler(0, 0, _oldAngleZ);
- return;
- }
- _currentPrefab.transform.rotation = Quaternion.Euler(0, 0, _currentAngleZ);
- _oldRoad = _currentPrefab;
- }
- private void SetCorrectAngle(Direction _dir)
- {
- switch (_dir)
- {
- case Direction.LEFT:
- _currentAngleZ += _angleOffset;
- break;
- case Direction.RIGHT:
- _currentAngleZ -= _angleOffset;
- break;
- }
- }
- private void OnTriggerExit2D(Collider2D collision)
- {
- if (_generationTag)
- {
- CreatePlatform();
- }
- if (collision.gameObject.tag == "LeftTurn")
- {
- collision.gameObject.SetActive(false);
- collision.gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
- _leftTurns.Enqueue(collision.gameObject);
- }
- if(collision.gameObject.tag == "RightTurn")
- {
- collision.gameObject.SetActive(false);
- collision.gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
- _rightTurns.Enqueue(collision.gameObject);
- }
- if(collision.gameObject.tag == "Straight")
- {
- collision.gameObject.SetActive(false);
- collision.gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
- _straightRoad.Enqueue(collision.gameObject);
- }
- if(collision.gameObject.tag == "Generation Tag")
- {
- _generationTag = true;
- collision.gameObject.SetActive(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement