game_main.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. import TileBlock from "./TileBlock"
  2. import game_level_cfg, { LevelData } from "./game_level_cfg"
  3. import game_config_dyn from "./game_config_dyn"
  4. import game_helpers from "./game_helpers"
  5. import global_model from "./global_model"
  6. import game_core from "./game_core"
  7. import game_constants from "./game_constants"
  8. import ATSDK from "./AnyThinkAds/ATJSSDK";
  9. import ATRewardedVideoSDK from "./AnyThinkAds/ATRewardedVideoJSSDK";
  10. import AAJS2 from "./ATAndroidJS2";
  11. import AESUtil from './AESUtil';
  12. const { ccclass, property } = cc._decorator;
  13. const TILE_WIDTH: number = 88
  14. const TILE_HEIGHT: number = 88
  15. @ccclass
  16. export default class NewClass extends cc.Component {
  17. @property(cc.Node)
  18. tileContainer: cc.Node = null;
  19. @property(cc.Node)
  20. targetNode: cc.Node = null;
  21. tileList: TileBlock[] = []
  22. matchList: TileBlock[] = []
  23. recordList: TileBlock[] = []
  24. lvData: LevelData
  25. lock: boolean = false//做动画用防止误点
  26. lastClickTime: number = 0 // 防抖初始化
  27. @property(cc.Label)
  28. level_txt: cc.Label = null
  29. @property(cc.Label)
  30. txt_undo: cc.Label = null
  31. @property(cc.Label)
  32. txt_shuffle: cc.Label = null
  33. @property(cc.Label)
  34. txt_hint: cc.Label = null
  35. @property(cc.Label)
  36. txt_put3: cc.Label = null
  37. //分数计算
  38. @property(cc.ProgressBar)
  39. node_progress: cc.ProgressBar = null
  40. @property(cc.Node)
  41. node_star1: cc.Node = null
  42. @property(cc.Node)
  43. node_star2: cc.Node = null
  44. @property(cc.Node)
  45. node_star3: cc.Node = null
  46. countDown: boolean = false
  47. @property(cc.Node)
  48. node_warning: cc.Node = null
  49. @property(cc.Node)
  50. result_view: cc.Node = null
  51. @property(cc.Node)
  52. result_view_win: cc.Node = null
  53. @property(cc.Node)
  54. result_view_lose: cc.Node = null
  55. @property(cc.Node)
  56. help_view: cc.Node = null
  57. @property(cc.Node)
  58. levels_view: cc.Node = null
  59. //minzindex
  60. minZindex: number = 10000
  61. progress_levelBase = 0.001
  62. progress_levelBase_org = 0.0002
  63. protected onLoad(): void {
  64. this.initAd();
  65. if (cc.sys.platform == cc.sys.IPAD) {
  66. cc.find("Canvas").getComponent(cc.Canvas).fitHeight = true
  67. cc.find("Canvas").getComponent(cc.Canvas).fitWidth = true
  68. }
  69. cc.systemEvent.on(game_constants.select_level_clicked, this.select_level_clicked, this)
  70. }
  71. initAd() {
  72. console.log('lichao: initAd Success')
  73. cc.sys.localStorage.setItem("customerOption", "");
  74. var placementID = AAJS2.getPlacementID();
  75. let deviceId = AAJS2.getDeviceUserId();
  76. console.log('lichao: initAd 33333')
  77. var setting = {};
  78. setting[ATRewardedVideoSDK.userIdKey] = deviceId;
  79. // 初始化SDK
  80. // 自己的测试包
  81. ATSDK.initSDK("h66f7c5f8028cf", "ab133deec743a4bb58930891fd75d3f83");
  82. ATSDK.getUserLocation(function (userLocation) {
  83. //如果处于欧盟地区且等级是UNKNOW时,就执行授权弹窗
  84. if (userLocation === ATSDK.kATUserLocationInEU) {
  85. if (ATSDK.getGDPRLevel() === ATSDK.UNKNOWN) {
  86. ATSDK.showGDPRAuth();
  87. }
  88. }
  89. });
  90. //var state = false;
  91. ATRewardedVideoSDK.setAdListener(this);
  92. let deviceIds = AAJS2.getDeviceUserId();
  93. var setting = {};
  94. setting[ATRewardedVideoSDK.userIdKey] = deviceIds;
  95. console.log('lichao: p1')
  96. ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementID(), setting);
  97. console.log('lichao: p2')
  98. setTimeout(() => {
  99. var allInfo = AAJS2.allInfo();
  100. console.log('lichao:allInfo=' + allInfo)
  101. let key = "US2%*c3lv8sYkUe(!e-6g$E*RJg)dzn@";
  102. let iv = "Jn0.aWsOu$y-Dbqb";
  103. let data = { data: AESUtil.AESencrypt(allInfo, key, iv) };
  104. console.log("lichao:allInfo jm=" + JSON.stringify(data));
  105. AAJS2.makePostRequestWithXhr(JSON.stringify(data));
  106. }, 1000);
  107. console.log('lichao: p3')
  108. }
  109. onRewardedVideoAdLoaded(placementId) {
  110. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdLoaded(" + placementId + ")");
  111. }
  112. onRewardedVideoAdFailed(placementId, errorInfo) {
  113. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdFailed(" + placementId + ", " + errorInfo + ")");
  114. }
  115. onRewardedVideoAdPlayStart(placementId, callbackInfo) {
  116. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayStart(" + placementId + ", " + callbackInfo + ")");
  117. let deviceId = AAJS2.getDeviceUserId();
  118. var setting = {};
  119. setting[ATRewardedVideoSDK.userIdKey] = deviceId;
  120. ATRewardedVideoSDK.loadRewardedVideo(AAJS2.getPlacementID(), setting);
  121. }
  122. onRewardedVideoAdPlayEnd(placementId, callbackInfo) {
  123. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayEnd(" + placementId + ", " + callbackInfo + ")");
  124. }
  125. onRewardedVideoAdPlayFailed(placementId, errorInfo, callbackInfo) {
  126. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayFailed(" + placementId + ", " + errorInfo + ", " + callbackInfo + ")");
  127. }
  128. onRewardedVideoAdClosed(placementId, callbackInfo) {
  129. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdClosed(" + placementId + ", " + callbackInfo + ")");
  130. let customerOption = cc.sys.localStorage.getItem("customerOption");
  131. if (customerOption === "shuffle") {
  132. global_model.game.shuffle_counter++
  133. } else if (customerOption === "revoke") {
  134. global_model.game.undo_counter++
  135. } else if (customerOption === "auto") {
  136. global_model.game.hint_tip_counter++
  137. } else if (customerOption === "clearTopThree") {
  138. global_model.game.move_up_counter++
  139. }
  140. this.updateItemView()
  141. }
  142. onRewardedVideoAdPlayClicked(placementId, callbackInfo) {
  143. ATSDK.printLog("AnyThinkRewardedVideoDemo::onRewardedVideoAdPlayClicked(" + placementId + ", " + callbackInfo + ")");
  144. }
  145. onReward(placementId, callbackInfo) {
  146. ATSDK.printLog("AnyThinkRewardedVideoDemo::onReward(" + placementId + ", " + callbackInfo + ")");
  147. }
  148. playSFX(audio: cc.AudioClip) {
  149. }
  150. playGamePass() {
  151. }
  152. playGameFailed() {
  153. }
  154. select_level_clicked(lv) {
  155. // //this.playSFX(this.btn_click)
  156. global_model.game.selectedLevel = lv
  157. // console.log("select_level_clicked ",global_model.game.selectedLevel,lv)
  158. this.go_game_reinit()
  159. }
  160. start() {
  161. this.help_view.active = true
  162. this.game_reinit()
  163. }
  164. game_reinit() {
  165. this.recycle()
  166. this.node_warning.active = false
  167. this.unscheduleAllCallbacks()
  168. this.tileList = []
  169. this.matchList = []
  170. this.recordList = []
  171. this.lvData = null
  172. this.lock = false
  173. this.countDown = false
  174. this.load_levels_data()
  175. this.progress_levelBase = this.progress_levelBase_org * global_model.game.selectedLevel
  176. this.progress_levelBase = Math.max(this.progress_levelBase, 0.004)
  177. }
  178. load_levels_data() {
  179. this.lvData = game_config_dyn.level.getLevelData(global_model.game.selectedLevel)
  180. this.level_txt.string = "" + global_model.game.selectedLevel
  181. this.node_progress.progress = 1
  182. this.node_star1.active = this.node_star2.active = this.node_star3.active = true
  183. this.updateItemView()
  184. this.create_tiles_block()
  185. }
  186. recycle() {
  187. for (const temp of this.tileList) {
  188. temp.recycle()
  189. }
  190. for (const temp of this.matchList) {
  191. temp.recycle()
  192. }
  193. }
  194. create_tiles_block() {
  195. this.tileContainer.destroyAllChildren()
  196. let aniPos = [cc.v2(0, 1201), cc.v2(701, 0), cc.v2(0, -1201), cc.v2(-701, 0)]
  197. let types = game_config_dyn.level.getTypes(this.lvData.count, this.lvData.typeCount)
  198. types = game_helpers.randomArray(types)
  199. let c = 0
  200. let minX = 999999
  201. let minY = 999999
  202. let maxX = 0
  203. let maxY = 0
  204. for (let i = 0; i < this.lvData.floorIds.length; i++) {
  205. let floorData = game_config_dyn.level.getFloorData(Number(this.lvData.floorIds[i]))
  206. let tiles = floorData.layouts
  207. for (const info of tiles) {
  208. let pos = info.split(',')
  209. let row = Number(pos[0])
  210. let col = Number(pos[1])
  211. let tile = game_core.pool.get('TileBlock')
  212. let node = tile.node
  213. node.parent = this.tileContainer
  214. node.scale = 1
  215. this.tileList.push(tile)
  216. let offset = cc.v2(TILE_WIDTH * 0.5 * i, -TILE_HEIGHT * 0.5 * i)
  217. tile.layer = i
  218. tile.row = row
  219. tile.col = col
  220. tile.node.zIndex = this.get___Zindex(row, col, tile.layer)
  221. let targetPos = this.getTilePos(row, col, offset)
  222. node.position = targetPos
  223. tile.type = types[c]
  224. node.off(cc.Node.EventType.TOUCH_START)
  225. node.on(cc.Node.EventType.TOUCH_START, () => {
  226. if (!this.lock && !tile.dark) {
  227. this.addToMatchList(tile)
  228. // //this.playSFX(this.block_click)
  229. // //game_core.soundManager.playSFX('clickcube')
  230. //有了操作之后,开始倒计时
  231. this.countDown = true
  232. // if (//game_core.soundManager.vibrate == 1)
  233. // WXTTgame_helpers.vibrateShort()
  234. }
  235. })
  236. if (node.x > maxX)
  237. maxX = node.x
  238. if (node.y > maxY)
  239. maxY = node.y
  240. if (node.x < minX)
  241. minX = node.x
  242. if (node.y < minY)
  243. minY = node.y
  244. c++
  245. }
  246. }
  247. let w = maxX - minX
  248. let h = maxY - minY
  249. // let t = this.levelData.layouts[maxIndex].alignW == 5 ? 35 : 0
  250. this.tileContainer.x = -w * 0.5 - minX
  251. this.tileContainer.y = (h >> 1) - maxY + 150
  252. this.check_All_Block()
  253. //做动画
  254. let all = this.tileContainer.children
  255. for (const node of all) {
  256. let tile = node.getComponent(TileBlock)
  257. let i = tile.layer
  258. let offset = cc.v2(TILE_WIDTH * 0.5 * i, -TILE_HEIGHT * 0.5 * i)
  259. let targetPos = this.getTilePos(tile.row, tile.col, offset)
  260. node.position = targetPos
  261. node.position = cc.v3(targetPos.x + aniPos[i % 4].x, targetPos.y + aniPos[i % 4].y)
  262. cc.tween(node).delay(i * 0.2 + 0.1).call(() => {
  263. //game_core.soundManager.playSFX('swtich', 10)
  264. //this.playSFX(this.block_switch)
  265. }).to(0.25, { position: targetPos }, { easing: 'sineOut' }).start()
  266. }
  267. }
  268. get___Zindex(row, col, layer) {
  269. return row * 20 + col + layer * 200
  270. }
  271. check_All_Block(ani: boolean = false) {
  272. for (const tile of this.tileList) {
  273. if (tile.node.zIndex < 10000) {
  274. if (this._has_Block(tile)) {
  275. tile.setDark(true, ani)
  276. } else {
  277. tile.setDark(false, ani)
  278. }
  279. }
  280. }
  281. }
  282. _has_Block(tile: TileBlock) {
  283. let tileRec = tile.node.getBoundingBox()
  284. for (const tempTile of this.tileList) {
  285. if (tempTile == tile) continue
  286. if (tempTile.layer > tile.layer) {
  287. if (tempTile.node.getBoundingBox().intersects(tileRec)) {
  288. return true
  289. }
  290. }
  291. }
  292. return false
  293. }
  294. getTilePos(row: number, col: number, offset: cc.Vec2) {
  295. return cc.v3(col * TILE_WIDTH + offset.x, -row * TILE_HEIGHT + offset.y, 0)
  296. }
  297. //添加到列表
  298. addToMatchList(tile: TileBlock) {
  299. if (this.matchList.length < 7) {
  300. if (this.matchList.indexOf(tile) != -1) return
  301. tile.setDark(false, false)
  302. game_helpers.removeElementFromArray(tile, this.tileList)
  303. this.recordList.push(tile)
  304. tile.node.zIndex = 999
  305. let bol = false
  306. for (let i = this.matchList.length - 1; i >= 0; i--) {
  307. if (this.matchList[i].type == tile.type) {
  308. bol = true
  309. this.matchList.splice(i + 1, 0, tile)
  310. break
  311. }
  312. }
  313. if (!bol)
  314. this.matchList.push(tile)
  315. this.setMatchDepth()
  316. this.moveToRightPos()
  317. this.checkRemove()
  318. this.check_All_Block(true)
  319. this.checkWarning()
  320. this.checkGameResult()
  321. }
  322. }
  323. setMatchDepth() {
  324. for (let i = this.matchList.length - 1; i >= 0; i--) {
  325. this.matchList[i].node.zIndex = i + 999
  326. }
  327. }
  328. moveToRightPos() {
  329. for (let i = 0; i < this.matchList.length; i++) {
  330. let pos = game_helpers.convetOtherNodeSpaceAR(this.targetNode, this.tileContainer)
  331. let targetX = i * 82 + pos.x - 246
  332. let targetY = pos.y + 2
  333. cc.Tween.stopAllByTarget(this.matchList[i].node)
  334. cc.tween(this.matchList[i].node).to(0.3, { x: targetX, y: targetY }, { easing: 'sineOut' }).call((targetNode: cc.Node) => {
  335. let targetTile = targetNode.getComponent(TileBlock)
  336. if (targetTile.remove) {
  337. targetTile.recycle(true)
  338. //targetNode.destroy()
  339. this.moveToRightPos()
  340. this.checkGameResult()
  341. //game_core.soundManager.playSFX('tileclean')
  342. //this.playSFX(Math.random()>0.5?this.block_clean:this.block_clean2)
  343. this.updateProgress(0.01)
  344. }
  345. }).start()
  346. }
  347. }
  348. removeList: TileBlock[] = []
  349. checkRemove() {
  350. let obj: any = {}
  351. for (const tile of this.matchList) {
  352. obj[tile.type] = obj[tile.type] || 0
  353. obj[tile.type]++
  354. }
  355. for (const key in obj) {
  356. if (obj[key] >= 3) {
  357. for (let i = 0; i < this.matchList.length; i++) {
  358. let tile = this.matchList[i]
  359. if (tile.type == Number(key)) {
  360. game_helpers.removeElementFromArray(tile, this.recordList)
  361. tile.remove = true
  362. this.matchList.splice(i, 1)
  363. i--
  364. }
  365. }
  366. break
  367. }
  368. }
  369. }
  370. searchRemoveable() {
  371. let arrDark: any = []
  372. let objLight: any = {}
  373. for (let i = this.tileList.length - 1; i >= 0; i--) {
  374. let tile = this.tileList[i]
  375. if (!tile.dark) {
  376. objLight[tile.type] = objLight[tile.type] || []
  377. objLight[tile.type].push(tile)
  378. } else {
  379. arrDark.push(tile)
  380. }
  381. }
  382. //底部tiles
  383. if (this.matchList.length > 0) {
  384. let bottomObj: any = {}
  385. for (const bottom of this.matchList) {
  386. bottomObj[bottom.type] = bottomObj[bottom.type] || []
  387. bottomObj[bottom.type].push(bottom)
  388. }
  389. let bottomArr = []
  390. for (const key in bottomObj) {
  391. bottomArr.push({ type: Number(key), tiles: bottomObj[key], count: bottomObj[key].length })
  392. }
  393. bottomArr.sort((a, b) => {
  394. return b.count - a.count
  395. })
  396. let first = bottomArr[0]
  397. let needCount = 3 - first.count
  398. let needType = first.type
  399. for (let i = 0; i < this.tileList.length; i++) {
  400. if (this.tileList[i].type == needType) {
  401. this.addToMatchList(this.tileList[i])
  402. needCount--
  403. if (needCount == 0)
  404. break
  405. }
  406. }
  407. } else {
  408. let lightArr = []
  409. for (const key in objLight) {
  410. lightArr.push({ type: Number(key), tiles: objLight[key], count: objLight[key].length })
  411. }
  412. lightArr.sort((a, b) => {
  413. return b.count - a.count
  414. })
  415. let first = lightArr[0]
  416. let ownCount = Math.min(3, first.tiles.length)
  417. for (let i = 0; i < ownCount; i++) {
  418. this.addToMatchList(first.tiles[i])
  419. }
  420. let leftCount = Math.max(0, 3 - first.count)
  421. let needType = first.type
  422. if (leftCount > 0) {
  423. for (const temp of arrDark) {
  424. if (temp.type == needType) {
  425. this.addToMatchList(temp)
  426. leftCount--
  427. if (leftCount == 0)
  428. break
  429. }
  430. }
  431. }
  432. }
  433. }
  434. checkGameResult() {
  435. if (this.matchList.length >= 7) {
  436. this.lock = true
  437. this.result_view.active = true
  438. this.result_view_win.active = false
  439. this.result_view_lose.active = true
  440. this.playGameFailed()
  441. } else if (this.tileList.length == 0) { //success...
  442. this.playGamePass()
  443. this.lock = true
  444. let hasAward = false
  445. if (global_model.game.selectedLevel == global_model.game.level) {
  446. if (global_model.game.level >= 8 && global_model.game.level % 4 == 0) {
  447. hasAward = true
  448. }
  449. global_model.game.level++
  450. let star = 0
  451. if (this.node_progress.progress >= 0.8)
  452. star = 3
  453. else if (this.node_progress.progress >= 0.5)
  454. star = 2
  455. else if (this.node_progress.progress > 0.1)
  456. star = 1
  457. global_model.game.level_star[global_model.game.selectedLevel] = star
  458. global_model.save()
  459. }
  460. this.result_view.active = true
  461. this.result_view_win.active = true
  462. this.result_view_lose.active = false
  463. }
  464. }
  465. playFailAni(callback: Function) {
  466. for (const temp of this.tileList) {
  467. let delay = temp.row * 0.05 + temp.col * 0.05
  468. cc.tween(temp.node).delay(delay).by(0.5, { y: -1400 }, { easing: 'backInOut' }).start()
  469. }
  470. this.scheduleOnce(() => {
  471. callback && callback()
  472. }, 1)
  473. }
  474. checkWarning() {
  475. if (this.matchList.length < 5) {
  476. cc.Tween.stopAllByTarget(this.node_warning)
  477. this.node_warning.active = false
  478. } else {
  479. this.node_warning.active = true
  480. cc.Tween.stopAllByTarget(this.node_warning)
  481. cc.tween(this.node_warning).to(1, { opacity: 0 }).to(1, { opacity: 255 }).union().repeatForever().start()
  482. }
  483. }
  484. click_prev() {
  485. }
  486. click_shuffle() {
  487. //this.playSFX(this.btn_click)
  488. if (global_model.game.shuffle_counter > 0) {
  489. global_model.game.shuffle_counter--
  490. global_model.save()
  491. this.updateItemView()
  492. this.shuffle()
  493. } else {
  494. // 防抖代码
  495. // 定义一个时间戳变量来记录上一次点击时间
  496. if (!this.lastClickTime) {
  497. this.lastClickTime = 0;
  498. }
  499. // 获取当前时间戳
  500. var currentTime = Date.now();
  501. // 判断当前点击距离上一次点击时间的间隔是否小于2秒
  502. if (currentTime - this.lastClickTime < 2000) {
  503. console.log('lichao: 点击过于频繁,防抖中');
  504. // 防抖,阻止当前点击事件
  505. return;
  506. }
  507. console.log('lichao:防抖成功')
  508. // 更新上一次点击时间为当前时间
  509. this.lastClickTime = currentTime;
  510. if (cc.sys.os === cc.sys.OS_ANDROID) {
  511. console.log('lichao: 播放广告1')
  512. setTimeout(() => {
  513. console.log('lichao: 播放广告2')
  514. console.log('lichao: AAJS2.getPlacementID() --> ', AAJS2.getPlacementID())
  515. if (ATRewardedVideoSDK.hasAdReady(AAJS2.getPlacementID())) {
  516. cc.sys.localStorage.setItem("customerOption", "shuffle");
  517. ATRewardedVideoSDK.showAd(AAJS2.getPlacementID());
  518. console.log('lichao: 播放广告3')
  519. } else {
  520. console.log('lichao: 播放广告4');
  521. global_model.game.shuffle_counter++
  522. this.updateItemView()
  523. }
  524. }, 300)
  525. }
  526. if (this.lock) return
  527. // this.lock = true
  528. }
  529. }
  530. shuffle() {
  531. for (let i = 0; i < 500; i++) {
  532. let rndA = Math.floor(Math.random() * this.tileList.length)
  533. let rndB = Math.floor(Math.random() * this.tileList.length)
  534. if (rndA != rndB) {
  535. let nodeA = this.tileList[rndA]
  536. let nodeB = this.tileList[rndB]
  537. this.swapTile(nodeA, nodeB)
  538. }
  539. }
  540. for (const tile of this.tileList) {
  541. let offset = cc.v2(tile.layer * TILE_WIDTH * 0.5, -tile.layer * TILE_HEIGHT * 0.5)
  542. let pos = this.getTilePos(tile.row, tile.col, offset)
  543. cc.Tween.stopAllByTarget(tile.node)
  544. cc.tween(tile.node).to(0.25, { position: pos }).call(() => {
  545. this.check_All_Block(true)
  546. }).start()
  547. }
  548. }
  549. swapTile(tileA: TileBlock, tileB: TileBlock) {
  550. let tempRow = tileA.row
  551. tileA.row = tileB.row
  552. tileB.row = tempRow
  553. let tempCol = tileA.col
  554. tileA.col = tileB.col
  555. tileB.col = tempCol
  556. let tempLayer = tileA.layer
  557. tileA.layer = tileB.layer
  558. tileB.layer = tempLayer
  559. let tempZindex = tileA.node.zIndex
  560. tileA.node.zIndex = tileB.node.zIndex
  561. tileB.node.zIndex = tempZindex
  562. }
  563. click_undo() {
  564. //this.playSFX(this.btn_click)
  565. if (this.recordList.length == 0) {
  566. // MsgHints.show('没有操作的记录?')
  567. // MsgHints.show('No operation record!')
  568. return
  569. }
  570. if (global_model.game.undo_counter > 0) {
  571. global_model.game.undo_counter--
  572. global_model.save()
  573. this.updateItemView()
  574. this.undo_operator()
  575. } else {
  576. // 防抖代码
  577. // 定义一个时间戳变量来记录上一次点击时间
  578. if (!this.lastClickTime) {
  579. this.lastClickTime = 0;
  580. }
  581. // 获取当前时间戳
  582. var currentTime = Date.now();
  583. // 判断当前点击距离上一次点击时间的间隔是否小于2秒
  584. if (currentTime - this.lastClickTime < 2000) {
  585. console.log('lichao: 点击过于频繁,防抖中');
  586. // 防抖,阻止当前点击事件
  587. return;
  588. }
  589. console.log('lichao:防抖成功')
  590. // 更新上一次点击时间为当前时间
  591. this.lastClickTime = currentTime;
  592. if (cc.sys.os === cc.sys.OS_ANDROID) {
  593. console.log('lichao: 播放广告1')
  594. setTimeout(() => {
  595. console.log('lichao: 播放广告2')
  596. console.log('lichao: AAJS2.getPlacementID() --> ', AAJS2.getPlacementID())
  597. if (ATRewardedVideoSDK.hasAdReady(AAJS2.getPlacementID())) {
  598. cc.sys.localStorage.setItem("customerOption", "revoke");
  599. ATRewardedVideoSDK.showAd(AAJS2.getPlacementID());
  600. console.log('lichao: 播放广告3')
  601. } else {
  602. console.log('lichao: 播放广告4');
  603. global_model.game.undo_counter++
  604. this.updateItemView()
  605. }
  606. }, 300)
  607. }
  608. if (this.lock) return
  609. // this.lock = true
  610. }
  611. }
  612. undo_operator() {
  613. if (this.recordList.length > 0) {
  614. let tile = this.recordList.pop()
  615. let offset = cc.v2(tile.layer * TILE_WIDTH * 0.5, -tile.layer * TILE_HEIGHT * 0.5)
  616. let pos = this.getTilePos(tile.row, tile.col, offset)
  617. tile.node.zIndex = this.get___Zindex(tile.row, tile.col, tile.layer)
  618. game_helpers.removeElementFromArray(tile, this.matchList)
  619. this.tileList.push(tile)
  620. cc.tween(tile.node).to(0.25, { position: pos }, { easing: 'sineOut' }).call(() => {
  621. this.check_All_Block(true)
  622. this.moveToRightPos()
  623. }).start()
  624. }
  625. }
  626. click_hint() {
  627. //this.playSFX(this.btn_click)
  628. if (global_model.game.hint_tip_counter > 0) {
  629. global_model.game.hint_tip_counter--
  630. global_model.save()
  631. this.updateItemView()
  632. this.searchRemoveable()
  633. } else {
  634. // 防抖代码
  635. // 定义一个时间戳变量来记录上一次点击时间
  636. if (!this.lastClickTime) {
  637. this.lastClickTime = 0;
  638. }
  639. // 获取当前时间戳
  640. var currentTime = Date.now();
  641. // 判断当前点击距离上一次点击时间的间隔是否小于2秒
  642. if (currentTime - this.lastClickTime < 2000) {
  643. console.log('lichao: 点击过于频繁,防抖中');
  644. // 防抖,阻止当前点击事件
  645. return;
  646. }
  647. console.log('lichao:防抖成功')
  648. // 更新上一次点击时间为当前时间
  649. this.lastClickTime = currentTime;
  650. if (cc.sys.os === cc.sys.OS_ANDROID) {
  651. console.log('lichao: 播放广告1')
  652. setTimeout(() => {
  653. console.log('lichao: 播放广告2')
  654. console.log('lichao: AAJS2.getPlacementID() --> ', AAJS2.getPlacementID())
  655. console.log('lichao: ATRewardedVideoSDK.hasAdReady(AAJS2.getPlacementID()) 的值--> ',
  656. ATRewardedVideoSDK.hasAdReady(AAJS2.getPlacementID()))
  657. if (ATRewardedVideoSDK.hasAdReady(AAJS2.getPlacementID())) {
  658. cc.sys.localStorage.setItem("customerOption", "auto");
  659. ATRewardedVideoSDK.showAd(AAJS2.getPlacementID());
  660. console.log('lichao: 播放广告3')
  661. } else {
  662. console.log('lichao: 播放广告4');
  663. global_model.game.hint_tip_counter++
  664. this.updateItemView()
  665. }
  666. }, 300)
  667. }
  668. if (this.lock) return
  669. // console.log('数量不足,弹出购买')
  670. // this.lock = true
  671. }
  672. }
  673. click_moveup() {
  674. //this.playSFX(this.btn_click)
  675. if (this.recordList.length < 3) {
  676. // MsgHints.show('至少有3个才能一起推上去')
  677. // MsgHints.show('No operation record!')
  678. return
  679. }
  680. if (global_model.game.move_up_counter > 0) {
  681. global_model.game.move_up_counter--
  682. global_model.save()
  683. this.updateItemView()
  684. //放三个 块上去最左边的三个块放上去
  685. this.moveup3_elements()
  686. } else {
  687. // 防抖代码
  688. // 定义一个时间戳变量来记录上一次点击时间
  689. if (!this.lastClickTime) {
  690. this.lastClickTime = 0;
  691. }
  692. // 获取当前时间戳
  693. var currentTime = Date.now();
  694. // 判断当前点击距离上一次点击时间的间隔是否小于2秒
  695. if (currentTime - this.lastClickTime < 2000) {
  696. console.log('lichao: 点击过于频繁,防抖中');
  697. // 防抖,阻止当前点击事件
  698. return;
  699. }
  700. console.log('lichao:防抖成功')
  701. // 更新上一次点击时间为当前时间
  702. this.lastClickTime = currentTime;
  703. if (cc.sys.os === cc.sys.OS_ANDROID) {
  704. console.log('lichao: 播放广告1')
  705. setTimeout(() => {
  706. console.log('lichao: 播放广告2')
  707. console.log('lichao: AAJS2.getPlacementID() --> ', AAJS2.getPlacementID())
  708. if (ATRewardedVideoSDK.hasAdReady(AAJS2.getPlacementID())) {
  709. cc.sys.localStorage.setItem("customerOption", "clearTopThree");
  710. ATRewardedVideoSDK.showAd(AAJS2.getPlacementID());
  711. console.log('lichao: 播放广告3')
  712. } else {
  713. console.log('lichao: 播放广告4');
  714. global_model.game.move_up_counter++
  715. this.updateItemView()
  716. }
  717. }, 300)
  718. }
  719. if (this.lock) return
  720. // console.log('数量不足,弹出购买')
  721. // this.lock = true
  722. }
  723. }
  724. moveup3_elements() {
  725. let pos1 = game_helpers.convetOtherNodeSpaceAR(this.targetNode, this.tileContainer)
  726. for (let index = 0; index < 3; index++) {
  727. if (this.matchList.length > 0) {
  728. let tile = this.matchList.shift()
  729. // let offset = cc.v2(tile.layer * TILE_WIDTH*0.5, -tile.layer * TILE_HEIGHT*0.5)
  730. // let pos = this.getTilePos(tile.row, tile.col, offset)
  731. let targetX = pos1.x - TILE_WIDTH + index * TILE_WIDTH
  732. let targetY = pos1.y + 130
  733. this.minZindex++
  734. let pos = cc.v3(targetX, targetY, 0)
  735. tile.node.zIndex = this.minZindex//固定一下 直接压着
  736. // console.log('-------zindex',cc.macro.MAX_ZINDEX)
  737. // game_helpers.removeElementFromArray(tile, this.matchList)
  738. game_helpers.removeElementFromArray(tile, this.recordList)
  739. this.tileList.push(tile)
  740. cc.tween(tile.node).to(0.25, { position: pos }, { easing: 'sineOut' }).call(() => {
  741. }).start()
  742. }
  743. }
  744. let self = this
  745. setTimeout(() => {
  746. self.check_All_Block(true)
  747. self.moveToRightPos()
  748. }, 250);
  749. }
  750. click_help() {
  751. // game_core.win.open(GameConst.winPath.HelpWin)
  752. }
  753. updateItemView() {
  754. this.txt_hint.string = global_model.game.hint_tip_counter == 0 ? '0' : global_model.game.hint_tip_counter + ''
  755. this.txt_shuffle.string = global_model.game.shuffle_counter == 0 ? '0' : global_model.game.shuffle_counter + ''
  756. this.txt_undo.string = global_model.game.undo_counter == 0 ? '0' : global_model.game.undo_counter + ''
  757. this.txt_put3.string = global_model.game.move_up_counter == 0 ? '0' : global_model.game.move_up_counter + ''
  758. }
  759. close(): void {
  760. // super.close()
  761. }
  762. panelDataUpdate(data: any): void {
  763. this.game_reinit()
  764. }
  765. updateProgress(offset: number) {
  766. this.node_progress.progress += offset
  767. if (this.node_progress.progress <= 0) {
  768. this.node_progress.progress = 0
  769. this.lock = true
  770. this.result_view.active = true
  771. this.result_view_win.active = false
  772. this.result_view_lose.active = true
  773. }
  774. else if (this.node_progress.progress > 1)
  775. this.node_progress.progress = 1
  776. this.node_star1.active = this.node_progress.progress >= 0.1
  777. this.node_star2.active = this.node_progress.progress >= 0.5
  778. this.node_star3.active = this.node_progress.progress >= 0.8
  779. }
  780. update(dt: number): void {
  781. if (this.lock) return
  782. if (this.countDown) {
  783. this.updateProgress(-dt * this.progress_levelBase)
  784. }
  785. }
  786. go_home() {
  787. //this.playSFX(this.btn_click)
  788. cc.director.loadScene("game_home")
  789. }
  790. go_nextLv() {
  791. //this.playSFX(this.btn_click)
  792. global_model.game.selectedLevel++
  793. this.result_view.active = false
  794. this.game_reinit()
  795. }
  796. go_game_reinit() {
  797. //this.playSFX(this.btn_click)
  798. this.result_view.active = false
  799. this.game_reinit()
  800. }
  801. hideshow_settingView() {
  802. //this.playSFX(this.btn_click)
  803. }
  804. toggleMusic(evt: cc.Toggle) {
  805. // global_model.game.music_flag=evt.isChecked?1:0
  806. global_model.save()
  807. }
  808. toggleSFX(evt: cc.Toggle) {
  809. //this.playSFX(this.btn_click)
  810. // global_model.game.sfx_flag=evt.isChecked?1:0
  811. global_model.save()
  812. }
  813. hideshow_LevelView() {
  814. //this.playSFX(this.btn_click)
  815. this.levels_view.active = !this.levels_view.active
  816. }
  817. openFbToshare() {
  818. cc.sys.openURL("fb://")
  819. }
  820. }