Files
zeling_v2/Assets/Scripts/World/CollectibleSpawner.cs
2026-05-12 15:34:08 +08:00

59 lines
2.2 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 UnityEngine;
namespace BaseGames.World
{
/// <summary>
/// 可收集物生成器(静态工具类)。
/// 封装 Geo / 道具 Collectible 的实例化逻辑,供 LootResolver 等调用。
/// Prefab 引用通过 CollectibleSpawnerConfig SO 注入,避免 Resources.Load。
/// </summary>
public static class CollectibleSpawner
{
/// <summary>
/// 全局配置引用(由 CollectibleSpawnerConfig.Initialize() 在游戏启动时设置)。
/// </summary>
private static CollectibleSpawnerConfig _config;
/// <summary>由 CollectibleSpawnerConfig.Awake() 注册自身。</summary>
internal static void Register(CollectibleSpawnerConfig config) => _config = config;
/// <summary>
/// 在世界坐标生成 Geo 拾取物。
/// 若配置未注册则仅输出日志(编辑器 / 测试场景兜底)。
/// </summary>
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<Collectible>(out var c))
{
c.SetGeo(amount);
}
}
/// <summary>
/// 在世界坐标生成道具拾取物(通过 itemId 广播 EVT_CollectiblePickup
/// 若配置未注册则仅输出日志。
/// </summary>
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<Collectible>(out var c))
{
c.SetItem(itemId);
}
}
}
}