//#region 文件引入 import { _decorator, Node, Button, instantiate } from "cc"; import { TipsAnimation } from "./TipsAnimation"; import BaseUIForm from "../../Mgr_UI/BaseUIForm"; import NodePools from "../../CComEx/NodePools"; //#endregion 文件引入 const {ccclass, property,menu} = _decorator; @ccclass("TipsUI_Proxy") export default class TipsUI_Proxy extends BaseUIForm{ //#region 节点自动生成 @property(Node) m_Tips: Node = null; @property(Node) n_Node: Node = null; //#endregion 节点自动生成 // 使用对象池管理提示节点 tips_pool: NodePools = null; tips_list:TipsAnimation[]=[]; pending_tips:string[]=[]; // 待处理的提示队列 last_show_time:number = 0; // 上次显示提示的时间 tips_cooldown:number = 0.1; // 提示显示冷却时间(秒) tipsmap:Map = new Map(); // 最大显示数量限制 MAX_VISIBLE_TIPS: number = 6; protected onLoad(): void { super.onLoad(); // 初始化对象池,预创建8个实例 this.tips_pool = new NodePools(this.m_Tips, 8); } onShow(tips: string) { // 添加到待处理队列 this.pending_tips.push(tips); } processPendingTips() { // 处理待显示的提示 while (this.pending_tips.length > 0) { const tips = this.pending_tips.shift(); if (tips !== undefined) { // 检查是否与最新显示的提示相同 if (this.tips_list.length > 0) { const lastTip = this.tips_list[this.tips_list.length - 1]; // 注意:这里需要访问lastTip中的文本内容,但由于架构限制无法直接获取 // 可以考虑在TipsAnimation中添加getText方法来支持此功能 } // 检查全局冷却时间 const currentTime = performance.now() / 1000; // 转换为秒 if (currentTime - this.last_show_time < this.tips_cooldown) { // 冷却中,将提示重新放入队列等待下次处理 this.pending_tips.unshift(tips); break; } // 检查是否超过最大显示数量 if (this.tips_list.length >= this.MAX_VISIBLE_TIPS) { // 移除最早的一个提示 const earliestTip = this.tips_list.shift(); if (earliestTip) { earliestTip.onStopAnimation(); } } // 创建并显示新提示 let t_node = this.tips_pool.get(this.n_Node); let t_tips_con = t_node.getComponent(TipsAnimation); // 修改组件的finish回调,使其回收到对象池而不是销毁 t_tips_con.setFinishCallback((tips) => { this.recycleTipsNode(tips.node); const idx = this.tips_list.findIndex(tips => tips === tips); idx >= 0 && this.tips_list.splice(idx, 1); }); CNodeUtils.switchRichText(t_tips_con.tips,tips); this.tips_list.push(t_tips_con); // 更新最后显示时间 this.last_show_time = currentTime; // 激活动画 t_tips_con.node.active = true; t_tips_con.PlayAnimation(); } } } // 回收提示节点到对象池 private recycleTipsNode(tipsNode: Node) { if (this.tips_pool && tipsNode) { tipsNode.active = false; this.tips_pool.put(tipsNode); } } tick(dt){ // 处理待显示的提示 this.processPendingTips(); // 原有的去重逻辑保持不变 this.tipsmap.forEach((value: number, key: string) => { value -= dt; if (value <= 0) { this.tipsmap.delete(key); } else { this.tipsmap.set(key, value) } }) } protected update(dt: number): void { this.tick(dt); } protected onDestroy(): void { super.onDestroy(); // 清理对象池 if (this.tips_pool) { this.tips_pool.clear(); } } }