181 lines
7.8 KiB
C#
181 lines
7.8 KiB
C#
using Data;
|
|
using System.Collections.Generic;
|
|
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();
|
|
}
|
|
}
|
|
} |