Guest User

Untitled

a guest
Jan 13th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class RoomInfo
  7. {
  8. public string name;
  9. public int X;
  10. public int Y;
  11. }
  12.  
  13. public class RoomController : MonoBehaviour
  14. {
  15. public static RoomController instance;
  16. string currentWorldName = "Basement";
  17. RoomInfo currentLoadRoomData;
  18. Room currRoom;
  19. Queue<RoomInfo> loadRoomQueue = new Queue<RoomInfo>();
  20. public List<Room> loadedRooms = new List<Room>();
  21. bool isLoadingRoom = false;
  22.  
  23. void Awake()
  24. {
  25. instance = this;
  26. }
  27.  
  28. void Start()
  29. {
  30. //LoadRoom("Start", 0, 0);
  31. //LoadRoom("Empty", 1, 0);
  32. //LoadRoom("Empty", -1, 0);
  33. //LoadRoom("Empty", 0, 1);
  34. //LoadRoom("Empty", 0, -1);
  35. }
  36.  
  37. void Update()
  38. {
  39. UpdateRoomQueue();
  40. }
  41.  
  42. void UpdateRoomQueue()
  43. {
  44. if(isLoadingRoom)
  45. {
  46. return;
  47. }
  48.  
  49. if(loadRoomQueue.Count == 0)
  50. {
  51. return;
  52. }
  53.  
  54. currentLoadRoomData = loadRoomQueue.Dequeue();
  55. isLoadingRoom = true;
  56.  
  57. StartCoroutine(LoadRoomRoutine(currentLoadRoomData));
  58. }
  59.  
  60. public void LoadRoom(string name, int x, int y)
  61. {
  62. if(DoesRoomExist(x, y))
  63. {
  64. return;
  65. }
  66.  
  67. RoomInfo newRoomData = new RoomInfo();
  68. newRoomData.name = name;
  69. newRoomData.X = x;
  70. newRoomData.Y = y;
  71.  
  72. loadRoomQueue.Enqueue(newRoomData);
  73. }
  74.  
  75. IEnumerator LoadRoomRoutine(RoomInfo info)
  76. {
  77. string roomName = currentWorldName + info.name;
  78.  
  79. AsyncOperation loadRoom = SceneManager.LoadSceneAsync(roomName, LoadSceneMode.Additive);
  80.  
  81. while(loadRoom.isDone == false)
  82. {
  83. yield return null;
  84. }
  85. }
  86.  
  87. public void RegisterRoom( Room room)
  88. {
  89. if(!DoesRoomExist(currentLoadRoomData.X, currentLoadRoomData.Y))
  90. {
  91. room.transform.position = new Vector3(
  92. currentLoadRoomData.X * room.Width,
  93. currentLoadRoomData.Y * room.Height,
  94. 0
  95. );
  96.  
  97. room.X = currentLoadRoomData.X;
  98. room.Y = currentLoadRoomData.Y;
  99. room.name = currentWorldName + "-" + currentLoadRoomData.name + " " + room.X + ", " + room.Y;
  100. room.transform.parent = transform;
  101.  
  102. isLoadingRoom = false;
  103.  
  104. if(loadedRooms.Count == 0)
  105. {
  106. CameraController.instance.currRoom = room;
  107. }
  108.  
  109. loadedRooms.Add(room);
  110. room.RemoveUnconnectedDoors();
  111. }
  112. else
  113. {
  114. Destroy(room.gameObject);
  115. isLoadingRoom = false;
  116. }
  117. }
  118.  
  119. public bool DoesRoomExist( int x, int y)
  120. {
  121. return loadedRooms.Find( item => item.X == x && item.Y == y) != null;
  122. }
  123.  
  124. public Room FindRoom( int x, int y)
  125. {
  126. return loadedRooms.Find( item => item.X == x && item.Y == y);
  127. }
  128.  
  129. public void OnPlayerEnterRoom(Room room)
  130. {
  131. CameraController.instance.currRoom = room;
  132. currRoom = room;
  133. }
  134. }
Add Comment
Please, Sign In to add comment