cc.macro.ENABLE_TRANSPARENT_CANVAS = true; import common, { AttributeGame, AttributeUnit } from "../src/common/common"; import CConst from "../src/common/CConst"; import NotifierCenter from "../src/webtcp/NotifierCenter"; import Loader from "../src/config/Loader"; import GameStart, { PropsStart } from "../res/luojigouStart/src/LuojigouStart"; import GameFinish, { PropsFinish } from "../res/luojigouFinish/src/LuojigouFinish"; import AudioCB from "../src/audioUitls/AudioCB"; import AudioManager from "../src/audioUitls/AudioManager"; const { ccclass, property } = cc._decorator; @ccclass export default class Scene extends cc.Component { @property({ tooltip: '游戏配置', type: cc.JsonAsset }) jsonConfig: cc.JsonAsset = null; @property({ tooltip: '视频页预制体', type: cc.Prefab }) prefabVideo: cc.Prefab = null; @property({ tooltip: '游戏开始预制体', type: cc.Prefab }) prefabStart: cc.Prefab = null; @property({ tooltip: '游戏结束预制体', type: cc.Prefab }) prefabFinish: cc.Prefab = null; @property({ tooltip: '地图预制体', type: cc.Prefab }) prefabPageMap: cc.Prefab = null; @property({ tooltip: '提示预制体', type: cc.Prefab }) prefabTip: cc.Prefab = null; @property({ tooltip: '下一个游戏', type: cc.Node }) btnNext: cc.Node = null; nodeShow: cc.Node = null; // 显示节点 nodeVideo: cc.Node = null; nodeStart: cc.Node = null; nodeFinish: cc.Node = null; nodePageMap: cc.Node = null; // 缓存数据 objData: { bundles: cc.AssetManager.Bundle | any, prefabs: cc.Prefab | any, } = { bundles: {}, prefabs: {} }; protected onLoad(): void { // 设置游戏窗口变化的回调(仅 Web 平台有效) cc.view.setResizeCallback(() => this.onResize()); } /** 注册监听事件 */ protected onEnable(): void { this.adapt();// 第一次显示的屏幕适配 NotifierCenter.listen(CConst.EVENT_LJG_START, this.showLgjStart, this, false); NotifierCenter.listen(CConst.EVENT_LJG_FINISH, this.showLgjFinish, this, false); NotifierCenter.listen(CConst.EVENT_ENTER_UNIT, this.msgResultEnterUnit, this, false); NotifierCenter.listen(CConst.EVENT_ENTER_GAME, this.msgResultEnterGame, this, false); NotifierCenter.listen(CConst.EVENT_SHOW_TIP, this.msgResultShowTip, this, false); } /** 取消监听事件 */ protected onDisable(): void { NotifierCenter.ignoreScope(this); } /** 游戏开始 */ async start() { common.hideLoading(); this.initUI(); await this.initData(); common.log("场景加载完成,等待进入游戏层"); if (common.isDebug) { this.nodePageMap.active = false; this.addPrefab(common.getUnitNum(), common.getPageNum()); } } /** 初始化游戏数据 */ async initData() { await AudioCB.getInstance().init(); await AudioManager.getInstance().loadAudios(); // 保存地图配置 let unitAll = this.jsonConfig.json; for (const key in unitAll) { if (Object.prototype.hasOwnProperty.call(unitAll, key)) { let _attribute: AttributeUnit = new AttributeUnit(); const unitOne = unitAll[key]; for (const key in unitOne) { if (Object.prototype.hasOwnProperty.call(unitOne, key)) { _attribute[key] = unitOne[key]; } } _attribute.bundle = await Loader.loadBundle(_attribute.bundleName); _attribute.config = []; let gameAll = await Loader.loadBundleJson(_attribute.bundle, _attribute.configName); gameAll.json.forEach((gameOne, index) => { let attributeGame: AttributeGame = new AttributeGame(); for (const key in gameOne) { if (Object.prototype.hasOwnProperty.call(gameOne, key)) { attributeGame[key] = gameOne[key]; } } _attribute.config[index] = attributeGame; }); common.attributeMap[key] = _attribute; } } } initUI() { // 初始隐藏的节点 this.nodeVideo = cc.instantiate(this.prefabVideo); this.nodeStart = cc.instantiate(this.prefabStart); this.nodeFinish = cc.instantiate(this.prefabFinish); this.nodeVideo.active = false; this.nodeStart.active = false; this.nodeFinish.active = false; this.node.addChild(this.nodeVideo, CConst.ZORDER_VIDEO); this.node.addChild(this.nodeStart, CConst.ZORDER_LJG_START); this.node.addChild(this.nodeFinish, CConst.ZORDER_LJG_FINISH); this.nodePageMap = cc.instantiate(this.prefabPageMap); this.node.addChild(this.nodePageMap, CConst.ZORDER_PAGE_MAP); this.nodeShow = null; this.btnNext.zIndex = CConst.ZORDER_BUTTON_NEXT; } /** 添加预制体 分别处理游戏跟视频*/ async addPrefab(unitNum: number, pageNum: number) { this.resetBtnNext(false); common.setPageNum(pageNum); let attribute: AttributeUnit = common.attributeMap[unitNum]; let configOne = attribute.config[pageNum - 1] // 游戏 if (configOne.prefabName) { this.addPrefabGame(attribute.bundle, configOne); } // 视频 else { this.addPrefabVideo(attribute.bundle, configOne); } } /** 加载游戏节点 */ async addPrefabGame(bundle, config) { // 加载新的地图页 let prefab = await Loader.loadBundlePrefab(bundle, config.prefabName); this.removePrefab();// 去掉游戏页 this.nodeVideo.active = false;// 隐藏视频页 this.nodePageMap.active = false; // 隐藏地图页 this.nodeShow = cc.instantiate(prefab); this.node.addChild(this.nodeShow, CConst.ZORDER_GAME); this.node.getChildByName('bg').active = true; this.resetBtnNext(true); } /** 加载视频 先不去除游戏节点 */ async addPrefabVideo(bundle, config) { this.removePrefab();// 去掉游戏页 this.nodePageMap.active = false; // 隐藏地图页 // 加载视频页 let script = this.getScriptByNode(this.nodeVideo, config.scriptName); if (script && script.initBundle) { script.initBundle(bundle, config); } this.nodeVideo.active = true; this.node.getChildByName('bg').active = false; this.resetBtnNext(true); } resetBtnNext(show) { if (common.isDebug) { this.btnNext.active = true; } else{ this.btnNext.active = false;// 显示下一步按钮 if (!show) return; let gameOne = common.getGameFormLocal(); this.btnNext.active = Number(gameOne) >= 0; } } /** * 删除预制体 * 游戏相关的逻辑狗卡片 倒计时都隐藏 */ removePrefab() { if (this.nodeShow) { this.nodeShow.removeFromParent(true); this.nodeShow.destroy(); this.nodeShow = null; } this.nodeStart.active = false; this.nodeFinish.active = false; } /** 事件回调:显示逻辑狗卡片 开始 */ showLgjStart(props: PropsStart): void { let script = this.nodeStart.getComponent(GameStart); script.setLuojigouCard(props); } /** 事件回调:显示逻辑狗卡片 结束 */ showLgjFinish(props: PropsFinish) { let scriptFinish = this.nodeFinish.getComponent(GameFinish); scriptFinish.setAnimation(props); } /** * 返回到地图页 */ enterPageMap() { this.resetBtnNext(false); this.removePrefab();// 隐藏游戏相关 this.nodeVideo.active = false; // 隐藏视频页 this.nodePageMap.active = true; // 显示地图页 }; /** 事件回调:进入某个单元 */ msgResultEnterUnit(unitNum: number) { common.setUnitNum(unitNum); this.addPrefab(unitNum, 1); } /** 事件回调:进入下一个游戏 */ msgResultEnterGame() { let curUint = common.getUnitNum(); let curPage = common.getPageNum(); let attribute: AttributeUnit = common.attributeMap[curUint]; let length = attribute.config.length; if (curPage > length - 1) { this.enterPageMap(); return; } this.addPrefab(curUint, curPage + 1); } /** 事件回调:显示提示 */ msgResultShowTip() { let nodeTip = cc.instantiate(this.prefabTip); this.node.addChild(nodeTip, CConst.ZORDER_TIP); } /** * 根据 组件名 获取 脚本组件 * @param scriptName * @returns */ getScriptByNode(node: cc.Node, scriptName: string): any { if (!node || !scriptName) return null; let script = node.getComponent(scriptName); if (!script) { script = this.nodeShow['_components'][0]; } return script; }; /** * 窗口变化回调 */ protected onResize() { // 适配 this.adapt(); } /** * 适配 */ protected adapt() { // 实际屏幕比例 const winSize = cc.winSize, screenRatio = winSize.width / winSize.height; // 设计比例 const designResolution = cc.Canvas.instance.designResolution, designRatio = designResolution.width / designResolution.height; // 对比判断 common.log('win ratio: ', screenRatio, '; w: ', winSize.width, '; h: ', winSize.height); common.log('des ratio: ', designRatio, '; w: ', designResolution.width, '; h: ', designResolution.height); if (screenRatio < designRatio) { common.log("适配宽 屏幕宽高比 小于 设计比例"); this.setFitWidth(); } else { common.log("适配高 屏幕宽高比 大于等于 设计比例"); this.setFitHeight(); } } /** * 适配高度模式 */ protected setFitHeight() { const canvas = cc.Canvas.instance; canvas.fitHeight = true; canvas.fitWidth = false; } /** * 适配宽度模式 */ protected setFitWidth() { const canvas = cc.Canvas.instance; canvas.fitHeight = false; canvas.fitWidth = true; } /** * 下载游戏zip * @param zipName */ downLoadZip(zipName) { window['luojigou']['downloadAssets'](zipName); } /** * 接收下载进度 * @param info */ reseveInfoForDownLoad(info) { }; }