mirror of
https://github.com/doublespeakgames/adarkroom.git
synced 2026-05-28 00:01:54 +08:00
6ace681a91
This commit adds a pause button during the fight. It relies upon the new Events.paused property: when set to true, no buttons will work except the pause button itself, and cooldown on attack/healing buttons is interrupted. This commit also groups event buttons in different groups (attack, healing, scene), so that the position of "eat meat" and "use meds" does not depend on how many scene buttons are present (sometimes 1, other times 2)
127 lines
3.3 KiB
JavaScript
127 lines
3.3 KiB
JavaScript
var Button = {
|
|
Button: function(options) {
|
|
if(typeof options.cooldown == 'number') {
|
|
this.data_cooldown = options.cooldown;
|
|
}
|
|
this.data_remaining = 0;
|
|
if(typeof options.click == 'function') {
|
|
this.data_handler = options.click;
|
|
}
|
|
|
|
var el = $('<div>')
|
|
.attr('id', typeof(options.id) != 'undefined' ? options.id : "BTN_" + Engine.getGuid())
|
|
.addClass('button')
|
|
.text(typeof(options.text) != 'undefined' ? options.text : "button")
|
|
.click(function() {
|
|
if(!$(this).hasClass('disabled') && (!Events.paused || $(this).data("handler") == Events.togglePause)) {
|
|
Button.cooldown($(this));
|
|
$(this).data("handler")($(this));
|
|
}
|
|
})
|
|
.data("handler", typeof options.click == 'function' ? options.click : function() { Engine.log("click"); })
|
|
.data("remaining", 0)
|
|
.data("cooldown", typeof options.cooldown == 'number' ? options.cooldown : 0);
|
|
|
|
el.append($("<div>").addClass('cooldown'));
|
|
|
|
// waiting for expiry of residual cooldown detected in state
|
|
Button.cooldown(el, 'state');
|
|
|
|
if(options.cost) {
|
|
var ttPos = options.ttPos ? options.ttPos : "bottom right";
|
|
var costTooltip = $('<div>').addClass('tooltip ' + ttPos);
|
|
for(var k in options.cost) {
|
|
$("<div>").addClass('row_key').text(_(k)).appendTo(costTooltip);
|
|
$("<div>").addClass('row_val').text(options.cost[k]).appendTo(costTooltip);
|
|
}
|
|
if(costTooltip.children().length > 0) {
|
|
costTooltip.appendTo(el);
|
|
}
|
|
}
|
|
|
|
if(options.width) {
|
|
el.css('width', options.width);
|
|
}
|
|
|
|
return el;
|
|
},
|
|
|
|
saveCooldown: true,
|
|
|
|
setDisabled: function(btn, disabled) {
|
|
if(btn) {
|
|
if(!disabled && !btn.data('onCooldown')) {
|
|
btn.removeClass('disabled');
|
|
} else if(disabled) {
|
|
btn.addClass('disabled');
|
|
}
|
|
btn.data('disabled', disabled);
|
|
}
|
|
},
|
|
|
|
isDisabled: function(btn) {
|
|
if(btn) {
|
|
return btn.data('disabled') === true;
|
|
}
|
|
return false;
|
|
},
|
|
|
|
cooldown: function(btn, option) {
|
|
var cd = btn.data("cooldown");
|
|
var id = 'cooldown.'+ btn.attr('id');
|
|
if(cd > 0) {
|
|
// param "start" takes value from cooldown time if not specified
|
|
var start, left;
|
|
switch(option){
|
|
case 'state':
|
|
if(!$SM.get(id)){
|
|
return;
|
|
}
|
|
start = Math.min($SM.get(id), cd);
|
|
left = (start / cd).toFixed(3);
|
|
break;
|
|
case 'pause':
|
|
left = (btn.children('div.cooldown').width() / btn.width()).toFixed(3);
|
|
start = cd * left;
|
|
break;
|
|
default:
|
|
start = cd;
|
|
left = 1;
|
|
}
|
|
Button.clearCooldown(btn);
|
|
if(Button.saveCooldown){
|
|
$SM.set(id,start);
|
|
// residual value is measured in seconds
|
|
// saves program performance
|
|
btn.data('countdown', Engine.setInterval(function(){
|
|
$SM.set(id, $SM.get(id, true) - 0.5, true);
|
|
},500));
|
|
}
|
|
var time = start;
|
|
if (Engine.options.doubleTime){
|
|
time /= 2;
|
|
}
|
|
$('div.cooldown', btn).width(Math.floor(left * 100) +"%").animate({width: '0%'}, time * 1000, 'linear', function() {
|
|
Button.clearCooldown(btn, true);
|
|
});
|
|
btn.addClass('disabled');
|
|
btn.data('onCooldown', true);
|
|
}
|
|
},
|
|
|
|
clearCooldown: function(btn, ended) {
|
|
if(!ended){
|
|
$('div.cooldown', btn).stop(true, true);
|
|
}
|
|
btn.data('onCooldown', false);
|
|
if(btn.data('countdown')){
|
|
window.clearInterval(btn.data('countdown'));
|
|
$SM.remove('cooldown.'+ btn.attr('id'));
|
|
btn.removeData('countdown');
|
|
}
|
|
if(!btn.data('disabled')) {
|
|
btn.removeClass('disabled');
|
|
}
|
|
}
|
|
};
|