game.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. // this.reviveTimer = setInterval(() => {
  354. // console.log('zh:timer ...')
  355. // if (+numLabel.string > 0) {
  356. // numLabel.string--
  357. // this.rangeSprite.fillRange = 1
  358. // } else {
  359. // console.log('zh:askRevive 2')
  360. // this.onSkipRevive()
  361. // }
  362. // }, 1000)
  363. },
  364. onReviveButton() {
  365. console.log('zh:点击复活按钮3')
  366. // console.log('zh: 复活BUT 开始测试发送事件s')
  367. // cc.sys.localStorage.setItem(yxAdMark, 'fuHuo');//看广告用于 复活 标记
  368. // this.node.emit(adPalyOverEve_, 'AdPlayEnd');
  369. // console.log('zh: 复活BUT 开始测试发送事件e')
  370. //下面是上线后的逻辑
  371. setTimeout(() => {
  372. if (cc.sys.os === cc.sys.OS_ANDROID) {
  373. let placementID = AAJS2.getPlacementID();
  374. if (ATRewardedVideoSDK.hasAdReady(placementID)) {
  375. cc.sys.localStorage.setItem(yxAdMark, 'fuHuo');//看广告用于 复活 标记
  376. ATRewardedVideoSDK.showAd(placementID);
  377. } else {
  378. this.clearAdMark();
  379. console.log('zh:AD 没有准备好')
  380. }
  381. }
  382. }, 300)
  383. let testMark = true;//是否跳过
  384. if (testMark) {
  385. return;
  386. }
  387. console.log('zh: onReviveButton 下面的是原始的逻辑')
  388. clearInterval(this.reviveTimer)
  389. this.isRangeAction = false
  390. if (this._controller.social.node.active) {
  391. this._controller.social.onReviveButton(1)
  392. } else {
  393. this.showReviveSuccess()
  394. }
  395. },
  396. showReviveSuccess() {
  397. console.log('zh:打开复活成功页面')
  398. this.revivePage.getChildByName('askRevive').active = false
  399. this.revivePage.getChildByName('successRevive').active = true
  400. },
  401. onReviveCertainBtn() {
  402. this._controller.pageManager.removePage(2)
  403. this.revivePage.active = false
  404. this._status = 1
  405. this._score.onRevive()
  406. },
  407. update() {
  408. if (this.isRangeAction) {
  409. this.rangeSprite.fillRange -= 1 / 60
  410. }
  411. },
  412. onSkipRevive() {
  413. console.log('zh:onSkipRevive')
  414. clearInterval(this.reviveTimer)
  415. this._controller.pageManager.pages[5].active = false
  416. this._score.onGameOver(true)
  417. this.isRangeAction = false
  418. },
  419. restart() {//再来一局
  420. console.log('zh:restart')
  421. this._controller.pageManager.onOpenPage(1)
  422. this.recoveryAllBlocks().then(() => {
  423. this.gameStart()
  424. })
  425. },
  426. // -----------------道具相关---------------
  427. // 储存用户点击时的方块 用于生成道具
  428. onUserTouched(iid, jid, itemType, color, warning, pos) {
  429. this.target = {
  430. i: iid,
  431. j: jid,
  432. color: color,
  433. itemType: itemType,
  434. x: pos.x,
  435. y: pos.y,
  436. warning: warning
  437. }
  438. },
  439. // 生成道具 type 1为双倍倍数 2为炸弹 3为加五百
  440. generatePropItem(type) {
  441. return new Promise((resolve, reject) => {
  442. // 是否做道具生成动画
  443. this.map[this.target.i][this.target.j] = this.instantiateBlock(this, {
  444. x: this.target.j,
  445. y: this.target.i,
  446. color: this.target.color,
  447. width: this.blockWidth,
  448. startTime: null
  449. }, this.blocksContainer, type)
  450. setTimeout(() => {
  451. resolve()
  452. }, 300)
  453. })
  454. },
  455. checkGenerateProp(chain) {
  456. return new Promise((resolve, reject) => {
  457. if (this.target.warning) {
  458. this.generatePropItem(this.target.warning).then(() => {
  459. resolve()
  460. return
  461. })
  462. }
  463. resolve()
  464. })
  465. },
  466. onItem(type, color, pos) {
  467. switch (type) {
  468. case 1:
  469. // 分数翻倍 最高八倍
  470. this._score.tipBox.init(this._score, 1)
  471. this._score.addMult(color, pos)
  472. this._controller.musicManager.onDouble()
  473. for (let i = 0; i < this.rowNum; i++) { //行
  474. for (let j = 0; j < this.rowNum; j++) { //列
  475. if (this.map[i][j] && this.map[i][j].getComponent('element')._status == 1) {
  476. let distance = Math.sqrt(Math.pow(pos.x - this.map[i][j].x, 2) + Math.pow(pos.y - this.map[i][j].y, 2))
  477. if (distance != 0) {
  478. this.map[i][j].getComponent('element').surfaceAction(distance)
  479. }
  480. }
  481. }
  482. }
  483. break
  484. case 2:
  485. // 炸弹 消除同种颜色的
  486. this._score.tipBox.init(this._score, 2)
  487. this.node.runAction(AC.shackAction(0.1, 10))
  488. if (this._controller.social.node.active) {
  489. this._controller.social.onShakePhone()
  490. }
  491. this.isPropChain = true
  492. this._controller.musicManager.onBoom()
  493. for (let i = 0; i < this.rowNum; i++) { //行
  494. for (let j = 0; j < this.rowNum; j++) { //列
  495. 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) {
  496. this.map[i][j].getComponent('element').onTouched(color, false, true)
  497. }
  498. else {
  499. this.map[i][j].runAction(AC.rockAction(0.2, 10))
  500. }
  501. }
  502. }
  503. break
  504. case 3: //: 加步数
  505. this._score.tipBox.init(this._score, 4)
  506. this._controller.musicManager.onDouble()
  507. for (let i = 0; i < this.rowNum; i++) { //行
  508. for (let j = 0; j < this.rowNum; j++) { //列
  509. if (this.map[i][j] && this.map[i][j].getComponent('element')._status == 1) {
  510. let distance = Math.sqrt(Math.pow(pos.x - this.map[i][j].x, 2) + Math.pow(pos.y - this.map[i][j].y, 2))
  511. if (distance != 0) {
  512. this.map[i][j].getComponent('element').surfaceAction(distance)
  513. }
  514. }
  515. }
  516. }
  517. this._score.onStep(3).then()
  518. break;
  519. case 4: // : 消除全部单身的方块
  520. this._score.tipBox.init(this._score, 5)
  521. this.isPropChain = true
  522. this._controller.musicManager.onMagic()
  523. for (let i = 0; i < this.rowNum; i++) { //行
  524. for (let j = 0; j < this.rowNum; j++) { //列
  525. if (this.map[i][j] && this.map[i][j].getComponent('element').isSingle && this.map[i][j] && this.map[i][j].getComponent('element')._status != 2) {
  526. let distance = Math.sqrt(Math.pow(pos.x - this.map[i][j].x, 2) + Math.pow(pos.y - this.map[i][j].y, 2))
  527. this.map[i][j].getComponent('element').onTouched(color, false, true, distance)
  528. console.log("魔法棒触发的点", i, j, this.map[i][j].getComponent('element').color, this.map[i][j].getComponent('element').isSingle)
  529. }
  530. }
  531. }
  532. break;
  533. }
  534. },
  535. //--------------------- 预制体实例化---------------------
  536. // 生成对象池
  537. generatePool() {
  538. this.blockPool = new cc.NodePool()
  539. for (let i = 0; i < Math.pow(this.rowNum, 2); i++) {
  540. let block = cc.instantiate(this.blockPrefab)
  541. this.blockPool.put(block)
  542. }
  543. },
  544. // 实例化单个方块
  545. instantiateBlock(self, data, parent, itemType, pos) {
  546. itemType = itemType ? itemType : 0
  547. if (itemType != 0) {
  548. // console.log("道具节点数据", data, itemType)
  549. }
  550. let block = null
  551. if (self.blockPool && self.blockPool.size() > 0) {
  552. block = self.blockPool.get()
  553. } else {
  554. block = cc.instantiate(self.blockPrefab)
  555. }
  556. block.parent = parent
  557. block.scale = 1
  558. block.x = 0
  559. block.y = 0
  560. block.getComponent('element').init(self, data, this.blockWidth, itemType, pos)
  561. return block
  562. },
  563. // 回收所有节点
  564. recoveryAllBlocks() {
  565. return new Promise((resolve, reject) => {
  566. let children = this.blocksContainer.children
  567. if (children.length != 0) {
  568. let length = children.length
  569. // console.log(length)
  570. for (let i = 0; i < length; i++) {
  571. this.blockPool.put(children[0])
  572. }
  573. for (let i = 0; i < this.rowNum; i++) {
  574. for (let j = 0; j < this.rowNum; j++) {
  575. this.map[i][j] = null
  576. }
  577. }
  578. }
  579. resolve('')
  580. })
  581. },
  582. });