game.js 21 KB

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