using System; using System.Linq; // 用于 LINQ 查询 namespace Data { public class ConditionalEvent : Define { public string parameter; public EventDef[] eventDefs; } // 实体事件类型枚举 public enum EntityEventType { None = 0, OnSpawn, OnMoveForward, OnMoveBackward, OnMoveLeft, OnMoveRight, OnMeleeAttack, OnRangedAttack, OnHit, OnDeath, } public class EntityEventDef : Define { public EventDef[] onSpawnEvents; // 生成时触发的事件 public EventDef[] onMoveForwardEvents; // 向前移动时触发的事件 public EventDef[] onMoveBackwardEvents; // 向后移动时触发的事件 public EventDef[] onMoveLeftEvents; // 向左移动时触发的事件 public EventDef[] onMoveRightEvents; // 向右移动时触发的事件 public EventDef[] onMeleeAttackEvents; // 近战攻击时触发的事件 public EventDef[] onRangedAttackEvents; // 远程攻击时触发的事件 public EventDef[] onHitEvents; // 受击时触发的事件 public EventDef[] onDeathEvents; // 死亡时触发的事件 public ConditionalEvent[] conditionalEvents; // 由和行为树相同的条件回调 // =============== 查询函数 =============== /// /// 获取特定事件类型下的所有事件定义。 /// /// 要查询的实体事件类型。 /// 对应事件类型下的 EventDef 数组,如果不存在则返回空数组或 null (取决于设计)。 public EventDef[] GetEventsByType(EntityEventType eventType) { switch (eventType) { case EntityEventType.OnSpawn: return onSpawnEvents ?? Array.Empty(); case EntityEventType.OnMoveForward: return onMoveForwardEvents ?? Array.Empty(); case EntityEventType.OnMoveBackward: return onMoveBackwardEvents ?? Array.Empty(); case EntityEventType.OnMoveLeft: return onMoveLeftEvents ?? Array.Empty(); case EntityEventType.OnMoveRight: return onMoveRightEvents ?? Array.Empty(); case EntityEventType.OnMeleeAttack: return onMeleeAttackEvents ?? Array.Empty(); case EntityEventType.OnRangedAttack: return onRangedAttackEvents ?? Array.Empty(); case EntityEventType.OnHit: return onHitEvents ?? Array.Empty(); case EntityEventType.OnDeath: return onDeathEvents ?? Array.Empty(); default: return Array.Empty(); // 或者抛出异常,或者返回 null } } /// /// 获取所有条件事件定义。 /// /// ConditionalEvent 数组,如果不存在则返回空数组。 public ConditionalEvent[] GetAllConditionalEvents() { return conditionalEvents ?? Array.Empty(); } /// /// 根据条件参数获取特定的 ConditionalEvent。 /// /// 条件参数的字符串名称。 /// 匹配的 ConditionalEvent,如果未找到则返回 null。 public ConditionalEvent GetConditionalEventByParameter(string parameterName) { if (string.IsNullOrEmpty(parameterName) || conditionalEvents == null) { return null; } return conditionalEvents.FirstOrDefault(ce => ce.parameter == parameterName); } /// /// 查询所有事件中(包括通用和条件事件)是否存在特定 EventId 的事件。 /// 这个查询可能比较耗时,取决于事件数量。 /// /// 要查询的 EventDef 的 EventId。 /// 如果找到,则返回第一个匹配的 EventDef,否则返回 null。 public EventDef FindEventDefById(string eventId) { if (string.IsNullOrEmpty(eventId)) { return null; } // 查询通用事件 foreach (EntityEventType type in Enum.GetValues(typeof(EntityEventType))) { if (type == EntityEventType.None) continue; // 排除 None 类型 var events = GetEventsByType(type); if (events != null) { var foundEvent = events.Where(e => e != null).FirstOrDefault(e => e.defName == eventId); if (foundEvent != null) { return foundEvent; } } } // 查询条件事件 if (conditionalEvents != null) { foreach (var condEvent in conditionalEvents) { if (condEvent?.eventDefs != null) { var foundEvent = condEvent.eventDefs.Where(e => e != null).FirstOrDefault(e => e.defName == eventId); if (foundEvent != null) { return foundEvent; } } } } return null; } /// /// 获取属于特定事件类型下,并且描述包含特定关键字的所有事件。 /// /// 要查询的事件类型。 /// 描述中需要包含的关键字。 /// 匹配的 EventDef 数组。 public EventDef[] GetEventsByTypeAndDescriptionKeyword(EntityEventType eventType, string keyword) { var events = GetEventsByType(eventType); if (events == null || string.IsNullOrEmpty(keyword)) { return Array.Empty(); } return events.Where(e => e is { description: not null } && e.description.Contains(keyword, StringComparison.OrdinalIgnoreCase)).ToArray(); } } }