Game.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import { Base64 } from "./Utils/Base64";
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. wrap: cc.Node,
  6. shooter: cc.Prefab,
  7. inGameUI: cc.Node,
  8. endUI: cc.Node,
  9. share1UI: cc.Node,
  10. share2UI: cc.Node,
  11. waveMng: cc.Node,
  12. bottleMng: cc.Node, //对象池[Bottle]
  13. shootWrap: cc.Node, //容器[shoot]
  14. bottleWrap: cc.Node, //容器[Bottle]
  15. modeHit: cc.Node, //必中模式
  16. audioMng: cc.Node, //音效管理
  17. tMask: cc.Node,
  18. tButton: cc.Node,
  19. },
  20. onLoad() {
  21. console.log('gameInit');
  22. // cc.director.getCollisionManager().enabled = true;
  23. let physicsManager = cc.director.getPhysicsManager();
  24. physicsManager.enabled = true;
  25. physicsManager.enabledAccumulator = true;
  26. physicsManager.FIXED_TIME_STEP = 1/20; //修改物理引擎更新步长
  27. // physicsManager.debugDrawFlags = 1;
  28. // cc.director.getCollisionManager().enabledDebugDraw = true;
  29. this.audioMng = this.audioMng.getComponent('AudioMng');
  30. this.bottleMng = this.bottleMng.getComponent('BottleMng');
  31. this.bottleMng.init(this);
  32. this.waveMng = this.waveMng.getComponent('WaveMng');
  33. this.waveMng.init(this);
  34. //UI 初始化
  35. this.inGameUI = this.inGameUI.getComponent('InGameUI');
  36. this.inGameUI.init(this);
  37. this.endUI = this.endUI.getComponent('EndUI');
  38. this.endUI.init(this);
  39. this.share1UI = this.share1UI.getComponent('Share1');
  40. this.share1UI.init(this);
  41. this.share2UI = this.share2UI.getComponent('Share2');
  42. this.share2UI.init(this);
  43. this.modeHit = this.modeHit.getComponent('BottleHit');
  44. this.modeHit.init(this);
  45. this.gameReviveCount = 0;
  46. },
  47. start() {
  48. this.gameStart()
  49. },
  50. gamePause() {
  51. this.waveMng.endWave();
  52. },
  53. resume() {
  54. },
  55. /**
  56. * 初始化箭矢
  57. */
  58. readyShooter() {
  59. let shooter = cc.instantiate(this.shooter)
  60. this.shootWrap.addChild(shooter);
  61. shooter.getComponent('Shooter').init(this);
  62. },
  63. resetShooter(node) {
  64. if(node) {
  65. this.delShooter(node);
  66. }
  67. this.readyShooter();
  68. },
  69. delShooter (node) {
  70. this.shootWrap.removeChild(node);
  71. },
  72. /**
  73. * 激活模式
  74. * @param {String} modeType 模式类型
  75. */
  76. activeMode(modeType) {
  77. console.log(this.gameMode, modeType);
  78. if (this.gameMode == modeType) {
  79. return
  80. } else {
  81. console.log(`=========== enterMode: ${modeType} ===========`);
  82. switch (modeType) {
  83. case 'subline':
  84. this.gameMode = 'subline';
  85. this.inGameUI.showSubline();
  86. break;
  87. case 'undead':
  88. this.gameMode = 'undead';
  89. this.inGameUI.showUndead();
  90. break;
  91. case 'bingo':
  92. this.gameMode = 'bingo';
  93. this.bingoHit = 0; //华彩模式下必中瓶模式完成次数,最多为1
  94. this.inGameUI.showBingo();
  95. break;
  96. case 'gold':
  97. this.inGameUI.addGold();
  98. break;
  99. case 'hit':
  100. this.gameMode = 'hit';
  101. this.bingoHit = 1; //华彩模式下必中瓶模式完成次数,最多为1
  102. break;
  103. default:
  104. break;
  105. }
  106. }
  107. },
  108. runHitMode (bottleType) {
  109. this.activeMode('hit');
  110. this.modeHit.startHit(bottleType);
  111. },
  112. /**
  113. * 重置为Normal模式
  114. */
  115. resetMode() {
  116. this.gameMode = 'normal';
  117. },
  118. /**
  119. * 游戏准备
  120. */
  121. gameReady() {
  122. console.log('=========== gameReady ===========');
  123. this.state = 'gameReady';
  124. this.gameMode = 'normal';
  125. this.tMask.active = true;
  126. this.tButton.active = true;
  127. },
  128. /**
  129. * 游戏开始
  130. */
  131. gameStart() {
  132. console.log('=========== gameStart ===========');
  133. /** 更新状态 */
  134. this.state = 'gameStart';
  135. this.gameMode = 'normal';
  136. /** 播放背景音乐 */
  137. this.audioMng.stopAll();
  138. this.scheduleOnce(() => {
  139. this.audioMng.playBgm();
  140. }, 0.3)
  141. /** 隐藏部分UI内容 */
  142. this.tMask.active = false;
  143. this.tButton.active = false;
  144. /** 准备箭矢 */
  145. this.readyShooter();
  146. /** 开始量产花瓶 */
  147. this.waveMng.startWave();
  148. },
  149. gameRestart() {
  150. cc.director.loadScene("Game");
  151. // this.gameStart()
  152. // this.share1UI.hide();
  153. // this.share2UI.hide();
  154. // this.endUI.hide();
  155. },
  156. gameContinue() {
  157. // this.waveMng.continueWave();
  158. /** 准备箭矢 */
  159. this.readyShooter();
  160. /** 重新播放背景音乐 */
  161. this.audioMng.playBgm();
  162. console.log('gameContinue');
  163. },
  164. /**
  165. * 游戏结束
  166. */
  167. gameOver() {
  168. this.audioMng.stopAll();
  169. this.scheduleOnce(() => {
  170. this.audioMng.playLose();
  171. }, 0.3)
  172. //上报游戏数据
  173. this.doUserReport();
  174. //暂停游戏
  175. // this.gamePause();
  176. // if (this.gameReviveCount == 0) {
  177. // //第一次复活,引导分享
  178. // this.share1UI.show(this.inGameUI.score)
  179. // this.gameReviveCount += 1;
  180. // } else if (this.gameReviveCount == 1) {
  181. // //第二次复活,使用复活卡,同时引导分享
  182. // this.share2UI.show(this.inGameUI.score)
  183. // this.gameReviveCount += 1;
  184. // } else if (this.gameReviveCount == 2) {
  185. // //游戏结束
  186. // this.state = 'gameOver';
  187. // this.waveMng.endWave();
  188. // this.endUI.show(this.inGameUI.score)
  189. // }
  190. if(this.gameReviveCount == 0) {
  191. this.share2UI.show(this.inGameUI.score)
  192. this.gameReviveCount += 1;
  193. } else if (this.gameReviveCount == 1) {
  194. //游戏结束
  195. this.state = 'gameOver';
  196. this.waveMng.endWave();
  197. this.endUI.show(this.inGameUI.score)
  198. }
  199. },
  200. doUserReport() {
  201. if(!window.Global.user) {
  202. return;
  203. }
  204. let postObj = {
  205. "coin": this.inGameUI.gold,
  206. "score": this.inGameUI.score,
  207. "uid": window.Global.user.uid
  208. }
  209. let ecData = Base64.encode(JSON.stringify(postObj))
  210. wx.request({
  211. url: Global.userReport,
  212. header: {
  213. "Content-Type": "application/x-www-form-urlencoded"
  214. },
  215. method: "POST",
  216. data: {
  217. token: window.Global.user.token,
  218. uid: window.Global.user.uid,
  219. channel: window.Global.channel,
  220. os: window.Global.os,
  221. ver: window.Global.ver,
  222. ecData: ecData
  223. },
  224. success: res => {
  225. console.log(res);
  226. }
  227. })
  228. },
  229. doShare() {
  230. let shareImg = [
  231. 'https://pub.dwstatic.com/wxgame/dwgame01/wxshare/share1.jpg',
  232. 'https://pub.dwstatic.com/wxgame/dwgame01/wxshare/share2.jpg',
  233. 'https://pub.dwstatic.com/wxgame/dwgame01/wxshare/share3.jpg'
  234. ]
  235. let shareText = [
  236. '这游戏怎么一玩上就停不下来呢!',
  237. '点一下~玩一年~皮肤不用一分钱!',
  238. `太虐了,欢乐投壶终于${this.inGameUI.score}分了!你能超过我吗?`
  239. ]
  240. let r1 = Math.floor(cc.random0To1() * 3);
  241. let r2 = Math.floor(cc.random0To1() * 3);
  242. if (wx) {
  243. wx.shareAppMessage({
  244. title: shareText[r1],
  245. imageUrl: shareImg[r2],
  246. query:"uid="+Global.user.uid
  247. })
  248. }
  249. },
  250. goMainPage() {
  251. cc.director.loadScene("MainMenu");
  252. },
  253. goFriendRank() {
  254. window.Global.showFriendRank = true;
  255. this.goMainPage();
  256. },
  257. goTotalRank() {
  258. window.Global.showTotalRank = true;
  259. this.goMainPage();
  260. }
  261. // update (dt) {},
  262. });