monsterFactory.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. import { LevelMgr } from "../../Script/levelMgr";
  8. import { EditorSet } from "./common/editorSet";
  9. import { GameLogic } from "./update/GameLogic";
  10. /**
  11. * 怪物类型:
  12. * 1 飞入飞出
  13. * 2 飞入后,静止
  14. * 3 飞入后,播放循环动画
  15. */
  16. /**
  17. * 关卡结束条件:
  18. * 1 所有的怪物创建完成
  19. * 2 所有的怪物被消灭
  20. */
  21. const {ccclass, property, executeInEditMode, playOnFocus} = cc._decorator;
  22. @ccclass
  23. @executeInEditMode
  24. @playOnFocus
  25. export default class MonsterFactory extends GameLogic {
  26. @property(cc.Prefab) monster: cc.Prefab = null;
  27. @property(cc.AnimationClip) aniClip:cc.AnimationClip = null;
  28. @property(cc.Integer)
  29. public get num(): number {
  30. return this._num;
  31. }
  32. public set num(value: number) {
  33. this._num = value;
  34. if(!CC_EDITOR) return;
  35. this.play();
  36. }
  37. @property(cc.Integer)
  38. private _num: number = 1;
  39. @property(cc.Integer)
  40. public get time(): number {
  41. return this._time;
  42. }
  43. public set time(value: number) {
  44. this._time = value;
  45. if(!CC_EDITOR) return;
  46. this.play();
  47. }
  48. @property(cc.Integer)
  49. private _time: number = 1;
  50. @property(cc.Integer)
  51. private _delay: number = 0;
  52. @property(cc.Integer)
  53. public get delay(): number {
  54. return this._delay;
  55. }
  56. public set delay(value: number) {
  57. this._delay = value;
  58. }
  59. timeArr = [];
  60. monsterCnt: number;
  61. monsterNode: cc.Node;
  62. protected onLoad(): void {
  63. let monsterNode = new cc.Node('monsterGroup');
  64. this.monsterNode = monsterNode;
  65. EditorSet.DontShow(monsterNode);
  66. this.node.addChild(monsterNode);
  67. this.play();
  68. if(CC_EDITOR){
  69. let node = new cc.Node('Collide');
  70. this.node.addChild(node);
  71. node.zIndex = cc.macro.MAX_ZINDEX;
  72. EditorSet.DontShow(node);
  73. let debugDrawer = node.addComponent(cc.Graphics);
  74. debugDrawer.lineWidth = 5;
  75. debugDrawer.strokeColor = new cc.Color(255, 0, 0);
  76. debugDrawer.fillColor = new cc.Color(255, 0, 0);
  77. debugDrawer.circle(0, 0, 20);
  78. debugDrawer.stroke();
  79. }
  80. }
  81. protected onFocusInEditor(): void {
  82. this.play();
  83. }
  84. play(): void {
  85. this.monsterNode.destroyAllChildren();
  86. this.timeArr.forEach((v)=>{
  87. clearTimeout(v);
  88. })
  89. this.timeArr = [];
  90. if(!this.monster) {
  91. cc.log('设置怪物资源');
  92. return;
  93. }
  94. if(!this.aniClip) {
  95. cc.log('设置动画');
  96. return;
  97. }
  98. if(this._delay > 0) {
  99. let tId = setTimeout(() => {
  100. this.createMonster();
  101. }, this._delay * 1000);
  102. this.timeArr.push(tId);
  103. }
  104. else{
  105. this.createMonster();
  106. }
  107. }
  108. private createMonster() {
  109. this.monsterCnt = 0;
  110. for (let index = 0; index < this.num; index++) {
  111. let tId = setTimeout(() => {
  112. let node = cc.instantiate(this.monster);
  113. let ani = node.addComponent(cc.Animation);
  114. ani.addClip(this.aniClip, "path");
  115. EditorSet.DontShow(node);
  116. node.active = false;
  117. this.monsterNode.addChild(node);
  118. node.active = true;
  119. ani.play('path');
  120. this.monsterCnt++;
  121. ani.once(cc.Animation.EventType.FINISHED, ()=>{
  122. let nodeBox = node.getBoundingBoxToWorld();
  123. let viewBox = cc.view.getViewportRect();
  124. if(!viewBox.containsRect(nodeBox)) {
  125. node.destroy();
  126. }
  127. });
  128. }, 1000 * this.time * index);
  129. this.timeArr.push(tId);
  130. }
  131. }
  132. gameUpdate(dt: number): void {
  133. if(CC_EDITOR) return;
  134. if(this.monsterCnt < this.num) return;
  135. if(this.monsterNode.childrenCount > 0) return;
  136. LevelMgr.inst.next();
  137. // 结束
  138. }
  139. // protected
  140. }