(clienr) feat:事件定义
This commit is contained in:
@ -1,181 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Data;
|
||||
using Entity;
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
using EventType = Data.EventType;
|
||||
using Utils;
|
||||
using Random = System.Random;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
// 新增私有结构体,用于在事件队列中存储事件定义及其运行时上下文
|
||||
public struct EventPayload // Make it public if EventManager exposes it directly
|
||||
class EventManager:Singleton<EventManager>,ILaunchManager
|
||||
{
|
||||
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
|
||||
}
|
||||
private static Random _random = new();
|
||||
public EventDef[] EventDefs { get; private set; }
|
||||
public string StepDescription => "正在载入事件";
|
||||
|
||||
public class EventManager : Utils.MonoSingleton<EventManager>
|
||||
{
|
||||
private Queue<EventPayload> _eventQueue = new Queue<EventPayload>();
|
||||
|
||||
private EventManager()
|
||||
public void Init()
|
||||
{
|
||||
/* 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"}'.");
|
||||
if(EventDefs!=null)
|
||||
return;
|
||||
}
|
||||
|
||||
_eventQueue.Enqueue(new EventPayload
|
||||
{
|
||||
DimensionId = dimensionId,
|
||||
EventDefinition = eventDef,
|
||||
Position = pos,
|
||||
});
|
||||
Debug.Log($"Event '{eventDef.defName}' (SpawnCharacter) enqueued for dimension {dimensionId} at {pos}.");
|
||||
EventDefs = DefineManager.Instance.QueryDefinesByType<EventDef>();
|
||||
}
|
||||
|
||||
public void EnqueueBuildingSpawnEvent(string dimensionId, EventDef eventDef, Vector3Int gridPos)
|
||||
public void Clear()
|
||||
{
|
||||
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"}'.");
|
||||
EventDefs = null;
|
||||
}
|
||||
|
||||
public static void ExecuteEvent(EventDef eventDef)
|
||||
{
|
||||
if(eventDef == null)
|
||||
return;
|
||||
}
|
||||
|
||||
_eventQueue.Enqueue(new EventPayload
|
||||
if (eventDef.hediffEvent != null)
|
||||
{
|
||||
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))
|
||||
var entityList = EntityManage.Instance.FindEntitiesByFaction(Program.Instance.FocusedDimensionId,
|
||||
eventDef.hediffEvent.affiliation.defName);
|
||||
List<Entity.Entity> filteredEntitiesTraditional = new();
|
||||
foreach (var prefab in entityList)
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"Event '{eventData.EventDefinition.defName}' for dimension {eventData.DimensionId} dropped as dimension is no longer active.");
|
||||
continue;
|
||||
var entity = prefab.entity;
|
||||
if (entity is Character || entity is Monster)
|
||||
{
|
||||
filteredEntitiesTraditional.Add(entity);
|
||||
}
|
||||
}
|
||||
|
||||
// 核心:调用 EntityManage.Instance 的现有公共方法
|
||||
switch (eventData.EventDefinition.eventType)
|
||||
var selectedElements = SelectRandomElements_FisherYates(filteredEntitiesTraditional, eventDef.hediffEvent.specificPawnCount);
|
||||
foreach (var selectedElement in selectedElements)
|
||||
{
|
||||
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()
|
||||
public static List<T> SelectRandomElements_FisherYates<T>(List<T> sourceList, int count)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
if (sourceList == null) throw new ArgumentNullException(nameof(sourceList));
|
||||
if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), "Count cannot be negative.");
|
||||
if (count == 0) return new List<T>();
|
||||
if (count >= sourceList.Count) return new List<T>(sourceList); // 如果n大于等于列表大小,则返回所有元素副本
|
||||
|
||||
List<T> result = new List<T>(count); // 预分配容量
|
||||
List<T> temp = new List<T>(sourceList); // 创建一个副本以避免修改原始列表
|
||||
|
||||
int n = temp.Count;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
int k = _random.Next(i, n);
|
||||
result.Add(temp[k]);
|
||||
(temp[k], temp[i]) = (temp[i], temp[k]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user