4 changed files with 132 additions and 16 deletions
@ -1,7 +1,102 @@ |
|||||
window.addEventListener( |
window.addEventListener( |
||||
'message', |
'message', |
||||
function (event) { |
function (e) { |
||||
console.log('event: ', event); |
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, |
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); |
||||
|
} |
||||
|
Loading…
Reference in new issue