You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
2.9 KiB
133 lines
2.9 KiB
4 years ago
|
window.addEventListener(
|
||
|
'message',
|
||
|
function (e) {
|
||
|
const res = e.data;
|
||
|
console.log('子->接受: ', res);
|
||
|
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) {
|
||
|
if (state === 2) {
|
||
|
location.reload();
|
||
|
return;
|
||
|
}
|
||
|
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;
|
||
|
config.mode = game.mode;
|
||
|
|
||
|
window.timeInstance.setDuration(game.duration);
|
||
|
Level.of(config.level);
|
||
|
if (config.mode === 0) {
|
||
|
// 开始倒计时
|
||
|
Count.of(countOver);
|
||
|
} else {
|
||
|
sevenClick();
|
||
|
}
|
||
|
if (!window.soundInstance) return;
|
||
|
window.soundInstance.playBgm();
|
||
|
}
|
||
|
|
||
|
function playGame(data) {
|
||
|
if (state !== 1) return;
|
||
|
const { status, param } = data;
|
||
|
// config.currentScore = score;
|
||
|
// config.currentTimes = times;
|
||
|
state = status;
|
||
|
if (config.config.directions[config.currentTimes] === param.direction) {
|
||
|
main.play(param.direction);
|
||
|
} else {
|
||
|
alert('动作不匹配');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function pauseGame(data) {
|
||
|
if (state !== 1) return;
|
||
|
state = data.status;
|
||
|
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;
|
||
|
End.of(score || 0);
|
||
|
window.timeInstance.setDuration(0);
|
||
|
}
|
||
|
},
|
||
|
false,
|
||
|
);
|
||
|
|
||
|
// 发消息
|
||
|
function sendMessage(data) {
|
||
|
if (!data) {
|
||
|
return alert('错误: 发送消息数据为空');
|
||
|
}
|
||
|
console.log('子->发送: ', data);
|
||
|
top.postMessage(data, document.referrer);
|
||
|
}
|
||
|
|
||
|
// 发送继续游戏消息
|
||
|
function continueMessage() {
|
||
|
const data = {
|
||
|
event: 'continue',
|
||
|
data: {
|
||
|
status: 1, // 1 -> 进行中
|
||
|
},
|
||
|
};
|
||
|
sendMessage(data);
|
||
|
}
|
||
|
|
||
|
// 发送游戏结束消息
|
||
|
function finishMessage(score, times) {
|
||
|
const data = {
|
||
|
event: 'finish',
|
||
|
data: {
|
||
|
score: score, // 得分
|
||
|
times: times, // 次数
|
||
|
status: 2, // 游戏状态 0 1 2
|
||
|
param: {}, // 额外个性化参数
|
||
|
},
|
||
|
};
|
||
|
sendMessage(data);
|
||
|
}
|
||
|
|
||
|
// 发送再来一次消息
|
||
|
function againMessage() {
|
||
|
const data = {
|
||
|
event: 'again',
|
||
|
data: {
|
||
|
param: {}, // 额外个性化参数
|
||
|
},
|
||
|
};
|
||
|
sendMessage(data);
|
||
|
}
|