Last active
December 17, 2015 02:49
-
-
Save wilsonjackson/5538538 to your computer and use it in GitHub Desktop.
Questing hot keys for Candy Box !
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Automatically creates hot keys for each potion/scroll/magic item you can use in a quest using the | |
// first available letter in the button's text. Special exclusions are made for keys that are used by | |
// the game itself — if you don't know why, I won't spoil it for you. | |
// Hot keys can be enabled or disabled by a button at the top of the page, or by pressing CTRL+K or | |
// CMD+K. | |
!function (undefined) { | |
var enabled = false, bindings = {}, reserved = 'IK'; | |
$.each(potions.list, function (i, potion) { | |
var text = potion.buttonText, | |
action = potion.action, | |
pos = 0, | |
chrUp, chr; | |
do { | |
chr = text.charAt(pos++); | |
chrUp = chr.toUpperCase(); | |
} | |
while (chrUp && (bindings[chrUp] || reserved.indexOf(chrUp) > -1)); | |
if (chrUp) { | |
potion.buttonText = text.replace(chr, '<u>' + chr + '</u>'); | |
bindings[chrUp] = function () { | |
if (!enabled) return; | |
var $element = $('#quest_potions').find('[onclick="' + action + '"]'); | |
if ($element.length > 0 && !$element.prop('disabled')) $element.click(); | |
} | |
} | |
}); | |
potions.updateOnPage(); | |
$(document).on('keydown.candykeys', function (ev) { | |
(bindings[String.fromCharCode(ev.which)] || $.noop)(); | |
if ((ev.ctrlKey || ev.metaKey) && ev.which === "K".charCodeAt(0)) { | |
toggleKeys(); | |
} | |
}); | |
$('<button id="candykeys-toggle"/>') | |
.css({fontSize: '9px', fontFamily: 'monospace', backgroundColor: 'white', | |
position: 'absolute', top: '5px', right: '111px'}) | |
.click(function () { toggleKeys(); }) | |
.insertBefore('button.toggle'); | |
toggleKeys(true); | |
function toggleKeys(state) { | |
enabled = state || state === undefined && !enabled; | |
$('#quest_potions').find('u').css('text-decoration', enabled ? 'underline' : 'none'); | |
$('#candykeys-toggle').html(enabled ? 'Hot keys : on ' : 'Hot keys : off'); | |
} | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment