Files
18xchat/assets/Scripts/test/ChatContentsLayout.ts
T

61 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-07-21 22:14:06 +08:00
import { _decorator, Component, instantiate, Node, Vec3 } from "cc";
import { DemoManager } from "./DemoManager";
import { DialogBubble } from "./DialogBubble";
import { Dialog } from "./DemoData";
const { ccclass, property } = _decorator;
2025-07-22 16:43:10 +08:00
const BUTTOM_Y = -955.571;
2025-07-21 22:14:06 +08:00
@ccclass("ChatContentsLayout")
export class ChatContentsLayout extends Component {
manager: DemoManager = 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;
2025-07-22 16:43:10 +08:00
}
protected onEnable(): void {
if (!this.manager) this.manager = DemoManager.getInstance();
2025-07-21 22:14:06 +08:00
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;
2025-07-22 16:43:10 +08:00
let offset = newBubble.updateBubbleContent(dialog.content);
2025-07-21 22:14:06 +08:00
let pos = newBubble.node.position;
newBubble.node.position = new Vec3(pos.x, initPosY, pos.z);
2025-07-22 16:43:10 +08:00
initPosY += offset + 20;
2025-07-21 22:14:06 +08:00
this.bubbles.push(newBubble);
}
}
}