58 lines
1.2 KiB
TypeScript
58 lines
1.2 KiB
TypeScript
export interface Dialog {
|
|
isPlayer: boolean;
|
|
content: string;
|
|
}
|
|
|
|
export class DiaLogData {
|
|
// 统一的对话数组,保存所有历史对话
|
|
private dialogs: Dialog[] = [];
|
|
|
|
public cleanDialog() {
|
|
this.dialogs = [];
|
|
}
|
|
|
|
public pushDialog(
|
|
isPlayer: boolean,
|
|
str: string,
|
|
isLoading: boolean = false
|
|
) {
|
|
// 追加新对话到数组末尾
|
|
this.dialogs.push({ isPlayer, content: str });
|
|
}
|
|
|
|
/**
|
|
* 获取最后一条玩家对话(兼容旧接口)
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 获取所有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];
|
|
}
|
|
}
|