monsterFactory.ts 3.8 KB

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