game_main.ts 33 KB

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