Raspsnir/public_html/browserquest/js/app.js

1 line
8.5 KiB
JavaScript

define(["jquery","storage"],function(e,t){var n=Class.extend({init:function(){this.currentPage=1,this.blinkInterval=null,this.previousState=null,this.isParchmentReady=!0,this.ready=!1,this.storage=new t,this.watchNameInputInterval=setInterval(this.toggleButton.bind(this),100),this.$playButton=e(".play"),this.$playDiv=e(".play div")},setGame:function(e){this.game=e,this.isMobile=this.game.renderer.mobile,this.isTablet=this.game.renderer.tablet,this.isDesktop=!this.isMobile&&!this.isTablet,this.supportsWorkers=!!window.Worker,this.ready=!0},center:function(){window.scrollTo(0,1)},canStartGame:function(){return this.isDesktop?this.game&&this.game.map&&this.game.map.isLoaded:this.game},tryStartingGame:function(e,t){var n=this,r=this.$playButton;if(e!=="")if(!this.ready||!this.canStartGame()){this.isMobile||r.addClass("loading"),this.$playDiv.unbind("click");var i=setInterval(function(){log.debug("waiting..."),n.canStartGame()&&(setTimeout(function(){n.isMobile||r.removeClass("loading")},1500),clearInterval(i),n.startGame(e,t))},100)}else this.$playDiv.unbind("click"),this.startGame(e,t)},startGame:function(e,t){var n=this;t&&t(),this.hideIntro(function(){n.isDesktop||n.game.loadMap(),n.start(e)})},start:function(t){var n=this,r=!n.storage.hasAlreadyPlayed();if(t&&!this.game.started){var i=!1,s=this.config;i||(log.debug("Starting game with build config."),this.game.setServerOptions(s.build.host,s.build.port,t)),this.center(),this.game.run(function(){e("body").addClass("started"),r&&n.toggleInstructions()})}},setMouseCoordinates:function(t){var n=e("#container").offset(),r=this.game.renderer.getScaleFactor(),i=this.game.renderer.getWidth(),s=this.game.renderer.getHeight(),o=this.game.mouse;o.x=t.pageX-n.left-(this.isMobile?0:5*r),o.y=t.pageY-n.top-(this.isMobile?0:7*r),o.x<=0?o.x=0:o.x>=i&&(o.x=i-1),o.y<=0?o.y=0:o.y>=s&&(o.y=s-1)},initHealthBar:function(){var t=this.game.renderer.getScaleFactor(),n=e("#healthbar").width()-12*t;this.game.onPlayerHealthChange(function(t,r){var i=Math.round(n/r*(t>0?t:0));e("#hitpoints").css("width",i+"px")}),this.game.onPlayerHurt(this.blinkHealthBar.bind(this))},blinkHealthBar:function(){var t=e("#hitpoints");t.addClass("white"),setTimeout(function(){t.removeClass("white")},500)},toggleButton:function(){var t=e("#parchment input").val(),n=e("#createcharacter .play");t&&t.length>0?(n.removeClass("disabled"),e("#character").removeClass("disabled")):(n.addClass("disabled"),e("#character").addClass("disabled"))},hideIntro:function(t){clearInterval(this.watchNameInputInterval),e("body").removeClass("intro"),setTimeout(function(){e("body").addClass("game"),t()},1e3)},showChat:function(){this.game.started&&(e("#chatbox").addClass("active"),e("#chatinput").focus(),e("#chatbutton").addClass("active"))},hideChat:function(){this.game.started&&(e("#chatbox").removeClass("active"),e("#chatinput").blur(),e("#chatbutton").removeClass("active"))},toggleInstructions:function(){e("#achievements").hasClass("active")&&(this.toggleAchievements(),e("#achievementsbutton").removeClass("active")),e("#instructions").toggleClass("active")},toggleAchievements:function(){e("#instructions").hasClass("active")&&(this.toggleInstructions(),e("#helpbutton").removeClass("active")),this.resetPage(),e("#achievements").toggleClass("active")},resetPage:function(){var t=this,n=e("#achievements");n.hasClass("active")&&n.bind(TRANSITIONEND,function(){n.removeClass("page"+t.currentPage).addClass("page1"),t.currentPage=1,n.unbind(TRANSITIONEND)})},initEquipmentIcons:function(){var t=this.game.renderer.getScaleFactor(),n=function(e){return"img/"+t+"/item-"+e+".png"},r=this.game.player.getWeaponName(),i=this.game.player.getSpriteName(),s=n(r),o=n(i);e("#weapon").css("background-image",'url("'+s+'")'),i!=="firefox"&&e("#armor").css("background-image",'url("'+o+'")')},hideWindows:function(){e("#achievements").hasClass("active")&&(this.toggleAchievements(),e("#achievementsbutton").removeClass("active")),e("#instructions").hasClass("active")&&(this.toggleInstructions(),e("#helpbutton").removeClass("active")),e("body").hasClass("credits")&&this.closeInGameCredits(),e("body").hasClass("about")&&this.closeInGameAbout()},showAchievementNotification:function(t,n){var r=e("#achievement-notification"),i=r.find(".name"),s=e("#achievementsbutton");r.removeClass().addClass("active achievement"+t),i.text(n),this.game.storage.getAchievementCount()===1&&(this.blinkInterval=setInterval(function(){s.toggleClass("blink")},500)),setTimeout(function(){r.removeClass("active"),s.removeClass("blink")},5e3)},displayUnlockedAchievement:function(t){var n=e("#achievements li.achievement"+t),r=this.game.getAchievementById(t);r&&r.hidden&&this.setAchievementData(n,r.name,r.desc),n.addClass("unlocked")},unlockAchievement:function(t,n){this.showAchievementNotification(t,n),this.displayUnlockedAchievement(t);var r=parseInt(e("#unlocked-achievements").text());e("#unlocked-achievements").text(r+1)},initAchievementList:function(t){var n=this,r=e("#lists"),i=e("#page-tmpl"),s=e("#achievement-tmpl"),o=0,u=0,a=null;_.each(t,function(t){u++;var f=s.clone();f.removeAttr("id"),f.addClass("achievement"+u),t.hidden||n.setAchievementData(f,t.name,t.desc),f.find(".twitter").attr("href","http://twitter.com/share?url=http%3A%2F%2Fbrowserquest.mozilla.org&text=I%20unlocked%20the%20%27"+t.name+"%27%20achievement%20on%20Mozilla%27s%20%23BrowserQuest%21&related=glecollinet:Creators%20of%20BrowserQuest%2Cwhatthefranck"),f.show(),f.find("a").click(function(){var t=e(this).attr("href");return n.openPopup("twitter",t),!1}),(u-1)%4===0&&(o++,a=i.clone(),a.attr("id","page"+o),a.show(),r.append(a)),a.append(f)}),e("#total-achievements").text(e("#achievements").find("li").length)},initUnlockedAchievements:function(t){var n=this;_.each(t,function(e){n.displayUnlockedAchievement(e)}),e("#unlocked-achievements").text(t.length)},setAchievementData:function(e,t,n){e.find(".achievement-name").html(t),e.find(".achievement-description").html(n)},toggleCredits:function(){var t=e("#parchment").attr("class");this.game.started?(e("#parchment").removeClass().addClass("credits"),e("body").toggleClass("credits"),this.game.player||e("body").toggleClass("death"),e("body").hasClass("about")&&(this.closeInGameAbout(),e("#helpbutton").removeClass("active"))):t!=="animate"&&(t==="credits"?this.animateParchment(t,this.previousState):(this.animateParchment(t,"credits"),this.previousState=t))},toggleAbout:function(){var t=e("#parchment").attr("class");this.game.started?(e("#parchment").removeClass().addClass("about"),e("body").toggleClass("about"),this.game.player||e("body").toggleClass("death"),e("body").hasClass("credits")&&this.closeInGameCredits()):t!=="animate"&&(t==="about"?localStorage&&localStorage.data?this.animateParchment(t,"loadcharacter"):this.animateParchment(t,"createcharacter"):(this.animateParchment(t,"about"),this.previousState=t))},closeInGameCredits:function(){e("body").removeClass("credits"),e("#parchment").removeClass("credits"),this.game.player||e("body").addClass("death")},closeInGameAbout:function(){e("body").removeClass("about"),e("#parchment").removeClass("about"),this.game.player||e("body").addClass("death"),e("#helpbutton").removeClass("active")},togglePopulationInfo:function(){e("#population").toggleClass("visible")},openPopup:function(t,n){var r=e(window).height(),i=e(window).width(),s,o,u,a;switch(t){case"twitter":s=450,o=550;break;case"facebook":s=400,o=580}u=r/2-s/2,a=i/2-o/2,newwindow=window.open(n,"name","height="+s+",width="+o+",top="+u+",left="+a),window.focus&&newwindow.focus()},animateParchment:function(t,n){var r=this,i=e("#parchment"),s=1;this.isMobile?i.removeClass(t).addClass(n):this.isParchmentReady&&(this.isTablet&&(s=0),this.isParchmentReady=!this.isParchmentReady,i.toggleClass("animate"),i.removeClass(t),setTimeout(function(){e("#parchment").toggleClass("animate"),i.addClass(n)},s*1e3),setTimeout(function(){r.isParchmentReady=!r.isParchmentReady},s*1e3))},animateMessages:function(){var t=e("#notifications div");t.addClass("top")},resetMessagesPosition:function(){var t=e("#message2").text();e("#notifications div").removeClass("top"),e("#message2").text(""),e("#message1").text(t)},showMessage:function(t){var n=e("#notifications div"),r=e("#notifications #message2");this.animateMessages(),r.text(t),this.messageTimer&&this.resetMessageTimer(),this.messageTimer=setTimeout(function(){n.addClass("top")},5e3)},resetMessageTimer:function(){clearTimeout(this.messageTimer)},resizeUi:function(){if(this.game)if(this.game.started)this.game.resize(),this.initHealthBar(),this.game.updateBars();else{var e=this.game.renderer.getScaleFactor();this.game.renderer.rescale(e)}}});return n});