优化聊天部分
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import { find } from "cc";
|
||||
import { ChatContentsLayout } from "../ui/components/ChatContentsLayout";
|
||||
import { DemoData, Dialog } from "../data/DialogData";
|
||||
import { NavigationManager } from "./NavigationManager";
|
||||
import { DiaLogData, Dialog } from "../data/DialogData";
|
||||
import Utils from "db://assets/Scripts/Main/Common/Utils";
|
||||
import { InnerMsgCode } from "db://assets/Scripts/Main/Config/InnerMsgCode";
|
||||
|
||||
@@ -31,19 +29,15 @@ export class DialogManager {
|
||||
}
|
||||
|
||||
private constructor() {
|
||||
this.demoData = new DemoData();
|
||||
this.dialogData = new DiaLogData();
|
||||
}
|
||||
|
||||
/** 对话数据实例 */
|
||||
private demoData: DemoData;
|
||||
|
||||
/** 当前主题ID */
|
||||
private themeId = -1;
|
||||
private dialogData: DiaLogData;
|
||||
|
||||
/** 聊天内容布局组件引用 */
|
||||
public layoutout: ChatContentsLayout;
|
||||
|
||||
|
||||
/**
|
||||
* 更新对话内容
|
||||
*
|
||||
@@ -57,14 +51,9 @@ export class DialogManager {
|
||||
fromPlayer: boolean = false
|
||||
): void {
|
||||
if (fromPlayer) {
|
||||
this.demoData.cleanDialog();
|
||||
this.dialogData.cleanDialog();
|
||||
}
|
||||
this.demoData.pushDialog(isPlayer, str);
|
||||
console.log("Dialog updated:", {
|
||||
isPlayer,
|
||||
content: str.substring(0, 50) + "...",
|
||||
});
|
||||
Utils.sendInnerMsg(InnerMsgCode.Chat_DialogRefresh);
|
||||
this.dialogData.pushDialog(isPlayer, str);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,27 +69,25 @@ export class DialogManager {
|
||||
fromPlayer: boolean = false
|
||||
): void {
|
||||
if (fromPlayer) {
|
||||
this.demoData.cleanDialog();
|
||||
this.dialogData.cleanDialog();
|
||||
}
|
||||
|
||||
// 如果是玩家消息,直接添加单个气泡
|
||||
if (isPlayer) {
|
||||
this.demoData.pushDialog(isPlayer, str);
|
||||
this.dialogData.pushDialog(isPlayer, str);
|
||||
console.log("Dialog updated:", {
|
||||
isPlayer,
|
||||
content: str.substring(0, 50) + "...",
|
||||
});
|
||||
Utils.sendInnerMsg(InnerMsgCode.Chat_DialogRefresh);
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果是AI消息,进行分段处理
|
||||
const segments = this.splitMessageIntoSegments(str);
|
||||
|
||||
|
||||
if (segments.length <= 1) {
|
||||
// 如果只有一段,直接显示单个气泡
|
||||
this.demoData.pushDialog(isPlayer, str);
|
||||
Utils.sendInnerMsg(InnerMsgCode.Chat_DialogRefresh);
|
||||
this.dialogData.pushDialog(isPlayer, str);
|
||||
} else {
|
||||
// 多段内容,依次显示多个气泡
|
||||
this.displaySegmentsWithDelay(segments, isPlayer);
|
||||
@@ -115,16 +102,16 @@ export class DialogManager {
|
||||
private splitMessageIntoSegments(message: string): string[] {
|
||||
// 首先按双换行符分割
|
||||
let segments = message.split(/\n\n+/);
|
||||
|
||||
|
||||
// 如果只有一段,则按单换行符分割
|
||||
if (segments.length === 1) {
|
||||
segments = message.split(/\n+/);
|
||||
}
|
||||
|
||||
|
||||
// 过滤空段落并去除首尾空白
|
||||
return segments
|
||||
.map(segment => segment.trim())
|
||||
.filter(segment => segment.length > 0);
|
||||
.map((segment) => segment.trim())
|
||||
.filter((segment) => segment.length > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,22 +119,17 @@ export class DialogManager {
|
||||
* @param segments 段落数组
|
||||
* @param isPlayer 是否为玩家消息
|
||||
*/
|
||||
private displaySegmentsWithDelay(segments: string[], isPlayer: boolean): void {
|
||||
let currentDelay = 500; // 基础延迟 500ms
|
||||
|
||||
private displaySegmentsWithDelay(
|
||||
segments: string[],
|
||||
isPlayer: boolean
|
||||
): void {
|
||||
// let currentDelay = 500; // 基础延迟 500ms
|
||||
// 每个后续段落增加随机延迟 (800-1500ms)
|
||||
|
||||
// currentDelay += 800 + Math.random() * 700;
|
||||
|
||||
segments.forEach((segment, index) => {
|
||||
setTimeout(() => {
|
||||
this.demoData.pushDialog(isPlayer, segment);
|
||||
console.log(`Segment ${index + 1}/${segments.length} displayed:`, {
|
||||
isPlayer,
|
||||
content: segment.substring(0, 30) + "...",
|
||||
delay: currentDelay
|
||||
});
|
||||
Utils.sendInnerMsg(InnerMsgCode.Chat_DialogRefresh);
|
||||
}, currentDelay);
|
||||
|
||||
// 每个后续段落增加随机延迟 (800-1500ms)
|
||||
currentDelay += 800 + Math.random() * 700;
|
||||
this.dialogData.pushDialog(isPlayer, segment);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -156,7 +138,7 @@ export class DialogManager {
|
||||
* 显示"..."循环动画表示AI正在回复
|
||||
*/
|
||||
public addLoadingDialog(): void {
|
||||
this.demoData.pushDialog(false, "...", true);
|
||||
this.dialogData.pushDialog(false, "...", true);
|
||||
console.log("Loading dialog added");
|
||||
Utils.sendInnerMsg(InnerMsgCode.Chat_DialogRefresh);
|
||||
}
|
||||
@@ -166,8 +148,8 @@ export class DialogManager {
|
||||
* 在收到AI回复或出错时调用
|
||||
*/
|
||||
public removeLoadingDialog(): void {
|
||||
const dialogs = this.demoData.GetDialogs();
|
||||
const loadingIndex = dialogs.findIndex(dialog => dialog.isLoading);
|
||||
const dialogs = this.dialogData.GetDialogs();
|
||||
const loadingIndex = dialogs.findIndex((dialog) => dialog.isLoading);
|
||||
if (loadingIndex !== -1) {
|
||||
dialogs.splice(loadingIndex, 1);
|
||||
console.log("Loading dialog removed");
|
||||
@@ -181,14 +163,14 @@ export class DialogManager {
|
||||
* @returns {Dialog[]} 对话记录数组
|
||||
*/
|
||||
public getDialogs() {
|
||||
return this.demoData.GetDialogs();
|
||||
return this.dialogData.GetDialogs();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空当前对话记录
|
||||
*/
|
||||
public clearDialogs(): void {
|
||||
this.demoData.cleanDialog();
|
||||
this.dialogData.cleanDialog();
|
||||
console.log("All dialogs cleared");
|
||||
Utils.sendInnerMsg(InnerMsgCode.Chat_DialogRefresh);
|
||||
}
|
||||
@@ -198,9 +180,13 @@ export class DialogManager {
|
||||
* @param dialogs 对话记录数组
|
||||
*/
|
||||
public setDialogs(dialogs: Dialog[]): void {
|
||||
this.demoData.cleanDialog();
|
||||
dialogs.forEach(dialog => {
|
||||
this.demoData.pushDialog(dialog.isPlayer, dialog.content, dialog.isLoading || false);
|
||||
this.dialogData.cleanDialog();
|
||||
dialogs.forEach((dialog) => {
|
||||
this.dialogData.pushDialog(
|
||||
dialog.isPlayer,
|
||||
dialog.content,
|
||||
dialog.isLoading || false
|
||||
);
|
||||
});
|
||||
console.log(`DialogManager: Set ${dialogs.length} dialogs`);
|
||||
Utils.sendInnerMsg(InnerMsgCode.Chat_DialogRefresh);
|
||||
@@ -219,14 +205,14 @@ export class DialogManager {
|
||||
* 获取对话记录数量
|
||||
*/
|
||||
public getDialogCount(): number {
|
||||
return this.demoData.GetDialogs().length;
|
||||
return this.dialogData.GetDialogs().length;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最后一条对话记录
|
||||
*/
|
||||
public getLastDialog(): Dialog | null {
|
||||
const dialogs = this.demoData.GetDialogs();
|
||||
const dialogs = this.dialogData.GetDialogs();
|
||||
return dialogs.length > 0 ? dialogs[dialogs.length - 1] : null;
|
||||
}
|
||||
|
||||
@@ -234,6 +220,6 @@ export class DialogManager {
|
||||
* 检查是否有对话记录
|
||||
*/
|
||||
public hasDialogs(): boolean {
|
||||
return this.demoData.GetDialogs().length > 0;
|
||||
return this.dialogData.GetDialogs().length > 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user