import { _decorator, Asset, assetManager, AssetManager, Component, error, EventTouch, instantiate, Label, log, Node, NodeEventType, Prefab, sys, UITransform, Vec3 } from 'cc'; import { BundleName } from 'db://assets/script/Config/EnumCfg'; import { GameCfg } from 'db://assets/script/Config/GameCfg'; import EventMgr from 'db://assets/script/Manager/EventMgr'; import { LayerMgr } from 'db://assets/script/Manager/LayerMgr'; import PlatformService from 'db://assets/script/Platform/PlatformService'; import { page_result } from './page_result'; import { AudioManager } from 'db://assets/script/Manager/AudioMgr'; import { GDM } from '../dtta/JsonMgr'; import { User } from 'db://assets/script/Manager/LocalDataMgr'; import { gamecfg } from '../dtta/interfaceMgr'; import { mainscene } from 'db://assets/script/mainscene'; import { Data } from '../dtta/Data'; const { ccclass, property } = _decorator; @ccclass('page_game') export class page_game extends Component { @property(Node) node_bg: Node = null; @property(Node) node_loading: Node = null; @property(Label) txt_countdown: Label = null; @property(Label) txt_title2: Label = null; @property(Node) node_yaobazi: Node = null; num_countdown: number = 120; data: gamecfg = null; protected start(): void { if (User.userData.leveltime != 0) { this.num_countdown = User.userData.leveltime } EventMgr.ins.addEventListener("gameStart", this.gameStart, this) EventMgr.ins.addEventListener("goto_nextlevel", this.gotoNextLevel, this) EventMgr.ins.addEventListener("restart_game", this.restartgame, this) EventMgr.ins.addEventListener("game_win", () => { this.showResult(true) }, this) EventMgr.ins.addEventListener("back_chooselevel", () => { this.node.destroy() }, this) EventMgr.ins.addEventListener("add_time", () => { this.num_countdown += 60 this.schedule(this.Countdown, 1) }, this) if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) { mainscene.instance.ge.track( "levelenter", //追踪事件的名称 { version: "123", level_id: this.data.unit } //需要上传的事件属性 ); } if (sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) { this.num_countdown = 99999 this.txt_countdown.node.active = false this.node_yaobazi.active = true this.txt_title2.node.active = true let tmp = this.getPageAndNum() this.txt_title2.string = `开始游戏 第 ${tmp.page} 页 ????`// ${this.data.title}` } this.node_yaobazi.on(Node.EventType.TOUCH_MOVE, this.bazi_touchmove, this) this.txt_title2.node.on(Node.EventType.TOUCH_START, this.title_touchstart, this) this.txt_title2.node.on(Node.EventType.TOUCH_MOVE, this.title_touchmove, this) this.txt_title2.node.on(Node.EventType.TOUCH_END, this.title_touchend, this) this.txt_title2.node.on(Node.EventType.TOUCH_CANCEL, this.title_touchend, this) } OnClickTitle2() { let tmp = this.getPageAndNum() this.txt_title2.string = `开始游戏 第 ${tmp.page} 页 ${this.data.title}` } getPageAndNum() { let index = this.data.id let page = Math.floor(index / 6) + 1 let num = index % 6 return { page: page, num: num } } gameStart() { this.schedule(this.Countdown, 1) } Init(data: gamecfg) { this.data = data; assetManager.loadBundle(data.bundle, (err: Error, bundle: AssetManager.Bundle) => { if (err) { error(err); } else { bundle.load("prefab/game", (err: Error, asset: Asset) => { if (err) { error(err) return } let node = instantiate(asset as Prefab); node.parent = this.node_bg; node.setSiblingIndex(1) this.gameStart() this.node_loading.active = false }) } }) // PlatformService.getInstance().platformApi.reportAnalytics("enterlevel", { // level: data.title // }) } OnClickTishi() {//点击提示 AudioManager.instance.playBundleAudio("button") if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) { mainscene.instance.ge.track( "ad_show", //追踪事件的名称 { version: "123", ad_position: "提示", level_id: this.data.unit } //需要上传的事件属性 ); } PlatformService.getInstance().platformApi.loadAndShowVideoAd(() => { // PlatformService.getInstance().platformApi.reportAnalytics("tips", { // level: this.data.title // }) if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) { mainscene.instance.ge.track( "ad_showend", //追踪事件的名称 { version: "123", ad_position: "提示", level_id: this.data.unit } //需要上传的事件属性 ); } EventMgr.ins.dispatchEvent("game_tishi") }) } OnClickExit() { AudioManager.instance.playBundleAudio("button") this.node.destroy() } OnClickShare() { AudioManager.instance.playBundleAudio("button") PlatformService.getInstance().platformApi.recordedGameScreenStop() PlatformService.getInstance().platformApi.shareRecordedGameScreen() } OnClickAddTime() { if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) { mainscene.instance.ge.track( "ad_show", //追踪事件的名称 { version: "123", ad_position: "加时", level_id: this.data.unit } //需要上传的事件属性 ); } AudioManager.instance.playBundleAudio("button") PlatformService.getInstance().platformApi.loadAndShowVideoAd(() => { if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) { mainscene.instance.ge.track( "ad_showend", //追踪事件的名称 { version: "123", ad_position: "加时", level_id: this.data.unit } //需要上传的事件属性 ); } // PlatformService.getInstance().platformApi.reportAnalytics("addtime", { // level: this.data.title // }) this.num_countdown += 60 }) } Countdown() { this.num_countdown--; if (this.num_countdown == 13) { this.schedule(() => { AudioManager.instance.playBundleAudio("daojishi") }, 3, 3) } if (this.num_countdown <= 0) { this.num_countdown = 0 this.unscheduleAllCallbacks() this.showResult(false) } this.txt_countdown.string = this.formatCountdown(this.num_countdown) } formatCountdown(seconds: number): string { const minutes = Math.floor(seconds / 60); const secs = seconds % 60; const secsStr = secs < 10 ? '0' + secs : secs.toString(); return `${minutes}:${secsStr}`; } showResult(isWin: boolean) { log(isWin ? "赢" : "输") this.unscheduleAllCallbacks() LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/page_result", (node) => { node.getComponent(page_result).Init(isWin, this.data) }) if (isWin) { if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) { mainscene.instance.ge.track( "levelfinish", //追踪事件的名称 { version: "123", level_id: this.data.unit, time_remaining: 120 - this.num_countdown } //需要上传的事件属性 ); } let data = GDM.gamecfgMgr.paixuLevelinfo.length > 0 ? GDM.gamecfgMgr.paixuLevelinfo[GameCfg.CurUnit + 1] : GDM.gamecfgMgr.data[GameCfg.CurUnit + 1] if (data) { User.userData.unlockUnits.push(data.unit) User.saveUserData() EventMgr.ins.dispatchEvent("unlock_unit", data.unit) } } // PlatformService.getInstance().platformApi.reportAnalytics("finishlevel", { // level: this.data.title, // win: isWin ? "0" : "1" // }) } gotoNextLevel() { let data = GDM.gamecfgMgr.paixuLevelinfo.length > 0 ? GDM.gamecfgMgr.paixuLevelinfo : GDM.gamecfgMgr.data let info = data[GameCfg.CurUnit + 1] GameCfg.CurUnit += 1 if (info == undefined) { info = data[0] GameCfg.CurUnit = 0 } console.log("gotoNextLevel", info) LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/page_game", (node) => { node.getComponent(page_game).Init(info) }) this.node.destroy() } restartgame() { let data = GDM.gamecfgMgr.paixuLevelinfo.length > 0 ? GDM.gamecfgMgr.paixuLevelinfo : GDM.gamecfgMgr.data let info = data[GameCfg.CurUnit] LayerMgr.instance.ShowPrefab(BundleName.hall, "prefab/page_game", (node) => { node.getComponent(page_game).Init(info) }) this.node.destroy() } protected onDestroy(): void { PlatformService.getInstance().platformApi.recordedGameScreenStop() this.unscheduleAllCallbacks() console.log("page_game destroy") } bazi_touchstart() { } bazi_touchmove(event: EventTouch) { // let worldpos = event.getLocation() // let localpos = this.node_yaobazi.parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(worldpos.x, worldpos.y, 0)) // this.node_yaobazi.setPosition(localpos) // // 获取触摸点的世界坐标 // const touchLocation = event.getLocation(); // // 将世界坐标转换为当前节点父节点的本地坐标系 // const localPosition = this.node_yaobazi.parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(touchLocation.x, touchLocation.y + 320, 0)); // // 设置节点位置 // this.node_yaobazi.setPosition(localPosition); const change = event.getDelta(); this.node_yaobazi.setPosition(this.node_yaobazi.position.x + change.x, this.node_yaobazi.position.y + change.y) } bazi_touchend() { } title_touchmove(event: EventTouch) { // let worldpos = event.getLocation() // let localpos = this.node_yaobazi.parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(worldpos.x, worldpos.y, 0)) // this.node_yaobazi.setPosition(localpos) // // 获取触摸点的世界坐标 // const touchLocation = event.getLocation(); // // 将世界坐标转换为当前节点父节点的本地坐标系 // const localPosition = this.node_yaobazi.parent.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(touchLocation.x, touchLocation.y + 320, 0)); // // 设置节点位置 // this.node_yaobazi.setPosition(localPosition); const change = event.getDelta(); this.txt_title2.node.setPosition(this.txt_title2.node.position.x + change.x, this.txt_title2.node.position.y + change.y) } touchtime = 0 title_touchend(TOUCH_MOVE: NodeEventType, title_touchend: any, arg2: this) { let tmptime = Date.now() if (tmptime - this.touchtime < 100) { this.OnClickTitle2() } } title_touchstart(TOUCH_MOVE: NodeEventType, title_touchstart: any, arg2: this) { this.touchtime = Date.now() } }