page_game.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import { _decorator, Asset, assetManager, AssetManager, Component, error, EventTouch, instantiate, Label, log, Node, NodeEventType, Prefab, sys, UITransform, Vec3 } from 'cc';
  2. import { BundleName } from 'db://assets/script/Config/EnumCfg';
  3. import { GameCfg } from 'db://assets/script/Config/GameCfg';
  4. import EventMgr from 'db://assets/script/Manager/EventMgr';
  5. import { LayerMgr } from 'db://assets/script/Manager/LayerMgr';
  6. import PlatformService from 'db://assets/script/Platform/PlatformService';
  7. import { page_result } from './page_result';
  8. import { AudioManager } from 'db://assets/script/Manager/AudioMgr';
  9. import { GDM } from '../dtta/JsonMgr';
  10. import { User } from 'db://assets/script/Manager/LocalDataMgr';
  11. import { gamecfg } from '../dtta/interfaceMgr';
  12. import { mainscene } from 'db://assets/script/mainscene';
  13. import { Data } from '../dtta/Data';
  14. const { ccclass, property } = _decorator;
  15. @ccclass('page_game')
  16. export class page_game extends Component {
  17. @property(Node)
  18. node_bg: Node = null;
  19. @property(Node)
  20. node_loading: Node = null;
  21. @property(Label)
  22. txt_countdown: Label = null;
  23. @property(Label)
  24. txt_title2: Label = null;
  25. @property(Node)
  26. node_yaobazi: Node = null;
  27. num_countdown: number = 120;
  28. data: gamecfg = null;
  29. protected start(): void {
  30. if (User.userData.leveltime != 0) {
  31. this.num_countdown = User.userData.leveltime
  32. }
  33. EventMgr.ins.addEventListener("gameStart", this.gameStart, this)
  34. EventMgr.ins.addEventListener("goto_nextlevel", this.gotoNextLevel, this)
  35. EventMgr.ins.addEventListener("restart_game", this.restartgame, this)
  36. EventMgr.ins.addEventListener("game_win", () => {
  37. this.showResult(true)
  38. }, this)
  39. EventMgr.ins.addEventListener("back_chooselevel", () => {
  40. this.node.destroy()
  41. }, this)
  42. EventMgr.ins.addEventListener("add_time", () => {
  43. this.num_countdown += 60
  44. this.schedule(this.Countdown, 1)
  45. }, this)
  46. if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
  47. mainscene.instance.ge.track(
  48. "levelenter", //追踪事件的名称
  49. {
  50. version: "123",
  51. level_id: this.data.unit
  52. } //需要上传的事件属性
  53. );
  54. }
  55. if (sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
  56. this.num_countdown = 99999
  57. this.txt_countdown.node.active = false
  58. this.node_yaobazi.active = true
  59. this.txt_title2.node.active = true
  60. let tmp = this.getPageAndNum()
  61. this.txt_title2.string = `开始游戏 第 ${tmp.page} 页 ????`// ${this.data.title}`
  62. }
  63. this.node_yaobazi.on(Node.EventType.TOUCH_MOVE, this.bazi_touchmove, this)
  64. this.txt_title2.node.on(Node.EventType.TOUCH_START, this.title_touchstart, this)
  65. this.txt_title2.node.on(Node.EventType.TOUCH_MOVE, this.title_touchmove, this)
  66. this.txt_title2.node.on(Node.EventType.TOUCH_END, this.title_touchend, this)
  67. this.txt_title2.node.on(Node.EventType.TOUCH_CANCEL, this.title_touchend, this)
  68. }
  69. OnClickTitle2() {
  70. let tmp = this.getPageAndNum()
  71. this.txt_title2.string = `开始游戏 第 ${tmp.page} 页 ${this.data.title}`
  72. }
  73. getPageAndNum() {
  74. let index = this.data.id
  75. let page = Math.floor(index / 6) + 1
  76. let num = index % 6
  77. return { page: page, num: num }
  78. }
  79. gameStart() {
  80. this.schedule(this.Countdown, 1)
  81. }
  82. Init(data: gamecfg) {
  83. this.data = data;
  84. assetManager.loadBundle(data.bundle, (err: Error, bundle: AssetManager.Bundle) => {
  85. if (err) {
  86. error(err);
  87. } else {
  88. bundle.load("prefab/game", (err: Error, asset: Asset) => {
  89. if (err) {
  90. error(err)
  91. return
  92. }
  93. let node = instantiate(asset as Prefab);
  94. node.parent = this.node_bg;
  95. node.setSiblingIndex(1)
  96. this.gameStart()
  97. this.node_loading.active = false
  98. })
  99. }
  100. })
  101. // PlatformService.getInstance().platformApi.reportAnalytics("enterlevel", {
  102. // level: data.title
  103. // })
  104. }
  105. OnClickTishi() {//点击提示
  106. AudioManager.instance.playBundleAudio("button")
  107. if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
  108. mainscene.instance.ge.track(
  109. "ad_show", //追踪事件的名称
  110. {
  111. version: "123",
  112. ad_position: "提示",
  113. level_id: this.data.unit
  114. } //需要上传的事件属性
  115. );
  116. }
  117. PlatformService.getInstance().platformApi.loadAndShowVideoAd(() => {
  118. // PlatformService.getInstance().platformApi.reportAnalytics("tips", {
  119. // level: this.data.title
  120. // })
  121. if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
  122. mainscene.instance.ge.track(
  123. "ad_showend", //追踪事件的名称
  124. {
  125. version: "123",
  126. ad_position: "提示",
  127. level_id: this.data.unit
  128. } //需要上传的事件属性
  129. );
  130. }
  131. EventMgr.ins.dispatchEvent("game_tishi")
  132. })
  133. }
  134. OnClickExit() {
  135. AudioManager.instance.playBundleAudio("button")
  136. this.node.destroy()
  137. }
  138. OnClickShare() {
  139. AudioManager.instance.playBundleAudio("button")
  140. PlatformService.getInstance().platformApi.recordedGameScreenStop()
  141. PlatformService.getInstance().platformApi.shareRecordedGameScreen()
  142. }
  143. OnClickAddTime() {
  144. if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
  145. mainscene.instance.ge.track(
  146. "ad_show", //追踪事件的名称
  147. {
  148. version: "123",
  149. ad_position: "加时",
  150. level_id: this.data.unit
  151. } //需要上传的事件属性
  152. );
  153. }
  154. AudioManager.instance.playBundleAudio("button")
  155. PlatformService.getInstance().platformApi.loadAndShowVideoAd(() => {
  156. if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
  157. mainscene.instance.ge.track(
  158. "ad_showend", //追踪事件的名称
  159. {
  160. version: "123",
  161. ad_position: "加时",
  162. level_id: this.data.unit
  163. } //需要上传的事件属性
  164. );
  165. }
  166. // PlatformService.getInstance().platformApi.reportAnalytics("addtime", {
  167. // level: this.data.title
  168. // })
  169. this.num_countdown += 60
  170. })
  171. }
  172. Countdown() {
  173. this.num_countdown--;
  174. if (this.num_countdown == 13) {
  175. this.schedule(() => {
  176. AudioManager.instance.playBundleAudio("daojishi")
  177. }, 3, 3)
  178. }
  179. if (this.num_countdown <= 0) {
  180. this.num_countdown = 0
  181. this.unscheduleAllCallbacks()
  182. this.showResult(false)
  183. }
  184. this.txt_countdown.string = this.formatCountdown(this.num_countdown)
  185. }
  186. formatCountdown(seconds: number): string {
  187. const minutes = Math.floor(seconds / 60);
  188. const secs = seconds % 60;
  189. const secsStr = secs < 10 ? '0' + secs : secs.toString();
  190. return `${minutes}:${secsStr}`;
  191. }
  192. showResult(isWin: boolean) {
  193. log(isWin ? "赢" : "输")
  194. this.unscheduleAllCallbacks()
  195. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/page_result", (node) => {
  196. node.getComponent(page_result).Init(isWin, this.data)
  197. })
  198. if (isWin) {
  199. if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
  200. mainscene.instance.ge.track(
  201. "levelfinish", //追踪事件的名称
  202. {
  203. version: "123",
  204. level_id: this.data.unit,
  205. time_remaining: 120 - this.num_countdown
  206. } //需要上传的事件属性
  207. );
  208. }
  209. let data = GDM.gamecfgMgr.paixuLevelinfo.length > 0 ? GDM.gamecfgMgr.paixuLevelinfo[GameCfg.CurUnit + 1] : GDM.gamecfgMgr.data[GameCfg.CurUnit + 1]
  210. if (data) {
  211. User.userData.unlockUnits.push(data.unit)
  212. User.saveUserData()
  213. EventMgr.ins.dispatchEvent("unlock_unit", data.unit)
  214. }
  215. }
  216. // PlatformService.getInstance().platformApi.reportAnalytics("finishlevel", {
  217. // level: this.data.title,
  218. // win: isWin ? "0" : "1"
  219. // })
  220. }
  221. gotoNextLevel() {
  222. let data = GDM.gamecfgMgr.paixuLevelinfo.length > 0 ? GDM.gamecfgMgr.paixuLevelinfo : GDM.gamecfgMgr.data
  223. let info = data[GameCfg.CurUnit + 1]
  224. GameCfg.CurUnit += 1
  225. if (info == undefined) {
  226. info = data[0]
  227. GameCfg.CurUnit = 0
  228. }
  229. console.log("gotoNextLevel", info)
  230. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/page_game", (node) => {
  231. node.getComponent(page_game).Init(info)
  232. })
  233. this.node.destroy()
  234. }
  235. restartgame() {
  236. let data = GDM.gamecfgMgr.paixuLevelinfo.length > 0 ? GDM.gamecfgMgr.paixuLevelinfo : GDM.gamecfgMgr.data
  237. let info = data[GameCfg.CurUnit]
  238. LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/page_game", (node) => {
  239. node.getComponent(page_game).Init(info)
  240. })
  241. this.node.destroy()
  242. }
  243. protected onDestroy(): void {
  244. PlatformService.getInstance().platformApi.recordedGameScreenStop()
  245. this.unscheduleAllCallbacks()
  246. console.log("page_game destroy")
  247. }
  248. bazi_touchstart() {
  249. }
  250. bazi_touchmove(event: EventTouch) {
  251. // let worldpos = event.getLocation()
  252. // let localpos = this.node_yaobazi.parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(worldpos.x, worldpos.y, 0))
  253. // this.node_yaobazi.setPosition(localpos)
  254. // // 获取触摸点的世界坐标
  255. // const touchLocation = event.getLocation();
  256. // // 将世界坐标转换为当前节点父节点的本地坐标系
  257. // const localPosition = this.node_yaobazi.parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(touchLocation.x, touchLocation.y + 320, 0));
  258. // // 设置节点位置
  259. // this.node_yaobazi.setPosition(localPosition);
  260. const change = event.getDelta();
  261. this.node_yaobazi.setPosition(this.node_yaobazi.position.x + change.x, this.node_yaobazi.position.y + change.y)
  262. }
  263. bazi_touchend() {
  264. }
  265. title_touchmove(event: EventTouch) {
  266. // let worldpos = event.getLocation()
  267. // let localpos = this.node_yaobazi.parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(worldpos.x, worldpos.y, 0))
  268. // this.node_yaobazi.setPosition(localpos)
  269. // // 获取触摸点的世界坐标
  270. // const touchLocation = event.getLocation();
  271. // // 将世界坐标转换为当前节点父节点的本地坐标系
  272. // const localPosition = this.node_yaobazi.parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(touchLocation.x, touchLocation.y + 320, 0));
  273. // // 设置节点位置
  274. // this.node_yaobazi.setPosition(localPosition);
  275. const change = event.getDelta();
  276. this.txt_title2.node.setPosition(this.txt_title2.node.position.x + change.x, this.txt_title2.node.position.y + change.y)
  277. }
  278. touchtime = 0
  279. title_touchend(TOUCH_MOVE: NodeEventType, title_touchend: any, arg2: this) {
  280. let tmptime = Date.now()
  281. if (tmptime - this.touchtime < 100) {
  282. this.OnClickTitle2()
  283. }
  284. }
  285. title_touchstart(TOUCH_MOVE: NodeEventType, title_touchstart: any, arg2: this) {
  286. this.touchtime = Date.now()
  287. }
  288. }