"use strict"; cc._RF.push(module, 'b26a49RCSFPT4C+F7xAd62t', 'GameManager'); // Script/GameManager.ts "use strict"; // Learn TypeScript: // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); var GameData_1 = require("./GameData"); var GameTool_1 = require("./tool/GameTool"); var Notification_1 = require("./tool/Notification"); var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property; var GameManager = /** @class */ (function (_super) { __extends(GameManager, _super); function GameManager() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.Camera = null; _this.topUI = null; _this.Player = null; _this.Ground = null; _this.Xin = null; _this.time = null; _this.GameOver = null; _this.Tip = null; _this.blockPrefab = []; return _this; } GameManager_1 = GameManager; // LIFE-CYCLE CALLBACKS: GameManager.prototype.onLoad = function () { // cc.game.setFrameRate(59.9); // 示例使用 var manager = cc.director.getCollisionManager(); manager.enabled = true; // manager.enabledDebugDraw = true; GameManager_1._instance = this; GameData_1.default._instance.GM_INFO.probation = false; }; GameManager.prototype.onEnable = function () { Notification_1.Notification.on("createBlock", this.createBlock, this); Notification_1.Notification.on("addScore", this.addScore, this); Notification_1.Notification.on("createCrackBlock", this.createCrackBlock, this); Notification_1.Notification.on("showGround", this.showGround, this); Notification_1.Notification.on("death", this.getDeath, this); Notification_1.Notification.on("jump", this.jump, this); Notification_1.Notification.on("setData", this.setData, this); }; GameManager.prototype.onDestroy = function () { Notification_1.Notification.off("createBlock", this.createBlock); Notification_1.Notification.off("addScore", this.addScore); Notification_1.Notification.off("createBlock", this.createCrackBlock); Notification_1.Notification.off("death", this.getDeath); Notification_1.Notification.off("jump", this.jump); Notification_1.Notification.off("showGround", this.showGround); Notification_1.Notification.off("setData", this.setData); }; GameManager.prototype.init = function () { this.drop = this.topUI.y - this.Camera.node.y; this.score = 0; this.oldSpeed = 10; this.begin = true; this.over = false; this.interfere = false; this.round = 0; this.level = 0; GameData_1.default._instance.GM_INFO.levelMax = 1; GameData_1.default._instance.GM_INFO.difficultyMax = 1; GameData_1.default._instance.GM_INFO.life = 3; this.blockArray = []; this.cameraMove = false; this.countTime = 120; this.topUI.getChildByName("xin1").active = true; this.topUI.getChildByName("xin2").active = true; this.topUI.getChildByName("xin3").active = true; this.topUI.getChildByName("star1").active = false; this.topUI.getChildByName("star2").active = false; this.topUI.getChildByName("star3").active = false; GameData_1.default._instance.LEVEL_init(); this.schedule(this.updateCountDownTime, 1); }; GameManager.prototype.start = function () { // this.node.on(cc.Node.EventType.TOUCH_START, this.jump, this); this.fit(); this.init(); if (GameData_1.default._instance.GM_INFO.probation) this.startGuide(); else this.startGame(); }; //根据是否全面屏,做独立适配方面 GameManager.prototype.fit = function () { this.node.getChildByName("bg2").y = -2167.675; var jg = this.setFit(); if (!jg) { this.node.getChildByName("bg2").y = -2100; this.topUI.y = 660; } }; //判断全面屏 GameManager.prototype.getSetScreenResolutionFlag = function () { var size = cc.winSize; var width = size.width; var height = size.height; if ((height / width) > (16.2 / 9)) return false; return true; }; //判断全面屏适配 GameManager.prototype.setFit = function () { var flag = this.getSetScreenResolutionFlag(); if (flag) { } else { } return flag; }; GameManager.prototype.startGuide = function () { //打开卷 var left = this.Tip.getChildByName("left"); var right = this.Tip.getChildByName("right"); var tip = this.Tip.getChildByName("tip"); left.width = 0; right.width = 0; tip.opacity = 0; // cc.tween(left) // .to(0.25,{width:348}) // .start(); // cc.tween(right) // .to(0.25,{width:348}) // .start(); // cc.tween(tip) // .delay(0.15) // .to(0.15,{opacity:255}) // .start(); }; GameManager.prototype.showGround = function (tempY) { var _this = this; setTimeout(function () { _this.Ground.active = true; _this.Ground.y = tempY; }, 500); }; GameManager.prototype.jump = function () { this.Player.getComponent("Player").jump(); }; GameManager.prototype.startGame = function () { this.createBlock(); }; GameManager.prototype.addScore = function (score) { var _this = this; GameData_1.default._instance.CLICK_DATA.getScore = parseInt(score); GameManager_1._instance.score += parseInt(score); GameData_1.default._instance.CLICK_DATA.totalScore = GameManager_1._instance.score; this.topUI.getChildByName("nandu").getComponent(cc.Label).string = GameManager_1._instance.score + ""; var scale = GameManager_1._instance.score / GameData_1.default._instance.LEVEL_INFO.scoreMax; cc.tween(this.topUI.getChildByName("progress").getComponent(cc.Sprite)) .to(0.5, { fillRange: scale }) .call(function () { if (scale >= GameData_1.default._instance.LEVEL_INFO.star1 && !_this.topUI.getChildByName("star1").active) { _this.topUI.getChildByName("star1").active = true; } if (scale >= GameData_1.default._instance.LEVEL_INFO.star2 && !_this.topUI.getChildByName("star2").active) { _this.topUI.getChildByName("star2").active = true; } if (scale >= GameData_1.default._instance.LEVEL_INFO.star3 && !_this.topUI.getChildByName("star3").active) { _this.topUI.getChildByName("star3").active = true; // GameData._instance.scoreMax = 150000; } }) .start(); }; GameManager.prototype.createBlock = function () { if (this.over == false && this.begin == true) { this.Ground.active = false; var num = Math.floor(Math.random() * 12); if (this.round <= 10 && num == 5) { num = Math.floor(Math.random() * 6 + 6); } var block = cc.instantiate(this.blockPrefab[num]); var height = this.Player.getComponent("Player").basicHeight + GameData_1.default._instance.GM_INFO.blockMin; if (num > GameData_1.default._instance.GM_INFO.blockScale) { height += block.height + GameData_1.default._instance.GM_INFO.blockMax; GameData_1.default._instance.CLICK_DATA.height = true; } if (height >= 0 && this.cameraMove == false) { this.countHeight = this.Player.getComponent("Player").basicHeight - this.Camera.node.y; this.cameraMove = true; } block.setPosition(cc.v2(this.round % 2 == 0 ? 480 : -480, height)); block.parent = this.node.getChildByName("Block"); block.zIndex = 10; this.round += 1; this.level += 1; if (this.round > GameData_1.default._instance.GM_INFO.levelMax) GameData_1.default._instance.GM_INFO.levelMax = this.round; GameData_1.default._instance.CLICK_DATA.level = this.round; GameData_1.default._instance.CLICK_DATA.round = this.level; GameData_1.default._instance.LEVEL_INFO.layer += GameData_1.default._instance.LEVEL_INFO.layerAdd; GameData_1.default._instance.LEVEL_INFO.speed += GameData_1.default._instance.LEVEL_INFO.addSpeed; if (GameData_1.default._instance.LEVEL_INFO.reduceSpeed <= 100) GameData_1.default._instance.LEVEL_INFO.reduceSpeed += GameData_1.default._instance.LEVEL_INFO.jiansu; if (this.round == GameData_1.default._instance.LEVEL_INFO.round1) GameData_1.default._instance.LEVEL_INFO.jiansu = 2; else if (this.round == GameData_1.default._instance.LEVEL_INFO.round2) GameData_1.default._instance.LEVEL_INFO.jiansu = 1; else if (this.round == GameData_1.default._instance.LEVEL_INFO.round3) GameData_1.default._instance.LEVEL_INFO.jiansu = 0.5; } }; //虚假碎裂块 GameManager.prototype.createCrackBlock = function () { this.interfere = true; GameData_1.default._instance.CLICK_DATA.fake = this.interfere; if (this.round > 1) this.destroyBlock(); if (this.over == false && this.begin == true) { var block = cc.instantiate(this.blockPrefab[12]); var height = this.Player.getComponent("Player").basicHeight; if (GameData_1.default._instance.CLICK_DATA.height == true) { height += block.height / 2; } block.setPosition(cc.v2(this.round % 2 == 0 ? 480 : -480, height)); block.parent = this.node.getChildByName("CrackBlock"); block.zIndex = 1; } }; GameManager.prototype.destroyBlock = function () { if (this.node) { if (this.node.getChildByName("CrackBlock")) { if (this.node.getChildByName("CrackBlock").children) { var block2 = this.node.getChildByName("CrackBlock").children; for (var i = 0; i < block2.length; i++) { if (block2[i]) { block2[i].getComponent("Block").hide(); } } } } } }; GameManager.prototype.xinAction = function () { var xin = this.topUI.getChildByName("xin1"); if (GameData_1.default._instance.GM_INFO.life == 2) xin = this.topUI.getChildByName("xin3"); else if (GameData_1.default._instance.GM_INFO.life == 1) xin = this.topUI.getChildByName("xin2"); cc.tween(xin) .to(0.6, { scale: 1.6, opacity: 0 }) .call(function () { xin.active = false; }) .start(); }; //死亡 GameManager.prototype.getDeath = function () { if (GameData_1.default._instance.GM_INFO.life > 0) { GameData_1.default._instance.GM_INFO.life -= 1; this.getDevive(1); } else { GameData_1.default._instance.GM_INFO.life = -1; this.getDevive(3); } }; //复活 GameManager.prototype.getDevive = function (time) { var _this = this; this.round = 0; GameData_1.default._instance.LEVEL_init(); if (this.topUI.getChildByName("star3").active == true) { // GameData._instance.LEVEL_INFO.scoreMax = 150000; } this.interfere = false; GameData_1.default._instance.CLICK_DATA.fake = this.interfere; var block = this.node.getChildByName("Block").children; for (var i = 0; i < block.length; i++) { if (block[i]) { block[i].active = false; } } var block2 = this.node.getChildByName("CrackBlock").children; for (var i = 0; i < block2.length; i++) { if (block2[i]) { block2[i].active = false; } } // this.blockArray = []; var actionTime = 1; if (this.Player.y > 500) { actionTime = (this.Player.y + 500) / 1000 * actionTime; } cc.tween(this.Player) .to(actionTime, { y: -500 }, { easing: 'cubicIn' }) .call(function () { _this.Player.getComponent("Player").xinAction(); }) .delay(0.5) .call(function () { _this.xinAction(); _this.Player.getComponent("Player").init(); if (time > 1) { _this.Xin.parent.active = true; _this.Xin.getComponent(cc.Sprite).fillRange = 0; cc.tween(_this.Xin.getComponent(cc.Sprite)) .to(time, { fillRange: 1 }) .delay(0.3) .call(function () { _this.Xin.parent.active = false; }) .delay(0) .call(function () { _this.createBlock(); }) .start(); } else { setTimeout(function () { _this.createBlock(); }, (time + 0.5) * 1000); } }) .start(); }; GameManager.prototype.setData = function () { GameData_1.default._instance.CLICK_DATA.timer = this.countTime; GameData_1.default._instance.CLICK_DATA.level = this.round; GameData_1.default._instance.CLICK_DATA.round = this.level; //CLICK_DATA 数据处理完毕后调用上传接口 上传接口内CLICK_DATA不做改变 GameTool_1.GameTool.setGameData(); }; //获取matchId 用于上传每次点击数据里面记录id方便查询 GameManager.prototype.getMatchId = function () { var matchId = cc.sys.localStorage.getItem("matchId"); if (matchId == "undifend" || matchId == null) { matchId = this.setMatchId(); } else { if (this.containsNanana(matchId) == true) { matchId = this.setMatchId(); } else { var char = parseInt(matchId[10]); if (this.level == 1) { char += 1; } matchId = matchId.slice(0, 10) + char + ""; GameData_1.default._instance.GM_INFO.matchId = matchId; cc.sys.localStorage.setItem("matchId", matchId); } } return matchId; }; //检测matchId 如果有缓存以前的nanana数据清除 GameManager.prototype.containsNanana = function (str) { return /na/i.test(str); }; //重新设置MatchId GameManager.prototype.setMatchId = function () { // 定义包含可用字符的字符集 var characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; // 创建一个数组以保存随机字符 var uuidArray = []; // 循环10次 生成10位的UUID for (var i = 0; i < 10; i++) { // 生成随机索引,范围是字符集的长度 var randomIndex = Math.floor(Math.random() * characters.length); // 从字符集中获取随机字符 var randomChar = characters.charAt(randomIndex); // 将字符添加到数组中 uuidArray.push(randomChar); } var data = uuidArray.join('') + 1 + ""; cc.sys.localStorage.setItem("matchNumber", 1); cc.sys.localStorage.setItem("matchId", data); GameData_1.default._instance.GM_INFO.matchId = data; return data; }; GameManager.prototype.updateCountDownTime = function () { var _this = this; if (this.countTime > 0) { this.countTime -= 1; this.time.string = this.getTimeMargin(this.countTime); if (this.countTime < 5) { cc.tween(this.time.node) .to(0.25, { scale: 1.5, color: cc.color(255, 0, 0) }) .to(0.25, { scale: 1, color: cc.color(255, 255, 255) }) .start(); } if (this.countTime <= 0) { this.unschedule(this.updateCountDownTime); this.begin = false; this.over = true; this.overStop(); this.setRank(); setTimeout(function () { _this.overOpen(); }, 2000); } } }; //停止所有动画 GameManager.prototype.overStop = function () { if (this.Player.getComponent("Player").jumpAction) this.Player.getComponent("Player").jumpAction.stop(); var block = this.node.getChildByName("Block").children; for (var i = 0; i < block.length; i++) { if (block[i]) { if (block[i].getComponent("Block").tween) block[i].getComponent("Block").tween.stop(); } } var block2 = this.node.getChildByName("CrackBlock").children; for (var i = 0; i < block2.length; i++) { if (block2[i]) { if (block2[i].getComponent("Block").tween) block2[i].getComponent("Block").tween.stop(); } } }; GameManager.prototype.setRank = function () { GameData_1.default._instance.GM_INFO.score = this.score; GameTool_1.GameTool.setRank(); }; GameManager.prototype.overOpen = function () { this.Player.active = false; var block = this.node.getChildByName("Block").children; for (var i = 0; i < block.length; i++) { if (block[i]) { block[i].active = false; } } this.destroyBlock(); this.GameOver.active = true; this.GameOver.getChildByName("score").getComponent(cc.Label).string = this.score + ""; this.GameOver.getChildByName("level").getComponent(cc.Label).string = GameData_1.default._instance.GM_INFO.levelMax + ""; this.GameOver.getChildByName("difficulty").getComponent(cc.Label).string = GameData_1.default._instance.GM_INFO.difficultyMax + ""; }; GameManager.prototype.backLoad = function () { cc.director.loadScene("LoadScene"); }; GameManager.prototype.again = function () { cc.director.loadScene("GameScene"); }; GameManager.prototype.openRank = function () { cc.director.loadScene("RankScene"); }; //时间格式转换 GameManager.prototype.getTimeMargin = function (second) { var total = 0; total = second; var hour = 0; hour = parseInt((total / 3600) + ""); //计算整数小时数 var afterHour = total - hour * 60 * 60; //取得算出小时数后剩余的秒数 var min = parseInt((afterHour / 60) + ""); //计算整数分 var m = "" + min; if (min < 10) m = "0" + min; var afterMin = total - hour * 60 * 60 - min * 60; //取得算出分后剩余的秒数 var miao = afterMin + ""; if (afterMin < 10) miao = "0" + afterMin; return m + ':' + miao; }; GameManager.prototype.update = function (dt) { if (this.cameraMove == true && this.over == false) { this.Camera.node.y = this.Player.y - this.countHeight; if (this.Camera.node.y <= 0) this.Camera.node.y = 0; this.topUI.y = this.Camera.node.y + this.drop; } }; var GameManager_1; __decorate([ property(cc.Camera) ], GameManager.prototype, "Camera", void 0); __decorate([ property(cc.Node) ], GameManager.prototype, "topUI", void 0); __decorate([ property(cc.Node) ], GameManager.prototype, "Player", void 0); __decorate([ property(cc.Node) ], GameManager.prototype, "Ground", void 0); __decorate([ property(cc.Node) ], GameManager.prototype, "Xin", void 0); __decorate([ property(cc.Label) ], GameManager.prototype, "time", void 0); __decorate([ property(cc.Node) ], GameManager.prototype, "GameOver", void 0); __decorate([ property(cc.Node) ], GameManager.prototype, "Tip", void 0); __decorate([ property([cc.Prefab]) ], GameManager.prototype, "blockPrefab", void 0); GameManager = GameManager_1 = __decorate([ ccclass ], GameManager); return GameManager; }(cc.Component)); exports.default = GameManager; cc._RF.pop();