game.js 21 KB

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