mirror of
http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite.git
synced 2025-11-20 03:47:13 +08:00
Co-authored-by: m0_75251201 <m0_75251201@noreply.gitcode.com> Reviewed-on: http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite/pulls/57
162 lines
6.5 KiB
C#
162 lines
6.5 KiB
C#
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; // 由和行为树相同的条件回调
|
||
|
||
// =============== 查询函数 ===============
|
||
|
||
/// <summary>
|
||
/// 获取特定事件类型下的所有事件定义。
|
||
/// </summary>
|
||
/// <param name="eventType">要查询的实体事件类型。</param>
|
||
/// <returns>对应事件类型下的 EventDef 数组,如果不存在则返回空数组或 null (取决于设计)。</returns>
|
||
public EventDef[] GetEventsByType(EntityEventType eventType)
|
||
{
|
||
switch (eventType)
|
||
{
|
||
case EntityEventType.OnSpawn:
|
||
return onSpawnEvents ?? Array.Empty<EventDef>();
|
||
case EntityEventType.OnMoveForward:
|
||
return onMoveForwardEvents ?? Array.Empty<EventDef>();
|
||
case EntityEventType.OnMoveBackward:
|
||
return onMoveBackwardEvents ?? Array.Empty<EventDef>();
|
||
case EntityEventType.OnMoveLeft:
|
||
return onMoveLeftEvents ?? Array.Empty<EventDef>();
|
||
case EntityEventType.OnMoveRight:
|
||
return onMoveRightEvents ?? Array.Empty<EventDef>();
|
||
case EntityEventType.OnMeleeAttack:
|
||
return onMeleeAttackEvents ?? Array.Empty<EventDef>();
|
||
case EntityEventType.OnRangedAttack:
|
||
return onRangedAttackEvents ?? Array.Empty<EventDef>();
|
||
case EntityEventType.OnHit:
|
||
return onHitEvents ?? Array.Empty<EventDef>();
|
||
case EntityEventType.OnDeath:
|
||
return onDeathEvents ?? Array.Empty<EventDef>();
|
||
default:
|
||
return Array.Empty<EventDef>(); // 或者抛出异常,或者返回 null
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取所有条件事件定义。
|
||
/// </summary>
|
||
/// <returns>ConditionalEvent 数组,如果不存在则返回空数组。</returns>
|
||
public ConditionalEvent[] GetAllConditionalEvents()
|
||
{
|
||
return conditionalEvents ?? Array.Empty<ConditionalEvent>();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据条件参数获取特定的 ConditionalEvent。
|
||
/// </summary>
|
||
/// <param name="parameterName">条件参数的字符串名称。</param>
|
||
/// <returns>匹配的 ConditionalEvent,如果未找到则返回 null。</returns>
|
||
public ConditionalEvent GetConditionalEventByParameter(string parameterName)
|
||
{
|
||
if (string.IsNullOrEmpty(parameterName) || conditionalEvents == null)
|
||
{
|
||
return null;
|
||
}
|
||
return conditionalEvents.FirstOrDefault(ce => ce.parameter == parameterName);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询所有事件中(包括通用和条件事件)是否存在特定 EventId 的事件。
|
||
/// 这个查询可能比较耗时,取决于事件数量。
|
||
/// </summary>
|
||
/// <param name="eventId">要查询的 EventDef 的 EventId。</param>
|
||
/// <returns>如果找到,则返回第一个匹配的 EventDef,否则返回 null。</returns>
|
||
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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取属于特定事件类型下,并且描述包含特定关键字的所有事件。
|
||
/// </summary>
|
||
/// <param name="eventType">要查询的事件类型。</param>
|
||
/// <param name="keyword">描述中需要包含的关键字。</param>
|
||
/// <returns>匹配的 EventDef 数组。</returns>
|
||
public EventDef[] GetEventsByTypeAndDescriptionKeyword(EntityEventType eventType, string keyword)
|
||
{
|
||
var events = GetEventsByType(eventType);
|
||
if (events == null || string.IsNullOrEmpty(keyword))
|
||
{
|
||
return Array.Empty<EventDef>();
|
||
}
|
||
|
||
return events.Where(e => e is { description: not null } && e.description.Contains(keyword, StringComparison.OrdinalIgnoreCase)).ToArray();
|
||
}
|
||
}
|
||
}
|