InitialRoomSetup.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. public class InitialRoomSetup : MonoBehaviour
  4. {
  5. [Header("Starting Room Settings")]
  6. [SerializeField] private Vector3 startingRoomSize = new Vector3(20f, 3f, 15f);
  7. [SerializeField] private Vector3 startingRoomPosition = Vector3.zero;
  8. [SerializeField] private Material wallMaterial;
  9. [SerializeField] private float wallThickness = 0.2f;
  10. [Header("Ground/Floor")]
  11. [SerializeField] private bool createFloor = true;
  12. [SerializeField] private Material floorMaterial;
  13. private List<GameObject> initialRoomWalls = new List<GameObject>();
  14. private GameObject floorObject;
  15. private void Start()
  16. {
  17. CreateInitialRoom();
  18. CreateNavigationSurface();
  19. }
  20. private void CreateInitialRoom()
  21. {
  22. Vector3 center = startingRoomPosition;
  23. float width = startingRoomSize.x;
  24. float height = startingRoomSize.y;
  25. float depth = startingRoomSize.z;
  26. // Calculate wall positions
  27. Vector3[] wallPositions = new Vector3[]
  28. {
  29. center + new Vector3(0, height/2, depth/2), // North wall
  30. center + new Vector3(0, height/2, -depth/2), // South wall
  31. center + new Vector3(width/2, height/2, 0), // East wall
  32. center + new Vector3(-width/2, height/2, 0) // West wall
  33. };
  34. Vector3[] wallScales = new Vector3[]
  35. {
  36. new Vector3(width, height, wallThickness), // North wall
  37. new Vector3(width, height, wallThickness), // South wall
  38. new Vector3(wallThickness, height, depth), // East wall
  39. new Vector3(wallThickness, height, depth) // West wall
  40. };
  41. string[] wallNames = { "North Wall", "South Wall", "East Wall", "West Wall" };
  42. // Create walls
  43. for (int i = 0; i < wallPositions.Length; i++)
  44. {
  45. GameObject wall = CreateWall(wallPositions[i], wallScales[i], wallNames[i]);
  46. wall.tag = "Wall";
  47. initialRoomWalls.Add(wall);
  48. }
  49. // Create floor
  50. if (createFloor)
  51. {
  52. CreateFloor(center, width, depth);
  53. }
  54. Debug.Log("Initial hotel room created successfully");
  55. }
  56. private GameObject CreateWall(Vector3 position, Vector3 scale, string name)
  57. {
  58. GameObject wall = GameObject.CreatePrimitive(PrimitiveType.Cube);
  59. wall.name = name;
  60. wall.transform.position = position;
  61. wall.transform.localScale = scale;
  62. wall.tag = "Wall";
  63. // Apply material
  64. if (wallMaterial != null)
  65. {
  66. wall.GetComponent<Renderer>().material = wallMaterial;
  67. }
  68. else
  69. {
  70. // Default wall color
  71. wall.GetComponent<Renderer>().material.color = Color.gray;
  72. }
  73. // Ensure collider is set up for navigation obstacles
  74. Collider wallCollider = wall.GetComponent<Collider>();
  75. wallCollider.isTrigger = false;
  76. // Add to initial room parent for organization
  77. wall.transform.SetParent(this.transform);
  78. return wall;
  79. }
  80. private void CreateFloor(Vector3 center, float width, float depth)
  81. {
  82. floorObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
  83. floorObject.name = "Hotel Floor";
  84. // Position floor slightly below ground level
  85. floorObject.transform.position = center + new Vector3(0, -0.05f, 0);
  86. floorObject.transform.localScale = new Vector3(width, 0.1f, depth);
  87. // Apply floor material
  88. if (floorMaterial != null)
  89. {
  90. floorObject.GetComponent<Renderer>().material = floorMaterial;
  91. }
  92. else
  93. {
  94. floorObject.GetComponent<Renderer>().material.color = Color.white;
  95. }
  96. // Set up collider for walking surface
  97. Collider floorCollider = floorObject.GetComponent<Collider>();
  98. floorCollider.isTrigger = false;
  99. floorObject.transform.SetParent(this.transform);
  100. }
  101. private void CreateNavigationSurface()
  102. {
  103. // Create a larger ground plane for navigation if one doesn't exist
  104. GameObject existingGround = GameObject.Find("Ground");
  105. if (existingGround == null)
  106. {
  107. GameObject ground = GameObject.CreatePrimitive(PrimitiveType.Plane);
  108. ground.name = "Ground";
  109. // Only set tag if it exists
  110. try
  111. {
  112. ground.tag = "Ground";
  113. }
  114. catch (UnityException)
  115. {
  116. Debug.LogWarning("Ground tag not defined. Skipping tag assignment.");
  117. }
  118. ground.transform.position = Vector3.zero;
  119. ground.transform.localScale = new Vector3(10f, 1f, 10f); // Large navigation area
  120. // Make ground invisible but keep collider for navigation
  121. Renderer groundRenderer = ground.GetComponent<Renderer>();
  122. groundRenderer.enabled = false;
  123. ground.transform.SetParent(this.transform);
  124. }
  125. }
  126. #region Public Methods for Room Management
  127. public void AddEntranceToInitialRoom(Vector3 entrancePosition)
  128. {
  129. // Find the closest wall and create an entrance
  130. GameObject closestWall = FindClosestWall(entrancePosition);
  131. if (closestWall != null)
  132. {
  133. CreateEntrance(closestWall, entrancePosition);
  134. if (GameManager.Instance != null)
  135. {
  136. GameManager.Instance.SetEntrance(true);
  137. }
  138. }
  139. }
  140. private GameObject FindClosestWall(Vector3 position)
  141. {
  142. GameObject closest = null;
  143. float closestDistance = float.MaxValue;
  144. foreach (GameObject wall in initialRoomWalls)
  145. {
  146. if (wall == null) continue;
  147. float distance = Vector3.Distance(wall.transform.position, position);
  148. if (distance < closestDistance)
  149. {
  150. closestDistance = distance;
  151. closest = wall;
  152. }
  153. }
  154. return closest;
  155. }
  156. private void CreateEntrance(GameObject wall, Vector3 entrancePosition)
  157. {
  158. // Create entrance by splitting the wall
  159. Transform wallTransform = wall.transform;
  160. Vector3 wallScale = wallTransform.localScale;
  161. Vector3 wallPosition = wallTransform.position;
  162. // For simplicity, create a gap in the center of the wall
  163. float gapWidth = 2f;
  164. // Determine if it's a north/south wall or east/west wall
  165. bool isNorthSouthWall = Mathf.Abs(wallScale.x) > Mathf.Abs(wallScale.z);
  166. if (isNorthSouthWall)
  167. {
  168. // Split horizontally
  169. float leftWidth = (wallScale.x - gapWidth) / 2f;
  170. if (leftWidth > 0.5f)
  171. {
  172. // Left piece
  173. GameObject leftWall = Instantiate(wall);
  174. leftWall.name = wall.name + " (Left)";
  175. leftWall.transform.position = wallPosition + new Vector3(-gapWidth / 2f - leftWidth / 2f, 0, 0);
  176. leftWall.transform.localScale = new Vector3(leftWidth, wallScale.y, wallScale.z);
  177. // Right piece
  178. GameObject rightWall = Instantiate(wall);
  179. rightWall.name = wall.name + " (Right)";
  180. rightWall.transform.position = wallPosition + new Vector3(gapWidth / 2f + leftWidth / 2f, 0, 0);
  181. rightWall.transform.localScale = new Vector3(leftWidth, wallScale.y, wallScale.z);
  182. initialRoomWalls.Add(leftWall);
  183. initialRoomWalls.Add(rightWall);
  184. }
  185. }
  186. else
  187. {
  188. // Split vertically
  189. float frontDepth = (wallScale.z - gapWidth) / 2f;
  190. if (frontDepth > 0.5f)
  191. {
  192. // Front piece
  193. GameObject frontWall = Instantiate(wall);
  194. frontWall.name = wall.name + " (Front)";
  195. frontWall.transform.position = wallPosition + new Vector3(0, 0, -gapWidth / 2f - frontDepth / 2f);
  196. frontWall.transform.localScale = new Vector3(wallScale.x, wallScale.y, frontDepth);
  197. // Back piece
  198. GameObject backWall = Instantiate(wall);
  199. backWall.name = wall.name + " (Back)";
  200. backWall.transform.position = wallPosition + new Vector3(0, 0, gapWidth / 2f + frontDepth / 2f);
  201. backWall.transform.localScale = new Vector3(wallScale.x, wallScale.y, frontDepth);
  202. initialRoomWalls.Add(frontWall);
  203. initialRoomWalls.Add(backWall);
  204. }
  205. }
  206. // Remove original wall
  207. initialRoomWalls.Remove(wall);
  208. DestroyImmediate(wall);
  209. Debug.Log("Entrance created in initial room");
  210. }
  211. public Vector3 GetRoomCenter()
  212. {
  213. return startingRoomPosition;
  214. }
  215. public Vector3 GetRoomSize()
  216. {
  217. return startingRoomSize;
  218. }
  219. #endregion
  220. }