SamsaraGame/assets/Script/game/UIChatLogic.js

151 lines
3.8 KiB
JavaScript
Raw Permalink Normal View History

2025-04-24 17:03:28 +08:00
const { default: VIPUtil } = require("../ts/game/role/VIPUtil");
cc.Class({
extends: cc.Component,
properties: {
scrollNode: cc.Node,
contentNode: cc.Node,
chatPanel: cc.Prefab,
joinTeamBg:cc.SpriteFrame,
fontRes:cc.Font
},
onLoad() {
this.chatItemList = [];
// 聊天緩存
this.maxItems = 100;
this.chatItemNodes = [];
this.scrollNode.on('scroll-began', this.scrollBegan.bind(this));
this.scrollNode.on('scroll-ended', this.scrollEnded.bind(this));
this.scrollNode.on('touch-up', this.touchUp.bind(this));
this.beganPos = this.contentNode.y;
},
scrollBegan(event) {
this.beganPos = this.contentNode.y;
},
scrollEnded(event) {
this.beganPos = this.contentNode.y;
},
touchUp(event) {
if (Math.abs(this.contentNode.y - this.beganPos) < 4) {
let parent = cc.find('Canvas');
if (parent.getChildByName('ChatPanel') == null) {
let panel = cc.instantiate(this.chatPanel);
panel.parent = parent;
panel.getComponent('ChatPanel').loadListInfo(this.chatItemList);
this.scheduleOnce(() => {
panel.x = panel.y = 0;
}, 0);
}
}
},
onChatBtnClicked(e, d) {
let parent = cc.find('Canvas');
if (parent.getChildByName('ChatPanel') == null) {
let panel = cc.instantiate(this.chatPanel);
panel.x = panel.y = 0;
panel.parent = parent;
panel.getComponent('ChatPanel').loadListInfo(this.chatItemList);
this.scheduleOnce(() => {
panel.x = panel.y = 0;
}, 0);
}
},
addListInfo(info) {
if (info.scale == 5) {
this.fixChatItem(info.roleid);
return;
}
this.chatItemList.push(info);
this.addChatItem(info);
// console.log(this.chatItemList)
if (this.chatItemList.length > this.maxItems) {
this.delFirstChatItem();
this.chatItemList.shift();
}
},
delFirstChatItem() {
let delItem = this.chatItemNodes.shift();
if (delItem) {
this.contentNode.height -= delItem.height;
for (const item of this.chatItemNodes) {
item.y += delItem.height;
}
delItem.destroy();
}
},
addChatItem(info) {
if (info.msg.length < 0 && info.voice < 0) {
return;
}
let self = this;
let vipLevel = VIPUtil.getVipLevel(info.chargesum || 0);
cc.loader.loadRes('Common/ChatEmoji', cc.SpriteAtlas, function (err, atlas) {
let chatItem = new cc.Node();
chatItem.parent = self.contentNode;
let richText = chatItem.addComponent('CustomRichText');
richText.maxWidth = self.contentNode.width - 6;
richText.fontSize = 18;
richText.font = self.fontRes;
richText.lineHeight = 20;
richText.type = 1;
richText.scale = info.scale;
richText.vipLevel = vipLevel;
richText.rolename = info.name;
richText.emojiAtlas = atlas;
richText.joinTeamBg = self.joinTeamBg;
if (info.msg.length > 0) {
// console.log(richText)
richText.string = info.msg;
} else {
if (info.voice >= 0) {
richText.string = '[語音消息]';
}
}
chatItem.roleid = info.roleid;
chatItem.x = 5;
let blankHeight = self.contentNode.height;
if (self.chatItemNodes[0]) {
blankHeight = -self.chatItemNodes[0].y;
}
if (chatItem.height > blankHeight) {
self.contentNode.height += chatItem.height - blankHeight;
for (const item of self.chatItemNodes) {
item.y += blankHeight;
}
}
else {
for (const item of self.chatItemNodes) {
item.y += chatItem.height;
}
}
chatItem.y = -self.contentNode.height + chatItem.height;
self.chatItemNodes.push(chatItem);
if (self.contentNode.y > self.contentNode.height - self.contentNode.parent.height - chatItem.height - 20) {
self.contentNode.y = self.contentNode.height - self.contentNode.parent.height;
self.beganPos = self.contentNode.y;
}
});
},
fixChatItem(roleid) {
for (const chatitem of this.chatItemNodes) {
if (chatitem.roleid == roleid) {
let richText = chatitem.getComponent('CustomRichText');
richText.string = '給全體玩家拜年啦! ';
}
}
},
});