BottleHit.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const Spawn = require('Spawn');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. tipsFrames: [cc.SpriteFrame],
  6. bingoFrames: [cc.SpriteFrame],
  7. hitWrap: cc.Node,
  8. hitLabel: cc.Label,
  9. hitTips: cc.Node,
  10. hitDisX: 800,
  11. hitDisY: 280,
  12. hitInterval : 0.15,
  13. bottleHitMng: cc.Node, //对象池[Bottle]
  14. timer: cc.Label
  15. },
  16. init (game) {
  17. this.game = game;
  18. this.inGameUI = game.inGameUI;
  19. this.waveMng = game.waveMng;
  20. this.bottleHitMng = this.bottleHitMng.getComponent('BottleHitMng');
  21. this.bottleHitMng.init(this);
  22. this.touchInterval = 0;
  23. this.status = 0; //{0: 默认状态; 1: 开启必中瓶状态; 2: 必中瓶模式结束后2秒静止状态}
  24. },
  25. /**
  26. * 开始必中瓶模式
  27. */
  28. startHit (bottleType) {
  29. this.hitTips.active = true;
  30. this.hitLabel.node.active = true;
  31. this.hitLabel.getComponent(cc.Animation).play();
  32. this.hitList = [];
  33. this.frameId = 0;
  34. this.status = 1;
  35. this.hitTime = 30; //必中模式倒计时,持续3秒
  36. this.hitWrap.on(cc.Node.EventType.TOUCH_START, this.hitEvent, this)
  37. //设置必中瓶模式瓶身类型
  38. this.hitSprite = this.bingoFrames[bottleType];
  39. this.schedule(this.showHitTips, 0.2)
  40. this.scheduleOnce(() => {
  41. this.hitLabel.node.active = false;
  42. }, 1)
  43. //倒计时执行
  44. this.timer.node.active = true;
  45. this.timer.string = (this.hitTime / 10).toFixed(1) + 'S';
  46. this.timeCtrl = () => {
  47. if(this.hitTime == 0) {
  48. this.timer.node.active = false;
  49. this.endHit();
  50. this.unschedule(this.timeCtrl)
  51. } else {
  52. this.timer.string = (this.hitTime / 10).toFixed(1) + 'S';
  53. this.hitTime--;
  54. }
  55. }
  56. this.schedule(this.timeCtrl, 0.1)
  57. },
  58. showHitTips () {
  59. this.hitTips.getComponent(cc.Sprite).spriteFrame = this.tipsFrames[this.frameId]
  60. this.frameId = this.frameId == 0 ? 1 : 0;
  61. },
  62. hitEvent () {
  63. // 限制点击间隔
  64. if(this.touchInterval <= this.hitInterval) {
  65. return;
  66. } else {
  67. this.touchInterval = 0;
  68. let sp = new Spawn()
  69. sp.init({
  70. bottleType: 0,
  71. total: 1
  72. })
  73. let hb = sp.spawn(this.bottleHitMng)
  74. //显示对应瓶身尺寸的皮肤
  75. hb.children[1].getComponent(cc.Sprite).spriteFrame = this.hitSprite;
  76. if(this.hitList.length == 0) {
  77. this.hitList.unshift(hb)
  78. this.hitWrap.addChild(hb);
  79. hb.setPosition(-this.hitDisX, -this.hitDisY)
  80. let action = cc.moveTo(0.1, cc.p(0, -this.hitDisY))
  81. hb.runAction(action)
  82. this.scheduleOnce(() => {
  83. hb.getComponent(cc.Animation).play()
  84. }, 0.1)
  85. } else {
  86. this.hitList.unshift(hb)
  87. this.hitWrap.addChild(hb);
  88. hb.setPosition(-this.hitDisX, -this.hitDisY)
  89. let action1 = cc.moveTo(0.1, cc.p(0, -this.hitDisY))
  90. hb.runAction(action1)
  91. this.scheduleOnce(() => {
  92. hb.getComponent(cc.Animation).play()
  93. }, 0.1)
  94. let action2 = cc.moveTo(0.1, cc.p(this.hitDisX, -this.hitDisY))
  95. this.hitList[this.hitList.length - 1].runAction(action2)
  96. this.scheduleOnce(() => {
  97. let p = this.hitList.pop();
  98. p && this.bottleHitMng.returnBottle(0, p);
  99. }, 0.1)
  100. }
  101. this.inGameUI.addScore();
  102. }
  103. },
  104. /**
  105. * 结束必中瓶模式
  106. */
  107. endHit () {
  108. this.status = 2;
  109. //恢复游戏模式为华彩模式bingo
  110. this.game.gameMode = 'bingo';
  111. //重新生成新的波段内容
  112. this.waveMng.repeatWave();
  113. this.scheduleOnce(() => {
  114. //取消必中瓶模式事件监听
  115. this.hitWrap.off(cc.Node.EventType.TOUCH_START, this.hitEvent, this)
  116. //清除必中瓶模式的资源
  117. this.hitList.forEach(n => {
  118. let action = cc.moveTo(0.1, cc.p(this.hitDisX, -this.hitDisY))
  119. n.runAction(action)
  120. this.scheduleOnce(() => {
  121. this.bottleHitMng.returnBottle(0, n)
  122. }, 0.12)
  123. })
  124. //清除必中瓶UI提示
  125. this.unschedule(this.showHitTips);
  126. this.hitTips.active = false;
  127. //必中瓶模式结束后1.5秒内暂停发射箭矢
  128. this.scheduleOnce(() => {
  129. this.status = 0;
  130. }, 1.5)
  131. }, 0.1)
  132. },
  133. update (dt) {
  134. if(this.status == 1) {
  135. this.touchInterval += dt;
  136. }
  137. },
  138. });