EnergyEfficiencyManagement/Assets/Zion/Scripts/Dialogue/DialogueEventSystem.cs

96 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 对话事件系统,用于处理游戏中的对话事件。
/// </summary>
public class DialogueEventSystem : MonoBehaviour
{
/// <summary>
/// 事件动作类,用于存储事件名称、角色和选择结果映射。
/// </summary>
[System.Serializable]
public class EventAction
{
public string eventName;
public string eventRole;
public System.Action<int> onChoiceSelected; //选择回调
public Dictionary<int, string> choiceResults; //选择结果映射
}
//事件注册表,用于存储事件名称和对应的事件动作对象。
private Dictionary<string, EventAction> eventRegistry = new Dictionary<string, EventAction>();
private void Start()
{
// 示例:注册一个事件
RegisterEvent("event1", "role1", (int choice) =>
{
Debug.Log("选择了选项:" + choice);
//TODO: 这里可以添加选择后的逻辑处理,例如根据选择的选项执行不同的操作。
// 示例:添加选择结果
eventRegistry["event1"].choiceResults[0] = "选项A";
eventRegistry["event1"].choiceResults[1] = "选项B";
eventRegistry["event1"].choiceResults[2] = "选项C";
// 更多选择结果可以根据需要添加...
// 触发选择回调
eventRegistry["event1"].onChoiceSelected?.Invoke(choice);
// 触发选择回调
eventRegistry["event2"].onChoiceSelected?.Invoke(choice);
// 注意:这里使用了延迟调用,确保在注册事件后立即触发回调。
// 延迟调用,确保在注册事件后立即触发回调
Invoke("TriggerChoiceCallback", 0.1f);
});
}
/// <summary>
/// 注册事件。
/// </summary>
/// <param name="eventName"></param>
/// <param name="eventRole"></param>
/// <param name="callback"></param>
public void RegisterEvent(string eventName, string eventRole, System.Action<int> callback)
{
EventAction eventAction = new EventAction()
{
eventName = eventName,
eventRole = eventRole,
onChoiceSelected = callback,
choiceResults = new Dictionary<int, string>()
};
eventRegistry[eventName] = eventAction;
}
/// <summary>
/// 移除事件。
/// </summary>
/// <param name="eventName"></param>
public void UnRegisterEvent(string eventName)
{
if (eventRegistry.ContainsKey(eventName))
{
eventRegistry.Remove(eventName);
}
}
/// <summary>
/// 尝试获取事件。如果事件存在则返回true并填充action参数否则返回false。
/// </summary>
/// <param name="eventName"></param>
/// <param name="action"></param>
/// <returns></returns>
public bool TryGetEvent(string eventName, out EventAction action)
{
return eventRegistry.TryGetValue(eventName, out action);
}
}