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.
82 lines
2.2 KiB
82 lines
2.2 KiB
/**
|
|
* 游戏主体
|
|
* @param {function} endCallback 游戏结束的回调函数
|
|
* @param {number} max 最多运动次数
|
|
*
|
|
* @property {object} lib 资源对象
|
|
* @property {object} element 主体元素对象
|
|
* @property {number} prevTime 上次完成的时间ms
|
|
* @property {number} max 最多运动(play)次数
|
|
* @property {number} times 当前运动次数 play依次+1
|
|
* @property {function} endCallback 结束后 调用的回调函数
|
|
*/
|
|
function Main(endCallback, max) {
|
|
this.lib = library;
|
|
|
|
this.element = null;
|
|
this.prevTime = 0;
|
|
|
|
this.max = max;
|
|
this.times = 0;
|
|
this.endCallback = endCallback;
|
|
}
|
|
|
|
/**
|
|
* 静态方法 封装new init 返回实例
|
|
* @param {function} endCallback 游戏结束的回调函数
|
|
* @param {number} max 最多运动次数
|
|
*/
|
|
Main.of = (function () {
|
|
let instance = null;
|
|
return function (endCallback, max = config.times) {
|
|
if (!instance) {
|
|
instance = new Main(endCallback, max);
|
|
}
|
|
instance.init();
|
|
return instance;
|
|
};
|
|
})();
|
|
|
|
// 初始方法
|
|
Main.prototype.init = function () {
|
|
const target = new this.lib.Main();
|
|
this.element = target;
|
|
stage.addChild(target);
|
|
};
|
|
|
|
// play
|
|
// 限制了两次动作间隔时间不能少于3.5s
|
|
// 游戏状态在进行中才能触发
|
|
// play次数 >= 最多完成次数 调用结束的callback
|
|
Main.prototype.play = function (direction) {
|
|
if (Date.now() - this.prevTime <= 3500 || state !== 1) return;
|
|
// if (state !== 1) return;
|
|
this.element.play();
|
|
if (direction === 99) return;
|
|
|
|
this.times += 1;
|
|
|
|
this.computeScore(this.times, direction);
|
|
window.soundInstance.playBomb(direction);
|
|
|
|
this.prevTime = Date.now();
|
|
if (this.times >= this.max) {
|
|
this.times = this.max;
|
|
this.endCallback();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 计算当前的次数与分值 并发送给父窗口
|
|
* @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;
|
|
|
|
if (config.mode === 0) {
|
|
sendMessage({ event: 'play', data: { currentTimes: times, currentScore: config.currentScore } });
|
|
}
|
|
};
|
|
|