Bottle.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. const BottleType = require('Types').BottleType;
  2. const Roll = require('Roll');
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. //{0: 小瓶; 1: 中瓶; 2: 大瓶; 3: 特大瓶}
  7. bottleType: {
  8. default: BottleType.MNormal,
  9. type: BottleType
  10. },
  11. // tp1: cc.Node,
  12. // tp2: cc.Node,
  13. // tp3: cc.Node,
  14. bottleFrames: [cc.SpriteFrame],
  15. shooterFrames: [cc.SpriteFrame],
  16. item: cc.Node,
  17. stick: cc.Node,
  18. subline: cc.Node,
  19. bingo: cc.Node,
  20. gold: cc.Node,
  21. hit: cc.Node,
  22. bounder: cc.Node,
  23. },
  24. init (waveMng) {
  25. this.waveMng = waveMng;
  26. this.vSize = cc.director.getVisibleSize();
  27. this.subline = this.subline.getComponent('bItem');
  28. this.subline.init(this);
  29. this.gold = this.gold.getComponent('bItem');
  30. this.gold.init(this);
  31. this.hit = this.hit.getComponent('bItem');
  32. this.hit.init(this);
  33. this.stick = this.stick.getComponent('Stick');
  34. this.stick.init(this);
  35. // 设置箭矢皮肤
  36. let arrIndex = window.Global.arrowSkinIndex || 0
  37. this.stick.getComponent(cc.Sprite).spriteFrame = this.shooterFrames[arrIndex]
  38. this.isEnter = false;
  39. this.hasChange = false;
  40. //刚体组件
  41. this.rigidBody = this.getComponent(cc.RigidBody);
  42. //包围盒上下限范围设定
  43. let lRange = this.vSize.width > 1550 ? 240 : 150
  44. this.checkLimit = [
  45. this.vSize.width / 2 - lRange,
  46. this.vSize.width / 2 + lRange,
  47. ]
  48. this.reset();
  49. this.tMode = 'normal';
  50. if(waveMng.game.gameMode == 'normal') { //当前场景为普通模式
  51. //根据权重生成特殊瓶子
  52. let roller = new Roll();
  53. if(this.bottleType == 3) {
  54. roller.add('subline', 5); //辅助线瓶 5%
  55. roller.add('bingo', 5); //华彩瓶 5%
  56. roller.add('gold', 20); //金币瓶 20%
  57. roller.add('normal', 70); //普通瓶 70%
  58. } else {
  59. roller.add('subline', 5); //辅助线瓶 5%
  60. roller.add('bingo', 10); //华彩瓶 10%
  61. roller.add('gold', 20); //金币瓶 20%
  62. roller.add('normal', 65); //普通瓶 65%
  63. }
  64. this.tMode = roller.roll();
  65. switch (this.tMode) {
  66. case 'subline':
  67. if(!this.waveMng.checkSpecialMode('subline')) {
  68. this.waveMng.recordSpecialMode('subline');
  69. this.subline.showSubline();
  70. this.changeBottle(0)
  71. } else {
  72. this.tMode = 'normal';
  73. }
  74. break;
  75. case 'bingo':
  76. if(!this.waveMng.checkSpecialMode('bingo')) {
  77. this.waveMng.recordSpecialMode('bingo');
  78. this.bingo.active = true;
  79. this.changeBottle(9);
  80. this.tMode = 'bingo';
  81. } else {
  82. this.bingo.active = false;
  83. this.changeBottle(0);
  84. this.tMode = 'normal';
  85. }
  86. break;
  87. case 'gold':
  88. this.gold.showGold();
  89. this.changeBottle(0)
  90. break;
  91. default:
  92. this.changeBottle(0)
  93. break;
  94. }
  95. } else if(waveMng.game.gameMode == 'bingo') {
  96. //超大瓶体不出现华彩瓶
  97. this.tMode = 'bingo';
  98. this.bingo.active = true;
  99. this.changeBottle(9)
  100. if(!this.waveMng.checkSpecialMode('hit') && this.waveMng.game.bingoHit == 0) {
  101. let ran = cc.random0To1();
  102. // 8%概率出现必中瓶
  103. if(ran > 0.08) {
  104. this.tMode = 'hit';
  105. this.hit.showHit();
  106. this.waveMng.recordSpecialMode('hit');
  107. }
  108. }
  109. } else {
  110. this.tMode = 'normal'
  111. this.bingo.active = false;
  112. this.changeBottle(0)
  113. }
  114. },
  115. /**
  116. * 重置瓶子各种模式
  117. */
  118. reset () {
  119. this.finished = false;
  120. this.stick.item.active = false;
  121. this.subline.item.active = false;
  122. this.gold.item.active = false;
  123. this.hit.item.active = false;
  124. this.bingo.active = false;
  125. },
  126. /**
  127. * 切换瓶子类型
  128. * @param {Number} bottleType 瓶子类型{0: 普通瓶; 9: 华彩瓶}
  129. */
  130. changeBottle (bottleType) {
  131. if(bottleType == 0) {
  132. bottleType = window.Global.bottleSkinIndex || 0
  133. }
  134. this.item.getComponent(cc.Sprite).spriteFrame = this.bottleFrames[bottleType]
  135. },
  136. /**
  137. * 切换X轴位移速度
  138. * @param {Number} speed 瓶子沿X轴位移速度
  139. */
  140. // changeSpeed (speed) {
  141. // this.MoveX = speed;
  142. // },
  143. // onCollisionEnter (other, self) {
  144. // this.doEnterCtrl();
  145. // },
  146. // 只在两个碰撞体开始接触时被调用一次
  147. onBeginContact (contact, selfCollider, otherCollider) {
  148. if(otherCollider.node.group == "shooter") {
  149. this.rigidBody.enabledContactListener = false;
  150. this.doEnterCtrl();
  151. }
  152. },
  153. doEnterCtrl () {
  154. console.log(this.tMode);
  155. if(this.isEnter) {
  156. return;
  157. } else {
  158. this.isEnter = true;
  159. //播放箭矢击中瓶身动画
  160. this.stick.item.active = true;
  161. let clipRan = cc.random0To1() * 3
  162. clipRan = Math.ceil(clipRan)
  163. switch (this.bottleType) {
  164. case 0:
  165. //小瓶播放特定的动画
  166. this.getComponent(cc.Animation).play('bottle-run-s')
  167. break;
  168. case 3:
  169. //特大瓶播放特定的动画
  170. this.getComponent(cc.Animation).play('bottle-run' + clipRan)
  171. break;
  172. default:
  173. //其他瓶播放随机动画
  174. this.getComponent(cc.Animation).play('bottle-run' + clipRan)
  175. break;
  176. }
  177. if(this.bingo.active) {
  178. if(this.hit.item.active) {
  179. //激活必中瓶模式
  180. this.hit.getComponent(cc.Animation).play();
  181. this.waveMng.game.activeMode('hit');
  182. this.waveMng.game.runHitMode(this.bottleType);
  183. this.hit.item.active = false;
  184. } else {
  185. // 激活华彩模式
  186. this.waveMng.game.activeMode('bingo');
  187. }
  188. if(this.gold.item.active) {
  189. /** 增加金币 */
  190. this.gold.getComponent(cc.Animation).play();
  191. this.waveMng.game.activeMode('gold');
  192. }
  193. } else if(this.subline.item.active) {
  194. /** 激活辅助线模式 */
  195. this.subline.getComponent(cc.Animation).play();
  196. this.waveMng.game.activeMode('subline');
  197. } else if(this.gold.item.active) {
  198. /** 增加金币 */
  199. this.gold.getComponent(cc.Animation).play();
  200. this.waveMng.game.activeMode('gold');
  201. }
  202. }
  203. },
  204. start () {
  205. // let action = cc.moveTo(3, cc.p(this.vSize.width + this.node.x + 1000, this.node.y))
  206. // this.node.runAction(action)
  207. },
  208. update (dt) {
  209. // if(this.waveMng.state == 3) {
  210. // return;
  211. // }
  212. if(this.node.x > this.checkLimit[0] && this.node.x < this.checkLimit[1]) {
  213. this.rigidBody.enabledContactListener = true;
  214. } else {
  215. this.rigidBody.enabledContactListener = false;
  216. }
  217. //如果在华彩模式冷却时间下,转换为普通瓶子
  218. if(this.waveMng.game.inGameUI.bingoFreeze && this.bingo.active) {
  219. this.bingo.active = false;
  220. this.hit.item.active = false;
  221. this.hasChange = true;
  222. this.changeBottle(0)
  223. return;
  224. }
  225. if(this.waveMng.game.gameMode == 'bingo' && !this.bingo.active) {
  226. if(!this.hasChange) {
  227. this.bingo.active = true;
  228. this.subline.item.active = false;
  229. this.changeBottle(9);
  230. return;
  231. }
  232. }
  233. if(this.waveMng.game.gameMode == 'hit') {
  234. let spawn = cc.spawn(cc.moveTo(0.15, cc.p(this.node.x, this.node.y - 250)), cc.fadeOut(0.15));
  235. this.node.runAction(spawn);
  236. this.scheduleOnce(() => {
  237. this.recycle();
  238. }, 0.15)
  239. } else if (this.node.x > this.vSize.width + 80) {
  240. this.node.active = false;
  241. this.recycle();
  242. } else {
  243. this.node.x += dt * this.waveMng.MoveX;
  244. }
  245. },
  246. recycle () {
  247. if(!this.finished) {
  248. // console.log('=========== recycle ===========');
  249. this.finished = true;
  250. this.waveMng.despawnBottle(this);
  251. }
  252. }
  253. });