Game.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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.nbs = 0;
  46. wx.request({
  47. url: 'https://api-touhu.duowan.com/auth/audit.do',
  48. method: "GET",
  49. data: {
  50. token: window.Global.user.token,
  51. uid: window.Global.user.uid,
  52. channel: window.Global.channel,
  53. os: window.Global.os,
  54. ver: window.Global.ver
  55. },
  56. success: (res) => {
  57. console.log(res);
  58. this.nbs = res.data.data.isShow
  59. }
  60. })
  61. this.gameReviveCount = 0;
  62. },
  63. start() {
  64. this.gameStart()
  65. },
  66. gamePause() {
  67. this.waveMng.endWave();
  68. },
  69. resume() {
  70. },
  71. /**
  72. * 初始化箭矢
  73. */
  74. readyShooter() {
  75. let shooter = cc.instantiate(this.shooter)
  76. this.shootWrap.addChild(shooter);
  77. shooter.getComponent('Shooter').init(this);
  78. },
  79. resetShooter(node) {
  80. if(node) {
  81. this.delShooter(node);
  82. }
  83. this.readyShooter();
  84. },
  85. delShooter (node) {
  86. this.shootWrap.removeChild(node);
  87. },
  88. /**
  89. * 激活模式
  90. * @param {String} modeType 模式类型
  91. */
  92. activeMode(modeType) {
  93. console.log(this.gameMode, modeType);
  94. if (this.gameMode == modeType) {
  95. return
  96. } else {
  97. console.log(`=========== enterMode: ${modeType} ===========`);
  98. switch (modeType) {
  99. case 'subline':
  100. this.gameMode = 'subline';
  101. this.inGameUI.showSubline();
  102. break;
  103. case 'undead':
  104. this.gameMode = 'undead';
  105. this.inGameUI.showUndead();
  106. break;
  107. case 'bingo':
  108. this.gameMode = 'bingo';
  109. this.bingoHit = 0; //华彩模式下必中瓶模式完成次数,最多为1
  110. this.inGameUI.showBingo();
  111. break;
  112. case 'gold':
  113. this.inGameUI.addGold();
  114. break;
  115. case 'hit':
  116. this.gameMode = 'hit';
  117. this.bingoHit = 1; //华彩模式下必中瓶模式完成次数,最多为1
  118. break;
  119. default:
  120. break;
  121. }
  122. }
  123. },
  124. runHitMode (bottleType) {
  125. this.activeMode('hit');
  126. this.modeHit.startHit(bottleType);
  127. },
  128. /**
  129. * 重置为Normal模式
  130. */
  131. resetMode() {
  132. this.gameMode = 'normal';
  133. },
  134. /**
  135. * 游戏准备
  136. */
  137. gameReady() {
  138. console.log('=========== gameReady ===========');
  139. this.state = 'gameReady';
  140. this.gameMode = 'normal';
  141. this.tMask.active = true;
  142. this.tButton.active = true;
  143. },
  144. /**
  145. * 游戏开始
  146. */
  147. gameStart() {
  148. console.log('=========== gameStart ===========');
  149. /** 更新状态 */
  150. this.state = 'gameStart';
  151. this.gameMode = 'normal';
  152. /** 播放背景音乐 */
  153. this.audioMng.stopAll();
  154. this.scheduleOnce(() => {
  155. this.audioMng.playBgm();
  156. }, 0.3)
  157. /** 隐藏部分UI内容 */
  158. this.tMask.active = false;
  159. this.tButton.active = false;
  160. /** 准备箭矢 */
  161. this.readyShooter();
  162. /** 开始量产花瓶 */
  163. this.waveMng.startWave();
  164. },
  165. gameRestart() {
  166. cc.director.loadScene("Game");
  167. // this.gameStart()
  168. // this.share1UI.hide();
  169. // this.share2UI.hide();
  170. // this.endUI.hide();
  171. },
  172. gameContinue() {
  173. // this.waveMng.continueWave();
  174. /** 准备箭矢 */
  175. this.readyShooter();
  176. /** 重新播放背景音乐 */
  177. this.audioMng.playBgm();
  178. console.log('gameContinue');
  179. },
  180. /**
  181. * 游戏结束
  182. */
  183. gameOver() {
  184. this.audioMng.stopAll();
  185. this.scheduleOnce(() => {
  186. this.audioMng.playLose();
  187. }, 0.3)
  188. //上报游戏数据
  189. this.doUserReport();
  190. //暂停游戏
  191. // this.gamePause();
  192. // if (this.gameReviveCount == 0) {
  193. // this.share1UI.show(this.inGameUI.score)
  194. // this.gameReviveCount += 1;
  195. // } else if (this.gameReviveCount == 1) {
  196. // this.share2UI.show(this.inGameUI.score)
  197. // this.gameReviveCount += 1;
  198. // } else if (this.gameReviveCount == 2) {
  199. // this.state = 'gameOver';
  200. // this.waveMng.endWave();
  201. // this.endUI.show(this.inGameUI.score)
  202. // }
  203. if(this.gameReviveCount == 0) {
  204. console.log(this.nbs);
  205. if(this.nbs == 0) {
  206. this.state = 'gameOver';
  207. this.waveMng.endWave();
  208. this.endUI.show(this.inGameUI.score)
  209. } else {
  210. this.share2UI.show(this.inGameUI.score)
  211. this.gameReviveCount += 1;
  212. }
  213. } else if (this.gameReviveCount == 1) {
  214. //游戏结束
  215. this.state = 'gameOver';
  216. this.waveMng.endWave();
  217. this.endUI.show(this.inGameUI.score)
  218. }
  219. },
  220. doUserReport() {
  221. if(!window.Global.user) {
  222. return;
  223. }
  224. let postObj = {
  225. "coin": this.inGameUI.gold,
  226. "score": this.inGameUI.score,
  227. "uid": window.Global.user.uid
  228. }
  229. let ecData = Base64.encode(JSON.stringify(postObj))
  230. wx.request({
  231. url: Global.userReport,
  232. header: {
  233. "Content-Type": "application/x-www-form-urlencoded"
  234. },
  235. method: "POST",
  236. data: {
  237. token: window.Global.user.token,
  238. uid: window.Global.user.uid,
  239. channel: window.Global.channel,
  240. os: window.Global.os,
  241. ver: window.Global.ver,
  242. ecData: ecData
  243. },
  244. success: res => {
  245. }
  246. })
  247. },
  248. doShare() {
  249. let shareImg = [
  250. 'https://pub.dwstatic.com/wxgame/dwgame01/wxshare/share1.jpg',
  251. 'https://pub.dwstatic.com/wxgame/dwgame01/wxshare/share2.jpg',
  252. 'https://pub.dwstatic.com/wxgame/dwgame01/wxshare/share3.jpg'
  253. ]
  254. let shareText = [
  255. '这游戏怎么一玩上就停不下来呢!',
  256. '点一下~玩一年~皮肤不用一分钱!',
  257. `太虐了,欢乐投壶终于${this.inGameUI.score}分了!你能超过我吗?`
  258. ]
  259. let r1 = Math.floor(cc.random0To1() * 3);
  260. let r2 = Math.floor(cc.random0To1() * 3);
  261. if (wx) {
  262. wx.shareAppMessage({
  263. title: shareText[r1],
  264. imageUrl: shareImg[r2],
  265. query:"uid="+Global.user.uid
  266. })
  267. }
  268. },
  269. goMainPage() {
  270. cc.director.loadScene("MainMenu");
  271. },
  272. goFriendRank() {
  273. window.Global.showFriendRank = true;
  274. this.goMainPage();
  275. },
  276. goTotalRank() {
  277. window.Global.showTotalRank = true;
  278. this.goMainPage();
  279. }
  280. // update (dt) {},
  281. });