| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- 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<GameObject> initialRoomWalls = new List<GameObject>();
- 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<Renderer>().material = wallMaterial;
- }
- else
- {
- // Default wall color
- wall.GetComponent<Renderer>().material.color = Color.gray;
- }
- // Ensure collider is set up for navigation obstacles
- Collider wallCollider = wall.GetComponent<Collider>();
- 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<Renderer>().material = floorMaterial;
- }
- else
- {
- floorObject.GetComponent<Renderer>().material.color = Color.white;
- }
- // Set up collider for walking surface
- Collider floorCollider = floorObject.GetComponent<Collider>();
- 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<Renderer>();
- 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
- }
|