(client) feat:实现掉落物,UI显示,维度
This commit is contained in:
@ -4,7 +4,6 @@ using System.Linq;
|
||||
using Base;
|
||||
using Data;
|
||||
using Entity;
|
||||
using Map;
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
@ -46,11 +45,12 @@ namespace Managers
|
||||
|
||||
/// <summary> 子弹实体的预制体。 </summary>
|
||||
public EntityPrefab bulletPrefab;
|
||||
|
||||
public EntityPrefab pickupPrefab;
|
||||
|
||||
/// <summary> 默认实体的预制体,用于生成失败时的回退。 </summary>
|
||||
public EntityPrefab defaultEntityPrefab;
|
||||
|
||||
// 已移除:RegisterDimension 和 UnregisterDimension 方法的相关注释,因为这些方法已不存在且与逻辑无关。
|
||||
|
||||
/// <summary>
|
||||
/// 在指定维度中,根据派系键查找所有实体。
|
||||
@ -238,7 +238,7 @@ namespace Managers
|
||||
string dimensionId,
|
||||
GameObject prefab,
|
||||
Vector3 pos,
|
||||
Data.EntityDef def,
|
||||
EntityDef def,
|
||||
Action<EntityPrefab> extraInit = null)
|
||||
{
|
||||
// 验证维度是否活跃 (通过 Program)。
|
||||
@ -277,7 +277,7 @@ namespace Managers
|
||||
_pendingAdditions.Add(Tuple.Create(dimensionId, factionKey, entityComponent));
|
||||
return entityComponent;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (instantiatedEntity) Destroy(instantiatedEntity);
|
||||
Debug.LogError($"实体管理器:在维度 '{dimensionId}' 中生成实体失败:{ex.Message}\n{ex.StackTrace}");
|
||||
@ -345,7 +345,7 @@ namespace Managers
|
||||
/// <param name="dimensionId">实体所属的维度ID。</param>
|
||||
/// <param name="entityDef">实体定义对象。</param>
|
||||
/// <param name="pos">生成位置。</param>
|
||||
public void GenerateEntity(string dimensionId, Data.EntityDef entityDef, Vector3 pos)
|
||||
public void GenerateEntity(string dimensionId, EntityDef entityDef, Vector3 pos)
|
||||
{
|
||||
if (!characterPrefab)
|
||||
{
|
||||
@ -376,7 +376,7 @@ namespace Managers
|
||||
/// <param name="dimensionId">实体所属的维度ID。</param>
|
||||
/// <param name="buildingDef">建筑定义对象。</param>
|
||||
/// <param name="pos">网格位置。</param>
|
||||
public void GenerateBuildingEntity(string dimensionId, Data.BuildingDef buildingDef, Vector3Int pos)
|
||||
public void GenerateBuildingEntity(string dimensionId, BuildingDef buildingDef, Vector3Int pos)
|
||||
{
|
||||
if (!buildingPrefab)
|
||||
{
|
||||
@ -410,7 +410,7 @@ namespace Managers
|
||||
/// <param name="pos">生成位置。</param>
|
||||
/// <param name="dir">子弹的初始方向。</param>
|
||||
/// <param name="source">发射子弹的源实体。</param>
|
||||
public void GenerateBulletEntity(string dimensionId, Data.BulletDef bulletDef, Vector3 pos, Vector3 dir,
|
||||
public void GenerateBulletEntity(string dimensionId, BulletDef bulletDef, Vector3 pos, Vector3 dir,
|
||||
Entity.Entity source = null)
|
||||
{
|
||||
if (!bulletPrefab)
|
||||
@ -453,7 +453,27 @@ namespace Managers
|
||||
|
||||
if (result == null) GenerateDefaultEntity(dimensionId, pos);
|
||||
}
|
||||
public void GeneratePickupEntity(string dimensionId, ItemDef itemDef, Vector3 pos)
|
||||
{
|
||||
if (!pickupPrefab)
|
||||
{
|
||||
Debug.LogError("实体管理器:pickupPrefab 为空!请分配一个有效的预制体。");
|
||||
GenerateDefaultEntity(dimensionId, pos);
|
||||
return;
|
||||
}
|
||||
|
||||
if (itemDef == null)
|
||||
{
|
||||
Debug.LogError("实体管理器:itemDef 为空!无法生成子弹。");
|
||||
GenerateDefaultEntity(dimensionId, pos);
|
||||
return;
|
||||
}
|
||||
var result=Instantiate(pickupPrefab, pos, Quaternion.identity);
|
||||
var pickup = result.GetComponent<Pickup>();
|
||||
pickup.Init(itemDef);
|
||||
|
||||
if (result == null) GenerateDefaultEntity(dimensionId, pos);
|
||||
}
|
||||
/// <summary>
|
||||
/// 在指定维度和位置生成一个默认实体(通常作为回退选项)。
|
||||
/// </summary>
|
||||
@ -616,7 +636,7 @@ namespace Managers
|
||||
|
||||
// 关系判断:判断当前实体与源实体是否为“敌对”关系。
|
||||
if (affiliationManager.GetRelation(sourceEntityPrefab.entity, currentEntityPrefab.entity) ==
|
||||
Data.Relation.Hostile)
|
||||
Relation.Hostile)
|
||||
{
|
||||
return true; // 找到第一个敌对实体即返回 true,提高效率。
|
||||
}
|
||||
@ -682,7 +702,7 @@ namespace Managers
|
||||
|
||||
// 关系判断:判断当前实体与源实体是否为“敌对”关系。
|
||||
if (affiliationManager.GetRelation(sourceEntityPrefab.entity, currentEntityPrefab.entity) ==
|
||||
Data.Relation.Hostile)
|
||||
Relation.Hostile)
|
||||
{
|
||||
// 距离判断:如果关系敌对,进一步判断其是否在视野范围内。
|
||||
var distanceSqr = Vector3.SqrMagnitude(currentEntityPrefab.transform.position - sourcePos);
|
||||
|
181
Client/Assets/Scripts/Managers/EventManager.cs
Normal file
181
Client/Assets/Scripts/Managers/EventManager.cs
Normal file
@ -0,0 +1,181 @@
|
||||
using System.Collections.Generic;
|
||||
using Data;
|
||||
using UnityEngine;
|
||||
using EventType = Data.EventType;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
// 新增私有结构体,用于在事件队列中存储事件定义及其运行时上下文
|
||||
public struct EventPayload // Make it public if EventManager exposes it directly
|
||||
{
|
||||
public string DimensionId;
|
||||
public EventDef EventDefinition;
|
||||
public Vector3 Position; // 适用于 Character, Bullet, Pickup, DefaultEntity
|
||||
public Vector3 Direction; // 仅适用于 Bullet
|
||||
public Entity.Entity SourceEntity; // 仅适用于 Bullet (发射源)
|
||||
public Vector3Int GridPosition; // 仅适用于 Building
|
||||
}
|
||||
|
||||
public class EventManager : Utils.MonoSingleton<EventManager>
|
||||
{
|
||||
private Queue<EventPayload> _eventQueue = new Queue<EventPayload>();
|
||||
|
||||
private EventManager()
|
||||
{
|
||||
/* Private constructor for singleton */
|
||||
}
|
||||
|
||||
// ===================================
|
||||
// 公共入队方法
|
||||
// ===================================
|
||||
public void EnqueueCharacterSpawnEvent(string dimensionId, EventDef eventDef, Vector3 pos)
|
||||
{
|
||||
if (eventDef == null || eventDef.eventType != EventType.SpawnCharacter ||
|
||||
eventDef.entityDef_Character == null)
|
||||
{
|
||||
Debug.LogError(
|
||||
$"EnqueueCharacterSpawnEvent: Invalid EventDef, mismatched EventType ({eventDef?.eventType}), or missing characterDef for event '{eventDef?.defName ?? "Unknown"}'.");
|
||||
return;
|
||||
}
|
||||
|
||||
_eventQueue.Enqueue(new EventPayload
|
||||
{
|
||||
DimensionId = dimensionId,
|
||||
EventDefinition = eventDef,
|
||||
Position = pos,
|
||||
});
|
||||
Debug.Log($"Event '{eventDef.defName}' (SpawnCharacter) enqueued for dimension {dimensionId} at {pos}.");
|
||||
}
|
||||
|
||||
public void EnqueueBuildingSpawnEvent(string dimensionId, EventDef eventDef, Vector3Int gridPos)
|
||||
{
|
||||
if (eventDef == null || eventDef.eventType != EventType.SpawnBuilding ||
|
||||
eventDef.entityDef_Building == null)
|
||||
{
|
||||
Debug.LogError(
|
||||
$"EnqueueBuildingSpawnEvent: Invalid EventDef, mismatched EventType ({eventDef?.eventType}), or missing buildingDef for event '{eventDef?.defName ?? "Unknown"}'.");
|
||||
return;
|
||||
}
|
||||
|
||||
_eventQueue.Enqueue(new EventPayload
|
||||
{
|
||||
DimensionId = dimensionId,
|
||||
EventDefinition = eventDef,
|
||||
GridPosition = gridPos,
|
||||
});
|
||||
Debug.Log($"Event '{eventDef.defName}' (SpawnBuilding) enqueued for dimension {dimensionId} at grid {gridPos}.");
|
||||
}
|
||||
|
||||
public void EnqueueBulletSpawnEvent(string dimensionId, EventDef eventDef, Vector3 pos, Vector3 dir,
|
||||
Entity.Entity source = null)
|
||||
{
|
||||
if (eventDef == null || eventDef.eventType != EventType.SpawnBullet || eventDef.entityDef_Bullet == null)
|
||||
{
|
||||
Debug.LogError(
|
||||
$"EnqueueBulletSpawnEvent: Invalid EventDef, mismatched EventType ({eventDef?.eventType}), or missing bulletDef for event '{eventDef?.defName ?? "Unknown"}'.");
|
||||
return;
|
||||
}
|
||||
|
||||
_eventQueue.Enqueue(new EventPayload
|
||||
{
|
||||
DimensionId = dimensionId,
|
||||
EventDefinition = eventDef,
|
||||
Position = pos,
|
||||
Direction = dir,
|
||||
SourceEntity = source,
|
||||
});
|
||||
Debug.Log($"Event '{eventDef.defName}' (SpawnBullet) enqueued for dimension {dimensionId} at {pos}, dir {dir}.");
|
||||
}
|
||||
|
||||
public void EnqueuePickupSpawnEvent(string dimensionId, EventDef eventDef, Vector3 pos)
|
||||
{
|
||||
if (eventDef == null || eventDef.eventType != EventType.SpawnPickup || eventDef.entityDef_Pickup == null)
|
||||
{
|
||||
Debug.LogError(
|
||||
$"EnqueuePickupSpawnEvent: Invalid EventDef, mismatched EventType ({eventDef?.eventType}), or missing itemDef for event '{eventDef?.defName ?? "Unknown"}'.");
|
||||
return;
|
||||
}
|
||||
|
||||
_eventQueue.Enqueue(new EventPayload
|
||||
{
|
||||
DimensionId = dimensionId,
|
||||
EventDefinition = eventDef,
|
||||
Position = pos,
|
||||
});
|
||||
Debug.Log($"Event '{eventDef.defName}' (SpawnPickup) enqueued for dimension {dimensionId} at {pos}.");
|
||||
}
|
||||
|
||||
public void EnqueueDefaultEntitySpawnEvent(string dimensionId, EventDef eventDef, Vector3 pos)
|
||||
{
|
||||
if (eventDef == null || eventDef.eventType != EventType.SpawnDefaultEntity)
|
||||
{
|
||||
Debug.LogError(
|
||||
$"EnqueueDefaultEntitySpawnEvent: Invalid EventDef or mismatched EventType ({eventDef?.eventType}) for event '{eventDef?.defName ?? "Unknown"}'.");
|
||||
return;
|
||||
}
|
||||
|
||||
_eventQueue.Enqueue(new EventPayload
|
||||
{
|
||||
DimensionId = dimensionId,
|
||||
EventDefinition = eventDef,
|
||||
Position = pos,
|
||||
});
|
||||
Debug.Log($"Event '{eventDef.defName}' (SpawnDefaultEntity) enqueued for dimension {dimensionId} at {pos}.");
|
||||
}
|
||||
|
||||
// ===================================
|
||||
// 事件处理方法
|
||||
// ===================================
|
||||
/// <summary>
|
||||
/// 处理所有在队列中的待处理事件。
|
||||
/// </summary>
|
||||
public void ProcessEvents()
|
||||
{
|
||||
while (_eventQueue.Count > 0)
|
||||
{
|
||||
EventPayload eventData = _eventQueue.Dequeue();
|
||||
if (!Program.Instance.GetDimension(eventData.DimensionId))
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"Event '{eventData.EventDefinition.defName}' for dimension {eventData.DimensionId} dropped as dimension is no longer active.");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 核心:调用 EntityManage.Instance 的现有公共方法
|
||||
switch (eventData.EventDefinition.eventType)
|
||||
{
|
||||
case EventType.SpawnCharacter:
|
||||
EntityManage.Instance.GenerateEntity(eventData.DimensionId,
|
||||
eventData.EventDefinition.entityDef_Character, eventData.Position);
|
||||
break;
|
||||
case EventType.SpawnBuilding:
|
||||
EntityManage.Instance.GenerateBuildingEntity(eventData.DimensionId,
|
||||
eventData.EventDefinition.entityDef_Building, eventData.GridPosition);
|
||||
break;
|
||||
case EventType.SpawnBullet:
|
||||
EntityManage.Instance.GenerateBulletEntity(eventData.DimensionId,
|
||||
eventData.EventDefinition.entityDef_Bullet, eventData.Position, eventData.Direction,
|
||||
eventData.SourceEntity);
|
||||
break;
|
||||
case EventType.SpawnPickup:
|
||||
EntityManage.Instance.GeneratePickupEntity(eventData.DimensionId,
|
||||
eventData.EventDefinition.entityDef_Pickup, eventData.Position);
|
||||
break;
|
||||
case EventType.SpawnDefaultEntity:
|
||||
EntityManage.Instance.GenerateDefaultEntity(eventData.DimensionId, eventData.Position);
|
||||
break;
|
||||
case EventType.None:
|
||||
default:
|
||||
Debug.LogWarning(
|
||||
$"EventManager: Unhandled or invalid event type: {eventData.EventDefinition.eventType} for event '{eventData.EventDefinition.defName}'.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Managers/EventManager.cs.meta
Normal file
3
Client/Assets/Scripts/Managers/EventManager.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fe7c735088384400a50b418f85252c70
|
||||
timeCreated: 1756268066
|
Reference in New Issue
Block a user