commit 04c062bacb9612197bc38576ea6f079dea685791
Author: CortexCore <2630229280@qq.com>
Date: Sun Dec 8 20:49:16 2024 +0800
1
diff --git a/Src/Dialogue.meta b/Src/Dialogue.meta
new file mode 100644
index 0000000..1fc748e
--- /dev/null
+++ b/Src/Dialogue.meta
@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: ee183f11afcda5644bd64299bd2f47f8
+folderAsset: yes
+DefaultImporter:
+ externalObjects: {}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Src/Dialogue/IDialogueData.cs b/Src/Dialogue/IDialogueData.cs
new file mode 100644
index 0000000..198df2e
--- /dev/null
+++ b/Src/Dialogue/IDialogueData.cs
@@ -0,0 +1,79 @@
+using System.Collections;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+
+namespace Net.Project.B.Dialogue
+{
+ ///
+ /// 对话数据
+ ///
+ public interface IDialogueData
+ {
+ ///
+ /// 唯一对话ID
+ ///
+ public int Identity { get; set; }
+ ///
+ /// 通道ID,用于区分不同的对话
+ ///
+ public int Channel { get; }
+ ///
+ /// 演员ID,谁是说话的人
+ ///
+ public int ActorIdentity { get; }
+ ///
+ /// 文本
+ ///
+ public string Text { get; }
+ ///
+ /// 语音路径
+ ///
+ public string VoicePath { get; }
+ ///
+ /// 等待对话完成
+ ///
+ ///
+ TaskAwaiter GetAwaiter();
+ }
+ ///
+ /// 对话选择
+ ///
+ public interface IDialogueChoice
+ {
+ ///
+ /// 索引
+ ///
+ public int Index { get; }
+ ///
+ /// 文本
+ ///
+ public string Text { get; }
+ ///
+ /// 是否可选
+ ///
+ public bool Enabled { get; }
+ ///
+ /// 备注,例如:选择这个选项会触发什么事件,不可选的原因等
+ ///
+ public string Remark { get; }
+ }
+ public struct DialogueData:IDialogueData
+ {
+ public int Identity { get; set; }
+ public int Channel { get; set; }
+ public int ActorIdentity { get; set; }
+ public string Text { get; set; }
+ public string VoicePath { get; set; }
+ public TaskAwaiter GetAwaiter()
+ {
+ throw new System.NotImplementedException();
+ }
+ }
+ public struct DialogueChoice:IDialogueChoice
+ {
+ public int Index { get; set; }
+ public string Text { get; set; }
+ public bool Enabled { get; set; }
+ public string Remark { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/Src/Dialogue/IDialogueData.cs.meta b/Src/Dialogue/IDialogueData.cs.meta
new file mode 100644
index 0000000..95b904d
--- /dev/null
+++ b/Src/Dialogue/IDialogueData.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: ebb9eb40cce574a44b3419bd4d58411f
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Src/Dialogue/IDialogueService.cs b/Src/Dialogue/IDialogueService.cs
new file mode 100644
index 0000000..ee48ff0
--- /dev/null
+++ b/Src/Dialogue/IDialogueService.cs
@@ -0,0 +1,91 @@
+using System;
+using System.Collections;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Linq;
+using BITKit;
+using Cysharp.Threading.Tasks;
+
+namespace Net.Project.B.Dialogue
+{
+ ///
+ /// 对话服务
+ ///
+ public interface IDialogueService
+ {
+ ///
+ /// 所有对话
+ ///
+ IReadOnlyDictionary Dialogues { get; }
+
+ ///
+ /// 对话开始
+ ///
+ public event Func OnDialogueStart;
+
+ ///
+ /// 对话结束
+ ///
+ public event Action OnDialogueEnd;
+
+ ///
+ /// 对话选择
+ ///
+ public event Func, UniTask> OnDialogueChoose;
+
+ ///
+ /// 对话选择回调
+ ///
+ public event Action OnDialogueChose;
+
+ ///
+ /// 开启对话
+ ///
+ /// 对话
+ ///
+ UniTask CreateDialogue(IDialogueData dialogueData);
+
+ UniTask CreateDialogue(IDialogueData dialogueData, IReadOnlyCollection dialogueChoices);
+ }
+
+ public class DialogueService : IDialogueService
+ {
+ public IReadOnlyDictionary Dialogues => _dialogues;
+
+ public event Func OnDialogueStart;
+ public event Action OnDialogueEnd;
+ public event Func, UniTask> OnDialogueChoose;
+ public event Action OnDialogueChose;
+
+ public async UniTask CreateDialogue(IDialogueData dialogueData)
+ {
+ if (_dialogues.TryAdd(dialogueData.Identity, dialogueData) is false)
+ {
+ dialogueData.Identity = _dialogues.Keys.Max() + 1;
+ _dialogues.TryAdd(dialogueData.Identity, dialogueData);
+ }
+
+ await OnDialogueStart.UniTaskFunc(dialogueData);
+
+ OnDialogueEnd?.Invoke(dialogueData);
+ }
+
+ public async UniTask CreateDialogue(IDialogueData dialogueData,
+ IReadOnlyCollection dialogueChoices)
+ {
+ if (OnDialogueChoose is null)
+ {
+ throw new NullReferenceException("OnDialogueChoose is null,unable to create dialogue");
+ }
+
+ var task = OnDialogueChoose.Invoke(dialogueData, dialogueChoices);
+ var index = await task;
+
+ OnDialogueChose?.Invoke(dialogueData, dialogueChoices.ElementAt(index));
+
+ return index;
+ }
+
+ private readonly ConcurrentDictionary _dialogues = new();
+ }
+}
\ No newline at end of file
diff --git a/Src/Dialogue/IDialogueService.cs.meta b/Src/Dialogue/IDialogueService.cs.meta
new file mode 100644
index 0000000..62210a7
--- /dev/null
+++ b/Src/Dialogue/IDialogueService.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 4b6700c7171a00048b0d5907bd7c0133
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Src/Dialogue/Net.Project.B.Dialogue.asmdef b/Src/Dialogue/Net.Project.B.Dialogue.asmdef
new file mode 100644
index 0000000..a7e5bd0
--- /dev/null
+++ b/Src/Dialogue/Net.Project.B.Dialogue.asmdef
@@ -0,0 +1,17 @@
+{
+ "name": "Net.Project.B.Dialogue",
+ "rootNamespace": "",
+ "references": [
+ "GUID:14fe60d984bf9f84eac55c6ea033a8f4",
+ "GUID:f51ebe6a0ceec4240a699833d6309b23"
+ ],
+ "includePlatforms": [],
+ "excludePlatforms": [],
+ "allowUnsafeCode": false,
+ "overrideReferences": false,
+ "precompiledReferences": [],
+ "autoReferenced": true,
+ "defineConstraints": [],
+ "versionDefines": [],
+ "noEngineReferences": true
+}
\ No newline at end of file
diff --git a/Src/Dialogue/Net.Project.B.Dialogue.asmdef.meta b/Src/Dialogue/Net.Project.B.Dialogue.asmdef.meta
new file mode 100644
index 0000000..168bd75
--- /dev/null
+++ b/Src/Dialogue/Net.Project.B.Dialogue.asmdef.meta
@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: ba8323a5a01afc24188ded7f4b05db8a
+AssemblyDefinitionImporter:
+ externalObjects: {}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Src/Dialogue/package.json b/Src/Dialogue/package.json
new file mode 100644
index 0000000..fa225de
--- /dev/null
+++ b/Src/Dialogue/package.json
@@ -0,0 +1,9 @@
+{
+ "name": "net.project.b.dialogue",
+ "displayName": "Project B Dialogue",
+ "version": "2024.3.31",
+ "unity": "2022.3",
+ "description": "Project B 对话服务",
+ "keywords": [],
+ "dependencies": {}
+}
\ No newline at end of file
diff --git a/Src/Dialogue/package.json.meta b/Src/Dialogue/package.json.meta
new file mode 100644
index 0000000..5e283d8
--- /dev/null
+++ b/Src/Dialogue/package.json.meta
@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: e4f24e056b51e32478c042d2b2147084
+TextScriptImporter:
+ externalObjects: {}
+ userData:
+ assetBundleName:
+ assetBundleVariant: