using UnityEngine;
namespace BaseGames.World
{
///
/// 可收集物生成器(静态工具类)。
/// 封装 Geo / 道具 Collectible 的实例化逻辑,供 LootResolver 等调用。
/// Prefab 引用通过 CollectibleSpawnerConfig SO 注入,避免 Resources.Load。
///
public static class CollectibleSpawner
{
///
/// 全局配置引用(由 CollectibleSpawnerConfig.Initialize() 在游戏启动时设置)。
///
private static CollectibleSpawnerConfig _config;
/// 由 CollectibleSpawnerConfig.Awake() 注册自身。
internal static void Register(CollectibleSpawnerConfig config) => _config = config;
///
/// 在世界坐标生成 Geo 拾取物。
/// 若配置未注册则仅输出日志(编辑器 / 测试场景兜底)。
///
public static void SpawnGeo(Vector2 position, int amount)
{
if (_config == null || _config.GeoPrefab == null)
{
Debug.LogWarning($"[CollectibleSpawner] GeoPrefab 未配置,Geo x{amount} 无法生成 at {position}");
return;
}
var go = Object.Instantiate(_config.GeoPrefab, position, Quaternion.identity);
if (go.TryGetComponent(out var c))
{
c.SetGeo(amount);
}
}
///
/// 在世界坐标生成道具拾取物(通过 itemId 广播 EVT_CollectiblePickup)。
/// 若配置未注册则仅输出日志。
///
public static void SpawnItem(Vector2 position, string itemId)
{
if (_config == null || _config.ItemPrefab == null)
{
Debug.LogWarning($"[CollectibleSpawner] ItemPrefab 未配置,物品 {itemId} 无法生成 at {position}");
return;
}
var go = Object.Instantiate(_config.ItemPrefab, position, Quaternion.identity);
if (go.TryGetComponent(out var c))
{
c.SetItem(itemId);
}
}
}
}