Files
18xchat/assets/Scripts/chat18x/data/DialogData.ts
T

58 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-07-21 22:14:06 +08:00
export interface Dialog {
isPlayer: boolean;
content: string;
}
2025-09-16 16:55:39 +08:00
export class DiaLogData {
2025-10-15 18:55:01 +08:00
// 统一的对话数组,保存所有历史对话
private dialogs: Dialog[] = [];
2025-07-21 22:14:06 +08:00
public cleanDialog() {
2025-10-15 18:55:01 +08:00
this.dialogs = [];
2025-07-21 22:14:06 +08:00
}
2025-09-16 16:55:39 +08:00
public pushDialog(
isPlayer: boolean,
str: string,
isLoading: boolean = false
) {
2025-10-15 18:55:01 +08:00
// 追加新对话到数组末尾
this.dialogs.push({ isPlayer, content: str });
2025-07-21 22:14:06 +08:00
}
2025-10-15 18:55:01 +08:00
/**
* 获取最后一条玩家对话(兼容旧接口)
*/
public GetPlayerDialog(): Dialog | null {
// 从后往前查找最后一条玩家消息
for (let i = this.dialogs.length - 1; i >= 0; i--) {
if (this.dialogs[i].isPlayer) {
return this.dialogs[i];
}
}
return null;
2025-09-16 19:03:00 +08:00
}
2025-10-15 18:55:01 +08:00
/**
* 获取所有AI对话(兼容旧接口)
*/
public GetAIDialog(): Dialog[] {
// 返回所有AI消息
return this.dialogs.filter(dialog => !dialog.isPlayer);
}
/**
* 获取所有对话记录
*/
public getAllDialogs(): Dialog[] {
return [...this.dialogs];
}
/**
* 批量设置对话记录(用于加载历史)
*/
public setDialogs(dialogs: Dialog[]): void {
this.dialogs = [...dialogs];
2025-07-21 22:14:06 +08:00
}
}