Files
zeling_v2/Assets/_Game/Scripts/World/RoomController.cs
2026-05-17 07:56:12 +08:00

56 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using BaseGames.Camera;
using BaseGames.Core;
using UnityEngine;
namespace BaseGames.World
{
/// <summary>
/// 房间控制器。挂在每个房间场景的 [RoomRoot] 下。
/// Start 时切换摄像机到该房间的 CameraArea并提供出生点查询。
/// </summary>
public class RoomController : MonoBehaviour
{
[SerializeField] private string _roomId;
[SerializeField] private PlayerSpawnPoint[] _spawnPoints;
[SerializeField] private CameraArea _cameraArea; // 该房间默认的相机区域
public string RoomId => _roomId;
private void Start()
{
CameraArea area = _cameraArea;
// 未手动绑定时,自动在当前场景中查找(每个房间场景通常只有一个 CameraArea
if (area == null)
{
#if UNITY_6000_0_OR_NEWER
area = Object.FindFirstObjectByType<CameraArea>();
#else
area = Object.FindObjectOfType<CameraArea>();
#endif
if (area != null)
Debug.LogWarning($"[RoomController] {name}_cameraArea 未绑定,自动找到 {area.name}。建议在 Inspector 中手动指定。");
else
Debug.LogError($"[RoomController] {name}:未找到 CameraArea相机不会切换。");
}
if (area != null)
// instantCut = true房间入口传送后相机硬切无混合拖影
ServiceLocator.GetOrDefault<ICameraService>()?.SwitchArea(area, 0, instantCut: true);
}
/// <summary>通过 transitionId 查找对应的出生点。</summary>
public PlayerSpawnPoint GetSpawnPoint(string transitionId)
{
if (_spawnPoints == null) return null;
foreach (var sp in _spawnPoints)
{
if (sp != null && sp.TransitionId == transitionId)
return sp;
}
// Fallback返回第一个出生点
return _spawnPoints.Length > 0 ? _spawnPoints[0] : null;
}
}
}