123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- // 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 { LevelMgr } from "../../Script/levelMgr";
- import { GameLogic } from "./update/logic";
- /**
- * 怪物类型:
- * 1 飞入飞出
- * 2 飞入后,静止
- * 3 飞入后,播放循环动画
- */
- /**
- * 关卡结束条件:
- * 1 所有的怪物创建完成
- * 2 所有的怪物被消灭
- */
- const {ccclass, property, executeInEditMode, playOnFocus} = cc._decorator;
- @ccclass
- @executeInEditMode
- @playOnFocus
- export default class MonsterFactory extends GameLogic {
- @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;
- protected onLoad(): void {
- this.play();
- }
- protected onFocusInEditor(): void {
- this.play();
- }
- protected play(): void {
- this.node.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");
- if(CC_EDITOR){
- // @ts-ignore
- node._objFlags |= cc.Object.Flags.DontSave | cc.Object.Flags.LockedInEditor | cc.Object.Flags.HideInHierarchy;
- }
- node.active = false;
- this.node.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);
- }
- }
- gameUpdate(dt: number): void {
- if(CC_EDITOR) return;
- if(this.monsterCnt < this.num) return;
- if(this.node.childrenCount > 0) return;
- LevelMgr.inst.next();
- // 结束
- }
- // protected
- }
|