Files
Gen_Hack-and-Slash-Roguelite/Client/Assets/Scripts/EventWorkClass/Event_MessageWorkClass.cs

116 lines
5.3 KiB
C#
Raw Normal View History

using System;
using Data;
using Managers;
using Newtonsoft.Json;
using UnityEngine;
// Unity 环境使用 UnityEngine.Color 和 Debug.Log
namespace EventWorkClass
{
/// <summary>
/// 表示消息事件的配置数据结构,从 JSON 反序列化而来。
/// </summary>
public class MessageData
{
/// <summary>
/// 要显示的消息内容。这是必填字段。
/// </summary>
[JsonProperty("message", Required = Required.Always)]
public string Message { get; set; }
/// <summary>
/// 消息的显示类别决定了消息在UI中的呈现方式。这是必填字段。
/// </summary>
[JsonProperty("type", Required = Required.Always)]
public PromptDisplayCategory Type { get; set; }
/// <summary>
/// 消息的颜色可以是一个HTML颜色字符串例如"#RRGGBB"或"#RRGGBBAA")。可选。
/// </summary>
[JsonProperty("color")]
public string ColorHex { get; set; }
/// <summary>
/// 消息的显示时长(秒)。可选,如果未指定,将使用 DisplayMessage API 的默认值。
/// </summary>
[JsonProperty("showTime")]
public float? ShowTime { get; set; }
}
/// <summary>
/// 一个继承自 EventWorkClassBase 的类,用于处理在游戏中显示消息的事件。
/// 通过 JSON 配置来指定消息内容、显示方式、颜色和时长。
/// </summary>
public class Event_MessageWorkClass : EventWorkClassBase
{
private MessageData _messageData;
private Color? _parsedColor; // 存储解析后的 UnityEngine.Color
/// <summary>
/// 初始化消息工作类。通过解析 JSON 字符串来配置消息显示参数。
/// JSON 必须包含 "message" 和 "type" 字段,可以可选地包含 "color" 和 "showTime"。
/// </summary>
/// <param name="value">包含消息显示配置的 JSON 字符串。</param>
/// <exception cref="ArgumentNullException">当 JSON 字符串为空或null时抛出。</exception>
/// <exception cref="JsonSerializationException">当 JSON 字符串格式不正确时抛出。</exception>
/// <exception cref="InvalidOperationException">当 JSON 中缺少必要的字段或颜色解析失败时抛出。</exception>
public override void Init(string value)
{
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentNullException(nameof(value), "Event_MessageWorkClass 初始化 JSON 不能为空。");
try
{
_messageData = JsonConvert.DeserializeObject<MessageData>(value);
}
catch (JsonException ex)
{
throw new JsonSerializationException(
"Event_MessageWorkClass 配置 JSON 反序列化失败。请检查 JSON 格式,尤其是枚举值。", ex
);
}
// 反序列化后进行业务逻辑验证
if (_messageData == null) throw new InvalidOperationException("消息数据无法反序列化。结果对象为空。");
if (string.IsNullOrWhiteSpace(_messageData.Message))
throw new InvalidOperationException("消息配置缺少必需字段 'message'。");
// 解析颜色字符串(如果存在)
if (!string.IsNullOrWhiteSpace(_messageData.ColorHex))
{
if (ColorUtility.TryParseHtmlString(_messageData.ColorHex, out var parsedColor))
{
_parsedColor = parsedColor;
}
else
{
Debug.LogError(
$"[Event_MessageWorkClass] 无法解析消息颜色十六进制字符串 '{_messageData.ColorHex}'。请使用有效的 HTML 颜色格式(例如 '#RRGGBB' 或 '#RRGGBBAA')。");
throw new InvalidOperationException(
$"无法解析消息颜色十六进制字符串 '{_messageData.ColorHex}'。请使用有效的 HTML 颜色格式(例如 '#RRGGBB' 或 '#RRGGBBAA')。");
}
}
}
/// <summary>
/// 执行消息显示操作。
/// </summary>
/// <param name="dimensionID">当前维度ID此事件通常不直接使用但作为 EventWorkClassBase 的Run方法签名。</param>
/// <param name="initiator">触发此事件的实体,此事件通常不直接使用,但作为 EventWorkClassBase 的Run方法签名。</param>
/// <exception cref="InvalidOperationException">当类未初始化时抛出。</exception>
public override void Run(string dimensionID, Entity.Entity initiator = null)
{
// 确保 Init 方法已经成功调用
if (_messageData == null) throw new InvalidOperationException("Event_MessageWorkClass 尚未初始化。请先调用 Init()。");
MessageManager.Instance.DisplayMessage(
_messageData.Message,
_messageData.Type,
_parsedColor, // 直接传递解析后的Color?
_messageData.ShowTime.GetValueOrDefault(3f) // 如果 ShowTime 为 null则使用 3f
);
}
}
}