56 lines
2.1 KiB
C#
56 lines
2.1 KiB
C#
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;
|
||
}
|
||
}
|
||
}
|