using UnityEngine; using System.Collections.Generic; public class InitialRoomSetup : MonoBehaviour { [Header("Starting Room Settings")] [SerializeField] private Vector3 startingRoomSize = new Vector3(20f, 3f, 15f); [SerializeField] private Vector3 startingRoomPosition = Vector3.zero; [SerializeField] private Material wallMaterial; [SerializeField] private float wallThickness = 0.2f; [Header("Ground/Floor")] [SerializeField] private bool createFloor = true; [SerializeField] private Material floorMaterial; private List initialRoomWalls = new List(); private GameObject floorObject; private void Start() { CreateInitialRoom(); CreateNavigationSurface(); } private void CreateInitialRoom() { Vector3 center = startingRoomPosition; float width = startingRoomSize.x; float height = startingRoomSize.y; float depth = startingRoomSize.z; // Calculate wall positions Vector3[] wallPositions = new Vector3[] { center + new Vector3(0, height/2, depth/2), // North wall center + new Vector3(0, height/2, -depth/2), // South wall center + new Vector3(width/2, height/2, 0), // East wall center + new Vector3(-width/2, height/2, 0) // West wall }; Vector3[] wallScales = new Vector3[] { new Vector3(width, height, wallThickness), // North wall new Vector3(width, height, wallThickness), // South wall new Vector3(wallThickness, height, depth), // East wall new Vector3(wallThickness, height, depth) // West wall }; string[] wallNames = { "North Wall", "South Wall", "East Wall", "West Wall" }; // Create walls for (int i = 0; i < wallPositions.Length; i++) { GameObject wall = CreateWall(wallPositions[i], wallScales[i], wallNames[i]); wall.tag = "Wall"; initialRoomWalls.Add(wall); } // Create floor if (createFloor) { CreateFloor(center, width, depth); } Debug.Log("Initial hotel room created successfully"); } private GameObject CreateWall(Vector3 position, Vector3 scale, string name) { GameObject wall = GameObject.CreatePrimitive(PrimitiveType.Cube); wall.name = name; wall.transform.position = position; wall.transform.localScale = scale; wall.tag = "Wall"; // Apply material if (wallMaterial != null) { wall.GetComponent().material = wallMaterial; } else { // Default wall color wall.GetComponent().material.color = Color.gray; } // Ensure collider is set up for navigation obstacles Collider wallCollider = wall.GetComponent(); wallCollider.isTrigger = false; // Add to initial room parent for organization wall.transform.SetParent(this.transform); return wall; } private void CreateFloor(Vector3 center, float width, float depth) { floorObject = GameObject.CreatePrimitive(PrimitiveType.Cube); floorObject.name = "Hotel Floor"; // Position floor slightly below ground level floorObject.transform.position = center + new Vector3(0, -0.05f, 0); floorObject.transform.localScale = new Vector3(width, 0.1f, depth); // Apply floor material if (floorMaterial != null) { floorObject.GetComponent().material = floorMaterial; } else { floorObject.GetComponent().material.color = Color.white; } // Set up collider for walking surface Collider floorCollider = floorObject.GetComponent(); floorCollider.isTrigger = false; floorObject.transform.SetParent(this.transform); } private void CreateNavigationSurface() { // Create a larger ground plane for navigation if one doesn't exist GameObject existingGround = GameObject.Find("Ground"); if (existingGround == null) { GameObject ground = GameObject.CreatePrimitive(PrimitiveType.Plane); ground.name = "Ground"; // Only set tag if it exists try { ground.tag = "Ground"; } catch (UnityException) { Debug.LogWarning("Ground tag not defined. Skipping tag assignment."); } ground.transform.position = Vector3.zero; ground.transform.localScale = new Vector3(10f, 1f, 10f); // Large navigation area // Make ground invisible but keep collider for navigation Renderer groundRenderer = ground.GetComponent(); groundRenderer.enabled = false; ground.transform.SetParent(this.transform); } } #region Public Methods for Room Management public void AddEntranceToInitialRoom(Vector3 entrancePosition) { // Find the closest wall and create an entrance GameObject closestWall = FindClosestWall(entrancePosition); if (closestWall != null) { CreateEntrance(closestWall, entrancePosition); if (GameManager.Instance != null) { GameManager.Instance.SetEntrance(true); } } } private GameObject FindClosestWall(Vector3 position) { GameObject closest = null; float closestDistance = float.MaxValue; foreach (GameObject wall in initialRoomWalls) { if (wall == null) continue; float distance = Vector3.Distance(wall.transform.position, position); if (distance < closestDistance) { closestDistance = distance; closest = wall; } } return closest; } private void CreateEntrance(GameObject wall, Vector3 entrancePosition) { // Create entrance by splitting the wall Transform wallTransform = wall.transform; Vector3 wallScale = wallTransform.localScale; Vector3 wallPosition = wallTransform.position; // For simplicity, create a gap in the center of the wall float gapWidth = 2f; // Determine if it's a north/south wall or east/west wall bool isNorthSouthWall = Mathf.Abs(wallScale.x) > Mathf.Abs(wallScale.z); if (isNorthSouthWall) { // Split horizontally float leftWidth = (wallScale.x - gapWidth) / 2f; if (leftWidth > 0.5f) { // Left piece GameObject leftWall = Instantiate(wall); leftWall.name = wall.name + " (Left)"; leftWall.transform.position = wallPosition + new Vector3(-gapWidth / 2f - leftWidth / 2f, 0, 0); leftWall.transform.localScale = new Vector3(leftWidth, wallScale.y, wallScale.z); // Right piece GameObject rightWall = Instantiate(wall); rightWall.name = wall.name + " (Right)"; rightWall.transform.position = wallPosition + new Vector3(gapWidth / 2f + leftWidth / 2f, 0, 0); rightWall.transform.localScale = new Vector3(leftWidth, wallScale.y, wallScale.z); initialRoomWalls.Add(leftWall); initialRoomWalls.Add(rightWall); } } else { // Split vertically float frontDepth = (wallScale.z - gapWidth) / 2f; if (frontDepth > 0.5f) { // Front piece GameObject frontWall = Instantiate(wall); frontWall.name = wall.name + " (Front)"; frontWall.transform.position = wallPosition + new Vector3(0, 0, -gapWidth / 2f - frontDepth / 2f); frontWall.transform.localScale = new Vector3(wallScale.x, wallScale.y, frontDepth); // Back piece GameObject backWall = Instantiate(wall); backWall.name = wall.name + " (Back)"; backWall.transform.position = wallPosition + new Vector3(0, 0, gapWidth / 2f + frontDepth / 2f); backWall.transform.localScale = new Vector3(wallScale.x, wallScale.y, frontDepth); initialRoomWalls.Add(frontWall); initialRoomWalls.Add(backWall); } } // Remove original wall initialRoomWalls.Remove(wall); DestroyImmediate(wall); Debug.Log("Entrance created in initial room"); } public Vector3 GetRoomCenter() { return startingRoomPosition; } public Vector3 GetRoomSize() { return startingRoomSize; } #endregion }