diff --git a/package.json b/package.json index 5691ff4..ff9646b 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,9 @@ "version": "1.0.0", "description": "", "main": ".svrxrc.js", - "scripts": {}, + "scripts": { + "dev": "gulp" + }, "repository": { "type": "git", "url": "gitea@dd.tall.wiki:ccsens_fe/bird-go-home.git" diff --git a/src/classes/main.js b/src/classes/main.js index 4effc22..962589c 100644 --- a/src/classes/main.js +++ b/src/classes/main.js @@ -43,14 +43,29 @@ Main.prototype.init = function () { // 限制了两次动作间隔时间不能少于2s // 游戏状态在进行中才能触发 // play次数 >= 最多完成次数 调用结束的callback -Main.prototype.play = function () { - if (Date.now() - this.prevTime <= 2000 || state !== 1) return; +Main.prototype.play = function (direction) { + if (Date.now() - this.prevTime <= 1200 || state !== 1) return; + // if (state !== 1) return; this.element.play(); this.times += 1; + + this.computeScore(this.times, direction); + this.prevTime = Date.now(); if (this.times >= this.max) { this.times = this.max; this.endCallback(); } - console.log('this.times: ', this.times); +}; + +/** + * 计算当前的次数与分值 并发送给父窗口 + * @param {number} times 当前动作执行成功的次数 + * @param {number} direction play code + */ +Main.prototype.computeScore = function (times, direction = 0) { + const directionTarget = config.config.scores.find(item => item.direction === direction); + config.currentTimes = times; + config.currentScore += directionTarget.score; + sendMessage({ event: 'play', data: { currentTimes: times, currentScore: config.currentScore } }); }; diff --git a/src/classes/suspend.js b/src/classes/suspend.js index c33abb5..4102677 100644 --- a/src/classes/suspend.js +++ b/src/classes/suspend.js @@ -27,14 +27,24 @@ Suspend.prototype.suspendGame = function () { suspend.x = 50; suspend.y = lib.properties.height - 100; this.suspend = suspend; + const _this = this; // 继续游戏 this.suspend.addEventListener( 'click', function () { - stage.removeChild(suspend); + _this.hide(); continueMessage(); }, false, ); - stage.addChild(suspend); + // stage.addChild(this.suspend); + // stage.removeChild(this.suspend); +}; + +Suspend.prototype.hide = function () { + stage.removeChild(this.suspend); +}; + +Suspend.prototype.show = function () { + stage.addChild(this.suspend); }; diff --git a/src/custom.js b/src/custom.js index b6a8b35..66e95a7 100644 --- a/src/custom.js +++ b/src/custom.js @@ -6,7 +6,10 @@ const config = { level: 1, // 游戏难度级别 currentScore: 0, // 当前得分 currentTimes: 0, // 当前次数 - config: {}, // 得分配置 + config: { + scores: [{ direction: 0, score: 5 }], + directions: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + }, // 得分配置 }; let library = null; @@ -25,15 +28,15 @@ function initStage(lib) { Level.of(2); // 游戏难度级别 Back.of(); // 返回按钮 isHash(); + window.suspend = Suspend.of(); } // 游戏结束 显示结束得分面板 function gameOver() { state = 2; const times = main.times; - const score = parseInt((config.total / config.times) * times); setTimeout(() => { - End.of(score || 0); - finishMessage(score, times); + End.of(config.currentScore || 0); + finishMessage(config.currentScore, times); }, 2000); } diff --git a/src/message.js b/src/message.js index c6df1df..8154b90 100644 --- a/src/message.js +++ b/src/message.js @@ -2,6 +2,7 @@ window.addEventListener( 'message', function (e) { const res = e.data; + console.log('子->接受: ', res); switch (res.event) { case 'start': startGame(res.data); @@ -40,12 +41,12 @@ window.addEventListener( function playGame(data) { if (state !== 1) return; - const { score, times, status, param } = data; - config.currentScore = score; - config.currentTimes = times; + const { status, param } = data; + // config.currentScore = score; + // config.currentTimes = times; state = status; - if (config.config.directions[times - 1] === param.direction) { - main.play(); + if (config.config.directions[config.currentTimes] === param.direction) { + main.play(param.direction); } else { alert('动作不匹配'); } @@ -54,34 +55,36 @@ window.addEventListener( function pauseGame(data) { if (state !== 1) return; state = data.status; - Suspend.of(); + window.suspend.show(); window.timeInstance.pause(); } function continueGame(data) { if (state !== 3) return; state = data.status; + window.suspend.hide(); window.timeInstance.start(); } function finishGame(data) { const { score, times, status, param } = data; state = status; - config.total = score; - config.times = times; + // config.total = score; + // config.times = times; End.of(score || 0); - window.timeInstance.setDuration(0); + // window.timeInstance.setDuration(0); } }, false, ); // 发消息 -function sendMessage(data, src) { +function sendMessage(data) { if (!data) { return alert('错误: 发送消息数据为空'); } - top.postMessage(data, src); + console.log('子->发送: ', data); + top.postMessage(data, document.referrer); } // 发送继续游戏消息 @@ -92,7 +95,7 @@ function continueMessage() { status: 1, // 1 -> 进行中 }, }; - sendMessage(data, document.referrer); + sendMessage(data); } // 发送游戏结束消息 @@ -106,5 +109,5 @@ function finishMessage(score, times) { param: {}, // 额外个性化参数 }, }; - sendMessage(data, document.referrer); + sendMessage(data); } diff --git a/src/test.js b/src/test.js index f0673fa..5b69b21 100644 --- a/src/test.js +++ b/src/test.js @@ -51,7 +51,8 @@ function addHash() { function test() { document.addEventListener('click', () => { - main.play(); + if (!isDemo) return; + main.play(0); }); } test();