From 07998376e72cce92b106856d83d4b32eb7950509 Mon Sep 17 00:00:00 2001 From: Andrea Rendine Date: Wed, 17 Feb 2016 01:53:21 +0100 Subject: [PATCH 1/4] Several changes As some of them are related to each other, this commit involves a lot of updates. * Autopause on blur. This is an idea I took from Gridland (!), if window loses focus the fight pauses (timed check courtesy of the enemy), and it restarts when the window has focus again (if the pause button is clicked, then the player must click again in order to resume). * Delayed start for fights. This will allow players to read the description without the player attacking them. No way to cheat, buttons are not enabled. I used Events._LEAVE_COOLDOWN as cooldown time, not for laziness, but because I guess the two durations have the same purpose: to allow (force) the player to read stuff. * Take everything and leave just by name. If the player can just take something, the text is "take all you can", if they can take everything but there are several options it's "take everything", if the option is only one the text is "take everything and X", where X is the verb of the only possible option (note: in the latter case, the button cools down together with the option itself). * success/failure mechanics no longer relies upon try/catch, but on a simple mechanism: a variable taking a bool value, set to true when one of the end conditions happens. The precedence goes to the enemy (if the player kills and dies the fight is lost). --- script/events.js | 101 +++++++++++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 39 deletions(-) diff --git a/script/events.js b/script/events.js index f6d614b..156e81a 100644 --- a/script/events.js +++ b/script/events.js @@ -73,23 +73,23 @@ var Events = { startCombat: function(scene) { Engine.event('game event', 'combat'); - Events.lost = false; + Events.fought = false; var desc = $('#description', Events.eventPanel()); $('
').text(scene.notification).appendTo(desc); // Draw pause button - Events.paused = true; var pauseBox = $('
').attr('id', 'pauseButton').appendTo(desc); var pause = new Button.Button({ id: 'pause', text: '', - cooldown: Events._PAUSE_COOLDOWN, + cooldown: Events._LEAVE_COOLDOWN, click: Events.togglePause }).appendTo(pauseBox); $('').addClass('text').insertBefore(pause.children('.cooldown')); $('
').addClass('clear').appendTo(pauseBox); - Events.togglePause(pause); + Events.setPause(pause, 'set'); + Events.removePause(pause, 'set'); var fightBox = $('
').attr('id', 'fight').appendTo(desc); // Draw the wanderer @@ -139,34 +139,56 @@ var Events = { Events._enemyAttackTimer = Engine.setInterval(Events.enemyAttack, scene.attackDelay * 1000); }, - setPause: function(){ - Events.paused = true; - $('#event').addClass('paused'); - Button.clearCooldown($('#pause')); - var active = 0; + setPause: function(btn, state){ + if(!btn) { + btn = $('#pause'); + } + var event = btn.closest('#event'); + var string, log; + if(state == 'set') { + string = 'start.'; + log = 'loaded'; + } else { + string = 'resume.'; + log = 'paused'; + } + btn.children('.text').first().text( _(string) ) + Events.paused = (state == 'auto') ? 'auto' : true; + event.addClass('paused'); + Button.clearCooldown(btn); $('#buttons').find('.button').each(function(i){ if($(this).data('onCooldown')){ $(this).children('.cooldown').stop(true,false); - active++; } }); - Engine.log('fight paused. stopped '+ active +' buttons'); + Engine.log('fight '+ log +'.'); }, - removePause: function(state){ + removePause: function(btn, state){ + if(!btn) { + btn = $('#pause'); + } + var event = btn.closest('#event'); var log, time, target; + if(state == 'auto' && Events.paused != 'auto') { + return; + } switch(state){ case 'set': log = 'started'; - time = 0; + time = btn.data('cooldown') * 1000; target = $(); + Button.cooldown(btn); + btn.data('cooldown',Events._PAUSE_COOLDOWN); break; case 'end': - $('#pause').addClass('disabled'); + Button.setDisabled(btn, true); log = 'ended'; time = Events._FIGHT_SPEED; target = $(); break; + case 'auto': + Button.cooldown(btn); default: log = 'resumed'; time = Events._PAUSE_COOLDOWN * 1000; @@ -174,8 +196,9 @@ var Events = { break; } Engine.setTimeout(function(){ + btn.children('.text').first().text( _('pause.') ); Events.paused = false; - $('#event').removeClass('paused'); + event.removeClass('paused'); target.each(function(i){ if($(this).data('onCooldown')){ Button.cooldown($(this), 'pause'); @@ -185,15 +208,16 @@ var Events = { }, time); }, - togglePause: function(btn){ - var text = btn.children('.text').first(); - if(Events.paused) { - Events.removePause() - text.text( _('pause.') ); - } else { - Events.setPause(); - text.text( _('resume.') ) + togglePause: function(btn, auto){ + if(!btn) { + btn = $('#pause'); } + if((auto) && (document.hasFocus() == !Events.paused)) { + return; + } + var f = (Events.paused) ? Events.removePause : Events.setPause; + var state = (auto) ? 'auto' : false; + f(btn, state); }, createEatMeatButton: function(cooldown) { @@ -474,6 +498,7 @@ var Events = { }, enemyAttack: function() { + Events.togglePause($('#pause'),'auto'); if(Events.paused){ return; @@ -507,14 +532,14 @@ var Events = { }, endFight: function() { + Events.fought = true; clearTimeout(Events._enemyAttackTimer); - Events.removePause('end'); + Events.removePause($('#pause'), 'end'); }, winFight: function() { Engine.setTimeout(function() { - // Check after animation time, to see if the player has died before winning - if(Events.lost) { + if(Events.fought) { return; } Events.endFight(); @@ -566,7 +591,6 @@ var Events = { }, loseFight: function(){ - Events.lost = true; Events.endFight(); Events.endEvent(); World.die(); @@ -665,6 +689,7 @@ var Events = { var takeET = new Button.Button({ id: 'loot_takeEverything', text: '', + cooldown: Events._LEAVE_COOLDOWN, click: Events.takeEverything }).appendTo(takeETrow); $('').insertBefore(takeET.children('.cooldown')); @@ -679,7 +704,9 @@ var Events = { }, setTakeAll: function(lootButtons){ - var lootButtons = lootButtons || $('#lootButtons'); + if(!lootButtons) { + lootButtons = $('#lootButtons'); + } var canTakeSomething = false; var free = Path.getFreeSpace(); var takeETbutton = lootButtons.find('#loot_takeEverything'); @@ -703,11 +730,7 @@ var Events = { takeAll.children('span').first().text(_('all')); } }); - if(canTakeSomething){ - takeETbutton.removeClass('disabled'); - } else { - takeETbutton.addClass('disabled'); - } + Button.setDisabled(takeETbutton, !canTakeSomething); takeETbutton.data('canTakeEverything', (free >= 0) ? true : false); return takeETbutton; }, @@ -722,16 +745,16 @@ var Events = { }, canLeave: function(btn){ - var basetext = _('take everything'); + var basetext = (btn.data('canTakeEverything')) ? _('take everything') : _('take all you can'); var textbox = btn.children('span'); var takeAndLeave = (btn.data('leaveBtn')) ? btn.data('canTakeEverything') : false; + var text = _(basetext); if(takeAndLeave){ - textbox.text( basetext + _(' and ') + _('leave') ); - btn.data('canLeave', true); - } else { - textbox.text( basetext ); - btn.data('canLeave', false) + Button.cooldown(btn); + text += _(' and ') + btn.data('leaveBtn').text(); } + textbox.text( text ); + btn.data('canLeave', takeAndLeave); }, dropStuff: function(e) { From e4bb616a938e5f7d90eb0169d21d2592e156f133 Mon Sep 17 00:00:00 2001 From: Andrea Rendine Date: Wed, 17 Feb 2016 01:55:53 +0100 Subject: [PATCH 2/4] Legends Text set by data-legend attribute --- css/main.css | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/css/main.css b/css/main.css index 35ceb0e..8249e56 100644 --- a/css/main.css +++ b/css/main.css @@ -581,7 +581,7 @@ body.noMask #description { } #lootButtons:before { - content: "take:"; + content: attr(data-legend); position: absolute; top: -25px; left: 0px; @@ -599,7 +599,7 @@ body.noMask #description { } #dropMenu:before { - content: "drop:"; + content: attr(data-legend); border-bottom: 1px solid black; display: block; margin-bottom: 5px; From ac34b717d32146e8efae8a0784e5b7d1c1ca08b9 Mon Sep 17 00:00:00 2001 From: Andrea Rendine Date: Wed, 17 Feb 2016 10:19:32 +0100 Subject: [PATCH 3/4] Forcing cooldown Added an option. Button.cooldown(btn, X) where X is a number, forces a one-time cooldown of X seconds for btn. --- script/Button.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/script/Button.js b/script/Button.js index 4d92077..03aa042 100644 --- a/script/Button.js +++ b/script/Button.js @@ -70,6 +70,9 @@ var Button = { var cd = btn.data("cooldown"); var id = 'cooldown.'+ btn.attr('id'); if(cd > 0) { + if(typeof option == 'number') { + cd = option; + } // param "start" takes value from cooldown time if not specified var start, left; switch(option){ From df63ccb6514c9f1c64f20b9a67fa3cff3971da6e Mon Sep 17 00:00:00 2001 From: Andrea Rendine Date: Wed, 17 Feb 2016 10:27:38 +0100 Subject: [PATCH 4/4] Switch cooldown Easy way to switch cooldown on pause button according to new button option --- script/events.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/script/events.js b/script/events.js index 156e81a..b26e42a 100644 --- a/script/events.js +++ b/script/events.js @@ -83,7 +83,7 @@ var Events = { var pause = new Button.Button({ id: 'pause', text: '', - cooldown: Events._LEAVE_COOLDOWN, + cooldown: Events._PAUSE_COOLDOWN, click: Events.togglePause }).appendTo(pauseBox); $('').addClass('text').insertBefore(pause.children('.cooldown')); @@ -175,11 +175,10 @@ var Events = { } switch(state){ case 'set': + Button.cooldown(btn, Events._LEAVE_COOLDOWN); log = 'started'; - time = btn.data('cooldown') * 1000; + time = Events._LEAVE_COOLDOWN * 1000; target = $(); - Button.cooldown(btn); - btn.data('cooldown',Events._PAUSE_COOLDOWN); break; case 'end': Button.setDisabled(btn, true);