(client) feat:添加消息定义,添加故事定义及其运算,武器动画可用,装备UI可用以及切换武器 fix:修复快速攻击导致协程释放出错卡死,重构为计时器,修复类型转换错误导致报错

This commit is contained in:
m0_75251201
2025-09-08 00:13:12 +08:00
parent 15cdd2b244
commit 379ed07773
39 changed files with 8353 additions and 937 deletions

View File

@ -1,8 +1,10 @@
using System;
using System.Collections.Generic; // 新增用于List<T>
using Data;
using Managers;
using UnityEngine;
using Random = UnityEngine.Random;
using Newtonsoft.Json; // 新增Newtonsoft.Json的引用
namespace EventWorkClass
{
@ -13,14 +15,15 @@ namespace EventWorkClass
public class EntityGenerateConfig
{
/// <summary>
/// 要生成的实体定义名 (defName)
/// 要生成的实体定义列表,将从中随机选择
/// </summary>
public string EntityDefName;
public List<EntityDefinitionEntry> DefinitionsToChooseFrom;
/// <summary>
/// 要生成的实体定义的具体类型名,例如 "CharacterDef" 或 "MonsterDef"
/// 用于 DefineManager 的严格类型查找。
/// 要生成的实体总数
/// </summary>
public string EntityDefTypeName;
public int Count = 1; // 默认生成一个
/// <summary>
/// 生成位置类型。
/// </summary>
@ -37,7 +40,7 @@ namespace EventWorkClass
/// <summary>
/// 用于 InsideSpecificBuildingType 类型目标建筑的类型ID。
/// </summary>
public string BuildingTypeId;
public string BuildingTypeDefName;
/// <summary>
/// 用于 AroundTargetEntity 类型:目标派系的定义名 (factionDefName)。
/// </summary>
@ -45,12 +48,29 @@ namespace EventWorkClass
/// <summary>
/// 用于 AtPredefinedSpawnPoint 类型预定义生成点的ID。
/// </summary>
public string SpawnPointId;
public string SpawnPointTileMapDefName;
/// <summary>
/// 用于 OffMap 类型:距离地图边界的额外偏移量。
/// </summary>
public float OffMapOffset = 5f;
}
/// <summary>
/// 单个实体定义的配置条目。
/// </summary>
[Serializable] // 保持可序列化以便在Unity Inspector中显示
public class EntityDefinitionEntry
{
/// <summary>
/// 实体定义名 (defName)。
/// </summary>
public string DefName;
/// <summary>
/// 实体定义的具体类型名。
/// </summary>
public string DefTypeName;
}
/// <summary>
/// 定义生成器生成地图实体的可能位置类型。
/// </summary>
@ -91,10 +111,11 @@ namespace EventWorkClass
/// </summary>
AtPredefinedSpawnPoint
}
public class Event_EntityGenerater : EventWorkClassBase
{
private EntityGenerateConfig _config;
private EntityDef _aimEntity;
private List<EntityDef> _validatedEntityDefs; // 用于存储所有已验证的实体定义
/// <summary>
/// 初始化实体生成器事件。
@ -109,26 +130,46 @@ namespace EventWorkClass
}
try
{
_config = JsonUtility.FromJson<EntityGenerateConfig>(value);
if (_config == null)
{
Debug.LogError($"无法解析配置JSON: {value}");
return;
}
if (string.IsNullOrEmpty(_config.EntityDefTypeName))
{
Debug.LogError($"实体定义类型名为空或null (实体定义名: '{_config.EntityDefName}')。无法查找实体定义。");
return;
}
_aimEntity = (EntityDef)DefineManager.Instance.FindDefine(_config.EntityDefTypeName,_config.EntityDefName);
if (_aimEntity == null)
{
Debug.LogError($"未找到实体定义 (名称: '{_config.EntityDefName}', 类型: '{_config.EntityDefTypeName}')。请检查配置。");
}
_config = JsonConvert.DeserializeObject<EntityGenerateConfig>(value); // 使用Newtonsoft.Json
}
catch (Exception ex)
{
Debug.LogError($"解析配置JSON时出错: {value}。异常信息: {ex.Message}");
return; // 解析失败,直接返回
}
if (_config == null)
{
Debug.LogError($"无法解析配置JSON: {value}");
return;
}
if (_config.DefinitionsToChooseFrom == null || _config.DefinitionsToChooseFrom.Count == 0)
{
Debug.LogError($"实体生成配置中没有定义任何要生成的实体。请检查 'DefinitionsToChooseFrom' 列表。");
return;
}
_validatedEntityDefs = new List<EntityDef>();
foreach (var entry in _config.DefinitionsToChooseFrom)
{
if (string.IsNullOrEmpty(entry.DefTypeName))
{
Debug.LogWarning($"实体定义类型名为空或null (实体定义名: '{entry.DefName}')。跳过此定义。");
continue;
}
var entityDef = (EntityDef)DefineManager.Instance.FindDefine(entry.DefTypeName, entry.DefName);
if (entityDef == null)
{
Debug.LogWarning($"未找到实体定义 (名称: '{entry.DefName}', 类型: '{entry.DefTypeName}')。请检查配置。跳过此定义。");
}
else
{
_validatedEntityDefs.Add(entityDef);
}
}
if (_validatedEntityDefs.Count == 0)
{
Debug.LogError($"所有配置的实体定义都无效或未找到。事件初始化失败。");
}
}
@ -143,33 +184,39 @@ namespace EventWorkClass
Debug.LogError("事件配置(_config)为空。Init()可能失败了。");
return;
}
if (_aimEntity == null)
if (_validatedEntityDefs == null || _validatedEntityDefs.Count == 0)
{
Debug.LogError($"目标实体定义为空 (名称: {_config.EntityDefName}, 类型: {_config.EntityDefTypeName})。无法生成实体。");
Debug.LogError("没有有效实体定义可供生成。请检查Init()方法和配置。");
return;
}
var position = GetPosition(dimensionID);
// 检查 GetPosition 是否返回了有效的非零位置,除非它是 AroundSpecificCoordinates 且中心点就是 Vector3.zero
if (position == Vector3.zero && (_config.LocationType != EntitySpawnLocationType.AroundSpecificCoordinates || _config.CenterCoordinates != Vector3.zero))
for (int i = 0; i < _config.Count; i++)
{
Debug.LogWarning($"未能为类型 {_config.LocationType} 获取有效的生成位置。实体可能在原点 (0,0,0) 生成。");
// 随机选择一个实体定义
var selectedEntityDef = _validatedEntityDefs[Random.Range(0, _validatedEntityDefs.Count)];
var position = GetPosition(dimensionID);
// 检查 GetPosition 是否返回了有效的非零位置,除非它是 AroundSpecificCoordinates 且中心点就是 Vector3.zero
if (position == Vector3.zero && (_config.LocationType != EntitySpawnLocationType.AroundSpecificCoordinates
|| _config.CenterCoordinates != Vector3.zero))
{
Debug.LogWarning($"未能为类型 {_config.LocationType} 获取有效的生成位置。实体可能在原点 (0,0,0) 生成。");
}
if (selectedEntityDef is CharacterDef characterDef)
{
EntityManage.Instance.GenerateEntity(dimensionID, characterDef, position);
continue;
}
if (selectedEntityDef is MonsterDef monsterDef)
{
EntityManage.Instance.GenerateMonsterEntity(dimensionID, monsterDef, position);
continue;
}
Debug.LogWarning($"目标实体 '{selectedEntityDef.defName}' (类型: {selectedEntityDef.GetType().Name}) 既不是 CharacterDef 也不是 MonsterDef。" +
$"如果你想生成其他类型EntityManage需要一个通用的生成实体方法。没有生成此实体。");
}
if (_aimEntity is CharacterDef characterDef)
{
EntityManage.Instance.GenerateEntity(dimensionID, characterDef, position);
Debug.Log($"已在维度 {dimensionID} 的 {position} 位置生成角色 '{characterDef.defName}'。");
return;
}
if (_aimEntity is MonsterDef monsterDef)
{
EntityManage.Instance.GenerateMonsterEntity(dimensionID, monsterDef, position);
Debug.Log($"已在维度 {dimensionID} 的 {position} 位置生成怪物 '{monsterDef.defName}'。");
return;
}
Debug.LogWarning($"目标实体 '{_aimEntity.defName}' (类型: {_aimEntity.GetType().Name}) 既不是 CharacterDef 也不是 MonsterDef。" +
$"如果你想生成其他类型EntityManage需要一个通用的生成实体方法。没有生成任何实体。");
}
/// <summary>
@ -287,12 +334,12 @@ namespace EventWorkClass
}
case EntitySpawnLocationType.InsideSpecificBuildingType:
{
Debug.LogWarning($"类型为 'InsideSpecificBuildingType' ({_config.BuildingTypeId}) 的生成逻辑尚未实现。返回 Vector3.zero。");
Debug.LogWarning($"类型为 'InsideSpecificBuildingType' ({_config.BuildingTypeDefName}) 的生成逻辑尚未实现。返回 Vector3.zero。");
return Vector3.zero;
}
case EntitySpawnLocationType.AtPredefinedSpawnPoint:
{
Debug.LogWarning($"类型为 'AtPredefinedSpawnPoint' ({_config.SpawnPointId}) 的生成逻辑尚未实现。返回 Vector3.zero。");
Debug.LogWarning($"类型为 'AtPredefinedSpawnPoint' ({_config.SpawnPointTileMapDefName}) 的生成逻辑尚未实现。返回 Vector3.zero。");
return Vector3.zero;
}
case EntitySpawnLocationType.None: