123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- cc.macro.ENABLE_TRANSPARENT_CANVAS = true;
- import common, { AttributeGame, AttributeUnit, UitlData } 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 Tools from "../src/common/Tools";
- import AudioManager from "../src/audioUitls/AudioManager";
- import PopupManager, { PopupCacheMode } from "../src/popUp/PopupManager";
- 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 })
- 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;
- // 缓存数据
- objData: {
- bundles: cc.AssetManager.Bundle | any,
- prefabs: cc.Prefab | any,
- } = { bundles: {}, prefabs: {} };
- protected onLoad(): void {
- // 设置游戏窗口变化的回调(仅 Web 平台有效)
- cc.view.setResizeCallback(() => this.onResize());
- }
- /** 注册监听事件 */
- protected onEnable(): void {
- 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);
- this.adapt();
- }
- /** 取消监听事件 */
- protected onDisable(): void {
- NotifierCenter.ignoreScope(this);
- }
- /** 游戏开始 */
- async start() {
- common.hideLoading();
- let result = common.isDebug ? true : await common.httpCheckToken();
- if (result) {
- this.enterGame();
- }
- else {
- window.location.href = common.urlClass;
- }
- return;
- }
- /** 进入游戏 */
- async enterGame() {
- // 获取网络地址信息
- let json: UitlData = Tools.getNetLocationInfo();
- let unitNum = json.modelId ? json.modelId : common.unitCur;
- common.setUnitNum(unitNum);
- common.setItemId(json.itemId);
- let isHas = await this.initData(unitNum);
- if (isHas) {
- await AudioCB.getInstance().init();
- await AudioManager.getInstance().loadAudios();
- this.initUI();
- common.log("场景加载完成,等待进入游戏层");
- if (common.isDebug) {
- this.addPrefab(unitNum, common.getPageNum());
- }
- else {
- this.msgResultEnterUnit(unitNum);
- }
- }
- else {
- const options = {
- title: '',
- content: '当前动画游戏不存在!',
- };
- const params = {
- mode: PopupCacheMode.Once,
- };
- let prefab = await PopupManager.load('prefab/popUp');
- PopupManager.show(cc.instantiate(prefab), options, params);
- }
- };
- /** 初始化游戏数据 */
- async initData(key): Promise<boolean> {
- // 保存地图配置
- let unitAll = this.jsonConfig.json;
- 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;
- return true;
- }
- else {
- return false;
- }
- }
- 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.btnNext.zIndex = CConst.ZORDER_BUTTON_NEXT;
- this.nodeShow = null;
- }
- /** 添加预制体 分别处理游戏跟视频*/
- async addPrefab(unitNum: number, pageNum: number) {
- 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.nodeShow = cc.instantiate(prefab);
- let script = this.getScriptByNode(this.nodeShow, config.scriptName);
- if (script && script.initBundle) {
- script.initBundle(bundle, config);
- }
- this.node.addChild(this.nodeShow, CConst.ZORDER_GAME);
- this.node.getChildByName('bg').active = true;
- this.resetBtnNext(true);
- }
- /** 加载视频 先不去除游戏节点 */
- async addPrefabVideo(bundle, config) {
- this.removePrefab();// 隐藏游戏页
- // 加载视频页
- 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);
- }
- /** 事件回调:进入某个单元 */
- msgResultEnterUnit(unitNum: number) {
- common.setUnitNum(unitNum);
- this.addPrefab(unitNum, 1);
- }
- /** 事件回调:进入下一个游戏 */
- msgResultEnterGame() {
- this.resetBtnNext(false);
- let curUint = common.getUnitNum();
- let curPage = common.getPageNum();
- let attribute: AttributeUnit = common.attributeMap[curUint];
- let length = attribute.config.length;
- if (curPage < length) {
- this.addPrefab(curUint, curPage + 1);
- }
- else {
- this.scheduleOnce(()=>{
- // 返回课程详情页
- window.location.href = common.urlClass;
- }, 0.5);
- }
- }
- /**
- * 根据 组件名 获取 脚本组件
- * @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 */
- downLoadZip(zipName) {
- window['luojigou']['downloadAssets'](zipName);
- }
- /** 接收下载进度 */
- reseveInfoForDownLoad(info) { };
- }
|