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.
125 lines
3.3 KiB
125 lines
3.3 KiB
/**
|
|
* 游戏时长 计时
|
|
* @param {function} endCallback 倒计时结束的回调函数
|
|
* @param {number} duration 游戏时长
|
|
* @param {number} count 游戏倒计时时长
|
|
*
|
|
* @property {number} duration 游戏时长
|
|
* @property {number} count 游戏倒计时时长
|
|
* @property {number} startTime 游戏开始是ms
|
|
* @property {boolean} started 游戏是否开始
|
|
* @property {object} lib 资源库
|
|
* @property {object} bg 时间倒计时背景
|
|
* @property {object} text 倒计时文本
|
|
* @property {number} timerId 定时器id
|
|
* @property {function} endCallback 结束回调函数
|
|
*/
|
|
function Time(endCallback, duration, count) {
|
|
this.duration = duration;
|
|
this.count = count;
|
|
this.startTime = Date.now();
|
|
|
|
this.started = false; // 是否开始游戏
|
|
|
|
this.lib = library;
|
|
this.bg = null;
|
|
this.text = null;
|
|
this.timerId = null;
|
|
this.endCallback = endCallback;
|
|
}
|
|
|
|
/**
|
|
* 静态方法 封装new init 返回实例
|
|
* @param {function} endCallback 倒计时结束的回调函数
|
|
* @param {number} duration 游戏时长
|
|
* @param {number} count 游戏倒计时时长
|
|
* @returns
|
|
*/
|
|
Time.of = (function () {
|
|
let instance = null;
|
|
return function (endCallback, duration = config.duration || 60, count = config.count || 5) {
|
|
if (!instance) {
|
|
instance = new Time(endCallback, duration, count);
|
|
}
|
|
instance.init();
|
|
return instance;
|
|
};
|
|
})();
|
|
|
|
// 初始化
|
|
Time.prototype.init = function () {
|
|
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);
|
|
this.timerId = null;
|
|
// 执行结束后的回调
|
|
this.endCallback();
|
|
};
|
|
|
|
// 渲染背景
|
|
Time.prototype.renderBg = function () {
|
|
const initX = this.lib.properties.width - 100;
|
|
const initY = 80;
|
|
const instance = new this.lib.timeBg();
|
|
instance.x = initX;
|
|
instance.y = initY;
|
|
this.bg = instance;
|
|
stage.addChild(instance);
|
|
};
|
|
|
|
// 渲染文本
|
|
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;
|
|
text.textAlign = 'center';
|
|
text.textBaseline = 'alphabetic';
|
|
this.text = text;
|
|
stage.addChild(text);
|
|
};
|
|
|
|
// 更新文本
|
|
Time.prototype.update = function () {
|
|
if (this.timerId || !this.started) return;
|
|
// const endTime = this.startTime + this.duration * 1000;
|
|
this.timerId = setInterval(() => {
|
|
// let leftTime = Math.round((endTime - Date.now()) / 1000);
|
|
this.duration--;
|
|
leftDuration = this.duration;
|
|
this.text && stage.removeChild(this.text);
|
|
if (this.duration <= 0) {
|
|
this.duration = 0;
|
|
this.end();
|
|
}
|
|
this.renderText(this.duration);
|
|
}, 1000);
|
|
};
|
|
|