import { _decorator, Component, instantiate, Node, NodePool, Prefab } from 'cc'; import { GBundle } from '../../game/ConfigRes'; import { GEvent } from 'db://assets/mx/module/event/GEvent'; const { ccclass, property } = _decorator; @ccclass('monsterScript') export class monsterScript extends Component { @property(Prefab) monsterPrefab: Prefab = null; @property(Prefab) bossPrefab: Prefab = null; //怪物内存池 guaiwuPool: NodePool = null; protected onDestroy(): void { if (this.guaiwuPool) this.guaiwuPool.clear() //GEvent.Ins.off(GEvent.atGMonsterDie, this.onAtGMonsterDie, this); } start() { //atGMonsterDie //GEvent.Ins.on(GEvent.atGMonsterDie, this.onAtGMonsterDie, this); this.guaiwuPool = new NodePool() //添加20波小美 for (let i = 0; i < 50; i++) { let xiaomei = instantiate(this.monsterPrefab) this.guaiwuPool.put(xiaomei) } } onAtGMonsterDie(node){ //回收 this.putGuaiwu(node) } getBoss(): Node { let boss = instantiate(this.bossPrefab) return boss } //获取怪物 getGuaiwu(): Node { if (this.guaiwuPool.size() > 0) { return this.guaiwuPool.get() }else{ let guaiwu = instantiate(this.monsterPrefab) this.guaiwuPool.put(guaiwu) return guaiwu } } //放入怪物 putGuaiwu(guaiwu: Node) { // console.log('putGuaiwu 回收 放入怪物 节点',this.guaiwuPool.size()) this.guaiwuPool.put(guaiwu) } }