多轮审查和修复
This commit is contained in:
96
Assets/Scripts/World/Map/MapManager.cs
Normal file
96
Assets/Scripts/World/Map/MapManager.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using BaseGames.Core;
|
||||
using BaseGames.Core.Events;
|
||||
using BaseGames.Core.Save;
|
||||
|
||||
namespace BaseGames.World.Map
|
||||
{
|
||||
/// <summary>
|
||||
/// 运行时地图管理器(架构 15_MapShopModule §1.2)。
|
||||
/// 挂在 Persistent 场景 [GameManagers] 下,通过事件驱动记录已探索/已完整地图的房间。
|
||||
/// 实现 ISaveable 持久化探索进度。
|
||||
/// </summary>
|
||||
[DefaultExecutionOrder(-700)]
|
||||
public class MapManager : MonoBehaviour, ISaveable, IMapService
|
||||
{
|
||||
[SerializeField] private MapDatabaseSO _database;
|
||||
|
||||
[Header("Event Channels")]
|
||||
[SerializeField] private StringEventChannelSO _onRoomEntered; // 订阅 EVT_RoomEntered
|
||||
[SerializeField] private StringEventChannelSO _onMapUpdated; // 发布:房间发现时
|
||||
|
||||
// 三级可见性:
|
||||
// Unknown → 未进入过(默认)
|
||||
// Explored → 进入过(显示轮廓/格子)
|
||||
// Mapped → 完整地图信息(购买 MapFragment 或存档点揭示)
|
||||
private HashSet<string> _exploredRooms = new();
|
||||
private HashSet<string> _mappedRooms = new();
|
||||
private readonly CompositeDisposable _subs = new();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (ServiceLocator.GetOrDefault<IMapService>() != null) { Destroy(gameObject); return; }
|
||||
ServiceLocator.Register<IMapService>(this);
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_onRoomEntered?.Subscribe(OnRoomEntered).AddTo(_subs);
|
||||
ServiceLocator.GetOrDefault<ISaveableRegistry>()?.Register(this);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
_subs.Clear();
|
||||
ServiceLocator.GetOrDefault<ISaveableRegistry>()?.Unregister(this);
|
||||
}
|
||||
|
||||
// ── ISaveable ─────────────────────────────────────────────────────────
|
||||
|
||||
public void OnSave(SaveData data)
|
||||
{
|
||||
data.Map.ExploredRooms ??= new List<string>();
|
||||
data.Map.ExploredRooms.Clear();
|
||||
data.Map.ExploredRooms.AddRange(_exploredRooms);
|
||||
data.Map.MappedRooms ??= new List<string>();
|
||||
data.Map.MappedRooms.Clear();
|
||||
data.Map.MappedRooms.AddRange(_mappedRooms);
|
||||
}
|
||||
|
||||
public void OnLoad(SaveData data)
|
||||
{
|
||||
_exploredRooms = new HashSet<string>(data.Map.ExploredRooms ?? new System.Collections.Generic.List<string>());
|
||||
_mappedRooms = new HashSet<string>(data.Map.MappedRooms ?? new System.Collections.Generic.List<string>());
|
||||
}
|
||||
|
||||
// ── 事件驱动房间发现 ──────────────────────────────────────────────────
|
||||
|
||||
private void OnRoomEntered(string roomId)
|
||||
{
|
||||
bool changed = _exploredRooms.Add(roomId);
|
||||
if (changed) _onMapUpdated?.Raise(roomId);
|
||||
}
|
||||
|
||||
/// <summary>标记为已完整获取地图信息(购买 MapFragment SO 触发)。</summary>
|
||||
public void SetMapped(string roomId)
|
||||
{
|
||||
_exploredRooms.Add(roomId);
|
||||
if (_mappedRooms.Add(roomId))
|
||||
_onMapUpdated?.Raise(roomId);
|
||||
}
|
||||
|
||||
// ── 查询 API ──────────────────────────────────────────────────────────
|
||||
|
||||
public bool IsExplored(string roomId) => _exploredRooms.Contains(roomId);
|
||||
public bool IsMapped(string roomId) => _mappedRooms.Contains(roomId);
|
||||
|
||||
public MapDatabaseSO Database => _database;
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
ServiceLocator.Unregister<IMapService>(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user