From a4c46863b5cdfbf1725fff05df0004ec51ed5bb0 Mon Sep 17 00:00:00 2001 From: song Date: Wed, 29 Sep 2021 18:02:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8E=E7=88=B6=E7=AA=97=E5=8F=A3=E9=80=9A?= =?UTF-8?q?=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/classes/suspend.js | 8 ++-- src/classes/time.js | 32 ++++++++++---- src/custom.js | 9 +++- src/message.js | 99 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 132 insertions(+), 16 deletions(-) diff --git a/src/classes/suspend.js b/src/classes/suspend.js index 1ba344c..c33abb5 100644 --- a/src/classes/suspend.js +++ b/src/classes/suspend.js @@ -24,15 +24,15 @@ Suspend.prototype.init = function () { Suspend.prototype.suspendGame = function () { const lib = this.lib; const suspend = new lib.suspend(); - suspend.x = 180; + suspend.x = 50; suspend.y = lib.properties.height - 100; this.suspend = suspend; - // 关闭演示模式 + // 继续游戏 this.suspend.addEventListener( 'click', function () { - console.log('暂停游戏'); - stage.removeChild(stopDemo); + stage.removeChild(suspend); + continueMessage(); }, false, ); diff --git a/src/classes/time.js b/src/classes/time.js index 35e2f08..2cc824c 100644 --- a/src/classes/time.js +++ b/src/classes/time.js @@ -43,20 +43,34 @@ Time.of = function (endCallback, duration = config.duration || 60, count = confi // 初始化 Time.prototype.init = function () { - console.log('初始化'); this.renderBg(); this.renderText(this.duration); }; +// 设置时长 +Time.prototype.setDuration = function (duration) { + this.duration = duration; + this.renderText(this.duration); +}; + // 开始游戏 开始倒计时 Time.prototype.start = function (startTime = Date.now()) { this.started = true; state = 1; this.startTime = startTime; + this.duration = leftDuration ? leftDuration : this.duration; this.update(); }; -// 开始游戏 开始倒计时 +// 暂停游戏 暂停倒计时 +Time.prototype.pause = function () { + this.started = false; + state = 3; + clearTimeout(this.timerId); + this.timerId = null; +}; + +// 结束游戏 结束倒计时 Time.prototype.end = function () { this.started = false; this.timerId && clearTimeout(this.timerId); @@ -78,6 +92,7 @@ Time.prototype.renderBg = function () { // 渲染文本 Time.prototype.renderText = function (time) { + if (this.text) stage.removeChild(this.text); const text = new createjs.Text(time, 'bold 40px Arial', '#823d16'); text.x = this.lib.properties.width - 100; text.y = 100; @@ -90,15 +105,16 @@ Time.prototype.renderText = function (time) { // 更新文本 Time.prototype.update = function () { if (this.timerId || !this.started) return; - // const endTime = this.startTime + this.count * 1000 + this.duration * 1000; - const endTime = this.startTime + this.duration * 1000; + // const endTime = this.startTime + this.duration * 1000; this.timerId = setInterval(() => { - let leftTime = Math.round((endTime - Date.now()) / 1000); + // let leftTime = Math.round((endTime - Date.now()) / 1000); + this.duration--; + leftDuration = this.duration; this.text && stage.removeChild(this.text); - if (leftTime <= 0) { - leftTime = 0; + if (this.duration <= 0) { + this.duration = 0; this.end(); } - this.renderText(leftTime); + this.renderText(this.duration); }, 1000); }; diff --git a/src/custom.js b/src/custom.js index 29b7445..f91982c 100644 --- a/src/custom.js +++ b/src/custom.js @@ -1,14 +1,18 @@ const config = { count: 5, // 默认倒计时时长 duration: 60, // 总时长 s - level: 1, // 游戏难度级别 total: 100, // 总分 times: 20, // 动作次数 + level: 1, // 游戏难度级别 + currentScore: 0, // 当前得分 + currentTimes: 0, // 当前次数 + config: {}, // 得分配置 }; let library = null; -let state = 0; // 游戏状态 0->未开始 1->进行中 2->结束 +let state = 0; // 游戏状态 0->未开始 1->进行中 2->结束 3->暂停 let isDemo = false; // 是不是演示模式 +let leftDuration = null; // 暂停时的时间 function initStage(lib) { library = lib; @@ -28,5 +32,6 @@ function gameOver() { const score = parseInt((config.total / config.times) * times); setTimeout(() => { End.of(score); + finishMessage(score, times); }, 2000); } diff --git a/src/message.js b/src/message.js index f32097e..f548fe5 100644 --- a/src/message.js +++ b/src/message.js @@ -1,7 +1,102 @@ window.addEventListener( 'message', - function (event) { - console.log('event: ', event); + function (e) { + const res = e.data; + switch (res.event) { + case 'start': + startGame(res.data); + return; + case 'play': + playGame(res.data); + return; + case 'pause': + pauseGame(res.data); + return; + case 'continue': + continueGame(res.data); + return; + default: + finishGame(res.data); + return; + } + + function startGame(data) { + const { count, game, status, param } = data; + config.count = count.duration; + config.duration = game.duration; + config.total = game.totalScore; + config.times = game.totalTimes; + config.level = game.level; + config.config = game.config; + state = status; + + window.timeInstance.setDuration(game.duration); + // 开始倒计时 + Count.of(countOver); + } + + function playGame(data) { + const { score, times, status, param } = data; + state = status; + config.currentScore = score; + config.currentTimes = times; + if (config.config.directions[times - 1] === param.direction) { + main.play(); + } else { + alert('动作不匹配'); + } + } + + function pauseGame(data) { + state = data.status; + Suspend.of(); + window.timeInstance.pause(); + } + + function continueGame(data) { + state = data.status; + window.timeInstance.start(); + } + + function finishGame(data) { + const { score, times, status, param } = data; + state = status; + config.total = score; + config.times = times; + } }, false, ); + +// 发消息 +function sendMessage(data, src) { + if (!data) { + return alert('错误: 发送消息数据为空'); + } + top.postMessage(data, src); +} + +// 发送继续游戏消息 +function continueMessage() { + const data = { + event: 'continue', + data: { + status: 1, // 1 -> 进行中 + }, + }; + sendMessage(data, document.referrer); +} + +// 发送游戏结束消息 +function finishMessage(score, times) { + const data = { + event: 'finish', + data: { + score: score, // 得分 + times: times, // 次数 + status: 2, // 游戏状态 0 1 2 + param: {}, // 额外个性化参数 + }, + }; + sendMessage(data, document.referrer); +}