From 6ee6470972c2fc2ae8c76a41980eb16e8d618206 Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Wed, 11 Feb 2015 18:55:27 -0500 Subject: [PATCH] Merging updates --- script/engine.js | 1480 ++++++++++++------------- script/events.js | 1700 ++++++++++++++--------------- script/events/outside.js | 564 +++++----- script/events/room.js | 1206 ++++++++++---------- script/notifications.js | 156 +-- script/outside.js | 1230 ++++++++++----------- script/path.js | 634 +++++------ script/room.js | 2240 +++++++++++++++++++------------------- script/space.js | 1018 ++++++++--------- script/world.js | 1898 ++++++++++++++++---------------- 10 files changed, 6063 insertions(+), 6063 deletions(-) diff --git a/script/engine.js b/script/engine.js index ae90548..1e527ae 100644 --- a/script/engine.js +++ b/script/engine.js @@ -1,740 +1,740 @@ -(function() { - var Engine = window.Engine = { - - SITE_URL: encodeURIComponent("http://adarkroom.doublespeakgames.com"), - VERSION: 1.3, - MAX_STORE: 99999999999999, - SAVE_DISPLAY: 30 * 1000, - GAME_OVER: false, - - //object event types - topics: {}, - - Perks: { - 'boxer': { - name: _('boxer'), - desc: _('punches do more damage'), - /// TRANSLATORS : means with more force. - notify: _('learned to throw punches with purpose') - }, - 'martial artist': { - name: _('martial artist'), - desc: _('punches do even more damage.'), - notify: _('learned to fight quite effectively without weapons') - }, - 'unarmed master': { - /// TRANSLATORS : master of unarmed combat - name: _('unarmed master'), - desc: _('punch twice as fast, and with even more force'), - notify: _('learned to strike faster without weapons') - }, - 'barbarian': { - name: _('barbarian'), - desc: _('melee weapons deal more damage'), - notify: _('learned to swing weapons with force') - }, - 'slow metabolism': { - name: _('slow metabolism'), - desc: _('go twice as far without eating'), - notify: _('learned how to ignore the hunger') - }, - 'desert rat': { - name: _('desert rat'), - desc: _('go twice as far without drinking'), - notify: _('learned to love the dry air') - }, - 'evasive': { - name: _('evasive'), - desc: _('dodge attacks more effectively'), - notify: _("learned to be where they're not") - }, - 'precise': { - name: _('precise'), - desc: _('land blows more often'), - notify: _('learned to predict their movement') - }, - 'scout': { - name: _('scout'), - desc: _('see farther'), - notify: _('learned to look ahead') - }, - 'stealthy': { - name: _('stealthy'), - desc: _('better avoid conflict in the wild'), - notify: _('learned how not to be seen') - }, - 'gastronome': { - name: _('gastronome'), - desc: _('restore more health when eating'), - notify: _('learned to make the most of food') - } - }, - - options: { - state: null, - debug: false, - log: false, - dropbox: false, - doubleTime: false - }, - - init: function(options) { - this.options = $.extend( - this.options, - options - ); - this._debug = this.options.debug; - this._log = this.options.log; - - // Check for HTML5 support - if(!Engine.browserValid()) { - window.location = 'browserWarning.html'; - } - - // Check for mobile - if(Engine.isMobile()) { - window.location = 'mobileWarning.html'; - } - - Engine.disableSelection(); - - if(this.options.state != null) { - window.State = this.options.state; - } else { - Engine.loadGame(); - } - - $('
').attr('id', 'locationSlider').appendTo('#main'); - - var menu = $('
') - .addClass('menu') - .appendTo('body'); - - if(typeof langs != 'undefined'){ - var customSelect = $('') - .addClass('customSelect') - .addClass('menuBtn') - .appendTo(menu); - var selectOptions = $('') - .addClass('customSelectOptions') - .appendTo(customSelect); - var optionsList = $('
    ') - .appendTo(selectOptions); - $('
  • ') - .text("language.") - .appendTo(optionsList); - - $.each(langs, function(name,display){ - $('
  • ') - .text(display) - .attr('data-language', name) - .on("click", function() { Engine.switchLanguage(this); }) - .appendTo(optionsList); - }); - } - - $('') - .addClass('lightsOff menuBtn') - .text(_('lights off.')) - .click(Engine.turnLightsOff) - .appendTo(menu); - - $('') - .addClass('menuBtn') - .text(_('hyper.')) - .click(function(){ - Engine.options.doubleTime = !Engine.options.doubleTime; - if(Engine.options.doubleTime) - $(this).text(_('classic.')); - else - $(this).text(_('hyper.')); - }) - .appendTo(menu); - - $('') - .addClass('menuBtn') - .text(_('restart.')) - .click(Engine.confirmDelete) - .appendTo(menu); - - $('') - .addClass('menuBtn') - .text(_('share.')) - .click(Engine.share) - .appendTo(menu); - - $('') - .addClass('menuBtn') - .text(_('save.')) - .click(Engine.exportImport) - .appendTo(menu); - - if(this.options.dropbox && Engine.Dropbox) { - this.dropbox = Engine.Dropbox.init(); - - $('') - .addClass('menuBtn') - .text(_('dropbox.')) - .click(Engine.Dropbox.startDropbox) - .appendTo(menu); - } - - $('') - .addClass('menuBtn') - .text(_('app store.')) - .click(function() { window.open('https://itunes.apple.com/us/app/a-dark-room/id736683061'); }) - .appendTo(menu); - - $('') - .addClass('menuBtn') - .text(_('github.')) - .click(function() { window.open('https://github.com/Continuities/adarkroom'); }) - .appendTo(menu); - - // Register keypress handlers - $('body').off('keydown').keydown(Engine.keyDown); - $('body').off('keyup').keyup(Engine.keyUp); - - // Register swipe handlers - swipeElement = $('#outerSlider'); - swipeElement.on('swipeleft', Engine.swipeLeft); - swipeElement.on('swiperight', Engine.swipeRight); - swipeElement.on('swipeup', Engine.swipeUp); - swipeElement.on('swipedown', Engine.swipeDown); - - // subscribe to stateUpdates - $.Dispatch('stateUpdate').subscribe(Engine.handleStateUpdates); - - $SM.init(); - Notifications.init(); - Events.init(); - Room.init(); - - if(typeof $SM.get('stores.wood') != 'undefined') { - Outside.init(); - } - if($SM.get('stores.compass', true) > 0) { - Path.init(); - } - if($SM.get('features.location.spaceShip')) { - Ship.init(); - } - - Engine.saveLanguage(); - Engine.travelTo(Room); - - }, - - browserValid: function() { - return ( location.search.indexOf( 'ignorebrowser=true' ) >= 0 || ( typeof Storage != 'undefined' && !oldIE ) ); - }, - - isMobile: function() { - return ( location.search.indexOf( 'ignorebrowser=true' ) < 0 && /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test( navigator.userAgent ) ); - }, - - saveGame: function() { - if(typeof Storage != 'undefined' && localStorage) { - if(Engine._saveTimer != null) { - clearTimeout(Engine._saveTimer); - } - if(typeof Engine._lastNotify == 'undefined' || Date.now() - Engine._lastNotify > Engine.SAVE_DISPLAY){ - $('#saveNotify').css('opacity', 1).animate({opacity: 0}, 1000, 'linear'); - Engine._lastNotify = Date.now(); - } - localStorage.gameState = JSON.stringify(State); - } - }, - - loadGame: function() { - try { - var savedState = JSON.parse(localStorage.gameState); - if(savedState) { - State = savedState; - $SM.updateOldState(); - Engine.log("loaded save!"); - } - } catch(e) { - State = {}; - $SM.set('version', Engine.VERSION); - Engine.event('progress', 'new game'); - } - }, - - exportImport: function() { - Events.startEvent({ - title: _('Export / Import'), - scenes: { - start: { - text: [ - _('export or import save data, for backing up'), - _('or migrating computers') - ], - buttons: { - 'export': { - text: _('export'), - onChoose: Engine.export64 - }, - 'import': { - text: _('import'), - nextScene: {1: 'confirm'} - }, - 'cancel': { - text: _('cancel'), - nextScene: 'end' - } - } - }, - 'confirm': { - text: [ - _('are you sure?'), - _('if the code is invalid, all data will be lost.'), - _('this is irreversible.') - ], - buttons: { - 'yes': { - text: _('yes'), - nextScene: {1: 'inputImport'}, - onChoose: Engine.enableSelection - }, - 'no': { - text: _('no'), - nextScene: 'end' - } - } - }, - 'inputImport': { - text: [_('put the save code here.')], - textarea: '', - buttons: { - 'okay': { - text: _('import'), - nextScene: 'end', - onChoose: Engine.import64 - }, - 'cancel': { - text: _('cancel'), - nextScene: 'end' - } - } - } - } - }); - }, - - generateExport64: function(){ - var string64 = Base64.encode(localStorage.gameState); - string64 = string64.replace(/\s/g, ''); - string64 = string64.replace(/\./g, ''); - string64 = string64.replace(/\n/g, ''); - - return string64; - }, - - export64: function() { - Engine.saveGame(); - var string64 = Engine.generateExport64(); - Engine.enableSelection(); - Events.startEvent({ - title: _('Export'), - scenes: { - start: { - text: [_('save this.')], - textarea: string64, - readonly: true, - buttons: { - 'done': { - text: _('got it'), - nextScene: 'end', - onChoose: Engine.disableSelection - } - } - } - } - }); - Engine.autoSelect('#description textarea'); - }, - - import64: function(string64) { - Engine.disableSelection(); - string64 = string64.replace(/\s/g, ''); - string64 = string64.replace(/\./g, ''); - string64 = string64.replace(/\n/g, ''); - var decodedSave = Base64.decode(string64); - localStorage.gameState = decodedSave; - location.reload(); - }, - - event: function(cat, act) { - if(typeof ga === 'function') { - ga('send', 'event', cat, act); - } - }, - - confirmDelete: function() { - Events.startEvent({ - title: _('Restart?'), - scenes: { - start: { - text: [_('restart the game?')], - buttons: { - 'yes': { - text: _('yes'), - nextScene: 'end', - onChoose: Engine.deleteSave - }, - 'no': { - text: _('no'), - nextScene: 'end' - } - } - } - } - }); - }, - - deleteSave: function(noReload) { - if(typeof Storage != 'undefined' && localStorage) { - var prestige = Prestige.get(); - window.State = {}; - localStorage.clear(); - Prestige.set(prestige); - } - if(!noReload) { - location.reload(); - } - }, - - share: function() { - Events.startEvent({ - title: _('Share'), - scenes: { - start: { - text: [_('bring your friends.')], - buttons: { - 'facebook': { - text: _('facebook'), - nextScene: 'end', - onChoose: function() { - window.open('https://www.facebook.com/sharer/sharer.php?u=' + Engine.SITE_URL, 'sharer', 'width=626,height=436,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no'); - } - }, - 'google': { - text:_('google+'), - nextScene: 'end', - onChoose: function() { - window.open('https://plus.google.com/share?url=' + Engine.SITE_URL, 'sharer', 'width=480,height=436,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no'); - } - }, - 'twitter': { - text: _('twitter'), - nextScene: 'end', - onChoose: function() { - window.open('https://twitter.com/intent/tweet?text=A%20Dark%20Room&url=' + Engine.SITE_URL, 'sharer', 'width=660,height=260,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no'); - } - }, - 'reddit': { - text: _('reddit'), - nextScene: 'end', - onChoose: function() { - window.open('http://www.reddit.com/submit?url=' + Engine.SITE_URL, 'sharer', 'width=960,height=700,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no'); - } - }, - 'close': { - text: _('close'), - nextScene: 'end' - } - } - } - } - }, - { - width: '400px' - }); - }, - - findStylesheet: function(title) { - for(var i=0; i'); - $('.lightsOff').text(_('lights on.')); - } else if (darkCss.disabled) { - darkCss.disabled = false; - $('.lightsOff').text(_('lights on.')); - } else { - $("#darkenLights").attr("disabled", "disabled"); - darkCss.disabled = true; - $('.lightsOff').text(_('lights off.')); - } - }, - - // Gets a guid - getGuid: function() { - return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { - var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); - return v.toString(16); - }); - }, - - activeModule: null, - - travelTo: function(module) { - if(Engine.activeModule != module) { - var currentIndex = Engine.activeModule ? $('.location').index(Engine.activeModule.panel) : 1; - $('div.headerButton').removeClass('selected'); - module.tab.addClass('selected'); - - var slider = $('#locationSlider'); - var stores = $('#storesContainer'); - var panelIndex = $('.location').index(module.panel); - var diff = Math.abs(panelIndex - currentIndex); - slider.animate({left: -(panelIndex * 700) + 'px'}, 300 * diff); - - if($SM.get('stores.wood') !== undefined) { - // FIXME Why does this work if there's an animation queue...? - stores.animate({right: -(panelIndex * 700) + 'px'}, 300 * diff); - } - - Engine.activeModule = module; - - module.onArrival(diff); - - if(Engine.activeModule == Room || Engine.activeModule == Path) { - // Don't fade out the weapons if we're switching to a module - // where we're going to keep showing them anyway. - if (module != Room && module != Path) { - $('div#weapons').animate({opacity: 0}, 300); - } - } - - if(module == Room || module == Path) { - $('div#weapons').animate({opacity: 1}, 300); - } - - Notifications.printQueue(module); - - } - }, - - /* Move the stores panel beneath top_container (or to top: 0px if top_container - * either hasn't been filled in or is null) using transition_diff to sync with - * the animation in Engine.travelTo(). - */ - moveStoresView: function(top_container, transition_diff) { - var stores = $('#storesContainer'); - - // If we don't have a storesContainer yet, leave. - if(typeof(stores) === 'undefined') return; - - if(typeof(transition_diff) === 'undefined') transition_diff = 1; - - if(top_container === null) { - stores.animate({top: '0px'}, {queue: false, duration: 300 * transition_diff}); - } - else if(!top_container.length) { - stores.animate({top: '0px'}, {queue: false, duration: 300 * transition_diff}); - } - else { - stores.animate({ - top: top_container.height() + 26 + 'px' - }, - { - queue: false, - duration: 300 * transition_diff - }); - } - }, - - log: function(msg) { - if(this._log) { - console.log(msg); - } - }, - - updateSlider: function() { - var slider = $('#locationSlider'); - slider.width((slider.children().length * 700) + 'px'); - }, - - updateOuterSlider: function() { - var slider = $('#outerSlider'); - slider.width((slider.children().length * 700) + 'px'); - }, - - getIncomeMsg: function(num, delay) { - return _("{0} per {1}s", (num > 0 ? "+" : "") + num, delay); - //return (num > 0 ? "+" : "") + num + " per " + delay + "s"; - }, - - keyDown: function(e) { - e = e || window.event; - if(!Engine.keyPressed && !Engine.keyLock) { - Engine.pressed = true; - if(Engine.activeModule.keyDown) { - Engine.activeModule.keyDown(e); - } - } - return jQuery.inArray(e.keycode, [37,38,39,40]) < 0; - }, - - keyUp: function(e) { - Engine.pressed = false; - if(Engine.activeModule.keyUp) { - Engine.activeModule.keyUp(e); - } - else - { - switch(e.which) { - case 38: // Up - case 87: - Engine.log('up'); - break; - case 40: // Down - case 83: - Engine.log('down'); - break; - case 37: // Left - case 65: - if(Engine.activeModule == Ship && Path.tab) - Engine.travelTo(Path); - else if(Engine.activeModule == Path && Outside.tab) - Engine.travelTo(Outside); - else if(Engine.activeModule == Outside && Room.tab) - Engine.travelTo(Room); - Engine.log('left'); - break; - case 39: // Right - case 68: - if(Engine.activeModule == Room && Outside.tab) - Engine.travelTo(Outside); - else if(Engine.activeModule == Outside && Path.tab) - Engine.travelTo(Path); - else if(Engine.activeModule == Path && Ship.tab) - Engine.travelTo(Ship); - Engine.log('right'); - break; - } - } - - return false; - }, - - swipeLeft: function(e) { - if(Engine.activeModule.swipeLeft) { - Engine.activeModule.swipeLeft(e); - } - }, - - swipeRight: function(e) { - if(Engine.activeModule.swipeRight) { - Engine.activeModule.swipeRight(e); - } - }, - - swipeUp: function(e) { - if(Engine.activeModule.swipeUp) { - Engine.activeModule.swipeUp(e); - } - }, - - swipeDown: function(e) { - if(Engine.activeModule.swipeDown) { - Engine.activeModule.swipeDown(e); - } - }, - - disableSelection: function() { - document.onselectstart = eventNullifier; // this is for IE - document.onmousedown = eventNullifier; // this is for the rest - }, - - enableSelection: function() { - document.onselectstart = eventPassthrough; - document.onmousedown = eventPassthrough; - }, - - autoSelect: function(selector) { - $(selector).focus().select(); - }, - - handleStateUpdates: function(e){ - - }, - - switchLanguage: function(dom){ - var lang = $(dom).data("language"); - if(document.location.href.search(/[\?\&]lang=[a-z_]+/) != -1){ - document.location.href = document.location.href.replace( /([\?\&]lang=)([a-z_]+)/gi , "$1"+lang ); - }else{ - document.location.href = document.location.href + ( (document.location.href.search(/\?/) != -1 )?"&":"?") + "lang="+lang; - } - }, - - saveLanguage: function(){ - var lang = decodeURIComponent((new RegExp('[?|&]lang=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null; - if(lang && typeof Storage != 'undefined' && localStorage) { - localStorage.lang = lang; - } - }, - - setTimeout: function(callback, timeout, skipDouble){ - - if( Engine.options.doubleTime && !skipDouble ){ - Engine.log('Double time, cutting timeout in half'); - timeout /= 2; - } - - return setTimeout(callback, timeout); - - } - - }; - - function eventNullifier(e) { - return $(e.target).hasClass('menuBtn'); - } - - function eventPassthrough(e) { - return true; - } - -})(); - -//create jQuery Callbacks() to handle object events -$.Dispatch = function( id ) { - var callbacks, topic = id && Engine.topics[ id ]; - if ( !topic ) { - callbacks = jQuery.Callbacks(); - topic = { - publish: callbacks.fire, - subscribe: callbacks.add, - unsubscribe: callbacks.remove - }; - if ( id ) { - Engine.topics[ id ] = topic; - } - } - return topic; -}; - -$(function() { - Engine.init(); -}); +(function() { + var Engine = window.Engine = { + + SITE_URL: encodeURIComponent("http://adarkroom.doublespeakgames.com"), + VERSION: 1.3, + MAX_STORE: 99999999999999, + SAVE_DISPLAY: 30 * 1000, + GAME_OVER: false, + + //object event types + topics: {}, + + Perks: { + 'boxer': { + name: _('boxer'), + desc: _('punches do more damage'), + /// TRANSLATORS : means with more force. + notify: _('learned to throw punches with purpose') + }, + 'martial artist': { + name: _('martial artist'), + desc: _('punches do even more damage.'), + notify: _('learned to fight quite effectively without weapons') + }, + 'unarmed master': { + /// TRANSLATORS : master of unarmed combat + name: _('unarmed master'), + desc: _('punch twice as fast, and with even more force'), + notify: _('learned to strike faster without weapons') + }, + 'barbarian': { + name: _('barbarian'), + desc: _('melee weapons deal more damage'), + notify: _('learned to swing weapons with force') + }, + 'slow metabolism': { + name: _('slow metabolism'), + desc: _('go twice as far without eating'), + notify: _('learned how to ignore the hunger') + }, + 'desert rat': { + name: _('desert rat'), + desc: _('go twice as far without drinking'), + notify: _('learned to love the dry air') + }, + 'evasive': { + name: _('evasive'), + desc: _('dodge attacks more effectively'), + notify: _("learned to be where they're not") + }, + 'precise': { + name: _('precise'), + desc: _('land blows more often'), + notify: _('learned to predict their movement') + }, + 'scout': { + name: _('scout'), + desc: _('see farther'), + notify: _('learned to look ahead') + }, + 'stealthy': { + name: _('stealthy'), + desc: _('better avoid conflict in the wild'), + notify: _('learned how not to be seen') + }, + 'gastronome': { + name: _('gastronome'), + desc: _('restore more health when eating'), + notify: _('learned to make the most of food') + } + }, + + options: { + state: null, + debug: false, + log: false, + dropbox: false, + doubleTime: false + }, + + init: function(options) { + this.options = $.extend( + this.options, + options + ); + this._debug = this.options.debug; + this._log = this.options.log; + + // Check for HTML5 support + if(!Engine.browserValid()) { + window.location = 'browserWarning.html'; + } + + // Check for mobile + if(Engine.isMobile()) { + window.location = 'mobileWarning.html'; + } + + Engine.disableSelection(); + + if(this.options.state != null) { + window.State = this.options.state; + } else { + Engine.loadGame(); + } + + $('
    ').attr('id', 'locationSlider').appendTo('#main'); + + var menu = $('
    ') + .addClass('menu') + .appendTo('body'); + + if(typeof langs != 'undefined'){ + var customSelect = $('') + .addClass('customSelect') + .addClass('menuBtn') + .appendTo(menu); + var selectOptions = $('') + .addClass('customSelectOptions') + .appendTo(customSelect); + var optionsList = $('
      ') + .appendTo(selectOptions); + $('
    • ') + .text("language.") + .appendTo(optionsList); + + $.each(langs, function(name,display){ + $('
    • ') + .text(display) + .attr('data-language', name) + .on("click", function() { Engine.switchLanguage(this); }) + .appendTo(optionsList); + }); + } + + $('') + .addClass('lightsOff menuBtn') + .text(_('lights off.')) + .click(Engine.turnLightsOff) + .appendTo(menu); + + $('') + .addClass('menuBtn') + .text(_('hyper.')) + .click(function(){ + Engine.options.doubleTime = !Engine.options.doubleTime; + if(Engine.options.doubleTime) + $(this).text(_('classic.')); + else + $(this).text(_('hyper.')); + }) + .appendTo(menu); + + $('') + .addClass('menuBtn') + .text(_('restart.')) + .click(Engine.confirmDelete) + .appendTo(menu); + + $('') + .addClass('menuBtn') + .text(_('share.')) + .click(Engine.share) + .appendTo(menu); + + $('') + .addClass('menuBtn') + .text(_('save.')) + .click(Engine.exportImport) + .appendTo(menu); + + if(this.options.dropbox && Engine.Dropbox) { + this.dropbox = Engine.Dropbox.init(); + + $('') + .addClass('menuBtn') + .text(_('dropbox.')) + .click(Engine.Dropbox.startDropbox) + .appendTo(menu); + } + + $('') + .addClass('menuBtn') + .text(_('app store.')) + .click(function() { window.open('https://itunes.apple.com/us/app/a-dark-room/id736683061'); }) + .appendTo(menu); + + $('') + .addClass('menuBtn') + .text(_('github.')) + .click(function() { window.open('https://github.com/Continuities/adarkroom'); }) + .appendTo(menu); + + // Register keypress handlers + $('body').off('keydown').keydown(Engine.keyDown); + $('body').off('keyup').keyup(Engine.keyUp); + + // Register swipe handlers + swipeElement = $('#outerSlider'); + swipeElement.on('swipeleft', Engine.swipeLeft); + swipeElement.on('swiperight', Engine.swipeRight); + swipeElement.on('swipeup', Engine.swipeUp); + swipeElement.on('swipedown', Engine.swipeDown); + + // subscribe to stateUpdates + $.Dispatch('stateUpdate').subscribe(Engine.handleStateUpdates); + + $SM.init(); + Notifications.init(); + Events.init(); + Room.init(); + + if(typeof $SM.get('stores.wood') != 'undefined') { + Outside.init(); + } + if($SM.get('stores.compass', true) > 0) { + Path.init(); + } + if($SM.get('features.location.spaceShip')) { + Ship.init(); + } + + Engine.saveLanguage(); + Engine.travelTo(Room); + + }, + + browserValid: function() { + return ( location.search.indexOf( 'ignorebrowser=true' ) >= 0 || ( typeof Storage != 'undefined' && !oldIE ) ); + }, + + isMobile: function() { + return ( location.search.indexOf( 'ignorebrowser=true' ) < 0 && /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test( navigator.userAgent ) ); + }, + + saveGame: function() { + if(typeof Storage != 'undefined' && localStorage) { + if(Engine._saveTimer != null) { + clearTimeout(Engine._saveTimer); + } + if(typeof Engine._lastNotify == 'undefined' || Date.now() - Engine._lastNotify > Engine.SAVE_DISPLAY){ + $('#saveNotify').css('opacity', 1).animate({opacity: 0}, 1000, 'linear'); + Engine._lastNotify = Date.now(); + } + localStorage.gameState = JSON.stringify(State); + } + }, + + loadGame: function() { + try { + var savedState = JSON.parse(localStorage.gameState); + if(savedState) { + State = savedState; + $SM.updateOldState(); + Engine.log("loaded save!"); + } + } catch(e) { + State = {}; + $SM.set('version', Engine.VERSION); + Engine.event('progress', 'new game'); + } + }, + + exportImport: function() { + Events.startEvent({ + title: _('Export / Import'), + scenes: { + start: { + text: [ + _('export or import save data, for backing up'), + _('or migrating computers') + ], + buttons: { + 'export': { + text: _('export'), + onChoose: Engine.export64 + }, + 'import': { + text: _('import'), + nextScene: {1: 'confirm'} + }, + 'cancel': { + text: _('cancel'), + nextScene: 'end' + } + } + }, + 'confirm': { + text: [ + _('are you sure?'), + _('if the code is invalid, all data will be lost.'), + _('this is irreversible.') + ], + buttons: { + 'yes': { + text: _('yes'), + nextScene: {1: 'inputImport'}, + onChoose: Engine.enableSelection + }, + 'no': { + text: _('no'), + nextScene: 'end' + } + } + }, + 'inputImport': { + text: [_('put the save code here.')], + textarea: '', + buttons: { + 'okay': { + text: _('import'), + nextScene: 'end', + onChoose: Engine.import64 + }, + 'cancel': { + text: _('cancel'), + nextScene: 'end' + } + } + } + } + }); + }, + + generateExport64: function(){ + var string64 = Base64.encode(localStorage.gameState); + string64 = string64.replace(/\s/g, ''); + string64 = string64.replace(/\./g, ''); + string64 = string64.replace(/\n/g, ''); + + return string64; + }, + + export64: function() { + Engine.saveGame(); + var string64 = Engine.generateExport64(); + Engine.enableSelection(); + Events.startEvent({ + title: _('Export'), + scenes: { + start: { + text: [_('save this.')], + textarea: string64, + readonly: true, + buttons: { + 'done': { + text: _('got it'), + nextScene: 'end', + onChoose: Engine.disableSelection + } + } + } + } + }); + Engine.autoSelect('#description textarea'); + }, + + import64: function(string64) { + Engine.disableSelection(); + string64 = string64.replace(/\s/g, ''); + string64 = string64.replace(/\./g, ''); + string64 = string64.replace(/\n/g, ''); + var decodedSave = Base64.decode(string64); + localStorage.gameState = decodedSave; + location.reload(); + }, + + event: function(cat, act) { + if(typeof ga === 'function') { + ga('send', 'event', cat, act); + } + }, + + confirmDelete: function() { + Events.startEvent({ + title: _('Restart?'), + scenes: { + start: { + text: [_('restart the game?')], + buttons: { + 'yes': { + text: _('yes'), + nextScene: 'end', + onChoose: Engine.deleteSave + }, + 'no': { + text: _('no'), + nextScene: 'end' + } + } + } + } + }); + }, + + deleteSave: function(noReload) { + if(typeof Storage != 'undefined' && localStorage) { + var prestige = Prestige.get(); + window.State = {}; + localStorage.clear(); + Prestige.set(prestige); + } + if(!noReload) { + location.reload(); + } + }, + + share: function() { + Events.startEvent({ + title: _('Share'), + scenes: { + start: { + text: [_('bring your friends.')], + buttons: { + 'facebook': { + text: _('facebook'), + nextScene: 'end', + onChoose: function() { + window.open('https://www.facebook.com/sharer/sharer.php?u=' + Engine.SITE_URL, 'sharer', 'width=626,height=436,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no'); + } + }, + 'google': { + text:_('google+'), + nextScene: 'end', + onChoose: function() { + window.open('https://plus.google.com/share?url=' + Engine.SITE_URL, 'sharer', 'width=480,height=436,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no'); + } + }, + 'twitter': { + text: _('twitter'), + nextScene: 'end', + onChoose: function() { + window.open('https://twitter.com/intent/tweet?text=A%20Dark%20Room&url=' + Engine.SITE_URL, 'sharer', 'width=660,height=260,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no'); + } + }, + 'reddit': { + text: _('reddit'), + nextScene: 'end', + onChoose: function() { + window.open('http://www.reddit.com/submit?url=' + Engine.SITE_URL, 'sharer', 'width=960,height=700,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no'); + } + }, + 'close': { + text: _('close'), + nextScene: 'end' + } + } + } + } + }, + { + width: '400px' + }); + }, + + findStylesheet: function(title) { + for(var i=0; i'); + $('.lightsOff').text(_('lights on.')); + } else if (darkCss.disabled) { + darkCss.disabled = false; + $('.lightsOff').text(_('lights on.')); + } else { + $("#darkenLights").attr("disabled", "disabled"); + darkCss.disabled = true; + $('.lightsOff').text(_('lights off.')); + } + }, + + // Gets a guid + getGuid: function() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); + return v.toString(16); + }); + }, + + activeModule: null, + + travelTo: function(module) { + if(Engine.activeModule != module) { + var currentIndex = Engine.activeModule ? $('.location').index(Engine.activeModule.panel) : 1; + $('div.headerButton').removeClass('selected'); + module.tab.addClass('selected'); + + var slider = $('#locationSlider'); + var stores = $('#storesContainer'); + var panelIndex = $('.location').index(module.panel); + var diff = Math.abs(panelIndex - currentIndex); + slider.animate({left: -(panelIndex * 700) + 'px'}, 300 * diff); + + if($SM.get('stores.wood') !== undefined) { + // FIXME Why does this work if there's an animation queue...? + stores.animate({right: -(panelIndex * 700) + 'px'}, 300 * diff); + } + + Engine.activeModule = module; + + module.onArrival(diff); + + if(Engine.activeModule == Room || Engine.activeModule == Path) { + // Don't fade out the weapons if we're switching to a module + // where we're going to keep showing them anyway. + if (module != Room && module != Path) { + $('div#weapons').animate({opacity: 0}, 300); + } + } + + if(module == Room || module == Path) { + $('div#weapons').animate({opacity: 1}, 300); + } + + Notifications.printQueue(module); + + } + }, + + /* Move the stores panel beneath top_container (or to top: 0px if top_container + * either hasn't been filled in or is null) using transition_diff to sync with + * the animation in Engine.travelTo(). + */ + moveStoresView: function(top_container, transition_diff) { + var stores = $('#storesContainer'); + + // If we don't have a storesContainer yet, leave. + if(typeof(stores) === 'undefined') return; + + if(typeof(transition_diff) === 'undefined') transition_diff = 1; + + if(top_container === null) { + stores.animate({top: '0px'}, {queue: false, duration: 300 * transition_diff}); + } + else if(!top_container.length) { + stores.animate({top: '0px'}, {queue: false, duration: 300 * transition_diff}); + } + else { + stores.animate({ + top: top_container.height() + 26 + 'px' + }, + { + queue: false, + duration: 300 * transition_diff + }); + } + }, + + log: function(msg) { + if(this._log) { + console.log(msg); + } + }, + + updateSlider: function() { + var slider = $('#locationSlider'); + slider.width((slider.children().length * 700) + 'px'); + }, + + updateOuterSlider: function() { + var slider = $('#outerSlider'); + slider.width((slider.children().length * 700) + 'px'); + }, + + getIncomeMsg: function(num, delay) { + return _("{0} per {1}s", (num > 0 ? "+" : "") + num, delay); + //return (num > 0 ? "+" : "") + num + " per " + delay + "s"; + }, + + keyDown: function(e) { + e = e || window.event; + if(!Engine.keyPressed && !Engine.keyLock) { + Engine.pressed = true; + if(Engine.activeModule.keyDown) { + Engine.activeModule.keyDown(e); + } + } + return jQuery.inArray(e.keycode, [37,38,39,40]) < 0; + }, + + keyUp: function(e) { + Engine.pressed = false; + if(Engine.activeModule.keyUp) { + Engine.activeModule.keyUp(e); + } + else + { + switch(e.which) { + case 38: // Up + case 87: + Engine.log('up'); + break; + case 40: // Down + case 83: + Engine.log('down'); + break; + case 37: // Left + case 65: + if(Engine.activeModule == Ship && Path.tab) + Engine.travelTo(Path); + else if(Engine.activeModule == Path && Outside.tab) + Engine.travelTo(Outside); + else if(Engine.activeModule == Outside && Room.tab) + Engine.travelTo(Room); + Engine.log('left'); + break; + case 39: // Right + case 68: + if(Engine.activeModule == Room && Outside.tab) + Engine.travelTo(Outside); + else if(Engine.activeModule == Outside && Path.tab) + Engine.travelTo(Path); + else if(Engine.activeModule == Path && Ship.tab) + Engine.travelTo(Ship); + Engine.log('right'); + break; + } + } + + return false; + }, + + swipeLeft: function(e) { + if(Engine.activeModule.swipeLeft) { + Engine.activeModule.swipeLeft(e); + } + }, + + swipeRight: function(e) { + if(Engine.activeModule.swipeRight) { + Engine.activeModule.swipeRight(e); + } + }, + + swipeUp: function(e) { + if(Engine.activeModule.swipeUp) { + Engine.activeModule.swipeUp(e); + } + }, + + swipeDown: function(e) { + if(Engine.activeModule.swipeDown) { + Engine.activeModule.swipeDown(e); + } + }, + + disableSelection: function() { + document.onselectstart = eventNullifier; // this is for IE + document.onmousedown = eventNullifier; // this is for the rest + }, + + enableSelection: function() { + document.onselectstart = eventPassthrough; + document.onmousedown = eventPassthrough; + }, + + autoSelect: function(selector) { + $(selector).focus().select(); + }, + + handleStateUpdates: function(e){ + + }, + + switchLanguage: function(dom){ + var lang = $(dom).data("language"); + if(document.location.href.search(/[\?\&]lang=[a-z_]+/) != -1){ + document.location.href = document.location.href.replace( /([\?\&]lang=)([a-z_]+)/gi , "$1"+lang ); + }else{ + document.location.href = document.location.href + ( (document.location.href.search(/\?/) != -1 )?"&":"?") + "lang="+lang; + } + }, + + saveLanguage: function(){ + var lang = decodeURIComponent((new RegExp('[?|&]lang=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null; + if(lang && typeof Storage != 'undefined' && localStorage) { + localStorage.lang = lang; + } + }, + + setTimeout: function(callback, timeout, skipDouble){ + + if( Engine.options.doubleTime && !skipDouble ){ + Engine.log('Double time, cutting timeout in half'); + timeout /= 2; + } + + return setTimeout(callback, timeout); + + } + + }; + + function eventNullifier(e) { + return $(e.target).hasClass('menuBtn'); + } + + function eventPassthrough(e) { + return true; + } + +})(); + +//create jQuery Callbacks() to handle object events +$.Dispatch = function( id ) { + var callbacks, topic = id && Engine.topics[ id ]; + if ( !topic ) { + callbacks = jQuery.Callbacks(); + topic = { + publish: callbacks.fire, + subscribe: callbacks.add, + unsubscribe: callbacks.remove + }; + if ( id ) { + Engine.topics[ id ] = topic; + } + } + return topic; +}; + +$(function() { + Engine.init(); +}); diff --git a/script/events.js b/script/events.js index abff110..3682470 100644 --- a/script/events.js +++ b/script/events.js @@ -1,850 +1,850 @@ -/** - * Module that handles the random event system - */ -var Events = { - - _EVENT_TIME_RANGE: [3, 6], // range, in minutes - _PANEL_FADE: 200, - _FIGHT_SPEED: 100, - _EAT_COOLDOWN: 5, - _MEDS_COOLDOWN: 7, - _LEAVE_COOLDOWN: 1, - STUN_DURATION: 4000, - BLINK_INTERVAL: false, - - init: function(options) { - this.options = $.extend( - this.options, - options - ); - - // Build the Event Pool - Events.EventPool = [].concat( - Events.Global, - Events.Room, - Events.Outside - ); - - Events.eventStack = []; - - Events.scheduleNextEvent(); - - //subscribe to stateUpdates - $.Dispatch('stateUpdate').subscribe(Events.handleStateUpdates); - }, - - options: {}, // Nothing for now - - activeScene: null, - - loadScene: function(name) { - Engine.log('loading scene: ' + name); - Events.activeScene = name; - var scene = Events.activeEvent().scenes[name]; - - // Scene reward - if(scene.reward) { - $SM.addM('stores', scene.reward); - } - - // onLoad - if(scene.onLoad) { - scene.onLoad(); - } - - // Notify the scene change - if(scene.notification) { - Notifications.notify(null, scene.notification); - } - - $('#description', Events.eventPanel()).empty(); - $('#buttons', Events.eventPanel()).empty(); - if(scene.combat) { - Events.startCombat(scene); - } else { - Events.startStory(scene); - } - }, - - startCombat: function(scene) { - Engine.event('game event', 'combat'); - Events.won = false; - var desc = $('#description', Events.eventPanel()); - - $('
      ').text(scene.notification).appendTo(desc); - - // Draw the wanderer - Events.createFighterDiv('@', World.health, World.getMaxHealth()).attr('id', 'wanderer').appendTo(desc); - - // Draw the enemy - Events.createFighterDiv(scene.chara, scene.health, scene.health).attr('id', 'enemy').appendTo(desc); - - // Draw the action buttons - var btns = $('#buttons', Events.eventPanel()); - - var numWeapons = 0; - for(var k in World.Weapons) { - var weapon = World.Weapons[k]; - if(typeof Path.outfit[k] == 'number' && Path.outfit[k] > 0) { - if(typeof weapon.damage != 'number' || weapon.damage === 0) { - // Weapons that deal no damage don't count - numWeapons--; - } else if(weapon.cost){ - for(var c in weapon.cost) { - var num = weapon.cost[c]; - if(typeof Path.outfit[c] != 'number' || Path.outfit[c] < num) { - // Can't use this weapon, so don't count it - numWeapons--; - } - } - } - numWeapons++; - Events.createAttackButton(k).appendTo(btns); - } - } - if(numWeapons === 0) { - // No weapons? You can punch stuff! - Events.createAttackButton('fists').prependTo(btns); - } - - Events.createEatMeatButton().appendTo(btns); - if((Path.outfit['medicine'] || 0) !== 0) { - Events.createUseMedsButton().appendTo(btns); - } - - // Set up the enemy attack timer - Events._enemyAttackTimer = Engine.setTimeout(Events.enemyAttack, scene.attackDelay * 1000); - }, - - createEatMeatButton: function(cooldown) { - if (cooldown == null) { - cooldown = Events._EAT_COOLDOWN; - } - - var btn = new Button.Button({ - id: 'eat', - text: _('eat meat'), - cooldown: cooldown, - click: Events.eatMeat, - cost: { 'cured meat': 1 } - }); - - if(Path.outfit['cured meat'] === 0) { - Button.setDisabled(btn, true); - } - - return btn; - }, - - createUseMedsButton: function(cooldown) { - if (cooldown == null) { - cooldown = Events._MEDS_COOLDOWN; - } - - var btn = new Button.Button({ - id: 'meds', - text: _('use meds'), - cooldown: cooldown, - click: Events.useMeds, - cost: { 'medicine': 1 } - }); - - if((Path.outfit['medicine'] || 0) === 0) { - Button.setDisabled(btn, true); - } - - return btn; - }, - - createAttackButton: function(weaponName) { - var weapon = World.Weapons[weaponName]; - var cd = weapon.cooldown; - if(weapon.type == 'unarmed') { - if($SM.hasPerk('unarmed master')) { - cd /= 2; - } - } - var btn = new Button.Button({ - id: 'attack_' + weaponName.replace(' ', '-'), - text: weapon.verb, - cooldown: cd, - click: Events.useWeapon, - cost: weapon.cost - }); - if(typeof weapon.damage == 'number' && weapon.damage > 0) { - btn.addClass('weaponButton'); - } - - for(var k in weapon.cost) { - if(typeof Path.outfit[k] != 'number' || Path.outfit[k] < weapon.cost[k]) { - Button.setDisabled(btn, true); - break; - } - } - - return btn; - }, - - drawFloatText: function(text, parent) { - $('
      ').text(text).addClass('damageText').appendTo(parent).animate({ - 'bottom': '50px', - 'opacity': '0' - }, - 300, - 'linear', - function() { - $(this).remove(); - }); - }, - - eatMeat: function() { - if(Path.outfit['cured meat'] > 0) { - Path.outfit['cured meat']--; - World.updateSupplies(); - if(Path.outfit['cured meat'] === 0) { - Button.setDisabled($('#eat'), true); - } - - var hp = World.health; - hp += World.meatHeal(); - hp = hp > World.getMaxHealth() ? World.getMaxHealth() : hp; - World.setHp(hp); - - if(Events.activeEvent()) { - var w = $('#wanderer'); - w.data('hp', hp); - Events.updateFighterDiv(w); - Events.drawFloatText('+' + World.meatHeal(), '#wanderer .hp'); - } - } - }, - - useMeds: function() { - if(Path.outfit['medicine'] > 0) { - Path.outfit['medicine']--; - World.updateSupplies(); - if(Path.outfit['medicine'] === 0) { - Button.setDisabled($('#meds'), true); - } - - var hp = World.health; - hp += World.medsHeal(); - hp = hp > World.getMaxHealth() ? World.getMaxHealth() : hp; - World.setHp(hp); - - if(Events.activeEvent()) { - var w = $('#wanderer'); - w.data('hp', hp); - Events.updateFighterDiv(w); - Events.drawFloatText('+' + World.medsHeal(), '#wanderer .hp'); - } - } - }, - - useWeapon: function(btn) { - if(Events.activeEvent()) { - var weaponName = btn.attr('id').substring(7).replace('-', ' '); - var weapon = World.Weapons[weaponName]; - if(weapon.type == 'unarmed') { - if(!$SM.get('character.punches')) $SM.set('character.punches', 0); - $SM.add('character.punches', 1); - if($SM.get('character.punches') == 50 && !$SM.hasPerk('boxer')) { - $SM.addPerk('boxer'); - } else if($SM.get('character.punches') == 150 && !$SM.hasPerk('martial artist')) { - $SM.addPerk('martial artist'); - } else if($SM.get('character.punches') == 300 && !$SM.hasPerk('unarmed master')) { - $SM.addPerk('unarmed master'); - } - - } - if(weapon.cost) { - var mod = {}; - var out = false; - for(var k in weapon.cost) { - if(typeof Path.outfit[k] != 'number' || Path.outfit[k] < weapon.cost[k]) { - return; - } - mod[k] = -weapon.cost[k]; - if(Path.outfit[k] - weapon.cost[k] < weapon.cost[k]) { - out = true; - } - } - for(var k in mod) { - Path.outfit[k] += mod[k]; - } - if(out) { - Button.setDisabled(btn, true); - var validWeapons = false; - $('.weaponButton').each(function(){ - if(!Button.isDisabled($(this)) && $(this).attr('id') != 'attack_fists') { - validWeapons = true; - return false; - } - }); - if(!validWeapons) { - // enable or create the punch button - var fists = $('#attack_fists'); - if(fists.length === 0) { - Events.createAttackButton('fists').prependTo('#buttons', Events.eventPanel()); - } else { - Button.setDisabled(fists, false); - } - } - } - World.updateSupplies(); - } - var dmg = -1; - if(Math.random() <= World.getHitChance()) { - dmg = weapon.damage; - if(typeof dmg == 'number') { - if(weapon.type == 'unarmed' && $SM.hasPerk('boxer')) { - dmg *= 2; - } - if(weapon.type == 'unarmed' && $SM.hasPerk('martial artist')) { - dmg *= 3; - } - if(weapon.type == 'unarmed' && $SM.hasPerk('unarmed master')) { - dmg *= 2; - } - if(weapon.type == 'melee' && $SM.hasPerk('barbarian')) { - dmg = Math.floor(dmg * 1.5); - } - } - } - - var attackFn = weapon.type == 'ranged' ? Events.animateRanged : Events.animateMelee; - attackFn($('#wanderer'), dmg, function() { - if($('#enemy').data('hp') <= 0 && !Events.won) { - // Success! - Events.winFight(); - } - }); - } - }, - - animateMelee: function(fighter, dmg, callback) { - var start, end, enemy; - if(fighter.attr('id') == 'wanderer') { - start = {'left': '50%'}; - end = {'left': '25%'}; - enemy = $('#enemy'); - } else { - start = {'right': '50%'}; - end = {'right': '25%'}; - enemy = $('#wanderer'); - } - - fighter.stop(true, true).animate(start, Events._FIGHT_SPEED, function() { - var enemyHp = enemy.data('hp'); - var msg = ""; - if(typeof dmg == 'number') { - if(dmg < 0) { - msg = _('miss'); - dmg = 0; - } else { - msg = '-' + dmg; - enemyHp = ((enemyHp - dmg) < 0) ? 0 : (enemyHp - dmg); - enemy.data('hp', enemyHp); - if(fighter.attr('id') == 'enemy') { - World.setHp(enemyHp); - } - Events.updateFighterDiv(enemy); - } - } else { - if(dmg == 'stun') { - msg = _('stunned'); - enemy.data('stunned', true); - Engine.setTimeout(function() { - enemy.data('stunned', false); - }, Events.STUN_DURATION); - } - } - - Events.drawFloatText(msg, $('.hp', enemy)); - - $(this).animate(end, Events._FIGHT_SPEED, callback); - }); - }, - - animateRanged: function(fighter, dmg, callback) { - var start, end, enemy; - if(fighter.attr('id') == 'wanderer') { - start = {'left': '25%'}; - end = {'left': '50%'}; - enemy = $('#enemy'); - } else { - start = {'right': '25%'}; - end = {'right': '50%'}; - enemy = $('#wanderer'); - } - - $('
      ').css(start).addClass('bullet').text('o').appendTo('#description') - .animate(end, Events._FIGHT_SPEED * 2, 'linear', function() { - var enemyHp = enemy.data('hp'); - var msg = ""; - if(typeof dmg == 'number') { - if(dmg < 0) { - msg = _('miss'); - dmg = 0; - } else { - msg = '-' + dmg; - enemyHp = ((enemyHp - dmg) < 0) ? 0 : (enemyHp - dmg); - enemy.data('hp', enemyHp); - if(fighter.attr('id') == 'enemy') { - World.setHp(enemyHp); - } - Events.updateFighterDiv(enemy); - } - } else { - if(dmg == 'stun') { - msg = _('stunned'); - enemy.data('stunned', true); - Engine.setTimeout(function() { - enemy.data('stunned', false); - }, Events.STUN_DURATION); - } - } - - Events.drawFloatText(msg, $('.hp', enemy)); - - $(this).remove(); - if(typeof callback == 'function') { - callback(); - } - }); - }, - - enemyAttack: function() { - - var scene = Events.activeEvent().scenes[Events.activeScene]; - - if(!$('#enemy').data('stunned')) { - var toHit = scene.hit; - toHit *= $SM.hasPerk('evasive') ? 0.8 : 1; - var dmg = -1; - if(Math.random() <= toHit) { - dmg = scene.damage; - } - - var attackFn = scene.ranged ? Events.animateRanged : Events.animateMelee; - - attackFn($('#enemy'), dmg, function() { - if($('#wanderer').data('hp') <= 0) { - // Failure! - clearTimeout(Events._enemyAttackTimer); - Events.endEvent(); - World.die(); - } - }); - } - - Events._enemyAttackTimer = Engine.setTimeout(Events.enemyAttack, scene.attackDelay * 1000); - }, - - winFight: function() { - Events.won = true; - clearTimeout(Events._enemyAttackTimer); - $('#enemy').animate({opacity: 0}, 300, 'linear', function() { - Engine.setTimeout(function() { - try { - var scene = Events.activeEvent().scenes[Events.activeScene]; - var desc = $('#description', Events.eventPanel()); - var btns = $('#buttons', Events.eventPanel()); - desc.empty(); - btns.empty(); - $('
      ').text(scene.deathMessage).appendTo(desc); - - Events.drawLoot(scene.loot); - - if(scene.buttons) { - // Draw the buttons - Events.drawButtons(scene); - } else { - Button.cooldown(new Button.Button({ - id: 'leaveBtn', - cooldown: Events._LEAVE_COOLDOWN, - click: function() { - var scene = Events.activeEvent().scenes[Events.activeScene]; - if(scene.nextScene && scene.nextScene != 'end') { - Events.loadScene(scene.nextScene); - } else { - Events.endEvent(); - } - }, - text: _('leave') - }).appendTo(btns)); - - Events.createEatMeatButton(0).appendTo(btns); - if((Path.outfit['medicine'] || 0) !== 0) { - Events.createUseMedsButton(0).appendTo(btns); - } - } - } catch(e) { - // It is possible to die and win if the timing is perfect. Just let it fail. - } - }, 1000, true); - }); - }, - - drawLoot: function(lootList) { - var desc = $('#description', Events.eventPanel()); - var lootButtons = $('
      ').attr('id', 'lootButtons'); - for(var k in lootList) { - var loot = lootList[k]; - if(Math.random() < loot.chance) { - var num = Math.floor(Math.random() * (loot.max - loot.min)) + loot.min; - new Button.Button({ - id: 'loot_' + k.replace(' ', '-'), - text: _(k) + ' [' + num + ']', - click: Events.getLoot - }).data('numLeft', num).appendTo(lootButtons); - } - } - $('
      ').addClass('clear').appendTo(lootButtons); - if(lootButtons.children().length > 1) { - lootButtons.appendTo(desc); - } - }, - - dropStuff: function(e) { - e.stopPropagation(); - var btn = $(this); - var thing = btn.data('thing'); - var num = btn.data('num'); - var lootButtons = $('#lootButtons'); - Engine.log('dropping ' + num + ' ' + thing); - - var lootBtn = $('#loot_' + thing.replace(' ', '-'), lootButtons); - if(lootBtn.length > 0) { - var curNum = lootBtn.data('numLeft'); - curNum += num; - lootBtn.text(_(thing) + ' [' + curNum + ']').data('numLeft', curNum); - } else { - new Button.Button({ - id: 'loot_' + thing.replace(' ', '-'), - text: _(thing) + ' [' + num + ']', - click: Events.getLoot - }).data('numLeft', num).insertBefore($('.clear', lootButtons)); - } - Path.outfit[thing] -= num; - Events.getLoot(btn.closest('.button')); - World.updateSupplies(); - }, - - getLoot: function(btn) { - var name = btn.attr('id').substring(5).replace('-', ' '); - if(btn.data('numLeft') > 0) { - var weight = Path.getWeight(name); - var freeSpace = Path.getFreeSpace(); - if(weight <= freeSpace) { - var num = btn.data('numLeft'); - num--; - btn.data('numLeft', num); - if(num === 0) { - Button.setDisabled(btn); - btn.animate({'opacity':0}, 300, 'linear', function() { - $(this).remove(); - if($('#lootButtons').children().length == 1) { - $('#lootButtons').remove(); - } - }); - } else { - // #dropMenu gets removed by this. - btn.text(_(name) + ' [' + num + ']'); - } - var curNum = Path.outfit[name]; - curNum = typeof curNum == 'number' ? curNum : 0; - curNum++; - Path.outfit[name] = curNum; - World.updateSupplies(); - - // Update weight and free space variables so we can decide - // whether or not to bring up/update the drop menu. - weight = Path.getWeight(name); - freeSpace = Path.getFreeSpace(); - } - - if(weight > freeSpace && btn.data('numLeft') > 0) { - // Draw the drop menu - Engine.log('drop menu'); - $('#dropMenu').remove(); - var dropMenu = $('
      ').attr('id', 'dropMenu'); - for(var k in Path.outfit) { - var itemWeight = Path.getWeight(k); - if(itemWeight > 0) { - var numToDrop = Math.ceil((weight - freeSpace) / itemWeight); - if(numToDrop > Path.outfit[k]) { - numToDrop = Path.outfit[k]; - } - if(numToDrop > 0) { - var dropRow = $('
      ').attr('id', 'drop_' + k.replace(' ', '-')) - .text(_(k) + ' x' + numToDrop) - .data('thing', k) - .data('num', numToDrop) - .click(Events.dropStuff); - dropRow.appendTo(dropMenu); - } - } - } - dropMenu.appendTo(btn); - btn.one("mouseleave", function() { - $('#dropMenu').remove(); - }); - } - } - }, - - createFighterDiv: function(chara, hp, maxhp) { - var fighter = $('
      ').addClass('fighter').text(_(chara)).data('hp', hp).data('maxHp', maxhp).data('refname',chara); - $('
      ').addClass('hp').text(hp+'/'+maxhp).appendTo(fighter); - return fighter; - }, - - updateFighterDiv: function(fighter) { - $('.hp', fighter).text(fighter.data('hp') + '/' + fighter.data('maxHp')); - }, - - startStory: function(scene) { - // Write the text - var desc = $('#description', Events.eventPanel()); - for(var i in scene.text) { - $('
      ').text(scene.text[i]).appendTo(desc); - } - - if(scene.textarea != null) { - var ta = $('