多轮审查和修复
This commit is contained in:
51
Assets/Scripts/World/Map/MapPlayerTracker.cs
Normal file
51
Assets/Scripts/World/Map/MapPlayerTracker.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace BaseGames.World.Map
|
||||
{
|
||||
/// <summary>
|
||||
/// 将玩家世界坐标转换为地图格子坐标,供 MapPanel 显示玩家位置图标(架构 15_MapShopModule §1.4)。
|
||||
/// 挂在 Player GameObject 上(LateUpdate 每帧计算)。
|
||||
/// </summary>
|
||||
public class MapPlayerTracker : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform _playerTransform;
|
||||
[SerializeField] private MapDatabaseSO _database;
|
||||
|
||||
[Header("世界坐标 → 格子坐标换算参数")]
|
||||
[SerializeField] private float _worldUnitsPerCell = 18f; // 1 格 = N 世界单位
|
||||
|
||||
/// <summary>玩家当前所在房间 ID(用于地图高亮当前房间)。</summary>
|
||||
public string CurrentRoomId { get; private set; }
|
||||
|
||||
/// <summary>玩家在当前格子房间内的归一化坐标(0~1)。</summary>
|
||||
public Vector2 NormalizedPositionInRoom { get; private set; }
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (_playerTransform == null || _database?.AllRooms == null) return;
|
||||
|
||||
Vector2 worldPos = _playerTransform.position;
|
||||
Vector2Int cellPos = WorldToCell(worldPos);
|
||||
|
||||
foreach (var room in _database.AllRooms)
|
||||
{
|
||||
if (room == null) continue;
|
||||
var rect = new RectInt(room.GridPosition, room.GridSize);
|
||||
if (rect.Contains(cellPos))
|
||||
{
|
||||
CurrentRoomId = room.RoomId;
|
||||
Vector2 inRoom = (Vector2)(cellPos - room.GridPosition);
|
||||
NormalizedPositionInRoom = new Vector2(
|
||||
inRoom.x / Mathf.Max(1, room.GridSize.x),
|
||||
inRoom.y / Mathf.Max(1, room.GridSize.y));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Vector2Int WorldToCell(Vector2 worldPos)
|
||||
=> new(
|
||||
Mathf.FloorToInt(worldPos.x / _worldUnitsPerCell),
|
||||
Mathf.FloorToInt(worldPos.y / _worldUnitsPerCell));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user