Files
18xchat/assets/Scripts/chat18x/ui/components/ChatContentsLayout.ts
T
2025-08-11 16:08:59 +08:00

61 lines
1.7 KiB
TypeScript

import { _decorator, Component, instantiate, Node, Vec3 } from "cc";
import { DialogManager } from "../../manager/DialogManager";
import { DialogBubble } from "./DialogBubble";
import { Dialog } from "../../utils/DemoData";
const { ccclass, property } = _decorator;
const BUTTOM_Y = -955.571;
@ccclass("ChatContentsLayout")
export class ChatContentsLayout extends Component {
manager: DialogManager = null;
@property(DialogBubble)
lBubble: DialogBubble = null;
@property(DialogBubble)
rBubble: DialogBubble = null;
bubbles: DialogBubble[] = [];
protected start(): void {
this.lBubble.node.active = false;
this.rBubble.node.active = false;
}
protected onEnable(): void {
if (!this.manager) this.manager = DialogManager.getInstance();
this.manager.layoutout = this;
}
UpdateDialog(dialogs: Dialog[]) {
//if (!this.bubbles) this.bubbles = [];
for (let i = this.bubbles.length - 1; i >= 0; i--) {
if (this.bubbles[i].node) {
this.bubbles[i].node.destroy();
}
}
this.bubbles = [];
let initPosY: number = BUTTOM_Y;
for (let i = dialogs.length - 1; i >= 0; i--) {
const dialog = dialogs[i]; //倒序
let newBubbleNode = dialog.isPlayer
? instantiate(this.rBubble.node)
: instantiate(this.lBubble.node);
let newBubble = newBubbleNode.getComponent(DialogBubble);
newBubbleNode.setParent(this.node);
newBubble.node.active = true;
let offset = newBubble.updateBubbleContent(dialog.content);
let pos = newBubble.node.position;
newBubble.node.position = new Vec3(pos.x, initPosY, pos.z);
initPosY += offset + 20;
this.bubbles.push(newBubble);
}
}
}