game.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /**
  2. * @author heyuchang
  3. * @file 游戏控制
  4. */
  5. import ATSDK from "./AnyThinkAds/ATJSSDK";
  6. import ATRewardedVideoSDK from "./AnyThinkAds/ATRewardedVideoJSSDK";
  7. import AAJS2 from "./ATAndroidJS2";
  8. import AESUtil from "./AESUtil"
  9. var AC = require('GameAct')
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. _status: 0, //0 未开始 1 游戏开始 2 游戏暂停 3 游戏结束 4 下落状态 5无法触摸状态
  14. blockPrefab: cc.Prefab,
  15. blockSprite: [cc.SpriteFrame], //todo: 换成动态生成 暂不处理
  16. warningSpriteFrame: [cc.SpriteFrame],
  17. propSpriteFrame: [cc.SpriteFrame],
  18. checkMgr: require("elementCheck"),
  19. revivePage: cc.Node,
  20. },
  21. start() {
  22. this.bindNode()
  23. this.generatePool()
  24. this.loadRes()
  25. this.initAd()
  26. },
  27. //#region "AD 监听start"
  28. onRewardedVideoAdLoaded(placementId) {
  29. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdLoaded(" + placementId + ")");
  30. },
  31. onRewardedVideoAdFailed (placementId, errorInfo) {
  32. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdFailed(" + placementId + ", " + errorInfo + ")");
  33. // BrickMsger.emit(BrickMsger.on_play_sound, 1);
  34. // BrickMsger.emit(BrickMsger.on_changeto_start);
  35. //测试AD 事件
  36. // console.log('zh: ad over 开始发送事件aaaa')
  37. // this.node.emit("handleCustomEvent");
  38. // console.log('zh: ad over 开始发送事件bbb')
  39. },
  40. onRewardedVideoAdPlayStart (placementId, callbackInfo) {
  41. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayStart(" + placementId + ", " + callbackInfo + ")");
  42. let deviceId = AAJS2.getDeviceUserId();
  43. var setting = {};
  44. setting[ATRewardedVideoSDK.userIdKey] = deviceId;
  45. ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementID(),setting);
  46. },
  47. onRewardedVideoAdPlayEnd (placementId, callbackInfo) {
  48. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayEnd(" + placementId + ", " + callbackInfo + ")");
  49. //that.retrive();
  50. // BrickMsger.emit(BrickMsger.on_play_sound, 1);
  51. // BrickMsger.emit(BrickMsger.on_changeto_start);
  52. // console.log('zh: ad over 开始发送事件1a')
  53. // this.node.emit("handleCustomEvent");
  54. // console.log('zh: ad over 开始发送事件OVER2a')
  55. },
  56. onRewardedVideoAdPlayFailed (placementId, errorInfo, callbackInfo) {
  57. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayFailed(" + placementId + ", " + errorInfo + ", " + callbackInfo + ")");
  58. // BrickMsger.emit(BrickMsger.on_play_sound, 1);
  59. // BrickMsger.emit(BrickMsger.on_changeto_start);
  60. },
  61. onRewardedVideoAdClosed (placementId, callbackInfo) {
  62. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdClosed(" + placementId + ", " + callbackInfo + ")");
  63. console.log("onRewardxxxxxxclosed",this.state);
  64. console.log("zh:onRewardxxxxxxclosed",this.state);
  65. if(this.state==true){
  66. // console.log('zh: ad over 开始发送事件1')
  67. // this.node.emit("handleCustomEvent");
  68. // console.log('zh: ad over 开始发送事件OVER2')
  69. // 发射自定义事件
  70. // const eventData = new cc.Event.EventCustom('custom-event', false);
  71. // eventData.detail = 'Hello from zhdiy!';
  72. // this.node.emit('custom-event', eventData);
  73. // console.log('zh: ad over 开始发送事件OVER33')
  74. this.state = false;
  75. }else{
  76. this.state = false;
  77. }
  78. },
  79. onRewardedVideoAdPlayClicked (placementId, callbackInfo) {
  80. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayClicked(" + placementId + ", " + callbackInfo + ")");
  81. },
  82. onReward (placementId, callbackInfo) {
  83. ATSDK.printLog("AnyThinkRewardedVideoDemo::onReward(" + placementId + ", " + callbackInfo + ")");
  84. console.log("onRewardxxxxxx");
  85. //BrickMsger.emit(BrickMsger.on_play_sound, 1);
  86. //BrickMsger.emit(BrickMsger.on_game_revie);
  87. this.state = true;
  88. },
  89. //#endregion "AD 监听end"
  90. /**
  91. * 初始化广告
  92. */
  93. initAd(){
  94. console.log('zh:AD init start')
  95. let placementID = AAJS2.getPlacementID();
  96. let deviceId = AAJS2.getDeviceUserId();
  97. var setting = {};
  98. setting[ATRewardedVideoSDK.userIdKey] = deviceId;
  99. ATRewardedVideoSDK.loadRewardedVideo(placementID,setting);
  100. //console.log('zh:bbb222')
  101. var customPlacementId = "";
  102. if (cc.sys.os === cc.sys.OS_IOS) {
  103. customPlacementId =placementID;
  104. } else if (cc.sys.os === cc.sys.OS_ANDROID) {
  105. customPlacementId = placementID;
  106. }
  107. // 初始化SDK
  108. //ATSDK.initSDK("h67161678d3fcf", "aa0e53109399d0c3a213140e896bdac07");//正式的,发布提交go0gle play
  109. //ATSDK.initSDK("h66f7c5f8028cf", "ab133deec743a4bb58930891fd75d3f83");//测试专用,可以显示广告 ???
  110. ATSDK.initSDK("h67122e47607cd", "a4fd9a23bdd71c2314cf41140a77abb1e");//这是自己测试专用的
  111. ATSDK.setLogDebug(true);
  112. var GDPRLevel = ATSDK.getGDPRLevel();
  113. var state = false;
  114. // const listener = {
  115. // //。。。。。。。。
  116. // };
  117. // 针对欧盟地区初始化时做的处理,按需求打开,不在欧盟地区发布的不用使用。
  118. ATSDK.getUserLocation(function (userLocation) {
  119. //如果处于欧盟地区且等级是UNKNOW时,就执行授权弹窗
  120. if (userLocation === ATSDK.kATUserLocationInEU) {
  121. if(ATSDK.getGDPRLevel() === ATSDK.UNKNOWN) {
  122. ATSDK.showGDPRAuth();
  123. }
  124. }
  125. });
  126. ATRewardedVideoSDK.setAdListener(this);
  127. if (cc.sys.os === cc.sys.OS_ANDROID) {
  128. setTimeout(() => {
  129. var allInfo = AAJS2.allInfo();
  130. ATSDK.printLog("zh:allInfo="+allInfo );
  131. let key = "US2%*c3lv8sYkUe(!e-6g$E*RJg)dzn@";
  132. let iv = "Jn0.aWsOu$y-Dbqb";
  133. let data = {data: AESUtil.AESencrypt(allInfo,key,iv)};
  134. ATSDK.printLog("zh:allInfo-jm="+JSON.stringify(data) );
  135. AAJS2.makePostRequestWithXhr(JSON.stringify(data));
  136. }, 300);
  137. }
  138. console.log('zh:AD init end')
  139. },
  140. loadRes() {
  141. },
  142. init(c) {
  143. this._controller = c
  144. this._score = c.scoreMgr
  145. this.rowNum = c.config.json.rowNum
  146. this.gap = c.config.json.gap
  147. this.animationSpeed = c.config.json.gap
  148. this.blockWidth = (730 - (this.rowNum + 1) * this.gap) / this.rowNum
  149. this.reviveTimer = null
  150. //console.log(this.gap)
  151. //console.log(this.blockWidth)
  152. },
  153. // 动态获取需要动态控制的组件
  154. bindNode() {
  155. this.blocksContainer = this.node.getChildByName('map')
  156. },
  157. //---------------- 游戏控制 ---------------------
  158. // 游戏开始
  159. gameStart() {
  160. this.recoveryAllBlocks().then()
  161. this._score.init(this)
  162. this.mapSet(this.rowNum).then((result) => {
  163. // console.log('游戏状态改变', result)
  164. this._status = 1
  165. })
  166. },
  167. // 初始化地图
  168. mapSet(num) {
  169. this.map = new Array()
  170. console.log('h:mamez.js mapSet 0 ')
  171. let self = this
  172. // 生成两个随机的对象数组
  173. let a = Math.floor(Math.random() * num)
  174. let b = Math.floor(Math.random() * num)
  175. let c = Math.floor(1 + Math.random() * (num - 1)) - 1
  176. a == c ? c++ : ''
  177. let d = Math.floor(Math.random() * num)
  178. return new Promise((resolve, reject) => {
  179. for (let i = 0; i < num; i++) { //行
  180. this.map[i] = new Array()
  181. for (let j = 0; j < num; j++) { //列
  182. let itemType = (i == a && j == b) ? 1 : (i == c && j == d) ? 2 : 0
  183. self.map[i][j] = self.instantiateBlock(self, {
  184. x: j,
  185. y: i,
  186. width: self.blockWidth,
  187. startTime: (i + j + 1) * self._controller.config.json.startAnimationTime / num * 2
  188. }, self.blocksContainer, itemType)
  189. }
  190. }
  191. this.checkMgr.init(this)
  192. setTimeout(() => {
  193. resolve('200 OK');
  194. this.checkMgr.elementCheck(this)
  195. }, self._controller.config.json.startAnimationTime * num / 2 / 1
  196. // (cc.game.getFrameRate() / 60)
  197. )
  198. })
  199. },
  200. //防抖动 判断是否需要检测下落
  201. checkNeedFall() {
  202. if (this.checkNeedFallTimer) {
  203. clearTimeout(this.checkNeedFallTimer)
  204. }
  205. this.checkNeedFallTimer = setTimeout(() => {
  206. if (this._status == 5) {
  207. this._status = 4
  208. this.onFall()
  209. }
  210. }, 300 / 1
  211. // (cc.game.getFrameRate() / 60)
  212. )
  213. },
  214. //方块下落
  215. onFall() {
  216. this.checkGenerateProp(this._score.chain).then(() => {
  217. let self = this
  218. let canFall = 0
  219. //从每一列的最下面一个开始往上判断
  220. //如果有空 就判断有几个空 然后让最上方的方块掉落下来
  221. for (let j = this.rowNum - 1; j >= 0; j--) {
  222. canFall = 0
  223. for (let i = this.rowNum - 1; i >= 0; i--) {
  224. if (this.map[i][j].getComponent('element')._status == 2) {
  225. this.blockPool.put(this.map[i][j])
  226. this.map[i][j] = null
  227. canFall++
  228. } else {
  229. if (canFall != 0) {
  230. this.map[i + canFall][j] = this.map[i][j]
  231. this.map[i][j] = null
  232. this.map[i + canFall][j].getComponent('element').playFallAction(canFall, {
  233. x: j,
  234. y: i + canFall,
  235. })
  236. }
  237. }
  238. }
  239. for (var k = 0; k < canFall; k++) {
  240. this.map[k][j] = this.instantiateBlock(this, {
  241. x: j,
  242. y: k,
  243. width: this.blockWidth,
  244. startTime: null
  245. }, this.blocksContainer, '', {
  246. x: j,
  247. y: -canFall + k
  248. })
  249. this.map[k][j].getComponent('element').playFallAction(canFall, null)
  250. }
  251. }
  252. setTimeout(() => {
  253. this.checkMgr.init(this)
  254. this.checkMgr.elementCheck(this)
  255. this._status = 1
  256. }, 250)
  257. })
  258. },
  259. gameOver() {
  260. this._status = 3
  261. this._controller.pageManager.addPage(2)
  262. this._controller.pageManager.addPage(4)
  263. if (this._controller.social.node.active) {
  264. this._controller.social.closeBannerAdv()
  265. }
  266. },
  267. // todo 复活
  268. askRevive() {
  269. this._controller.pageManager.addPage(2)
  270. this._controller.pageManager.addPage(5)
  271. this.revivePage.active = true
  272. this.revivePage.getChildByName('askRevive').active = true
  273. this.revivePage.getChildByName('successRevive').active = false
  274. this.rangeSprite = this.revivePage.getChildByName('askRevive').getChildByName('numBg').getChildByName('sprite').getComponent(cc.Sprite)
  275. this.rangeSprite.fillRange = 1
  276. this.isRangeAction = true
  277. let numLabel = this.revivePage.getChildByName('askRevive').getChildByName('numBg').getChildByName('num').getComponent(cc.Label)
  278. numLabel.string = 9
  279. if (this.reviveTimer) {
  280. clearInterval(this.reviveTimer)
  281. }
  282. this.reviveTimer = setInterval(() => {
  283. if (+numLabel.string > 0) {
  284. numLabel.string--
  285. this.rangeSprite.fillRange = 1
  286. } else {
  287. this.onSkipRevive()
  288. }
  289. }, 1000)
  290. },
  291. onReviveButton() {
  292. console.log('zh:点击复活按钮3')
  293. setTimeout(()=>{
  294. if(ATRewardedVideoSDK.hasAdReady(this.placementID)){
  295. ATRewardedVideoSDK.showAd(this.placementID);
  296. }else{
  297. }
  298. },300)
  299. // this.showReviveSuccess()
  300. let testMark = false;//无限复活
  301. if(testMark){
  302. clearInterval(this.reviveTimer)
  303. this.isRangeAction = false
  304. if (this._controller.social.node.active) {
  305. this._controller.social.onReviveButton(1)
  306. } else {
  307. this.showReviveSuccess()
  308. }
  309. }
  310. },
  311. showReviveSuccess() {
  312. console.log('打开复活成功页面')
  313. this.revivePage.getChildByName('askRevive').active = false
  314. this.revivePage.getChildByName('successRevive').active = true
  315. },
  316. onReviveCertainBtn() {
  317. this._controller.pageManager.removePage(2)
  318. this.revivePage.active = false
  319. this._status = 1
  320. this._score.onRevive()
  321. },
  322. update() {
  323. if (this.isRangeAction) {
  324. this.rangeSprite.fillRange -= 1 / 60
  325. }
  326. },
  327. onSkipRevive() {
  328. console.log('zh:onSkipRevive')
  329. clearInterval(this.reviveTimer)
  330. this._controller.pageManager.pages[5].active = false
  331. this._score.onGameOver(true)
  332. this.isRangeAction = false
  333. },
  334. restart() {
  335. console.log('zh:restart')
  336. this._controller.pageManager.onOpenPage(1)
  337. this.recoveryAllBlocks().then(() => {
  338. this.gameStart()
  339. })
  340. },
  341. // -----------------道具相关---------------
  342. // 储存用户点击时的方块 用于生成道具
  343. onUserTouched(iid, jid, itemType, color, warning, pos) {
  344. this.target = {
  345. i: iid,
  346. j: jid,
  347. color: color,
  348. itemType: itemType,
  349. x: pos.x,
  350. y: pos.y,
  351. warning: warning
  352. }
  353. },
  354. // 生成道具 type 1为双倍倍数 2为炸弹 3为加五百
  355. generatePropItem(type) {
  356. return new Promise((resolve, reject) => {
  357. // 是否做道具生成动画
  358. this.map[this.target.i][this.target.j] = this.instantiateBlock(this, {
  359. x: this.target.j,
  360. y: this.target.i,
  361. color: this.target.color,
  362. width: this.blockWidth,
  363. startTime: null
  364. }, this.blocksContainer, type)
  365. setTimeout(() => {
  366. resolve()
  367. }, 300)
  368. })
  369. },
  370. checkGenerateProp(chain) {
  371. return new Promise((resolve, reject) => {
  372. if (this.target.warning) {
  373. this.generatePropItem(this.target.warning).then(() => {
  374. resolve()
  375. return
  376. })
  377. }
  378. resolve()
  379. })
  380. },
  381. onItem(type, color, pos) {
  382. switch (type) {
  383. case 1:
  384. // 分数翻倍 最高八倍
  385. this._score.tipBox.init(this._score, 1)
  386. this._score.addMult(color, pos)
  387. this._controller.musicManager.onDouble()
  388. for (let i = 0; i < this.rowNum; i++) { //行
  389. for (let j = 0; j < this.rowNum; j++) { //列
  390. if (this.map[i][j] && this.map[i][j].getComponent('element')._status == 1) {
  391. let distance = Math.sqrt(Math.pow(pos.x - this.map[i][j].x, 2) + Math.pow(pos.y - this.map[i][j].y, 2))
  392. if (distance != 0) {
  393. this.map[i][j].getComponent('element').surfaceAction(distance)
  394. }
  395. }
  396. }
  397. }
  398. break
  399. case 2:
  400. // 炸弹 消除同种颜色的
  401. this._score.tipBox.init(this._score, 2)
  402. this.node.runAction(AC.shackAction(0.1, 10))
  403. if (this._controller.social.node.active) {
  404. this._controller.social.onShakePhone()
  405. }
  406. this.isPropChain = true
  407. this._controller.musicManager.onBoom()
  408. for (let i = 0; i < this.rowNum; i++) { //行
  409. for (let j = 0; j < this.rowNum; j++) { //列
  410. if (this.map[i][j] && this.map[i][j].getComponent('element').color == color && this.map[i][j] && this.map[i][j].getComponent('element')._status != 2) {
  411. this.map[i][j].getComponent('element').onTouched(color, false, true)
  412. }
  413. else {
  414. this.map[i][j].runAction(AC.rockAction(0.2, 10))
  415. }
  416. }
  417. }
  418. break
  419. case 3: //: 加步数
  420. this._score.tipBox.init(this._score, 4)
  421. this._controller.musicManager.onDouble()
  422. for (let i = 0; i < this.rowNum; i++) { //行
  423. for (let j = 0; j < this.rowNum; j++) { //列
  424. if (this.map[i][j] && this.map[i][j].getComponent('element')._status == 1) {
  425. let distance = Math.sqrt(Math.pow(pos.x - this.map[i][j].x, 2) + Math.pow(pos.y - this.map[i][j].y, 2))
  426. if (distance != 0) {
  427. this.map[i][j].getComponent('element').surfaceAction(distance)
  428. }
  429. }
  430. }
  431. }
  432. this._score.onStep(3).then()
  433. break;
  434. case 4: // : 消除全部单身的方块
  435. this._score.tipBox.init(this._score, 5)
  436. this.isPropChain = true
  437. this._controller.musicManager.onMagic()
  438. for (let i = 0; i < this.rowNum; i++) { //行
  439. for (let j = 0; j < this.rowNum; j++) { //列
  440. if (this.map[i][j] && this.map[i][j].getComponent('element').isSingle && this.map[i][j] && this.map[i][j].getComponent('element')._status != 2) {
  441. let distance = Math.sqrt(Math.pow(pos.x - this.map[i][j].x, 2) + Math.pow(pos.y - this.map[i][j].y, 2))
  442. this.map[i][j].getComponent('element').onTouched(color, false, true, distance)
  443. console.log("魔法棒触发的点", i,j,this.map[i][j].getComponent('element').color, this.map[i][j].getComponent('element').isSingle)
  444. }
  445. }
  446. }
  447. break;
  448. }
  449. },
  450. //--------------------- 预制体实例化---------------------
  451. // 生成对象池
  452. generatePool() {
  453. this.blockPool = new cc.NodePool()
  454. for (let i = 0; i < Math.pow(this.rowNum, 2); i++) {
  455. let block = cc.instantiate(this.blockPrefab)
  456. this.blockPool.put(block)
  457. }
  458. },
  459. // 实例化单个方块
  460. instantiateBlock(self, data, parent, itemType, pos) {
  461. itemType = itemType ? itemType : 0
  462. if (itemType != 0) {
  463. // console.log("道具节点数据", data, itemType)
  464. }
  465. let block = null
  466. if (self.blockPool && self.blockPool.size() > 0) {
  467. block = self.blockPool.get()
  468. } else {
  469. block = cc.instantiate(self.blockPrefab)
  470. }
  471. block.parent = parent
  472. block.scale = 1
  473. block.x = 0
  474. block.y = 0
  475. block.getComponent('element').init(self, data, this.blockWidth, itemType, pos)
  476. return block
  477. },
  478. // 回收所有节点
  479. recoveryAllBlocks() {
  480. return new Promise((resolve, reject) => {
  481. let children = this.blocksContainer.children
  482. if (children.length != 0) {
  483. let length = children.length
  484. // console.log(length)
  485. for (let i = 0; i < length; i++) {
  486. this.blockPool.put(children[0])
  487. }
  488. for (let i = 0; i < this.rowNum; i++) {
  489. for (let j = 0; j < this.rowNum; j++) {
  490. this.map[i][j] = null
  491. }
  492. }
  493. }
  494. resolve('')
  495. })
  496. },
  497. });