monsterFactory.ts 4.3 KB

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