// Learn TypeScript: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html import { EditorSet } from "../common/editorSet"; /** * 怪物类型: * 1 飞入飞出 * 2 飞入后,静止 * 3 飞入后,播放循环动画 */ /** * 关卡结束条件: * 1 所有的怪物创建完成 * 2 所有的怪物被消灭 */ const {ccclass, property, executeInEditMode, playOnFocus} = cc._decorator; @ccclass @executeInEditMode @playOnFocus export default class MonsterFactory extends cc.Component { @property(cc.Prefab) monster: cc.Prefab = null; @property(cc.AnimationClip) aniClip:cc.AnimationClip = null; @property(cc.Integer) public get num(): number { return this._num; } public set num(value: number) { this._num = value; if(!CC_EDITOR) return; this.play(); } @property(cc.Integer) private _num: number = 1; @property(cc.Integer) public get time(): number { return this._time; } public set time(value: number) { this._time = value; if(!CC_EDITOR) return; this.play(); } @property(cc.Integer) private _time: number = 1; @property(cc.Integer) private _delay: number = 0; @property(cc.Integer) public get delay(): number { return this._delay; } public set delay(value: number) { this._delay = value; } timeArr = []; monsterCnt: number; monsterNode: cc.Node; protected onLoad(): void { let monsterNode = new cc.Node('monsterGroup'); this.monsterNode = monsterNode; EditorSet.DontShow(monsterNode); this.node.addChild(monsterNode); if(CC_EDITOR){ let node = new cc.Node('Collide'); this.node.addChild(node); node.zIndex = cc.macro.MAX_ZINDEX; EditorSet.DontShow(node); let debugDrawer = node.addComponent(cc.Graphics); debugDrawer.lineWidth = 5; debugDrawer.strokeColor = new cc.Color(255, 0, 0); debugDrawer.fillColor = new cc.Color(255, 0, 0); debugDrawer.circle(0, 0, 20); debugDrawer.stroke(); } else{ this.play(); } } protected onFocusInEditor(): void { // this.play(); } protected onLostFocusInEditor(): void { // this.end(); } end(){ this.monsterNode.destroyAllChildren(); this.timeArr.forEach((v)=>{ clearTimeout(v); }) this.timeArr = []; } play(): void { this.monsterNode.destroyAllChildren(); this.timeArr.forEach((v)=>{ clearTimeout(v); }) this.timeArr = []; if(!this.monster) { cc.log('设置怪物资源'); return; } if(!this.aniClip) { cc.log('设置动画'); return; } if(this._delay > 0) { let tId = setTimeout(() => { this.createMonster(); }, this._delay * 1000); this.timeArr.push(tId); } else{ this.createMonster(); } } private createMonster() { this.monsterCnt = 0; for (let index = 0; index < this.num; index++) { let tId = setTimeout(() => { let node = cc.instantiate(this.monster); let ani = node.addComponent(cc.Animation); ani.addClip(this.aniClip, "path"); EditorSet.DontShow(node); node.active = false; this.monsterNode.addChild(node); node.active = true; ani.play('path'); this.monsterCnt++; ani.once(cc.Animation.EventType.FINISHED, ()=>{ let nodeBox = node.getBoundingBoxToWorld(); let viewBox = cc.view.getViewportRect(); if(!viewBox.containsRect(nodeBox)) { node.destroy(); } }); }, 1000 * this.time * index); this.timeArr.push(tId); } } }