neingeist
/
cosmos
Archived
1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

5 lines
57 KiB
JavaScript

/*! Quintus - v0.1.5 - 2013-08-04
* http://html5quintus.com
* Copyright (c) 2013 Pascal Rettig; Licensed MIT, GPLv2 */
var Quintus=function a(b){var c=function(a,b,d){return c.select(a,b,d)};c.select=function(){},c.include=function(b){return c._each(c._normalizeArg(b),function(b){var d=a[b]||b;if(!c._isFunction(d))throw"Invalid Module:"+b;d(c)}),c},c._normalizeArg=function(a){return c._isString(a)&&(a=a.replace(/\s+/g,"").split(",")),c._isArray(a)||(a=[a]),a},c._extend=function(a,b){if(!b)return a;for(var c in b)a[c]=b[c];return a},c._clone=function(a){return c._extend({},a)},c._defaults=function(a,b){if(!b)return a;for(var c in b)void 0===a[c]&&(a[c]=b[c]);return a},c._has=function(a,b){return Object.prototype.hasOwnProperty.call(a,b)},c._isString=function(a){return"string"==typeof a},c._isNumber=function(a){return"[object Number]"===Object.prototype.toString.call(a)},c._isFunction=function(a){return"[object Function]"===Object.prototype.toString.call(a)},c._isObject=function(a){return"[object Object]"===Object.prototype.toString.call(a)},c._isArray=function(a){return"[object Array]"===Object.prototype.toString.call(a)},c._isUndefined=function(a){return void 0===a},c._popProperty=function(a,b){var c=a[b];return delete a[b],c},c._each=function(a,b,c){if(null!=a)if(a.forEach)a.forEach(b,c);else if(a.length===+a.length)for(var d=0,e=a.length;e>d;d++)b.call(c,a[d],d,a);else for(var f in a)b.call(c,a[f],f,a)},c._invoke=function(a,b,c,d){if(null!=a)for(var e=0,f=a.length;f>e;e++)a[e][b](c,d)},c._detect=function(a,b,c,d,e){var f;if(null!=a){if(a.length===+a.length){for(var g=0,h=a.length;h>g;g++)if(f=b.call(c,a[g],g,d,e))return f;return!1}for(var i in a)if(f=b.call(c,a[i],i,d,e))return f;return!1}},c._map=function(a,b,d){var e=[];return null==a?e:a.map?a.map(b,d):(c._each(a,function(a,c,f){e[e.length]=b.call(d,a,c,f)}),a.length===+a.length&&(e.length=a.length),e)},c._uniq=function(a){a=a.slice().sort();for(var b=[],c=null,d=0;d<a.length;d++)void 0!==a[d]&&c!==a[d]&&b.push(a[d]),c=a[d];return b},c._shuffle=function(a){var b,d=[];return c._each(a,function(a,c){b=Math.floor(Math.random()*(c+1)),d[c]=d[b],d[b]=a}),d},c._keys=Object.keys||function(a){if(c._isObject(a))throw new TypeError("Invalid object");var b=[];for(var d in a)c._has(a,d)&&(b[b.length]=d);return b},c._range=function(a,b,c){c=c||1;for(var d=Math.max(Math.ceil((b-a)/c),0),e=0,f=new Array(d);d>e;)f[e++]=a,a+=c;return f};var d=0;return c._uniqueId=function(){return d++},c.options={imagePath:"images/",audioPath:"audio/",dataPath:"data/",audioSupported:["mp3","ogg"],sound:!0,frameTimeLimit:100},b&&c._extend(c.options,b),c.gameLoop=function(a){return c.lastGameLoopFrame=(new Date).getTime(),c.loop=!0,c._loopFrame=0,c.gameLoopCallbackWrapper=function(){var b=(new Date).getTime();c._loopFrame++,c.loop=window.requestAnimationFrame(c.gameLoopCallbackWrapper);var d=b-c.lastGameLoopFrame;d>c.options.frameTimeLimit&&(d=c.options.frameTimeLimit),a.apply(c,[d/1e3]),c.lastGameLoopFrame=b},window.requestAnimationFrame(c.gameLoopCallbackWrapper),c},c.pauseGame=function(){c.loop&&window.cancelAnimationFrame(c.loop),c.loop=null},c.unpauseGame=function(){c.loop||(c.lastGameLoopFrame=(new Date).getTime(),c.loop=window.requestAnimationFrame(c.gameLoopCallbackWrapper))},function(){var a=!1,b=/xyz/.test(function(){})?/\b_super\b/:/.*/;c.Class=function(){},c.Class.prototype.isA=function(a){return this.className===a},c.Class.extend=function(d,e,f){function g(a,b){return function(){var c=this._super;this._super=i[a];var d=b.apply(this,arguments);return this._super=c,d}}function h(){!a&&this.init&&this.init.apply(this,arguments)}c._isString(d)||(f=e,e=d,d=null);var i=this.prototype,j=this;a=!0;var k=new j;a=!1;for(var l in e)k[l]="function"==typeof e[l]&&"function"==typeof i[l]&&b.test(e[l])?g(l,e[l]):e[l];return h.prototype=k,h.prototype.constructor=h,h.extend=c.Class.extend,f&&c._extend(h,f),d&&(c[d]=h,h.prototype.className=d,h.className=d),h}}(),c.Class.extend("Evented",{on:function(a,b,d){if(c._isArray(a)||-1!==a.indexOf(",")){a=c._normalizeArg(a);for(var e=0;e<a.length;e++)this.on(a[e],b,d)}else d||(d=b,b=null),d||(d=a),c._isString(d)&&(d=(b||this)[d]),this.listeners=this.listeners||{},this.listeners[a]=this.listeners[a]||[],this.listeners[a].push([b||this,d]),b&&(b.binds||(b.binds=[]),b.binds.push([this,a,d]))},trigger:function(a,b){if(this.listeners&&this.listeners[a])for(var c=0,d=this.listeners[a].length;d>c;c++){var e=this.listeners[a][c];e[1].call(e[0],b)}},off:function(a,b,d){if(b){c._isString(d)&&b[d]&&(d=b[d]);var e=this.listeners&&this.listeners[a];if(e)for(var f=e.length-1;f>=0;f--)e[f][0]===b&&(d&&d!==e[f][1]||this.listeners[a].splice(f,1))}else this.listeners[a]&&delete this.listeners[a]},debind:function(){if(this.binds)for(var a=0,b=this.binds.length;b>a;a++){var c=this.binds[a],d=c[0],e=c[1];d.off(e,this)}}}),c.components={},c.Evented.extend("Component",{init:function(a){this.entity=a,this.extend&&c._extend(a,this.extend),a[this.name]=this,a.activeComponents.push(this.componentName),a.stage&&a.stage.addToList&&a.stage.addToList(this.componentName,a),this.added&&this.added()},destroy:function(){if(this.extend)for(var a=c._keys(this.extend),b=0,d=a.length;d>b;b++)delete this.entity[a[b]];delete this.entity[this.name];var e=this.entity.activeComponents.indexOf(this.componentName);-1!==e&&(this.entity.activeComponents.splice(e,1),this.entity.stage&&this.entity.stage.addToList&&this.entity.stage.addToLists(this.componentName,this.entity)),this.debind(),this.destroyed&&this.destroyed()}}),c.Evented.extend("GameObject",{has:function(a){return this[a]?!0:!1},add:function(a){a=c._normalizeArg(a),this.activeComponents||(this.activeComponents=[]);for(var b=0,d=a.length;d>b;b++){var e=a[b],f=c.components[e];if(!this.has(e)&&f){var g=new f(this);this.trigger("addComponent",g)}}return this},del:function(a){a=c._normalizeArg(a);for(var b=0,d=a.length;d>b;b++){var e=a[b];e&&this.has(e)&&(this.trigger("delComponent",this[e]),this[e].destroy())}return this},destroy:function(){this.isDestroyed||(this.trigger("destroyed"),this.debind(),this.stage&&this.stage.remove&&this.stage.remove(this),this.isDestroyed=!0,this.destroyed&&this.destroyed())}}),c.component=function(a,b){return b?(b.name=a,b.componentName="."+a,c.components[a]=c.Component.extend(a+"Component",b)):c.components[a]},c.GameObject.extend("GameState",{init:function(a){this.p=c._extend({},a),this.listeners={}},reset:function(a){this.init(a),this.trigger("reset")},_triggerProperty:function(a,b){this.p[b]!==a&&(this.p[b]=a,this.trigger("change."+b,a))},set:function(a,b){c._isObject(a)?c._each(a,this._triggerProperty,this):this._triggerProperty(b,a),this.trigger("change")},inc:function(a,b){this.set(a,this.get(a)+b)},dec:function(a,b){this.set(a,this.get(a)-b)},get:function(a){return this.p[a]}}),c.state=new c.GameState,c.reset=function(){c.state.reset()},c.touchDevice="ontouchstart"in document,c.setup=function(a,b){c._isObject(a)&&(b=a,a=null),b=b||{},a=a||"quintus",c.el=c._isString(a)?document.getElementById(a):a,c.el||(c.el=document.createElement("canvas"),c.el.width=b.width||320,c.el.height=b.height||420,c.el.id=a,document.body.appendChild(c.el));var d=parseInt(c.el.width,10),e=parseInt(c.el.height,10),f=b.maxWidth||5e3,g=b.maxHeight||5e3,h=b.resampleWidth,i=b.resampleHeight,j=b.upsampleWidth,k=b.upsampleHeight;b.maximize===!0||c.touchDevice&&"touch"===b.maximize?(document.body.style.padding=0,document.body.style.margin=0,d=Math.min(window.innerWidth,f),e=Math.min(window.innerHeight-5,g),c.touchDevice&&(c.el.style.height=2*e+"px",window.scrollTo(0,1),d=Math.min(window.innerWidth,f),e=Math.min(window.innerHeight,g))):c.touchDevice&&window.scrollTo(0,1),j&&j>=d||k&&k>=e?(c.el.style.height=e+"px",c.el.style.width=d+"px",c.el.width=2*d,c.el.height=2*e):(h&&d>h||i&&e>i)&&c.touchDevice?(c.el.style.height=e+"px",c.el.style.width=d+"px",c.el.width=d/2,c.el.height=e/2):(c.el.style.height=e+"px",c.el.style.width=d+"px",c.el.width=d,c.el.height=e);var l=c.el.parentNode;return l&&(c.wrapper=document.createElement("div"),c.wrapper.id=a+"_container",c.wrapper.style.width=d+"px",c.wrapper.style.margin="0 auto",c.wrapper.style.position="relative",l.insertBefore(c.wrapper,c.el),c.wrapper.appendChild(c.el)),c.el.style.position="relative",c.ctx=c.el.getContext&&c.el.getContext("2d"),c.width=parseInt(c.el.width,10),c.height=parseInt(c.el.height,10),c.cssWidth=d,c.cssHeight=e,window.addEventListener("orientationchange",function(){setTimeout(function(){window.scrollTo(0,1)},0)}),c},c.clear=function(){c.clearColor?(c.ctx.globalAlpha=1,c.ctx.fillStyle=c.clearColor,c.ctx.fillRect(0,0,c.width,c.height)):c.ctx.clearRect(0,0,c.width,c.height)},c.imageData=function(a){var b=document.createElement("canvas");b.width=a.width,b.height=a.height;var c=b.getContext("2d");return c.drawImage(a,0,0),c.getImageData(0,0,a.width,a.height)},c.assetTypes={png:"Image",jpg:"Image",gif:"Image",jpeg:"Image",ogg:"Audio",wav:"Audio",m4a:"Audio",mp3:"Audio"},c.assetType=function(a){var b=a.split("."),d=b[b.length-1].toLowerCase(),e=c.assetTypes[d];return"Audio"===e&&c.audio&&"WebAudio"===c.audio.type&&(e="WebAudio"),e||"Other"},c.assetUrl=function(a,b){var d="";return c.options.development&&(d=(/\?/.test(b)?"&":"?")+"_t="+(new Date).getTime()),/^https?:\/\//.test(b)||"/"===b[0]?b+d:a+b+d},c.loadAssetImage=function(a,b,d,e){var f=new Image;f.onload=function(){d(a,f)},f.onerror=e,f.src=c.assetUrl(c.options.imagePath,b)},c.audioMimeTypes={mp3:"audio/mpeg",ogg:'audio/ogg; codecs="vorbis"',m4a:"audio/m4a",wav:"audio/wav"},c._audioAssetExtension=function(){if(c._audioAssetPreferredExtension)return c._audioAssetPreferredExtension;var a=new Audio;return c._audioAssetPreferredExtension=c._detect(c.options.audioSupported,function(b){return a.canPlayType(c.audioMimeTypes[b])?b:null})},c.loadAssetAudio=function(a,b,d,e){if(!document.createElement("audio").play||!c.options.sound)return d(a,null),void 0;var f=c._removeExtension(b),g=c._audioAssetExtension(),h=new Audio;return g?(h.addEventListener("error",e),c.touchDevice||h.addEventListener("canplaythrough",function(){d(a,h)}),h.src=c.assetUrl(c.options.audioPath,f+"."+g),h.load(),c.touchDevice&&d(a,h),void 0):(d(a,null),void 0)},c.loadAssetWebAudio=function(a,b,d,e){var f=new XMLHttpRequest,g=c._removeExtension(b),h=c._audioAssetExtension();f.open("GET",c.assetUrl(c.options.audioPath,g+"."+h),!0),f.responseType="arraybuffer",f.onload=function(){f.response,c.audioContext.decodeAudioData(f.response,function(b){d(a,b)},e)},f.send()},c.loadAssetOther=function(a,b,d,e){var f=new XMLHttpRequest,g=b.split("."),h=g[g.length-1].toLowerCase();f.onreadystatechange=function(){4===f.readyState&&(200===f.status?"json"===h?d(a,JSON.parse(f.responseText)):d(a,f.responseText):e())},f.open("GET",c.assetUrl(c.options.dataPath,b),!0),f.send(null)},c._removeExtension=function(a){return a.replace(/\.(\w{3,4})$/,"")},c.assets={},c.asset=function(a){return c.assets[a]},c.load=function(a,b,d){var e={};d||(d={});var f=d.progressCallback,g=!1,h=function(a){g=!0,(d.errorCallback||function(a){throw"Error Loading: "+a})(a)};c._isString(a)&&(a=c._normalizeArg(a)),c._isArray(a)?c._each(a,function(a){c._isObject(a)?c._extend(e,a):e[a]=a}):e=a;var i=c._keys(e).length,j=i,k=function(a,d,e){g||((!c.assets[a]||e)&&(c.assets[a]=d,j--,f&&f(i-j,i)),0===j&&b&&b.apply(c))};c._each(e,function(a,b){var d=c.assetType(a);c.assets[b]?k(b,c.assets[b],!0):c["loadAsset"+d](b,a,k,function(){h(a)})})},c.preloads=[],c.preload=function(a,b){c._isFunction(a)?(c.load(c._uniq(c.preloads),a,b),c.preloads=[]):c.preloads=c.preloads.concat(a)},c.matrices2d=[],c.matrix2d=function(){return c.matrices2d.length>0?c.matrices2d.pop().identity():new c.Matrix2D},c.Matrix2D=c.Class.extend({init:function(a){a?(this.m=[],this.clone(a)):this.m=[1,0,0,0,1,0]},identity:function(){var a=this.m;return a[0]=1,a[1]=0,a[2]=0,a[3]=0,a[4]=1,a[5]=0,this},clone:function(a){var b=this.m,c=a.m;return b[0]=c[0],b[1]=c[1],b[2]=c[2],b[3]=c[3],b[4]=c[4],b[5]=c[5],this},multiply:function(a){var b=this.m,c=a.m,d=b[0]*c[0]+b[1]*c[3],e=b[0]*c[1]+b[1]*c[4],f=b[0]*c[2]+b[1]*c[5]+b[2],g=b[3]*c[0]+b[4]*c[3],h=b[3]*c[1]+b[4]*c[4],i=b[3]*c[2]+b[4]*c[5]+b[5];return b[0]=d,b[1]=e,b[2]=f,b[3]=g,b[4]=h,b[5]=i,this},rotate:function(a){if(0===a)return this;var b=Math.cos(a),c=Math.sin(a),d=this.m,e=d[0]*b+d[1]*c,f=d[0]*-c+d[1]*b,g=d[3]*b+d[4]*c,h=d[3]*-c+d[4]*b;return d[0]=e,d[1]=f,d[3]=g,d[4]=h,this},rotateDeg:function(a){return 0===a?this:this.rotate(Math.PI*a/180)},scale:function(a,b){var c=this.m;return void 0===b&&(b=a),c[0]*=a,c[1]*=b,c[3]*=a,c[4]*=b,this},translate:function(a,b){var c=this.m;return c[2]+=c[0]*a+c[1]*b,c[5]+=c[3]*a+c[4]*b,this},transform:function(a,b){return[a*this.m[0]+b*this.m[1]+this.m[2],a*this.m[3]+b*this.m[4]+this.m[5]]},transformPt:function(a){var b=a.x,c=a.y;return a.x=b*this.m[0]+c*this.m[1]+this.m[2],a.y=b*this.m[3]+c*this.m[4]+this.m[5],a},transformArr:function(a,b){var c=a[0],d=a[1];return b[0]=c*this.m[0]+d*this.m[1]+this.m[2],b[1]=c*this.m[3]+d*this.m[4]+this.m[5],b},transformX:function(a,b){return a*this.m[0]+b*this.m[1]+this.m[2]},transformY:function(a,b){return a*this.m[3]+b*this.m[4]+this.m[5]},release:function(){return c.matrices2d.push(this),null},setContextTransform:function(a){var b=this.m;a.transform(b[0],b[3],b[1],b[4],b[2],b[5])}}),c};!function(){for(var a=0,b=["ms","moz","webkit","o"],c=0;c<b.length&&!window.requestAnimationFrame;++c)window.requestAnimationFrame=window[b[c]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[b[c]+"CancelAnimationFrame"]||window[b[c]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||(window.requestAnimationFrame=function(b){var c=(new Date).getTime(),d=Math.max(0,16-(c-a)),e=window.setTimeout(function(){b(c+d)},d);return a=c+d,e}),window.cancelAnimationFrame||(window.cancelAnimationFrame=function(a){clearTimeout(a)})}(),Quintus["2D"]=function(a){a.component("viewport",{added:function(){this.entity.on("prerender",this,"prerender"),this.entity.on("render",this,"postrender"),this.x=0,this.y=0,this.offsetX=0,this.offsetY=0,this.centerX=a.width/2,this.centerY=a.height/2,this.scale=1},extend:{follow:function(a,b){this.off("poststep",this.viewport,"follow"),this.viewport.directions=b||{x:!0,y:!0},this.viewport.following=a,this.on("poststep",this.viewport,"follow"),this.viewport.follow(!0)},unfollow:function(){this.off("poststep",this.viewport,"follow")},centerOn:function(a,b){this.viewport.centerOn(a,b)},moveTo:function(a,b){return this.viewport.moveTo(a,b)}},follow:function(b){var c=a._isFunction(this.directions.x)?this.directions.x(this.following):this.directions.x,d=a._isFunction(this.directions.y)?this.directions.y(this.following):this.directions.y;this[b===!0?"centerOn":"softCenterOn"](c?this.following.p.x+this.following.p.w/2-this.offsetX:void 0,d?this.following.p.y+this.following.p.h/2-this.offsetY:void 0)},offset:function(a,b){this.offsetX=a,this.offsetY=b},softCenterOn:function(b,c){void 0!==b&&(this.x+=(b-a.width/2/this.scale-this.x)/3),void 0!==c&&(this.y+=(c-a.height/2/this.scale-this.y)/3)},centerOn:function(b,c){void 0!==b&&(this.x=b-a.width/2/this.scale),void 0!==c&&(this.y=c-a.height/2/this.scale)},moveTo:function(a,b){return void 0!==a&&(this.x=a),void 0!==b&&(this.y=b),this.entity},prerender:function(){this.centerX=this.x+a.width/2/this.scale,this.centerY=this.y+a.height/2/this.scale,a.ctx.save(),a.ctx.translate(Math.floor(a.width/2),Math.floor(a.height/2)),a.ctx.scale(this.scale,this.scale),a.ctx.translate(-Math.floor(this.centerX),-Math.floor(this.centerY))},postrender:function(){a.ctx.restore()}}),a.TileLayer=a.Sprite.extend({init:function(a){this._super(a,{tileW:32,tileH:32,blockTileW:10,blockTileH:10,type:1,layerIndex:0}),this.p.dataAsset&&this.load(this.p.dataAsset),this.blocks=[],this.p.blockW=this.p.tileW*this.p.blockTileW,this.p.blockH=this.p.tileH*this.p.blockTileH,this.colBounds={},this.directions=["top","left","right","bottom"],this.collisionObject={p:{w:this.p.tileW,h:this.p.tileH,cx:this.p.tileW/2,cy:this.p.tileH/2}},this.collisionNormal={separate:[]}},load:function(b){var c=b.split("."),d=c[c.length-1].toLowerCase();if("json"==d)var e=a._isString(b)?a.asset(b):b;else{if("tmx"!=d&&"xml"!=d)throw"file type not supported";for(var f=new DOMParser,g=f.parseFromString(a.asset(b),"application/xml"),h=g.getElementsByTagName("layer")[this.p.layerIndex],i=parseInt(h.getAttribute("width")),j=parseInt(h.getAttribute("height")),e=[],k=h.getElementsByTagName("tile"),l=0,m=0;j>m;m++){e[m]=[];for(var n=0;i>n;n++){var o=k[l];e[m].push(parseInt(o.getAttribute("gid")-1)),l++}}}this.p.tiles=e,this.p.rows=e.length,this.p.cols=e[0].length,this.p.w=this.p.cols*this.p.tileW,this.p.h=this.p.rows*this.p.tileH},getTile:function(a,b){return this.p.tiles[b]&&this.p.tiles[b][a]},setTile:function(a,b,c){var d=this.p,e=Math.floor(a/d.blockTileW),f=Math.floor(b/d.blockTileH);e>=0&&f>=0&&e<this.p.cols&&f<this.p.cols&&(this.p.tiles[b][a]=c,this.blocks[f]&&(this.blocks[f][e]=null))},tilePresent:function(a,b){return this.p.tiles[b]&&this.collidableTile(this.p.tiles[b][a])},drawableTile:function(a){return a>0},collidableTile:function(a){return a>0},collide:function(b){var c,d=this.p,e=Math.floor((b.p.x-b.p.cx-d.x)/d.tileW),f=Math.floor((b.p.y-b.p.cy-d.y)/d.tileH),g=Math.ceil((b.p.x-b.p.cx+b.p.w-d.x)/d.tileW),h=Math.ceil((b.p.y-b.p.cy+b.p.h-d.y)/d.tileH),i=this.collisionObject,j=this.collisionNormal;j.collided=!1;for(var k=f;h>=k;k++)for(var l=e;g>=l;l++)this.tilePresent(l,k)&&(i.p.x=l*d.tileW+d.x+d.tileW/2,i.p.y=k*d.tileH+d.y+d.tileH/2,c=a.collision(b,i),c&&c.magnitude>0&&(!j.collided||j.magnitude<c.magnitude)&&(j.collided=!0,j.separate[0]=c.separate[0],j.separate[1]=c.separate[1],j.magnitude=c.magnitude,j.distance=c.distance,j.normalX=c.normalX,j.normalY=c.normalY,j.tileX=l,j.tileY=k,j.tile=this.getTile(l,k)));return j.collided?j:!1},prerenderBlock:function(a,b){var c=this.p,d=c.tiles,e=this.sheet(),f=a*c.blockTileW,g=b*c.blockTileH;if(!(0>f||f>=this.p.cols||0>g||g>=this.p.rows)){var h=document.createElement("canvas"),i=h.getContext("2d");h.width=c.blockW,h.height=c.blockH,this.blocks[b]=this.blocks[b]||{},this.blocks[b][a]=h;for(var j=0;j<c.blockTileH;j++)if(d[j+g])for(var k=0;k<c.blockTileW;k++)this.drawableTile(d[j+g][k+f])&&e.draw(i,k*c.tileW,j*c.tileH,d[j+g][k+f])}},drawBlock:function(a,b,c){var d=this.p,e=Math.floor(b*d.blockW+d.x),f=Math.floor(c*d.blockH+d.y);this.blocks[c]&&this.blocks[c][b]||this.prerenderBlock(b,c),this.blocks[c]&&this.blocks[c][b]&&a.drawImage(this.blocks[c][b],e,f)},draw:function(b){for(var c=this.p,d=this.stage.viewport,e=d?d.scale:1,f=d?d.x:0,g=d?d.y:0,h=a.width/e,i=a.height/e,j=Math.floor((f-c.x)/c.blockW),k=Math.floor((g-c.y)/c.blockH),l=Math.floor((f+h-c.x)/c.blockW),m=Math.floor((g+i-c.y)/c.blockH),n=k;m>=n;n++)for(var o=j;l>=o;o++)this.drawBlock(b,o,n)}}),a.gravityY=9.8*100,a.gravityX=0,a.component("2d",{added:function(){var b=this.entity;a._defaults(b.p,{vx:0,vy:0,ax:0,ay:0,gravity:1,collisionMask:a.SPRITE_DEFAULT}),b.on("step",this,"step"),b.on("hit",this,"collision")},collision:function(a){var b=this.entity,c=b.p;if(a.obj.p&&a.obj.p.sensor)return a.obj.trigger("sensor",b),void 0;a.impact=0;var d=Math.abs(c.vx),e=Math.abs(c.vy);c.x-=a.separate[0],c.y-=a.separate[1],a.normalY<-.3&&(c.vy>0&&(c.vy=0),a.impact=e,b.trigger("bump.bottom",a)),a.normalY>.3&&(c.vy<0&&(c.vy=0),a.impact=e,b.trigger("bump.top",a)),a.normalX<-.3&&(c.vx>0&&(c.vx=0),a.impact=d,b.trigger("bump.right",a)),a.normalX>.3&&(c.vx<0&&(c.vx=0),a.impact=d,b.trigger("bump.left",a))},step:function(b){for(var c=this.entity.p,d=b;d>0;)b=Math.min(1/30,d),c.vx+=c.ax*b+(void 0==c.gravityX?a.gravityX:c.gravityX)*b*c.gravity,c.vy+=c.ay*b+(void 0==c.gravityY?a.gravityY:c.gravityY)*b*c.gravity,c.x+=c.vx*b,c.y+=c.vy*b,this.entity.stage.collide(this.entity),d-=b}}),a.component("aiBounce",{added:function(){this.entity.on("bump.right",this,"goLeft"),this.entity.on("bump.left",this,"goRight")},goLeft:function(a){this.entity.p.vx=-a.impact},goRight:function(a){this.entity.p.vx=a.impact}})},Quintus.Anim=function(a){a._animations={},a.animations=function(b,c){a._animations[b]||(a._animations[b]={}),a._extend(a._animations[b],c)},a.animation=function(b,c){return a._animations[b]&&a._animations[b][c]},a.component("animation",{added:function(){var a=this.entity.p;a.animation=null,a.animationPriority=-1,a.animationFrame=0,a.animationTime=0,this.entity.on("step",this,"step")},extend:{play:function(a,b){this.animation.play(a,b)}},step:function(b){var c=this.entity,d=c.p;if(d.animation){var e=a.animation(d.sprite,d.animation),f=e.rate||d.rate,g=0;if(d.animationTime+=b,d.animationChanged?d.animationChanged=!1:(d.animationTime+=b,d.animationTime>f&&(g=Math.floor(d.animationTime/f),d.animationTime-=g*f,d.animationFrame+=g)),g>0){if(d.animationFrame>=e.frames.length){if(e.loop===!1||e.next)return d.animationFrame=e.frames.length-1,c.trigger("animEnd"),c.trigger("animEnd."+d.animation),d.animation=null,d.animationPriority=-1,e.trigger&&c.trigger(e.trigger,e.triggerData),e.next&&this.play(e.next,e.nextPriority),void 0;c.trigger("animLoop"),c.trigger("animLoop."+d.animation),d.animationFrame=d.animationFrame%e.frames.length}c.trigger("animFrame")}d.sheet=e.sheet||d.sheet,d.frame=e.frames[d.animationFrame]}},play:function(a,b){var c=this.entity,d=c.p;b=b||0,a!==d.animation&&b>=d.animationPriority&&(d.animation=a,d.animationChanged=!0,d.animationTime=0,d.animationFrame=0,d.animationPriority=b,c.trigger("anim"),c.trigger("anim."+d.animation))}}),a.Sprite.extend("Repeater",{init:function(b){this._super(a._defaults(b,{speedX:1,speedY:1,repeatY:!0,repeatX:!0,type:0})),this.p.repeatW=this.p.repeatW||this.p.w,this.p.repeatH=this.p.repeatH||this.p.h},draw:function(b){var c,d,e,f=this.p,g=this.asset(),h=this.sheet(),i=this.stage.viewport?this.stage.viewport.scale:1,j=this.stage.viewport?this.stage.viewport.x:0,k=this.stage.viewport?this.stage.viewport.y:0,l=f.x+j*this.p.speedX,m=f.y+k*this.p.speedY;for(f.repeatX?(c=Math.floor(-l%f.repeatW),c>0&&(c-=f.repeatW)):c=f.x-j,f.repeatY?(d=Math.floor(-m%f.repeatH),d>0&&(d-=f.repeatH)):d=f.y-k,e=c;d<a.height/i;){for(c=e;c<a.width/i&&(h?h.draw(b,Math.floor(c+j),Math.floor(d+k),f.frame):b.drawImage(g,Math.floor(c+j),Math.floor(d+k)),c+=f.repeatW,f.repeatX););if(d+=f.repeatH,!f.repeatY)break}}}),a.Tween=a.Class.extend({init:function(b,c,d,e,f){a._isObject(e)&&(f=e,e=a.Easing.Linear),a._isObject(d)&&(f=d,d=1),this.entity=b,this.duration=d||1,this.time=0,this.options=f||{},this.delay=this.options.delay||0,this.easing=e||this.options.easing||a.Easing.Linear,this.startFrame=a._loopFrame+1,this.properties=c,this.start={},this.diff={}},step:function(b){var c;if(this.startFrame>a._loopFrame)return!0;if(this.delay>=b)return this.delay-=b,!0;if(this.delay>0&&(b-=this.delay,this.delay=0),0===this.time){var d=this.entity,e=this.properties;this.p=d instanceof a.Stage?d.viewport:d.p;for(c in e)this.start[c]=this.p[c],a._isUndefined(this.start[c])||(this.diff[c]=e[c]-this.start[c])}this.time+=b;var f=Math.min(1,this.time/this.duration),g=this.easing(f);for(c in this.start)a._isUndefined(this.p[c])||(this.p[c]=this.start[c]+this.diff[c]*g);return f>=1&&this.options.callback&&this.options.callback.apply(this.entity),1>f}}),a.Easing={Linear:function(a){return a},Quadratic:{In:function(a){return a*a},Out:function(a){return a*(2-a)},InOut:function(a){return(a*=2)<1?.5*a*a:-.5*(--a*(a-2)-1)}}},a.component("tween",{added:function(){this._tweens=[],this.entity.on("step",this,"step")},extend:{animate:function(b,c,d,e){return this.tween._tweens.push(new a.Tween(this,b,c,d,e)),this},chain:function(b,c,d,e){a._isObject(d)&&(e=d,d=a.Easing.Linear);var f=this.tween._tweens.length;if(f>0){var g=this.tween._tweens[f-1];e=e||{},e.delay=g.duration-g.time+g.delay}return this.animate(b,c,d,e),this},stop:function(){return this.tween._tweens.length=0,this}},step:function(a){for(var b=0;b<this._tweens.length;b++)this._tweens[b].step(a)||(this._tweens.splice(b,1),b--)}})},Quintus.Audio=function(a){a.audio={channels:[],channelMax:a.options.channelMax||10,active:{},play:function(){}},a.hasWebAudio="undefined"!=typeof AudioContext||"undefined"!=typeof webkitAudioContext,a.hasWebAudio&&(a.audioContext="undefined"!=typeof AudioContext?new AudioContext:new window.webkitAudioContext),a.enableSound=function(){return a.hasWebAudio?a.audio.enableWebAudioSound():a.audio.enableHTML5Sound(),a},a.audio.enableWebAudioSound=function(){a.audio.type="WebAudio",a.audio.soundID=0,a.audio.playingSounds={},a.audio.removeSound=function(b){delete a.audio.playingSounds[b]},a.audio.play=function(b,c){var d=(new Date).getTime();if(!(a.audio.active[b]&&a.audio.active[b]>d)){c&&c.debounce?a.audio.active[b]=d+c.debounce:delete a.audio.active[b];var e=a.audio.soundID++,f=a.audioContext.createBufferSource();f.buffer=a.asset(b),f.connect(a.audioContext.destination),c&&c.loop?f.loop=!0:setTimeout(function(){a.audio.removeSound(e)},1e3*f.buffer.duration),f.assetName=b,f.start?f.start(0):f.noteOn(0),a.audio.playingSounds[e]=f}},a.audio.stop=function(b){for(var c in a.audio.playingSounds){var d=a.audio.playingSounds[c];b&&b!==d.assetName||(d.stop?d.stop(0):d.noteOff(0))}}},a.audio.enableHTML5Sound=function(){a.audio.type="HTML5";for(var b=0;b<a.audio.channelMax;b++)a.audio.channels[b]={},a.audio.channels[b].channel=new Audio,a.audio.channels[b].finished=-1;a.audio.play=function(b,c){var d=(new Date).getTime();if(!(a.audio.active[b]&&a.audio.active[b]>d)){c&&c.debounce?a.audio.active[b]=d+c.debounce:delete a.audio.active[b];for(var e=0;e<a.audio.channels.length;e++)if(!a.audio.channels[e].loop&&a.audio.channels[e].finished<d){a.audio.channels[e].channel.src=a.asset(b).src,c&&c.loop?(a.audio.channels[e].loop=!0,a.audio.channels[e].channel.loop=!0):a.audio.channels[e].finished=d+1e3*a.asset(b).duration,a.audio.channels[e].channel.load(),a.audio.channels[e].channel.play();break}}},a.audio.stop=function(b){for(var c=b?a.asset(b).src:null,d=(new Date).getTime(),e=0;e<a.audio.channels.length;e++)c&&a.audio.channels[e].channel.src!==c||!(a.audio.channels[e].loop||a.audio.channels[e].finished>=d)||(a.audio.channels[e].channel.pause(),a.audio.channels[e].loop=!1)}}},Quintus.Input=function(a){var b={LEFT:37,RIGHT:39,SPACE:32,UP:38,DOWN:40,Z:90,X:88},c={LEFT:"left",RIGHT:"right",UP:"up",DOWN:"down",SPACE:"fire",Z:"fire",X:"action"},d=[["left","<"],["right",">"],[],["action","b"],["fire","a"]],e=["up","right","down","left"];a.inputs={},a.joypad={};var f=!!("ontouchstart"in window);a.canvasToStageX=function(b,c){return b=b/a.cssWidth*a.width,c.viewport&&(b/=c.viewport.scale,b+=c.viewport.x),b},a.canvasToStageY=function(b,c){return b=b/a.cssWidth*a.width,c.viewport&&(b/=c.viewport.scale,b+=c.viewport.y),b},a.InputSystem=a.Evented.extend({keys:{},keypad:{},keyboardEnabled:!1,touchEnabled:!1,joypadEnabled:!1,bindKey:function(c,d){a.input.keys[b[c]||c]=d},keyboardControls:function(b){b=b||c,a._each(b,function(a,b){this.bindKey(b,a)},a.input),this.enableKeyboard()},enableKeyboard:function(){return this.keyboardEnabled?!1:(a.el.tabIndex=0,a.el.style.outline=0,a.el.addEventListener("keydown",function(b){if(a.input.keys[b.keyCode]){var c=a.input.keys[b.keyCode];a.inputs[c]=!0,a.input.trigger(c),a.input.trigger("keydown",b.keyCode)}b.preventDefault()},!1),a.el.addEventListener("keyup",function(b){if(a.input.keys[b.keyCode]){var c=a.input.keys[b.keyCode];a.inputs[c]=!1,a.input.trigger(c+"Up"),a.input.trigger("keyup",b.keyCode)}b.preventDefault()},!1),a.el.focus(),this.keyboardEnabled=!0,void 0)},_containerOffset:function(){a.input.offsetX=0,a.input.offsetY=0;var b=a.el;do a.input.offsetX+=b.offsetLeft,a.input.offsetY+=b.offsetTop;while(b=b.offsetParent)},touchLocation:function(b){var c,d,e=(a.el,b.offsetX),f=b.offsetY;return(a._isUndefined(e)||a._isUndefined(f))&&(e=b.layerX,f=b.layerY),(a._isUndefined(e)||a._isUndefined(f))&&(void 0===a.input.offsetX&&a.input._containerOffset(),e=b.pageX-a.input.offsetX,f=b.pageY-a.input.offsetY),c=a.width*e/a.cssWidth,d=a.height*f/a.cssHeight,{x:c,y:d}},touchControls:function(b){function c(c){for(var d=a.input.touchLocation(c),e=0,f=b.controls.length;f>e;e++)if(d.x<b.unit*(e+1))return b.controls[e][0]}function e(d){var e,f,g,h,i,j={};for(e=0,f=b.controls.length;f>e;e++)i=b.controls[e][0],a.inputs[i]&&(j[i]=!0),a.inputs[i]=!1;var k=d.touches?d.touches:[d];for(e=0,f=k.length;f>e;e++)g=k[e],h=c(g),h&&(a.inputs[h]=!0,j[h]?delete j[h]:a.input.trigger(h));for(i in j)a.input.trigger(i+"Up");return null}return this.touchEnabled?!1:f?(a.input.keypad=b=a._extend({left:0,gutter:10,controls:d,width:a.width,bottom:a.height},b),b.unit=b.width/b.controls.length,b.size=b.unit-2*b.gutter,this.touchDispatchHandler=function(a){e(a),a.preventDefault()},a._each(["touchstart","touchend","touchmove","touchcancel"],function(b){a.el.addEventListener(b,this.touchDispatchHandler)},this),this.touchEnabled=!0,void 0):!1},disableTouchControls:function(){a._each(["touchstart","touchend","touchmove","touchcancel"],function(b){a.el.removeEventListener(b,this.touchDispatchHandler)},this),a.el.removeEventListener("touchstart",this.joypadStart),a.el.removeEventListener("touchmove",this.joypadMove),a.el.removeEventListener("touchend",this.joypadEnd),a.el.removeEventListener("touchcancel",this.joypadEnd),this.touchEnabled=!1},joypadControls:function(b){if(this.joypadEnabled)return!1;if(!f)return!1;var c=a.joypad=a._defaults(b||{},{size:50,trigger:20,center:25,color:"#CCC",background:"#000",alpha:.5,zone:a.width/2,joypadTouch:null,inputs:e,triggers:[]});this.joypadStart=function(b){if(null===c.joypadTouch){var d=b.changedTouches[0],e=a.input.touchLocation(d);e.x<c.zone&&(c.joypadTouch=d.identifier,c.centerX=e.x,c.centerY=e.y,c.x=null,c.y=null)}},this.joypadMove=function(b){if(null!==c.joypadTouch)for(var d=b,e=0,f=d.changedTouches.length;f>e;e++){var g=d.changedTouches[e];if(g.identifier===c.joypadTouch){var h=a.input.touchLocation(g),i=h.x-c.centerX,j=h.y-c.centerY,k=Math.sqrt(i*i+j*j),l=Math.max(1,k/c.size),m=Math.atan2(i,j);l>1&&(i/=l,j/=l,k/=l);for(var n=[j<-c.trigger,i>c.trigger,j>c.trigger,i<-c.trigger],o=0;o<n.length;o++){var p=c.inputs[o];n[o]?(a.inputs[p]=!0,c.triggers[o]||a.input.trigger(p)):(a.inputs[p]=!1,c.triggers[o]&&a.input.trigger(p+"Up"))}a._extend(c,{dx:i,dy:j,x:c.centerX+i,y:c.centerY+j,dist:k,ang:m,triggers:n});break}}b.preventDefault()},this.joypadEnd=function(b){var d=b;if(null!==c.joypadTouch)for(var e=0,f=d.changedTouches.length;f>e;e++){var g=d.changedTouches[e];if(g.identifier===c.joypadTouch){for(var h=0;h<c.triggers.length;h++){var i=c.inputs[h];a.inputs[i]=!1}c.joypadTouch=null;break}}b.preventDefault()},a.el.addEventListener("touchstart",this.joypadStart),a.el.addEventListener("touchmove",this.joypadMove),a.el.addEventListener("touchend",this.joypadEnd),a.el.addEventListener("touchcancel",this.joypadEnd),this.joypadEnabled=!0},mouseControls:function(b){b=b||{};var c=b.stageNum||0,d=b.mouseX||"mouseX",e=b.mouseY||"mouseY",f={};a.el.style.cursor="none",a.inputs[d]=0,a.inputs[e]=0,a._mouseMove=function(b){b.preventDefault();var g=b.touches?b.touches[0]:b,h=(a.el,g.offsetX),i=g.offsetY,j=a.stage(c);(a._isUndefined(h)||a._isUndefined(i))&&(h=g.layerX,i=g.layerY),(a._isUndefined(h)||a._isUndefined(i))&&(void 0===a.input.offsetX&&a.input._containerOffset(),h=g.pageX-a.input.offsetX,i=g.pageY-a.input.offsetY),j&&(f.x=a.canvasToStageX(h,j),f.y=a.canvasToStageY(i,j),a.inputs[d]=f.x,a.inputs[e]=f.y,a.input.trigger("mouseMove",f))},a.el.addEventListener("mousemove",a._mouseMove,!0),a.el.addEventListener("touchstart",a._mouseMove,!0),a.el.addEventListener("touchmove",a._mouseMove,!0)},disableMouseControls:function(){a._mouseMove&&(a.el.removeEventListener("mousemove",a._mouseMove),a.el.style.cursor="inherit",a._mouseMove=null)},drawButtons:function(){var b=a.input.keypad,c=a.ctx;c.save(),c.textAlign="center",c.textBaseline="middle";for(var d=0;d<b.controls.length;d++){var e=b.controls[d];if(e[0]){c.font="bold "+b.size/2+"px arial";var f=d*b.unit+b.gutter,g=b.bottom-b.unit,h=a.inputs[e[0]];c.fillStyle=b.color||"#FFFFFF",c.globalAlpha=h?1:.5,c.fillRect(f,g,b.size,b.size),c.fillStyle=b.text||"#000000",c.fillText(e[1],f+b.size/2,g+b.size/2)
}}c.restore()},drawCircle:function(b,c,d,e){var f=a.ctx,g=a.joypad;f.save(),f.beginPath(),f.globalAlpha=g.alpha,f.fillStyle=d,f.arc(b,c,e,0,2*Math.PI,!0),f.closePath(),f.fill(),f.restore()},drawJoypad:function(){var b=a.joypad;null!==b.joypadTouch&&(a.input.drawCircle(b.centerX,b.centerY,b.background,b.size),null!==b.x&&a.input.drawCircle(b.x,b.y,b.color,b.center))},drawCanvas:function(){this.touchEnabled&&this.drawButtons(),this.joypadEnabled&&this.drawJoypad()}}),a.input=new a.InputSystem,a.controls=function(b){return a.input.keyboardControls(),b?(a.input.touchControls({controls:[[],[],[],["action","b"],["fire","a"]]}),a.input.joypadControls()):a.input.touchControls(),a},a.component("platformerControls",{defaults:{speed:200,jumpSpeed:-300},added:function(){var b=this.entity.p;a._defaults(b,this.defaults),this.entity.on("step",this,"step"),this.entity.on("bump.bottom",this,"landed"),b.landed=0,b.direction="right"},landed:function(){var a=this.entity.p;a.landed=.2},step:function(b){var c=this.entity.p;a.inputs.left?(c.vx=-c.speed,c.direction="left"):a.inputs.right?(c.direction="right",c.vx=c.speed):c.vx=0,c.landed>0&&(a.inputs.up||a.inputs.action)&&(c.vy=c.jumpSpeed,c.landed=-b),c.landed-=b}}),a.component("stepControls",{added:function(){var a=this.entity.p;a.stepDistance||(a.stepDistance=32),a.stepDelay||(a.stepDelay=.2),a.stepWait=0,this.entity.on("step",this,"step"),this.entity.on("hit",this,"collision")},collision:function(){var a=this.entity.p;a.stepping&&(a.stepping=!1,a.x=a.origX,a.y=a.origY)},step:function(b){var c=this.entity.p;c.stepWait-=b,c.stepping&&(c.x+=c.diffX*b/c.stepDelay,c.y+=c.diffY*b/c.stepDelay),c.stepWait>0||(c.stepping&&(c.x=c.destX,c.y=c.destY),c.stepping=!1,c.diffX=0,c.diffY=0,a.inputs.left?c.diffX=-c.stepDistance:a.inputs.right&&(c.diffX=c.stepDistance),a.inputs.up?c.diffY=-c.stepDistance:a.inputs.down&&(c.diffY=c.stepDistance),(c.diffY||c.diffX)&&(c.stepping=!0,c.origX=c.x,c.origY=c.y,c.destX=c.x+c.diffX,c.destY=c.y+c.diffY,c.stepWait=c.stepDelay))}})},Quintus.Scenes=function(a){a.scenes={},a.stages=[],a.Scene=a.Class.extend({init:function(a,b){this.opts=b||{},this.sceneFunc=a}}),a.scene=function(b,c,d){return void 0===c?a.scenes[b]:(a._isFunction(c)&&(c=new a.Scene(c,d)),a.scenes[b]=c,c)},a._nullContainer={c:{x:0,y:0,angle:0,scale:1},matrix:a.matrix2d()},a.collision=function(){function b(a,b){var c=a[b],d=a[b+1]||a[0];f=-(d[1]-c[1]),g=d[0]-c[0];var e=Math.sqrt(f*f+g*g);e>0&&(f/=e,g/=e)}function c(a){return f*a[0]+g*a[1]}function d(a,d,e){var k,l,m,n,o,p,q,r,s,t,u,v,w,x,y=Number.POSITIVE_INFINITY,z=!1,A=e?j:i;for(h[0]=0,h[1]=0,a.c?w=a.c.points:(w=a.p.points,h[0]+=a.p.x,h[1]+=a.p.y),d.c?x=d.c.points:(x=d.p.points,h[0]+=-d.p.x,h[1]+=-d.p.y),a=a.p,d=d.p,s=0;s<w.length;s++){for(b(w,s),k=c(w[0]),l=k,t=1;t<w.length;t++)r=c(w[t]),k>r&&(k=r),r>l&&(l=r);for(m=c(x[0]),n=m,t=1;t<x.length;t++)r=c(x[t]),m>r&&(m=r),r>n&&(n=r);if(q=c(h),k+=q,l+=q,o=k-n,p=m-l,o>0||p>0)return null;u=-1*(n-k),e&&(u*=-1),v=Math.abs(u),y>v&&(A.distance=u,A.magnitude=v,A.normalX=f,A.normalY=g,A.distance>0&&(A.distance*=-1,A.normalX*=-1,A.normalY*=-1),z=!0,y=v)}return z?A:null}function e(b,c){var e,f,g;return b.p.points||a._generatePoints(b),c.p.points||a._generatePoints(c),a._generateCollisionPoints(b),a._generateCollisionPoints(c),(e=d(b,c))?(f=d(c,b,!0))?(g=f.magnitude<e.magnitude?f:e,0===g.magnitude?!1:(g.separate[0]=g.distance*g.normalX,g.separate[1]=g.distance*g.normalY,g)):!1:!1}var f,g,h=[0,0],i={separate:[]},j={separate:[]};return e}(),a.overlap=function(a,b){var c=a.c||a.p,d=b.c||b.p,e=c.x-c.cx,f=c.y-c.cy,g=d.x-d.cx,h=d.y-d.cy;return!(f+c.h<h||f>h+d.h||e+c.w<g||e>g+d.w)},a.Stage=a.GameObject.extend({defaults:{sort:!1,gridW:400,gridH:400},init:function(b,c){this.scene=b,this.items=[],this.lists={},this.index={},this.removeList=[],this.grid={},this.options=a._extend({},this.defaults),this.scene&&a._extend(this.options,b.opts),c&&a._extend(this.options,c),this.options.sort&&!a._isFunction(this.options.sort)&&(this.options.sort=function(a,b){return(a.p&&a.p.z||-1)-(b.p&&b.p.z||-1)})},destroyed:function(){this.invoke("debind"),this.trigger("destroyed")},loadScene:function(){this.scene&&this.scene.sceneFunc(this)},loadAssets:function(b){for(var c=a._isArray(b)?b:a.asset(stage.options.asset),d=0;d<c.length;d++){var e=c[d][0],f=c[d][1];this.insert(new a[e](f))}},each:function(a){for(var b=0,c=this.items.length;c>b;b++)a.call(this.items[b],arguments[1],arguments[2])},invoke:function(a){for(var b=0,c=this.items.length;c>b;b++)this.items[b][a].call(this.items[b],arguments[1],arguments[2])},detect:function(a){for(var b=this.items.length-1;b>=0;b--)if(a.call(this.items[b],arguments[1],arguments[2],arguments[3]))return this.items[b];return!1},identify:function(a){for(var b,c=this.items.length-1;c>=0;c--)if(b=a.call(this.items[c],arguments[1],arguments[2],arguments[3]))return b;return!1},addToLists:function(a,b){for(var c=0;c<a.length;c++)this.addToList(a[c],b)},addToList:function(a,b){this.lists[a]||(this.lists[a]=[]),this.lists[a].push(b)},removeFromLists:function(a,b){for(var c=0;c<a.length;c++)this.removeFromList(a[c],b)},removeFromList:function(a,b){var c=this.lists[a].indexOf(b);-1!==c&&this.lists[a].splice(c,1)},insert:function(b,c){return this.items.push(b),b.stage=this,b.container=c,c&&c.children.push(b),b.grid={},a._generatePoints(b),a._generateCollisionPoints(b),b.className&&this.addToList(b.className,b),b.activeComponents&&this.addToLists(b.activeComponents,b),b.p&&(this.index[b.p.id]=b),this.trigger("inserted",b),b.trigger("inserted",this),this.regrid(b),b},remove:function(a){this.delGrid(a),this.removeList.push(a)},forceRemove:function(a){var b=this.items.indexOf(a);if(-1!==b){if(this.items.splice(b,1),a.className&&this.removeFromList(a.className,a),a.activeComponents&&this.removeFromLists(a.activeComponents,a),a.container){var c=a.container.children.indexOf(a);-1!==c&&a.container.children.splice(c,1)}a.destroy&&a.destroy(),a.p.id&&delete this.index[a.p.id],this.trigger("removed",a)}},pause:function(){this.paused=!0},unpause:function(){this.paused=!1},_gridCellCheck:function(b,c,d,e){if(!e||e&b){var f=this.index[c];if(f&&f!==d&&a.overlap(d,f)){var g=a.collision(d,f);return g?(g.obj=f,g):!1}}},gridTest:function(b,c){for(var d,e,f=b.grid,g=f.Y1;g<=f.Y2;g++)if(this.grid[g])for(var h=f.X1;h<=f.X2;h++)if(d=this.grid[g][h],d&&(e=a._detect(d,this._gridCellCheck,this,b,c)))return e;return!1},collisionLayer:function(a){return this._collisionLayer=a,this.insert(a)},search:function(a,b){var c;return b=b||a.p&&a.p.collisionMask,this._collisionLayer&&this._collisionLayer.p.type&b&&(c=this._collisionLayer.collide(a))?c:c=this.gridTest(a,b,this._collisionLayer)},_locateObj:{p:{x:0,y:0,cx:0,cy:0,w:1,h:1},grid:{}},locate:function(a,b,c){var d=null;return this._locateObj.p.x=a,this._locateObj.p.y=b,this.regrid(this._locateObj,!0),this._collisionLayer&&this._collisionLayer.p.type&c&&(d=this._collisionLayer.collide(this._locateObj)),d||(d=this.gridTest(this._locateObj,c,this._collisionLayer)),d&&d.obj?d.obj:!1},collide:function(b,c){var d,e,f,g,h,i;if(a._isObject(c)?(f=c.collisionMask,g=c.maxCol,i=c.skipEvents):f=c,f=f||b.p&&b.p.collisionMask,g=g||3,h=g,this.regrid(b),this._collisionLayer&&this._collisionLayer.p.type&f)for(;h>0&&(d=this._collisionLayer.collide(b));)d.obj=this._collisionLayer,i||(b.trigger("hit",d),b.trigger("hit.collision",d)),this.regrid(b),h--;for(h=g;h>0&&(e=this.gridTest(b,f,this._collisionLayer));){if(b.trigger("hit",e),b.trigger("hit.sprite",e),!i){var j=e.obj;e.obj=b,e.normalX*=-1,e.normalY*=-1,e.distance=0,e.magnitude=0,e.separate[0]=0,e.separate[1]=0,j.trigger("hit",e),j.trigger("hit.sprite",e)}this.regrid(b),h--}return e||d},delGrid:function(a){for(var b=a.grid,c=b.Y1;c<=b.Y2;c++)if(this.grid[c])for(var d=b.X1;d<=b.X2;d++)this.grid[c][d]&&delete this.grid[c][d][a.p.id]},addGrid:function(a){for(var b=a.grid,c=b.Y1;c<=b.Y2;c++){this.grid[c]||(this.grid[c]={});for(var d=b.X1;d<=b.X2;d++)this.grid[c][d]||(this.grid[c][d]={}),this.grid[c][d][a.p.id]=a.p.type}},regrid:function(a,b){if((!this._collisionLayer||a!==this._collisionLayer)&&(a.p.type||b)){var c=a.c||a.p,d=Math.floor((c.x-c.cx)/this.options.gridW),e=Math.floor((c.y-c.cy)/this.options.gridH),f=Math.floor((c.x-c.cx+c.w)/this.options.gridW),g=Math.floor((c.y-c.cy+c.h)/this.options.gridH),h=a.grid;(h.X1!==d||h.X2!==f||h.Y1!==e||h.Y2!==g)&&(void 0!==h.X1&&this.delGrid(a),h.X1=d,h.X2=f,h.Y1=e,h.Y2=g,b||this.addGrid(a))}},updateSprites:function(b,c,d){for(var e,f=0,g=b.length;g>f;f++)e=b[f],(d||!e.container)&&(e.update(c),a._generateCollisionPoints(e),this.regrid(e))},step:function(a){if(this.paused)return!1;if(this.trigger("prestep",a),this.updateSprites(this.items,a),this.trigger("step",a),this.removeList.length>0){for(var b=0,c=this.removeList.length;c>b;b++)this.forceRemove(this.removeList[b]);this.removeList.length=0}this.trigger("poststep",a)},hide:function(){this.hidden=!0},show:function(){this.hidden=!1},stop:function(){this.hide(),this.pause()},start:function(){this.show(),this.unpause()},render:function(a){if(this.hidden)return!1;this.options.sort&&this.items.sort(this.options.sort),this.trigger("prerender",a),this.trigger("beforerender",a);for(var b=0,c=this.items.length;c>b;b++){var d=this.items[b];d.container||d.render(a)}this.trigger("render",a),this.trigger("postrender",a)}}),a.activeStage=0,a.StageSelector=a.Class.extend({emptyList:[],init:function(a,b){this.stage=a,this.selector=b,this.items=this.stage.lists[this.selector]||this.emptyList,this.length=this.items.length},each:function(a){for(var b=0,c=this.items.length;c>b;b++)a.call(this.items[b],arguments[1],arguments[2]);return this},invoke:function(a){for(var b=0,c=this.items.length;c>b;b++)this.items[b][a].call(this.items[b],arguments[1],arguments[2]);return this},trigger:function(a,b){this.invoke("trigger",a,b)},destroy:function(){this.invoke("destroy")},detect:function(a){for(var b=0,c=this.items.length;c>b;b++)if(a.call(this.items[b],arguments[1],arguments[2]))return this.items[b];return!1},identify:function(a){for(var b=null,c=0,d=this.items.length;d>c;c++)if(b=a.call(this.items[c],arguments[1],arguments[2]))return b;return!1},_pObject:function(b){a._extend(this.p,b)},_pSingle:function(a,b){this.p[a]=b},set:function(a,b){return void 0===b?this.each(this._pObject,a):this.each(this._pSingle,a,b),this},at:function(a){return this.items[a]},first:function(){return this.items[0]},last:function(){return this.items[this.items.length-1]}}),a.select=function(b,c){return c=void 0===c?a.activeStage:c,c=a.stage(c),a._isNumber(b)?c.index[b]:new a.StageSelector(c,b)},a.stage=function(b){return b=void 0===b?a.activeStage:b,a.stages[b]},a.stageScene=function(b,c,d){a._isString(b)&&(b=a.scene(b)),a._isObject(c)&&(d=c,c=a._popProperty(d,"stage")||b&&b.opts.stage||0),d=a._clone(d);var e=a._popProperty(d,"stageClass")||b&&b.opts.stageClass||a.Stage;c=a._isUndefined(c)?b&&b.opts.stage||0:c,a.stages[c]&&a.stages[c].destroy(),a.activeStage=c;var f=a.stages[c]=new e(b,d);return f.options.asset&&f.loadAssets(),b&&f.loadScene(),a.activeStage=0,a.loop||a.gameLoop(a.stageGameLoop),f},a.stageGameLoop=function(b){var c,d,e;for(0>b&&(b=1/60),b>1/15&&(b=1/15),c=0,d=a.stages.length;d>c;c++)a.activeStage=c,e=a.stage(),e&&e.step(b);for(a.ctx&&a.clear(),c=0,d=a.stages.length;d>c;c++)a.activeStage=c,e=a.stage(),e&&e.render(a.ctx);a.activeStage=0,a.input&&a.ctx&&a.input.drawCanvas(a.ctx)},a.clearStage=function(b){a.stages[b]&&(a.stages[b].destroy(),a.stages[b]=null)},a.clearStages=function(){for(var b=0,c=a.stages.length;c>b;b++)a.stages[b]&&a.stages[b].destroy();a.stages.length=0}},Quintus.Sprites=function(a){return a.Class.extend("SpriteSheet",{init:function(b,c,d){if(!a.asset(c))throw"Invalid Asset:"+c;a._extend(this,{name:b,asset:c,w:a.asset(c).width,h:a.asset(c).height,tilew:64,tileh:64,sx:0,sy:0}),d&&a._extend(this,d),this.cols=this.cols||Math.floor(this.w/this.tilew)},fx:function(a){return Math.floor(a%this.cols*this.tilew+this.sx)},fy:function(a){return Math.floor(Math.floor(a/this.cols)*this.tileh+this.sy)},draw:function(b,c,d,e){b||(b=a.ctx),b.drawImage(a.asset(this.asset),this.fx(e),this.fy(e),this.tilew,this.tileh,Math.floor(c),Math.floor(d),this.tilew,this.tileh)}}),a.sheets={},a.sheet=function(b,c,d){return c?(a.sheets[b]=new a.SpriteSheet(b,c,d),void 0):a.sheets[b]},a.compileSheets=function(b,c){var d=a.asset(c);a._each(d,function(c,d){a.sheet(d,b,c)})},a.SPRITE_NONE=0,a.SPRITE_DEFAULT=1,a.SPRITE_PARTICLE=2,a.SPRITE_ACTIVE=4,a.SPRITE_FRIENDLY=8,a.SPRITE_ENEMY=16,a.SPRITE_POWERUP=32,a.SPRITE_UI=64,a.SPRITE_ALL=65535,a._generatePoints=function(a,b){if(!a.p.points||b){var c=a.p,d=c.w/2,e=c.h/2;c.points=[[-d,-e],[d,-e],[d,e],[-d,e]]}},a._generateCollisionPoints=function(b){if(b.matrix||b.refreshMatrix){b.c||(b.c={points:[]});var c=b.p,d=b.c;if(c.moved||d.origX!==c.x||d.origY!==c.y||d.origScale!==c.scale||d.origScale!==c.angle){d.origX=c.x,d.origY=c.y,d.origScale=c.scale,d.origAngle=c.angle,b.refreshMatrix();var e=b.container||a._nullContainer;d.x=e.matrix.transformX(c.x,c.y),d.y=e.matrix.transformY(c.x,c.y),d.angle=c.angle+e.c.angle,d.scale=(e.c.scale||1)*(c.scale||1);for(var f=1/0,g=1/0,h=-1/0,i=-1/0,j=0;j<b.p.points.length;j++){b.c.points[j]||(b.c.points[j]=[]),b.matrix.transformArr(b.p.points[j],b.c.points[j]);var k=b.c.points[j][0],l=b.c.points[j][1];f>k&&(f=k),k>h&&(h=k),g>l&&(g=l),l>i&&(i=l)}f===h&&(h+=1),g===i&&(i+=1),d.cx=d.x-f,d.cy=d.y-g,d.w=h-f,d.h=i-g}}},a.GameObject.extend("Sprite",{init:function(b,c){this.p=a._extend({x:0,y:0,z:0,opacity:1,angle:0,frame:0,type:a.SPRITE_DEFAULT|a.SPRITE_ACTIVE},c),this.matrix=new a.Matrix2D,this.children=[],a._extend(this.p,b),this.size(),this.p.id=this.p.id||a._uniqueId(),this.c={points:[]},this.refreshMatrix()},size:function(a){!a&&this.p.w&&this.p.h||(this.asset()?(this.p.w=this.asset().width,this.p.h=this.asset().height):this.sheet()&&(this.p.w=this.sheet().tilew,this.p.h=this.sheet().tileh)),this.p.cx=a||void 0===this.p.cx?this.p.w/2:this.p.cx,this.p.cy=a||void 0===this.p.cy?this.p.h/2:this.p.cy},asset:function(b,c){return b?(this.p.asset=b,c&&(this.size(!0),a._generatePoints(this,!0)),void 0):a.asset(this.p.asset)},sheet:function(b,c){return b?(this.p.sheet=b,c&&(this.size(!0),a._generatePoints(this,!0)),void 0):a.sheet(this.p.sheet)},hide:function(){this.p.hidden=!0},show:function(){this.p.hidden=!1},set:function(b){return a._extend(this.p,b),this},_sortChild:function(a,b){return(a.p&&a.p.z||-1)-(b.p&&b.p.z||-1)},_flipArgs:{x:[-1,1],y:[1,-1],xy:[-1,-1]},render:function(b){var c=this.p;c.hidden||(b||(b=a.ctx),this.trigger("predraw",b),b.save(),void 0!==this.p.opacity&&1!==this.p.opacity&&(b.globalAlpha=this.p.opacity),this.matrix.setContextTransform(b),this.p.flip&&b.scale.apply(b,this._flipArgs[this.p.flip]),this.trigger("beforedraw",b),this.draw(b),this.trigger("draw",b),b.restore(),this.p.sort&&this.children.sort(this._sortChild),a._invoke(this.children,"render",b),this.trigger("postdraw",b),a.debug&&this.debugRender(b))},center:function(){this.container?(this.p.x=this.container.p.w/2,this.p.y=this.container.p.h/2):(this.p.x=a.width/2,this.p.y=a.height/2)},draw:function(b){var c=this.p;c.sheet?this.sheet().draw(b,-c.cx,-c.cy,c.frame):c.asset&&b.drawImage(a.asset(c.asset),-c.cx,-c.cy)},debugRender:function(b){this.p.points||a._generatePoints(this),b.save(),this.matrix.setContextTransform(b),b.beginPath(),b.fillStyle=this.p.hit?"blue":"red",b.strokeStyle="#FF0000",b.fillStyle="rgba(0,0,0,0.5)",b.moveTo(this.p.points[0][0],this.p.points[0][1]);for(var c=0;c<this.p.points.length;c++)b.lineTo(this.p.points[c][0],this.p.points[c][1]);if(b.lineTo(this.p.points[0][0],this.p.points[0][1]),b.stroke(),a.debugFill&&b.fill(),b.restore(),this.c){var d=this.c;b.save(),b.globalAlpha=1,b.lineWidth=2,b.strokeStyle="#FF00FF",b.beginPath(),b.moveTo(d.x-d.cx,d.y-d.cy),b.lineTo(d.x-d.cx+d.w,d.y-d.cy),b.lineTo(d.x-d.cx+d.w,d.y-d.cy+d.h),b.lineTo(d.x-d.cx,d.y-d.cy+d.h),b.lineTo(d.x-d.cx,d.y-d.cy),b.stroke(),b.restore()}},update:function(a){this.trigger("prestep",a),this.step&&this.step(a),this.trigger("step",a),this.refreshMatrix(),this.stage&&this.children.length>0&&this.stage.updateSprites(this.children,a,!0)},refreshMatrix:function(){var a=this.p;this.matrix.identity(),this.container&&this.matrix.multiply(this.container.matrix),this.matrix.translate(a.x,a.y),a.scale&&this.matrix.scale(a.scale,a.scale),this.matrix.rotateDeg(a.angle)}}),a.Sprite.extend("MovingSprite",{init:function(b,c){this._super(a._extend({vx:0,vy:0,ax:0,ay:0},b),c)},step:function(a){var b=this.p;b.vx+=b.ax*a,b.vy+=b.ay*a,b.x+=b.vx*a,b.y+=b.vy*a}}),a},Quintus.Touch=function(a){if(a._isUndefined(Quintus.Sprites))throw"Quintus.Touch requires Quintus.Sprites Module";var b=[0],c=0;a.Evented.extend("TouchSystem",{init:function(){var b=this;this.boundTouch=function(a){b.touch(a)},this.boundDrag=function(a){b.drag(a)},this.boundEnd=function(a){b.touchEnd(a)},a.el.addEventListener("touchstart",this.boundTouch),a.el.addEventListener("mousedown",this.boundTouch),a.el.addEventListener("touchmove",this.boundDrag),a.el.addEventListener("mousemove",this.boundDrag),a.el.addEventListener("touchend",this.boundEnd),a.el.addEventListener("mouseup",this.boundEnd),a.el.addEventListener("touchcancel",this.boundEnd),this.touchPos=new a.Evented,this.touchPos.grid={},this.touchPos.p={w:1,h:1,cx:0,cy:0},this.activeTouches={},this.touchedObjects={}},destroy:function(){a.el.removeEventListener("touchstart",this.boundTouch),a.el.removeEventListener("mousedown",this.boundTouch),a.el.removeEventListener("touchmove",this.boundDrag),a.el.removeEventListener("mousemove",this.boundDrag),a.el.removeEventListener("touchend",this.boundEnd),a.el.removeEventListener("mouseup",this.boundEnd),a.el.removeEventListener("touchcancel",this.boundEnd)},normalizeTouch:function(b,c){var d=b.offsetX,e=b.offsetY;if((a._isUndefined(d)||a._isUndefined(e))&&(d=b.layerX,e=b.layerY),a._isUndefined(d)||a._isUndefined(e)){if(void 0===a.touch.offsetX){a.touch.offsetX=0,a.touch.offsetY=0;var f=a.el;do a.touch.offsetX+=f.offsetLeft,a.touch.offsetY+=f.offsetTop;while(f=f.offsetParent)}d=b.pageX-a.touch.offsetX,e=b.pageY-a.touch.offsetY}return this.touchPos.p.ox=this.touchPos.p.px=d/a.cssWidth*a.width,this.touchPos.p.oy=this.touchPos.p.py=e/a.cssHeight*a.height,c.viewport&&(this.touchPos.p.px/=c.viewport.scale,this.touchPos.p.py/=c.viewport.scale,this.touchPos.p.px+=c.viewport.x,this.touchPos.p.py+=c.viewport.y),this.touchPos.p.x=this.touchPos.p.px,this.touchPos.p.y=this.touchPos.p.py,this.touchPos.obj=null,this.touchPos},touch:function(d){for(var e=d.changedTouches||[d],f=0;f<e.length;f++)for(var g=0;g<b.length;g++){var h=e[f],i=a.stage(b[g]);if(i){h.identifier=h.identifier||0;var j=this.normalizeTouch(h,i);i.regrid(j,!0);var k,l=i.search(j,c);if((l||g===b.length-1)&&(k=l&&l.obj,j.obj=k,this.trigger("touch",j)),k&&!this.touchedObjects[k]){this.activeTouches[h.identifier]={x:j.p.px,y:j.p.py,origX:k.p.x,origY:k.p.y,sx:j.p.ox,sy:j.p.oy,identifier:h.identifier,obj:k,stage:i},this.touchedObjects[k.p.id]=!0,k.trigger("touch",this.activeTouches[h.identifier]);break}}}},drag:function(a){for(var b=a.changedTouches||[a],c=0;c<b.length;c++){var d=b[c];d.identifier=d.identifier||0;var e=this.activeTouches[d.identifier],f=e&&e.stage;if(e){var g=this.normalizeTouch(d,f);e.x=g.p.px,e.y=g.p.py,e.dx=g.p.ox-e.sx,e.dy=g.p.oy-e.sy,e.obj.trigger("drag",e)}}a.preventDefault()},touchEnd:function(a){for(var b=a.changedTouches||[a],c=0;c<b.length;c++){var d=b[c];d.identifier=d.identifier||0;var e=this.activeTouches[d.identifier];e&&(e.obj.trigger("touchEnd",e),delete this.touchedObjects[e.obj.p.id],this.activeTouches[d.identifier]=null)}a.preventDefault()}}),a.touch=function(d,e){return a.untouch(),c=d||a.SPRITE_UI,b=e||[2,1,0],a._isArray(b)||(b=[b]),a._touch||(a.touchInput=new a.TouchSystem),a},a.untouch=function(){return a.touchInput&&(a.touchInput.destroy(),delete a.touchInput),a}},Quintus.UI=function(a){if(a._isUndefined(Quintus.Touch))throw"Quintus.UI requires Quintus.Touch Module";a.UI={},a.UI.roundRect=function(a,b){a.beginPath(),a.moveTo(-b.cx+b.radius,-b.cy),a.lineTo(-b.cx+b.w-b.radius,-b.cy),a.quadraticCurveTo(-b.cx+b.w,-b.cy,-b.cx+b.w,-b.cy+b.radius),a.lineTo(-b.cx+b.w,-b.cy+b.h-b.radius),a.quadraticCurveTo(-b.cx+b.w,-b.cy+b.h,-b.cx+b.w-b.radius,-b.cy+b.h),a.lineTo(-b.cx+b.radius,-b.cy+b.h),a.quadraticCurveTo(-b.cx,-b.cy+b.h,-b.cx,-b.cy+b.h-b.radius),a.lineTo(-b.cx,-b.cy+b.radius),a.quadraticCurveTo(-b.cx,-b.cy,-b.cx+b.radius,-b.cy),a.closePath()},a.UI.Container=a.Sprite.extend("UI.Container",{init:function(b){var c,d=a._clone(b||{});b&&a._isString(b.w)&&(c=b.w.match(/^[0-9]+%$/))&&(d.w=parseInt(b.w,10)*a.width/100,d.x=a.width/2-d.w/2),b&&a._isString(b.h)&&(c=b.h.match(/^[0-9]+%$/))&&(d.h=parseInt(b.h,10)*a.height/100,d.y=a.height/2-d.h/2),this._super(d,{opacity:1,hidden:!1,fill:null,highlight:null,radius:5,stroke:"#000",border:!1,shadow:!1,shadowColor:!1,type:a.SPRITE_NONE})},insert:function(a){return this.stage.insert(a,this),a},fit:function(a,b){if(0!==this.children.length){void 0===a&&(a=0),void 0===b&&(b=a);for(var c=1/0,d=1/0,e=-1/0,f=-1/0,g=0;g<this.children.length;g++){var h=this.children[g],i=h.p.x-h.p.cx,j=h.p.y-h.p.cy,k=h.p.x-h.p.cx+h.p.w,l=h.p.y-h.p.cy+h.p.h;c>i&&(c=i),d>j&&(d=j),k>e&&(e=k),l>f&&(f=l)}this.p.cx=-c+b,this.p.cy=-d+a,this.p.w=e-c+2*b,this.p.h=f-d+2*a}},addShadow:function(b){if(this.p.shadow){var c=a._isNumber(this.p.shadow)?this.p.shadow:5;b.shadowOffsetX=c,b.shadowOffsetY=c,b.shadowColor=this.p.shadowColor||"rgba(0,0,50,0.1)"}},clearShadow:function(a){a.shadowColor="transparent"},drawRadius:function(b){a.UI.roundRect(b,this.p),this.addShadow(b),b.fill(),this.p.border&&(this.clearShadow(b),b.lineWidth=this.p.border,b.stroke())},drawSquare:function(a){this.addShadow(a),this.p.fill&&a.fillRect(-this.p.cx,-this.p.cy,this.p.w,this.p.h),this.p.border&&(this.clearShadow(a),a.lineWidth=this.p.border,a.strokeRect(-this.p.cx,-this.p.cy,this.p.w,this.p.h))},draw:function(a){return this.p.hidden?!1:((this.p.border||this.p.fill)&&(a.globalAlpha=this.p.opacity,a.fillStyle=1===this.p.frame&&this.p.highlight?this.p.highlight:this.p.fill,a.strokeStyle=this.p.stroke,this.p.radius>0?this.drawRadius(a):this.drawSquare(a)),void 0)}}),a.UI.Text=a.Sprite.extend("UI.Text",{init:function(b,c){this._super(a._defaults(b||{},c),{type:a.SPRITE_UI,size:24}),this.p.label&&this.calcSize()},calcSize:function(){this.setFont(a.ctx),this.splitLabel=this.p.label.split("\n");for(var b="",c=0;c<this.splitLabel.length;c++)this.splitLabel[c].length>b.length&&(b=this.splitLabel[c]);var d=a.ctx.measureText(b);this.p.h=1.2*(this.p.size||24)*this.splitLabel.length,this.p.w=d.width,this.p.cx=this.p.w/2,this.p.cy=this.p.h/2},prerender:function(){this.p.oldLabel!==this.p.label&&(this.p.oldLabel=this.p.label,this.calcSize(),this.el.width=this.p.w,this.el.height=4*this.p.h,this.ctx.clearRect(0,0,this.p.w,this.p.h),this.ctx.fillStyle="#FF0",this.ctx.fillRect(0,0,this.p.w,this.p.h/2),this.setFont(this.ctx),this.ctx.fillText(this.p.label,0,0))},draw:function(a){if(0!==this.p.opacity){this.p.oldLabel!==this.p.label&&this.calcSize(),this.setFont(a),void 0!==this.p.opacity&&(a.globalAlpha=this.p.opacity);for(var b=0;b<this.splitLabel.length;b++)"center"===this.p.align?a.fillText(this.splitLabel[b],0,-this.p.cy+1.2*b*this.p.size):"right"===this.p.align?a.fillText(this.splitLabel[b],this.p.cx,-this.p.cy+1.2*b*this.p.size):a.fillText(this.splitLabel[b],-this.p.cx,-this.p.cy+1.2*b*this.p.size)}},asset:function(){return this.el},setFont:function(a){a.textBaseline="top",a.font=this.font(),a.fillStyle=this.p.color||"black",a.textAlign=this.p.align||"left"},font:function(){return this.fontString?this.fontString:(this.fontString=(this.p.weight||"800")+" "+(this.p.size||24)+"px "+(this.p.family||"Arial"),this.fontString)}}),a.UI.Button=a.UI.Container.extend("UI.Button",{init:function(b,c){if(this._super(a._defaults(b,{type:a.SPRITE_UI|a.SPRITE_DEFAULT})),this.p.label&&(!this.p.w||!this.p.h)){a.ctx.save(),this.setFont(a.ctx);var d=a.ctx.measureText(this.p.label);a.ctx.restore(),this.p.h||(this.p.h=44),this.p.w||(this.p.w=d.width+20)}isNaN(this.p.cx)&&(this.p.cx=this.p.w/2),isNaN(this.p.cy)&&(this.p.cy=this.p.h/2),this.callback=c,this.on("touch",this,"highlight"),this.on("touchEnd",this,"push")},highlight:function(){(!this.sheet()||this.sheet().frames>1)&&(this.p.frame=1)},push:function(){this.p.frame=0,this.callback&&this.callback(),this.trigger("click")},draw:function(b){this._super(b),(this.p.asset||this.p.sheet)&&a.Sprite.prototype.draw.call(this,b),this.p.label&&(b.save(),this.setFont(b),b.fillText(this.p.label,0,0),b.restore())},setFont:function(a){a.textBaseline="middle",a.font=this.p.font||"400 24px arial",a.fillStyle=this.p.fontColor||"black",a.textAlign="center"}}),a.UI.IFrame=a.Sprite.extend("UI.IFrame",{init:function(b){this._super(b,{opacity:1,type:a.SPRITE_UI|a.SPRITE_DEFAULT}),a.wrapper.style.overflow="hidden",this.iframe=document.createElement("IFRAME"),this.iframe.setAttribute("src",this.p.url),this.iframe.style.position="absolute",this.iframe.style.zIndex=500,this.iframe.setAttribute("width",this.p.w),this.iframe.setAttribute("height",this.p.h),this.iframe.setAttribute("frameborder",0),this.p.background&&(this.iframe.style.backgroundColor=this.p.background),a.wrapper.appendChild(this.iframe),this.on("inserted",function(a){this.positionIFrame(),a.on("destroyed",this,"remove")})},positionIFrame:function(){var a=this.p.x,b=this.p.y;this.stage.viewport&&(a-=this.stage.viewport.x,b-=this.stage.viewport.y),(this.oldX!==a||this.oldY!==b||this.oldOpacity!==this.p.opacity)&&(this.iframe.style.top=b-this.p.cy+"px",this.iframe.style.left=a-this.p.cx+"px",this.iframe.style.opacity=this.p.opacity,this.oldX=a,this.oldY=b,this.oldOpacity=this.p.opacity)},step:function(a){this._super(a),this.positionIFrame()},remove:function(){this.iframe&&(a.wrapper.removeChild(this.iframe),this.iframe=null)}}),a.UI.HTMLElement=a.Sprite.extend("UI.HTMLElement",{init:function(b){this._super(b,{opacity:1,type:a.SPRITE_UI}),a.wrapper.style.overflow="hidden",this.el=document.createElement("div"),this.el.innerHTML=this.p.html,a.wrapper.appendChild(this.el),this.on("inserted",function(a){this.position(),a.on("destroyed",this,"remove"),a.on("clear",this,"remove")})},position:function(){},step:function(a){this._super(a),this.position()},remove:function(){this.el&&(a.wrapper.removeChild(this.el),this.el=null)}}),a.UI.VerticalLayout=a.Sprite.extend("UI.VerticalLayout",{init:function(a){this.children=[],this._super(a,{type:0})},insert:function(a){return this.stage.insert(a,this),this.relayout(),a},relayout:function(){for(var a=0,b=0;b<this.children.length;b++)a+=this.children[b].p.h||0;this.p.h-a}})};