using System; using System.Collections.Generic; using Data; using Managers; using UnityEngine; using System.Linq; using Entity; using Map; // 用于 LINQ namespace EventWorkClass { /// /// Event_GiveHediff 事件的配置数据结构。 /// [Serializable] public class GiveHediffConfig { /// /// 要添加或移除的 Hediff 的定义名 (defName)。 /// public string HediffDefName; /// /// 要添加或移除的 Hediff 定义的具体类型名,例如 "HediffDef" 或其子类名。 /// 用于 DefineManager 的严格类型查找。 /// public string HediffDefTypeName; /// /// 要执行的操作类型:添加或移除 Hediff。 /// public HediffActionType ActionType; /// /// 目标实体选择类型。 /// public TargetEntitySelectionType TargetSelectionType; // 以下是各种 TargetSelectionType 可能需要的配置参数 /// /// 用于 SpecificEntityById 类型:目标实体的唯一ID。 /// public string TargetEntityId; /// /// 用于 AllEntitiesInFaction 或 TargetSpecificFactionLeader 类型:目标派系的定义名 (factionDefName)。 /// public string TargetFactionDefName; /// /// 用于 AroundSpecificCoordinates 类型:生成中心坐标。 /// public Vector3 CenterCoordinates; /// /// 用于 AroundSpecificCoordinates 类型:搜索半径。 /// public float Radius = 10f; // 默认半径 } /// /// 定义针对 Hediff 的操作类型。 /// public enum HediffActionType { /// /// 添加 Hediff。 /// Add, /// /// 移除 Hediff。 /// Remove } /// /// 定义目标实体选择的类型。 /// public enum TargetEntitySelectionType { /// /// 未指定或无效的目标选择类型。 /// None = 0, /// /// 目标是当前玩家(如果存在)。 /// Player, /// /// 目标是指定派系的所有活动实体。 /// AllEntitiesInFaction, /// /// 目标是指定坐标周围半径内的所有生物。 /// AroundSpecificCoordinates } /// /// Event_GiveHediff 事件类:用于给予或移除健康状态 (Hediff)。 /// public class Event_GiveHediff : EventWorkClassBase { private GiveHediffConfig _config; private HediffDef _aimHediffDef; /// /// 初始化 Hediff 给予/移除事件。 /// /// 包含事件配置的JSON字符串。 public override void Init(string value) { if (string.IsNullOrEmpty(value)) { Debug.LogError("Event_GiveHediff: 初始化值为空或null。请提供一个JSON配置字符串。"); return; } try { _config = JsonUtility.FromJson(value); if (_config == null) { Debug.LogError($"Event_GiveHediff: 无法解析配置JSON: {value}"); return; } if (string.IsNullOrEmpty(_config.HediffDefTypeName) || string.IsNullOrEmpty(_config.HediffDefName)) { Debug.LogError($"Event_GiveHediff: Hediff定义类型名或名称为空或null (名称: '{_config.HediffDefName}', 类型: '{_config.HediffDefTypeName}')。无法查找Hediff定义。"); return; } _aimHediffDef = (HediffDef)DefineManager.Instance.FindDefine(_config.HediffDefTypeName, _config.HediffDefName); if (_aimHediffDef == null) { Debug.LogError($"Event_GiveHediff: 未找到Hediff定义 (名称: '{_config.HediffDefName}', 类型: '{_config.HediffDefTypeName}')。请检查配置。"); } } catch (Exception ex) { Debug.LogError($"Event_GiveHediff: 解析配置JSON时出错: {value}。异常信息: {ex.Message}"); } } /// /// 运行 Hediff 给予/移除事件。 /// /// 目标实体所在的维度ID。 public override void Run(string dimensionID) { if (_config == null) { Debug.LogError("Event_GiveHediff: 事件配置(_config)为空。Init()可能失败了。"); return; } if (_aimHediffDef == null) { Debug.LogError($"Event_GiveHediff: 目标Hediff定义为空 (名称: {_config.HediffDefName}, 类型: {_config.HediffDefTypeName})。无法执行操作。"); return; } var targetEntities = GetTargetEntities(dimensionID); if (targetEntities == null || targetEntities.Count == 0) { Debug.LogWarning($"Event_GiveHediff: 在维度 '{dimensionID}' 中未找到符合条件的目标实体 (选择类型: {_config.TargetSelectionType})。"); return; } var successCount = 0; var failedCount = 0; var hediffInstance = new Hediff(_aimHediffDef); // 创建 Hediff 实例 foreach (var entity in targetEntities) { if (entity is LivingEntity livingEntity) { try { if (_config.ActionType == HediffActionType.Add) { livingEntity.AddHediff(hediffInstance); Debug.Log($"Event_GiveHediff: 已向实体 '{livingEntity.name}' 添加 Hediff '{_aimHediffDef.defName}'。"); successCount++; } else if (_config.ActionType == HediffActionType.Remove) { livingEntity.RemoveHediff(hediffInstance); Debug.Log($"Event_GiveHediff: 已从实体 '{livingEntity.name}' 移除 Hediff '{_aimHediffDef.defName}'。"); successCount++; } } catch (Exception ex) { Debug.LogError($"Event_GiveHediff: 对实体 '{entity.name}' 执行 Hediff 操作时出错: {ex.Message}"); failedCount++; } } else { Debug.LogWarning($"Event_GiveHediff: 实体 '{entity.name}' (类型: {entity.GetType().Name}) 不是 LivingEntity,无法添加/移除 Hediff。"); failedCount++; } } Debug.Log($"Event_GiveHediff: 完成操作。成功: {successCount},失败: {failedCount} (Hediff: {_aimHediffDef.defName}, 操作: {_config.ActionType})。"); } /// /// 根据配置获取目标实体列表。 /// /// 维度ID。 /// 符合条件的目标实体列表。 private List GetTargetEntities(string dimensionID) { var entities = new List(); var dimension = Program.Instance.GetDimension(dimensionID); if (dimension == null) { Debug.LogError($"Event_GiveHediff: 未找到维度 '{dimensionID}'。无法获取目标实体。"); return entities; } switch (_config.TargetSelectionType) { case TargetEntitySelectionType.Player: // 假设 PlayerManager.Instance.GetPlayerEntity(dimensionID) 返回 LivingEntity // 或者你的玩家实体本身就是 LivingEntity var player = dimension.focusEntity as LivingEntity; // 假设玩家是维度的一部分且是LivingEntity if (player != null) { entities.Add(player); } else { Debug.LogWarning($"Event_GiveHediff: 未能找到维度 '{dimensionID}' 中的玩家实体或玩家不是 LivingEntity。"); } break; case TargetEntitySelectionType.AllEntitiesInFaction: if (string.IsNullOrEmpty(_config.TargetFactionDefName)) { Debug.LogWarning("Event_GiveHediff: 配置了 'AllEntitiesInFaction',但 'TargetFactionDefName' 为空。"); return entities; } entities.AddRange(EntityManage.Instance .FindEntitiesByFaction(dimensionID, _config.TargetFactionDefName) .Select(item => item.entity)); break; case TargetEntitySelectionType.AroundSpecificCoordinates: // 假设 EntityManage 提供了根据位置和半径查找实体的方法 // 或者可以直接通过场景查询(例如 Physics.OverlapSphere 或者自定义管理系统) // 为了简化,我将假设 EntityManage 提供 FindEntitiesAroundRadius 方法 var allEntitiesInDimension = EntityManage.Instance.GetAllEntities(dimensionID) .Select(item => item.entity); var center = _config.CenterCoordinates; var radius = _config.Radius; foreach (var ent in allEntitiesInDimension) { // 确保实体有 Transform 且在半径内 if (ent != null && ent.transform != null && Vector3.Distance(ent.transform.position, center) <= radius) { entities.Add(ent); } } break; case TargetEntitySelectionType.None: default: Debug.LogWarning($"Event_GiveHediff: 未知或不支持的目标实体选择类型: {_config.TargetSelectionType}。"); break; } return entities; } } }