This commit is contained in:
CortexCore
2025-03-03 18:43:55 +08:00
parent 54f40090c9
commit 4402ae533b
32 changed files with 337 additions and 73 deletions

View File

@@ -1,6 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using Cysharp.Threading.Tasks;
namespace Net.Project.B.Dialogue
{
@@ -33,7 +35,15 @@ namespace Net.Project.B.Dialogue
/// 等待对话完成
/// </summary>
/// <returns></returns>
TaskAwaiter GetAwaiter();
CancellationTokenAwaitable.Awaiter GetAwaiter();
/// <summary>
/// 跳过对话
/// </summary>
public CancellationTokenSource CancellationTokenSource { get; set; }
/// <summary>
/// 元对象
/// </summary>
public object MetaObject { get; set; }
}
/// <summary>
/// 对话选择
@@ -64,10 +74,9 @@ namespace Net.Project.B.Dialogue
public int ActorIdentity { get; set; }
public string Text { get; set; }
public string VoicePath { get; set; }
public TaskAwaiter GetAwaiter()
{
throw new System.NotImplementedException();
}
public CancellationTokenAwaitable.Awaiter GetAwaiter() => CancellationTokenSource.Token.WaitUntilCanceled().GetAwaiter();
public CancellationTokenSource CancellationTokenSource { get; set; }
public object MetaObject { get; set; }
}
public struct DialogueChoice:IDialogueChoice
{

View File

@@ -4,6 +4,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using BITKit;
using Cysharp.Threading.Tasks;
@@ -57,9 +58,10 @@ namespace Net.Project.B.Dialogue
public event Action<IDialogueData> OnDialogueEnd;
public event Func<IDialogueData, IReadOnlyCollection<IDialogueChoice>, UniTask<int>> OnDialogueChoose;
public event Action<IDialogueData, IDialogueChoice> OnDialogueChose;
public async UniTask CreateDialogue(IDialogueData dialogueData)
{
dialogueData.CancellationTokenSource ??= new CancellationTokenSource();
if (_dialogues.TryAdd(dialogueData.Identity, dialogueData) is false)
{
dialogueData.Identity = _dialogues.Keys.Max() + 1;
@@ -68,19 +70,23 @@ namespace Net.Project.B.Dialogue
try
{
await OnDialogueStart.UniTaskFunc(dialogueData);
await OnDialogueStart.UniTaskFunc(dialogueData)
.AttachExternalCancellation(dialogueData.CancellationTokenSource.Token);
}
catch (OperationCanceledException)
{
}
catch (OperationAbortedException)
{
}
finally
{
dialogueData.CancellationTokenSource.Cancel();
OnDialogueEnd?.Invoke(dialogueData);
}
OnDialogueEnd?.Invoke(dialogueData);
}
public async UniTask<int> CreateDialogue(IDialogueData dialogueData,