gameMangager.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { _decorator, Component, instantiate, math, Node, Prefab } from 'cc';
  2. import { bullet } from '../bullet/bullet';
  3. import { constant } from './constant';
  4. import { enemyPlane } from '../plane/enemyPlane'
  5. const { ccclass, property } = _decorator;
  6. @ccclass('gameMangager')
  7. export class gameMangager extends Component {
  8. @property(Node)
  9. public playerPlane: Node = null;
  10. // bullet
  11. @property(Prefab)
  12. public bullet01: Prefab = null;
  13. @property(Prefab)
  14. public bullet02: Prefab = null;
  15. @property(Prefab)
  16. public bullet03: Prefab = null;
  17. @property(Prefab)
  18. public bullet04: Prefab = null;
  19. @property(Prefab)
  20. public bullet05: Prefab = null;
  21. @property
  22. public shootTime = 0.1; // 子弹周期
  23. @property
  24. public bulletSpeed = 1; // 子弹速度
  25. @property(Node)
  26. public bulletRoot: Node = null;
  27. // 敌机
  28. @property(Prefab)
  29. public enemyPlane01: Prefab = null;
  30. @property(Prefab)
  31. public enemyPlane02: Prefab = null;
  32. @property
  33. public createEnemyTime = 1;
  34. @property
  35. public enemySpeed1 = 0.5;
  36. @property
  37. public enemySpeed2 = 0.7;
  38. private _currShootTime = 0;
  39. private _isShooting = false;
  40. private _currCreatEnemyTime = 0;
  41. private _comBinationInterval = constant.Combination.PLAN1;
  42. start() {
  43. this._init()
  44. }
  45. update(deltaTime: number) {
  46. this._currShootTime += deltaTime
  47. if (this._isShooting && this._currShootTime > this.shootTime) {
  48. this.createPlayerBullet()
  49. this._currShootTime = 0
  50. }
  51. // 敌机类型
  52. this._currCreatEnemyTime += deltaTime
  53. if (this._comBinationInterval === constant.Combination.PLAN1) {
  54. if (this._currCreatEnemyTime > this.createEnemyTime) {
  55. this.createEnemyPlane();
  56. this._currCreatEnemyTime = 0;
  57. }
  58. } else if (this._comBinationInterval === constant.Combination.PLAN2) {
  59. if (this._currCreatEnemyTime > this.createEnemyTime * 0.9) {
  60. const randomCombiation = math.randomRangeInt(1, 3);
  61. if (randomCombiation === constant.Combination.PLAN2) {
  62. this.createCombination1()
  63. } else {
  64. this.createEnemyPlane();
  65. }
  66. this._currCreatEnemyTime = 0;
  67. }
  68. } else {
  69. if (this._currCreatEnemyTime > this.createEnemyTime * 0.8) {
  70. const randomCombiation = math.randomRangeInt(1, 4);
  71. if (randomCombiation === constant.Combination.PLAN2) {
  72. this.createCombination1()
  73. } else if (randomCombiation === constant.Combination.PLAN3) {
  74. this.createCombination2()
  75. } else {
  76. this.createEnemyPlane();
  77. }
  78. this._currCreatEnemyTime = 0;
  79. }
  80. }
  81. }
  82. // 创建子弹
  83. public createPlayerBullet() {
  84. const bullet01 = instantiate(this.bullet01);
  85. bullet01.setParent(this.bulletRoot);
  86. const pos = this.playerPlane.position;
  87. bullet01.setPosition(pos.x + 0, pos.y, pos.z)
  88. const bulletComp = bullet01.getComponent(bullet)
  89. bulletComp.bulletSpeed = this.bulletSpeed
  90. }
  91. // 创建敌机
  92. public createEnemyPlane() {
  93. const whichEnemy = math.randomRangeInt(1, 3);
  94. let prefab: Prefab = null;
  95. let speed = 0;
  96. if (whichEnemy == constant.EnemyPlaneType.TYPE1) {
  97. prefab = this.enemyPlane01;
  98. speed = this.enemySpeed1
  99. } else {
  100. prefab = this.enemyPlane02;
  101. speed = this.enemySpeed2
  102. }
  103. const enemy = instantiate(prefab)
  104. enemy.setParent(this.node)
  105. const enemyComp = enemy.getComponent(enemyPlane)
  106. enemyComp.show(speed)
  107. const randomPos = math.randomRangeInt(-11, 11)
  108. enemy.setPosition(30, 0, randomPos)
  109. }
  110. public createCombination1() {
  111. const enemyArry = new Array<Node>(3);
  112. for (let i = 0; i < enemyArry.length; i++) {
  113. enemyArry[i] = instantiate(this.enemyPlane01);
  114. const element = enemyArry[i]
  115. element.parent = this.node;
  116. element.setPosition(30, 0, -6 + i * 6)
  117. const enemyComp = element.getComponent(enemyPlane)
  118. enemyComp.show(this.enemySpeed1)
  119. }
  120. }
  121. public createCombination2() {
  122. const enemyArry = new Array<Node>(5);
  123. const combinationPost = [
  124. -8, 0, 40,
  125. -4, 0, 35,
  126. 0, 0, 30,
  127. 4, 0, 35,
  128. 8, 0, 40,
  129. ]
  130. for (let i = 0; i < enemyArry.length; i++) {
  131. enemyArry[i] = instantiate(this.enemyPlane02);
  132. const element = enemyArry[i]
  133. element.parent = this.node;
  134. const startIndex = i * 3
  135. element.setPosition(combinationPost[startIndex + 2], combinationPost[startIndex + 1], combinationPost[startIndex])
  136. const enemyComp = element.getComponent(enemyPlane)
  137. enemyComp.show(this.enemySpeed2)
  138. }
  139. }
  140. public isShooting(val: boolean) {
  141. this._isShooting = val
  142. }
  143. private _init() {
  144. this._currShootTime = this.shootTime
  145. this.changePlaneMode()
  146. }
  147. private changePlaneMode() {
  148. this.schedule(this._modeChanged, 10, 3)
  149. }
  150. private _modeChanged() {
  151. this._comBinationInterval++;
  152. }
  153. }