摄像机区域的架构改动
This commit is contained in:
64
Assets/_Game/Scripts/World/CollectibleSpawner.cs
Normal file
64
Assets/_Game/Scripts/World/CollectibleSpawner.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using UnityEngine;
|
||||
using BaseGames.Core;
|
||||
using BaseGames.Core.Assets;
|
||||
|
||||
namespace BaseGames.World
|
||||
{
|
||||
/// <summary>
|
||||
/// 可收集物生成器(静态工具类)。
|
||||
/// 封装 LingZhu / 道具 Collectible 的 Spawn 逻辑,供 LootResolver 等调用。
|
||||
/// 优先通过 IObjectPoolService 从对象池取用(需预热 COL_LingZhu / COL_Item);
|
||||
/// 池服务不可用时退回 Object.Instantiate(仅限编辑器 / 单元测试场景)。
|
||||
/// Prefab 引用通过 CollectibleSpawnerConfig 注入,避免 Resources.Load。
|
||||
/// </summary>
|
||||
public static class CollectibleSpawner
|
||||
{
|
||||
/// <summary>
|
||||
/// 全局配置引用(由 CollectibleSpawnerConfig.Awake() 注册)。
|
||||
/// </summary>
|
||||
private static CollectibleSpawnerConfig _config;
|
||||
|
||||
/// <summary>由 CollectibleSpawnerConfig.Awake() 注册自身。</summary>
|
||||
internal static void Register(CollectibleSpawnerConfig config) => _config = config;
|
||||
|
||||
/// <summary>
|
||||
/// 在世界坐标生成 LingZhu 拾取物。
|
||||
/// 优先从 GlobalObjectPool 取用(key = AddressKeys.PrefabCollectibleLingZhu),
|
||||
/// 池服务不可用时退回 Object.Instantiate。
|
||||
/// </summary>
|
||||
public static void SpawnLingZhu(Vector2 position, int amount)
|
||||
{
|
||||
var go = SpawnFromPool(AddressKeys.PrefabCollectibleLingZhu, position)
|
||||
?? InstantiateFallback(_config?.LingZhuPrefab, position,
|
||||
$"[CollectibleSpawner] LingZhuPrefab 未配置,LingZhu x{amount} 无法生成 at {position}");
|
||||
if (go != null && go.TryGetComponent<Collectible>(out var c))
|
||||
c.SetLingZhu(amount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在世界坐标生成道具拾取物(通过 itemId 广播 EVT_CollectiblePickup)。
|
||||
/// 优先从 GlobalObjectPool 取用(key = AddressKeys.PrefabCollectibleItem),
|
||||
/// 池服务不可用时退回 Object.Instantiate。
|
||||
/// </summary>
|
||||
public static void SpawnItem(Vector2 position, string itemId)
|
||||
{
|
||||
var go = SpawnFromPool(AddressKeys.PrefabCollectibleItem, position)
|
||||
?? InstantiateFallback(_config?.ItemPrefab, position,
|
||||
$"[CollectibleSpawner] ItemPrefab 未配置,物品 {itemId} 无法生成 at {position}");
|
||||
if (go != null && go.TryGetComponent<Collectible>(out var c))
|
||||
c.SetItem(itemId);
|
||||
}
|
||||
|
||||
// ── 内部工具 ──────────────────────────────────────────────────────
|
||||
|
||||
private static GameObject SpawnFromPool(string key, Vector2 position)
|
||||
=> ServiceLocator.GetOrDefault<IObjectPoolService>()
|
||||
?.Spawn(key, position, Quaternion.identity);
|
||||
|
||||
private static GameObject InstantiateFallback(GameObject prefab, Vector2 position, string warnMsg)
|
||||
{
|
||||
if (prefab == null) { Debug.LogWarning(warnMsg); return null; }
|
||||
return Object.Instantiate(prefab, position, Quaternion.identity);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user