monsterFactory.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 { LQCollide } from "../../Collide/lq_collide_system/lq_collide";
  8. import { LevelMgr } from "../../levelMgr";
  9. import { EditorSet } from "../common/editorSet";
  10. import { GameLogic } from "../update/GameLogic";
  11. /**
  12. * 怪物类型:
  13. * 1 飞入飞出
  14. * 2 飞入后,静止
  15. * 3 飞入后,播放循环动画
  16. */
  17. /**
  18. * 关卡结束条件:
  19. * 1 所有的怪物创建完成
  20. * 2 所有的怪物被消灭
  21. */
  22. const {ccclass, property, executeInEditMode, playOnFocus} = cc._decorator;
  23. @ccclass
  24. @executeInEditMode
  25. @playOnFocus
  26. export default class MonsterFactory extends cc.Component {
  27. @property(cc.Prefab) monster: cc.Prefab = null;
  28. @property(cc.AnimationClip) aniClip:cc.AnimationClip = null;
  29. @property(cc.Integer)
  30. public get num(): number {
  31. return this._num;
  32. }
  33. public set num(value: number) {
  34. this._num = value;
  35. if(!CC_EDITOR) return;
  36. this.play();
  37. }
  38. @property(cc.Integer)
  39. private _num: number = 1;
  40. @property(cc.Integer)
  41. public get time(): number {
  42. return this._time;
  43. }
  44. public set time(value: number) {
  45. this._time = value;
  46. if(!CC_EDITOR) return;
  47. this.play();
  48. }
  49. @property(cc.Integer)
  50. private _time: number = 1;
  51. @property(cc.Integer)
  52. private _delay: number = 0;
  53. @property(cc.Integer)
  54. public get delay(): number {
  55. return this._delay;
  56. }
  57. public set delay(value: number) {
  58. this._delay = value;
  59. }
  60. timeArr = [];
  61. monsterCnt: number;
  62. monsterNode: cc.Node;
  63. protected onLoad(): void {
  64. let monsterNode = new cc.Node('monsterGroup');
  65. this.monsterNode = monsterNode;
  66. EditorSet.DontShow(monsterNode);
  67. this.node.addChild(monsterNode);
  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. else{
  81. this.play();
  82. }
  83. }
  84. protected onFocusInEditor(): void {
  85. // this.play();
  86. }
  87. protected onLostFocusInEditor(): void {
  88. // this.end();
  89. }
  90. end(){
  91. this.monsterNode.destroyAllChildren();
  92. this.timeArr.forEach((v)=>{
  93. clearTimeout(v);
  94. })
  95. this.timeArr = [];
  96. }
  97. play(): void {
  98. this.monsterNode.destroyAllChildren();
  99. this.timeArr.forEach((v)=>{
  100. clearTimeout(v);
  101. })
  102. this.timeArr = [];
  103. if(!this.monster) {
  104. cc.log('设置怪物资源');
  105. return;
  106. }
  107. if(!this.aniClip) {
  108. cc.log('设置动画');
  109. return;
  110. }
  111. if(this._delay > 0) {
  112. let tId = setTimeout(() => {
  113. this.createMonster();
  114. }, this._delay * 1000);
  115. this.timeArr.push(tId);
  116. }
  117. else{
  118. this.createMonster();
  119. }
  120. }
  121. private createMonster() {
  122. this.monsterCnt = 0;
  123. for (let index = 0; index < this.num; index++) {
  124. let tId = setTimeout(() => {
  125. let node = cc.instantiate(this.monster);
  126. let ani = node.addComponent(cc.Animation);
  127. ani.addClip(this.aniClip, "path");
  128. EditorSet.DontShow(node);
  129. node.active = false;
  130. this.monsterNode.addChild(node);
  131. node.active = true;
  132. ani.play('path');
  133. this.monsterCnt++;
  134. ani.once(cc.Animation.EventType.FINISHED, ()=>{
  135. let nodeBox = node.getBoundingBoxToWorld();
  136. let viewBox = cc.view.getViewportRect();
  137. if(!viewBox.containsRect(nodeBox)) {
  138. node.destroy();
  139. }
  140. });
  141. }, 1000 * this.time * index);
  142. this.timeArr.push(tId);
  143. }
  144. }
  145. }