代码结构整理
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user