From f4029ab90e05b32ac43b1a4871fa4f0c34dd366c Mon Sep 17 00:00:00 2001 From: Kevin Simper Date: Fri, 6 Dec 2013 18:51:06 +0100 Subject: [PATCH 1/2] Add compiled version for bower support --- .gitignore | 4 +- bower.json | 2 +- dist/crafty-min.js | 12 + dist/crafty.js | 12573 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 12588 insertions(+), 3 deletions(-) create mode 100644 dist/crafty-min.js create mode 100644 dist/crafty.js diff --git a/.gitignore b/.gitignore index 454774c9..ddb8cc70 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -crafty.js -crafty-min.js +/crafty.js +/crafty-min.js build/api/* .*.sw? .DS_store diff --git a/bower.json b/bower.json index a714aed5..1ff74172 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "crafty", - "main": "crafty.js", + "main": "dist/crafty.js", "version": "0.6", "repository": { "type": "git", diff --git a/dist/crafty-min.js b/dist/crafty-min.js new file mode 100644 index 00000000..da0d0811 --- /dev/null +++ b/dist/crafty-min.js @@ -0,0 +1,12 @@ +/** + * crafty 0.6.0 + * http://craftyjs.com/ + * + * Copyright 2013, Louis Stowasser + * Dual licensed under the MIT or GPL licenses. + */ + +(function e(t,i,n){function s(o,a){if(!i[o]){if(!t[o]){var h="function"==typeof require&&require;if(!a&&h)return h(o,!0);if(r)return r(o,!0);throw Error("Cannot find module '"+o+"'")}var c=i[o]={exports:{}};t[o][0].call(c.exports,function(e){var i=t[o][1][e];return s(i?i:e)},c,c.exports,e,t,i,n)}return i[o].exports}for(var r="function"==typeof require&&require,o=0;n.length>o;o++)s(n[o]);return s})({1:[function(t){var e=t("./core.js"),i=(window.document,t("./HashMap.js"));e._rectPool=function(){var t=[],e=0;return{get:function(i,n,s,r){e>=t.length&&t.push({});var o=t[e++];return o._x=i,o._y=n,o._w=s,o._h=r,o},copy:function(i){e>=t.length&&t.push({});var n=t[e++];return n._x=i._x,n._y=i._y,n._w=i._w,n._h=i._h,n},recycle:function(){e--}}}(),e.map=new i;var n=Math,s=(n.cos,n.sin,n.PI),r=s/180;e.c("2D",{_x:0,_y:0,_w:0,_h:0,_z:0,_rotation:0,_alpha:1,_visible:!0,_globalZ:null,_origin:null,_mbr:null,_entry:null,_children:null,_parent:null,_changed:!1,_defineGetterSetter_setter:function(){this.__defineSetter__("x",function(t){this._attr("_x",t)}),this.__defineSetter__("y",function(t){this._attr("_y",t)}),this.__defineSetter__("w",function(t){this._attr("_w",t)}),this.__defineSetter__("h",function(t){this._attr("_h",t)}),this.__defineSetter__("z",function(t){this._attr("_z",t)}),this.__defineSetter__("rotation",function(t){this._attr("_rotation",t)}),this.__defineSetter__("alpha",function(t){this._attr("_alpha",t)}),this.__defineSetter__("visible",function(t){this._attr("_visible",t)}),this.__defineGetter__("x",function(){return this._x}),this.__defineGetter__("y",function(){return this._y}),this.__defineGetter__("w",function(){return this._w}),this.__defineGetter__("h",function(){return this._h}),this.__defineGetter__("z",function(){return this._z}),this.__defineGetter__("rotation",function(){return this._rotation}),this.__defineGetter__("alpha",function(){return this._alpha}),this.__defineGetter__("visible",function(){return this._visible}),this.__defineGetter__("parent",function(){return this._parent}),this.__defineGetter__("numChildren",function(){return this._children.length})},_defineGetterSetter_defineProperty:function(){Object.defineProperty(this,"x",{set:function(t){this._attr("_x",t)},get:function(){return this._x},configurable:!0}),Object.defineProperty(this,"y",{set:function(t){this._attr("_y",t)},get:function(){return this._y},configurable:!0}),Object.defineProperty(this,"w",{set:function(t){this._attr("_w",t)},get:function(){return this._w},configurable:!0}),Object.defineProperty(this,"h",{set:function(t){this._attr("_h",t)},get:function(){return this._h},configurable:!0}),Object.defineProperty(this,"z",{set:function(t){this._attr("_z",t)},get:function(){return this._z},configurable:!0}),Object.defineProperty(this,"rotation",{set:function(t){this._attr("_rotation",t)},get:function(){return this._rotation},configurable:!0}),Object.defineProperty(this,"alpha",{set:function(t){this._attr("_alpha",t)},get:function(){return this._alpha},configurable:!0}),Object.defineProperty(this,"visible",{set:function(t){this._attr("_visible",t)},get:function(){return this._visible},configurable:!0})},_defineGetterSetter_fallback:function(){this.x=this._x,this.y=this._y,this.w=this._w,this.h=this._h,this.z=this._z,this.rotation=this._rotation,this.alpha=this._alpha,this.visible=this._visible,this.bind("EnterFrame",function(){if(this.x!==this._x||this.y!==this._y||this.w!==this._w||this.h!==this._h||this.z!==this._z||this.rotation!==this._rotation||this.alpha!==this._alpha||this.visible!==this._visible){var t=e._rectPool.copy(this);if(this.rotation!==this._rotation)this._rotate(this.rotation);else{var i=this._mbr,n=!1;i&&(this.x!==this._x?(i._x-=this.x-this._x,n=!0):this.y!==this._y?(i._y-=this.y-this._y,n=!0):this.w!==this._w?(i._w-=this.w-this._w,n=!0):this.h!==this._h?(i._h-=this.h-this._h,n=!0):this.z!==this._z&&(i._z-=this.z-this._z,n=!0)),n&&this.trigger("Move",t)}this._x=this.x,this._y=this.y,this._w=this.w,this._h=this.h,this._z=this.z,this._rotation=this.rotation,this._alpha=this.alpha,this._visible=this.visible,this.trigger("Change",t),this.trigger("Move",t),e._rectPool.recycle(t)}})},init:function(){this._globalZ=this[0],this._origin={x:0,y:0},this._children=[],e.support.setter?this._defineGetterSetter_setter():e.support.defineProperty?this._defineGetterSetter_defineProperty():this._defineGetterSetter_fallback(),this._entry=e.map.insert(this),this.bind("Move",function(t){var e=this._mbr||this;this._entry.update(e),this._children.length>0&&this._cascade(t)}),this.bind("Rotate",function(t){var e=this._mbr||this;this._entry.update(e),this._children.length>0&&this._cascade(t)}),this.bind("Remove",function(){if(this._children){for(var t=0;this._children.length>t;t++)delete this._children[t]._parent,this._children[t].destroy&&this._children[t].destroy();this._children=[]}this._parent&&this._parent.detach(this),e.map.remove(this),this.detach()})},_calculateMBR:function(t,e,i){if(0===i)return this._mbr=null,void 0;var n=Math.cos(i),s=Math.sin(i);n=1e-10>n&&n>-1e-10?0:n,s=1e-10>s&&s>-1e-10?0:s;var r=t+(this._x-t)*n+(this._y-e)*s,o=e-(this._x-t)*s+(this._y-e)*n,a=t+(this._x+this._w-t)*n+(this._y-e)*s,h=e-(this._x+this._w-t)*s+(this._y-e)*n,c=t+(this._x+this._w-t)*n+(this._y+this._h-e)*s,u=e-(this._x+this._w-t)*s+(this._y+this._h-e)*n,l=t+(this._x-t)*n+(this._y+this._h-e)*s,d=e-(this._x-t)*s+(this._y+this._h-e)*n,_=Math.floor(Math.min(r,a,c,l)),p=Math.floor(Math.min(o,h,u,d)),f=Math.ceil(Math.max(r,a,c,l)),g=Math.ceil(Math.max(o,h,u,d));this._mbr?(this._mbr._x=_,this._mbr._y=p,this._mbr._w=f-_,this._mbr._h=g-p):this._mbr={_x:_,_y:p,_w:f-_,_h:g-p}},_rotate:function(t){var e=-1*(t%360),i=this._rotation-t;if(0!==i){var n=e*r,s={x:this._origin.x+this._x,y:this._origin.y+this._y};this._calculateMBR(s.x,s.y,n);var o=i*r,a=Math.cos(n),h=Math.sin(n);this.trigger("Rotate",{cos:Math.cos(o),sin:Math.sin(o),deg:i,rad:o,o:s,matrix:{M11:a,M12:h,M21:-h,M22:a}})}},area:function(){return this._w*this._h},intersect:function(t,e,i,n){var s,r=this._mbr||this;return s="object"==typeof t?t:{x:t,y:e,w:i,h:n},r._xs.x&&r._ys.y},within:function(t,e,i,n){var s,r=this._mbr||this;return s="object"==typeof t?t:{_x:t,_y:e,_w:i,_h:n},s._x<=r._x&&s._x+s._w>=r._x+r._w&&s._y<=r._y&&s._y+s._h>=r._y+r._h},contains:function(t,e,i,n){var s,r=this._mbr||this;return s="object"==typeof t?t:{_x:t,_y:e,_w:i,_h:n},s._x>=r._x&&s._x+s._w<=r._x+r._w&&s._y>=r._y&&s._y+s._h<=r._y+r._h},pos:function(){return{_x:this._x,_y:this._y,_w:this._w,_h:this._h}},mbr:function(){return this._mbr?{_x:this._mbr._x,_y:this._mbr._y,_w:this._mbr._w,_h:this._mbr._h}:this.pos()},isAt:function(t,e){if(this.mapArea)return this.mapArea.containsPoint(t,e);if(this.map)return this.map.containsPoint(t,e);var i=this._mbr||this;return t>=i._x&&i._x+i._w>=t&&e>=i._y&&i._y+i._h>=e},move:function(t,e){return"n"===t.charAt(0)&&(this.y-=e),"s"===t.charAt(0)&&(this.y+=e),("e"===t||"e"===t.charAt(1))&&(this.x+=e),("w"===t||"w"===t.charAt(1))&&(this.x-=e),this},shift:function(t,e,i,n){return t&&(this.x+=t),e&&(this.y+=e),i&&(this.w+=i),n&&(this.h+=n),this},_cascade:function(t){if(t){var e,i=0,n=this._children,s=n.length;if(t.cos)for(;s>i;++i)e=n[i],"rotate"in e&&e.rotate(t);else for(var r=this._x-t._x,o=this._y-t._y,a=this._w-t._w,h=this._h-t._h;s>i;++i)e=n[i],e.shift(r,o,a,h)}},attach:function(){for(var t,e=0,i=arguments,n=arguments.length;n>e;++e)t=i[e],t._parent&&t._parent.detach(t),t._parent=this,this._children.push(t);return this},detach:function(t){var e;if(!t){for(e=0;this._children.length>e;e++)this._children[e]._parent=null;return this._children=[],this}for(e=0;this._children.length>e;e++)this._children[e]==t&&this._children.splice(e,1);return t._parent=null,this},origin:function(t,e){if("string"==typeof t)if("centre"===t||"center"===t||-1===t.indexOf(" "))t=this._w/2,e=this._h/2;else{var i=t.split(" ");"top"===i[0]?e=0:"bottom"===i[0]?e=this._h:("middle"===i[0]||"center"===i[1]||"centre"===i[1])&&(e=this._h/2),"center"===i[1]||"centre"===i[1]||"middle"===i[1]?t=this._w/2:"left"===i[1]?t=0:"right"===i[1]&&(t=this._w)}return this._origin.x=t,this._origin.y=e,this},flip:function(t){return t=t||"X",this["_flip"+t]||(this["_flip"+t]=!0,this.trigger("Change")),this},unflip:function(t){return t=t||"X",this["_flip"+t]&&(this["_flip"+t]=!1,this.trigger("Change")),this},rotate:function(t){var e,i;e=(this._x+this._origin.x-t.o.x)*t.cos+(this._y+this._origin.y-t.o.y)*t.sin+(t.o.x-this._origin.x),i=(this._y+this._origin.y-t.o.y)*t.cos-(this._x+this._origin.x-t.o.x)*t.sin+(t.o.y-this._origin.y),this._attr("_rotation",this._rotation-t.deg),this._attr("_x",e),this._attr("_y",i)},_attr:function(t,i){if(this[t]!==i){var n,s=e._rectPool.copy(this);if("_rotation"===t)this._rotate(i);else if("_z"===t)this._globalZ=parseInt(i+e.zeroFill(this[0],5),10),this.trigger("reorder");else if("_x"===t||"_y"===t)n=this._mbr,n&&(n[t]-=this[t]-i),this[t]=i,this.trigger("Move",s);else if("_h"===t||"_w"===t){n=this._mbr;var o=this[t];this[t]=i,n&&this._calculateMBR(this._origin.x+this._x,this._origin.y+this._y,-this._rotation*r),"_w"===t?this.trigger("Resize",{axis:"w",amount:i-o}):"_h"===t&&this.trigger("Resize",{axis:"h",amount:i-o}),this.trigger("Move",s)}this[t]=i,this.trigger("Change",s),e._rectPool.recycle(s)}}}),e.c("Gravity",{_gravityConst:.2,_gy:0,_falling:!0,_anti:null,init:function(){this.requires("2D")},gravity:function(t){return t&&(this._anti=t),isNaN(this._jumpSpeed)&&(this._jumpSpeed=0),this.bind("EnterFrame",this._enterFrame),this},gravityConst:function(t){return this._gravityConst=t,this},_enterFrame:function(){this._falling?(this._gy+=this._gravityConst,this.y+=this._gy):this._gy=0;var t,i,n,s=!1,r=this.pos(),o=0;for(r._y++,r.x=r._x,r.y=r._y,r.w=r._w,r.h=r._h,i=e.map.search(r),n=i.length;n>o;++o)if(t=i[o],t!==this&&t.has(this._anti)&&t.intersect(r)){s=t;break}s?this._falling&&(this._gy>this._jumpSpeed||!this._up)&&this.stopFalling(s):this._falling=!0},stopFalling:function(t){t&&(this.y=t._y-this._h),this._falling=!1,this._up&&(this._up=!1),this.trigger("hit")},antigravity:function(){this.unbind("EnterFrame",this._enterFrame)}}),e.polygon=function(t){arguments.length>1&&(t=Array.prototype.slice.call(arguments,0)),this.points=t},e.polygon.prototype={containsPoint:function(t,e){var i,n,s=this.points,r=!1;for(i=0,n=s.length-1;s.length>i;n=i++)s[i][1]>e!=s[n][1]>e&&(s[n][0]-s[i][0])*(e-s[i][1])/(s[n][1]-s[i][1])+s[i][0]>t&&(r=!r);return r},shift:function(t,e){for(var i,n=0,s=this.points.length;s>n;n++)i=this.points[n],i[0]+=t,i[1]+=e},rotate:function(t){for(var e,i,n,s=0,r=this.points.length;r>s;s++)e=this.points[s],i=t.o.x+(e[0]-t.o.x)*t.cos+(e[1]-t.o.y)*t.sin,n=t.o.y-(e[0]-t.o.x)*t.sin+(e[1]-t.o.y)*t.cos,e[0]=i,e[1]=n}},e.circle=function(t,e,i){this.x=t,this.y=e,this.radius=i,this.points=[];for(var n,s=0;8>s;s++)n=s*Math.PI/4,this.points[s]=[this.x+Math.sin(n)*i,this.y+Math.cos(n)*i]},e.circle.prototype={containsPoint:function(t,e){var i=this.radius,n=(Math.sqrt,this.x-t),s=this.y-e;return i*i>n*n+s*s},shift:function(t,e){this.x+=t,this.y+=e;for(var i,n=0,s=this.points.length;s>n;n++)i=this.points[n],i[0]+=t,i[1]+=e},rotate:function(){}},e.matrix=function(t){this.mtx=t,this.width=t[0].length,this.height=t.length},e.matrix.prototype={x:function(t){if(this.width==t.height){for(var i=[],n=0;this.height>n;n++){i[n]=[];for(var s=0;t.width>s;s++){for(var r=0,o=0;this.width>o;o++)r+=this.mtx[n][o]*t.mtx[o][s];i[n][s]=r}}return new e.matrix(i)}},e:function(t,e){return 1>t||t>this.mtx.length||1>e||e>this.mtx[0].length?null:this.mtx[t-1][e-1]}}},{"./HashMap.js":4,"./core.js":9}],2:[function(t){var e=t("./core.js"),i=window.document;e.c("DOM",{_element:null,_cssStyles:null,avoidCss3dTransforms:!1,init:function(){function t(){var t=0,e=this.__c,i="";for(t in e)i+=" "+t;i=i.substr(1),this._element.className=i}this._cssStyles={visibility:"",left:"",top:"",width:"",height:"",zIndex:"",opacity:"",transformOrigin:"",transform:""},this._element=i.createElement("div"),e.stage.inner.appendChild(this._element),this._element.style.position="absolute",this._element.id="ent"+this[0],this.bind("Change",function(){this._changed||(this._changed=!0,e.DrawManager.addDom(this))}),this.bind("NewComponent",t).bind("RemoveComponent",t),"ms"===e.support.prefix&&9>e.support.version&&(this._filters={},this.bind("Rotate",function(t){var e=t.matrix,i=(this._element.style,e.M11.toFixed(8)),n=e.M12.toFixed(8),s=e.M21.toFixed(8),r=e.M22.toFixed(8);this._filters.rotation="progid:DXImageTransform.Microsoft.Matrix(M11="+i+", M12="+n+", M21="+s+", M22="+r+",sizingMethod='auto expand')"})),this.bind("Remove",this.undraw),this.bind("RemoveComponent",function(t){"DOM"===t&&this.undraw()})},getDomId:function(){return this._element.id},DOM:function(t){return t&&t.nodeType&&(this.undraw(),this._element=t,this._element.style.position="absolute"),this},draw:function(){var t=this._element.style,i=this.__coord||[0,0,0,0],n={x:i[0],y:i[1],w:i[2],h:i[3]},s=e.support.prefix,r=[];if(this._cssStyles.visibility!==this._visible&&(this._cssStyles.visibility=this._visible,t.visibility=this._visible?"visible":"hidden"),e.support.css3dtransform&&!this.avoidCss3dTransforms?r.push("translate3d("+~~this._x+"px,"+~~this._y+"px,0)"):(this._cssStyles.left!==this._x&&(this._cssStyles.left=this._x,t.left=~~this._x+"px"),this._cssStyles.top!==this._y&&(this._cssStyles.top=this._y,t.top=~~this._y+"px")),this._cssStyles.width!==this._w&&(this._cssStyles.width=this._w,t.width=~~this._w+"px"),this._cssStyles.height!==this._h&&(this._cssStyles.height=this._h,t.height=~~this._h+"px"),this._cssStyles.zIndex!==this._z&&(this._cssStyles.zIndex=this._z,t.zIndex=this._z),this._cssStyles.opacity!==this._alpha&&(this._cssStyles.opacity=this._alpha,t.opacity=this._alpha,t[s+"Opacity"]=this._alpha),"ms"===s&&9>e.support.version&&(this._filters.alpha=8===e.support.version?"progid:DXImageTransform.Microsoft.Alpha(Opacity="+100*this._alpha+")":"alpha(opacity="+100*this._alpha+")"),this._mbr){var o=this._origin.x+"px "+this._origin.y+"px";t.transformOrigin=o,t[s+"TransformOrigin"]=o,e.support.css3dtransform?r.push("rotateZ("+this._rotation+"deg)"):r.push("rotate("+this._rotation+"deg)")}return this._flipX&&(r.push("scaleX(-1)"),"ms"===s&&9>e.support.version&&(this._filters.flipX="fliph")),this._flipY&&(r.push("scaleY(-1)"),"ms"===s&&9>e.support.version&&(this._filters.flipY="flipv")),"ms"===s&&9>e.support.version&&this.applyFilters(),this._cssStyles.transform!=r.join(" ")&&(this._cssStyles.transform=r.join(" "),t.transform=this._cssStyles.transform,t[s+"Transform"]=this._cssStyles.transform),this.trigger("Draw",{style:t,type:"DOM",co:n}),this},applyFilters:function(){this._element.style.filter="";var t="";for(var e in this._filters)this._filters.hasOwnProperty(e)&&(t+=this._filters[e]+" ");this._element.style.filter=t},undraw:function(){return this._element&&e.stage.inner.removeChild(this._element),this},css:function(t,i){var n,s,r=this._element,o=r.style;if("object"==typeof t)for(n in t)t.hasOwnProperty(n)&&(s=t[n],"number"==typeof s&&(s+="px"),o[e.DOM.camelize(n)]=s);else{if(!i)return e.DOM.getStyle(r,t);"number"==typeof i&&(i+="px"),o[e.DOM.camelize(t)]=i}return this.trigger("Change"),this}});try{i.execCommand("BackgroundImageCache",!1,!0)}catch(n){}e.extend({DOM:{window:{init:function(){this.width=window.innerWidth||window.document.documentElement.clientWidth||window.document.body.clientWidth,this.height=window.innerHeight||window.document.documentElement.clientHeight||window.document.body.clientHeight,e.unbind("RenderScene",e.DrawManager.renderDOM),e.bind("RenderScene",e.DrawManager.renderDOM)},width:0,height:0},inner:function(t){var e=t.getBoundingClientRect(),n=e.left+(window.pageXOffset?window.pageXOffset:i.body.scrollLeft),s=e.top+(window.pageYOffset?window.pageYOffset:i.body.scrollTop),r=parseInt(this.getStyle(t,"border-left-width")||0,10)||parseInt(this.getStyle(t,"borderLeftWidth")||0,10)||0,o=parseInt(this.getStyle(t,"border-top-width")||0,10)||parseInt(this.getStyle(t,"borderTopWidth")||0,10)||0;return n+=r,s+=o,{x:n,y:s}},getStyle:function(t,e){var n;return t.currentStyle?n=t.currentStyle[this.camelize(e)]:window.getComputedStyle&&(n=i.defaultView.getComputedStyle(t,null).getPropertyValue(this.csselize(e))),n},camelize:function(t){return t.replace(/-+(.)?/g,function(t,e){return e?e.toUpperCase():""})},csselize:function(t){return t.replace(/[A-Z]/g,function(t){return t?"-"+t.toLowerCase():""})},translate:function(t,n){return{x:(t-e.stage.x+i.body.scrollLeft+i.documentElement.scrollLeft-e.viewport._x)/e.viewport._scale,y:(n-e.stage.y+i.body.scrollTop+i.documentElement.scrollTop-e.viewport._y)/e.viewport._scale}}}})},{"./core.js":9}],3:[function(t){var e=t("./core.js"),i=window.document;e.c("DebugCanvas",{init:function(){this.requires("2D"),e.DebugCanvas.context||e.DebugCanvas.init(),e.DebugCanvas.add(this),this._debug={alpha:1,lineWidth:1},this.bind("RemoveComponent",this.onDebugRemove),this.bind("Remove",this.onDebugDestroy)},onDebugRemove:function(t){"DebugCanvas"===t&&e.DebugCanvas.remove(this)},onDebugDestroy:function(){e.DebugCanvas.remove(this)},debugAlpha:function(t){return this._debug.alpha=t,this},debugFill:function(t){return t===void 0&&(t="red"),this._debug.fillStyle=t,this},debugStroke:function(t){return t===void 0&&(t="red"),this._debug.strokeStyle=t,this},debugDraw:function(t){var e=t.globalAlpha,i=this._debug;i.alpha&&(t.globalAlpha=this._debug.alpha),i.strokeStyle&&(t.strokeStyle=i.strokeStyle),i.lineWidth&&(t.lineWidth=i.lineWidth),i.fillStyle&&(t.fillStyle=i.fillStyle),this.trigger("DebugDraw"),t.globalAlpha=e}}),e.c("DebugRectangle",{init:function(){this.requires("2D, DebugCanvas")},debugRectangle:function(t){return this.debugRect=t,this.unbind("DebugDraw",this.drawDebugRect),this.bind("DebugDraw",this.drawDebugRect),this},drawDebugRect:function(){ctx=e.DebugCanvas.context;var t=this.debugRect;null!==t&&void 0!==t&&t._h&&t._w&&(this._debug.fillStyle&&ctx.fillRect(t._x,t._y,t._w,t._h),this._debug.strokeStyle&&ctx.strokeRect(t._x,t._y,t._w,t._h))}}),e.c("VisibleMBR",{init:function(){this.requires("DebugRectangle").debugFill("purple").bind("EnterFrame",this._assignRect)},_assignRect:function(){this._mbr?this.debugRectangle(this._mbr):this.debugRectangle(this)}}),e.c("DebugPolygon",{init:function(){this.requires("2D, DebugCanvas")},debugPolygon:function(t){return this.polygon=t,this.unbind("DebugDraw",this.drawDebugPolygon),this.bind("DebugDraw",this.drawDebugPolygon),this},drawDebugPolygon:function(){if(void 0!==this.polygon){ctx=e.DebugCanvas.context,ctx.beginPath();for(var t in this.polygon.points)ctx.lineTo(this.polygon.points[t][0],this.polygon.points[t][1]);ctx.closePath(),this._debug.fillStyle&&ctx.fill(),this._debug.strokeStyle&&ctx.stroke()}}}),e.c("WiredHitBox",{init:function(){this.requires("DebugPolygon").debugStroke("red").matchHitBox(),this.bind("NewHitbox",this.matchHitBox)},matchHitBox:function(){this.debugPolygon(this.map)}}),e.c("SolidHitBox",{init:function(){this.requires("Collision, DebugPolygon").debugFill("orange").debugAlpha(.7).matchHitBox(),this.bind("NewHitbox",this.matchHitBox)},matchHitBox:function(){this.debugPolygon(this.map)}}),e.DebugCanvas={context:null,entities:[],onetimeEntities:[],add:function(t){this.entities.push(t)},remove:function(t){for(var e=this.entities,i=e.length-1;i>=0;i--)e[i]==t&&e.splice(i,1)},init:function(){if(!e.DebugCanvas.context){if(!e.support.canvas)return e.trigger("NoCanvas"),e.stop(),void 0;var t;t=i.createElement("canvas"),t.width=e.viewport.width,t.height=e.viewport.height,t.style.position="absolute",t.style.left="0px",t.style.top="0px",t.id="debug-canvas",t.style.zIndex=1e5,e.stage.elem.appendChild(t),e.DebugCanvas.context=t.getContext("2d"),e.DebugCanvas._canvas=t}e.unbind("RenderScene",e.DebugCanvas.renderScene),e.bind("RenderScene",e.DebugCanvas.renderScene)},renderScene:function(t){t=t||e.viewport.rect();var i,n=e.DebugCanvas.entities,s=0,r=n.length,o=e.DebugCanvas.context,a=e.viewport;for(o.setTransform(a._scale,0,0,a._scale,a._x,a._y),o.clearRect(t._x,t._y,t._w,t._h);r>s;s++)i=n[s],i.debugDraw(o)}}},{"./core.js":9}],4:[function(t,e){function i(t,e,i){this.keys=t,this.map=i,this.obj=e}t("./core.js"),window.document;var n,s=function(t){n=t||64,this.map={}},r=" ",o={};s.prototype={insert:function(t){var e,n,r=s.key(t),o=new i(r,t,this),a=0;for(a=r.x1;r.x2>=a;a++)for(e=r.y1;r.y2>=e;e++)n=a<<16^e,this.map[n]||(this.map[n]=[]),this.map[n].push(t);return o},search:function(t,e){var i,n,r,a=s.key(t,o),h=[];for(void 0===e&&(e=!0),i=a.x1;a.x2>=i;i++)for(n=a.y1;a.y2>=n;n++)if(cell=this.map[i<<16^n])for(r=0;cell.length>r;r++)h.push(cell[r]);if(e){var c,u,d=[],_={};for(i=0,l=h.length;l>i;i++)c=h[i],c&&(u=c[0],c=c._mbr||c,!_[u]&&c._xt._x&&c._yt._y&&(_[u]=h[i]));for(c in _)d.push(_[c]);return d}return h},remove:function(t,e){var i,n,r=0;for(1==arguments.length&&(e=t,t=s.key(e,o)),r=t.x1;t.x2>=r;r++)for(i=t.y1;t.y2>=i;i++)if(n=r<<16^i,this.map[n]){var a,h=this.map[n],c=h.length;for(a=0;c>a;a++)h[a]&&h[a][0]===e[0]&&h.splice(a,1)}},refresh:function(t){var e,i,n,r,o,a=t.keys,h=t.obj;for(i=a.x1;a.x2>=i;i++)for(n=a.y1;a.y2>=n;n++)if(e=this.map[i<<16^n])for(o=e.length,r=0;o>r;r++)e[r]&&e[r][0]===h[0]&&e.splice(r,1);for(s.key(h,a),i=a.x1;a.x2>=i;i++)for(n=a.y1;a.y2>=n;n++)e=this.map[i<<16^n],e||(e=this.map[i<<16^n]=[]),e.push(h);return t},boundaries:function(){var t,e,i={max:{x:-1/0,y:-1/0},min:{x:1/0,y:1/0}},n={max:{x:-1/0,y:-1/0},min:{x:1/0,y:1/0}};for(var s in this.map)if(this.map[s].length){var r=s>>16,o=s<<16>>16;if(0>o&&(r=-1^r),r>=i.max.x){i.max.x=r;for(t in this.map[s])e=this.map[s][t],"object"==typeof e&&"requires"in e&&(n.max.x=Math.max(n.max.x,e.x+e.w))}if(i.min.x>=r){i.min.x=r;for(t in this.map[s])e=this.map[s][t],"object"==typeof e&&"requires"in e&&(n.min.x=Math.min(n.min.x,e.x))}if(o>=i.max.y){i.max.y=o;for(t in this.map[s])e=this.map[s][t],"object"==typeof e&&"requires"in e&&(n.max.y=Math.max(n.max.y,e.y+e.h))}if(i.min.y>=o){i.min.y=o;for(t in this.map[s])e=this.map[s][t],"object"==typeof e&&"requires"in e&&(n.min.y=Math.min(n.min.y,e.y))}}return n}},s.key=function(t,e){return t._mbr&&(t=t._mbr),e||(e={}),e.x1=Math.floor(t._x/n),e.y1=Math.floor(t._y/n),e.x2=Math.floor((t._w+t._x)/n),e.y2=Math.floor((t._h+t._y)/n),e},s.hash=function(t){return t.x1+r+t.y1+r+t.x2+r+t.y2},i.prototype={update:function(t){s.hash(s.key(t,o))!=s.hash(this.keys)&&this.map.refresh(this)}},e.exports=s},{"./core.js":9}],5:[function(t){var e=t("./core.js");window.document,e.easing=function(t){this.timePerFrame=1e3/e.timer.FPS(),this.duration=t,this.reset()},e.easing.prototype={duration:0,clock:0,steps:null,complete:!1,paused:!1,reset:function(){this.loops=1,this.clock=0,this.complete=!1,this.paused=!1},repeat:function(t){this.loops=t},setProgress:function(t,e){this.clock=this.duration*t,e!==void 0&&(this.loops=e)},pause:function(){this.paused=!0},resume:function(){this.paused=!1,this.complete=!1},tick:function(t){if(!this.paused&&!this.complete)for(this.clock+=t,this.frames=Math.floor(this.clock/this.timePerFrame);this.clock>=this.duration&&this.complete===!1;)this.loops--,this.loops>0?this.clock-=this.duration:this.complete=!0},time:function(){return Math.min(this.clock/this.duration,1)},value:function(){return this.time()}},e.c("SpriteAnimation",{_reels:null,_currentReelId:null,_currentReel:null,_isPlaying:!1,init:function(){this._reels={}},reel:function(t,i,n,s,r){if(0===arguments.length)return this._currentReelId;if(1===arguments.length&&"string"==typeof t){if(this._reels[t]===void 0)throw"The specified reel "+t+" is undefined.";return this.pauseAnimation(),this._currentReelId!==t&&(this._currentReelId=t,this._currentReel=this._reels[t],this._updateSprite(),this.trigger("ReelChange",this._currentReel)),this}var o,a,h,c,u;if(h=this.__tile+parseInt(this.__padding[0]||0,10),c=this.__tileh+parseInt(this.__padding[1]||0,10),o={id:t,frames:[],currentFrame:0,easing:new e.easing(i),defaultLoops:1},o.duration=o.easing.duration,"number"==typeof n)if(a=n,y=s,r>=0)for(;n+r>a;a++)o.frames.push([a*h,y*c]);else for(;a>n+r;a--)o.frames.push([a*h,y*c]);else{if(3!==arguments.length||"object"!=typeof n)throw"Urecognized arguments. Please see the documentation for 'reel(...)'.";for(a=0,toX=n.length-1;toX>=a;a++)u=n[a],o.frames.push([u[0]*h,u[1]*c])}return this._reels[t]=o,this},animate:function(t,e){"string"==typeof t&&this.reel(t);var i=this._currentReel;if(i===void 0||null===i)throw"No reel is specified, and there is no currently active reel.";return this.pauseAnimation(),e===void 0&&(e="number"==typeof t?t:1),i.easing.reset(),this.loops(e),this._setFrame(0),this.bind("EnterFrame",this._animationTick),this._isPlaying=!0,this.trigger("StartAnimation",i),this},resumeAnimation:function(){return this._isPlaying===!1&&null!==this._currentReel&&(this.bind("EnterFrame",this._animationTick),this._isPlaying=!0,this._currentReel.easing.resume(),this.trigger("StartAnimation",this._currentReel)),this},pauseAnimation:function(){return this._isPlaying===!0&&(this.unbind("EnterFrame",this._animationTick),this._isPlaying=!1,this._reels[this._currentReelId].easing.pause()),this},resetAnimation:function(){var t=this._currentReel;if(null===t)throw"No active reel to reset.";return this.reelPosition(0),t.easing.repeat(t.defaultLoops),this},loops:function(t){return 0===arguments.length?null!==this._currentReel?this._currentReel.easing.loops:0:(null!==this._currentReel&&(0>t&&(t=1/0),this._currentReel.easing.repeat(t),this._currentReel.defaultLoops=t),this)},reelPosition:function(t){if(null===this._currentReel)throw"No active reel.";if(0===arguments.length)return this._currentReel.currentFrame;var e,i=this._currentReel.frames.length;if("end"===t&&(t=i-1),1>t&&t>0)e=t,t=Math.floor(i*e);else{if(t!==Math.floor(t))throw"Position "+t+" is invalid.";0>t&&(t=i-1+t),e=t/i}return t=Math.min(t,i-1),t=Math.max(t,0),this._setProgress(e),this._setFrame(t),this},_animationTick:function(t){var e=this._reels[this._currentReelId];e.easing.tick(t.dt);var i=e.easing.value(),n=Math.min(Math.floor(e.frames.length*i),e.frames.length-1);this._setFrame(n),e.easing.complete===!0&&(this.trigger("AnimationEnd",this._currentReel),this.pauseAnimation())},_setFrame:function(t){var e=this._currentReel;t!==e.currentFrame&&(e.currentFrame=t,this._updateSprite(),this.trigger("FrameChange",e))},_updateSprite:function(){var t=this._currentReel,e=t.frames[t.currentFrame];this.__coord[0]=e[0],this.__coord[1]=e[1],this.trigger("Change")},_setProgress:function(t,e){this._currentReel.easing.setProgress(t,e)},isPlaying:function(t){return this._isPlaying?t?this._currentReelId===t:!!this._currentReelId:!1},getReel:function(t){if(0===arguments.length){if(!this._currentReelId)return null;t=this._currentReelId}return this._reels[t]}}),e.c("Tween",{init:function(){this.tweenGroup={},this.tweenStart={},this.tweens=[],this.bind("EnterFrame",this._tweenTick)},_tweenTick:function(t){var e,i,n;for(n=this.tweens.length-1;n>=0;n--)e=this.tweens[n],e.easing.tick(t.dt),i=e.easing.value(),this._doTween(e.props,i),e.easing.complete&&(this.tweens.splice(n,1),this._endTween(e.props))},_doTween:function(t,e){for(var i in t)this[i]=(1-e)*this.tweenStart[i]+e*t[i]},tween:function(t,i){var n={props:t,easing:new e.easing(i)};for(var s in t)this.tweenGroup[s]!==void 0&&this.cancelTween(s),this.tweenStart[s]=this[s],this.tweenGroup[s]=t;return this.tweens.push(n),this},cancelTween:function(t){if("string"==typeof t)"object"==typeof this.tweenGroup[t]&&delete this.tweenGroup[t][t];else if("object"==typeof t)for(var e in t)this.cancelTween(e);return this},_endTween:function(t){for(var e in t)delete this.tweenGroup[e];this.trigger("TweenEnd",t)}})},{"./core.js":9}],6:[function(t){var e=t("./core.js"),i=window.document;e.c("Canvas",{init:function(){e.canvas.context||e.canvas.init(),e.DrawManager.total2D++,this.currentRect={},this._changed=!0,e.DrawManager.addCanvas(this),this.bind("Change",function(){this._changed===!1&&(this._changed=!0,e.DrawManager.addCanvas(this))}),this.bind("Remove",function(){e.DrawManager.total2D--,this._changed=!0,e.DrawManager.addCanvas(this)})},drawVars:{type:"canvas",pos:{},ctx:null,coord:[0,0,0,0],co:{x:0,y:0,w:0,h:0}},draw:function(t,i,n,s,r){if(this.ready){4===arguments.length&&(r=s,s=n,n=i,i=t,t=e.canvas.context);var o=this.drawVars.pos;o._x=this._x+(i||0),o._y=this._y+(n||0),o._w=s||this._w,o._h=r||this._h,context=t||e.canvas.context,coord=this.__coord||[0,0,0,0];var a=this.drawVars.co;a.x=coord[0]+(i||0),a.y=coord[1]+(n||0),a.w=s||coord[2],a.h=r||coord[3],this._mbr&&(context.save(),context.translate(this._origin.x+this._x,this._origin.y+this._y),o._x=-this._origin.x,o._y=-this._origin.y,context.rotate(this._rotation%360*(Math.PI/180))),(this._flipX||this._flipY)&&(context.save(),context.scale(this._flipX?-1:1,this._flipY?-1:1),this._flipX&&(o._x=-(o._x+o._w)),this._flipY&&(o._y=-(o._y+o._h)));var h;return 1>this._alpha&&(h=context.globalAlpha,context.globalAlpha=this._alpha),this.drawVars.ctx=context,this.trigger("Draw",this.drawVars),(this._mbr||this._flipX||this._flipY)&&context.restore(),h&&(context.globalAlpha=h),this}}}),e.extend({canvas:{context:null,init:function(){if(!e.support.canvas)return e.trigger("NoCanvas"),e.stop(),void 0;var t;t=i.createElement("canvas"),t.width=e.viewport.width,t.height=e.viewport.height,t.style.position="absolute",t.style.left="0px",t.style.top="0px",e.stage.elem.appendChild(t),e.canvas.context=t.getContext("2d"),e.canvas._canvas=t;var n=e.viewport._scale;1!=n&&e.canvas.context.scale(n,n),e.unbind("RenderScene",e.DrawManager.renderCanvas),e.bind("RenderScene",e.DrawManager.renderCanvas)}}})},{"./core.js":9}],7:[function(t){var e=t("./core.js"),i=(window.document,Math.PI/180);e.c("Collision",{init:function(){this.requires("2D"),this._mbr||this,this.collision()},collision:function(t){if(this.unbind("Resize",this._resizeMap),t||(t=new e.polygon([0,0],[this._w,0],[this._w,this._h],[0,this._h]),this.bind("Resize",this._resizeMap)),arguments.length>1){var n=Array.prototype.slice.call(arguments,0);t=new e.polygon(n)}return this.rotation&&t.rotate({cos:Math.cos(-this.rotation*i),sin:Math.sin(-this.rotation*i),o:{x:this._origin.x,y:this._origin.y}}),this.map=t,this.attach(this.map),this.map.shift(this._x,this._y),this.trigger("NewHitbox",t),this},_resizeMap:function(t){var e,n,s=this.rotation*i,r=this.map.points;"w"===t.axis?(s?(e=t.amount*Math.cos(s),n=t.amount*Math.sin(s)):(e=t.amount,n=0),r[1][0]+=e,r[1][1]+=n):(s?(n=t.amount*Math.cos(s),e=-t.amount*Math.sin(s)):(e=0,n=t.amount),r[3][0]+=e,r[3][1]+=n),r[2][0]+=e,r[2][1]+=n},hit:function(t){var i,n,s,r,o=this._mbr||this,a=e.map.search(o,!1),h=0,c=a.length,u={},l="map"in this&&"containsPoint"in this.map,d=[];if(!c)return!1;for(;c>h;++h)n=a[h],s=n._mbr||n,n&&(i=n[0],!u[i]&&this[0]!==i&&n.__c[t]&&s._xo._x&&s._yo._y&&(u[i]=n));for(r in u)if(n=u[r],l&&"map"in n){var _=this._SAT(this.map,n.map);_.obj=n,_.type="SAT",_&&d.push(_)}else d.push({obj:n,type:"MBR"});return d.length?d:!1},onHit:function(t,e,i){var n=!1;return this.bind("EnterFrame",function(){var s=this.hit(t);s?(n=!0,e.call(this,s)):n&&("function"==typeof i&&i.call(this),n=!1)}),this},_SAT:function(t,e){for(var i,n,s,r,o,a,h,c,u,l,d=t.points,_=e.points,p=0,f=d.length,g=_.length,m={x:0,y:0},v=null,y=null,x=null;f>p;p++){for(u=d[p==f-1?0:p+1],l=d[p],m.x=-(u[1]-l[1]),m.y=u[0]-l[0],n=Math.sqrt(m.x*m.x+m.y*m.y),m.x/=n,m.y/=n,s=r=-1,o=a=-1,i=0;f>i;++i)c=d[i][0]*m.x+d[i][1]*m.y,(c>o||-1===o)&&(o=c),(s>c||-1===s)&&(s=c);for(i=0;g>i;++i)c=_[i][0]*m.x+_[i][1]*m.y,(c>a||-1===a)&&(a=c),(r>c||-1===r)&&(r=c);if(r>s?(h=r-o,m.x=-m.x,m.y=-m.y):h=s-a,h>=0)return!1;(null===v||h>v)&&(v=h,x={x:m.x,y:m.y})}for(p=0;g>p;p++){for(u=_[p==g-1?0:p+1],l=_[p],m.x=-(u[1]-l[1]),m.y=u[0]-l[0],n=Math.sqrt(m.x*m.x+m.y*m.y),m.x/=n,m.y/=n,s=r=-1,o=a=-1,i=0;f>i;++i)c=d[i][0]*m.x+d[i][1]*m.y,(c>o||-1===o)&&(o=c),(s>c||-1===s)&&(s=c);for(i=0;g>i;++i)c=_[i][0]*m.x+_[i][1]*m.y,(c>a||-1===a)&&(a=c),(r>c||-1===r)&&(r=c); +if(r>s?(h=r-o,m.x=-m.x,m.y=-m.y):h=s-a,h>=0)return!1;(null===v||h>v)&&(v=h),(h>y||null===y)&&(y=h,x={x:m.x,y:m.y})}return{overlap:y,normal:x}}})},{"./core.js":9}],8:[function(t){var e=t("./core.js"),i=window.document;e.extend({over:null,mouseObjs:0,mousePos:{},lastEvent:null,keydown:{},selected:!1,detectBlur:function(t){var i=t.clientX>e.stage.x&&t.clientXe.stage.y&&t.clientYt.button?e.mouseButtons.LEFT:4==t.button?e.mouseButtons.MIDDLE:e.mouseButtons.RIGHT:2>t.which?e.mouseButtons.LEFT:2==t.which?e.mouseButtons.MIDDLE:e.mouseButtons.RIGHT,t.realX=r=e.mousePos.x=c.x,t.realY=o=e.mousePos.y=c.y,"CANVAS"!=l.nodeName){for(;"string"!=typeof l.id&&-1==l.id.indexOf("ent");)l=l.parentNode;ent=e(parseInt(l.id.replace("ent",""),10)),ent.has("Mouse")&&ent.isAt(r,o)&&(i=ent)}if(!i)for(n=e.map.search({_x:r,_y:o,_w:1,_h:1},!1),s=n.length;s>h;++h)if(n[h].__c.Mouse&&n[h]._visible){var _=n[h],p=!1;if(!u[_[0]]&&(u[_[0]]=!0,_.mapArea?_.mapArea.containsPoint(r,o)&&(p=!0):_.isAt(r,o)&&(p=!0),p&&(_._z>=a||-1===a))){if(_._z===a&&_[0]=112&&135>=t.key?void 0:(t.stopPropagation?t.stopPropagation():t.cancelBubble=!0,t.target&&"INPUT"!==t.target.nodeName&&"TEXTAREA"!==t.target.nodeName&&(t.preventDefault?t.preventDefault():t.returnValue=!1),!1)}}),e.bind("Load",function(){e.addEvent(this,"keydown",e.keyboardDispatch),e.addEvent(this,"keyup",e.keyboardDispatch),e.addEvent(this,e.stage.elem,"mousedown",e.mouseDispatch),e.addEvent(this,e.stage.elem,"mouseup",e.mouseDispatch),e.addEvent(this,i.body,"mouseup",e.detectBlur),e.addEvent(this,e.stage.elem,"mousemove",e.mouseDispatch),e.addEvent(this,e.stage.elem,"click",e.mouseDispatch),e.addEvent(this,e.stage.elem,"dblclick",e.mouseDispatch),e.addEvent(this,e.stage.elem,"touchstart",e.touchDispatch),e.addEvent(this,e.stage.elem,"touchmove",e.touchDispatch),e.addEvent(this,e.stage.elem,"touchend",e.touchDispatch),e.addEvent(this,e.stage.elem,"touchcancel",e.touchDispatch),e.addEvent(this,e.stage.elem,"touchleave",e.touchDispatch)}),e.bind("CraftyStop",function(){e.removeEvent(this,"keydown",e.keyboardDispatch),e.removeEvent(this,"keyup",e.keyboardDispatch),e.stage&&(e.removeEvent(this,e.stage.elem,"mousedown",e.mouseDispatch),e.removeEvent(this,e.stage.elem,"mouseup",e.mouseDispatch),e.removeEvent(this,e.stage.elem,"mousemove",e.mouseDispatch),e.removeEvent(this,e.stage.elem,"click",e.mouseDispatch),e.removeEvent(this,e.stage.elem,"dblclick",e.mouseDispatch),e.removeEvent(this,e.stage.elem,"touchstart",e.touchDispatch),e.removeEvent(this,e.stage.elem,"touchmove",e.touchDispatch),e.removeEvent(this,e.stage.elem,"touchend",e.touchDispatch),e.removeEvent(this,e.stage.elem,"touchcancel",e.touchDispatch),e.removeEvent(this,e.stage.elem,"touchleave",e.touchDispatch)),e.removeEvent(this,i.body,"mouseup",e.detectBlur)}),e.c("Mouse",{init:function(){e.mouseObjs++,this.bind("Remove",function(){e.mouseObjs--})},areaMap:function(t){if(arguments.length>1){var i=Array.prototype.slice.call(arguments,0);t=new e.polygon(i)}return t.shift(this._x,this._y),this.mapArea=t,this.attach(this.mapArea),this}}),e.c("Draggable",{_origMouseDOMPos:null,_oldX:null,_oldY:null,_dragging:!1,_dir:null,init:function(){this.requires("Mouse"),this.enableDrag()},_ondrag:function(t){var i=e.DOM.translate(t.clientX,t.clientY);if(0===i.x||0===i.y)return!1;if(this._dir){var n=(i.x-this._origMouseDOMPos.x)*this._dir.x+(i.y-this._origMouseDOMPos.y)*this._dir.y;this.x=this._oldX+n*this._dir.x,this.y=this._oldY+n*this._dir.y}else this.x=this._oldX+(i.x-this._origMouseDOMPos.x),this.y=this._oldY+(i.y-this._origMouseDOMPos.y);this.trigger("Dragging",t)},_ondown:function(t){t.mouseButton===e.mouseButtons.LEFT&&this._startDrag(t)},_onup:function(t){this._dragging===!0&&(e.removeEvent(this,e.stage.elem,"mousemove",this._ondrag),e.removeEvent(this,e.stage.elem,"mouseup",this._onup),this._dragging=!1,this.trigger("StopDrag",t))},dragDirection:function(t){if(t===void 0)this._dir=null;else if(""+parseInt(t,10)==t)this._dir={x:Math.cos(t/180*Math.PI),y:Math.sin(t/180*Math.PI)};else{var e=Math.sqrt(t.x*t.x+t.y*t.y);this._dir={x:t.x/e,y:t.y/e}}},_startDrag:function(t){this._origMouseDOMPos=e.DOM.translate(t.clientX,t.clientY),this._oldX=this._x,this._oldY=this._y,this._dragging=!0,e.addEvent(this,e.stage.elem,"mousemove",this._ondrag),e.addEvent(this,e.stage.elem,"mouseup",this._onup),this.trigger("StartDrag",t)},stopDrag:function(){return e.removeEvent(this,e.stage.elem,"mousemove",this._ondrag),e.removeEvent(this,e.stage.elem,"mouseup",this._onup),this._dragging=!1,this.trigger("StopDrag"),this},startDrag:function(){return this._dragging||this._startDrag(e.lastEvent),this},enableDrag:function(){return this.bind("MouseDown",this._ondown),e.addEvent(this,e.stage.elem,"mouseup",this._onup),this},disableDrag:function(){return this.unbind("MouseDown",this._ondown),this._dragging&&this.stopDrag(),this}}),e.c("Keyboard",{isDown:function(t){return"string"==typeof t&&(t=e.keys[t]),!!e.keydown[t]}}),e.c("Multiway",{_speed:3,_keydown:function(t){this._keys[t.key]&&(this._movement.x=Math.round(1e3*(this._movement.x+this._keys[t.key].x))/1e3,this._movement.y=Math.round(1e3*(this._movement.y+this._keys[t.key].y))/1e3,this.trigger("NewDirection",this._movement))},_keyup:function(t){this._keys[t.key]&&(this._movement.x=Math.round(1e3*(this._movement.x-this._keys[t.key].x))/1e3,this._movement.y=Math.round(1e3*(this._movement.y-this._keys[t.key].y))/1e3,this.trigger("NewDirection",this._movement))},_enterframe:function(){this.disableControls||(0!==this._movement.x&&(this.x+=this._movement.x,this.trigger("Moved",{x:this.x-this._movement.x,y:this.y})),0!==this._movement.y&&(this.y+=this._movement.y,this.trigger("Moved",{x:this.x,y:this.y-this._movement.y})))},_initializeControl:function(){return this.unbind("KeyDown",this._keydown).unbind("KeyUp",this._keyup).unbind("EnterFrame",this._enterframe).bind("KeyDown",this._keydown).bind("KeyUp",this._keyup).bind("EnterFrame",this._enterframe)},multiway:function(t,i){this._keyDirection={},this._keys={},this._movement={x:0,y:0},this._speed={x:3,y:3},i?void 0!==t.x&&void 0!==t.y?(this._speed.x=t.x,this._speed.y=t.y):(this._speed.x=t,this._speed.y=t):i=t,this._keyDirection=i,this.speed(this._speed),this._initializeControl();for(var n in i)e.keydown[e.keys[n]]&&this.trigger("KeyDown",{key:e.keys[n]});return this},enableControl:function(){return this.disableControls=!1,this},disableControl:function(){return this.disableControls=!0,this},speed:function(t){for(var i in this._keyDirection){var n=e.keys[i]||i;this._keys[n]={x:Math.round(1e3*Math.cos(this._keyDirection[i]*(Math.PI/180))*t.x)/1e3,y:Math.round(1e3*Math.sin(this._keyDirection[i]*(Math.PI/180))*t.y)/1e3}}return this}}),e.c("Fourway",{init:function(){this.requires("Multiway")},fourway:function(t){return this.multiway(t,{UP_ARROW:-90,DOWN_ARROW:90,RIGHT_ARROW:0,LEFT_ARROW:180,W:-90,S:90,D:0,A:180,Z:-90,Q:180}),this}}),e.c("Twoway",{_speed:3,_up:!1,init:function(){this.requires("Fourway, Keyboard")},twoway:function(t,i){return this.multiway(t,{RIGHT_ARROW:0,LEFT_ARROW:180,D:0,A:180,Q:180}),t&&(this._speed=t),this._jumpSpeed=2>arguments.length?2*this._speed:i,this.bind("EnterFrame",function(){this.disableControls||this._up&&(this.y-=this._jumpSpeed,this._falling=!0)}).bind("KeyDown",function(t){(t.key===e.keys.UP_ARROW||t.key===e.keys.W||t.key===e.keys.Z)&&(this._up=!0)}),this}})},{"./core.js":9}],9:[function(t,e,i){function n(){var t=r++;return t in h?n():t}function s(t){if(null===t||"object"!=typeof t)return t;var e=t.constructor();for(var i in t)e[i]=s(t[i]);return e}var r,o,a,h,c,u,l,d,_,p,f=t("./version"),g=function(t){return new g.fn.init(t)};initState=function(){r=1,a={},h={},c={},u=[],d=Array.prototype.slice,_=/\s*,\s*/,p=/\s+/},initState(),g.fn=g.prototype={init:function(t){if("string"!=typeof t)return t||(t=0,t in h||(h[t]=this)),t in h?(this[0]=t,this.length=1,this.__c||(this.__c={}),h[t]||(h[t]=this),h[t]):(this.length=0,this);var e,i,n,s,r,o,c,u=0,l=!1,d=!1;if("*"===t){o=0;for(e in h)this[o]=+e,o++;return this.length=o,1===o?h[this[0]]:this}-1!==t.indexOf(",")?(d=!0,n=_):-1!==t.indexOf(" ")&&(l=!0,n=p);for(e in h)if(h.hasOwnProperty(e))if(i=h[e],l||d){for(s=t.split(n),o=0,c=s.length,r=0;c>o;o++)i.__c[s[o]]&&r++;(l&&r===c||d&&r>0)&&(this[u++]=+e)}else i.__c[t]&&(this[u++]=+e);if(u>0&&!l&&!d&&this.extend(a[t]),s&&l)for(o=0;c>o;o++)this.extend(a[s[o]]);return this.length=u,1===u?h[this[u-1]]:this},setName:function(t){var e=t+"";return this._entityName=e,this.trigger("NewEntityName",e),this},addComponent:function(t){var e,i,n,s,r=[],o=0,h=0;if(arguments.length>1)for(i=arguments.length;i>h;h++)r.push(arguments[h]);else if(-1!==t.indexOf(","))for(n=t.split(_),i=n.length;i>h;h++)r.push(n[h]);else r.push(t);for(e=r.length;e>o;o++)this.__c[r[o]]!==!0&&(this.__c[r[o]]=!0,s=a[r[o]],this.extend(s),s&&"init"in s&&s.init.call(this));return this.trigger("NewComponent",r),this},toggleComponent:function(t){var e,i,n=0;if(arguments.length>1)for(e=arguments.length;e>n;n++)this.has(arguments[n])?this.removeComponent(arguments[n]):this.addComponent(arguments[n]);else if(-1!==t.indexOf(","))for(i=t.split(_),e=i.length;e>n;n++)this.has(i[n])?this.removeComponent(i[n]):this.addComponent(i[n]);else this.has(t)?this.removeComponent(t):this.addComponent(t);return this},requires:function(t){return this.addComponent(t)},removeComponent:function(t,e){var i=a[t];if(this.trigger("RemoveComponent",t),i&&"remove"in i&&i.remove.call(this,!1),e===!1&&i)for(var n in i)delete this[n];return delete this.__c[t],this},getId:function(){return this[0]},has:function(t){return!!this.__c[t]},attr:function(t,e){if(1===arguments.length)return"string"==typeof t?this[t]:(this.extend(t),this.trigger("Change",t),this);this[t]=e;var i={};return i[t]=e,this.trigger("Change",i),this},toArray:function(){return d.call(this,0)},timeout:function(t,e){return this.each(function(){var i=this;setTimeout(function(){t.call(i)},e)}),this},bind:function(t,e){if(1===this.length){c[t]||(c[t]={});var i=c[t];return i[this[0]]||(i[this[0]]=[]),i[this[0]].push(e),this}return this.each(function(){c[t]||(c[t]={});var i=c[t];i[this[0]]||(i[this[0]]=[]),i[this[0]].push(e)}),this},uniqueBind:function(t,e){this.unbind(t,e),this.bind(t,e)},one:function(t,e){var i=this,n=function(s){e.call(i,s),i.unbind(t,n)};return i.bind(t,n)},unbind:function(t,e){return this.each(function(){var i,n,s=c[t],r=0;if(!s||!s[this[0]])return this;if(i=s[this[0]].length,!e)return delete s[this[0]],this;for(;i>r;r++)n=s[this[0]],n[r]==e&&delete n[r]}),this},trigger:function(t,e){if(1===this.length){if(c[t]&&c[t][this[0]]){var i,n=c[t][this[0]];for(i=0;n.length>i;i++)n[i]===void 0?(n.splice(i,1),i--):n[i].call(this,e)}return this}return this.each(function(){if(c[t]&&c[t][this[0]]){var i,n=c[t][this[0]];for(i=0;n.length>i;i++)n[i]===void 0?(n.splice(i,1),i--):n[i].call(this,e)}}),this},each:function(t){for(var e=0,i=this.length;i>e;e++)h[this[e]]&&t.call(h[this[e]],e);return this},clone:function(){var t,e,i=this.__c,n=g.e();for(t in i)n.addComponent(t);for(e in this)"0"!=e&&"_global"!=e&&"_changed"!=e&&"function"!=typeof this[e]&&"object"!=typeof this[e]&&(n[e]=this[e]);return n},setter:function(t,e){return g.support.setter?this.__defineSetter__(t,e):g.support.defineProperty?Object.defineProperty(this,t,{set:e,configurable:!0}):l.push({prop:t,obj:this,fn:e}),this},destroy:function(){this.each(function(){var t;this.trigger("Remove");for(var e in this.__c)t=a[e],t&&"remove"in t&&t.remove.call(this,!0);for(var i in c)this.unbind(i);delete h[this[0]]})}},g.fn.init.prototype=g.fn,g.extend=g.fn.extend=function(t){var e,i=this;if(!t)return i;for(e in t)i!==t[e]&&(i[e]=t[e]);return i},g.extend({init:function(t,e,i){return g.viewport.init(t,e,i),this.trigger("Load"),this.timer.init(),this},getVersion:function(){return f},stop:function(t){if(this.timer.stop(),t){if(g.audio.remove(),g.stage&&g.stage.elem.parentNode){var e=document.createElement("div");e.id=g.stage.elem.id,g.stage.elem.parentNode.replaceChild(e,g.stage.elem)}initState()}return g.trigger("CraftyStop"),this},pause:function(t){return(1===arguments.length?t:!this._paused)?(this.trigger("Pause"),this._paused=!0,setTimeout(function(){g.timer.stop()},0),g.keydown={}):(this.trigger("Unpause"),this._paused=!1,setTimeout(function(){g.timer.init()},0)),this},isPaused:function(){return this._paused},timer:function(){var t,e,i,n="fixed",s=5,r=40,o=0,a=0,h=0,c=50,u=1e3/c;return{init:function(){i===void 0&&(i=(new Date).getTime()-u);var n=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||null;n?(t=function(){g.timer.step(),e=n(t)},t()):t=setInterval(function(){g.timer.step()},1e3/c)},stop:function(){g.trigger("CraftyStopTimer"),"number"==typeof t&&clearInterval(t);var i=window.cancelRequestAnimationFrame||window.webkitCancelRequestAnimationFrame||window.mozCancelRequestAnimationFrame||window.oCancelRequestAnimationFrame||window.msCancelRequestAnimationFrame||null;i&&i(e),t=null},steptype:function(t,e){if("variable"===t||"semifixed"===t)n=t,e&&(r=e);else{if("fixed"!==t)throw"Invalid step type specified";n="fixed",e&&(s=e)}},step:function(){var t,e,c,l=0;if(currentTime=(new Date).getTime(),o>0&&g.trigger("MeasureWaitTime",currentTime-o),i+a>=currentTime)return o=currentTime,void 0;var d=currentTime-(i+a);d>20*u&&(a+=d-u,d=u),"fixed"===n?(l=Math.ceil(d/u),l=Math.min(l,s),e=u):"variable"===n?(l=1,e=d,e=Math.min(e,r)):"semifixed"===n&&(l=Math.ceil(d/r),e=d/l);for(var _=0;l>_;_++)c=currentTime,g.trigger("EnterFrame",{frame:h++,dt:e,gameTime:i}),i+=e,currentTime=(new Date).getTime(),g.trigger("MeasureFrameTime",currentTime-c);l>0&&(t=currentTime,g.trigger("RenderScene"),g.trigger("PostRender"),currentTime=(new Date).getTime(),g.trigger("MeasureRenderTime",currentTime-t)),o=currentTime},FPS:function(t){return t===void 0?c:(c=t,u=1e3/c,void 0)},simulateFrames:function(t,e){for(e===void 0&&(e=u);t-->0;)g.trigger("EnterFrame",{frame:h++,dt:e});g.trigger("RenderScene")}}}(),e:function(){var t,e=n();return h[e]=null,h[e]=t=g(e),arguments.length>0&&t.addComponent.apply(t,arguments),t.setName("Entity #"+e),t.addComponent("obj"),g.trigger("NewEntity",{id:e}),t},c:function(t,e){a[t]=e},trigger:function(t,e){var i,n,s,r,o=c[t];for(i in o)if(o.hasOwnProperty(i)&&(s=o[i],s&&0!==s.length))for(r=h[i]?g(+i):g,n=0;s.length>n;n++)s[n]===void 0?(s.splice(n,1),n--):s[n].call(r,e)},bind:function(t,e){c[t]||(c[t]={});var i=c[t];return i.global||(i.global=[]),i.global.push(e)-1},uniqueBind:function(t,e){this.unbind(t,e),this.bind(t,e)},one:function(t,e){var i=this,n=function(s){e.call(i,s),i.unbind(t,n)};return i.bind(t,n)},unbind:function(t,e){var i,n,s,r,o=c[t];if(void 0===o||void 0===o.global||0===o.global.length)return!1;if(1===arguments.length)return delete o.global,!0;for(s=o.global,r=!1,i=0,n=s.length;n>i;i++)s[i]===e&&(r=!0,delete s[i]);return r},frame:function(){return o},components:function(){return a},isComp:function(t){return t in a},debug:function(t){return"handlers"===t?c:h},settings:function(){var t={},e={};return{register:function(t,i){e[t]=i},modify:function(i,n){e[i]&&(e[i].call(t[i],n),t[i]=n)},get:function(e){return t[e]}}}(),clone:s}),g.bind("Load",function(){!g.support.setter&&g.support.defineProperty&&(l=[],g.bind("EnterFrame",function(){for(var t,e=0,i=l.length;i>e;++e)t=l[e],t.obj[t.prop]!==t.obj["_"+t.prop]&&t.fn.call(t.obj,t.obj[t.prop])}))}),"function"==typeof define?define("crafty",[],function(){return g}):"object"==typeof i&&(e.exports=g),window.Crafty=g},{"./version":25}],10:[function(t){var e=t("./core.js");window.document,e.extend({device:{_deviceOrientationCallback:!1,_deviceMotionCallback:!1,_normalizeDeviceOrientation:function(t){var i;window.DeviceOrientationEvent?i={tiltLR:t.gamma,tiltFB:t.beta,dir:t.alpha,motUD:null}:window.OrientationEvent&&(i={tiltLR:90*t.x,tiltFB:-90*t.y,dir:null,motUD:t.z}),e.device._deviceOrientationCallback(i)},_normalizeDeviceMotion:function(t){var i=t.accelerationIncludingGravity,n=i.z>0?1:-1,s={acceleration:i,rawAcceleration:"["+Math.round(i.x)+", "+Math.round(i.y)+", "+Math.round(i.z)+"]",facingUp:n,tiltLR:Math.round(-90*(i.x/9.81)),tiltFB:Math.round(90*((i.y+9.81)/9.81)*n)};e.device._deviceMotionCallback(s)},deviceOrientation:function(t){this._deviceOrientationCallback=t,e.support.deviceorientation&&(window.DeviceOrientationEvent?e.addEvent(this,window,"deviceorientation",this._normalizeDeviceOrientation):window.OrientationEvent&&e.addEvent(this,window,"MozOrientation",this._normalizeDeviceOrientation))},deviceMotion:function(t){this._deviceMotionCallback=t,e.support.devicemotion&&window.DeviceMotionEvent&&e.addEvent(this,window,"devicemotion",this._normalizeDeviceMotion)}}})},{"./core.js":9}],11:[function(t){var e=t("./core.js");window.document,e.extend({diamondIso:{_tile:{width:0,height:0,r:0},_map:{width:0,height:0,x:0,y:0},_origin:{x:0,y:0},init:function(t,e,i,n){return this._tile.width=parseInt(t,10),this._tile.height=parseInt(e,10)||parseInt(t,10)/2,this._tile.r=this._tile.width/this._tile.height,this._map.width=parseInt(i,10),this._map.height=parseInt(n,10)||parseInt(i,10),this._origin.x=this._map.height*this._tile.width/2,this},place:function(t,e,i,n){var s=this.pos2px(e,i);n||(n=1);var r=0,o=0;void 0!==t.__margin&&(r=t.__margin[0],o=t.__margin[1]),t.x=s.left+r,t.y=s.top+o-t.h,t.z=s.top*n},centerAt:function(t,i){var n=this.pos2px(t,i);e.viewport.x=-n.left+e.viewport.width/2-this._tile.width,e.viewport.y=-n.top+e.viewport.height/2},area:function(t){t||(t=0);var i=e.viewport.rect(),n=t*this._tile.width,s=t*this._tile.height;i._x-=this._tile.width/2+n,i._y-=this._tile.height/2+s,i._w+=this._tile.width/2+n,i._h+=this._tile.height/2+s;for(var r=[],o=i._y,a=i._y+i._h;a>o;o+=this._tile.height/2)for(var h=i._x,c=i._x+i._w;c>h;h+=this._tile.width/2){var u=this.px2pos(h,o);r.push([~~u.x,~~u.y])}return r},pos2px:function(t,e){return{left:(t-e)*this._tile.width/2+this._origin.x,top:(t+e)*this._tile.height/2}},px2pos:function(t,e){var i=(t-this._origin.x)/this._tile.r;return{x:(e+i)/this._tile.height,y:(e-i)/this._tile.height}},polygon:function(t){t.requires("Collision");var i=0,n=0;void 0!==t.__margin&&(i=t.__margin[0],n=t.__margin[1]);var s=[[i-0,t.h-n-this._tile.height/2],[i-this._tile.width/2,t.h-n-0],[i-this._tile.width,t.h-n-this._tile.height/2],[i-this._tile.width/2,t.h-n-this._tile.height]],r=new e.polygon(s);return r}}})},{"./core.js":9}],12:[function(t){var e=t("./core.js");window.document,e.c("Color",{_color:"",ready:!0,init:function(){this.bind("Draw",function(t){"DOM"===t.type?(t.style.backgroundColor=this._color,t.style.lineHeight=0):"canvas"===t.type&&(this._color&&(t.ctx.fillStyle=this._color),t.ctx.fillRect(t.pos._x,t.pos._y,t.pos._w,t.pos._h))})},color:function(t){return t?(this._color=t,this.trigger("Change"),this):this._color}}),e.c("Tint",{_color:null,_strength:1,init:function(){var t=function(t){var i=t.ctx||e.canvas.context;i.fillStyle=this._color||"rgba(0,0,0, 0)",i.fillRect(t.pos._x,t.pos._y,t.pos._w,t.pos._h)};this.bind("Draw",t).bind("RemoveComponent",function(e){"Tint"===e&&this.unbind("Draw",t)})},tint:function(t,i){return this._strength=i,this._color=e.toRGB(t,this._strength),this.trigger("Change"),this}}),e.c("Image",{_repeat:"repeat",ready:!1,init:function(){var t=function(t){if("canvas"===t.type){if(!this.ready||!this._pattern)return;var e=t.ctx;e.fillStyle=this._pattern,e.save(),e.translate(t.pos._x,t.pos._y),e.fillRect(0,0,this._w,this._h),e.restore()}else"DOM"===t.type&&this.__image&&(t.style.background="url("+this.__image+") "+this._repeat)};this.bind("Draw",t).bind("RemoveComponent",function(e){"Image"===e&&this.unbind("Draw",t)})},image:function(t,i){if(this.__image=t,this._repeat=i||"no-repeat",this.img=e.asset(t),!this.img){this.img=new Image,e.asset(t,this.img),this.img.src=t;var n=this;return this.img.onload=function(){n.has("Canvas")&&(n._pattern=e.canvas.context.createPattern(n.img,n._repeat)),n.ready=!0,"no-repeat"===n._repeat&&(n.w=n.img.width,n.h=n.img.height),n.trigger("Change")},this}return this.ready=!0,this.has("Canvas")&&(this._pattern=e.canvas.context.createPattern(this.img,this._repeat)),"no-repeat"===this._repeat&&(this.w=this.img.width,this.h=this.img.height),this.trigger("Change"),this}}),e.extend({_scenes:{},_current:null,scene:function(t,i,n){if(1===arguments.length){e.trigger("SceneDestroy",{newScene:t}),e.viewport.reset(),e("2D").each(function(){this.has("Persist")||this.destroy()}),null!==this._current&&"uninitialize"in this._scenes[this._current]&&this._scenes[this._current].uninitialize.call(this);var s=this._current;return this._current=t,e.trigger("SceneChange",{oldScene:s,newScene:t}),this._scenes[t].initialize.call(this),void 0}this._scenes[t]={},this._scenes[t].initialize=i,n!==void 0&&(this._scenes[t].uninitialize=n)},toRGB:function(t,e){t="#"===t.charAt(0)?t.substr(1):t;var i,n=[];return n[0]=parseInt(t.substr(0,2),16),n[1]=parseInt(t.substr(2,2),16),n[2]=parseInt(t.substr(4,2),16),i=void 0===e?"rgb("+n.join(",")+")":"rgba("+n.join(",")+","+e+")"}}),e.DrawManager=function(){function t(t,e){return t._globalZ-e._globalZ}var i=[],n=[],s=[],r=!1,o={merge:function(t,e,i){return i===void 0&&(i={}),i._h=Math.max(t._y+t._h,e._y+e._h),i._w=Math.max(t._x+t._w,e._x+e._w),i._x=Math.min(t._x,e._x),i._y=Math.min(t._y,e._y),i._w-=i._x,i._h-=i._y,i},clean:function(){var t,e,s;for(s=0,l=n.length;l>s;s++)e=n[s],t=e._mbr||e,e.staleRect===void 0&&(e.staleRect={}),e.staleRect._x=t._x,e.staleRect._y=t._y,e.staleRect._w=t._w,e.staleRect._h=t._h,e._changed=!1;n.length=0,i.length=0},createDirty:function(t){var e=t._mbr||t;if(t.staleRect){if(o.overlap(t.staleRect,e))return o.merge(t.staleRect,e,t.staleRect),i.push(t.staleRect),void 0;i.push(t.staleRect)}t.currentRect._x=e._x,t.currentRect._y=e._y,t.currentRect._w=e._w,t.currentRect._h=e._h,i.push(t.currentRect)},overlap:function(t,e){return t._xe._x&&t._y+t._h>e._y}};return e.bind("InvalidateViewport",function(){r=!0}),e.bind("PostRender",function(){r=!1}),{total2D:e("2D").length,onScreen:function(t){return e.viewport._x+t._x+t._w>0&&e.viewport._y+t._y+t._h>0&&e.viewport._x+t._xe;)o.overlap(t[e],t[e+1])?(o.merge(t[e],t[e+1],t[e]),t.splice(e+1,1),e>0&&e--):e++;return t},addCanvas:function(t){n.push(t)},addDom:function(t){s.push(t)},debug:function(){console.log(n,s)},drawAll:function(i){i=i||e.viewport.rect();var n,s=e.map.search(i),r=0,o=s.length,a=e.canvas.context;for(a.clearRect(i._x,i._y,i._w,i._h),s.sort(t);o>r;r++)n=s[r],n._visible&&n.__c.Canvas&&(n.draw(),n._changed=!1)},boundingRect:function(t){if(t&&t.length){var e,i,n=1,s=t.length,r=t[0];for(r=[r._x,r._y,r._x+r._w,r._y+r._h];s>n;)e=t[n],i=[e._x,e._y,e._x+e._w,e._y+e._h],i[0]r[2]&&(r[2]=i[2]),i[3]>r[3]&&(r[3]=i[3]),n++;return i=r,r={_x:i[0],_y:i[1],_w:i[2]-i[0],_h:i[3]-i[1]}}},renderCanvas:function(){var s=n.length;if(s||r){var a,h,c,u,l,d=0,_=e.canvas.context,p=e.DrawManager;if(r){var f=e.viewport;_.setTransform(f._scale,0,0,f._scale,f.x,f.y)}if(s/p.total2D>.6||r)return p.drawAll(),o.clean(),void 0;for(d=0;s>d;d++)o.createDirty(n[d]);i=p.mergeSet(i),s=i.length;var g=[],m=[];for(d=0;s>d;++d)if(a=i[d],g.length=0,m.length=0,a){for(a._w=a._x+a._w,a._h=a._y+a._h,a._x=a._x>0?0|a._x:(0|a._x)-1,a._y=a._y>0?0|a._y:(0|a._y)-1,a._w-=a._x,a._h-=a._y,a._w=a._w===(0|a._w)?a._w:(0|a._w)+1,a._h=a._h===(0|a._h)?a._h:(0|a._h)+1,h=e.map.search(a,!1),_.clearRect(a._x,a._y,a._w,a._h),_.save(),_.beginPath(),_.rect(a._x,a._y,a._w,a._h),_.clip(),c=0,u=h.length;u>c;++c)l=h[c],!g[l[0]]&&l._visible&&l.__c.Canvas&&(g[l[0]]=!0,m.push(l));for(m.sort(t),c=0,u=m.length;u>c;++c){l=m[c];var v=l._mbr||l;o.overlap(v,a)&&l.draw(),l._changed=!1}_.closePath(),_.restore()}if(e.DrawManager.debugDirty===!0)for(_.strokeStyle="red",d=0,s=i.length;s>d;++d)a=i[d],_.strokeRect(a._x,a._y,a._w,a._h);o.clean()}},renderDOM:function(){if(r){var t=e.stage.inner.style,i=e.viewport;t.transform=t[e.support.prefix+"Transform"]="scale("+i._scale+", "+i._scale+")",t.left=i.x+"px",t.top=i.y+"px",t.zIndex=10}if(s.length){for(var n=0,o=s.length;o>n;++n)s[n].draw()._changed=!1;s.length=0}}}}()},{"./core.js":9}],13:[function(t){var e=t("./core.js"),i=window.document;(function(){var t=e.support={},n=navigator.userAgent.toLowerCase(),s=/(webkit)[ \/]([\w.]+)/.exec(n)||/(o)pera(?:.*version)?[ \/]([\w.]+)/.exec(n)||/(ms)ie ([\w.]+)/.exec(n)||/(moz)illa(?:.*? rv:([\w.]+))?/.exec(n)||[],r=/iPad|iPod|iPhone|Android|webOS|IEMobile/i.exec(n);if(r&&(e.mobile=r[0]),t.setter="__defineSetter__"in this&&"__defineGetter__"in this,t.defineProperty=function(){if(!("defineProperty"in Object))return!1;try{Object.defineProperty({},"x",{})}catch(t){return!1}return!0}(),t.audio="Audio"in window,t.prefix=s[1]||s[0],"moz"===t.prefix&&(t.prefix="Moz"),"o"===t.prefix&&(t.prefix="O"),s[2]&&(t.versionName=s[2],t.version=+s[2].split(".")[0]),t.canvas="getContext"in i.createElement("canvas"),t.canvas){var o;try{o=i.createElement("canvas").getContext("experimental-webgl"),o.viewportWidth=t.canvas.width,o.viewportHeight=t.canvas.height}catch(a){}t.webgl=!!o}else t.webgl=!1;t.css3dtransform=i.createElement("div").style.Perspective!==void 0||i.createElement("div").style[t.prefix+"Perspective"]!==void 0,t.deviceorientation=window.DeviceOrientationEvent!==void 0||window.OrientationEvent!==void 0,t.devicemotion=window.DeviceMotionEvent!==void 0})(),e.extend({zeroFill:function(t,e){return e-=(""+t).length,e>0?Array(e+(/\./.test(t)?2:1)).join("0")+t:""+t},sprite:function(t,i,n,s,r,o){var a,h,c,u,l,d,_;"string"==typeof t&&(o=r,r=s,s=i,n=t,t=1,i=1),"string"==typeof i&&(o=r,r=s,s=n,n=i,i=t),!o&&r&&(o=r),r=parseInt(r||0,10),o=parseInt(o||0,10);var p=function(){this.ready=!0,this.trigger("Change")};_=e.asset(n),_||(_=new Image,_.src=n,e.asset(n,_),_.onload=function(){for(var t in s)e(t).each(p)});var f=function(){this.requires("2D, Sprite"),this.__trim=[0,0,0,0],this.__image=n,this.__coord=[this.__coord[0],this.__coord[1],this.__coord[2],this.__coord[3]],this.__tile=t,this.__tileh=i,this.__padding=[r,o],this.img=_,this.img.complete&&this.img.width>0&&(this.ready=!0,this.trigger("Change")),this.w=this.__coord[2],this.h=this.__coord[3]};for(a in s)s.hasOwnProperty(a)&&(h=s[a],c=h[0]*(t+r),u=h[1]*(i+o),l=h[2]*t||t,d=h[3]*i||i,e.c(a,{ready:!1,__coord:[c,u,l,d],init:f}));return this},_events:{},addEvent:function(t,e,i,n){3===arguments.length&&(n=i,i=e,e=window.document);var s=function(e){e=e||window.event,"function"==typeof n&&n.call(t,e)},r=t[0]||"";this._events[r+e+i+n]||(this._events[r+e+i+n]=s,e.attachEvent?e.attachEvent("on"+i,s):e.addEventListener(i,s,!1))},removeEvent:function(t,e,i,n){3===arguments.length&&(n=i,i=e,e=window.document);var s=t[0]||"",r=this._events[s+e+i+n];r&&(e.detachEvent?e.detachEvent("on"+i,r):e.removeEventListener(i,r,!1),delete this._events[s+e+i+n])},background:function(t){e.stage.elem.style.background=t},keys:{BACKSPACE:8,TAB:9,ENTER:13,PAUSE:19,CAPS:20,ESC:27,SPACE:32,PAGE_UP:33,PAGE_DOWN:34,END:35,HOME:36,LEFT_ARROW:37,UP_ARROW:38,RIGHT_ARROW:39,DOWN_ARROW:40,INSERT:45,DELETE:46,0:48,1:49,2:50,3:51,4:52,5:53,6:54,7:55,8:56,9:57,A:65,B:66,C:67,D:68,E:69,F:70,G:71,H:72,I:73,J:74,K:75,L:76,M:77,N:78,O:79,P:80,Q:81,R:82,S:83,T:84,U:85,V:86,W:87,X:88,Y:89,Z:90,NUMPAD_0:96,NUMPAD_1:97,NUMPAD_2:98,NUMPAD_3:99,NUMPAD_4:100,NUMPAD_5:101,NUMPAD_6:102,NUMPAD_7:103,NUMPAD_8:104,NUMPAD_9:105,MULTIPLY:106,ADD:107,SUBSTRACT:109,DECIMAL:110,DIVIDE:111,F1:112,F2:113,F3:114,F4:115,F5:116,F6:117,F7:118,F8:119,F9:120,F10:121,F11:122,F12:123,SHIFT:16,CTRL:17,ALT:18,PLUS:187,COMMA:188,MINUS:189,PERIOD:190,PULT_UP:29460,PULT_DOWN:29461,PULT_LEFT:4,PULT_RIGHT:5},mouseButtons:{LEFT:0,MIDDLE:1,RIGHT:2}})},{"./core.js":9}],14:[function(t){var e=t("./core.js");window.document,e.c("HTML",{inner:"",init:function(){this.requires("2D, DOM")},replace:function(t){return this.inner=t,this._element.innerHTML=t,this},append:function(t){return this.inner+=t,this._element.innerHTML+=t,this},prepend:function(t){return this.inner=t+this.inner,this._element.innerHTML=t+this.inner,this}})},{"./core.js":9}],15:[function(t){var e=t("./core.js"),i=window.document;e["import"]=function(t,n){if("string"!=typeof t){var s,r,o,a,h=0;if(t.n&&"object"==typeof t.n)for(r=t.n.length;r>h;++h)o=t.n[h],a=e.e(o.c),delete o.c,a.attr(o);for(s in t)a=e(s),a.attr(t[s])}else if(levelData)n?e.import(levelData[n]):e.import(levelData);else{var c;c=i.createElement("script"),c.onload=function(){n?e.import(levelData[n]):e.import(levelData)},c.src=t}}},{"./core.js":9}],16:[function(t){var e=t("./core.js");window.document,e.extend({isometric:{_tile:{width:0,height:0},_elements:{},_pos:{x:0,y:0},_z:0,size:function(t,e){return this._tile.width=t,this._tile.height=e>0?e:t/2,this},place:function(t,i,n,s){var r=this.pos2px(t,i);return r.top-=n*(this._tile.height/2),s.attr({x:r.left+e.viewport._x,y:r.top+e.viewport._y}).z+=n,this},pos2px:function(t,e){return{left:t*this._tile.width+(1&e)*(this._tile.width/2),top:e*this._tile.height/2}},px2pos:function(t,e){return{x:-Math.ceil(-t/this._tile.width-.5*(1&e)),y:2*(e/this._tile.height)}},centerAt:function(t,i){if("number"==typeof t&&"number"==typeof i){var n=this.pos2px(t,i);return e.viewport._x=-n.left+e.viewport.width/2-this._tile.width/2,e.viewport._y=-n.top+e.viewport.height/2-this._tile.height/2,this}return{top:-e.viewport._y+e.viewport.height/2-this._tile.height/2,left:-e.viewport._x+e.viewport.width/2-this._tile.width/2}},area:function(){var t=this.centerAt(),i=this.px2pos(-t.left+e.viewport.width/2,-t.top+e.viewport.height/2),n=this.px2pos(-t.left-e.viewport.width/2,-t.top-e.viewport.height/2);return{x:{start:i.x,end:n.x},y:{start:i.y,end:n.y}} +}}})},{"./core.js":9}],17:[function(t){var e=t("./core.js"),i=window.document;e.extend({assets:{},asset:function(t,i){return 1===arguments.length?e.assets[t]:e.assets[t]?void 0:(e.assets[t]=i,this.trigger("NewAsset",{key:t,value:i}),i)},image_whitelist:["jpg","jpeg","gif","png","svg"],load:function(t,i,n,s){function r(){var t=this.src;this.removeEventListener&&this.removeEventListener("canplaythrough",r,!1),++d,n&&n({loaded:d,total:l,percent:100*(d/l),src:t}),d===l&&i&&i()}function o(){var t=this.src;s&&s({loaded:d,total:l,percent:100*(d/l),src:t}),d++,d===l&&i&&i()}for(var a,h,c=0,u=t.length,l=u,d=0,_="";u>c;++c){if(a=t[c],_=a.substr(a.lastIndexOf(".")+1,3).toLowerCase(),h=e.asset(a)||null,e.audio.supports(_)){if(!h){var p=a.substr(a.lastIndexOf("/")+1).toLowerCase();h=e.audio.create(p,a).obj}h.addEventListener&&h.addEventListener("canplaythrough",r,!1)}else{if(!(e.image_whitelist.indexOf(_)>=0)){l--;continue}h||(h=new Image,e.asset(a,h)),h.onload=r,"webkit"===e.support.prefix&&(h.src=""),h.src=a}h.onerror=o}0===l&&i()},modules:function(t,e,n){2===arguments.length&&"object"==typeof t&&(n=e,e=t,t="http://cdn.craftycomponents.com");var s=function(){function t(t,e,i){for(i=0,j=t.length;j>i;++i)if(!e(t[i]))return f;return 1}function e(e,i){t(e,function(t){return!i(t)})}function n(i,o,a){function h(t){return t.call?t():l[t]}function u(){if(!--y){l[v]=1,m&&m();for(var i in _)t(i.split("|"),h)&&!e(_[i],h)&&(_[i]=[])}}i=i[g]?i:[i];var f=o&&o.call,m=f?o:a,v=f?i.join(""):o,y=i.length;return setTimeout(function(){e(i,function(t){return p[t]?(v&&(d[v]=1),2==p[t]&&u()):(p[t]=1,v&&(d[v]=1),s(!c.test(t)&&r?r+t+".js":t,u),void 0)})},0),n}function s(t,e){var i=a.createElement("script"),n=f;i.onload=i.onerror=i[x]=function(){i[v]&&!/^c|loade/.test(i[v])||n||(i.onload=i[x]=null,n=1,p[t]=2,e())},i.async=1,i.src=t,h.insertBefore(i,h.firstChild)}var r,o=this,a=i,h=a.getElementsByTagName("head")[0],c=/^https?:\/\//,u=o.$script,l={},d={},_={},p={},f=!1,g="push",m="DOMContentLoaded",v="readyState",y="addEventListener",x="onreadystatechange";return!a[v]&&a[y]&&(a[y](m,function w(){a.removeEventListener(m,w,f),a[v]="complete"},f),a[v]="loading"),n.get=s,n.order=function(t,e,i){(function s(r){r=t.shift(),t.length?n(r,s):n(r,e,i)})()},n.path=function(t){r=t},n.ready=function(i,s,r){i=i[g]?i:[i];var o=[];return!e(i,function(t){l[t]||o[g](t)})&&t(i,function(t){return l[t]})?s():!function(t){_[t]=_[t]||[],_[t][g](s),r&&r(o)}(i.join("|")),n},n.noConflict=function(){return o.$script=u,this},n}(),r=[],o=/^(https?|file):\/\//;for(var a in e)o.test(a)?r.push(a):r.push(t+"/"+a.toLowerCase()+"-"+e[a].toLowerCase()+".js");s(r,function(){n&&n()})}})},{"./core.js":9}],18:[function(t){var e=t("./core.js");window.document,e.math={abs:function(t){return 0>t?-t:t},amountOf:function(t,e,i){return i>e?(t-e)/(i-e):(t-i)/(e-i)},clamp:function(t,e,i){return t>i?i:e>t?e:t},degToRad:function(t){return t*Math.PI/180},distance:function(t,i,n,s){var r=e.math.squaredDistance(t,i,n,s);return Math.sqrt(parseFloat(r))},lerp:function(t,e,i){return t+(e-t)*i},negate:function(t){return t>Math.random()?-1:1},radToDeg:function(t){return 180*t/Math.PI},randomElementOfArray:function(t){return t[Math.floor(t.length*Math.random())]},randomInt:function(t,e){return t+Math.floor((1+e-t)*Math.random())},randomNumber:function(t,e){return t+(e-t)*Math.random()},squaredDistance:function(t,e,i,n){return(t-i)*(t-i)+(e-n)*(e-n)},withinRange:function(t,e,i){return t>=e&&i>=t}},e.math.Vector2D=function(){function t(e,i){if(e instanceof t)this.x=e.x,this.y=e.y;else if(2===arguments.length)this.x=e,this.y=i;else if(arguments.length>0)throw"Unexpected number of arguments for Vector2D()"}return t.prototype.x=0,t.prototype.y=0,t.prototype.add=function(t){return this.x+=t.x,this.y+=t.y,this},t.prototype.angleBetween=function(t){return Math.atan2(this.x*t.y-this.y*t.x,this.x*t.x+this.y*t.y)},t.prototype.angleTo=function(t){return Math.atan2(t.y-this.y,t.x-this.x)},t.prototype.clone=function(){return new t(this)},t.prototype.distance=function(t){return Math.sqrt((t.x-this.x)*(t.x-this.x)+(t.y-this.y)*(t.y-this.y))},t.prototype.distanceSq=function(t){return(t.x-this.x)*(t.x-this.x)+(t.y-this.y)*(t.y-this.y)},t.prototype.divide=function(t){return this.x/=t.x,this.y/=t.y,this},t.prototype.dotProduct=function(t){return this.x*t.x+this.y*t.y},t.prototype.equals=function(e){return e instanceof t&&this.x==e.x&&this.y==e.y},t.prototype.getNormal=function(e){return void 0===e?new t(-this.y,this.x):new t(e.y-this.y,this.x-e.x).normalize()},t.prototype.isZero=function(){return 0===this.x&&0===this.y},t.prototype.magnitude=function(){return Math.sqrt(this.x*this.x+this.y*this.y)},t.prototype.magnitudeSq=function(){return this.x*this.x+this.y*this.y},t.prototype.multiply=function(t){return this.x*=t.x,this.y*=t.y,this},t.prototype.negate=function(){return this.x=-this.x,this.y=-this.y,this},t.prototype.normalize=function(){var t=Math.sqrt(this.x*this.x+this.y*this.y);return 0===t?(this.x=1,this.y=0):(this.x/=t,this.y/=t),this},t.prototype.scale=function(t,e){return void 0===e&&(e=t),this.x*=t,this.y*=e,this},t.prototype.scaleToMagnitude=function(t){var e=t/this.magnitude();return this.x*=e,this.y*=e,this},t.prototype.setValues=function(e,i){return e instanceof t?(this.x=e.x,this.y=e.y):(this.x=e,this.y=i),this},t.prototype.subtract=function(t){return this.x-=t.x,this.y-=t.y,this},t.prototype.toString=function(){return"Vector2D("+this.x+", "+this.y+")"},t.prototype.translate=function(t,e){return void 0===e&&(e=t),this.x+=t,this.y+=e,this},t.tripleProduct=function(t,i,n){var s=t.dotProduct(n),r=i.dotProduct(n);return new e.math.Vector2D(i.x*s-t.x*r,i.y*s-t.y*r)},t}(),e.math.Matrix2D=function(){return Matrix2D=function(t,e,i,n,s,r){if(t instanceof Matrix2D)this.a=t.a,this.b=t.b,this.c=t.c,this.d=t.d,this.e=t.e,this.f=t.f;else if(6===arguments.length)this.a=t,this.b=e,this.c=i,this.d=n,this.e=s,this.f=r;else if(arguments.length>0)throw"Unexpected number of arguments for Matrix2D()"},Matrix2D.prototype.a=1,Matrix2D.prototype.b=0,Matrix2D.prototype.c=0,Matrix2D.prototype.d=1,Matrix2D.prototype.e=0,Matrix2D.prototype.f=0,Matrix2D.prototype.apply=function(t){var e=t.x;return t.x=e*this.a+t.y*this.c+this.e,t.y=e*this.b+t.y*this.d+this.f,t},Matrix2D.prototype.clone=function(){return new Matrix2D(this)},Matrix2D.prototype.combine=function(t){var e=this.a;return this.a=e*t.a+this.b*t.c,this.b=e*t.b+this.b*t.d,e=this.c,this.c=e*t.a+this.d*t.c,this.d=e*t.b+this.d*t.d,e=this.e,this.e=e*t.a+this.f*t.c+t.e,this.f=e*t.b+this.f*t.d+t.f,this},Matrix2D.prototype.equals=function(t){return t instanceof Matrix2D&&this.a==t.a&&this.b==t.b&&this.c==t.c&&this.d==t.d&&this.e==t.e&&this.f==t.f},Matrix2D.prototype.determinant=function(){return this.a*this.d-this.b*this.c},Matrix2D.prototype.invert=function(){var t=this.determinant();if(0!==t){var e={a:this.a,b:this.b,c:this.c,d:this.d,e:this.e,f:this.f};this.a=e.d/t,this.b=-e.b/t,this.c=-e.c/t,this.d=e.a/t,this.e=(e.c*e.f-e.e*e.d)/t,this.f=(e.e*e.b-e.a*e.f)/t}return this},Matrix2D.prototype.isIdentity=function(){return 1===this.a&&0===this.b&&0===this.c&&1===this.d&&0===this.e&&0===this.f},Matrix2D.prototype.isInvertible=function(){return 0!==this.determinant()},Matrix2D.prototype.preRotate=function(t){var e=Math.cos(t),i=Math.sin(t),n=this.a;return this.a=e*n-i*this.b,this.b=i*n+e*this.b,n=this.c,this.c=e*n-i*this.d,this.d=i*n+e*this.d,this},Matrix2D.prototype.preScale=function(t,e){return void 0===e&&(e=t),this.a*=t,this.b*=e,this.c*=t,this.d*=e,this},Matrix2D.prototype.preTranslate=function(t,e){return"number"==typeof t?(this.e+=t,this.f+=e):(this.e+=t.x,this.f+=t.y),this},Matrix2D.prototype.rotate=function(t){var e=Math.cos(t),i=Math.sin(t),n=this.a;return this.a=e*n-i*this.b,this.b=i*n+e*this.b,n=this.c,this.c=e*n-i*this.d,this.d=i*n+e*this.d,n=this.e,this.e=e*n-i*this.f,this.f=i*n+e*this.f,this},Matrix2D.prototype.scale=function(t,e){return void 0===e&&(e=t),this.a*=t,this.b*=e,this.c*=t,this.d*=e,this.e*=t,this.f*=e,this},Matrix2D.prototype.setValues=function(t,e,i,n,s,r){return t instanceof Matrix2D?(this.a=t.a,this.b=t.b,this.c=t.c,this.d=t.d,this.e=t.e,this.f=t.f):(this.a=t,this.b=e,this.c=i,this.d=n,this.e=s,this.f=r),this},Matrix2D.prototype.toString=function(){return"Matrix2D(["+this.a+", "+this.c+", "+this.e+"] ["+this.b+", "+this.d+", "+this.f+"] [0, 0, 1])"},Matrix2D.prototype.translate=function(t,e){return"number"==typeof t?(this.e+=this.a*t+this.c*e,this.f+=this.b*t+this.d*e):(this.e+=this.a*t.x+this.c*t.y,this.f+=this.b*t.x+this.d*t.y),this},Matrix2D}()},{"./core.js":9}],19:[function(t){var e=t("./core.js"),i=window.document;e.c("Particles",{init:function(){this._Particles=e.clone(this._Particles),this._Particles.parentEntity=this},particles:function(t){if(!e.support.canvas||e.deactivateParticles)return this;var n,s,r,o,a;n=i.createElement("canvas"),n.width=e.viewport.width,n.height=e.viewport.height,n.style.position="absolute",n.style.left="0px",n.style.top="0px",e.stage.elem.appendChild(n),s=n.getContext("2d"),this._Particles.init(t),this.bind("Remove",function(){e.stage.elem.removeChild(n)}).bind("RemoveComponent",function(t){"particles"===t&&e.stage.elem.removeChild(n)}),r=this.x+e.viewport.x,o=this.y+e.viewport.y,this._Particles.position=this._Particles.vectorHelpers.create(r,o);var h={x:e.viewport.x,y:e.viewport.y};return this.bind("EnterFrame",function(){r=this.x+e.viewport.x,o=this.y+e.viewport.y,this._Particles.viewportDelta={x:e.viewport.x-h.x,y:e.viewport.y-h.y},h={x:e.viewport.x,y:e.viewport.y},this._Particles.position=this._Particles.vectorHelpers.create(r,o),"function"==typeof e.DrawManager.boundingRect?(a=e.DrawManager.boundingRect(this._Particles.register),a&&s.clearRect(a._x,a._y,a._w,a._h)):s.clearRect(0,0,e.viewport.width,e.viewport.height),this._Particles.update(),this._Particles.render(s)}),this},_Particles:{presets:{maxParticles:150,size:18,sizeRandom:4,speed:1,speedRandom:1.2,lifeSpan:29,lifeSpanRandom:7,angle:65,angleRandom:34,startColour:[255,131,0,1],startColourRandom:[48,50,45,0],endColour:[245,35,0,0],endColourRandom:[60,60,60,0],sharpness:20,sharpnessRandom:10,spread:10,duration:-1,fastMode:!1,gravity:{x:0,y:.1},jitter:0,particles:[],active:!0,particleCount:0,elapsedFrames:0,emissionRate:0,emitCounter:0,particleIndex:0},init:function(t){this.position=this.vectorHelpers.create(0,0),t===void 0&&(t={});for(var e in this.presets)this[e]=t[e]!==void 0?t[e]:this.presets[e];this.emissionRate=this.maxParticles/this.lifeSpan,this.positionRandom=this.vectorHelpers.create(this.spread,this.spread)},addParticle:function(){if(this.particleCount==this.maxParticles)return!1;var t=new this.particle(this.vectorHelpers);return this.initParticle(t),this.particles[this.particleCount]=t,this.particleCount++,!0},RANDM1TO1:function(){return 2*Math.random()-1},initParticle:function(t){t.position.x=this.position.x+this.positionRandom.x*this.RANDM1TO1(),t.position.y=this.position.y+this.positionRandom.y*this.RANDM1TO1();var e=(this.angle+this.angleRandom*this.RANDM1TO1())*(Math.PI/180),i=this.vectorHelpers.create(Math.sin(e),-Math.cos(e)),n=this.speed+this.speedRandom*this.RANDM1TO1();t.direction=this.vectorHelpers.multiply(i,n),t.size=this.size+this.sizeRandom*this.RANDM1TO1(),t.size=0>t.size?0:~~t.size,t.timeToLive=this.lifeSpan+this.lifeSpanRandom*this.RANDM1TO1(),t.sharpness=this.sharpness+this.sharpnessRandom*this.RANDM1TO1(),t.sharpness=t.sharpness>100?100:0>t.sharpness?0:t.sharpness,t.sizeSmall=~~(t.size/200*t.sharpness);var s=[this.startColour[0]+this.startColourRandom[0]*this.RANDM1TO1(),this.startColour[1]+this.startColourRandom[1]*this.RANDM1TO1(),this.startColour[2]+this.startColourRandom[2]*this.RANDM1TO1(),this.startColour[3]+this.startColourRandom[3]*this.RANDM1TO1()],r=[this.endColour[0]+this.endColourRandom[0]*this.RANDM1TO1(),this.endColour[1]+this.endColourRandom[1]*this.RANDM1TO1(),this.endColour[2]+this.endColourRandom[2]*this.RANDM1TO1(),this.endColour[3]+this.endColourRandom[3]*this.RANDM1TO1()];t.colour=s,t.deltaColour[0]=(r[0]-s[0])/t.timeToLive,t.deltaColour[1]=(r[1]-s[1])/t.timeToLive,t.deltaColour[2]=(r[2]-s[2])/t.timeToLive,t.deltaColour[3]=(r[3]-s[3])/t.timeToLive},update:function(){if(this.active&&this.emissionRate>0){var t=1/this.emissionRate;for(this.emitCounter++;this.particleCountt;)this.addParticle(),this.emitCounter-=t;this.elapsedFrames++,-1!=this.duration&&this.duration0){i.direction=this.vectorHelpers.add(i.direction,this.gravity),i.position=this.vectorHelpers.add(i.position,i.direction),i.position=this.vectorHelpers.add(i.position,this.viewportDelta),this.jitter&&(i.position.x+=this.jitter*this.RANDM1TO1(),i.position.y+=this.jitter*this.RANDM1TO1()),i.timeToLive--;var n=i.colour[0]+=i.deltaColour[0],s=i.colour[1]+=i.deltaColour[1],r=i.colour[2]+=i.deltaColour[2],o=i.colour[3]+=i.deltaColour[3];e=[],e.push("rgba("+(n>255?255:0>n?0:~~n)),e.push(s>255?255:0>s?0:~~s),e.push(r>255?255:0>r?0:~~r),e.push((o>1?1:0>o?0:o.toFixed(2))+")"),i.drawColour=e.join(","),this.fastMode||(e[3]="0)",i.drawColourEnd=e.join(",")),this.particleIndex++}else this.particleIndex!=this.particleCount-1&&(this.particles[this.particleIndex]=this.particles[this.particleCount-1]),this.particleCount--;var a={};a._x=~~i.position.x,a._y=~~i.position.y,a._w=i.size,a._h=i.size,this.register.push(a)}},stop:function(){this.active=!1,this.elapsedFrames=0,this.emitCounter=0,this.parentEntity.trigger("ParticleEnd")},render:function(t){for(var i=0,n=this.particleCount;n>i;i++){var s=this.particles[i],r=s.size,o=r>>1;if(!(0>s.position.x+r||0>s.position.y+r||s.position.x-r>e.viewport.width||s.position.y-r>e.viewport.height)){var a=~~s.position.x,h=~~s.position.y;if(this.fastMode)t.fillStyle=s.drawColour;else{var c=t.createRadialGradient(a+o,h+o,s.sizeSmall,a+o,h+o,o);c.addColorStop(0,s.drawColour),c.addColorStop(.9,s.drawColourEnd),t.fillStyle=c}t.fillRect(a,h,r,r)}}},particle:function(t){this.position=t.create(0,0),this.direction=t.create(0,0),this.size=0,this.sizeSmall=0,this.timeToLive=0,this.colour=[],this.drawColour="",this.deltaColour=[],this.sharpness=0},vectorHelpers:{create:function(t,e){return{x:t,y:e}},multiply:function(t,e){return t.x*=e,t.y*=e,t},add:function(t,e){return t.x+=e.x,t.y+=e.y,t}}}})},{"./core.js":9}],20:[function(t){var e=t("./core.js"),i=window.document;e.extend({audio:{sounds:{},supported:null,codecs:{ogg:'audio/ogg; codecs="vorbis"',wav:'audio/wav; codecs="1"',webma:'audio/webm; codecs="vorbis"',mp3:'audio/mpeg; codecs="mp3"',m4a:'audio/mp4; codecs="mp4a.40.2"'},volume:1,muted:!1,paused:!1,playCheck:null,_canPlay:function(){if(this.supported={},e.support.audio){var t,i=this.audioElement();for(var n in this.codecs)t=i.canPlayType(this.codecs[n]),this.supported[n]=""!==t&&"no"!==t?!0:!1}},supports:function(t){return null===this.supported&&this._canPlay(),this.supported[t]?!0:!1},audioElement:function(){return"undefined"!=typeof Audio?new Audio(""):i.createElement("audio")},create:function(t,i){var n=i.substr(i.lastIndexOf(".")+1).toLowerCase();if(!this.supports(n))return!1;var s=this.audioElement();return s.id=t,s.preload="auto",s.volume=e.audio.volume,s.src=i,e.asset(i,s),this.sounds[t]={obj:s,played:0,volume:e.audio.volume},this.sounds[t]},add:function(t,i){if(e.support.audio){var n;if(1===arguments.length&&"object"==typeof t)for(var s in t)for(n in t[s])if(e.audio.create(s,t[s][n]))break;if("string"==typeof t&&("string"==typeof i&&e.audio.create(t,i),"object"==typeof i))for(n in i)if(e.audio.create(t,i[n]))break}},play:function(t,i,n){if(0!==i&&e.support.audio&&this.sounds[t]){var s=this.sounds[t],r=this.getOpenChannel();if(!r)return null;r.id=t,r.repeat=i;var o=r.obj;return r.volume=s.volume=s.obj.volume=n||e.audio.volume,o.volume=s.volume,o.src=s.obj.src,this.muted&&(o.volume=0),o.play(),s.played++,r.onEnd=function(){s.playedt&&(this.channels.length=t)},channels:[],getOpenChannel:function(){for(var t=0;this.channels.length>t;t++){var e=this.channels[t];if(e.active===!1||e.obj.ended&&e.repeat<=this.sounds[e.id].played)return e.active=!0,e}if(this.maxChannels>t){var i={obj:this.audioElement(),active:!0,_is:function(t){return this.id===t&&this.active}};return this.channels.push(i),i}return null},remove:function(t){if(e.support.audio){var i;if(t)this.sounds[t]&&(i=this.sounds[t],e.audio.stop(t),delete e.assets[i.obj.src],delete e.audio.sounds[t]);else for(var n in this.sounds)i=this.sounds[n],e.audio.stop(t),delete e.assets[i.obj.src],delete e.audio.sounds[t]}},stop:function(t){if(e.support.audio)for(var i in this.channels)c=this.channels[i],(!t&&c.active||c._is(t))&&(c.active=!1,c.obj.pause())},_mute:function(t){if(e.support.audio){var i;for(var n in this.channels)i=this.channels[n],i.obj.volume=t?0:i.volume;this.muted=t}},toggleMute:function(){this.muted?this._mute(!1):this._mute(!0)},mute:function(){this._mute(!0)},unmute:function(){this._mute(!1)},pause:function(t){if(e.support.audio&&t&&this.sounds[t]){var i;for(var n in this.channels)i=this.channels[n],i._is(t)&&!i.obj.paused&&i.obj.pause()}},unpause:function(t){if(e.support.audio&&t&&this.sounds[t]){var i;for(var n in this.channels)i=this.channels[n],i._is(t)&&i.obj.paused&&i.obj.play()}},togglePause:function(t){if(e.support.audio&&t&&this.sounds[t]){var i;for(var n in this.channels)i=this.channels[n],i._is(t)&&(i.obj.paused?i.obj.play():i.obj.pause())}}}})},{"./core.js":9}],21:[function(t){var e=t("./core.js");window.document,e.c("Sprite",{__image:"",__tile:0,__tileh:0,__padding:null,__trim:null,img:null,ready:!1,init:function(){this.__trim=[0,0,0,0];var t=function(t){var e=t.co,i=t.pos,n=t.ctx;if("canvas"===t.type)n.drawImage(this.img,e.x,e.y,e.w,e.h,i._x,i._y,i._w,i._h);else if("DOM"===t.type){var s=this._h/e.h,r=this._w/e.w,o=this._element.style;o.background=o.backgroundColor+" url('"+this.__image+"') no-repeat -"+e.x*r+"px -"+e.y*s+"px",(1!=s||1!=r)&&(o.backgroundSize=this.img.width*r+"px"+" "+this.img.height*s+"px")}};this.bind("Draw",t).bind("RemoveComponent",function(e){"Sprite"===e&&this.unbind("Draw",t)})},sprite:function(t,e,i,n){return this.__coord=[t*(this.__tile+this.__padding[0])+this.__trim[0],e*(this.__tileh+this.__padding[1])+this.__trim[1],this.__trim[2]||i*this.__tile||this.__tile,this.__trim[3]||n*this.__tileh||this.__tileh],this.trigger("Change"),this},crop:function(t,e,i,n){var s=this._mbr||this.pos();return this.__trim=[],this.__trim[0]=t,this.__trim[1]=e,this.__trim[2]=i,this.__trim[3]=n,this.__coord[0]+=t,this.__coord[1]+=e,this.__coord[2]=i,this.__coord[3]=n,this._w=i,this._h=n,this.trigger("Change",s),this}})},{"./core.js":9}],22:[function(require,module,exports){var Crafty=require("./core.js"),document=window.document;Crafty.storage=function(){function process(t){if(t.c){var e=Crafty.e(t.c).attr(t.attr).trigger("LoadData",t,process);return e}if("object"==typeof t)for(var i in t)t[i]=process(t[i]);return t}function unserialize(str){if("string"!=typeof str)return null;var data=JSON?JSON.parse(str):eval("("+str+")");return process(data)}function prep(t){if(t.__c){var e={c:[],attr:{}};t.trigger("SaveData",e,prep);for(var i in t.__c)e.c.push(i);e.c=e.c.join(", "),t=e}else if("object"==typeof t)for(var n in t)t[n]=prep(t[n]);return t}function serialize(t){if(JSON){var e=prep(t);return JSON.stringify(e)}return alert("Crafty does not support saving on your browser. Please upgrade to a newer browser."),!1}function external(t){url=t}function openExternal(){if(void 0!==url){var xml=new XMLHttpRequest;xhr.open("POST",url),xhr.onreadystatechange=function(evt){if(4==xhr.readyState&&200==xhr.status){var data=eval("("+xhr.responseText+")");for(var i in data)Crafty.storage.check(data[i].key,data[i].timestamp)&&loadExternal(data[i].key)}},xhr.send("mode=timestamps&game="+gameName)}}function saveExternal(t,e,i){if(void 0!==url){var n=new XMLHttpRequest;n.open("POST",url),n.send("mode=save&key="+t+"&data="+encodeURIComponent(e)+"&ts="+i+"&game="+gameName)}}function loadExternal(key){if(void 0!==url){var xhr=new XMLHttpRequest;xhr.open("POST",url),xhr.onreadystatechange=function(evt){if(4==xhr.readyState&&200==xhr.status){var data=eval("("+xhr.responseText+")");Crafty.storage.save(key,"save",data)}},xhr.send("mode=load&key="+key+"&game="+gameName)}}function ts(){var t=new Date;return t.getTime()}var db=null,url,gameName,timestamps={},transactionType={READ:"readonly",READ_WRITE:"readwrite"};return"object"!=typeof indexedDB&&(window.indexedDB=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB,window.IDBTransaction=window.IDBTransaction||window.webkitIDBTransaction,window.IDBKeyRange=window.IDBKeyRange||window.webkitIDBKeyRange||window.msIDBKeyRange,"object"==typeof IDBTransaction&&(transactionType.READ=IDBTransaction.READ||IDBTransaction.readonly||transactionType.READ||"read",transactionType.READ_WRITE=IDBTransaction.READ_WRITE||IDBTransaction.readwrite||transactionType.READ_WRITE||"readwrite")),"object"==typeof indexedDB?{open:function(t){function e(){try{var t=db.transaction(["save"],"read"),e=t.objectStore("save"),i=e.getAll();i.onsuccess=function(){for(var t=0,e=event.target.result,i=e.length;i>t;t++)timestamps[e[t].key]=e[t].timestamp}}catch(n){}}function i(){var t=db.setVersion("1.0");t.onsuccess=function(){for(var t=0;n.length>t;t++){var e=n[t];db.objectStoreNames.contains(e)||db.createObjectStore(e,{keyPath:"key"})}}}gameName=t;var n=[];if(1==arguments.length?(n.push("save"),n.push("cache")):(n=arguments,n.shift(),n.push("save"),n.push("cache")),null===db){var s=indexedDB.open(gameName);s.onsuccess=function(t){db=t.target.result,e(),openExternal()},s.onupgradeneeded=function(){i()}}else i(),e(),openExternal()},save:function(t,e,i,n){if(null===db)return setTimeout(function(){Crafty.storage.save(t,e,i)},1),void 0;var s=serialize(i),r=ts();"save"==e&&saveExternal(t,s,r);try{var o=db.transaction([e],transactionType.READ_WRITE).objectStore(e).add({data:s,timestamp:r,key:t});"function"==typeof n&&(o.onsuccess=n)}catch(a){console.error(a)}},load:function(t,e,i){if(null===db)return setTimeout(function(){Crafty.storage.load(t,e,i)},1),void 0;try{var n=db.transaction([e],transactionType.READ).objectStore(e).get(t);n.onsuccess=function(t){i(unserialize(t.target.result.data))}}catch(s){console.error(s)}},getAllKeys:function(t,e){null===db&&setTimeout(function(){Crafty.storage.getAllkeys(t,e)},1);try{var i=db.transaction([t],transactionType.READ).objectStore(t).openCursor(),n=[];i.onsuccess=function(t){var i=t.target.result;i?(n.push(i.key),i["continue"]()):e(n)}}catch(s){console.error(s)}},check:function(t,e){return timestamps[t]>e},external:external}:"function"==typeof openDatabase?{open:function(t){if(gameName=t,1==arguments.length)db={save:openDatabase(t+"_save","1.0","Saves games for "+t,5242880),cache:openDatabase(t+"_cache","1.0","Cache for "+t,5242880)};else{var e=arguments,i=0;for(e.shift();e.length>i;i++)db[e[i]]===void 0&&(db[e[i]]=openDatabase(gameName+"_"+e[i],"1.0",type,5242880))}db.save.transaction(function(t){t.executeSql("SELECT key, timestamp FROM data",[],function(t,e){for(var i=0,n=e.rows,s=n.length;s>i;i++)timestamps[n.item(i).key]=n.item(i).timestamp})})},save:function(t,e,i){db[e]===void 0&&""!==gameName&&this.open(gameName,e);var n=serialize(i),s=ts();"save"==e&&saveExternal(t,n,s),db[e].transaction(function(e){e.executeSql("CREATE TABLE IF NOT EXISTS data (key unique, text, timestamp)"),e.executeSql("SELECT * FROM data WHERE key = ?",[t],function(e,i){i.rows.length?e.executeSql("UPDATE data SET text = ?, timestamp = ? WHERE key = ?",[n,s,t]):e.executeSql("INSERT INTO data VALUES (?, ?, ?)",[t,n,s])})})},load:function(t,e,i){return db[e]===void 0?(setTimeout(function(){Crafty.storage.load(t,e,i)},1),void 0):(db[e].transaction(function(e){e.executeSql("SELECT text FROM data WHERE key = ?",[t],function(t,e){e.rows.length&&(res=unserialize(e.rows.item(0).text),i(res))})}),void 0)},getAllKeys:function(t,e){return db[t]===void 0?(setTimeout(function(){Crafty.storage.getAllKeys(t,e)},1),void 0):(db[t].transaction(function(t){t.executeSql("SELECT key FROM data",[],function(t,i){e(i.rows)})}),void 0)},check:function(t,e){return timestamps[t]>e},external:external}:"object"==typeof window.localStorage?{open:function(t){gameName=t},save:function(t,e,i){var n=gameName+"."+e+"."+t,s=serialize(i),r=ts();"save"==e&&saveExternal(t,s,r),window.localStorage[n]=s,"save"==e&&(window.localStorage[n+".ts"]=r)},load:function(t,e,i){var n=gameName+"."+e+"."+t,s=window.localStorage[n];i(unserialize(s))},getAllKeys:function(t,e){var i={},n=[],s=gameName+"."+t;for(var r in window.localStorage)if(-1!=r.indexOf(s)){var o=r.replace(s,"").replace(".ts","");i[o]=!0}for(r in i)n.push(r);e(n)},check:function(t,e){var i=window.localStorage[gameName+".save."+t+".ts"];return parseInt(e,10)>parseInt(i,10)},external:external}:{open:function(t){gameName=t},save:function(t,e,i){if("save"==e){var n=serialize(i),s=ts();"save"==e&&saveExternal(t,n,s),document.cookie=gameName+"_"+t+"="+n+"; "+gameName+"_"+t+"_ts="+s+"; expires=Thur, 31 Dec 2099 23:59:59 UTC; path=/"}},load:function(t,e,i){if("save"==e){var n=RegExp(gameName+"_"+t+"=[^;]*"),s=n.exec(document.cookie),r=unserialize(s[0].replace(gameName+"_"+t+"=",""));i(r)}},getAllKeys:function(t,e){if("save"==t){for(var i=RegExp(gameName+"_[^_=]","g"),n=i.exec(document.cookie),s=0,r=n.length,o={},a=[];r>s;s++){var h=n[s].replace(gameName+"_","");o[h]=!0}for(s in o)a.push(s);e(a)}},check:function(t,e){var i=gameName+"_"+t+"_ts",n=RegExp(i+"=[^;]"),s=n.exec(document.cookie),r=s[0].replace(i+"=","");return parseInt(e,10)>parseInt(r,10)},external:external}}()},{"./core.js":9}],23:[function(t){var e=t("./core.js");window.document,e.c("Text",{_text:"",defaultSize:"10px",defaultFamily:"sans-serif",ready:!0,init:function(){this.requires("2D"),this._textFont={type:"",weight:"",size:this.defaultSize,family:this.defaultFamily},this.bind("Draw",function(t){var e=this._fontString();if("DOM"===t.type){var i=this._element,n=i.style;n.color=this._textColor,n.font=e,i.innerHTML=this._text}else if("canvas"===t.type){var s=t.ctx;s.save(),s.textBaseline="top",s.fillStyle=this._textColor||"rgb(0,0,0)",s.font=e,s.fillText(this._text,this._x,this._y),s.restore()}})},_getFontHeight:function(){var t=/([a-zA-Z]+)\b/,e={px:1,pt:4/3,pc:16,cm:96/2.54,mm:96/25.4,"in":96,em:void 0,ex:void 0};return function(i){var n=parseFloat(i),s=t.exec(i),r=s?s[1]:"px";return void 0!==e[r]?Math.ceil(n*e[r]):Math.ceil(n)}}(),text:function(t){return void 0===t||null===t?this._text:(this._text="function"==typeof t?t.call(this):t,this.has("Canvas")&&this._resizeForCanvas(),this.trigger("Change"),this)},_resizeForCanvas:function(){var t=e.canvas.context;t.font=this._fontString(),this.w=t.measureText(this._text).width;var i=this._textFont.size||this.defaultSize;this.h=1.1*this._getFontHeight(i)},_fontString:function(){return this._textFont.type+" "+this._textFont.weight+" "+this._textFont.size+" "+this._textFont.family},textColor:function(t,i){return this._strength=i,this._textColor=e.toRGB(t,this._strength),this.trigger("Change"),this},textFont:function(t,e){if(1===arguments.length){if("string"==typeof t)return this._textFont[t];if("object"==typeof t)for(var i in t)this._textFont[i]="family"==i?"'"+t[i]+"'":t[i]}else this._textFont[t]=e;return this.has("Canvas")&&this._resizeForCanvas(),this.trigger("Change"),this},unselectable:function(){return this.has("DOM")&&(this.css({"-webkit-touch-callout":"none","-webkit-user-select":"none","-khtml-user-select":"none","-moz-user-select":"none","-ms-user-select":"none","user-select":"none"}),this.trigger("Change")),this}})},{"./core.js":9}],24:[function(t){var e=t("./core.js");window.document,e.c("Delay",{init:function(){this._delays=[],this.bind("EnterFrame",function(){for(var t=(new Date).getTime(),e=this._delays.length;--e>=0;){var i=this._delays[e];t>i.start+i.delay+i.pause&&(i.func.call(this),i.repeat>0?(i.start=t,i.pause=0,i.pauseBuffer=0,i.repeat--):0>=i.repeat&&this._delays.splice(e,1))}}),this.bind("Pause",function(){var t=(new Date).getTime();for(var e in this._delays)this._delays[e].pauseBuffer=t}),this.bind("Unpause",function(){var t=(new Date).getTime();for(var e in this._delays){var i=this._delays[e];i.pause+=t-i.pauseBuffer}})},delay:function(t,e,i){return this._delays.push({start:(new Date).getTime(),func:t,delay:e,repeat:(0>i?1/0:i)||0,pauseBuffer:0,pause:0}),this}})},{"./core.js":9}],25:[function(t,e){e.exports="0.6.0"},{}],26:[function(t){var e=t("./core.js"),i=window.document;e.extend({viewport:{clampToEntities:!0,width:0,height:0,_x:0,_y:0,_scale:1,bounds:null,scroll:function(t,i){i=Math.floor(i),this[t]=i,e.trigger("ViewportScroll"),e.trigger("InvalidateViewport")},rect:function(){return{_x:-this._x/this._scale,_y:-this._y/this._scale,_w:this.width/this._scale,_h:this.height/this._scale}},pan:function(){function t(){var t=0;for(var n in i){var s=i[n];s.remTime>0?(s.current+=s.diff,s.remTime--,e.viewport[n]=Math.floor(s.current),t++):delete i[n]}t&&e.viewport._clamp()}var i={},n=!1;return function(s,r,o){if(e.viewport.follow(),"reset"!=s)0===o&&(o=1),i[s]={diff:-r/o,current:e.viewport[s],remTime:o},n||(e.bind("EnterFrame",t),n=!0);else for(var a in i)i[a].remTime=0}}(),follow:function(){function t(){e.viewport.scroll("_x",-(this.x+this.w/2-e.viewport.width/2-n)),e.viewport.scroll("_y",-(this.y+this.h/2-e.viewport.height/2-s)),e.viewport._clamp()}var i,n,s;return function(r,o,a){i&&i.unbind("Change",t),r&&r.has("2D")&&(e.viewport.pan("reset"),i=r,n=o!==void 0?o:0,s=a!==void 0?a:0,r.bind("Change",t),t.call(r))}}(),centerOn:function(t,i){var n=t.x+e.viewport.x,s=t.y+e.viewport.y,r=t.w/2,o=t.h/2,a=e.viewport.width/2,h=e.viewport.height/2,c=n+r-a,u=s+o-h;e.viewport.pan("reset"),e.viewport.pan("x",c,i),e.viewport.pan("y",u,i)},_zoom:1,zoom:function(){function t(){if(s>0){isFinite(e.viewport._zoom)&&(i=e.viewport._zoom);var t={width:a.width*i,height:a.height*i};i+=n,e.viewport._zoom=i;var o={width:a.width*i,height:a.height*i},c={width:o.width-t.width,height:o.height-t.height};if(e.stage.inner.style[r]="scale("+i+","+i+")",e.canvas._canvas){var u=i/(i-n);e.canvas.context.scale(u,u),e.trigger("InvalidateViewport")}e.viewport.x-=c.width*h.width,e.viewport.y-=c.height*h.height,s--}}var i=1,n=0,s=0,r=e.support.prefix+"Transform",o=!1,a={},h={};return function(r,c,u,l){var d=this.bounds||e.map.boundaries(),_=r?i*r:1;r||(i=1,this._zoom=1),a.width=d.max.x-d.min.x,a.height=d.max.y-d.min.y,h.width=c/a.width,h.height=u/a.height,0===l&&(l=1),n=(_-i)/l,s=l,e.viewport.pan("reset"),o||(e.bind("EnterFrame",t),o=!0)}}(),scale:function(){return function(t){var i=(this.bounds||e.map.boundaries(),t?t:1);this._zoom=i,this._scale=i,e.trigger("InvalidateViewport"),e.trigger("ViewportScale")}}(),mouselook:function(){var t=!1,i=!1,n={};return old={},function(s,r){if("boolean"==typeof s)return t=s,t?e.mouseObjs++:e.mouseObjs=Math.max(0,e.mouseObjs-1),void 0;if(t)switch(s){case"move":case"drag":if(!i)return;diff={x:r.clientX-n.x,y:r.clientY-n.y},n.x=r.clientX,n.y=r.clientY,e.viewport.x+=diff.x,e.viewport.y+=diff.y,e.viewport._clamp();break;case"start":n.x=r.clientX,n.y=r.clientY,i=!0;break;case"stop":i=!1}}}(),_clamp:function(){if(this.clampToEntities){var t=this.bounds||e.map.boundaries();t.max.x*=this._zoom,t.min.x*=this._zoom,t.max.y*=this._zoom,t.min.y*=this._zoom,t.max.x-t.min.x>e.viewport.width?(t.max.x-=e.viewport.width,e.viewport.x<-t.max.x?e.viewport.x=-t.max.x:e.viewport.x>-t.min.x&&(e.viewport.x=-t.min.x)):e.viewport.x=-1*(t.min.x+(t.max.x-t.min.x)/2-e.viewport.width/2),t.max.y-t.min.y>e.viewport.height?(t.max.y-=e.viewport.height,e.viewport.y<-t.max.y?e.viewport.y=-t.max.y:e.viewport.y>-t.min.y&&(e.viewport.y=-t.min.y)):e.viewport.y=-1*(t.min.y+(t.max.y-t.min.y)/2-e.viewport.height/2) +}},init:function(t,n,s){e.DOM.window.init(),this.width=!t||e.mobile?e.DOM.window.width:t,this.height=!n||e.mobile?e.DOM.window.height:n,s===void 0&&(s="cr-stage");var r;if("string"==typeof s)r=i.getElementById(s);else{if(!("undefined"!=typeof HTMLElement?s instanceof HTMLElement:s instanceof Element))throw new TypeError("stage_elem must be a string or an HTMLElement");r=s}e.stage={x:0,y:0,fullscreen:!1,elem:r?r:i.createElement("div"),inner:i.createElement("div")},(!t&&!n||e.mobile)&&(i.body.style.overflow="hidden",e.stage.fullscreen=!0),e.addEvent(this,window,"resize",e.viewport.reload),e.addEvent(this,window,"blur",function(){e.settings.get("autoPause")&&(e._paused||e.pause())}),e.addEvent(this,window,"focus",function(){e._paused&&e.settings.get("autoPause")&&e.pause()}),e.settings.register("stageSelectable",function(t){e.stage.elem.onselectstart=t?function(){return!0}:function(){return!1}}),e.settings.modify("stageSelectable",!1),e.settings.register("stageContextMenu",function(t){e.stage.elem.oncontextmenu=t?function(){return!0}:function(){return!1}}),e.settings.modify("stageContextMenu",!1),e.settings.register("autoPause",function(){}),e.settings.modify("autoPause",!1),r||(i.body.appendChild(e.stage.elem),e.stage.elem.id=s);var o,a=e.stage.elem.style;if(e.stage.elem.appendChild(e.stage.inner),e.stage.inner.style.position="absolute",e.stage.inner.style.zIndex="1",e.stage.inner.style.transformStyle="preserve-3d",a.width=this.width+"px",a.height=this.height+"px",a.overflow="hidden",e.mobile){a.position="absolute",a.left="0px",a.top="0px",void 0!==typeof a.webkitTapHighlightColor&&(a.webkitTapHighlightColor="rgba(0,0,0,0)");var h=i.createElement("meta"),c=i.getElementsByTagName("HEAD")[0];h.setAttribute("name","viewport"),h.setAttribute("content","width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"),c.appendChild(h),h=i.createElement("meta"),h.setAttribute("name","apple-mobile-web-app-capable"),h.setAttribute("content","yes"),c.appendChild(h),setTimeout(function(){window.scrollTo(0,1)},0),e.addEvent(this,window,"touchmove",function(t){t.preventDefault()}),e.stage.x=0,e.stage.y=0}else a.position="relative",o=e.DOM.inner(e.stage.elem),e.stage.x=o.x,e.stage.y=o.y;e.support.setter?(this.__defineSetter__("x",function(t){this.scroll("_x",t)}),this.__defineSetter__("y",function(t){this.scroll("_y",t)}),this.__defineGetter__("x",function(){return this._x}),this.__defineGetter__("y",function(){return this._y})):e.support.defineProperty?(Object.defineProperty(this,"x",{set:function(t){this.scroll("_x",t)},get:function(){return this._x},configurable:!0}),Object.defineProperty(this,"y",{set:function(t){this.scroll("_y",t)},get:function(){return this._y},configurable:!0})):(this.x=this._x,this.y=this._y,e.bind("EnterFrame",function(){e.viewport._x!==e.viewport.x&&e.viewport.scroll("_x",e.viewport.x),e.viewport._y!==e.viewport.y&&e.viewport.scroll("_y",e.viewport.y)}))},reload:function(){e.DOM.window.init();var t,i=e.DOM.window.width,n=e.DOM.window.height;e.stage.fullscreen&&(this.width=i,this.height=n,e.stage.elem.style.width=i+"px",e.stage.elem.style.height=n+"px",e.canvas._canvas&&(e.canvas._canvas.width=i,e.canvas._canvas.height=n,e.trigger("InvalidateViewport"))),t=e.DOM.inner(e.stage.elem),e.stage.x=t.x,e.stage.y=t.y},reset:function(){e.viewport.pan("reset"),e.viewport.follow(),e.viewport.mouselook("stop"),e.viewport.scale()}}})},{"./core.js":9}]},{},[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]); \ No newline at end of file diff --git a/dist/crafty.js b/dist/crafty.js new file mode 100644 index 00000000..173674b8 --- /dev/null +++ b/dist/crafty.js @@ -0,0 +1,12573 @@ +/** + * crafty 0.6.0 + * http://craftyjs.com/ + * + * Copyright 2013, Louis Stowasser + * Dual licensed under the MIT or GPL licenses. + */ + + +;(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 0) { + this._cascade(e); + } + }); + + this.bind("Rotate", function (e) { + var old = this._mbr || this; + this._entry.update(old); + // Rotate children (if any) by the same amount + if (this._children.length > 0) { + this._cascade(e); + } + }); + + //when object is removed, remove from HashMap and destroy attached children + this.bind("Remove", function () { + if (this._children) { + for (var i = 0; i < this._children.length; i++) { + // delete the child's _parent link, or else the child will splice itself out of + // this._children while destroying itself (which messes up this for-loop iteration). + delete this._children[i]._parent; + + // Destroy child if possible (It's not always possible, e.g. the polygon attached + // by areaMap has no .destroy(), it will just get garbage-collected.) + if (this._children[i].destroy) { + this._children[i].destroy(); + } + } + this._children = []; + } + + if (this._parent) { + this._parent.detach(this); + } + + Crafty.map.remove(this); + + this.detach(); + }); + }, + + + /** + * Calculates the MBR when rotated some number of radians about an origin point o. + * Necessary on a rotation, or a resize (when already rotated) + */ + + _calculateMBR: function (ox, oy, rad) { + if (rad === 0) { + this._mbr = null; + return; + } + + var ct = Math.cos(rad), + st = Math.sin(rad); + // Special case 90 degree rotations to prevent rounding problems + ct = (ct < 1e-10 && ct > -1e-10) ? 0 : ct; + st = (st < 1e-10 && st > -1e-10) ? 0 : st; + var x0 = ox + (this._x - ox) * ct + (this._y - oy) * st, + y0 = oy - (this._x - ox) * st + (this._y - oy) * ct, + x1 = ox + (this._x + this._w - ox) * ct + (this._y - oy) * st, + y1 = oy - (this._x + this._w - ox) * st + (this._y - oy) * ct, + x2 = ox + (this._x + this._w - ox) * ct + (this._y + this._h - oy) * st, + y2 = oy - (this._x + this._w - ox) * st + (this._y + this._h - oy) * ct, + x3 = ox + (this._x - ox) * ct + (this._y + this._h - oy) * st, + y3 = oy - (this._x - ox) * st + (this._y + this._h - oy) * ct, + minx = Math.floor(Math.min(x0, x1, x2, x3)), + miny = Math.floor(Math.min(y0, y1, y2, y3)), + maxx = Math.ceil(Math.max(x0, x1, x2, x3)), + maxy = Math.ceil(Math.max(y0, y1, y2, y3)); + if (!this._mbr) { + this._mbr = { + _x: minx, + _y: miny, + _w: maxx - minx, + _h: maxy - miny + }; + } else { + this._mbr._x = minx; + this._mbr._y = miny; + this._mbr._w = maxx - minx; + this._mbr._h = maxy - miny; + } + + }, + + /** + * Handle changes that need to happen on a rotation + */ + _rotate: function (v) { + var theta = -1 * (v % 360); //angle always between 0 and 359 + var difference = this._rotation - v; + // skip if there's no rotation! + if (difference === 0) + return; + + //Calculate the new MBR + var rad = theta * DEG_TO_RAD, + o = { + x: this._origin.x + this._x, + y: this._origin.y + this._y + }; + + this._calculateMBR(o.x, o.y, rad); + + + //trigger "Rotate" event + var drad = difference * DEG_TO_RAD, + ct = Math.cos(rad), + st = Math.sin(rad); + + this.trigger("Rotate", { + cos: Math.cos(drad), + sin: Math.sin(drad), + deg: difference, + rad: drad, + o: o, + matrix: { + M11: ct, + M12: st, + M21: -st, + M22: ct + } + }); + }, + + /**@ + * #.area + * @comp 2D + * @sign public Number .area(void) + * Calculates the area of the entity + */ + area: function () { + return this._w * this._h; + }, + + /**@ + * #.intersect + * @comp 2D + * @sign public Boolean .intersect(Number x, Number y, Number w, Number h) + * @param x - X position of the rect + * @param y - Y position of the rect + * @param w - Width of the rect + * @param h - Height of the rect + * @sign public Boolean .intersect(Object rect) + * @param rect - An object that must have the `x, y, w, h` values as properties + * Determines if this entity intersects a rectangle. If the entity is rotated, its MBR is used for the test. + */ + intersect: function (x, y, w, h) { + var rect, mbr = this._mbr || this; + if (typeof x === "object") { + rect = x; + } else { + rect = { + x: x, + y: y, + w: w, + h: h + }; + } + + return mbr._x < rect.x + rect.w && mbr._x + mbr._w > rect.x && + mbr._y < rect.y + rect.h && mbr._h + mbr._y > rect.y; + }, + + /**@ + * #.within + * @comp 2D + * @sign public Boolean .within(Number x, Number y, Number w, Number h) + * @param x - X position of the rect + * @param y - Y position of the rect + * @param w - Width of the rect + * @param h - Height of the rect + * @sign public Boolean .within(Object rect) + * @param rect - An object that must have the `_x, _y, _w, _h` values as properties + * Determines if this current entity is within another rectangle. + */ + within: function (x, y, w, h) { + var rect, mbr = this._mbr || this; + if (typeof x === "object") { + rect = x; + } else { + rect = { + _x: x, + _y: y, + _w: w, + _h: h + }; + } + + return rect._x <= mbr._x && rect._x + rect._w >= mbr._x + mbr._w && + rect._y <= mbr._y && rect._y + rect._h >= mbr._y + mbr._h; + }, + + /**@ + * #.contains + * @comp 2D + * @sign public Boolean .contains(Number x, Number y, Number w, Number h) + * @param x - X position of the rect + * @param y - Y position of the rect + * @param w - Width of the rect + * @param h - Height of the rect + * @sign public Boolean .contains(Object rect) + * @param rect - An object that must have the `_x, _y, _w, _h` values as properties. + * Determines if the rectangle is within the current entity. If the entity is rotated, its MBR is used for the test. + */ + contains: function (x, y, w, h) { + var rect, mbr = this._mbr || this; + if (typeof x === "object") { + rect = x; + } else { + rect = { + _x: x, + _y: y, + _w: w, + _h: h + }; + } + + return rect._x >= mbr._x && rect._x + rect._w <= mbr._x + mbr._w && + rect._y >= mbr._y && rect._y + rect._h <= mbr._y + mbr._h; + }, + + /**@ + * #.pos + * @comp 2D + * @sign public Object .pos(void) + * Returns the x, y, w, h properties as a rect object + * (a rect object is just an object with the keys _x, _y, _w, _h). + * + * The keys have an underscore prefix. This is due to the x, y, w, h + * properties being merely setters and getters that wrap the properties with an underscore (_x, _y, _w, _h). + */ + pos: function () { + return { + _x: (this._x), + _y: (this._y), + _w: (this._w), + _h: (this._h) + }; + }, + + /**@ + * #.mbr + * @comp 2D + * @sign public Object .mbr() + * Returns the minimum bounding rectangle. If there is no rotation + * on the entity it will return the rect. + */ + mbr: function () { + if (!this._mbr) return this.pos(); + return { + _x: (this._mbr._x), + _y: (this._mbr._y), + _w: (this._mbr._w), + _h: (this._mbr._h) + }; + }, + + /**@ + * #.isAt + * @comp 2D + * @sign public Boolean .isAt(Number x, Number y) + * @param x - X position of the point + * @param y - Y position of the point + * Determines whether a point is contained by the entity. Unlike other methods, + * an object can't be passed. The arguments require the x and y value. + * + * The given point is tested against the first of the following that exists: a mapArea associated with "Mouse", the hitarea associated with "Collision", or the object's MBR. + */ + isAt: function (x, y) { + if (this.mapArea) { + return this.mapArea.containsPoint(x, y); + } else if (this.map) { + return this.map.containsPoint(x, y); + } + var mbr = this._mbr || this; + return mbr._x <= x && mbr._x + mbr._w >= x && + mbr._y <= y && mbr._y + mbr._h >= y; + }, + + /**@ + * #.move + * @comp 2D + * @sign public this .move(String dir, Number by) + * @param dir - Direction to move (n,s,e,w,ne,nw,se,sw) + * @param by - Amount to move in the specified direction + * Quick method to move the entity in a direction (n, s, e, w, ne, nw, se, sw) by an amount of pixels. + */ + move: function (dir, by) { + if (dir.charAt(0) === 'n') this.y -= by; + if (dir.charAt(0) === 's') this.y += by; + if (dir === 'e' || dir.charAt(1) === 'e') this.x += by; + if (dir === 'w' || dir.charAt(1) === 'w') this.x -= by; + + return this; + }, + + /**@ + * #.shift + * @comp 2D + * @sign public this .shift(Number x, Number y, Number w, Number h) + * @param x - Amount to move X + * @param y - Amount to move Y + * @param w - Amount to widen + * @param h - Amount to increase height + * Shift or move the entity by an amount. Use negative values + * for an opposite direction. + */ + shift: function (x, y, w, h) { + if (x) this.x += x; + if (y) this.y += y; + if (w) this.w += w; + if (h) this.h += h; + + return this; + }, + + /**@ + * #._cascade + * @comp 2D + * @sign public void ._cascade(e) + * @param e - An object describing the motion + * Move or rotate the entity's children according to a certain motion. + * This method is part of a function bound to "Move": It is used + * internally for ensuring that when a parent moves, the child also + * moves in the same way. + */ + _cascade: function (e) { + if (!e) return; //no change in position + var i = 0, + children = this._children, + l = children.length, + obj; + //rotation + if (e.cos) { + for (; i < l; ++i) { + obj = children[i]; + if ('rotate' in obj) obj.rotate(e); + } + } else { + //use current position + var dx = this._x - e._x, + dy = this._y - e._y, + dw = this._w - e._w, + dh = this._h - e._h; + + for (; i < l; ++i) { + obj = children[i]; + obj.shift(dx, dy, dw, dh); + } + } + }, + + /**@ + * #.attach + * @comp 2D + * @sign public this .attach(Entity obj[, .., Entity objN]) + * @param obj - Child entity(s) to attach + * Sets one or more entities to be children, with the current entity (`this`) + * as the parent. When the parent moves or rotates, its children move or + * rotate by the same amount. (But not vice-versa: If you move a child, it + * will not move the parent.) When the parent is destroyed, its children are + * destroyed. + * + * For any entity, `this._children` is the array of its children entity + * objects (if any), and `this._parent` is its parent entity object (if any). + * + * As many objects as wanted can be attached, and a hierarchy of objects is + * possible by attaching. + */ + attach: function () { + var i = 0, + arg = arguments, + l = arguments.length, + obj; + for (; i < l; ++i) { + obj = arg[i]; + if (obj._parent) { + obj._parent.detach(obj); + } + obj._parent = this; + this._children.push(obj); + } + + return this; + }, + + /**@ + * #.detach + * @comp 2D + * @sign public this .detach([Entity obj]) + * @param obj - The entity to detach. Left blank will remove all attached entities + * Stop an entity from following the current entity. Passing no arguments will stop + * every entity attached. + */ + detach: function (obj) { + var i; + //if nothing passed, remove all attached objects + if (!obj) { + for (i = 0; i < this._children.length; i++) { + this._children[i]._parent = null; + } + this._children = []; + return this; + } + + //if obj passed, find the handler and unbind + for (i = 0; i < this._children.length; i++) { + if (this._children[i] == obj) { + this._children.splice(i, 1); + } + } + obj._parent = null; + + return this; + }, + + /**@ + * #.origin + * @comp 2D + * @sign public this .origin(Number x, Number y) + * @param x - Pixel value of origin offset on the X axis + * @param y - Pixel value of origin offset on the Y axis + * @sign public this .origin(String offset) + * @param offset - Combination of center, top, bottom, middle, left and right + * Set the origin point of an entity for it to rotate around. + * + * @example + * ~~~ + * this.origin("top left") + * this.origin("center") + * this.origin("bottom right") + * this.origin("middle right") + * ~~~ + * + * @see .rotation + */ + origin: function (x, y) { + //text based origin + if (typeof x === "string") { + if (x === "centre" || x === "center" || x.indexOf(' ') === -1) { + x = this._w / 2; + y = this._h / 2; + } else { + var cmd = x.split(' '); + if (cmd[0] === "top") y = 0; + else if (cmd[0] === "bottom") y = this._h; + else if (cmd[0] === "middle" || cmd[1] === "center" || cmd[1] === "centre") y = this._h / 2; + + if (cmd[1] === "center" || cmd[1] === "centre" || cmd[1] === "middle") x = this._w / 2; + else if (cmd[1] === "left") x = 0; + else if (cmd[1] === "right") x = this._w; + } + } + + this._origin.x = x; + this._origin.y = y; + + return this; + }, + + /**@ + * #.flip + * @comp 2D + * @trigger Change - when the entity has flipped + * @sign public this .flip(String dir) + * @param dir - Flip direction + * + * Flip entity on passed direction + * + * @example + * ~~~ + * this.flip("X") + * ~~~ + */ + flip: function (dir) { + dir = dir || "X"; + if (!this["_flip" + dir]) { + this["_flip" + dir] = true; + this.trigger("Change"); + } + return this; + }, + + /**@ + * #.unflip + * @comp 2D + * @trigger Change - when the entity has unflipped + * @sign public this .unflip(String dir) + * @param dir - Unflip direction + * + * Unflip entity on passed direction (if it's flipped) + * + * @example + * ~~~ + * this.unflip("X") + * ~~~ + */ + unflip: function (dir) { + dir = dir || "X"; + if (this["_flip" + dir]) { + this["_flip" + dir] = false; + this.trigger("Change"); + } + return this; + }, + + /** + * Method for rotation rather than through a setter + */ + rotate: function (e) { + var x2, y2; + x2 = (this._x + this._origin.x - e.o.x) * e.cos + (this._y + this._origin.y - e.o.y) * e.sin + (e.o.x - this._origin.x); + y2 = (this._y + this._origin.y - e.o.y) * e.cos - (this._x + this._origin.x - e.o.x) * e.sin + (e.o.y - this._origin.y); + this._attr('_rotation', this._rotation - e.deg); + this._attr('_x', x2 ); + this._attr('_y', y2 ); + }, + + /**@ + * #._attr + * @comp 2D + * Setter method for all 2D properties including + * x, y, w, h, alpha, rotation and visible. + */ + _attr: function (name, value) { + // Return if there is no change + if (this[name] === value) { + return; + } + //keep a reference of the old positions + var old = Crafty._rectPool.copy(this); + + var mbr; + //if rotation, use the rotate method + if (name === '_rotation') { + this._rotate(value); // _rotate triggers "Rotate" + //set the global Z and trigger reorder just in case + } else if (name === '_z') { + this._globalZ = parseInt(value + Crafty.zeroFill(this[0], 5), 10); //magic number 10^5 is the max num of entities + this.trigger("reorder"); + //if the rect bounds change, update the MBR and trigger move + } else if (name === '_x' || name === '_y') { + mbr = this._mbr; + + if (mbr) { + mbr[name] -= this[name] - value; + } + this[name] = value; + + this.trigger("Move", old); + + } else if (name === '_h' || name === '_w') { + mbr = this._mbr; + + var oldValue = this[name]; + this[name] = value; + if (mbr) { + this._calculateMBR(this._origin.x + this._x, this._origin.y + this._y, -this._rotation * DEG_TO_RAD); + } + if (name === '_w') { + this.trigger("Resize", { + axis: 'w', + amount: value - oldValue + }); + } else if (name === '_h') { + this.trigger("Resize", { + axis: 'h', + amount: value - oldValue + }); + } + this.trigger("Move", old); + + } + + //everything will assume the value + this[name] = value; + + //trigger a change + this.trigger("Change", old); + + Crafty._rectPool.recycle(old); + } +}); + +/**@ + * #Gravity + * @category 2D + * Adds gravitational pull to the entity. + */ +Crafty.c("Gravity", { + _gravityConst: 0.2, + _gy: 0, + _falling: true, + _anti: null, + + init: function () { + this.requires("2D"); + }, + + /**@ + * #.gravity + * @comp Gravity + * @sign public this .gravity([comp]) + * @param comp - The name of a component that will stop this entity from falling + * + * Enable gravity for this entity no matter whether comp parameter is not specified, + * If comp parameter is specified all entities with that component will stop this entity from falling. + * For a player entity in a platform game this would be a component that is added to all entities + * that the player should be able to walk on. + * + * @example + * ~~~ + * Crafty.e("2D, DOM, Color, Gravity") + * .color("red") + * .attr({ w: 100, h: 100 }) + * .gravity("platform"); + * ~~~ + */ + gravity: function (comp) { + if (comp) this._anti = comp; + if(isNaN(this._jumpSpeed)) this._jumpSpeed = 0; //set to 0 if Twoway component is not present + + this.bind("EnterFrame", this._enterFrame); + + return this; + }, + + /**@ + * #.gravityConst + * @comp Gravity + * @sign public this .gravityConst(g) + * @param g - gravitational constant + * + * Set the gravitational constant to g. The default is .2. The greater g, the faster the object falls. + * + * @example + * ~~~ + * Crafty.e("2D, DOM, Color, Gravity") + * .color("red") + * .attr({ w: 100, h: 100 }) + * .gravity("platform") + * .gravityConst(2) + * ~~~ + */ + gravityConst: function (g) { + this._gravityConst = g; + return this; + }, + + _enterFrame: function () { + if (this._falling) { + //if falling, move the players Y + this._gy += this._gravityConst; + this.y += this._gy; + } else { + this._gy = 0; //reset change in y + } + + var obj, hit = false, + pos = this.pos(), + q, i = 0, + l; + + //Increase by 1 to make sure map.search() finds the floor + pos._y++; + + //map.search wants _x and intersect wants x... + pos.x = pos._x; + pos.y = pos._y; + pos.w = pos._w; + pos.h = pos._h; + + q = Crafty.map.search(pos); + l = q.length; + + for (; i < l; ++i) { + obj = q[i]; + //check for an intersection directly below the player + if (obj !== this && obj.has(this._anti) && obj.intersect(pos)) { + hit = obj; + break; + } + } + + if (hit) { //stop falling if found and player is moving down + if (this._falling && ((this._gy > this._jumpSpeed) || !this._up)){ + this.stopFalling(hit); + } + } else { + this._falling = true; //keep falling otherwise + } + }, + + stopFalling: function (e) { + if (e) this.y = e._y - this._h; //move object + + //this._gy = -1 * this._bounce; + this._falling = false; + if (this._up) this._up = false; + this.trigger("hit"); + }, + + /**@ + * #.antigravity + * @comp Gravity + * @sign public this .antigravity() + * Disable gravity for this component. It can be reenabled by calling .gravity() + */ + antigravity: function () { + this.unbind("EnterFrame", this._enterFrame); + } +}); + +/**@ + * #Crafty.polygon + * @category 2D + * + * Polygon object used for hitboxes and click maps. Must pass an Array for each point as an + * argument where index 0 is the x position and index 1 is the y position. + * + * For example one point of a polygon will look like this: `[0,5]` where the `x` is `0` and the `y` is `5`. + * + * Can pass an array of the points or simply put each point as an argument. + * + * When creating a polygon for an entity, each point should be offset or relative from the entities `x` and `y` + * (don't include the absolute values as it will automatically calculate this). + * + * + * @example + * ~~~ + * new Crafty.polygon([50,0],[100,100],[0,100]); + * new Crafty.polygon([[50,0],[100,100],[0,100]]); + * ~~~ + */ +Crafty.polygon = function (poly) { + if (arguments.length > 1) { + poly = Array.prototype.slice.call(arguments, 0); + } + this.points = poly; +}; + +Crafty.polygon.prototype = { + /**@ + * #.containsPoint + * @comp Crafty.polygon + * @sign public Boolean .containsPoint(Number x, Number y) + * @param x - X position of the point + * @param y - Y position of the point + * + * Method is used to determine if a given point is contained by the polygon. + * + * @example + * ~~~ + * var poly = new Crafty.polygon([50,0],[100,100],[0,100]); + * poly.containsPoint(50, 50); //TRUE + * poly.containsPoint(0, 0); //FALSE + * ~~~ + */ + containsPoint: function (x, y) { + var p = this.points, + i, j, c = false; + + for (i = 0, j = p.length - 1; i < p.length; j = i++) { + if (((p[i][1] > y) != (p[j][1] > y)) && (x < (p[j][0] - p[i][0]) * (y - p[i][1]) / (p[j][1] - p[i][1]) + p[i][0])) { + c = !c; + } + } + + return c; + }, + + /**@ + * #.shift + * @comp Crafty.polygon + * @sign public void .shift(Number x, Number y) + * @param x - Amount to shift the `x` axis + * @param y - Amount to shift the `y` axis + * + * Shifts every single point in the polygon by the specified amount. + * + * @example + * ~~~ + * var poly = new Crafty.polygon([50,0],[100,100],[0,100]); + * poly.shift(5,5); + * //[[55,5], [105,5], [5,105]]; + * ~~~ + */ + shift: function (x, y) { + var i = 0, + l = this.points.length, + current; + for (; i < l; i++) { + current = this.points[i]; + current[0] += x; + current[1] += y; + } + }, + + rotate: function (e) { + var i = 0, + l = this.points.length, + current, x, y; + + for (; i < l; i++) { + current = this.points[i]; + + x = e.o.x + (current[0] - e.o.x) * e.cos + (current[1] - e.o.y) * e.sin; + y = e.o.y - (current[0] - e.o.x) * e.sin + (current[1] - e.o.y) * e.cos; + + current[0] = x; + current[1] = y; + } + } +}; + +/**@ + * #Crafty.circle + * @category 2D + * Circle object used for hitboxes and click maps. Must pass a `x`, a `y` and a `radius` value. + * + *@example + * ~~~ + * var centerX = 5, + * centerY = 10, + * radius = 25; + * + * new Crafty.circle(centerX, centerY, radius); + * ~~~ + * + * When creating a circle for an entity, each point should be offset or relative from the entities `x` and `y` + * (don't include the absolute values as it will automatically calculate this). + */ +Crafty.circle = function (x, y, radius) { + this.x = x; + this.y = y; + this.radius = radius; + + // Creates an octagon that approximate the circle for backward compatibility. + this.points = []; + var theta; + + for (var i = 0; i < 8; i++) { + theta = i * Math.PI / 4; + this.points[i] = [this.x + (Math.sin(theta) * radius), this.y + (Math.cos(theta) * radius)]; + } +}; + +Crafty.circle.prototype = { + /**@ + * #.containsPoint + * @comp Crafty.circle + * @sign public Boolean .containsPoint(Number x, Number y) + * @param x - X position of the point + * @param y - Y position of the point + * + * Method is used to determine if a given point is contained by the circle. + * + * @example + * ~~~ + * var circle = new Crafty.circle(0, 0, 10); + * circle.containsPoint(0, 0); //TRUE + * circle.containsPoint(50, 50); //FALSE + * ~~~ + */ + containsPoint: function (x, y) { + var radius = this.radius, + sqrt = Math.sqrt, + deltaX = this.x - x, + deltaY = this.y - y; + + return (deltaX * deltaX + deltaY * deltaY) < (radius * radius); + }, + + /**@ + * #.shift + * @comp Crafty.circle + * @sign public void .shift(Number x, Number y) + * @param x - Amount to shift the `x` axis + * @param y - Amount to shift the `y` axis + * + * Shifts the circle by the specified amount. + * + * @example + * ~~~ + * var circle = new Crafty.circle(0, 0, 10); + * circle.shift(5,5); + * //{x: 5, y: 5, radius: 10}; + * ~~~ + */ + shift: function (x, y) { + this.x += x; + this.y += y; + + var i = 0, + l = this.points.length, + current; + for (; i < l; i++) { + current = this.points[i]; + current[0] += x; + current[1] += y; + } + }, + + rotate: function () { + // We are a circle, we don't have to rotate :) + } +}; + + +Crafty.matrix = function (m) { + this.mtx = m; + this.width = m[0].length; + this.height = m.length; +}; + +Crafty.matrix.prototype = { + x: function (other) { + if (this.width != other.height) { + return; + } + + var result = []; + for (var i = 0; i < this.height; i++) { + result[i] = []; + for (var j = 0; j < other.width; j++) { + var sum = 0; + for (var k = 0; k < this.width; k++) { + sum += this.mtx[i][k] * other.mtx[k][j]; + } + result[i][j] = sum; + } + } + return new Crafty.matrix(result); + }, + + + e: function (row, col) { + //test if out of bounds + if (row < 1 || row > this.mtx.length || col < 1 || col > this.mtx[0].length) return null; + return this.mtx[row - 1][col - 1]; + } +}; + +},{"./HashMap.js":4,"./core.js":9}],2:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +/**@ + * #DOM + * @category Graphics + * Draws entities as DOM nodes, specifically `
`s. + */ +Crafty.c("DOM", { + /**@ + * #._element + * @comp DOM + * The DOM element used to represent the entity. + */ + _element: null, + //holds current styles, so we can check if there are changes to be written to the DOM + _cssStyles: null, + + /**@ + * #.avoidCss3dTransforms + * @comp DOM + * Avoids using of CSS 3D Transform for positioning when true. Default value is false. + */ + avoidCss3dTransforms: false, + + init: function () { + this._cssStyles = { + visibility: '', + left: '', + top: '', + width: '', + height: '', + zIndex: '', + opacity: '', + transformOrigin: '', + transform: '' + }; + this._element = document.createElement("div"); + Crafty.stage.inner.appendChild(this._element); + this._element.style.position = "absolute"; + this._element.id = "ent" + this[0]; + + this.bind("Change", function () { + if (!this._changed) { + this._changed = true; + Crafty.DrawManager.addDom(this); + } + }); + + function updateClass() { + var i = 0, + c = this.__c, + str = ""; + for (i in c) { + str += ' ' + i; + } + str = str.substr(1); + this._element.className = str; + } + + this.bind("NewComponent", updateClass).bind("RemoveComponent", updateClass); + + if (Crafty.support.prefix === "ms" && Crafty.support.version < 9) { + this._filters = {}; + + this.bind("Rotate", function (e) { + var m = e.matrix, + elem = this._element.style, + M11 = m.M11.toFixed(8), + M12 = m.M12.toFixed(8), + M21 = m.M21.toFixed(8), + M22 = m.M22.toFixed(8); + + this._filters.rotation = "progid:DXImageTransform.Microsoft.Matrix(M11=" + M11 + ", M12=" + M12 + ", M21=" + M21 + ", M22=" + M22 + ",sizingMethod='auto expand')"; + }); + } + + this.bind("Remove", this.undraw); + this.bind("RemoveComponent", function (compName) { + if (compName === "DOM") + this.undraw(); + }); + }, + + /**@ + * #.getDomId + * @comp DOM + * @sign public this .getId() + * + * Get the Id of the DOM element used to represent the entity. + */ + getDomId: function () { + return this._element.id; + }, + + /**@ + * #.DOM + * @comp DOM + * @trigger Draw - when the entity is ready to be drawn to the stage - { style:String, type:"DOM", co} + * @sign public this .DOM(HTMLElement elem) + * @param elem - HTML element that will replace the dynamically created one + * + * Pass a DOM element to use rather than one created. Will set `._element` to this value. Removes the old element. + */ + DOM: function (elem) { + if (elem && elem.nodeType) { + this.undraw(); + this._element = elem; + this._element.style.position = 'absolute'; + } + return this; + }, + + /**@ + * #.draw + * @comp DOM + * @sign public this .draw(void) + * + * Updates the CSS properties of the node to draw on the stage. + */ + draw: function () { + var style = this._element.style, + coord = this.__coord || [0, 0, 0, 0], + co = { + x: coord[0], + y: coord[1], + w: coord[2], + h: coord[3] + }, + prefix = Crafty.support.prefix, + trans = []; + + if (this._cssStyles.visibility !== this._visible) { + this._cssStyles.visibility = this._visible; + if (!this._visible) { + style.visibility = "hidden"; + } else { + style.visibility = "visible"; + } + } + + //utilize CSS3 if supported + if (Crafty.support.css3dtransform && !this.avoidCss3dTransforms) { + trans.push("translate3d(" + (~~this._x) + "px," + (~~this._y) + "px,0)"); + } else { + if (this._cssStyles.left !== this._x) { + this._cssStyles.left = this._x; + style.left = ~~ (this._x) + "px"; + } + if (this._cssStyles.top !== this._y) { + this._cssStyles.top = this._y; + style.top = ~~ (this._y) + "px"; + } + } + + if (this._cssStyles.width !== this._w) { + this._cssStyles.width = this._w; + style.width = ~~ (this._w) + "px"; + } + if (this._cssStyles.height !== this._h) { + this._cssStyles.height = this._h; + style.height = ~~ (this._h) + "px"; + } + if (this._cssStyles.zIndex !== this._z) { + this._cssStyles.zIndex = this._z; + style.zIndex = this._z; + } + + if (this._cssStyles.opacity !== this._alpha) { + this._cssStyles.opacity = this._alpha; + style.opacity = this._alpha; + style[prefix + "Opacity"] = this._alpha; + } + + //if not version 9 of IE + if (prefix === "ms" && Crafty.support.version < 9) { + //for IE version 8, use ImageTransform filter + if (Crafty.support.version === 8) { + this._filters.alpha = "progid:DXImageTransform.Microsoft.Alpha(Opacity=" + (this._alpha * 100) + ")"; // first! + //all other versions use filter + } else { + this._filters.alpha = "alpha(opacity=" + (this._alpha * 100) + ")"; + } + } + + if (this._mbr) { + var origin = this._origin.x + "px " + this._origin.y + "px"; + style.transformOrigin = origin; + style[prefix + "TransformOrigin"] = origin; + if (Crafty.support.css3dtransform) trans.push("rotateZ(" + this._rotation + "deg)"); + else trans.push("rotate(" + this._rotation + "deg)"); + } + + if (this._flipX) { + trans.push("scaleX(-1)"); + if (prefix === "ms" && Crafty.support.version < 9) { + this._filters.flipX = "fliph"; + } + } + + if (this._flipY) { + trans.push("scaleY(-1)"); + if (prefix === "ms" && Crafty.support.version < 9) { + this._filters.flipY = "flipv"; + } + } + + //apply the filters if IE + if (prefix === "ms" && Crafty.support.version < 9) { + this.applyFilters(); + } + + if (this._cssStyles.transform != trans.join(" ")) { + this._cssStyles.transform = trans.join(" "); + style.transform = this._cssStyles.transform; + style[prefix + "Transform"] = this._cssStyles.transform; + } + + this.trigger("Draw", { + style: style, + type: "DOM", + co: co + }); + + return this; + }, + + applyFilters: function () { + this._element.style.filter = ""; + var str = ""; + + for (var filter in this._filters) { + if (!this._filters.hasOwnProperty(filter)) continue; + str += this._filters[filter] + " "; + } + + this._element.style.filter = str; + }, + + /**@ + * #.undraw + * @comp DOM + * @sign public this .undraw(void) + * + * Removes the element from the stage. + */ + undraw: function () { + if (this._element) { + Crafty.stage.inner.removeChild(this._element); + } + return this; + }, + + /**@ + * #.css + * @comp DOM + * @sign public * css(String property, String value) + * @param property - CSS property to modify + * @param value - Value to give the CSS property + * @sign public * css(Object map) + * @param map - Object where the key is the CSS property and the value is CSS value + * + * Apply CSS styles to the element. + * + * Can pass an object where the key is the style property and the value is style value. + * + * For setting one style, simply pass the style as the first argument and the value as the second. + * + * The notation can be CSS or JS (e.g. `text-align` or `textAlign`). + * + * To return a value, pass the property. + * + * Note: For entities with "Text" component, some css properties are controlled by separate functions + * `.textFont()` and `.textColor()`, and ignore `.css()` settings. See Text component for details. + * + * @example + * ~~~ + * this.css({'text-align', 'center', 'text-decoration': 'line-through'}); + * this.css("textAlign", "center"); + * this.css("text-align"); //returns center + * ~~~ + */ + css: function (obj, value) { + var key, + elem = this._element, + val, + style = elem.style; + + //if an object passed + if (typeof obj === "object") { + for (key in obj) { + if (!obj.hasOwnProperty(key)) continue; + val = obj[key]; + if (typeof val === "number") val += 'px'; + + style[Crafty.DOM.camelize(key)] = val; + } + } else { + //if a value is passed, set the property + if (value) { + if (typeof value === "number") value += 'px'; + style[Crafty.DOM.camelize(obj)] = value; + } else { //otherwise return the computed property + return Crafty.DOM.getStyle(elem, obj); + } + } + + this.trigger("Change"); + + return this; + } +}); + +/** + * Fix IE6 background flickering + */ +try { + document.execCommand("BackgroundImageCache", false, true); +} catch (e) {} + +Crafty.extend({ + /**@ + * #Crafty.DOM + * @category Graphics + * + * Collection of utilities for using the DOM. + */ + DOM: { + /**@ + * #Crafty.DOM.window + * @comp Crafty.DOM + * + * Object with `width` and `height` values representing the width + * and height of the `window`. + */ + window: { + init: function () { + this.width = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth); + this.height = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight); + + // Bind scene rendering (see drawing.js) + Crafty.unbind("RenderScene", Crafty.DrawManager.renderDOM); + Crafty.bind("RenderScene", Crafty.DrawManager.renderDOM); + }, + + width: 0, + height: 0 + }, + + /**@ + * #Crafty.DOM.inner + * @comp Crafty.DOM + * @sign public Object Crafty.DOM.inner(HTMLElement obj) + * @param obj - HTML element to calculate the position + * @returns Object with `x` key being the `x` position, `y` being the `y` position + * + * Find a DOM elements position including + * padding and border. + */ + inner: function (obj) { + var rect = obj.getBoundingClientRect(), + x = rect.left + (window.pageXOffset ? window.pageXOffset : document.body.scrollLeft), + y = rect.top + (window.pageYOffset ? window.pageYOffset : document.body.scrollTop), + + //border left + borderX = parseInt(this.getStyle(obj, 'border-left-width') || 0, 10) || parseInt(this.getStyle(obj, 'borderLeftWidth') || 0, 10) || 0, + borderY = parseInt(this.getStyle(obj, 'border-top-width') || 0, 10) || parseInt(this.getStyle(obj, 'borderTopWidth') || 0, 10) || 0; + + x += borderX; + y += borderY; + + return { + x: x, + y: y + }; + }, + + /**@ + * #Crafty.DOM.getStyle + * @comp Crafty.DOM + * @sign public Object Crafty.DOM.getStyle(HTMLElement obj, String property) + * @param obj - HTML element to find the style + * @param property - Style to return + * + * Determine the value of a style on an HTML element. Notation can be + * in either CSS or JS. + */ + getStyle: function (obj, prop) { + var result; + if (obj.currentStyle) + result = obj.currentStyle[this.camelize(prop)]; + else if (window.getComputedStyle) + result = document.defaultView.getComputedStyle(obj, null).getPropertyValue(this.csselize(prop)); + return result; + }, + + /** + * Used in the Zepto framework + * + * Converts CSS notation to JS notation + */ + camelize: function (str) { + return str.replace(/-+(.)?/g, function (match, chr) { + return chr ? chr.toUpperCase() : ''; + }); + }, + + /** + * Converts JS notation to CSS notation + */ + csselize: function (str) { + return str.replace(/[A-Z]/g, function (chr) { + return chr ? '-' + chr.toLowerCase() : ''; + }); + }, + + /**@ + * #Crafty.DOM.translate + * @comp Crafty.DOM + * @sign public Object Crafty.DOM.translate(Number x, Number y) + * @param x - x position to translate + * @param y - y position to translate + * @return Object with x and y as keys and translated values + * + * Method will translate x and y positions to positions on the + * stage. Useful for mouse events with `e.clientX` and `e.clientY`. + */ + translate: function (x, y) { + return { + x: (x - Crafty.stage.x + document.body.scrollLeft + document.documentElement.scrollLeft - Crafty.viewport._x) / Crafty.viewport._scale, + y: (y - Crafty.stage.y + document.body.scrollTop + document.documentElement.scrollTop - Crafty.viewport._y) / Crafty.viewport._scale + }; + } + } +}); + +},{"./core.js":9}],3:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +/**@ + * #DebugCanvas + * @category Debug + * @trigger Draw - when the entity is ready to be drawn to the stage + * @trigger NoCanvas - if the browser does not support canvas + * + * When this component is added to an entity it will be drawn by the DebugCanvas layer. + * + * Crafty.debugCanvas.init() will be automatically called if it is not called already to initialize the canvas element. + * + * To visualise an object's MBR, use "VisibleMBR". To visualise a "Collision" object's hitbox, use "WiredHitBox" or "SolidHitBox". + * @see DebugPolygon, DebugRectangle + */ +Crafty.c("DebugCanvas", { + init: function () { + this.requires("2D"); + if (!Crafty.DebugCanvas.context) + Crafty.DebugCanvas.init(); + Crafty.DebugCanvas.add(this); + this._debug = { + alpha: 1.0, + lineWidth: 1 + }; + this.bind("RemoveComponent", this.onDebugRemove); + this.bind("Remove", this.onDebugDestroy); + }, + + // When component is removed + onDebugRemove: function (id) { + if (id === "DebugCanvas") { + Crafty.DebugCanvas.remove(this); + } + }, + + //When entity is destroyed + onDebugDestroy: function (id) { + Crafty.DebugCanvas.remove(this); + }, + + /**@ + * #.debugAlpha + * @comp DebugCanvas + * @sign public .debugAlpha(Number alpha) + * @param alpha - The alpha level the component will be drawn with + */ + debugAlpha: function (alpha) { + this._debug.alpha = alpha; + return this; + }, + + /**@ + * #.debugFill + * @comp DebugCanvas + * @sign public .debugFill([String fillStyle]) + * @param fillStyle - The color the component will be filled with. Defaults to "red". Pass the boolean false to turn off filling. + * ~~~ + * var myEntity = Crafty.e("2D, Collision, SolidHitBox ").debugFill("purple") + *~~~ + */ + debugFill: function (fillStyle) { + if (typeof fillStyle === 'undefined') + fillStyle = "red"; + this._debug.fillStyle = fillStyle; + return this; + }, + + /**@ + * #.debugStroke + * @comp DebugCanvas + * @sign public .debugStroke([String strokeStyle]) + * @param strokeStyle - The color the component will be outlined with. Defaults to "red". Pass the boolean false to turn this off. + * ~~~ + * var myEntity = Crafty.e("2D, Collision, WiredHitBox ").debugStroke("white") + *~~~ + */ + debugStroke: function (strokeStyle) { + if (typeof strokeStyle === 'undefined') + strokeStyle = "red"; + this._debug.strokeStyle = strokeStyle; + return this; + }, + + debugDraw: function (ctx) { + var ga = ctx.globalAlpha; + var props = this._debug; + + if (props.alpha) + ctx.globalAlpha = this._debug.alpha; + + if (props.strokeStyle) + ctx.strokeStyle = props.strokeStyle; + + if (props.lineWidth) + ctx.lineWidth = props.lineWidth; + + if (props.fillStyle) + ctx.fillStyle = props.fillStyle; + + this.trigger("DebugDraw"); + + ctx.globalAlpha = ga; + + } + + +}); + + + +/**@ + * #DebugRectangle + * @category Debug + * + * A component for rendering an object with a position and dimensions to the debug canvas. + * + * + * ~~~ + * var myEntity = Crafty.e("2D, DebugRectangle") + * .attr({x: 13, y: 37, w: 42, h: 42}) + * .debugStroke("green"); + * myEntity.debugRectangle(myEntity) + *~~~ + * @see DebugCanvas + */ +Crafty.c("DebugRectangle", { + init: function () { + this.requires("2D, DebugCanvas"); + }, + + /**@ + * #.debugRectangle + * @comp DebugRectangle + * @sign public .debugRectangle(Object rect) + * @param rect - an object with _x, _y, _w, and _h to draw + * Sets the rectangle that this component draws to the debug canvas. + * + */ + debugRectangle: function (rect) { + this.debugRect = rect; + this.unbind("DebugDraw", this.drawDebugRect); + this.bind("DebugDraw", this.drawDebugRect); + return this; + + }, + + drawDebugRect: function () { + + ctx = Crafty.DebugCanvas.context; + var rect = this.debugRect; + if (rect === null || rect === undefined) + return; + if (rect._h && rect._w) { + if (this._debug.fillStyle) + ctx.fillRect(rect._x, rect._y, rect._w, rect._h); + if (this._debug.strokeStyle) + ctx.strokeRect(rect._x, rect._y, rect._w, rect._h); + } + + } + + + +}); + + + +/**@ + * #VisibleMBR + * @category Debug + * + * Adding this component to an entity will cause it's MBR to be drawn to the debug canvas. + * + * The methods of DebugCanvas can be used to control this component's appearance. + * @see 2D, DebugRectangle, DebugCanvas + */ +Crafty.c("VisibleMBR", { + init: function () { + this.requires("DebugRectangle") + .debugFill("purple") + .bind("EnterFrame", this._assignRect); + }, + + // Internal method for updating the MBR drawn. + _assignRect: function () { + if (this._mbr) + this.debugRectangle(this._mbr); + else + this.debugRectangle(this); + + } + + +}); + + +/**@ + * #DebugPolygon + * @category Debug + * + * For drawing a polygon to the debug canvas + * + * The methods of DebugCanvas can be used to control this component's appearance -- by default it is neither filled nor outlined + * + * For debugging hitboxes, use WiredHitBox or SolidHitBox. For debugging MBR, use VisibleMBR + * + * @see DebugCanvas + */ +Crafty.c("DebugPolygon", { + init: function () { + this.requires("2D, DebugCanvas"); + }, + + + /**@ + * #.debugPolygon + * @comp DebugPolygon + * @sign public .debugPolygon(Polygon poly) + * @param poly - a polygon to render + * Sets the polygon that this component renders to the debug canvas. + * + */ + debugPolygon: function (poly) { + this.polygon = poly; + this.unbind("DebugDraw", this.drawDebugPolygon); + this.bind("DebugDraw", this.drawDebugPolygon); + return this; + }, + + drawDebugPolygon: function () { + if (typeof this.polygon === "undefined") + return; + + ctx = Crafty.DebugCanvas.context; + ctx.beginPath(); + for (var p in this.polygon.points) { + ctx.lineTo(this.polygon.points[p][0], this.polygon.points[p][1]); + } + ctx.closePath(); + + if (this._debug.fillStyle) + ctx.fill(); + if (this._debug.strokeStyle) + ctx.stroke(); + } +}); + + +/**@ + * #WiredHitBox + * @category Debug + * + * Adding this component to an entity with a Collision component will cause its collision polygon to be drawn to the debug canvas as an outline + * + * The methods of DebugCanvas can be used to control this component's appearance. + * @see DebugPolygon, DebugCanvas + */ +Crafty.c("WiredHitBox", { + init: function () { + this.requires("DebugPolygon") + .debugStroke("red") + .matchHitBox(); + this.bind("NewHitbox", this.matchHitBox); + }, + matchHitBox: function () { + this.debugPolygon(this.map); + } +}); + +/**@ + * #SolidHitBox + * @category Debug + * + * Adding this component to an entity with a Collision component will cause its collision polygon to be drawn to the debug canvas, with a default alpha level of 0.7. + * + * The methods of DebugCanvas can be used to control this component's appearance. + * @see DebugPolygon, DebugCanvas + */ +Crafty.c("SolidHitBox", { + init: function () { + this.requires("Collision, DebugPolygon") + .debugFill("orange").debugAlpha(0.7) + .matchHitBox(); + this.bind("NewHitbox", this.matchHitBox); + }, + matchHitBox: function () { + this.debugPolygon(this.map); + } +}); + +Crafty.DebugCanvas = { + context: null, + entities: [], + onetimeEntities: [], + add: function (ent) { + this.entities.push(ent); + }, + + remove: function (ent) { + var list = this.entities; + for (var i = list.length - 1; i >= 0; i--) + if (list[i] == ent) + list.splice(i, 1); + + }, + + // Mostly copied from canvas.init() + // Called the first time a "DebugCanvas" component is added to an entity + // We should consider how to abstract the idea of multiple canvases + init: function () { + if (!Crafty.DebugCanvas.context) { + //check if canvas is supported + if (!Crafty.support.canvas) { + Crafty.trigger("NoCanvas"); + Crafty.stop(); + return; + } + + //create an empty canvas element + var c; + c = document.createElement("canvas"); + c.width = Crafty.viewport.width; + c.height = Crafty.viewport.height; + c.style.position = 'absolute'; + c.style.left = "0px"; + c.style.top = "0px"; + c.id = "debug-canvas"; + // The debug canvas should be on the very top; the highest a regular zindex can get is ~10000 + c.style.zIndex = 100000; + + Crafty.stage.elem.appendChild(c); + Crafty.DebugCanvas.context = c.getContext('2d'); + Crafty.DebugCanvas._canvas = c; + + + + } + //Bind rendering of canvas context (see drawing.js) + Crafty.unbind("RenderScene", Crafty.DebugCanvas.renderScene); + Crafty.bind("RenderScene", Crafty.DebugCanvas.renderScene); + + }, + + + // copied from drawAll() + renderScene: function (rect) { + rect = rect || Crafty.viewport.rect(); + var q = Crafty.DebugCanvas.entities, + i = 0, + l = q.length, + ctx = Crafty.DebugCanvas.context, + current; + + var view = Crafty.viewport; + ctx.setTransform(view._scale, 0, 0, view._scale, view._x, view._y); + + ctx.clearRect(rect._x, rect._y, rect._w, rect._h); + + + //sort the objects by the global Z + //q.sort(zsort); + for (; i < l; i++) { + current = q[i]; + current.debugDraw(ctx); + } + + } + +}; + +},{"./core.js":9}],4:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +/** + * Spatial HashMap for broad phase collision + * + * @author Louis Stowasser + */ + + /**@ + * #Crafty.HashMap.constructor + * @comp Crafty.HashMap + * @sign public void Crafty.HashMap([cellsize]) + * @param cellsize - the cell size. If omitted, `cellsize` is 64. + * + * Set `cellsize`. + * And create `this.map`. + */ + var cellsize, + + HashMap = function (cell) { + cellsize = cell || 64; + this.map = {}; + }, + + SPACE = " ", + keyHolder = {}; + + HashMap.prototype = { + /**@ + * #Crafty.map.insert + * @comp Crafty.map + * @sign public Object Crafty.map.insert(Object obj) + * @param obj - An entity to be inserted. + * + * `obj` is inserted in '.map' of the corresponding broad phase cells. An object of the following fields is returned. + * ~~~ + * - the object that keep track of cells (keys) + * - `obj` + * - the HashMap object + * ~~~ + */ + insert: function (obj) { + var keys = HashMap.key(obj), + entry = new Entry(keys, obj, this), + i = 0, + j, + hash; + + //insert into all x buckets + for (i = keys.x1; i <= keys.x2; i++) { + //insert into all y buckets + for (j = keys.y1; j <= keys.y2; j++) { + hash = (i << 16) ^ j; + if (!this.map[hash]) this.map[hash] = []; + this.map[hash].push(obj); + } + } + + return entry; + }, + + /**@ + * #Crafty.map.search + * @comp Crafty.map + * @sign public Object Crafty.map.search(Object rect[, Boolean filter]) + * @param rect - the rectangular region to search for entities. + * @param filter - Default value is true. Otherwise, must be false. + * + * - If `filter` is `false`, just search for all the entries in the give `rect` region by broad phase collision. Entity may be returned duplicated. + * - If `filter` is `true`, filter the above results by checking that they actually overlap `rect`. + * The easier usage is with `filter`=`true`. For performance reason, you may use `filter`=`false`, and filter the result yourself. See examples in drawing.js and collision.js + */ + + search: function (rect, filter) { + var keys = HashMap.key(rect, keyHolder), + i, j, k, + results = []; + + if (filter === undefined) filter = true; //default filter to true + + //search in all x buckets + for (i = keys.x1; i <= keys.x2; i++) { + //insert into all y buckets + for (j = keys.y1; j <= keys.y2; j++) { + cell = this.map[(i << 16) ^ j]; + if (cell) { + for (k = 0; k < cell.length; k++) + results.push(cell[k]); + } + } + } + + if (filter) { + var obj, id, finalresult = [], + found = {}; + //add unique elements to lookup table with the entity ID as unique key + for (i = 0, l = results.length; i < l; i++) { + obj = results[i]; + if (!obj) continue; //skip if deleted + id = obj[0]; //unique ID + obj = obj._mbr || obj; + //check if not added to hash and that actually intersects + if (!found[id] && obj._x < rect._x + rect._w && obj._x + obj._w > rect._x && + obj._y < rect._y + rect._h && obj._h + obj._y > rect._y) + found[id] = results[i]; + } + + //loop over lookup table and copy to final array + for (obj in found) finalresult.push(found[obj]); + + return finalresult; + } else { + return results; + } + }, + + /**@ + * #Crafty.map.remove + * @comp Crafty.map + * @sign public void Crafty.map.remove([Object keys, ]Object obj) + * @param keys - key region. If omitted, it will be derived from obj by `Crafty.HashMap.key`. + * @param obj - need more document. + * + * Remove an entity in a broad phase map. + * - The second form is only used in Crafty.HashMap to save time for computing keys again, where keys were computed previously from obj. End users should not call this form directly. + * + * @example + * ~~~ + * Crafty.map.remove(e); + * ~~~ + */ + remove: function (keys, obj) { + var i = 0, + j, hash; + + if (arguments.length == 1) { + obj = keys; + keys = HashMap.key(obj, keyHolder); + } + + //search in all x buckets + for (i = keys.x1; i <= keys.x2; i++) { + //insert into all y buckets + for (j = keys.y1; j <= keys.y2; j++) { + hash = (i << 16) ^ j; + + if (this.map[hash]) { + var cell = this.map[hash], + m, n = cell.length; + //loop over objs in cell and delete + for (m = 0; m < n; m++) + if (cell[m] && cell[m][0] === obj[0]) + cell.splice(m, 1); + } + } + } + }, + + /**@ + * #Crafty.map.refresh + * @comp Crafty.map + * @sign public void Crafty.map.remove(Entry entry) + * @param entry - An entry to update + * + * Refresh an entry's keys, and its position in the broad phrase map. + * + * @example + * ~~~ + * Crafty.map.refresh(e); + * ~~~ + */ + refresh: function (entry) { + var keys = entry.keys; + var obj = entry.obj; + var cell, i, j, m, n; + + //First delete current object from appropriate cells + for (i = keys.x1; i <= keys.x2; i++) { + for (j = keys.y1; j <= keys.y2; j++) { + cell = this.map[(i << 16) ^ j]; + if (cell) { + n = cell.length; + //loop over objs in cell and delete + for (m = 0; m < n; m++) + if (cell[m] && cell[m][0] === obj[0]) + cell.splice(m, 1); + } + } + } + + //update keys + HashMap.key(obj, keys); + + //insert into all rows and columns + for (i = keys.x1; i <= keys.x2; i++) { + for (j = keys.y1; j <= keys.y2; j++) { + cell = this.map[(i << 16) ^ j]; + if (!cell) cell = this.map[(i << 16) ^ j] = []; + cell.push(obj); + } + } + + return entry; + }, + + + + + /**@ + * #Crafty.map.boundaries + * @comp Crafty.map + * @sign public Object Crafty.map.boundaries() + * + * The return `Object` is of the following format. + * ~~~ + * { + * min: { + * x: val_x, + * y: val_y + * }, + * max: { + * x: val_x, + * y: val_y + * } + * } + * ~~~ + */ + boundaries: function () { + var k, ent, + hash = { + max: { + x: -Infinity, + y: -Infinity + }, + min: { + x: Infinity, + y: Infinity + } + }, + coords = { + max: { + x: -Infinity, + y: -Infinity + }, + min: { + x: Infinity, + y: Infinity + } + }; + + //Using broad phase hash to speed up the computation of boundaries. + for (var h in this.map) { + if (!this.map[h].length) continue; + + //broad phase coordinate + var i = h >> 16, + j = (h << 16) >> 16; + if (j < 0) { + i = i ^ -1; + } + if (i >= hash.max.x) { + hash.max.x = i; + for (k in this.map[h]) { + ent = this.map[h][k]; + //make sure that this is a Crafty entity + if (typeof ent == 'object' && 'requires' in ent) { + coords.max.x = Math.max(coords.max.x, ent.x + ent.w); + } + } + } + if (i <= hash.min.x) { + hash.min.x = i; + for (k in this.map[h]) { + ent = this.map[h][k]; + if (typeof ent == 'object' && 'requires' in ent) { + coords.min.x = Math.min(coords.min.x, ent.x); + } + } + } + if (j >= hash.max.y) { + hash.max.y = j; + for (k in this.map[h]) { + ent = this.map[h][k]; + if (typeof ent == 'object' && 'requires' in ent) { + coords.max.y = Math.max(coords.max.y, ent.y + ent.h); + } + } + } + if (j <= hash.min.y) { + hash.min.y = j; + for (k in this.map[h]) { + ent = this.map[h][k]; + if (typeof ent == 'object' && 'requires' in ent) { + coords.min.y = Math.min(coords.min.y, ent.y); + } + } + } + } + + return coords; + } + }; + + /**@ + * #Crafty.HashMap + * @category 2D + * Broad-phase collision detection engine. See background information at + * + * ~~~ + * - [N Tutorial B - Broad-Phase Collision](http://www.metanetsoftware.com/technique/tutorialB.html) + * - [Broad-Phase Collision Detection with CUDA](http.developer.nvidia.com/GPUGems3/gpugems3_ch32.html) + * ~~~ + * @see Crafty.map + */ + + /**@ + * #Crafty.HashMap.key + * @comp Crafty.HashMap + * @sign public Object Crafty.HashMap.key(Object obj) + * @param obj - an Object that has .mbr() or _x, _y, _w and _h. + * Get the rectangular region (in terms of the grid, with grid size `cellsize`), where the object may fall in. This region is determined by the object's bounding box. + * The `cellsize` is 64 by default. + * + * @see Crafty.HashMap.constructor + */ + HashMap.key = function (obj, keys) { + if (obj._mbr) { + obj = obj._mbr; + } + if (!keys) { + keys = {}; + } + + keys.x1 = Math.floor(obj._x / cellsize); + keys.y1 = Math.floor(obj._y / cellsize); + keys.x2 = Math.floor((obj._w + obj._x) / cellsize); + keys.y2 = Math.floor((obj._h + obj._y) / cellsize); + return keys; + }; + + HashMap.hash = function (keys) { + return keys.x1 + SPACE + keys.y1 + SPACE + keys.x2 + SPACE + keys.y2; + }; + + function Entry(keys, obj, map) { + this.keys = keys; + this.map = map; + this.obj = obj; + } + + Entry.prototype = { + update: function (rect) { + //check if buckets change + if (HashMap.hash(HashMap.key(rect, keyHolder)) != HashMap.hash(this.keys)) { + this.map.refresh(this); + } + } + }; + + module.exports = HashMap; + +},{"./core.js":9}],5:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +Crafty.easing = function(duration) { + this.timePerFrame = 1000 / Crafty.timer.FPS(); + this.duration = duration; //default duration given in ms + this.reset(); +}; + + +Crafty.easing.prototype = { + duration: 0, + clock:0, + steps: null, + complete: false, + paused: false, + + // init values + reset: function(){ + this.loops = 1; + this.clock = 0; + this.complete = false; + this.paused = false; + }, + + repeat: function(loopCount){ + this.loops = loopCount; + }, + + setProgress: function(progress, loopCount){ + this.clock = this.duration * progress; + if (typeof loopCount !== "undefined") + this.loops = loopCount; + + }, + + pause: function(){ + this.paused = true; + }, + + resume: function(){ + this.paused = false; + this.complete = false; + }, + + // Increment the clock by some amount dt + // Handles looping and sets a flag on completion + tick: function(dt){ + if (this.paused || this.complete) return; + this.clock += dt; + this.frames = Math.floor(this.clock/this.timePerFrame); + while (this.clock >= this.duration && this.complete === false){ + this.loops--; + if (this.loops > 0) + this.clock -= this.duration; + else + this.complete = true; + } + }, + + // same as value for now; with other time value functions would be more useful + time: function(){ + return ( Math.min(this.clock/this.duration, 1) ); + + }, + + // Value is where along the tweening curve we are + // For now it's simply linear; but we can easily add new types + value: function(){ + return this.time(); + } + +}; + + + + + + +/**@ +* #SpriteAnimation +* @category Animation +* @trigger StartAnimation - When an animation starts playing, or is resumed from the paused state - {Reel} +* @trigger AnimationEnd - When the animation finishes - { Reel } +* @trigger FrameChange - Each time the frame of the current reel changes - { Reel } +* @trigger ReelChange - When the reel changes - { Reel } +* +* Used to animate sprites by treating a sprite map as a set of animation frames. +* Must be applied to an entity that has a sprite-map component. +* +* To define an animation, see the `reel` method. To play an animation, see the `animate` method. +* +* A reel is an object that contains the animation frames and current state for an animation. The reel object has the following properties: +* @param id: (String) - the name of the reel +* @param frames: (Array) - A list of frames in the format [xpos, ypos] +* @param currentFrame: (Number) - The index of the current frame +* @param easing: (Crafty.easing object) - The object that handles the internal progress of the animation. +* @param duration: (Number) - The duration in milliseconds. +* +* Many animation related events pass a reel object as data. As typical with events, this should be treated as read only data that might be later altered by the entity. If you wish to preserve the data, make a copy of it. +* +* @see crafty.sprite +*/ +Crafty.c("SpriteAnimation", { + /* + * + * A map in which the keys are the names assigned to animations defined using + * the component (also known as reelIDs), and the values are objects describing + * the animation and its state. + */ + _reels: null, + + /* + * The reelID of the currently active reel (which is one of the elements in `this._reels`). + * This value is `null` if no reel is active. Some of the component's actions can be invoked + * without specifying a reel, in which case they will work on the active reel. + */ + _currentReelId: null, + + /* + * The currently active reel. + * This value is `null` if no reel is active. + */ + _currentReel: null, + + /* + * Whether or not an animation is currently playing. + */ + _isPlaying: false, + + + init: function () { + this._reels = {}; + }, + + /**@ + * #.reel + * @comp SpriteAnimation + * Used to define reels, to change the active reel, and to fetch the id of the active reel. + * + * @sign public this .reel(String reelId, Duration duration, Number fromX, Number fromY, Number frameCount) + * Defines a reel by starting and ending position on the sprite sheet. + * @param reelId - ID of the animation reel being created + * @param duration - The length of the animation in milliseconds. + * @param fromX - Starting `x` position on the sprite map (x's unit is the horizontal size of the sprite in the sprite map). + * @param fromY - `y` position on the sprite map (y's unit is the horizontal size of the sprite in the sprite map). Remains constant through the animation. + * @param frameCount - The number of sequential frames in the animation. If negative, the animation will play backwards. + * + * @sign public this .reel(String reelId, Duration duration, Array frames) + * Defines a reel by an explicit list of frames + * @param reelId - ID of the animation reel being created + * @param duration - The length of the animation in milliseconds. + * @param frames - An array of arrays containing the `x` and `y` values of successive frames: [[x1,y1],[x2,y2],...] (the values are in the unit of the sprite map's width/height respectively). + * + * @sign public this .reel(String reelId) + * Switches to the specified reel. The sprite will be updated to that reel's current frame + * @param reelID - the ID to switch to + * + * @sign public Reel .reel() + * @return The id of the current reel + * + * + * A method to handle animation reels. Only works for sprites built with the Crafty.sprite methods. + * See the Tween component for animation of 2D properties. + * + * To setup an animation reel, pass the name of the reel (used to identify the reel later), and either an + * array of absolute sprite positions or the start x on the sprite map, the y on the sprite map and then the end x on the sprite map. + * + * + * @example + * ~~~ + * // Define a sprite-map component + * Crafty.sprite(16, "images/sprite.png", { + * PlayerSprite: [0,0] + * }); + * + * // Define an animation on the second row of the sprite map (y = 1) + * // from the left most sprite (fromX = 0) to the fourth sprite + * // on that row (toX = 3), with a duration of 1 second + * Crafty.e("2D, DOM, SpriteAnimation, PlayerSprite").reel('PlayerRunning', 1000, 0, 1, 3); + * + * // This is the same animation definition, but using the alternative method + * Crafty.e("2D, DOM, SpriteAnimation, PlayerSprite").reel('PlayerRunning', 1000, [[0, 1], [1, 1], [2, 1], [3, 1]]); + * ~~~ + */ + reel: function (reelId, duration, fromX, fromY, frameCount) { + // @sign public this .reel() + if (arguments.length === 0) + return this._currentReelId; + + // @sign public this .reel(String reelID) + if (arguments.length === 1 && typeof reelId === "string"){ + if (typeof this._reels[reelId] === "undefined") + throw("The specified reel " + reelId + " is undefined."); + this.pauseAnimation(); + if (this._currentReelId !== reelId) { + this._currentReelId = reelId; + this._currentReel = this._reels[reelId]; + // Change the visible sprite + this._updateSprite(); + // Trigger event + this.trigger("ReelChange", this._currentReel); + } + return this; + } + + + var reel, i, tile, tileh, pos; + + // Get the dimensions of a single frame, as defind in Sprite component. + tile = this.__tile + parseInt(this.__padding[0] || 0, 10); + tileh = this.__tileh + parseInt(this.__padding[1] || 0, 10); + + reel = { + id: reelId, + frames: [], + currentFrame: 0, + easing: new Crafty.easing(duration), + defaultLoops: 1 + }; + + reel.duration = reel.easing.duration; + + // @sign public this .reel(String reelId, Number duration, Number fromX, Number y, Number toX) + if (typeof fromX === "number") { + i = fromX; + y = fromY; + if (frameCount >= 0) { + for (; i < fromX + frameCount ; i++) { + reel.frames.push([i * tile, y * tileh]); + } + } + else { + for (; i > fromX + frameCount; i--) { + reel.frames.push([i * tile, y * tileh]); + } + } + } + // @sign public this .reel(String reelId, Number duration, Array frames) + else if (arguments.length === 3 && typeof fromX === "object") { + + i = 0; + toX = fromX.length - 1; + + for (; i <= toX; i++) { + pos = fromX[i]; + reel.frames.push([pos[0] * tile, pos[1] * tileh]); + } + } + else { + throw "Urecognized arguments. Please see the documentation for 'reel(...)'."; + } + + this._reels[reelId] = reel; + + return this; + }, + + /**@ + * #.animate + * @comp SpriteAnimation + * @sign public this .animate([String reelId] [, Number loopCount]) + * @param reelId - ID of the animation reel to play. Defaults to the current reel if none is specified. + * @param loopCount - Number of times to repeat the animation. Use -1 to repeat indefinitely. Defaults to 1. + * + * Play one of the reels previously defined through `.reel(...)`. Simply pass the name of the reel. If you wish the + * animation to play multiple times in succession, pass in the amount of times as an additional parameter. + * To have the animation repeat indefinitely, pass in `-1`. + * + * If another animation is currently playing, it will be paused. + * + * This will always play an animation from the beginning. If you wish to resume from the current state of a reel, use `resumeAnimation()`. + * + * Once an animation ends, it will remain at its last frame. + * + * + * @example + * ~~~ + * // Define a sprite-map component + * Crafty.sprite(16, "images/sprite.png", { + * PlayerSprite: [0,0] + * }); + * + * // Play the animation across 20 frames (so each sprite in the 4 sprite animation should be seen for 5 frames) and repeat indefinitely + * Crafty.e("2D, DOM, SpriteAnimation, PlayerSprite") + * .reel('PlayerRunning', 20, 0, 0, 3) // setup animation + * .animate('PlayerRunning', -1); // start animation + * ~~~ + */ + animate: function(reelId, loopCount) { + + var pos; + + + // switch to the specified reel if necessary + if (typeof reelId === "string") + this.reel(reelId); + + var currentReel = this._currentReel; + + if (typeof currentReel === "undefined" || currentReel === null) + throw("No reel is specified, and there is no currently active reel."); + + this.pauseAnimation(); // This will pause the current animation, if one is playing + + // Handle repeats; if loopCount is undefined and reelID is a number, calling with that signature + if (typeof loopCount === "undefined") + if (typeof reelId === "number") + loopCount = reelId; + else + loopCount = 1; + + // set the animation to the beginning + currentReel.easing.reset(); + + + // user provided loop count. + this.loops(loopCount); + + // trigger the necessary events and switch to the first frame + this._setFrame(0); + + // Start the anim + this.bind("EnterFrame", this._animationTick); + this._isPlaying = true; + + this.trigger("StartAnimation", currentReel); + return this; + }, + + /**@ + * #.resumeAnimation + * @comp SpriteAnimation + * @sign public this .resumeAnimation() + * + * This will resume animation of the current reel from its current state. + * If a reel is already playing, or there is no current reel, there will be no effect. + */ + resumeAnimation: function() { + if (this._isPlaying === false && this._currentReel !== null) { + this.bind("EnterFrame", this._animationTick); + this._isPlaying = true; + this._currentReel.easing.resume(); + this.trigger("StartAnimation", this._currentReel); + } + return this; + }, + + /**@ + * #.pauseAnimation + * @comp SpriteAnimation + * @sign public this .pauseAnimation(void) + * + * Pauses the currently playing animation, or does nothing if no animation is playing. + */ + pauseAnimation: function () { + if (this._isPlaying === true) { + this.unbind("EnterFrame", this._animationTick); + this._isPlaying = false; + this._reels[this._currentReelId].easing.pause(); + } + return this; + }, + + /**@ + * #.resetAnimation + * @comp SpriteAnimation + * @sign public this .resetAnimation() + * + * Resets the current animation to its initial state. Resets the number of loops to the last specified value, which defaults to 1. + * + * Neither pauses nor resumes the current animation. + */ + resetAnimation: function(){ + var currentReel = this._currentReel; + if (currentReel === null) + throw("No active reel to reset."); + this.reelPosition(0); + currentReel.easing.repeat(currentReel.defaultLoops); + return this; + }, + + + /**@ + * #.loops + * @comp SpriteAnimation + * @sign public this .loops(Number loopCount) + * @param loopCount - The number of times to play the animation + * + * Sets the number of times the animation will loop for. + * If called while an animation is in progress, the current state will be considered the first loop. + * + * @sign public Number .loops() + * @returns The number of loops left. Returns 0 if no reel is active. + */ + loops: function(loopCount) { + if (arguments.length === 0){ + if (this._currentReel !== null) + return this._currentReel.easing.loops; + else + return 0; + } + + if (this._currentReel !== null){ + if (loopCount < 0) + loopCount = Infinity; + this._currentReel.easing.repeat(loopCount); + this._currentReel.defaultLoops = loopCount; + } + return this; + + }, + + /**@ + * #.reelPosition + * @comp SpriteAnimation + * + * @sign public this .reelPosition(Integer position) + * Sets the position of the current reel by frame number. + * @param position - the frame to jump to. This is zero-indexed. A negative values counts back from the last frame. + * + * @sign public this .reelPosition(Number position) + * Sets the position of the current reel by percent progress. + * @param position - a non-integer number between 0 and 1 + * + * @sign public this .reelPosition(String position) + * Jumps to the specified position. The only currently accepted value is "end", which will jump to the end of the reel. + * + * @sign public Number .reelPosition() + * @returns The current frame number + * + */ + reelPosition: function(position) { + if (this._currentReel === null) + throw("No active reel."); + + if (arguments.length === 0) + return this._currentReel.currentFrame; + + var progress, + l = this._currentReel.frames.length; + if (position === "end") + position = l - 1; + + if (position < 1 && position > 0) { + progress = position; + position = Math.floor(l * progress); + } else { + if (position !== Math.floor(position)) + throw("Position " + position + " is invalid."); + if (position < 0) + position = l - 1 + position; + progress = position / l; + } + // cap to last frame + position = Math.min(position, l-1); + position = Math.max(position, 0); + this._setProgress(progress); + this._setFrame(position); + + return this; + + }, + + + // Bound to "EnterFrame". Progresses the animation by dt, changing the frame if necessary. + _animationTick: function(frameData) { + var currentReel = this._reels[this._currentReelId]; + currentReel.easing.tick(frameData.dt); + var progress = currentReel.easing.value(); + var frameNumber = Math.min( Math.floor(currentReel.frames.length * progress), currentReel.frames.length - 1); + + this._setFrame(frameNumber); + + if(currentReel.easing.complete === true){ + this.trigger("AnimationEnd", this._currentReel); + this.pauseAnimation(); + } + }, + + + + + + // Set the current frame and update the displayed sprite + // The actual progress for the animation must be set seperately. + _setFrame: function(frameNumber) { + var currentReel = this._currentReel; + if (frameNumber === currentReel.currentFrame) + return; + currentReel.currentFrame = frameNumber; + this._updateSprite(); + this.trigger("FrameChange", currentReel); + }, + + // Update the displayed sprite. + _updateSprite: function() { + var currentReel = this._currentReel; + var pos = currentReel.frames[currentReel.currentFrame]; + this.__coord[0] = pos[0]; + this.__coord[1] = pos[1]; + this.trigger("Change"); // needed to trigger a redraw + + }, + + + // Sets the internal state of the current reel's easing object + _setProgress: function(progress, repeats) { + this._currentReel.easing.setProgress(progress, repeats); + + }, + + + /**@ + * #.isPlaying + * @comp SpriteAnimation + * @sign public Boolean .isPlaying([String reelId]) + * @param reelId - The reelId of the reel we wish to examine + * @returns The current animation state + * + * Determines if the specified animation is currently playing. If no reelId is specified, + * checks if any animation is playing. + * + * @example + * ~~~ + * myEntity.isPlaying() // is any animation playing + * myEntity.isPlaying('PlayerRunning') // is the PlayerRunning animation playing + * ~~~ + */ + isPlaying: function (reelId) { + if (!this._isPlaying) return false; + + if (!reelId) return !!this._currentReelId; + return this._currentReelId === reelId; + }, + + /**@ + * #.getReel + * @comp SpriteAnimation + * @sign public Reel .getReel() + * @returns The current reel, or null if there is no active reel + * + * @sign public Reel .getReel(reelId) + * @param reelId - The id of the reel to fetch. + * @returns The specified reel, or `undefined` if no such reel exists. + * + */ + getReel: function (reelId) { + if (arguments.length === 0){ + if (!this._currentReelId) return null; + reelId = this._currentReelId; + } + + return this._reels[reelId]; + } +}); + +/**@ + * #Tween + * @category Animation + * @trigger TweenEnd - when a tween finishes - String - property + * + * Component to animate the change in 2D properties over time. + */ +Crafty.c("Tween", { + + init: function(){ + this.tweenGroup = {}; + this.tweenStart = {}; + this.tweens = []; + this.bind("EnterFrame", this._tweenTick); + + }, + + _tweenTick: function(frameData){ + var tween, v, i; + for ( i = this.tweens.length-1; i>=0; i--){ + tween = this.tweens[i]; + tween.easing.tick(frameData.dt); + v = tween.easing.value(); + this._doTween(tween.props, v); + if (tween.easing.complete) { + this.tweens.splice(i, 1); + this._endTween(tween.props); + } + } + }, + + _doTween: function(props, v){ + for (var name in props) + this[name] = (1-v) * this.tweenStart[name] + v * props[name]; + + }, + + + + /**@ + * #.tween + * @comp Tween + * @sign public this .tween(Object properties, Number|String duration) + * @param properties - Object of numeric properties and what they should animate to + * @param duration - Duration to animate the properties over, in milliseconds. + * + * This method will animate numeric properties over the specified duration. + * These include `x`, `y`, `w`, `h`, `alpha` and `rotation`. + * + * The object passed should have the properties as keys and the value should be the resulting + * values of the properties. The passed object might be modified if later calls to tween animate the same properties. + * + * @example + * Move an object to 100,100 and fade out over 200 ms. + * ~~~ + * Crafty.e("2D, Tween") + * .attr({alpha: 1.0, x: 0, y: 0}) + * .tween({alpha: 0.0, x: 100, y: 100}, 200) + * ~~~ + * @example + * Rotate an object over 2 seconds + * ~~~ + * Crafty.e("2D, Tween") + * .attr({rotate:0}) + * .tween({rotate:180}, 2000) + * ~~~ + * + */ + tween: function (props, duration) { + + var tween = { + props: props, + easing: new Crafty.easing(duration) + }; + + // Tweens are grouped together by the original function call. + // Individual properties must belong to only a single group + // When a new tween starts, if it already belongs to a group, move it to the new one + // Record the group it currently belongs to, as well as its starting coordinate. + for (var propname in props){ + if (typeof this.tweenGroup[propname] !== "undefined") + this.cancelTween(propname); + this.tweenStart[propname] = this[propname]; + this.tweenGroup[propname] = props; + } + this.tweens.push(tween); + + return this; + + }, + + /**@ + * #.cancelTween + * @comp Tween + * @sign public this .cancelTween(String target) + * @param target - The property to cancel + * + * @sign public this .cancelTween(Object target) + * @param target - An object containing the properties to cancel. + * + * Stops tweening the specified property or properties. + * Passing the object used to start the tween might be a typical use of the second signature. + */ + cancelTween: function(target){ + if (typeof target === "string"){ + if (typeof this.tweenGroup[target] == "object" ) + delete this.tweenGroup[target][target]; + } else if (typeof target === "object") { + for (var propname in target) + this.cancelTween(propname); + } + + return this; + + }, + + /* + * Stops tweening the specified group of properties, and fires the "TweenEnd" event. + */ + _endTween: function(properties){ + for (var propname in properties){ + delete this.tweenGroup[propname]; + } + this.trigger("TweenEnd", properties); + } +}); + +},{"./core.js":9}],6:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +/**@ + * #Canvas + * @category Graphics + * @trigger Draw - when the entity is ready to be drawn to the stage - {type: "canvas", pos, co, ctx} + * @trigger NoCanvas - if the browser does not support canvas + * + * When this component is added to an entity it will be drawn to the global canvas element. The canvas element (and hence all Canvas entities) is always rendered below any DOM entities. + * + * Crafty.canvas.init() will be automatically called if it is not called already to initialize the canvas element. + * + * Create a canvas entity like this + * ~~~ + * var myEntity = Crafty.e("2D, Canvas, Color").color("green") + * .attr({x: 13, y: 37, w: 42, h: 42}); + *~~~ + */ +Crafty.c("Canvas", { + + init: function () { + if (!Crafty.canvas.context) { + Crafty.canvas.init(); + } + + //increment the amount of canvas objs + Crafty.DrawManager.total2D++; + //Allocate an object to hold this components current region + this.currentRect = {}; + this._changed = true; + Crafty.DrawManager.addCanvas(this); + + this.bind("Change", function (e) { + //flag if changed + if (this._changed === false) { + this._changed = true; + Crafty.DrawManager.addCanvas(this); + } + + }); + + + this.bind("Remove", function () { + Crafty.DrawManager.total2D--; + this._changed = true; + Crafty.DrawManager.addCanvas(this); + }); + }, + + /**@ + * #.draw + * @comp Canvas + * @sign public this .draw([[Context ctx, ]Number x, Number y, Number w, Number h]) + * @param ctx - Canvas 2D context if drawing on another canvas is required + * @param x - X offset for drawing a segment + * @param y - Y offset for drawing a segment + * @param w - Width of the segment to draw + * @param h - Height of the segment to draw + * + * Method to draw the entity on the canvas element. Can pass rect values for redrawing a segment of the entity. + */ + + // Cache the various objects and arrays used in draw: + drawVars: { + type: "canvas", + pos: {}, + ctx: null, + coord: [0, 0, 0, 0], + co: { + x: 0, + y: 0, + w: 0, + h: 0 + } + + + }, + + draw: function (ctx, x, y, w, h) { + if (!this.ready) return; + if (arguments.length === 4) { + h = w; + w = y; + y = x; + x = ctx; + ctx = Crafty.canvas.context; + } + + var pos = this.drawVars.pos; + pos._x = (this._x + (x || 0)); + pos._y = (this._y + (y || 0)); + pos._w = (w || this._w); + pos._h = (h || this._h); + + + context = ctx || Crafty.canvas.context; + coord = this.__coord || [0, 0, 0, 0]; + var co = this.drawVars.co; + co.x = coord[0] + (x || 0); + co.y = coord[1] + (y || 0); + co.w = w || coord[2]; + co.h = h || coord[3]; + + if (this._mbr) { + context.save(); + + context.translate(this._origin.x + this._x, this._origin.y + this._y); + pos._x = -this._origin.x; + pos._y = -this._origin.y; + + context.rotate((this._rotation % 360) * (Math.PI / 180)); + } + + if (this._flipX || this._flipY) { + context.save(); + context.scale((this._flipX ? -1 : 1), (this._flipY ? -1 : 1)); + if (this._flipX) { + pos._x = -(pos._x + pos._w); + } + if (this._flipY) { + pos._y = -(pos._y + pos._h); + } + } + + var globalpha; + + //draw with alpha + if (this._alpha < 1.0) { + globalpha = context.globalAlpha; + context.globalAlpha = this._alpha; + } + + this.drawVars.ctx = context; + this.trigger("Draw", this.drawVars); + + if (this._mbr || (this._flipX || this._flipY)) { + context.restore(); + } + if (globalpha) { + context.globalAlpha = globalpha; + } + return this; + } +}); + +/**@ + * #Crafty.canvas + * @category Graphics + * + * Collection of methods to draw on canvas. + */ +Crafty.extend({ + canvas: { + /**@ + * #Crafty.canvas.context + * @comp Crafty.canvas + * + * This will return the 2D context of the main canvas element. + * The value returned from `Crafty.canvas._canvas.getContext('2d')`. + */ + context: null, + /**@ + * #Crafty.canvas._canvas + * @comp Crafty.canvas + * + * Main Canvas element + */ + + /**@ + * #Crafty.canvas.init + * @comp Crafty.canvas + * @sign public void Crafty.canvas.init(void) + * @trigger NoCanvas - triggered if `Crafty.support.canvas` is false + * + * Creates a `canvas` element inside `Crafty.stage.elem`. Must be called + * before any entities with the Canvas component can be drawn. + * + * This method will automatically be called if no `Crafty.canvas.context` is + * found. + */ + init: function () { + //check if canvas is supported + if (!Crafty.support.canvas) { + Crafty.trigger("NoCanvas"); + Crafty.stop(); + return; + } + + //create an empty canvas element + var c; + c = document.createElement("canvas"); + c.width = Crafty.viewport.width; + c.height = Crafty.viewport.height; + c.style.position = 'absolute'; + c.style.left = "0px"; + c.style.top = "0px"; + + Crafty.stage.elem.appendChild(c); + Crafty.canvas.context = c.getContext('2d'); + Crafty.canvas._canvas = c; + + //Set any existing transformations + var zoom = Crafty.viewport._scale; + if (zoom != 1) + Crafty.canvas.context.scale(zoom, zoom); + + //Bind rendering of canvas context (see drawing.js) + Crafty.unbind("RenderScene", Crafty.DrawManager.renderCanvas); + Crafty.bind("RenderScene", Crafty.DrawManager.renderCanvas); + } + + } +}); + +},{"./core.js":9}],7:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document, + DEG_TO_RAD = Math.PI / 180; + +/**@ + * #Collision + * @category 2D + * Component to detect collision between any two convex polygons. + */ +Crafty.c("Collision", { + /**@ + * #.init + * @comp Collision + * Create a rectangle polygon based on the x, y, w, h dimensions. + * + * By default, the collision hitbox will match the dimensions (x, y, w, h) and rotation of the object. + */ + init: function () { + this.requires("2D"); + var area = this._mbr || this; + + this.collision(); + }, + + /**@ + * #.collision + * @comp Collision + * + * @trigger NewHitbox - when a new hitbox is assigned - Crafty.polygon + * + * @sign public this .collision([Crafty.polygon polygon]) + * @param polygon - Crafty.polygon object that will act as the hit area + * + * @sign public this .collision(Array point1, .., Array pointN) + * @param point# - Array with an `x` and `y` position to generate a polygon + * + * Constructor takes a polygon or array of points to use as the hit area. + * + * The hit area (polygon) must be a convex shape and not concave + * for the collision detection to work. + * + * Points are relative to the object's position and its unrotated state. + * + * If no parameter is passed, the x, y, w, h properties of the entity will be used, and the hitbox will be resized when the entity is. + * + * @example + * ~~~ + * Crafty.e("2D, Collision").collision( + * new Crafty.polygon([50,0], [100,100], [0,100]) + * ); + * + * Crafty.e("2D, Collision").collision([50,0], [100,100], [0,100]); + * ~~~ + * + * @see Crafty.polygon + */ + collision: function (poly) { + this.unbind("Resize", this._resizeMap); + if (!poly) { + poly = new Crafty.polygon([0, 0], [this._w, 0], [this._w, this._h], [0, this._h]); + this.bind("Resize", this._resizeMap); + } + + if (arguments.length > 1) { + //convert args to array to create polygon + var args = Array.prototype.slice.call(arguments, 0); + poly = new Crafty.polygon(args); + } + + if (this.rotation) { + poly.rotate({ + cos: Math.cos(-this.rotation * DEG_TO_RAD), + sin: Math.sin(-this.rotation * DEG_TO_RAD), + o: { + x: this._origin.x, + y: this._origin.y + } + }); + } + + this.map = poly; + this.attach(this.map); + this.map.shift(this._x, this._y); + this.trigger("NewHitbox", poly); + return this; + }, + + + // Change the hitbox when a "Resize" event triggers. + _resizeMap: function (e) { + + var dx, dy, rot = this.rotation * DEG_TO_RAD, + points = this.map.points; + + // Depending on the change of axis, move the corners of the rectangle appropriately + if (e.axis === 'w') { + + if (rot) { + dx = e.amount * Math.cos(rot); + dy = e.amount * Math.sin(rot); + } else { + dx = e.amount; + dy = 0; + } + + // "top right" point shifts on change of w + points[1][0] += dx; + points[1][1] += dy; + } else { + + if (rot) { + dy = e.amount * Math.cos(rot); + dx = -e.amount * Math.sin(rot); + } else { + dx = 0; + dy = e.amount; + } + + // "bottom left" point shifts on change of h + points[3][0] += dx; + points[3][1] += dy; + } + + // "bottom right" point shifts on either change + points[2][0] += dx; + points[2][1] += dy; + + }, + + /**@ + * #.hit + * @comp Collision + * @sign public Boolean/Array hit(String component) + * @param component - Check collision with entities that has this component + * @return `false` if no collision. If a collision is detected, returns an Array of objects that are colliding. + * + * Takes an argument for a component to test collision for. If a collision is found, an array of + * every object in collision along with the amount of overlap is passed. + * + * If no collision, will return false. The return collision data will be an Array of Objects with the + * type of collision used, the object collided and if the type used was SAT (a polygon was used as the hitbox) then an amount of overlap.\ + * ~~~ + * [{ + * obj: [entity], + * type "MBR" or "SAT", + * overlap: [number] + * }] + * ~~~ + * `MBR` is your standard axis aligned rectangle intersection (`.intersect` in the 2D component). + * `SAT` is collision between any convex polygon. + * + * @see .onHit, 2D + */ + hit: function (comp) { + var area = this._mbr || this, + results = Crafty.map.search(area, false), + i = 0, + l = results.length, + dupes = {}, + id, obj, oarea, key, + hasMap = ('map' in this && 'containsPoint' in this.map), + finalresult = []; + + if (!l) { + return false; + } + + for (; i < l; ++i) { + obj = results[i]; + oarea = obj._mbr || obj; //use the mbr + + if (!obj) continue; + id = obj[0]; + + //check if not added to hash and that actually intersects + if (!dupes[id] && this[0] !== id && obj.__c[comp] && + oarea._x < area._x + area._w && oarea._x + oarea._w > area._x && + oarea._y < area._y + area._h && oarea._h + oarea._y > area._y) + dupes[id] = obj; + } + + for (key in dupes) { + obj = dupes[key]; + + if (hasMap && 'map' in obj) { + var SAT = this._SAT(this.map, obj.map); + SAT.obj = obj; + SAT.type = "SAT"; + if (SAT) finalresult.push(SAT); + } else { + finalresult.push({ + obj: obj, + type: "MBR" + }); + } + } + + if (!finalresult.length) { + return false; + } + + return finalresult; + }, + + /**@ + * #.onHit + * @comp Collision + * @sign public this .onHit(String component, Function hit[, Function noHit]) + * @param component - Component to check collisions for + * @param hit - Callback method to execute upon collision with component. Will be passed the results of the collision check in the same format documented for hit(). + * @param noHit - Callback method executed once as soon as collision stops + * + * Creates an EnterFrame event calling .hit() each frame. When a collision is detected the callback will be invoked. + * + * @see .hit + */ + onHit: function (comp, callback, callbackOff) { + var justHit = false; + this.bind("EnterFrame", function () { + var hitdata = this.hit(comp); + if (hitdata) { + justHit = true; + callback.call(this, hitdata); + } else if (justHit) { + if (typeof callbackOff == 'function') { + callbackOff.call(this); + } + justHit = false; + } + }); + return this; + }, + + _SAT: function (poly1, poly2) { + var points1 = poly1.points, + points2 = poly2.points, + i = 0, + l = points1.length, + j, k = points2.length, + normal = { + x: 0, + y: 0 + }, + length, + min1, min2, + max1, max2, + interval, + MTV = null, + MTV2 = null, + MN = null, + dot, + nextPoint, + currentPoint; + + //loop through the edges of Polygon 1 + for (; i < l; i++) { + nextPoint = points1[(i == l - 1 ? 0 : i + 1)]; + currentPoint = points1[i]; + + //generate the normal for the current edge + normal.x = -(nextPoint[1] - currentPoint[1]); + normal.y = (nextPoint[0] - currentPoint[0]); + + //normalize the vector + length = Math.sqrt(normal.x * normal.x + normal.y * normal.y); + normal.x /= length; + normal.y /= length; + + //default min max + min1 = min2 = -1; + max1 = max2 = -1; + + //project all vertices from poly1 onto axis + for (j = 0; j < l; ++j) { + dot = points1[j][0] * normal.x + points1[j][1] * normal.y; + if (dot > max1 || max1 === -1) max1 = dot; + if (dot < min1 || min1 === -1) min1 = dot; + } + + //project all vertices from poly2 onto axis + for (j = 0; j < k; ++j) { + dot = points2[j][0] * normal.x + points2[j][1] * normal.y; + if (dot > max2 || max2 === -1) max2 = dot; + if (dot < min2 || min2 === -1) min2 = dot; + } + + //calculate the minimum translation vector should be negative + if (min1 < min2) { + interval = min2 - max1; + + normal.x = -normal.x; + normal.y = -normal.y; + } else { + interval = min1 - max2; + } + + //exit early if positive + if (interval >= 0) { + return false; + } + + if (MTV === null || interval > MTV) { + MTV = interval; + MN = { + x: normal.x, + y: normal.y + }; + } + } + + //loop through the edges of Polygon 2 + for (i = 0; i < k; i++) { + nextPoint = points2[(i == k - 1 ? 0 : i + 1)]; + currentPoint = points2[i]; + + //generate the normal for the current edge + normal.x = -(nextPoint[1] - currentPoint[1]); + normal.y = (nextPoint[0] - currentPoint[0]); + + //normalize the vector + length = Math.sqrt(normal.x * normal.x + normal.y * normal.y); + normal.x /= length; + normal.y /= length; + + //default min max + min1 = min2 = -1; + max1 = max2 = -1; + + //project all vertices from poly1 onto axis + for (j = 0; j < l; ++j) { + dot = points1[j][0] * normal.x + points1[j][1] * normal.y; + if (dot > max1 || max1 === -1) max1 = dot; + if (dot < min1 || min1 === -1) min1 = dot; + } + + //project all vertices from poly2 onto axis + for (j = 0; j < k; ++j) { + dot = points2[j][0] * normal.x + points2[j][1] * normal.y; + if (dot > max2 || max2 === -1) max2 = dot; + if (dot < min2 || min2 === -1) min2 = dot; + } + + //calculate the minimum translation vector should be negative + if (min1 < min2) { + interval = min2 - max1; + + normal.x = -normal.x; + normal.y = -normal.y; + } else { + interval = min1 - max2; + + + } + + //exit early if positive + if (interval >= 0) { + return false; + } + + if (MTV === null || interval > MTV) MTV = interval; + if (interval > MTV2 || MTV2 === null) { + MTV2 = interval; + MN = { + x: normal.x, + y: normal.y + }; + } + } + + return { + overlap: MTV2, + normal: MN + }; + } +}); + +},{"./core.js":9}],8:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +Crafty.extend({ + over: null, //object mouseover, waiting for out + mouseObjs: 0, + mousePos: {}, + lastEvent: null, + keydown: {}, + selected: false, + + /**@ + * #Crafty.keydown + * @category Input + * Remembering what keys (referred by Unicode) are down. + * + * @example + * ~~~ + * Crafty.c("Keyboard", { + * isDown: function (key) { + * if (typeof key === "string") { + * key = Crafty.keys[key]; + * } + * return !!Crafty.keydown[key]; + * } + * }); + * ~~~ + * @see Keyboard, Crafty.keys + */ + + detectBlur: function (e) { + var selected = ((e.clientX > Crafty.stage.x && e.clientX < Crafty.stage.x + Crafty.viewport.width) && + (e.clientY > Crafty.stage.y && e.clientY < Crafty.stage.y + Crafty.viewport.height)); + + if (!Crafty.selected && selected) + Crafty.trigger("CraftyFocus"); + if (Crafty.selected && !selected) + Crafty.trigger("CraftyBlur"); + + Crafty.selected = selected; + }, + /**@ + * #Crafty.mouseDispatch + * @category Input + * + * Internal method which dispatches mouse events received by Crafty (crafty.stage.elem). + * The mouse events get dispatched to the closest entity to the source of the event (if available). + * + * This method also sets a global property Crafty.lastEvent, which holds the most recent event that + * occured (useful for determining mouse position in every frame). + * ~~~ + * var newestX = Crafty.lastEvent.realX, + * newestY = Crafty.lastEvent.realY; + * ~~~ + * + * Notable properties of a MouseEvent e: + * ~~~ + * e.clientX, e.clientY //(x,y) coordinates of mouse event in web browser screen space + * e.realX, e.realY //(x,y) coordinates of mouse event in world/viewport space + * e.mouseButton // Normalized mouse button according to Crafty.mouseButtons + * ~~~ + * @see Crafty.touchDispatch + */ + mouseDispatch: function (e) { + + if (!Crafty.mouseObjs) return; + Crafty.lastEvent = e; + + var maxz = -1, + closest, + q, + i = 0, + l, + pos = Crafty.DOM.translate(e.clientX, e.clientY), + x, y, + dupes = {}, + tar = e.target ? e.target : e.srcElement, + type = e.type; + + //Normalize button according to http://unixpapa.com/js/mouse.html + if (typeof e.which === 'undefined') { + e.mouseButton = (e.button < 2) ? Crafty.mouseButtons.LEFT : ((e.button == 4) ? Crafty.mouseButtons.MIDDLE : Crafty.mouseButtons.RIGHT); + } else { + e.mouseButton = (e.which < 2) ? Crafty.mouseButtons.LEFT : ((e.which == 2) ? Crafty.mouseButtons.MIDDLE : Crafty.mouseButtons.RIGHT); + } + + e.realX = x = Crafty.mousePos.x = pos.x; + e.realY = y = Crafty.mousePos.y = pos.y; + + //if it's a DOM element with Mouse component we are done + if (tar.nodeName != "CANVAS") { + while (typeof (tar.id) != 'string' && tar.id.indexOf('ent') == -1) { + tar = tar.parentNode; + } + ent = Crafty(parseInt(tar.id.replace('ent', ''), 10)); + if (ent.has('Mouse') && ent.isAt(x, y)) + closest = ent; + } + //else we search for an entity with Mouse component + if (!closest) { + q = Crafty.map.search({ + _x: x, + _y: y, + _w: 1, + _h: 1 + }, false); + + for (l = q.length; i < l; ++i) { + if (!q[i].__c.Mouse || !q[i]._visible) continue; + + var current = q[i], + flag = false; + + //weed out duplicates + if (dupes[current[0]]) continue; + else dupes[current[0]] = true; + + if (current.mapArea) { + if (current.mapArea.containsPoint(x, y)) { + flag = true; + } + } else if (current.isAt(x, y)) flag = true; + + if (flag && (current._z >= maxz || maxz === -1)) { + //if the Z is the same, select the closest GUID + if (current._z === maxz && current[0] < closest[0]) { + continue; + } + maxz = current._z; + closest = current; + } + } + } + + //found closest object to mouse + if (closest) { + //click must mousedown and out on tile + if (type === "mousedown") { + closest.trigger("MouseDown", e); + } else if (type === "mouseup") { + closest.trigger("MouseUp", e); + } else if (type == "dblclick") { + closest.trigger("DoubleClick", e); + } else if (type == "click") { + closest.trigger("Click", e); + } else if (type === "mousemove") { + closest.trigger("MouseMove", e); + if (this.over !== closest) { //if new mousemove, it is over + if (this.over) { + this.over.trigger("MouseOut", e); //if over wasn't null, send mouseout + this.over = null; + } + this.over = closest; + closest.trigger("MouseOver", e); + } + } else closest.trigger(type, e); //trigger whatever it is + } else { + if (type === "mousemove" && this.over) { + this.over.trigger("MouseOut", e); + this.over = null; + } + if (type === "mousedown") { + Crafty.viewport.mouselook('start', e); + } else if (type === "mousemove") { + Crafty.viewport.mouselook('drag', e); + } else if (type == "mouseup") { + Crafty.viewport.mouselook('stop'); + } + } + + if (type === "mousemove") { + this.lastEvent = e; + } + + }, + + + /**@ + * #Crafty.touchDispatch + * @category Input + * + * TouchEvents have a different structure then MouseEvents. + * The relevant data lives in e.changedTouches[0]. + * To normalize TouchEvents we catch em and dispatch a mock MouseEvent instead. + * + * @see Crafty.mouseDispatch + */ + + touchDispatch: function (e) { + var type, + lastEvent = Crafty.lastEvent; + + if (e.type === "touchstart") type = "mousedown"; + else if (e.type === "touchmove") type = "mousemove"; + else if (e.type === "touchend") type = "mouseup"; + else if (e.type === "touchcancel") type = "mouseup"; + else if (e.type === "touchleave") type = "mouseup"; + + if (e.touches && e.touches.length) { + first = e.touches[0]; + } else if (e.changedTouches && e.changedTouches.length) { + first = e.changedTouches[0]; + } + + var simulatedEvent = document.createEvent("MouseEvent"); + simulatedEvent.initMouseEvent(type, true, true, window, 1, + first.screenX, + first.screenY, + first.clientX, + first.clientY, + false, false, false, false, 0, e.relatedTarget + ); + + first.target.dispatchEvent(simulatedEvent); + + // trigger click when it should be triggered + if (lastEvent !== null && lastEvent.type == 'mousedown' && type == 'mouseup') { + type = 'click'; + + simulatedEvent = document.createEvent("MouseEvent"); + simulatedEvent.initMouseEvent(type, true, true, window, 1, + first.screenX, + first.screenY, + first.clientX, + first.clientY, + false, false, false, false, 0, e.relatedTarget + ); + first.target.dispatchEvent(simulatedEvent); + } + + if (e.preventDefault) e.preventDefault(); + else e.returnValue = false; + }, + + + /**@ + * #KeyboardEvent + * @category Input + * Keyboard Event triggered by Crafty Core + * @trigger KeyDown - is triggered for each entity when the DOM 'keydown' event is triggered. + * @trigger KeyUp - is triggered for each entity when the DOM 'keyup' event is triggered. + * + * @example + * ~~~ + * Crafty.e("2D, DOM, Color") + * .attr({x: 100, y: 100, w: 50, h: 50}) + * .color("red") + * .bind('KeyDown', function(e) { + * if(e.key == Crafty.keys['LEFT_ARROW']) { + * this.x=this.x-1; + * } else if (e.key == Crafty.keys['RIGHT_ARROW']) { + * this.x=this.x+1; + * } else if (e.key == Crafty.keys['UP_ARROW']) { + * this.y=this.y-1; + * } else if (e.key == Crafty.keys['DOWN_ARROW']) { + * this.y=this.y+1; + * } + * }); + * ~~~ + * + * @see Crafty.keys + */ + + /**@ + * #Crafty.eventObject + * @category Input + * + * Event Object used in Crafty for cross browser compatibility + */ + + /**@ + * #.key + * @comp Crafty.eventObject + * + * Unicode of the key pressed + */ + keyboardDispatch: function (e) { + // Use a Crafty-standard event object to avoid cross-browser issues + var original = e, + evnt = {}, + props = "char charCode keyCode type shiftKey ctrlKey metaKey timestamp".split(" "); + for (var i = props.length; i;) { + var prop = props[--i]; + evnt[prop] = original[prop]; + } + evnt.which = original.charCode !== null ? original.charCode : original.keyCode; + evnt.key = original.keyCode || original.which; + evnt.originalEvent = original; + e = evnt; + + if (e.type === "keydown") { + if (Crafty.keydown[e.key] !== true) { + Crafty.keydown[e.key] = true; + Crafty.trigger("KeyDown", e); + } + } else if (e.type === "keyup") { + delete Crafty.keydown[e.key]; + Crafty.trigger("KeyUp", e); + } + + //prevent default actions for all keys except backspace and F1-F12 and except actions in INPUT and TEXTAREA. + //prevent bubbling up for all keys except backspace and F1-F12. + //Among others this prevent the arrow keys from scrolling the parent page + //of an iframe hosting the game + if (Crafty.selected && !(e.key == 8 || e.key >= 112 && e.key <= 135)) { + if (e.stopPropagation) e.stopPropagation(); + else e.cancelBubble = true; + + //Don't prevent default actions if target node is input or textarea. + if (e.target && e.target.nodeName !== 'INPUT' && e.target.nodeName !== 'TEXTAREA') { + if (e.preventDefault) { + e.preventDefault(); + } else { + e.returnValue = false; + } + } + return false; + } + } +}); + +//initialize the input events onload +Crafty.bind("Load", function () { + Crafty.addEvent(this, "keydown", Crafty.keyboardDispatch); + Crafty.addEvent(this, "keyup", Crafty.keyboardDispatch); + + Crafty.addEvent(this, Crafty.stage.elem, "mousedown", Crafty.mouseDispatch); + Crafty.addEvent(this, Crafty.stage.elem, "mouseup", Crafty.mouseDispatch); + Crafty.addEvent(this, document.body, "mouseup", Crafty.detectBlur); + Crafty.addEvent(this, Crafty.stage.elem, "mousemove", Crafty.mouseDispatch); + Crafty.addEvent(this, Crafty.stage.elem, "click", Crafty.mouseDispatch); + Crafty.addEvent(this, Crafty.stage.elem, "dblclick", Crafty.mouseDispatch); + + Crafty.addEvent(this, Crafty.stage.elem, "touchstart", Crafty.touchDispatch); + Crafty.addEvent(this, Crafty.stage.elem, "touchmove", Crafty.touchDispatch); + Crafty.addEvent(this, Crafty.stage.elem, "touchend", Crafty.touchDispatch); + Crafty.addEvent(this, Crafty.stage.elem, "touchcancel", Crafty.touchDispatch); + Crafty.addEvent(this, Crafty.stage.elem, "touchleave", Crafty.touchDispatch); +}); + +Crafty.bind("CraftyStop", function () { + Crafty.removeEvent(this, "keydown", Crafty.keyboardDispatch); + Crafty.removeEvent(this, "keyup", Crafty.keyboardDispatch); + + if (Crafty.stage) { + Crafty.removeEvent(this, Crafty.stage.elem, "mousedown", Crafty.mouseDispatch); + Crafty.removeEvent(this, Crafty.stage.elem, "mouseup", Crafty.mouseDispatch); + Crafty.removeEvent(this, Crafty.stage.elem, "mousemove", Crafty.mouseDispatch); + Crafty.removeEvent(this, Crafty.stage.elem, "click", Crafty.mouseDispatch); + Crafty.removeEvent(this, Crafty.stage.elem, "dblclick", Crafty.mouseDispatch); + + Crafty.removeEvent(this, Crafty.stage.elem, "touchstart", Crafty.touchDispatch); + Crafty.removeEvent(this, Crafty.stage.elem, "touchmove", Crafty.touchDispatch); + Crafty.removeEvent(this, Crafty.stage.elem, "touchend", Crafty.touchDispatch); + Crafty.removeEvent(this, Crafty.stage.elem, "touchcancel", Crafty.touchDispatch); + Crafty.removeEvent(this, Crafty.stage.elem, "touchleave", Crafty.touchDispatch); + } + + Crafty.removeEvent(this, document.body, "mouseup", Crafty.detectBlur); +}); + +/**@ + * #Mouse + * @category Input + * Provides the entity with mouse related events + * @trigger MouseOver - when the mouse enters the entity - MouseEvent + * @trigger MouseOut - when the mouse leaves the entity - MouseEvent + * @trigger MouseDown - when the mouse button is pressed on the entity - MouseEvent + * @trigger MouseUp - when the mouse button is released on the entity - MouseEvent + * @trigger Click - when the user clicks the entity. [See documentation](http://www.quirksmode.org/dom/events/click.html) - MouseEvent + * @trigger DoubleClick - when the user double clicks the entity - MouseEvent + * @trigger MouseMove - when the mouse is over the entity and moves - MouseEvent + * Crafty adds the mouseButton property to MouseEvents that match one of + * + * ~~~ + * - Crafty.mouseButtons.LEFT + * - Crafty.mouseButtons.RIGHT + * - Crafty.mouseButtons.MIDDLE + * ~~~ + * + * @example + * ~~~ + * myEntity.bind('Click', function() { + * console.log("Clicked!!"); + * }) + * + * myEntity.bind('MouseUp', function(e) { + * if( e.mouseButton == Crafty.mouseButtons.RIGHT ) + * console.log("Clicked right button"); + * }) + * ~~~ + * @see Crafty.mouseDispatch + */ +Crafty.c("Mouse", { + init: function () { + Crafty.mouseObjs++; + this.bind("Remove", function () { + Crafty.mouseObjs--; + }); + }, + + /**@ + * #.areaMap + * @comp Mouse + * @sign public this .areaMap(Crafty.polygon polygon) + * @param polygon - Instance of Crafty.polygon used to check if the mouse coordinates are inside this region + * @sign public this .areaMap(Array point1, .., Array pointN) + * @param point# - Array with an `x` and `y` position to generate a polygon + * + * Assign a polygon to the entity so that mouse events will only be triggered if + * the coordinates are inside the given polygon. + * + * @example + * ~~~ + * Crafty.e("2D, DOM, Color, Mouse") + * .color("red") + * .attr({ w: 100, h: 100 }) + * .bind('MouseOver', function() {console.log("over")}) + * .areaMap([0,0], [50,0], [50,50], [0,50]) + * ~~~ + * + * @see Crafty.polygon + */ + areaMap: function (poly) { + //create polygon + if (arguments.length > 1) { + //convert args to array to create polygon + var args = Array.prototype.slice.call(arguments, 0); + poly = new Crafty.polygon(args); + } + + poly.shift(this._x, this._y); + //this.map = poly; + this.mapArea = poly; + + this.attach(this.mapArea); + return this; + } +}); + +/**@ + * #Draggable + * @category Input + * Enable drag and drop of the entity. + * @trigger Dragging - is triggered each frame the entity is being dragged - MouseEvent + * @trigger StartDrag - is triggered when dragging begins - MouseEvent + * @trigger StopDrag - is triggered when dragging ends - MouseEvent + */ +Crafty.c("Draggable", { + _origMouseDOMPos: null, + _oldX: null, + _oldY: null, + _dragging: false, + _dir: null, + + //Note: the code is not tested with zoom, etc., that may distort the direction between the viewport and the coordinate on the canvas. + init: function () { + this.requires("Mouse"); + this.enableDrag(); + }, + + _ondrag: function (e) { + // While a drag is occurring, this method is bound to the mousemove DOM event + var pos = Crafty.DOM.translate(e.clientX, e.clientY); + + // ignore invalid 0 0 position - strange problem on ipad + if (pos.x === 0 || pos.y === 0) { + return false; + } + + if (this._dir) { + var len = (pos.x - this._origMouseDOMPos.x) * this._dir.x + (pos.y - this._origMouseDOMPos.y) * this._dir.y; + this.x = this._oldX + len * this._dir.x; + this.y = this._oldY + len * this._dir.y; + } else { + this.x = this._oldX + (pos.x - this._origMouseDOMPos.x); + this.y = this._oldY + (pos.y - this._origMouseDOMPos.y); + } + + this.trigger("Dragging", e); + }, + + _ondown: function (e) { + // When dragging is enabled, this method is bound to the MouseDown crafty event + if (e.mouseButton !== Crafty.mouseButtons.LEFT) return; + this._startDrag(e); + }, + + _onup: function (e) { + // While a drag is occurring, this method is bound to mouseup DOM event + if (this._dragging === true) { + Crafty.removeEvent(this, Crafty.stage.elem, "mousemove", this._ondrag); + Crafty.removeEvent(this, Crafty.stage.elem, "mouseup", this._onup); + this._dragging = false; + this.trigger("StopDrag", e); + } + }, + + /**@ + * #.dragDirection + * @comp Draggable + * @sign public this .dragDirection() + * Remove any previously specified direction. + * + * @sign public this .dragDirection(vector) + * @param vector - Of the form of {x: valx, y: valy}, the vector (valx, valy) denotes the move direction. + * + * @sign public this .dragDirection(degree) + * @param degree - A number, the degree (clockwise) of the move direction with respect to the x axis. + * Specify the dragging direction. + * + * @example + * ~~~ + * this.dragDirection() + * this.dragDirection({x:1, y:0}) //Horizontal + * this.dragDirection({x:0, y:1}) //Vertical + * // Note: because of the orientation of x and y axis, + * // this is 45 degree clockwise with respect to the x axis. + * this.dragDirection({x:1, y:1}) //45 degree. + * this.dragDirection(60) //60 degree. + * ~~~ + */ + dragDirection: function (dir) { + if (typeof dir === 'undefined') { + this._dir = null; + } else if (("" + parseInt(dir, 10)) == dir) { //dir is a number + this._dir = { + x: Math.cos(dir / 180 * Math.PI), + y: Math.sin(dir / 180 * Math.PI) + }; + } else { + var r = Math.sqrt(dir.x * dir.x + dir.y * dir.y); + this._dir = { + x: dir.x / r, + y: dir.y / r + }; + } + }, + + + /**@ + * #._startDrag + * @comp Draggable + * Internal method for starting a drag of an entity either programatically or via Mouse click + * + * @param e - a mouse event + */ + _startDrag: function (e) { + this._origMouseDOMPos = Crafty.DOM.translate(e.clientX, e.clientY); + this._oldX = this._x; + this._oldY = this._y; + this._dragging = true; + + Crafty.addEvent(this, Crafty.stage.elem, "mousemove", this._ondrag); + Crafty.addEvent(this, Crafty.stage.elem, "mouseup", this._onup); + this.trigger("StartDrag", e); + }, + + /**@ + * #.stopDrag + * @comp Draggable + * @sign public this .stopDrag(void) + * @trigger StopDrag - Called right after the mouse listeners are removed + * + * Stop the entity from dragging. Essentially reproducing the drop. + * + * @see .startDrag + */ + stopDrag: function () { + Crafty.removeEvent(this, Crafty.stage.elem, "mousemove", this._ondrag); + Crafty.removeEvent(this, Crafty.stage.elem, "mouseup", this._onup); + + this._dragging = false; + this.trigger("StopDrag"); + return this; + }, + + /**@ + * #.startDrag + * @comp Draggable + * @sign public this .startDrag(void) + * + * Make the entity follow the mouse positions. + * + * @see .stopDrag + */ + startDrag: function () { + if (!this._dragging) { + //Use the last known position of the mouse + this._startDrag(Crafty.lastEvent); + } + return this; + }, + + /**@ + * #.enableDrag + * @comp Draggable + * @sign public this .enableDrag(void) + * + * Rebind the mouse events. Use if `.disableDrag` has been called. + * + * @see .disableDrag + */ + enableDrag: function () { + this.bind("MouseDown", this._ondown); + + Crafty.addEvent(this, Crafty.stage.elem, "mouseup", this._onup); + return this; + }, + + /**@ + * #.disableDrag + * @comp Draggable + * @sign public this .disableDrag(void) + * + * Stops entity from being draggable. Reenable with `.enableDrag()`. + * + * @see .enableDrag + */ + disableDrag: function () { + this.unbind("MouseDown", this._ondown); + if (this._dragging) { + this.stopDrag(); + } + return this; + } +}); + +/**@ + * #Keyboard + * @category Input + * Give entities keyboard events (`keydown` and `keyup`). + */ +Crafty.c("Keyboard", { + /**@ + * #.isDown + * @comp Keyboard + * @sign public Boolean isDown(String keyName) + * @param keyName - Name of the key to check. See `Crafty.keys`. + * @sign public Boolean isDown(Number keyCode) + * @param keyCode - Key code in `Crafty.keys`. + * + * Determine if a certain key is currently down. + * + * @example + * ~~~ + * entity.requires('Keyboard').bind('KeyDown', function () { if (this.isDown('SPACE')) jump(); }); + * ~~~ + * + * @see Crafty.keys + */ + isDown: function (key) { + if (typeof key === "string") { + key = Crafty.keys[key]; + } + return !!Crafty.keydown[key]; + } +}); + +/**@ + * #Multiway + * @category Input + * Used to bind keys to directions and have the entity move accordingly + * @trigger NewDirection - triggered when direction changes - { x:Number, y:Number } - New direction + * @trigger Moved - triggered on movement on either x or y axis. If the entity has moved on both axes for diagonal movement the event is triggered twice - { x:Number, y:Number } - Old position + */ +Crafty.c("Multiway", { + _speed: 3, + + _keydown: function (e) { + if (this._keys[e.key]) { + this._movement.x = Math.round((this._movement.x + this._keys[e.key].x) * 1000) / 1000; + this._movement.y = Math.round((this._movement.y + this._keys[e.key].y) * 1000) / 1000; + this.trigger('NewDirection', this._movement); + } + }, + + _keyup: function (e) { + if (this._keys[e.key]) { + this._movement.x = Math.round((this._movement.x - this._keys[e.key].x) * 1000) / 1000; + this._movement.y = Math.round((this._movement.y - this._keys[e.key].y) * 1000) / 1000; + this.trigger('NewDirection', this._movement); + } + }, + + _enterframe: function () { + if (this.disableControls) return; + + if (this._movement.x !== 0) { + this.x += this._movement.x; + this.trigger('Moved', { + x: this.x - this._movement.x, + y: this.y + }); + } + if (this._movement.y !== 0) { + this.y += this._movement.y; + this.trigger('Moved', { + x: this.x, + y: this.y - this._movement.y + }); + } + }, + + _initializeControl: function () { + return this.unbind("KeyDown", this._keydown) + .unbind("KeyUp", this._keyup) + .unbind("EnterFrame", this._enterframe) + .bind("KeyDown", this._keydown) + .bind("KeyUp", this._keyup) + .bind("EnterFrame", this._enterframe); + }, + + /**@ + * #.multiway + * @comp Multiway + * @sign public this .multiway([Number speed,] Object keyBindings ) + * @param speed - Amount of pixels to move the entity whilst a key is down + * @param keyBindings - What keys should make the entity go in which direction. Direction is specified in degrees + * Constructor to initialize the speed and keyBindings. Component will listen to key events and move the entity appropriately. + * + * When direction changes a NewDirection event is triggered with an object detailing the new direction: {x: x_movement, y: y_movement} + * When entity has moved on either x- or y-axis a Moved event is triggered with an object specifying the old position {x: old_x, y: old_y} + * + * @example + * ~~~ + * this.multiway(3, {UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180}); + * this.multiway({x:3,y:1.5}, {UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180}); + * this.multiway({W: -90, S: 90, D: 0, A: 180}); + * ~~~ + */ + multiway: function (speed, keys) { + this._keyDirection = {}; + this._keys = {}; + this._movement = { + x: 0, + y: 0 + }; + this._speed = { + x: 3, + y: 3 + }; + + if (keys) { + if (speed.x !== undefined && speed.y !== undefined) { + this._speed.x = speed.x; + this._speed.y = speed.y; + } else { + this._speed.x = speed; + this._speed.y = speed; + } + } else { + keys = speed; + } + + this._keyDirection = keys; + this.speed(this._speed); + + this._initializeControl(); + + //Apply movement if key is down when created + for (var k in keys) { + if (Crafty.keydown[Crafty.keys[k]]) { + this.trigger("KeyDown", { + key: Crafty.keys[k] + }); + } + } + + return this; + }, + + /**@ + * #.enableControl + * @comp Multiway + * @sign public this .enableControl() + * + * Enable the component to listen to key events. + * + * @example + * ~~~ + * this.enableControl(); + * ~~~ + */ + enableControl: function () { + this.disableControls = false; + return this; + }, + + /**@ + * #.disableControl + * @comp Multiway + * @sign public this .disableControl() + * + * Disable the component to listen to key events. + * + * @example + * ~~~ + * this.disableControl(); + * ~~~ + */ + + disableControl: function () { + this.disableControls = true; + return this; + }, + + speed: function (speed) { + for (var k in this._keyDirection) { + var keyCode = Crafty.keys[k] || k; + this._keys[keyCode] = { + x: Math.round(Math.cos(this._keyDirection[k] * (Math.PI / 180)) * 1000 * speed.x) / 1000, + y: Math.round(Math.sin(this._keyDirection[k] * (Math.PI / 180)) * 1000 * speed.y) / 1000 + }; + } + return this; + } +}); + +/**@ + * #Fourway + * @category Input + * Move an entity in four directions by using the + * arrow keys or `W`, `A`, `S`, `D`. + */ +Crafty.c("Fourway", { + + init: function () { + this.requires("Multiway"); + }, + + /**@ + * #.fourway + * @comp Fourway + * @sign public this .fourway(Number speed) + * @param speed - Amount of pixels to move the entity whilst a key is down + * Constructor to initialize the speed. Component will listen for key events and move the entity appropriately. + * This includes `Up Arrow`, `Right Arrow`, `Down Arrow`, `Left Arrow` as well as `W`, `A`, `S`, `D`. + * + * When direction changes a NewDirection event is triggered with an object detailing the new direction: {x: x_movement, y: y_movement} + * When entity has moved on either x- or y-axis a Moved event is triggered with an object specifying the old position {x: old_x, y: old_y} + * + * The key presses will move the entity in that direction by the speed passed in the argument. + * + * @see Multiway + */ + fourway: function (speed) { + this.multiway(speed, { + UP_ARROW: -90, + DOWN_ARROW: 90, + RIGHT_ARROW: 0, + LEFT_ARROW: 180, + W: -90, + S: 90, + D: 0, + A: 180, + Z: -90, + Q: 180 + }); + + return this; + } +}); + +/**@ + * #Twoway + * @category Input + * Move an entity left or right using the arrow keys or `D` and `A` and jump using up arrow or `W`. + * + * When direction changes a NewDirection event is triggered with an object detailing the new direction: {x: x_movement, y: y_movement}. This is consistent with Fourway and Multiway components. + * When entity has moved on x-axis a Moved event is triggered with an object specifying the old position {x: old_x, y: old_y} + */ +Crafty.c("Twoway", { + _speed: 3, + _up: false, + + init: function () { + this.requires("Fourway, Keyboard"); + }, + + /**@ + * #.twoway + * @comp Twoway + * @sign public this .twoway(Number speed[, Number jump]) + * @param speed - Amount of pixels to move left or right + * @param jump - Vertical jump speed + * + * Constructor to initialize the speed and power of jump. Component will + * listen for key events and move the entity appropriately. This includes + * ~~~ + * `Up Arrow`, `Right Arrow`, `Left Arrow` as well as W, A, D. Used with the + * `gravity` component to simulate jumping. + * ~~~ + * + * The key presses will move the entity in that direction by the speed passed in + * the argument. Pressing the `Up Arrow` or `W` will cause the entity to jump. + * + * @see Gravity, Fourway + */ + twoway: function (speed, jump) { + + this.multiway(speed, { + RIGHT_ARROW: 0, + LEFT_ARROW: 180, + D: 0, + A: 180, + Q: 180 + }); + + if (speed) this._speed = speed; + if (arguments.length < 2){ + this._jumpSpeed = this._speed * 2; + } else{ + this._jumpSpeed = jump; + } + + this.bind("EnterFrame", function () { + if (this.disableControls) return; + if (this._up) { + this.y -= this._jumpSpeed; + this._falling = true; + } + }).bind("KeyDown", function (e) { + if (e.key === Crafty.keys.UP_ARROW || e.key === Crafty.keys.W || e.key === Crafty.keys.Z) + this._up = true; + }); + + return this; + } +}); + +},{"./core.js":9}],9:[function(require,module,exports){ +var version = require('./version'); + +/**@ + * #Crafty + * @category Core + * Select a set of or single entities by components or an entity's ID. + * + * Crafty uses syntax similar to jQuery by having a selector engine to select entities by their components. + * + * If there is more than one match, the return value is an Array-like object listing the ID numbers of each matching entity. If there is exactly one match, the entity itself is returned. If you're not sure how many matches to expect, check the number of matches via Crafty(...).length. Alternatively, use Crafty(...).each(...), which works in all cases. + * + * @example + * ~~~ + * Crafty("MyComponent") + * Crafty("Hello 2D Component") + * Crafty("Hello, 2D, Component") + * ~~~ + * + * The first selector will return all entities that have the component `MyComponent`. The second will return all entities that have `Hello` and `2D` and `Component` whereas the last will return all entities that have at least one of those components (or). + * + * ~~~ + * Crafty("*") + * ~~~ + * Passing `*` will select all entities. + * + * ~~~ + * Crafty(1) + * ~~~ + * Passing an integer will select the entity with that `ID`. + */ + +var Crafty = function (selector) { + return new Crafty.fn.init(selector); +}, + // Internal variables + GUID, frame, components, entities, handlers, onloads, + noSetter, slice, rlist, rspace, milliSecPerFrame; + + + initState = function () { + GUID = 1; //GUID for entity IDs + + components = {}; //map of components and their functions + entities = {}; //map of entities and their data + handlers = {}; //global event handlers + onloads = []; //temporary storage of onload handlers + + slice = Array.prototype.slice; + rlist = /\s*,\s*/; + rspace = /\s+/; + }; + +initState(); + +/**@ + * #Crafty Core + * @category Core + * @trigger NewEntityName - After setting new name for entity - String - entity name + * @trigger NewComponent - when a new component is added to the entity - String - Component + * @trigger RemoveComponent - when a component is removed from the entity - String - Component + * @trigger Remove - when the entity is removed by calling .destroy() + * + * Set of methods added to every single entity. + */ +Crafty.fn = Crafty.prototype = { + + init: function (selector) { + //select entities by component + if (typeof selector === "string") { + var elem = 0, //index elements + e, //entity forEach + current, + and = false, //flags for multiple + or = false, + del, + comps, + score, + i, l; + + if (selector === '*') { + i = 0; + for (e in entities) { + // entities is something like {2:entity2, 3:entity3, 11:entity11, ...} + // The for...in loop sets e to "2", "3", "11", ... i.e. all + // the entity ID numbers. e is a string, so +e converts to number type. + this[i] = +e; + i++; + } + this.length = i; + // if there's only one entity, return the actual entity + if (i === 1) { + return entities[this[0]]; + } + return this; + } + + //multiple components OR + if (selector.indexOf(',') !== -1) { + or = true; + del = rlist; + //deal with multiple components AND + } else if (selector.indexOf(' ') !== -1) { + and = true; + del = rspace; + } + + //loop over entities + for (e in entities) { + if (!entities.hasOwnProperty(e)) continue; //skip + current = entities[e]; + + if (and || or) { //multiple components + comps = selector.split(del); + i = 0; + l = comps.length; + score = 0; + + for (; i < l; i++) //loop over components + if (current.__c[comps[i]]) score++; //if component exists add to score + + //if anded comps and has all OR ored comps and at least 1 + if (and && score === l || or && score > 0) this[elem++] = +e; + + } else if (current.__c[selector]) this[elem++] = +e; //convert to int + } + + //extend all common components + if (elem > 0 && !and && !or) this.extend(components[selector]); + if (comps && and) + for (i = 0; i < l; i++) this.extend(components[comps[i]]); + + this.length = elem; //length is the last index (already incremented) + + // if there's only one entity, return the actual entity + if (elem === 1) { + return entities[this[elem - 1]]; + } + + } else { //Select a specific entity + + if (!selector) { //nothin passed creates God entity + selector = 0; + if (!(selector in entities)) entities[selector] = this; + } + + //if not exists, return undefined + if (!(selector in entities)) { + this.length = 0; + return this; + } + + this[0] = selector; + this.length = 1; + + //update from the cache + if (!this.__c) this.__c = {}; + + //update to the cache if NULL + if (!entities[selector]) entities[selector] = this; + return entities[selector]; //return the cached selector + } + + return this; + }, + + /**@ + * #.setName + * @comp Crafty Core + * @sign public this .setName(String name) + * @param name - A human readable name for debugging purposes. + * + * @example + * ~~~ + * this.setName("Player"); + * ~~~ + */ + setName: function (name) { + var entityName = String(name); + + this._entityName = entityName; + + this.trigger("NewEntityName", entityName); + return this; + }, + + /**@ + * #.addComponent + * @comp Crafty Core + * @sign public this .addComponent(String componentList) + * @param componentList - A string of components to add separated by a comma `,` + * @sign public this .addComponent(String Component1[, .., String ComponentN]) + * @param Component# - Component ID to add. + * Adds a component to the selected entities or entity. + * + * Components are used to extend the functionality of entities. + * This means it will copy properties and assign methods to + * augment the functionality of the entity. + * + * For adding multiple components, you can either pass a string with + * all the component names (separated by commas), or pass each component name as + * an argument. + * + * If the component has a function named `init` it will be called. + * + * If the entity already has the component, the component is skipped (nothing happens). + * + * @example + * ~~~ + * this.addComponent("2D, Canvas"); + * this.addComponent("2D", "Canvas"); + * ~~~ + */ + addComponent: function (id) { + var uninit = [], + c = 0, + ul, //array of components to init + i = 0, + l, comps, comp; + + //add multiple arguments + if (arguments.length > 1) { + l = arguments.length; + for (; i < l; i++) { + uninit.push(arguments[i]); + } + //split components if contains comma + } else if (id.indexOf(',') !== -1) { + comps = id.split(rlist); + l = comps.length; + for (; i < l; i++) { + uninit.push(comps[i]); + } + //single component passed + } else { + uninit.push(id); + } + + //extend the components + ul = uninit.length; + for (; c < ul; c++) { + if (this.__c[uninit[c]] === true) + continue; + this.__c[uninit[c]] = true; + comp = components[uninit[c]]; + this.extend(comp); + //if constructor, call it + if (comp && "init" in comp) { + comp.init.call(this); + } + } + + this.trigger("NewComponent", uninit); + return this; + }, + + /**@ + * #.toggleComponent + * @comp Crafty Core + * @sign public this .toggleComponent(String ComponentList) + * @param ComponentList - A string of components to add or remove separated by a comma `,` + * @sign public this .toggleComponent(String Component1[, .., String componentN]) + * @param Component# - Component ID to add or remove. + * Add or Remove Components from an entity. + * + * @example + * ~~~ + * var e = Crafty.e("2D,DOM,Test"); + * e.toggleComponent("Test,Test2"); //Remove Test, add Test2 + * e.toggleComponent("Test,Test2"); //Add Test, remove Test2 + * ~~~ + * + * ~~~ + * var e = Crafty.e("2D,DOM,Test"); + * e.toggleComponent("Test","Test2"); //Remove Test, add Test2 + * e.toggleComponent("Test","Test2"); //Add Test, remove Test2 + * e.toggleComponent("Test"); //Remove Test + * ~~~ + */ + toggleComponent: function (toggle) { + var i = 0, + l, comps; + if (arguments.length > 1) { + l = arguments.length; + + for (; i < l; i++) { + if (this.has(arguments[i])) { + this.removeComponent(arguments[i]); + } else { + this.addComponent(arguments[i]); + } + } + //split components if contains comma + } else if (toggle.indexOf(',') !== -1) { + comps = toggle.split(rlist); + l = comps.length; + for (; i < l; i++) { + if (this.has(comps[i])) { + this.removeComponent(comps[i]); + } else { + this.addComponent(comps[i]); + } + } + + //single component passed + } else { + if (this.has(toggle)) { + this.removeComponent(toggle); + } else { + this.addComponent(toggle); + } + } + + return this; + }, + + /**@ + * #.requires + * @comp Crafty Core + * @sign public this .requires(String componentList) + * @param componentList - List of components that must be added + * + * Makes sure the entity has the components listed. If the entity does not + * have the component, it will add it. + * + * (In the current version of Crafty, this function behaves exactly the same + * as `addComponent`. By convention, developers have used `requires` for + * component dependencies -- i.e. to indicate specifically that one component + * will only work properly if another component is present -- and used + * `addComponent` in all other situations.) + * + * @see .addComponent + */ + requires: function (list) { + return this.addComponent(list); + }, + + /**@ + * #.removeComponent + * @comp Crafty Core + * @sign public this .removeComponent(String Component[, soft]) + * @param component - Component to remove + * @param soft - Whether to soft remove it (defaults to `true`) + * + * Removes a component from an entity. A soft remove (the default) will only + * refrain `.has()` from returning true. Hard will remove all + * associated properties and methods. + * + * @example + * ~~~ + * var e = Crafty.e("2D,DOM,Test"); + * e.removeComponent("Test"); //Soft remove Test component + * e.removeComponent("Test", false); //Hard remove Test component + * ~~~ + */ + removeComponent: function (id, soft) { + var comp = components[id]; + this.trigger("RemoveComponent", id); + if (comp && "remove" in comp) { + comp.remove.call(this, false); + } + if (soft === false && comp) { + for (var prop in comp) { + delete this[prop]; + } + } + delete this.__c[id]; + + + return this; + }, + + /**@ + * #.getId + * @comp Crafty Core + * @sign public Number .getId(void) + * Returns the ID of this entity. + * + * For better performance, simply use the this[0] property. + * + * @example + * Finding out the `ID` of an entity can be done by returning the property `0`. + * ~~~ + * var ent = Crafty.e("2D"); + * ent[0]; //ID + * ent.getId(); //also ID + * ~~~ + */ + getId: function () { + return this[0]; + }, + + /**@ + * #.has + * @comp Crafty Core + * @sign public Boolean .has(String component) + * Returns `true` or `false` depending on if the + * entity has the given component. + * + * For better performance, simply use the `.__c` object + * which will be `true` if the entity has the component or + * will not exist (or be `false`). + */ + has: function (id) { + return !!this.__c[id]; + }, + + /**@ + * #.attr + * @comp Crafty Core + * @sign public this .attr(String property, * value) + * @param property - Property of the entity to modify + * @param value - Value to set the property to + * @sign public this .attr(Object map) + * @param map - Object where the key is the property to modify and the value as the property value + * @trigger Change - when properties change - {key: value} + * + * Use this method to set any property of the entity. + * + * @example + * ~~~ + * this.attr({key: "value", prop: 5}); + * this.key; //value + * this.prop; //5 + * + * this.attr("key", "newvalue"); + * this.key; //newvalue + * ~~~ + */ + attr: function (key, value) { + if (arguments.length === 1) { + //if just the key, return the value + if (typeof key === "string") { + return this[key]; + } + + //extend if object + this.extend(key); + this.trigger("Change", key); //trigger change event + return this; + } + //if key value pair + this[key] = value; + + var change = {}; + change[key] = value; + this.trigger("Change", change); //trigger change event + return this; + }, + + /**@ + * #.toArray + * @comp Crafty Core + * @sign public this .toArray(void) + * + * This method will simply return the found entities as an array. + */ + toArray: function () { + return slice.call(this, 0); + }, + + /**@ + * #.timeout + * @comp Crafty Core + * @sign public this .timeout(Function callback, Number delay) + * @param callback - Method to execute after given amount of milliseconds + * @param delay - Amount of milliseconds to execute the method + * + * The delay method will execute a function after a given amount of time in milliseconds. + * + * Essentially a wrapper for `setTimeout`. + * + * @example + * Destroy itself after 100 milliseconds + * ~~~ + * this.timeout(function() { + this.destroy(); + * }, 100); + * ~~~ + */ + timeout: function (callback, duration) { + this.each(function () { + var self = this; + setTimeout(function () { + callback.call(self); + }, duration); + }); + return this; + }, + + /**@ + * #.bind + * @comp Crafty Core + * @sign public this .bind(String eventName, Function callback) + * @param eventName - Name of the event to bind to + * @param callback - Method to execute when the event is triggered + * Attach the current entity (or entities) to listen for an event. + * + * Callback will be invoked when an event with the event name passed + * is triggered. Depending on the event, some data may be passed + * via an argument to the callback function. + * + * The first argument is the event name (can be anything) whilst the + * second argument is the callback. If the event has data, the + * callback should have an argument. + * + * Events are arbitrary and provide communication between components. + * You can trigger or bind an event even if it doesn't exist yet. + * + * Unlike DOM events, Crafty events are exectued synchronously. + * + * @example + * ~~~ + * this.attr("triggers", 0); //set a trigger count + * this.bind("myevent", function() { + * this.triggers++; //whenever myevent is triggered, increment + * }); + * this.bind("EnterFrame", function() { + * this.trigger("myevent"); //trigger myevent on every frame + * }); + * ~~~ + * + * @see .trigger, .unbind + */ + bind: function (event, callback) { + + // (To learn how the handlers object works, see inline comment at Crafty.bind) + + //optimization for 1 entity + if (this.length === 1) { + if (!handlers[event]) handlers[event] = {}; + var h = handlers[event]; + + if (!h[this[0]]) h[this[0]] = []; //init handler array for entity + h[this[0]].push(callback); //add current callback + return this; + } + + this.each(function () { + //init event collection + if (!handlers[event]) handlers[event] = {}; + var h = handlers[event]; + + if (!h[this[0]]) h[this[0]] = []; //init handler array for entity + h[this[0]].push(callback); //add current callback + }); + return this; + }, + + /**@ + * #.uniqueBind + * @comp Crafty Core + * @sign public Number .uniqueBind(String eventName, Function callback) + * @param eventName - Name of the event to bind to + * @param callback - Method to execute upon event triggered + * @returns ID of the current callback used to unbind + * + * Works like Crafty.bind, but prevents a callback from being bound multiple times. + * + * @see .bind + */ + uniqueBind: function (event, callback) { + this.unbind(event, callback); + this.bind(event, callback); + + }, + + /**@ + * #.one + * @comp Crafty Core + * @sign public Number one(String eventName, Function callback) + * @param eventName - Name of the event to bind to + * @param callback - Method to execute upon event triggered + * @returns ID of the current callback used to unbind + * + * Works like Crafty.bind, but will be unbound once the event triggers. + * + * @see .bind + */ + one: function (event, callback) { + var self = this; + var oneHandler = function (data) { + callback.call(self, data); + self.unbind(event, oneHandler); + }; + return self.bind(event, oneHandler); + + }, + + /**@ + * #.unbind + * @comp Crafty Core + * @sign public this .unbind(String eventName[, Function callback]) + * @param eventName - Name of the event to unbind + * @param callback - Function to unbind + * Removes binding with an event from current entity. + * + * Passing an event name will remove all events bound to + * that event. Passing a reference to the callback will + * unbind only that callback. + * @see .bind, .trigger + */ + unbind: function (event, callback) { + // (To learn how the handlers object works, see inline comment at Crafty.bind) + this.each(function () { + var hdl = handlers[event], + i = 0, + l, current; + //if no events, cancel + if (hdl && hdl[this[0]]) l = hdl[this[0]].length; + else return this; + + //if no function, delete all + if (!callback) { + delete hdl[this[0]]; + return this; + } + //look for a match if the function is passed + for (; i < l; i++) { + current = hdl[this[0]]; + if (current[i] == callback) { + delete current[i]; + } + } + }); + + return this; + }, + + /**@ + * #.trigger + * @comp Crafty Core + * @sign public this .trigger(String eventName[, Object data]) + * @param eventName - Event to trigger + * @param data - Arbitrary data that will be passed into every callback as an argument + * Trigger an event with arbitrary data. Will invoke all callbacks with + * the context (value of `this`) of the current entity object. + * + * *Note: This will only execute callbacks within the current entity, no other entity.* + * + * The first argument is the event name to trigger and the optional + * second argument is the arbitrary event data. This can be absolutely anything. + * + * Unlike DOM events, Crafty events are exectued synchronously. + */ + trigger: function (event, data) { + // (To learn how the handlers object works, see inline comment at Crafty.bind) + if (this.length === 1) { + //find the handlers assigned to the event and entity + if (handlers[event] && handlers[event][this[0]]) { + var callbacks = handlers[event][this[0]], + i; + for (i = 0; i < callbacks.length; i++) { + if (typeof callbacks[i] === "undefined") { + callbacks.splice(i, 1); + i--; + } else { + callbacks[i].call(this, data); + } + } + } + return this; + } + + this.each(function () { + //find the handlers assigned to the event and entity + if (handlers[event] && handlers[event][this[0]]) { + var callbacks = handlers[event][this[0]], + i; + for (i = 0; i < callbacks.length; i++) { + if (typeof callbacks[i] === "undefined") { + callbacks.splice(i, 1); + i--; + } else { + callbacks[i].call(this, data); + } + } + } + }); + return this; + }, + + /**@ + * #.each + * @comp Crafty Core + * @sign public this .each(Function method) + * @param method - Method to call on each iteration + * Iterates over found entities, calling a function for every entity. + * + * The function will be called for every entity and will pass the index + * in the iteration as an argument. The context (value of `this`) of the + * function will be the current entity in the iteration. + * + * @example + * Destroy every second 2D entity + * ~~~ + * Crafty("2D").each(function(i) { + * if(i % 2 === 0) { + * this.destroy(); + * } + * }); + * ~~~ + */ + each: function (func) { + var i = 0, + l = this.length; + for (; i < l; i++) { + //skip if not exists + if (!entities[this[i]]) continue; + func.call(entities[this[i]], i); + } + return this; + }, + + /**@ + * #.clone + * @comp Crafty Core + * @sign public Entity .clone(void) + * @returns Cloned entity of the current entity + * + * Method will create another entity with the exact same + * properties, components and methods as the current entity. + */ + clone: function () { + var comps = this.__c, + comp, + prop, + clone = Crafty.e(); + + for (comp in comps) { + clone.addComponent(comp); + } + for (prop in this) { + if (prop != "0" && prop != "_global" && prop != "_changed" && typeof this[prop] != "function" && typeof this[prop] != "object") { + clone[prop] = this[prop]; + } + } + + return clone; + }, + + /**@ + * #.setter + * @comp Crafty Core + * @sign public this .setter(String property, Function callback) + * @param property - Property to watch for modification + * @param callback - Method to execute if the property is modified + * Will watch a property waiting for modification and will then invoke the + * given callback when attempting to modify. + * + * *Note: Support in IE<9 is slightly different. The method will be executed + * after the property has been set* + */ + setter: function (prop, callback) { + if (Crafty.support.setter) { + this.__defineSetter__(prop, callback); + } else if (Crafty.support.defineProperty) { + Object.defineProperty(this, prop, { + set: callback, + configurable: true + }); + } else { + noSetter.push({ + prop: prop, + obj: this, + fn: callback + }); + } + return this; + }, + + /**@ + * #.destroy + * @comp Crafty Core + * @sign public this .destroy(void) + * Will remove all event listeners and delete all properties as well as removing from the stage + */ + destroy: function () { + //remove all event handlers, delete from entities + this.each(function () { + var comp; + this.trigger("Remove"); + for (var compName in this.__c) { + comp = components[compName]; + if (comp && "remove" in comp) + comp.remove.call(this, true); + } + for (var e in handlers) { + this.unbind(e); + } + delete entities[this[0]]; + }); + } +}; + +//give the init instances the Crafty prototype +Crafty.fn.init.prototype = Crafty.fn; + +/** + * Extension method to extend the namespace and + * selector instances + */ +Crafty.extend = Crafty.fn.extend = function (obj) { + var target = this, + key; + + //don't bother with nulls + if (!obj) return target; + + for (key in obj) { + if (target === obj[key]) continue; //handle circular reference + target[key] = obj[key]; + } + + return target; +}; + +/**@ + * #Crafty.extend + * @category Core + * Used to extend the Crafty namespace. + */ +Crafty.extend({ + /**@ + * #Crafty.init + * @category Core + * @trigger Load - Just after the viewport is initialised. Before the EnterFrame loops is started + * @sign public this Crafty.init([Number width, Number height, String stage_elem]) + * @sign public this Crafty.init([Number width, Number height, HTMLElement stage_elem]) + * @param Number width - Width of the stage + * @param Number height - Height of the stage + * @param String or HTMLElement stage_elem - the element to use for the stage + * + * Sets the element to use as the stage, creating it if necessary. By default a div with id 'cr-stage' is used, but if the 'stage_elem' argument is provided that will be used instead. (see `Crafty.viewport.init`) + * + * Starts the `EnterFrame` interval. This will call the `EnterFrame` event for every frame. + * + * Can pass width and height values for the stage otherwise will default to window size (see `Crafty.DOM.window`). + * + * All `Load` events will be executed. + * + * Uses `requestAnimationFrame` to sync the drawing with the browser but will default to `setInterval` if the browser does not support it. + * @see Crafty.stop, Crafty.viewport + */ + init: function (w, h, stage_elem) { + Crafty.viewport.init(w, h, stage_elem); + + //call all arbitrary functions attached to onload + this.trigger("Load"); + this.timer.init(); + + return this; + }, + + /**@ + * #Crafty.getVersion + * @category Core + * @sign public String Crafty.getVersion() + * @returns Current version of Crafty as a string + * + * Return current version of crafty + * + * @example + * ~~~ + * Crafty.getVersion(); //'0.5.2' + * ~~~ + */ + getVersion: function () { + return version; + }, + + /**@ + * #Crafty.stop + * @category Core + * @trigger CraftyStop - when the game is stopped + * @sign public this Crafty.stop([bool clearState]) + * @param clearState - if true the stage and all game state is cleared. + * + * Stops the EnterFrame interval and removes the stage element. + * + * To restart, use `Crafty.init()`. + * @see Crafty.init + */ + stop: function (clearState) { + this.timer.stop(); + if (clearState) { + Crafty.audio.remove(); + if (Crafty.stage && Crafty.stage.elem.parentNode) { + var newCrStage = document.createElement('div'); + newCrStage.id = Crafty.stage.elem.id; + Crafty.stage.elem.parentNode.replaceChild(newCrStage, Crafty.stage.elem); + } + initState(); + } + + Crafty.trigger("CraftyStop"); + + return this; + }, + + /**@ + * #Crafty.pause + * @category Core + * @trigger Pause - when the game is paused + * @trigger Unpause - when the game is unpaused + * @sign public this Crafty.pause(void) + * + * Pauses the game by stopping the EnterFrame event from firing. If the game is already paused it is unpaused. + * You can pass a boolean parameter if you want to pause or unpause mo matter what the current state is. + * Modern browsers pauses the game when the page is not visible to the user. If you want the Pause event + * to be triggered when that happens you can enable autoPause in `Crafty.settings`. + * + * @example + * Have an entity pause the game when it is clicked. + * ~~~ + * button.bind("click", function() { + * Crafty.pause(); + * }); + * ~~~ + */ + pause: function (toggle) { + if (arguments.length === 1 ? toggle : !this._paused) { + this.trigger('Pause'); + this._paused = true; + setTimeout(function () { + Crafty.timer.stop(); + }, 0); + Crafty.keydown = {}; + } else { + this.trigger('Unpause'); + this._paused = false; + setTimeout(function () { + Crafty.timer.init(); + }, 0); + } + return this; + }, + + /**@ + * #Crafty.isPaused + * @category Core + * @sign public this Crafty.isPaused() + * + * Check whether the game is already paused or not. + * + * @example + * ~~~ + * Crafty.isPaused(); + * ~~~ + */ + isPaused: function () { + return this._paused; + }, + + /**@ + * #Crafty.timer + * @category Game Loop + * Handles game ticks + */ + timer: (function () { + /* + * `window.requestAnimationFrame` or its variants is called for animation. + * `.requestID` keeps a record of the return value previous `window.requestAnimationFrame` call. + * This is an internal variable. Used to stop frame. + */ + var tick, requestID; + + // Internal variables used to control the game loop. Use Crafty.timer.steptype() to set these. + var mode = "fixed", + maxFramesPerStep = 5, + maxTimestep = 40; + + // variables used by the game loop to track state + var endTime = 0, + timeSlip = 0, + gameTime, + frame = 0; + + // Controls the target rate of fixed mode loop. Set these with the Crafty.timer.FPS function + var FPS = 50, + milliSecPerFrame = 1000 / FPS; + + + + + return { + init: function () { + // When first called, set the gametime one frame before now! + if (typeof gameTime === "undefined") + gameTime = (new Date().getTime()) - milliSecPerFrame; + var onFrame = window.requestAnimationFrame || + window.webkitRequestAnimationFrame || + window.mozRequestAnimationFrame || + window.oRequestAnimationFrame || + window.msRequestAnimationFrame || + null; + + if (onFrame) { + tick = function () { + Crafty.timer.step(); + requestID = onFrame(tick); + //console.log(requestID + ', ' + frame) + }; + + tick(); + } else { + tick = setInterval(function () { + Crafty.timer.step(); + }, 1000 / FPS); + } + }, + + stop: function () { + Crafty.trigger("CraftyStopTimer"); + + if (typeof tick === "number") clearInterval(tick); + + var onFrame = window.cancelRequestAnimationFrame || + window.webkitCancelRequestAnimationFrame || + window.mozCancelRequestAnimationFrame || + window.oCancelRequestAnimationFrame || + window.msCancelRequestAnimationFrame || + null; + + if (onFrame) onFrame(requestID); + tick = null; + }, + + + /**@ + * #Crafty.timer.steptype + * @comp Crafty.timer + * Can be called to set the type of timestep the game loop uses + * @sign public void Crafty.timer.steptype(mode [, maxTimeStep]) + * @param mode - the type of time loop. Allowed values are "fixed", "semifixed", and "variable". Crafty defaults to "fixed". + * @param mode - For "fixed", sets the max number of frames per step. For "variable" and "semifixed", sets the maximum time step allowed. + * + * * In "fixed" mode, each frame is sent the same value of `dt`, and to achieve the target game speed, mulitiple frame events are triggered before each render. + * * In "variable" mode, there is only one frame triggered per render. This recieves a value of `dt` equal to the actual elapsed time since the last frame. + * * In "semifixed" mode, multiple frames per render are processed, and the total time since the last frame is divided evenly between them. + * + */ + + steptype: function (newmode, option) { + if (newmode === "variable" || newmode === "semifixed") { + mode = newmode; + if (option) + maxTimestep = option; + + } else if (newmode === "fixed") { + mode = "fixed"; + if (option) + maxFramesPerStep = option; + } else { + throw "Invalid step type specified"; + } + + + }, + + /**@ + * #Crafty.timer.step + * @comp Crafty.timer + * @sign public void Crafty.timer.step() + * @trigger EnterFrame - Triggered on each frame. Passes the frame number, and the amount of time since the last frame. If the time is greater than maxTimestep, that will be used instead. (The default value of maxTimestep is 50 ms.) - { frame: Number, dt:Number } + * @trigger RenderScene - Triggered every time a scene should be rendered + * @trigger MeasureWaitTime - Triggered at the beginning of each step after the first. Passes the time the game loop waited between steps. - Number + * @trigger MeasureFrameTime - Triggered after each step. Passes the time it took to advance one frame. - Number + * @trigger MeasureRenderTime - Triggered after each render. Passes the time it took to render the scene - Number + * Advances the game by triggering `EnterFrame` and `RenderScene` + */ + step: function () { + var drawTimeStart, dt, lastFrameTime, loops = 0; + + currentTime = new Date().getTime(); + if (endTime > 0) + Crafty.trigger("MeasureWaitTime", currentTime - endTime); + + // If we're currently ahead of the current time, we need to wait until we're not! + if (gameTime + timeSlip >= currentTime) { + endTime = currentTime; + return; + } + + var netTimeStep = currentTime - (gameTime + timeSlip); + // We try to keep up with the target FPS by processing multiple frames per render + // If we're hopelessly behind, stop trying to catch up. + if (netTimeStep > milliSecPerFrame * 20) { + //gameTime = currentTime - milliSecPerFrame; + timeSlip += netTimeStep - milliSecPerFrame; + netTimeStep = milliSecPerFrame; + } + + // Set up how time is incremented + if (mode === "fixed") { + loops = Math.ceil(netTimeStep / milliSecPerFrame); + // maxFramesPerStep adjusts how willing we are to delay drawing in order to keep at the target FPS + loops = Math.min(loops, maxFramesPerStep); + dt = milliSecPerFrame; + } else if (mode === "variable") { + loops = 1; + dt = netTimeStep; + // maxTimestep is the maximum time to be processed in a frame. (Large dt => unstable physics) + dt = Math.min(dt, maxTimestep); + } else if (mode === "semifixed") { + loops = Math.ceil(netTimeStep / maxTimestep); + dt = netTimeStep / loops; + } + + // Process frames, incrementing the game clock with each frame. + // dt is determined by the mode + for (var i = 0; i < loops; i++) { + lastFrameTime = currentTime; + // Everything that changes over time hooks into this event + Crafty.trigger("EnterFrame", { + frame: frame++, + dt: dt, + gameTime: gameTime + }); + gameTime += dt; + currentTime = new Date().getTime(); + Crafty.trigger("MeasureFrameTime", currentTime - lastFrameTime); + } + + //If any frames were processed, render the results + if (loops > 0) { + drawTimeStart = currentTime; + Crafty.trigger("RenderScene"); + // Post-render cleanup opportunity + Crafty.trigger("PostRender"); + currentTime = new Date().getTime(); + Crafty.trigger("MeasureRenderTime", currentTime - drawTimeStart); + } + + endTime = currentTime; + }, + /**@ + * #Crafty.timer.FPS + * @comp Crafty.timer + * @sign public void Crafty.timer.FPS() + * Returns the target frames per second. This is not an actual frame rate. + * @sign public void Crafty.timer.FPS(Number value) + * @param value - the target rate + * Sets the target frames per second. This is not an actual frame rate. + * The default rate is 50. + */ + FPS: function (value) { + if (typeof value == "undefined") + return FPS; + else { + FPS = value; + milliSecPerFrame = 1000 / FPS; + } + }, + + /**@ + * #Crafty.timer.simulateFrames + * @comp Crafty.timer + * Advances the game state by a number of frames and draws the resulting stage at the end. Useful for tests and debugging. + * @sign public this Crafty.timer.simulateFrames(Number frames[, Number timestep]) + * @param frames - number of frames to simulate + * @param timestep - the duration to pass each frame. Defaults to milliSecPerFrame (20 ms) if not specified. + */ + simulateFrames: function (frames, timestep) { + if (typeof timestep === "undefined") + timestep = milliSecPerFrame; + while (frames-- > 0) { + Crafty.trigger("EnterFrame", { + frame: frame++, + dt: timestep + }); + } + Crafty.trigger("RenderScene"); + } + }; + })(), + + + /**@ + * #Crafty.e + * @category Core + * @trigger NewEntity - When the entity is created and all components are added - { id:Number } + * @sign public Entity Crafty.e(String componentList) + * @param componentList - List of components to assign to new entity + * @sign public Entity Crafty.e(String component1[, .., String componentN]) + * @param component# - Component to add + * + * Creates an entity. Any arguments will be applied in the same + * way `.addComponent()` is applied as a quick way to add components. + * + * Any component added will augment the functionality of + * the created entity by assigning the properties and methods from the component to the entity. + * + * @example + * ~~~ + * var myEntity = Crafty.e("2D, DOM, Color"); + * ~~~ + * + * @see Crafty.c + */ + e: function () { + var id = UID(), + craft; + + entities[id] = null; //register the space + entities[id] = craft = Crafty(id); + + if (arguments.length > 0) { + craft.addComponent.apply(craft, arguments); + } + craft.setName('Entity #' + id); //set default entity human readable name + craft.addComponent("obj"); //every entity automatically assumes obj + + Crafty.trigger("NewEntity", { + id: id + }); + + return craft; + }, + + /**@ + * #Crafty.c + * @category Core + * @sign public void Crafty.c(String name, Object component) + * @param name - Name of the component + * @param component - Object with the components properties and methods + * Creates a component where the first argument is the ID and the second + * is the object that will be inherited by entities. + * + * A couple of methods are treated specially. They are invoked in partiular contexts, and (in those contexts) cannot be overridden by other components. + * + * - `init` will be called when the component is added to an entity + * - `remove` will be called just before a component is removed, or before an entity is destroyed. It is passed a single boolean parameter that is `true` if the entity is being destroyed. + * + * In addition to these hardcoded special methods, there are some conventions for writing components. + * + * - Properties or methods that start with an underscore are considered private. + * - A method with the same name as the component is considered to be a constructor + * and is generally used when you need to pass configuration data to the component on a per entity basis. + * + * @example + * ~~~ + * Crafty.c("Annoying", { + * _message: "HiHi", + * init: function() { + * this.bind("EnterFrame", function() { alert(this.message); }); + * }, + * annoying: function(message) { this.message = message; } + * }); + * + * Crafty.e("Annoying").annoying("I'm an orange..."); + * ~~~ + * + * + * WARNING: + * + * in the example above the field _message is local to the entity. That is, if you create many entities with the Annoying component they can all have different values for _message. That is because it is a simple value, and simple values are copied by value. If however the field had been an object or array, the value would have been shared by all entities with the component because complex types are copied by reference in javascript. This is probably not what you want and the following example demonstrates how to work around it: + * + * ~~~ + * Crafty.c("MyComponent", { + * _iAmShared: { a: 3, b: 4 }, + * init: function() { + * this._iAmNotShared = { a: 3, b: 4 }; + * }, + * }); + * ~~~ + * + * @see Crafty.e + */ + c: function (compName, component) { + components[compName] = component; + }, + + /**@ + * #Crafty.trigger + * @category Core, Events + * @sign public void Crafty.trigger(String eventName, * data) + * @param eventName - Name of the event to trigger + * @param data - Arbitrary data to pass into the callback as an argument + * + * This method will trigger every single callback attached to the event name. This means + * every global event and every entity that has a callback. + * + * @see Crafty.bind + */ + trigger: function (event, data) { + + // (To learn how the handlers object works, see inline comment at Crafty.bind) + var hdl = handlers[event], + h, i, l, callbacks, context; + //loop over every object bound + for (h in hdl) { + + // Check whether h needs to be processed + if (!hdl.hasOwnProperty(h)) continue; + callbacks = hdl[h]; + if (!callbacks || callbacks.length === 0) continue; + + //if an entity, call with that context; else the global context + if (entities[h]) + context = Crafty(+h); + else + context = Crafty; + + //loop over every handler within object + for (i = 0; i < callbacks.length; i++) { + // Remove a callback if it has been deleted + if (typeof callbacks[i] === "undefined") { + callbacks.splice(i, 1); + i--; + } else + callbacks[i].call(context, data); + } + } + }, + + /**@ + * #Crafty.bind + * @category Core, Events + * @sign public Number bind(String eventName, Function callback) + * @param eventName - Name of the event to bind to + * @param callback - Method to execute upon event triggered + * @returns ID of the current callback used to unbind + * + * Binds to a global event. Method will be executed when `Crafty.trigger` is used + * with the event name. + * + * @see Crafty.trigger, Crafty.unbind + */ + bind: function (event, callback) { + + // Background: The structure of the global object "handlers" + // --------------------------------------------------------- + // Here is an example of what "handlers" can look like: + // handlers === + // { Move: {5:[fnA], 6:[fnB, fnC], global:[fnD]}, + // Change: {6:[fnE]} + // } + // In this example, when the 'Move' event is triggered on entity #6 (e.g. + // entity6.trigger('Move')), it causes the execution of fnB() and fnC(). When + // the Move event is triggered globally (i.e. Crafty.trigger('Move')), it + // will execute fnA, fnB, fnC, fnD. + // + // In this example, "this" is bound to entity #6 whenever fnB() is executed, and + // "this" is bound to Crafty whenever fnD() is executed. + // + // In other words, the structure of "handlers" is: + // + // handlers[event][entityID or 'global'] === (Array of callback functions) + + if (!handlers[event]) handlers[event] = {}; + var hdl = handlers[event]; + + if (!hdl.global) hdl.global = []; + return hdl.global.push(callback) - 1; + }, + + + /**@ + * #Crafty.uniqueBind + * @category Core, Events + * @sign public Number uniqueBind(String eventName, Function callback) + * @param eventName - Name of the event to bind to + * @param callback - Method to execute upon event triggered + * @returns ID of the current callback used to unbind + * + * Works like Crafty.bind, but prevents a callback from being bound multiple times. + * + * @see Crafty.bind + */ + uniqueBind: function (event, callback) { + this.unbind(event, callback); + this.bind(event, callback); + + }, + + /**@ + * #Crafty.one + * @category Core, Events + * @sign public Number one(String eventName, Function callback) + * @param eventName - Name of the event to bind to + * @param callback - Method to execute upon event triggered + * @returns ID of the current callback used to unbind + * + * Works like Crafty.bind, but will be unbound once the event triggers. + * + * @see Crafty.bind + */ + one: function (event, callback) { + var self = this; + var oneHandler = function (data) { + callback.call(self, data); + self.unbind(event, oneHandler); + }; + return self.bind(event, oneHandler); + + }, + + /**@ + * #Crafty.unbind + * @category Core, Events + * @sign public Boolean Crafty.unbind(String eventName, Function callback) + * @param eventName - Name of the event to unbind + * @param callback - Function to unbind + * @sign public Boolean Crafty.unbind(String eventName, Number callbackID) + * @param callbackID - ID of the callback + * @returns True or false depending on if a callback was unbound + * Unbind any event from any entity or global event. + * @example + * ~~~ + * var play_gameover_sound = function () {...}; + * Crafty.bind('GameOver', play_gameover_sound); + * ... + * Crafty.unbind('GameOver', play_gameover_sound); + * ~~~ + * + * The first line defines a callback function. The second line binds that + * function so that `Crafty.trigger('GameOver')` causes that function to + * run. The third line unbinds that function. + * + * ~~~ + * Crafty.unbind('GameOver'); + * ~~~ + * + * This unbinds ALL global callbacks for the event 'GameOver'. That + * includes all callbacks attached by `Crafty.bind('GameOver', ...)`, but + * none of the callbacks attached by `some_entity.bind('GameOver', ...)`. + */ + unbind: function (event, callback) { + // (To learn how the handlers object works, see inline comment at Crafty.bind) + var hdl = handlers[event], + i, l, global_callbacks, found_match; + + if (hdl === undefined || hdl.global === undefined || hdl.global.length === 0) { + return false; + } + + // If no callback was supplied, delete everything + if (arguments.length === 1) { + delete hdl.global; + return true; + } + + // loop over the globally-attached events + global_callbacks = hdl.global; + found_match = false; + for (i = 0, l = global_callbacks.length; i < l; i++) { + if (global_callbacks[i] === callback) { + found_match = true; + delete global_callbacks[i]; + } + } + return found_match; + }, + + /**@ + * #Crafty.frame + * @category Core + * @sign public Number Crafty.frame(void) + * Returns the current frame number + */ + frame: function () { + return frame; + }, + + components: function () { + return components; + }, + + isComp: function (comp) { + return comp in components; + }, + + debug: function (str) { + // access internal variables - handlers or entities + if (str === 'handlers') { + return handlers; + } + return entities; + }, + + /**@ + * #Crafty.settings + * @category Core + * Modify the inner workings of Crafty through the settings. + */ + settings: (function () { + var states = {}, + callbacks = {}; + + return { + /**@ + * #Crafty.settings.register + * @comp Crafty.settings + * @sign public void Crafty.settings.register(String settingName, Function callback) + * @param settingName - Name of the setting + * @param callback - Function to execute when use modifies setting + * + * Use this to register custom settings. Callback will be executed when `Crafty.settings.modify` is used. + * + * @see Crafty.settings.modify + */ + register: function (setting, callback) { + callbacks[setting] = callback; + }, + + /**@ + * #Crafty.settings.modify + * @comp Crafty.settings + * @sign public void Crafty.settings.modify(String settingName, * value) + * @param settingName - Name of the setting + * @param value - Value to set the setting to + * + * Modify settings through this method. + * + * @see Crafty.settings.register, Crafty.settings.get + */ + modify: function (setting, value) { + if (!callbacks[setting]) return; + callbacks[setting].call(states[setting], value); + states[setting] = value; + }, + + /**@ + * #Crafty.settings.get + * @comp Crafty.settings + * @sign public * Crafty.settings.get(String settingName) + * @param settingName - Name of the setting + * @returns Current value of the setting + * + * Returns the current value of the setting. + * + * @see Crafty.settings.register, Crafty.settings.get + */ + get: function (setting) { + return states[setting]; + } + }; + })(), + + clone: clone +}); + +/** + * Return a unique ID + */ + +function UID() { + var id = GUID++; + //if GUID is not unique + if (id in entities) { + return UID(); //recurse until it is unique + } + return id; +} + +/**@ + * #Crafty.clone + * @category Core + * @sign public Object .clone(Object obj) + * @param obj - an object + * + * Deep copy (a.k.a clone) of an object. + */ + +function clone(obj) { + if (obj === null || typeof (obj) != 'object') + return obj; + + var temp = obj.constructor(); // changed + + for (var key in obj) + temp[key] = clone(obj[key]); + return temp; +} + +Crafty.bind("Load", function () { + if (!Crafty.support.setter && Crafty.support.defineProperty) { + noSetter = []; + Crafty.bind("EnterFrame", function () { + var i = 0, + l = noSetter.length, + current; + for (; i < l; ++i) { + current = noSetter[i]; + if (current.obj[current.prop] !== current.obj['_' + current.prop]) { + current.fn.call(current.obj, current.obj[current.prop]); + } + } + }); + } +}); + + +// export Crafty +if (typeof define === 'function') { // AMD + define('crafty', [], function () { + return Crafty; + }); +} else if (typeof exports === 'object') { // CommonJS + module.exports = Crafty; +} + +window.Crafty = Crafty; + + +},{"./version":25}],10:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +Crafty.extend({ + /**@ + * #Crafty.device + * @category Misc + */ + device: { + _deviceOrientationCallback: false, + _deviceMotionCallback: false, + + /** + * The HTML5 DeviceOrientation event returns three pieces of data: + * * alpha the direction the device is facing according to the compass + * * beta the angle in degrees the device is tilted front-to-back + * * gamma the angle in degrees the device is tilted left-to-right. + * * The angles values increase as you tilt the device to the right or towards you. + * + * Since Firefox uses the MozOrientationEvent which returns similar data but + * using different parameters and a different measurement system, we want to + * normalize that before we pass it to our _deviceOrientationCallback function. + * + * @param eventData HTML5 DeviceOrientation event + */ + _normalizeDeviceOrientation: function (eventData) { + var data; + if (window.DeviceOrientationEvent) { + data = { + // gamma is the left-to-right tilt in degrees, where right is positive + 'tiltLR': eventData.gamma, + // beta is the front-to-back tilt in degrees, where front is positive + 'tiltFB': eventData.beta, + // alpha is the compass direction the device is facing in degrees + 'dir': eventData.alpha, + // deviceorientation does not provide this data + 'motUD': null + }; + } else if (window.OrientationEvent) { + data = { + // x is the left-to-right tilt from -1 to +1, so we need to convert to degrees + 'tiltLR': eventData.x * 90, + // y is the front-to-back tilt from -1 to +1, so we need to convert to degrees + // We also need to invert the value so tilting the device towards us (forward) + // results in a positive value. + 'tiltFB': eventData.y * -90, + // MozOrientation does not provide this data + 'dir': null, + // z is the vertical acceleration of the device + 'motUD': eventData.z + }; + } + + Crafty.device._deviceOrientationCallback(data); + }, + + /** + * @param eventData HTML5 DeviceMotion event + */ + _normalizeDeviceMotion: function (eventData) { + var acceleration = eventData.accelerationIncludingGravity, + facingUp = (acceleration.z > 0) ? +1 : -1; + + var data = { + // Grab the acceleration including gravity from the results + 'acceleration': acceleration, + 'rawAcceleration': "[" + Math.round(acceleration.x) + ", " + Math.round(acceleration.y) + ", " + Math.round(acceleration.z) + "]", + // Z is the acceleration in the Z axis, and if the device is facing up or down + 'facingUp': facingUp, + // Convert the value from acceleration to degrees acceleration.x|y is the + // acceleration according to gravity, we'll assume we're on Earth and divide + // by 9.81 (earth gravity) to get a percentage value, and then multiply that + // by 90 to convert to degrees. + 'tiltLR': Math.round(((acceleration.x) / 9.81) * -90), + 'tiltFB': Math.round(((acceleration.y + 9.81) / 9.81) * 90 * facingUp) + }; + + Crafty.device._deviceMotionCallback(data); + }, + + /**@ + * #Crafty.device.deviceOrientation + * @comp Crafty.device + * @sign public Crafty.device.deviceOrientation(Function callback) + * @param callback - Callback method executed once as soon as device orientation is change + * + * Do something with normalized device orientation data: + * ~~~ + * { + * 'tiltLR' : 'gamma the angle in degrees the device is tilted left-to-right.', + * 'tiltFB' : 'beta the angle in degrees the device is tilted front-to-back', + * 'dir' : 'alpha the direction the device is facing according to the compass', + * 'motUD' : 'The angles values increase as you tilt the device to the right or towards you.' + * } + * ~~~ + * + * @example + * ~~~ + * // Get DeviceOrientation event normalized data. + * Crafty.device.deviceOrientation(function(data){ + * console.log('data.tiltLR : '+Math.round(data.tiltLR)+', data.tiltFB : '+Math.round(data.tiltFB)+', data.dir : '+Math.round(data.dir)+', data.motUD : '+data.motUD+''); + * }); + * ~~~ + * + * See browser support at http://caniuse.com/#search=device orientation. + */ + deviceOrientation: function (func) { + this._deviceOrientationCallback = func; + if (Crafty.support.deviceorientation) { + if (window.DeviceOrientationEvent) { + // Listen for the deviceorientation event and handle DeviceOrientationEvent object + Crafty.addEvent(this, window, 'deviceorientation', this._normalizeDeviceOrientation); + } else if (window.OrientationEvent) { + // Listen for the MozOrientation event and handle OrientationData object + Crafty.addEvent(this, window, 'MozOrientation', this._normalizeDeviceOrientation); + } + } + }, + + /**@ + * #Crafty.device.deviceMotion + * @comp Crafty.device + * @sign public Crafty.device.deviceMotion(Function callback) + * @param callback - Callback method executed once as soon as device motion is change + * + * Do something with normalized device motion data: + * ~~~ + * { + * 'acceleration' : ' Grab the acceleration including gravity from the results', + * 'rawAcceleration' : 'Display the raw acceleration data', + * 'facingUp' : 'Z is the acceleration in the Z axis, and if the device is facing up or down', + * 'tiltLR' : 'Convert the value from acceleration to degrees. acceleration.x is the acceleration according to gravity, we'll assume we're on Earth and divide by 9.81 (earth gravity) to get a percentage value, and then multiply that by 90 to convert to degrees.', + * 'tiltFB' : 'Convert the value from acceleration to degrees.' + * } + * ~~~ + * + * @example + * ~~~ + * // Get DeviceMotion event normalized data. + * Crafty.device.deviceMotion(function(data){ + * console.log('data.moAccel : '+data.rawAcceleration+', data.moCalcTiltLR : '+Math.round(data.tiltLR)+', data.moCalcTiltFB : '+Math.round(data.tiltFB)+''); + * }); + * ~~~ + * + * See browser support at http://caniuse.com/#search=motion. + */ + deviceMotion: function (func) { + this._deviceMotionCallback = func; + if (Crafty.support.devicemotion) { + if (window.DeviceMotionEvent) { + // Listen for the devicemotion event and handle DeviceMotionEvent object + Crafty.addEvent(this, window, 'devicemotion', this._normalizeDeviceMotion); + } + } + } + } +}); + +},{"./core.js":9}],11:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +Crafty.extend({ + /**@ + * #Crafty.diamondIso + * @category 2D + * Place entities in a 45deg diamond isometric fashion. It is similar to isometric but has another grid locations + */ + diamondIso: { + _tile: { + width: 0, + height: 0, + r: 0 + }, + _map: { + width: 0, + height: 0, + x: 0, + y: 0 + }, + + _origin: { + x: 0, + y: 0 + }, + /**@ + * #Crafty.diamondIso.init + * @comp Crafty.diamondIso + * @sign public this Crafty.diamondIso.init(Number tileWidth,Number tileHeight,Number mapWidth,Number mapHeight) + * @param tileWidth - The size of base tile width in Pixel + * @param tileHeight - The size of base tile height in Pixel + * @param mapWidth - The width of whole map in Tiles + * @param mapHeight - The height of whole map in Tiles + * + * Method used to initialize the size of the isometric placement. + * Recommended to use a size alues in the power of `2` (128, 64 or 32). + * This makes it easy to calculate positions and implement zooming. + * + * @example + * ~~~ + * var iso = Crafty.diamondIso.init(64,128,20,20); + * ~~~ + * + * @see Crafty.diamondIso.place + */ + init: function (tw, th, mw, mh) { + this._tile.width = parseInt(tw, 10); + this._tile.height = parseInt(th, 10) || parseInt(tw, 10) / 2; + this._tile.r = this._tile.width / this._tile.height; + + this._map.width = parseInt(mw, 10); + this._map.height = parseInt(mh, 10) || parseInt(mw, 10); + + this._origin.x = this._map.height * this._tile.width / 2; + return this; + }, + /**@ + * #Crafty.diamondIso.place + * @comp Crafty.diamondIso + * @sign public this Crafty.diamondIso.place(Entity tile,Number x, Number y, Number layer) + * @param x - The `x` position to place the tile + * @param y - The `y` position to place the tile + * @param layer - The `z` position to place the tile (calculated by y position * layer) + * @param tile - The entity that should be position in the isometric fashion + * + * Use this method to place an entity in an isometric grid. + * + * @example + * ~~~ + * var iso = Crafty.diamondIso.init(64,128,20,20); + * isos.place(Crafty.e('2D, DOM, Color').color('red').attr({w:128, h:128}),1,1,2); + * ~~~ + * + * @see Crafty.diamondIso.size + */ + place: function (obj, x, y, layer) { + var pos = this.pos2px(x, y); + if (!layer) layer = 1; + var marginX = 0, + marginY = 0; + if (obj.__margin !== undefined) { + marginX = obj.__margin[0]; + marginY = obj.__margin[1]; + } + + obj.x = pos.left + (marginX); + obj.y = (pos.top + marginY) - obj.h; + obj.z = (pos.top) * layer; + + + }, + centerAt: function (x, y) { + var pos = this.pos2px(x, y); + Crafty.viewport.x = -pos.left + Crafty.viewport.width / 2 - this._tile.width; + Crafty.viewport.y = -pos.top + Crafty.viewport.height / 2; + + }, + area: function (offset) { + if (!offset) offset = 0; + //calculate the corners + var vp = Crafty.viewport.rect(); + var ow = offset * this._tile.width; + var oh = offset * this._tile.height; + vp._x -= (this._tile.width / 2 + ow); + vp._y -= (this._tile.height / 2 + oh); + vp._w += (this._tile.width / 2 + ow); + vp._h += (this._tile.height / 2 + oh); + /* Crafty.viewport.x = -vp._x; + Crafty.viewport.y = -vp._y; + Crafty.viewport.width = vp._w; + Crafty.viewport.height = vp._h; */ + + var grid = []; + for (var y = vp._y, yl = (vp._y + vp._h); y < yl; y += this._tile.height / 2) { + for (var x = vp._x, xl = (vp._x + vp._w); x < xl; x += this._tile.width / 2) { + var row = this.px2pos(x, y); + grid.push([~~row.x, ~~row.y]); + } + } + return grid; + }, + pos2px: function (x, y) { + return { + left: ((x - y) * this._tile.width / 2 + this._origin.x), + top: ((x + y) * this._tile.height / 2) + }; + }, + px2pos: function (left, top) { + var x = (left - this._origin.x) / this._tile.r; + return { + x: ((top + x) / this._tile.height), + y: ((top - x) / this._tile.height) + }; + }, + + polygon: function (obj) { + + obj.requires("Collision"); + var marginX = 0, + marginY = 0; + if (obj.__margin !== undefined) { + marginX = obj.__margin[0]; + marginY = obj.__margin[1]; + } + var points = [ + [marginX - 0, obj.h - marginY - this._tile.height / 2], + [marginX - this._tile.width / 2, obj.h - marginY - 0], + [marginX - this._tile.width, obj.h - marginY - this._tile.height / 2], + [marginX - this._tile.width / 2, obj.h - marginY - this._tile.height] + ]; + var poly = new Crafty.polygon(points); + return poly; + + } + + } +}); +},{"./core.js":9}],12:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +/**@ + * #Color + * @category Graphics + * Draw a solid color for the entity + */ +Crafty.c("Color", { + _color: "", + ready: true, + + init: function () { + this.bind("Draw", function (e) { + if (e.type === "DOM") { + e.style.backgroundColor = this._color; + e.style.lineHeight = 0; + } else if (e.type === "canvas") { + if (this._color) e.ctx.fillStyle = this._color; + e.ctx.fillRect(e.pos._x, e.pos._y, e.pos._w, e.pos._h); + } + }); + }, + + /**@ + * #.color + * @comp Color + * @trigger Change - when the color changes + * @sign public this .color(String color) + * @sign public String .color() + * @param color - Color of the rectangle + * Will create a rectangle of solid color for the entity, or return the color if no argument is given. + * + * The argument must be a color readable depending on which browser you + * choose to support. IE 8 and below doesn't support the rgb() syntax. + * + * @example + * ~~~ + * Crafty.e("2D, DOM, Color") + * .color("#969696"); + * ~~~ + */ + color: function (color) { + if (!color) return this._color; + this._color = color; + this.trigger("Change"); + return this; + } +}); + +/**@ + * #Tint + * @category Graphics + * Similar to Color by adding an overlay of semi-transparent color. + * + * *Note: Currently only works for Canvas* + */ +Crafty.c("Tint", { + _color: null, + _strength: 1.0, + + init: function () { + var draw = function d(e) { + var context = e.ctx || Crafty.canvas.context; + + context.fillStyle = this._color || "rgba(0,0,0, 0)"; + context.fillRect(e.pos._x, e.pos._y, e.pos._w, e.pos._h); + }; + + this.bind("Draw", draw).bind("RemoveComponent", function (id) { + if (id === "Tint") this.unbind("Draw", draw); + }); + }, + + /**@ + * #.tint + * @comp Tint + * @trigger Change - when the tint is applied + * @sign public this .tint(String color, Number strength) + * @param color - The color in hexadecimal + * @param strength - Level of opacity + * + * Modify the color and level opacity to give a tint on the entity. + * + * @example + * ~~~ + * Crafty.e("2D, Canvas, Tint") + * .tint("#969696", 0.3); + * ~~~ + */ + tint: function (color, strength) { + this._strength = strength; + this._color = Crafty.toRGB(color, this._strength); + + this.trigger("Change"); + return this; + } +}); + +/**@ + * #Image + * @category Graphics + * Draw an image with or without repeating (tiling). + */ +Crafty.c("Image", { + _repeat: "repeat", + ready: false, + + init: function () { + var draw = function (e) { + if (e.type === "canvas") { + //skip if no image + if (!this.ready || !this._pattern) return; + + var context = e.ctx; + + context.fillStyle = this._pattern; + + context.save(); + context.translate(e.pos._x, e.pos._y); + context.fillRect(0, 0, this._w, this._h); + context.restore(); + } else if (e.type === "DOM") { + if (this.__image) + e.style.background = "url(" + this.__image + ") " + this._repeat; + } + }; + + this.bind("Draw", draw).bind("RemoveComponent", function (id) { + if (id === "Image") this.unbind("Draw", draw); + }); + }, + + /**@ + * #.image + * @comp Image + * @trigger Change - when the image is loaded + * @sign public this .image(String url[, String repeat]) + * @param url - URL of the image + * @param repeat - If the image should be repeated to fill the entity. + * + * Draw specified image. Repeat follows CSS syntax (`"no-repeat", "repeat", "repeat-x", "repeat-y"`); + * + * *Note: Default repeat is `no-repeat` which is different to standard DOM (which is `repeat`)* + * + * If the width and height are `0` and repeat is set to `no-repeat` the width and + * height will automatically assume that of the image. This is an + * easy way to create an image without needing sprites. + * + * @example + * Will default to no-repeat. Entity width and height will be set to the images width and height + * ~~~ + * var ent = Crafty.e("2D, DOM, Image").image("myimage.png"); + * ~~~ + * Create a repeating background. + * ~~~ + * var bg = Crafty.e("2D, DOM, Image") + * .attr({w: Crafty.viewport.width, h: Crafty.viewport.height}) + * .image("bg.png", "repeat"); + * ~~~ + * + * @see Crafty.sprite + */ + image: function (url, repeat) { + this.__image = url; + this._repeat = repeat || "no-repeat"; + + this.img = Crafty.asset(url); + if (!this.img) { + this.img = new Image(); + Crafty.asset(url, this.img); + this.img.src = url; + var self = this; + + this.img.onload = function () { + if (self.has("Canvas")) self._pattern = Crafty.canvas.context.createPattern(self.img, self._repeat); + self.ready = true; + + if (self._repeat === "no-repeat") { + self.w = self.img.width; + self.h = self.img.height; + } + + self.trigger("Change"); + }; + + return this; + } else { + this.ready = true; + if (this.has("Canvas")) this._pattern = Crafty.canvas.context.createPattern(this.img, this._repeat); + if (this._repeat === "no-repeat") { + this.w = this.img.width; + this.h = this.img.height; + } + } + + + this.trigger("Change"); + + return this; + } +}); + +Crafty.extend({ + _scenes: {}, + _current: null, + + /**@ + * #Crafty.scene + * @category Scenes, Stage + * @trigger SceneChange - just before a new scene is initialized - { oldScene:String, newScene:String } + * @trigger SceneDestroy - just before the current scene is destroyed - { newScene:String } + * @sign public void Crafty.scene(String sceneName, Function init[, Function uninit]) + * @param sceneName - Name of the scene to add + * @param init - Function to execute when scene is played + * @param uninit - Function to execute before next scene is played, after entities with `2D` are destroyed + * @sign public void Crafty.scene(String sceneName) + * @param sceneName - Name of scene to play + * + * Method to create scenes on the stage. Pass an ID and function to register a scene. + * + * To play a scene, just pass the ID. When a scene is played, all + * previously-created entities with the `2D` component are destroyed. The + * viewport is also reset. + * + * If you want some entities to persist over scenes (as in, not be destroyed) + * simply add the component `Persist`. + * + * @example + * ~~~ + * Crafty.scene("loading", function() { + * Crafty.background("#000"); + * Crafty.e("2D, DOM, Text") + * .attr({ w: 100, h: 20, x: 150, y: 120 }) + * .text("Loading") + * .css({ "text-align": "center"}) + * .textColor("#FFFFFF"); + * }); + * + * Crafty.scene("UFO_dance", + * function() {Crafty.background("#444"); Crafty.e("UFO");}, + * function() {...send message to server...}); + * ~~~ + * This defines (but does not play) two scenes as discussed below. + * ~~~ + * Crafty.scene("loading"); + * ~~~ + * This command will clear the stage by destroying all `2D` entities (except + * those with the `Persist` component). Then it will set the background to + * black and display the text "Loading". + * ~~~ + * Crafty.scene("UFO_dance"); + * ~~~ + * This command will clear the stage by destroying all `2D` entities (except + * those with the `Persist` component). Then it will set the background to + * gray and create a UFO entity. Finally, the next time the game encounters + * another command of the form `Crafty.scene(scene_name)` (if ever), then the + * game will send a message to the server. + */ + scene: function (name, intro, outro) { + // ---FYI--- + // this._current is the name (ID) of the scene in progress. + // this._scenes is an object like the following: + // {'Opening scene': {'initialize': fnA, 'uninitialize': fnB}, + // 'Another scene': {'initialize': fnC, 'uninitialize': fnD}} + + // If there's one argument, play the scene + if (arguments.length === 1) { + Crafty.trigger("SceneDestroy", { + newScene: name + }); + Crafty.viewport.reset(); + + Crafty("2D").each(function () { + if (!this.has("Persist")) this.destroy(); + }); + // uninitialize previous scene + if (this._current !== null && 'uninitialize' in this._scenes[this._current]) { + this._scenes[this._current].uninitialize.call(this); + } + // initialize next scene + var oldScene = this._current; + this._current = name; + Crafty.trigger("SceneChange", { + oldScene: oldScene, + newScene: name + }); + this._scenes[name].initialize.call(this); + + return; + } + + // If there is more than one argument, add the scene information to _scenes + this._scenes[name] = {}; + this._scenes[name].initialize = intro; + if (typeof outro !== 'undefined') { + this._scenes[name].uninitialize = outro; + } + return; + }, + + /**@ + * #Crafty.toRGB + * @category Graphics + * @sign public String Crafty.scene(String hex[, Number alpha]) + * @param hex - a 6 character hex number string representing RGB color + * @param alpha - The alpha value. + * + * Get a rgb string or rgba string (if `alpha` presents). + * + * @example + * ~~~ + * Crafty.toRGB("ffffff"); // rgb(255,255,255) + * Crafty.toRGB("#ffffff"); // rgb(255,255,255) + * Crafty.toRGB("ffffff", .5); // rgba(255,255,255,0.5) + * ~~~ + * + * @see Text.textColor + */ + toRGB: function (hex, alpha) { + hex = (hex.charAt(0) === '#') ? hex.substr(1) : hex; + var c = [], + result; + + c[0] = parseInt(hex.substr(0, 2), 16); + c[1] = parseInt(hex.substr(2, 2), 16); + c[2] = parseInt(hex.substr(4, 2), 16); + + result = alpha === undefined ? 'rgb(' + c.join(',') + ')' : 'rgba(' + c.join(',') + ',' + alpha + ')'; + + return result; + } +}); + +/**@ + * #Crafty.DrawManager + * @category Graphics + * @sign Crafty.DrawManager + * + * An internal object manage objects to be drawn and implement + * the best method of drawing in both DOM and canvas + */ +Crafty.DrawManager = (function () { + /** Helper function to sort by globalZ */ + function zsort(a, b) { + return a._globalZ - b._globalZ; + } + + /** array of dirty rects on screen */ + var dirty_rects = [], + changed_objs = [], + /** array of DOMs needed updating */ + dom = [], + + dirtyViewport = false, + + + /** recManager: an object for managing dirty rectangles. */ + rectManager = { + /** Finds smallest rectangles that overlaps a and b, merges them into target */ + merge: function (a, b, target) { + if (typeof target === 'undefined') + target = {}; + // Doing it in this order means we can use either a or b as the target, with no conflict + target._h = Math.max(a._y + a._h, b._y + b._h); + target._w = Math.max(a._x + a._w, b._x + b._w); + target._x = Math.min(a._x, b._x); + target._y = Math.min(a._y, b._y); + target._w -= target._x; + target._h -= target._y; + + return target; + }, + + /** cleans up current dirty state, stores stale state for future passes */ + clean: function () { + var rect, obj, i; + for (i = 0, l = changed_objs.length; i < l; i++) { + obj = changed_objs[i]; + rect = obj._mbr || obj; + if (typeof obj.staleRect === 'undefined') + obj.staleRect = {}; + obj.staleRect._x = rect._x; + obj.staleRect._y = rect._y; + obj.staleRect._w = rect._w; + obj.staleRect._h = rect._h; + + obj._changed = false; + } + changed_objs.length = 0; + dirty_rects.length = 0; + + }, + + /** Takes the current and previous position of an object, and pushes the dirty regions onto the stack + * If the entity has only moved/changed a little bit, the regions are squashed together */ + createDirty: function (obj) { + var rect = obj._mbr || obj; + if (obj.staleRect) { + //If overlap, merge stale and current position together, then return + //Otherwise just push stale rectangle + if (rectManager.overlap(obj.staleRect, rect)) { + rectManager.merge(obj.staleRect, rect, obj.staleRect); + dirty_rects.push(obj.staleRect); + return; + } else { + dirty_rects.push(obj.staleRect); + } + } + + // We use the intermediate "currentRect" so it can be modified without messing with obj + obj.currentRect._x = rect._x; + obj.currentRect._y = rect._y; + obj.currentRect._w = rect._w; + obj.currentRect._h = rect._h; + dirty_rects.push(obj.currentRect); + + }, + + /** Checks whether two rectangles overlap */ + overlap: function (a, b) { + return (a._x < b._x + b._w && a._y < b._y + b._h && a._x + a._w > b._x && a._y + a._h > b._y); + } + + }; + + Crafty.bind("InvalidateViewport", function () { + dirtyViewport = true; + }); + Crafty.bind("PostRender", function () { + dirtyViewport = false; + }); + + return { + /**@ + * #Crafty.DrawManager.total2D + * @comp Crafty.DrawManager + * + * Total number of the entities that have the `2D` component. + */ + total2D: Crafty("2D").length, + + /**@ + * #Crafty.DrawManager.onScreen + * @comp Crafty.DrawManager + * @sign public Crafty.DrawManager.onScreen(Object rect) + * @param rect - A rectangle with field {_x: x_val, _y: y_val, _w: w_val, _h: h_val} + * + * Test if a rectangle is completely in viewport + */ + onScreen: function (rect) { + return Crafty.viewport._x + rect._x + rect._w > 0 && Crafty.viewport._y + rect._y + rect._h > 0 && + Crafty.viewport._x + rect._x < Crafty.viewport.width && Crafty.viewport._y + rect._y < Crafty.viewport.height; + }, + + /**@ + * #Crafty.DrawManager.mergeSet + * @comp Crafty.DrawManager + * @sign public Object Crafty.DrawManager.mergeSet(Object set) + * @param set - an array of rectangular regions + * + * Merge any consecutive, overlapping rects into each other. + * Its an optimization for the redraw regions. + * + * The order of set isn't strictly meaningful, + * but overlapping objects will often cause each other to change, + * and so might be consecutive. + */ + mergeSet: function (set) { + var i = 0; + while (i < set.length - 1) { + // If current and next overlap, merge them together into the first, removing the second + // Then skip the index backwards to compare the previous pair. + // Otherwise skip forward + if (rectManager.overlap(set[i], set[i + 1])) { + rectManager.merge(set[i], set[i + 1], set[i]); + set.splice(i + 1, 1); + if (i > 0) i--; + } else + i++; + } + + return set; + }, + + /**@ + * #Crafty.DrawManager.addCanvas + * @comp Crafty.DrawManager + * @sign public Crafty.DrawManager.addCanvas(ent) + * @param ent - The entity to add + * + * Add an entity to the list of Canvas objects to draw + */ + addCanvas: function addCanvas(ent) { + changed_objs.push(ent); + }, + + /**@ + * #Crafty.DrawManager.addDom + * @comp Crafty.DrawManager + * @sign public Crafty.DrawManager.addDom(ent) + * @param ent - The entity to add + * + * Add an entity to the list of DOM object to draw + */ + addDom: function addDom(ent) { + dom.push(ent); + }, + + /**@ + * #Crafty.DrawManager.debug + * @comp Crafty.DrawManager + * @sign public Crafty.DrawManager.debug() + */ + debug: function () { + console.log(changed_objs, dom); + }, + + /**@ + * #Crafty.DrawManager.drawAll + * @comp Crafty.DrawManager + * @sign public Crafty.DrawManager.drawAll([Object rect]) + * @param rect - a rectangular region {_x: x_val, _y: y_val, _w: w_val, _h: h_val} + * ~~~ + * - If rect is omitted, redraw within the viewport + * - If rect is provided, redraw within the rect + * ~~~ + */ + drawAll: function (rect) { + rect = rect || Crafty.viewport.rect(); + var q = Crafty.map.search(rect), + i = 0, + l = q.length, + ctx = Crafty.canvas.context, + current; + + ctx.clearRect(rect._x, rect._y, rect._w, rect._h); + + //sort the objects by the global Z + q.sort(zsort); + for (; i < l; i++) { + current = q[i]; + if (current._visible && current.__c.Canvas) { + current.draw(); + current._changed = false; + } + } + }, + + /**@ + * #Crafty.DrawManager.boundingRect + * @comp Crafty.DrawManager + * @sign public Crafty.DrawManager.boundingRect(set) + * @param set - Undocumented + * ~~~ + * - Calculate the common bounding rect of multiple canvas entities. + * - Returns coords + * ~~~ + */ + boundingRect: function (set) { + if (!set || !set.length) return; + var newset = [], + i = 1, + l = set.length, + current, master = set[0], + tmp; + master = [master._x, master._y, master._x + master._w, master._y + master._h]; + while (i < l) { + current = set[i]; + tmp = [current._x, current._y, current._x + current._w, current._y + current._h]; + if (tmp[0] < master[0]) master[0] = tmp[0]; + if (tmp[1] < master[1]) master[1] = tmp[1]; + if (tmp[2] > master[2]) master[2] = tmp[2]; + if (tmp[3] > master[3]) master[3] = tmp[3]; + i++; + } + tmp = master; + master = { + _x: tmp[0], + _y: tmp[1], + _w: tmp[2] - tmp[0], + _h: tmp[3] - tmp[1] + }; + + return master; + }, + + + + /**@ + * #Crafty.DrawManager.renderCanvas + * @comp Crafty.DrawManager + * @sign public Crafty.DrawManager.renderCanvas() + * ~~~ + * - Triggered by the "RenderScene" event + * - If the number of rects is over 60% of the total number of objects + * do the naive method redrawing `Crafty.DrawManager.drawAll` + * - Otherwise, clear the dirty regions, and redraw entities overlapping the dirty regions. + * ~~~ + * + * @see Canvas.draw + */ + + renderCanvas: function () { + var l = changed_objs.length; + if (!l && !dirtyViewport) { + return; + } + + var i = 0, + rect, q, + j, len, obj, ent, ctx = Crafty.canvas.context, + DM = Crafty.DrawManager; + + + if (dirtyViewport) { + var view = Crafty.viewport; + ctx.setTransform(view._scale, 0, 0, view._scale, view.x, view.y); + + } + //if the amount of changed objects is over 60% of the total objects + //do the naive method redrawing + // TODO: I'm not sure this condition really makes that much sense! + if (l / DM.total2D > 0.6 || dirtyViewport) { + DM.drawAll(); + rectManager.clean(); + return; + } + + // Calculate dirty_rects from all changed objects, then merge some overlapping regions together + for (i = 0; i < l; i++) { + rectManager.createDirty(changed_objs[i]); + } + dirty_rects = DM.mergeSet(dirty_rects); + + + l = dirty_rects.length; + var dupes = [], + objs = []; + // For each dirty rectangle, find entities near it, and draw the overlapping ones + for (i = 0; i < l; ++i) { //loop over every dirty rect + rect = dirty_rects[i]; + dupes.length = 0; + objs.length = 0; + if (!rect) continue; + + // Find the smallest rectangle with integer coordinates that encloses rect + rect._w = rect._x + rect._w; + rect._h = rect._y + rect._h; + rect._x = (rect._x > 0) ? (rect._x|0) : (rect._x|0) - 1; + rect._y = (rect._y > 0) ? (rect._y|0) : (rect._y|0) - 1; + rect._w -= rect._x; + rect._h -= rect._y; + rect._w = (rect._w === (rect._w|0)) ? rect._w : (rect._w|0) + 1; + rect._h = (rect._h === (rect._h|0)) ? rect._h : (rect._h|0) + 1; + + //search for ents under dirty rect + q = Crafty.map.search(rect, false); + + //clear the rect from the main canvas + ctx.clearRect(rect._x, rect._y, rect._w, rect._h); + + //Then clip drawing region to dirty rectangle + ctx.save(); + ctx.beginPath(); + ctx.rect(rect._x, rect._y, rect._w, rect._h); + ctx.clip(); + + // Loop over found objects removing dupes and adding visible canvas objects to array + for (j = 0, len = q.length; j < len; ++j) { + obj = q[j]; + + if (dupes[obj[0]] || !obj._visible || !obj.__c.Canvas) + continue; + dupes[obj[0]] = true; + objs.push(obj); + } + + // Sort objects by z level + objs.sort(zsort); + + // Then draw each object in that order + for (j = 0, len = objs.length; j < len; ++j) { + obj = objs[j]; + var area = obj._mbr || obj; + if (rectManager.overlap(area, rect)) + obj.draw(); + obj._changed = false; + } + + // Close rectangle clipping + ctx.closePath(); + ctx.restore(); + + } + + // Draw dirty rectangles for debugging, if that flag is set + if (Crafty.DrawManager.debugDirty === true) { + ctx.strokeStyle = 'red'; + for (i = 0, l = dirty_rects.length; i < l; ++i) { + rect = dirty_rects[i]; + ctx.strokeRect(rect._x, rect._y, rect._w, rect._h); + } + } + //Clean up lists etc + rectManager.clean(); + + }, + + /**@ + * #Crafty.DrawManager.renderDOM + * @comp Crafty.DrawManager + * @sign public Crafty.DrawManager.renderDOM() + * ~~~ + * When "RenderScene" is triggered, draws all DOM entities that have been flagged + * ~~~ + * + * @see DOM.draw + */ + renderDOM: function () { + // Adjust the viewport + if (dirtyViewport) { + var style = Crafty.stage.inner.style, + view = Crafty.viewport; + + style.transform = style[Crafty.support.prefix + "Transform"] = "scale(" + view._scale + ", " + view._scale + ")"; + style.left = view.x + "px"; + style.top = view.y + "px"; + style.zIndex = 10; + } + + //if no objects have been changed, stop + if (!dom.length) return; + + var i = 0, + k = dom.length; + //loop over all DOM elements needing updating + for (; i < k; ++i) { + dom[i].draw()._changed = false; + } + + //reset DOM array + dom.length = 0; + + } + + + }; +})(); + +},{"./core.js":9}],13:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +/**@ + * #Crafty.support + * @category Misc, Core + * Determines feature support for what Crafty can do. + */ +(function testSupport() { + var support = Crafty.support = {}, + ua = navigator.userAgent.toLowerCase(), + match = /(webkit)[ \/]([\w.]+)/.exec(ua) || + /(o)pera(?:.*version)?[ \/]([\w.]+)/.exec(ua) || + /(ms)ie ([\w.]+)/.exec(ua) || + /(moz)illa(?:.*? rv:([\w.]+))?/.exec(ua) || [], + mobile = /iPad|iPod|iPhone|Android|webOS|IEMobile/i.exec(ua); + + /**@ + * #Crafty.mobile + * @comp Crafty.device + * + * Determines if Crafty is running on mobile device. + * + * If Crafty.mobile is equal true Crafty does some things under hood: + * ~~~ + * - set viewport on max device width and height + * - set Crafty.stage.fullscreen on true + * - hide window scrollbars + * ~~~ + * + * @see Crafty.viewport + */ + if (mobile) Crafty.mobile = mobile[0]; + + /**@ + * #Crafty.support.setter + * @comp Crafty.support + * Is `__defineSetter__` supported? + */ + support.setter = ('__defineSetter__' in this && '__defineGetter__' in this); + + /**@ + * #Crafty.support.defineProperty + * @comp Crafty.support + * Is `Object.defineProperty` supported? + */ + support.defineProperty = (function () { + if (!('defineProperty' in Object)) return false; + try { + Object.defineProperty({}, 'x', {}); + } catch (e) { + return false; + } + return true; + })(); + + /**@ + * #Crafty.support.audio + * @comp Crafty.support + * Is HTML5 `Audio` supported? + */ + support.audio = ('Audio' in window); + + /**@ + * #Crafty.support.prefix + * @comp Crafty.support + * Returns the browser specific prefix (`Moz`, `O`, `ms`, `webkit`). + */ + support.prefix = (match[1] || match[0]); + + //browser specific quirks + if (support.prefix === "moz") support.prefix = "Moz"; + if (support.prefix === "o") support.prefix = "O"; + + if (match[2]) { + /**@ + * #Crafty.support.versionName + * @comp Crafty.support + * Version of the browser + */ + support.versionName = match[2]; + + /**@ + * #Crafty.support.version + * @comp Crafty.support + * Version number of the browser as an Integer (first number) + */ + support.version = +(match[2].split("."))[0]; + } + + /**@ + * #Crafty.support.canvas + * @comp Crafty.support + * Is the `canvas` element supported? + */ + support.canvas = ('getContext' in document.createElement("canvas")); + + /**@ + * #Crafty.support.webgl + * @comp Crafty.support + * Is WebGL supported on the canvas element? + */ + if (support.canvas) { + var gl; + try { + gl = document.createElement("canvas").getContext("experimental-webgl"); + gl.viewportWidth = support.canvas.width; + gl.viewportHeight = support.canvas.height; + } catch (e) {} + support.webgl = !! gl; + } else { + support.webgl = false; + } + + /**@ + * #Crafty.support.css3dtransform + * @comp Crafty.support + * Is css3Dtransform supported by browser. + */ + support.css3dtransform = (typeof document.createElement("div").style.Perspective !== "undefined") || (typeof document.createElement("div").style[support.prefix + "Perspective"] !== "undefined"); + + /**@ + * #Crafty.support.deviceorientation + * @comp Crafty.support + * Is deviceorientation event supported by browser. + */ + support.deviceorientation = (typeof window.DeviceOrientationEvent !== "undefined") || (typeof window.OrientationEvent !== "undefined"); + + /**@ + * #Crafty.support.devicemotion + * @comp Crafty.support + * Is devicemotion event supported by browser. + */ + support.devicemotion = (typeof window.DeviceMotionEvent !== "undefined"); + +})(); +Crafty.extend({ + + zeroFill: function (number, width) { + width -= number.toString().length; + if (width > 0) + return new Array(width + (/\./.test(number) ? 2 : 1)).join('0') + number; + return number.toString(); + }, + + /**@ + * #Crafty.sprite + * @category Graphics + * @sign public this Crafty.sprite([Number tile, [Number tileh]], String url, Object map[, Number paddingX[, Number paddingY]]) + * @param tile - Tile size of the sprite map, defaults to 1 + * @param tileh - Height of the tile; if provided, tile is interpreted as the width + * @param url - URL of the sprite image + * @param map - Object where the key is what becomes a new component and the value points to a position on the sprite map + * @param paddingX - Horizontal space in between tiles. Defaults to 0. + * @param paddingY - Vertical space in between tiles. Defaults to paddingX. + * Generates components based on positions in a sprite image to be applied to entities. + * + * Accepts a tile size, URL and map for the name of the sprite and its position. + * + * The position must be an array containing the position of the sprite where index `0` + * is the `x` position, `1` is the `y` position and optionally `2` is the width and `3` + * is the height. If the sprite map has padding, pass the values for the `x` padding + * or `y` padding. If they are the same, just add one value. + * + * If the sprite image has no consistent tile size, `1` or no argument need be + * passed for tile size. + * + * Entities that add the generated components are also given the `2D` component, and + * a component called `Sprite`. + * + * @example + * ~~~ + * Crafty.sprite("imgs/spritemap6.png", {flower:[0,0,20,30]}); + * var flower_entity = Crafty.e("2D, DOM, flower"); + * ~~~ + * The first line creates a component called `flower` associated with the sub-image of + * spritemap6.png with top-left corner (0,0), width 20 pixels, and height 30 pixels. + * The second line creates an entity with that image. (Note: The `2D` is not really + * necessary here, because adding the `flower` component automatically also adds the + * `2D` component.) + * ~~~ + * Crafty.sprite(50, "imgs/spritemap6.png", {flower:[0,0], grass:[0,1,3,1]}); + * ~~~ + * In this case, the `flower` component is pixels 0 <= x < 50, 0 <= y < 50, and the + * `grass` component is pixels 0 <= x < 150, 50 <= y < 100. (The `3` means grass has a + * width of 3 tiles, i.e. 150 pixels.) + * ~~~ + * Crafty.sprite(50, 100, "imgs/spritemap6.png", {flower:[0,0], grass:[0,1]}, 10); + * ~~~ + * In this case, each tile is 50x100, and there is a spacing of 10 pixels between + * consecutive tiles. So `flower` is pixels 0 <= x < 50, 0 <= y < 100, and `grass` is + * pixels 0 <= x < 50, 110 <= y < 210. + * + * @see Sprite + */ + sprite: function (tile, tileh, url, map, paddingX, paddingY) { + var spriteName, temp, x, y, w, h, img; + + //if no tile value, default to 1. + //(if the first passed argument is a string, it must be the url.) + if (typeof tile === "string") { + paddingY = paddingX; + paddingX = map; + map = tileh; + url = tile; + tile = 1; + tileh = 1; + } + + if (typeof tileh == "string") { + paddingY = paddingX; + paddingX = map; + map = url; + url = tileh; + tileh = tile; + } + + //if no paddingY, use paddingX + if (!paddingY && paddingX) paddingY = paddingX; + paddingX = parseInt(paddingX || 0, 10); //just incase + paddingY = parseInt(paddingY || 0, 10); + + var markSpritesReady = function() { + this.ready = true; + this.trigger("Change"); + }; + + img = Crafty.asset(url); + if (!img) { + img = new Image(); + img.src = url; + Crafty.asset(url, img); + img.onload = function () { + //all components with this img are now ready + for (var spriteName in map) { + Crafty(spriteName).each(markSpritesReady); + } + }; + } + + var sharedSpriteInit = function() { + this.requires("2D, Sprite"); + this.__trim = [0, 0, 0, 0]; + this.__image = url; + this.__coord = [this.__coord[0], this.__coord[1], this.__coord[2], this.__coord[3]]; + this.__tile = tile; + this.__tileh = tileh; + this.__padding = [paddingX, paddingY]; + this.img = img; + + //draw now + if (this.img.complete && this.img.width > 0) { + this.ready = true; + this.trigger("Change"); + } + + //set the width and height to the sprite size + this.w = this.__coord[2]; + this.h = this.__coord[3]; + }; + + for (spriteName in map) { + if (!map.hasOwnProperty(spriteName)) continue; + + temp = map[spriteName]; + x = temp[0] * (tile + paddingX); + y = temp[1] * (tileh + paddingY); + w = temp[2] * tile || tile; + h = temp[3] * tileh || tileh; + + //generates sprite components for each tile in the map + Crafty.c(spriteName, { + ready: false, + __coord: [x, y, w, h], + + init: sharedSpriteInit + }); + } + + return this; + }, + + _events: {}, + + /**@ + * #Crafty.addEvent + * @category Events, Misc + * @sign public this Crafty.addEvent(Object ctx, HTMLElement obj, String event, Function callback) + * @param ctx - Context of the callback or the value of `this` + * @param obj - Element to add the DOM event to + * @param event - Event name to bind to + * @param callback - Method to execute when triggered + * + * Adds DOM level 3 events to elements. The arguments it accepts are the call + * context (the value of `this`), the DOM element to attach the event to, + * the event name (without `on` (`click` rather than `onclick`)) and + * finally the callback method. + * + * If no element is passed, the default element will be `window.document`. + * + * Callbacks are passed with event data. + * + * @example + * Will add a stage-wide MouseDown event listener to the player. Will log which button was pressed + * & the (x,y) coordinates in viewport/world/game space. + * ~~~ + * var player = Crafty.e("2D"); + * player.onMouseDown = function(e) { + * console.log(e.mouseButton, e.realX, e.realY); + * }; + * Crafty.addEvent(player, Crafty.stage.elem, "mousedown", player.onMouseDown); + * ~~~ + * @see Crafty.removeEvent + */ + addEvent: function (ctx, obj, type, callback) { + if (arguments.length === 3) { + callback = type; + type = obj; + obj = window.document; + } + + //save anonymous function to be able to remove + var afn = function (e) { + e = e || window.event; + + if (typeof callback === 'function') { + callback.call(ctx, e); + } + }, + id = ctx[0] || ""; + + if (!this._events[id + obj + type + callback]) this._events[id + obj + type + callback] = afn; + else return; + + if (obj.attachEvent) { //IE + obj.attachEvent('on' + type, afn); + } else { //Everyone else + obj.addEventListener(type, afn, false); + } + }, + + /**@ + * #Crafty.removeEvent + * @category Events, Misc + * @sign public this Crafty.removeEvent(Object ctx, HTMLElement obj, String event, Function callback) + * @param ctx - Context of the callback or the value of `this` + * @param obj - Element the event is on + * @param event - Name of the event + * @param callback - Method executed when triggered + * + * Removes events attached by `Crafty.addEvent()`. All parameters must + * be the same that were used to attach the event including a reference + * to the callback method. + * + * @see Crafty.addEvent + */ + removeEvent: function (ctx, obj, type, callback) { + if (arguments.length === 3) { + callback = type; + type = obj; + obj = window.document; + } + + //retrieve anonymous function + var id = ctx[0] || "", + afn = this._events[id + obj + type + callback]; + + if (afn) { + if (obj.detachEvent) { + obj.detachEvent('on' + type, afn); + } else obj.removeEventListener(type, afn, false); + delete this._events[id + obj + type + callback]; + } + }, + + /**@ + * #Crafty.background + * @category Graphics, Stage + * @sign public void Crafty.background(String value) + * @param style - Modify the background with a color or image + * + * This method is essentially a shortcut for adding a background + * style to the stage element. + */ + background: function (style) { + Crafty.stage.elem.style.background = style; + }, + + + + /**@ + * #Crafty.keys + * @category Input + * Object of key names and the corresponding key code. + * + * ~~~ + * BACKSPACE: 8, + * TAB: 9, + * ENTER: 13, + * PAUSE: 19, + * CAPS: 20, + * ESC: 27, + * SPACE: 32, + * PAGE_UP: 33, + * PAGE_DOWN: 34, + * END: 35, + * HOME: 36, + * LEFT_ARROW: 37, + * UP_ARROW: 38, + * RIGHT_ARROW: 39, + * DOWN_ARROW: 40, + * INSERT: 45, + * DELETE: 46, + * 0: 48, + * 1: 49, + * 2: 50, + * 3: 51, + * 4: 52, + * 5: 53, + * 6: 54, + * 7: 55, + * 8: 56, + * 9: 57, + * A: 65, + * B: 66, + * C: 67, + * D: 68, + * E: 69, + * F: 70, + * G: 71, + * H: 72, + * I: 73, + * J: 74, + * K: 75, + * L: 76, + * M: 77, + * N: 78, + * O: 79, + * P: 80, + * Q: 81, + * R: 82, + * S: 83, + * T: 84, + * U: 85, + * V: 86, + * W: 87, + * X: 88, + * Y: 89, + * Z: 90, + * NUMPAD_0: 96, + * NUMPAD_1: 97, + * NUMPAD_2: 98, + * NUMPAD_3: 99, + * NUMPAD_4: 100, + * NUMPAD_5: 101, + * NUMPAD_6: 102, + * NUMPAD_7: 103, + * NUMPAD_8: 104, + * NUMPAD_9: 105, + * MULTIPLY: 106, + * ADD: 107, + * SUBSTRACT: 109, + * DECIMAL: 110, + * DIVIDE: 111, + * F1: 112, + * F2: 113, + * F3: 114, + * F4: 115, + * F5: 116, + * F6: 117, + * F7: 118, + * F8: 119, + * F9: 120, + * F10: 121, + * F11: 122, + * F12: 123, + * SHIFT: 16, + * CTRL: 17, + * ALT: 18, + * PLUS: 187, + * COMMA: 188, + * MINUS: 189, + * PERIOD: 190, + * PULT_UP: 29460, + * PULT_DOWN: 29461, + * PULT_LEFT: 4, + * PULT_RIGHT': 5 + * ~~~ + */ + keys: { + 'BACKSPACE': 8, + 'TAB': 9, + 'ENTER': 13, + 'PAUSE': 19, + 'CAPS': 20, + 'ESC': 27, + 'SPACE': 32, + 'PAGE_UP': 33, + 'PAGE_DOWN': 34, + 'END': 35, + 'HOME': 36, + 'LEFT_ARROW': 37, + 'UP_ARROW': 38, + 'RIGHT_ARROW': 39, + 'DOWN_ARROW': 40, + 'INSERT': 45, + 'DELETE': 46, + '0': 48, + '1': 49, + '2': 50, + '3': 51, + '4': 52, + '5': 53, + '6': 54, + '7': 55, + '8': 56, + '9': 57, + 'A': 65, + 'B': 66, + 'C': 67, + 'D': 68, + 'E': 69, + 'F': 70, + 'G': 71, + 'H': 72, + 'I': 73, + 'J': 74, + 'K': 75, + 'L': 76, + 'M': 77, + 'N': 78, + 'O': 79, + 'P': 80, + 'Q': 81, + 'R': 82, + 'S': 83, + 'T': 84, + 'U': 85, + 'V': 86, + 'W': 87, + 'X': 88, + 'Y': 89, + 'Z': 90, + 'NUMPAD_0': 96, + 'NUMPAD_1': 97, + 'NUMPAD_2': 98, + 'NUMPAD_3': 99, + 'NUMPAD_4': 100, + 'NUMPAD_5': 101, + 'NUMPAD_6': 102, + 'NUMPAD_7': 103, + 'NUMPAD_8': 104, + 'NUMPAD_9': 105, + 'MULTIPLY': 106, + 'ADD': 107, + 'SUBSTRACT': 109, + 'DECIMAL': 110, + 'DIVIDE': 111, + 'F1': 112, + 'F2': 113, + 'F3': 114, + 'F4': 115, + 'F5': 116, + 'F6': 117, + 'F7': 118, + 'F8': 119, + 'F9': 120, + 'F10': 121, + 'F11': 122, + 'F12': 123, + 'SHIFT': 16, + 'CTRL': 17, + 'ALT': 18, + 'PLUS': 187, + 'COMMA': 188, + 'MINUS': 189, + 'PERIOD': 190, + 'PULT_UP': 29460, + 'PULT_DOWN': 29461, + 'PULT_LEFT': 4, + 'PULT_RIGHT': 5 + + }, + + /**@ + * #Crafty.mouseButtons + * @category Input + * Object of mouseButton names and the corresponding button ID. + * In all mouseEvents we add the e.mouseButton property with a value normalized to match e.button of modern webkit + * + * ~~~ + * LEFT: 0, + * MIDDLE: 1, + * RIGHT: 2 + * ~~~ + */ + mouseButtons: { + LEFT: 0, + MIDDLE: 1, + RIGHT: 2 + } +}); +},{"./core.js":9}],14:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +/**@ + * #HTML + * @category Graphics + * Component allow for insertion of arbitrary HTML into an entity + */ +Crafty.c("HTML", { + inner: '', + + init: function () { + this.requires('2D, DOM'); + }, + + /**@ + * #.replace + * @comp HTML + * @sign public this .replace(String html) + * @param html - arbitrary html + * + * This method will replace the content of this entity with the supplied html + * + * @example + * Create a link + * ~~~ + * Crafty.e("HTML") + * .attr({x:20, y:20, w:100, h:100}) + * .replace("Crafty.js"); + * ~~~ + */ + replace: function (new_html) { + this.inner = new_html; + this._element.innerHTML = new_html; + return this; + }, + + /**@ + * #.append + * @comp HTML + * @sign public this .append(String html) + * @param html - arbitrary html + * + * This method will add the supplied html in the end of the entity + * + * @example + * Create a link + * ~~~ + * Crafty.e("HTML") + * .attr({x:20, y:20, w:100, h:100}) + * .append("Crafty.js"); + * ~~~ + */ + append: function (new_html) { + this.inner += new_html; + this._element.innerHTML += new_html; + return this; + }, + + /**@ + * #.prepend + * @comp HTML + * @sign public this .prepend(String html) + * @param html - arbitrary html + * + * This method will add the supplied html in the beginning of the entity + * + * @example + * Create a link + * ~~~ + * Crafty.e("HTML") + * .attr({x:20, y:20, w:100, h:100}) + * .prepend("Crafty.js"); + * ~~~ + */ + prepend: function (new_html) { + this.inner = new_html + this.inner; + this._element.innerHTML = new_html + this.inner; + return this; + } +}); +},{"./core.js":9}],15:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +/**@ + * #Crafty.import + * @sign public void Crafty.import(String url[, String scene]) + * @param url - Path to the saved file + * @param scene - Name of the scene to load if saved multiple scenes + * @sign public void Crafty.import(Object sceneData) + * @param sceneData - Scene data generated from builder + * This method will load in scene data generated by the Crafty Builder. + * + * @example + * ~~~ + * Crafty.import({ + * '0': {props: value}, + * 'n': [ + * {c: "comp, list", image: ''} + * ] + * }); + * ~~~ + */ +Crafty['import'] = function (obj, scene) { + //if its a string, load the script file + if (typeof obj === "string") { + if (levelData) { + if (scene) Crafty.import(levelData[scene]); + else Crafty.import(levelData); + } else { + var elem; + elem = document.createElement("script"); + elem.onload = function () { + if (scene) Crafty.import(levelData[scene]); + else Crafty.import(levelData); + }; + elem.src = obj; + } + return; + } + + var key, i = 0, + l, current, ent; + + //loop over new entities to create + if (obj.n && typeof obj.n === "object") { + for (l = obj.n.length; i < l; ++i) { + current = obj.n[i]; + + //create entity with components + ent = Crafty.e(current.c); + delete current.c; //remove the components + + //apply the other properties + ent.attr(current); + } + } + + //loop over modified entities + for (key in obj) { + ent = Crafty(key); + ent.attr(obj[key]); + } +}; +},{"./core.js":9}],16:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +Crafty.extend({ + /**@ + * #Crafty.isometric + * @category 2D + * Place entities in a 45deg isometric fashion. + */ + isometric: { + _tile: { + width: 0, + height: 0 + }, + _elements: {}, + _pos: { + x: 0, + y: 0 + }, + _z: 0, + /**@ + * #Crafty.isometric.size + * @comp Crafty.isometric + * @sign public this Crafty.isometric.size(Number tileSize) + * @param tileSize - The size of the tiles to place. + * + * Method used to initialize the size of the isometric placement. + * Recommended to use a size values in the power of `2` (128, 64 or 32). + * This makes it easy to calculate positions and implement zooming. + * + * @example + * ~~~ + * var iso = Crafty.isometric.size(128); + * ~~~ + * + * @see Crafty.isometric.place + */ + size: function (width, height) { + this._tile.width = width; + this._tile.height = height > 0 ? height : width / 2; //Setup width/2 if height isn't set + return this; + }, + /**@ + * #Crafty.isometric.place + * @comp Crafty.isometric + * @sign public this Crafty.isometric.place(Number x, Number y, Number z, Entity tile) + * @param x - The `x` position to place the tile + * @param y - The `y` position to place the tile + * @param z - The `z` position or height to place the tile + * @param tile - The entity that should be position in the isometric fashion + * + * Use this method to place an entity in an isometric grid. + * + * @example + * ~~~ + * var iso = Crafty.isometric.size(128); + * iso.place(2, 1, 0, Crafty.e('2D, DOM, Color').color('red').attr({w:128, h:128})); + * ~~~ + * + * @see Crafty.isometric.size + */ + place: function (x, y, z, obj) { + var pos = this.pos2px(x, y); + pos.top -= z * (this._tile.height / 2); + obj.attr({ + x: pos.left + Crafty.viewport._x, + y: pos.top + Crafty.viewport._y + }).z += z; + return this; + }, + /**@ + * #Crafty.isometric.pos2px + * @comp Crafty.isometric + * @sign public this Crafty.isometric.pos2px(Number x,Number y) + * @param x + * @param y + * @return Object {left Number,top Number} + * + * This method calculate the X and Y Coordinates to Pixel Positions + * + * @example + * ~~~ + * var iso = Crafty.isometric.size(128,96); + * var position = iso.pos2px(100,100); //Object { left=12800, top=4800} + * ~~~ + */ + pos2px: function (x, y) { + return { + left: x * this._tile.width + (y & 1) * (this._tile.width / 2), + top: y * this._tile.height / 2 + }; + }, + /**@ + * #Crafty.isometric.px2pos + * @comp Crafty.isometric + * @sign public this Crafty.isometric.px2pos(Number left,Number top) + * @param top + * @param left + * @return Object {x Number,y Number} + * + * This method calculate pixel top,left positions to x,y coordinates + * + * @example + * ~~~ + * var iso = Crafty.isometric.size(128,96); + * var px = iso.pos2px(12800,4800); + * console.log(px); //Object { x=100, y=100} + * ~~~ + */ + px2pos: function (left, top) { + return { + x: -Math.ceil(-left / this._tile.width - (top & 1) * 0.5), + y: top / this._tile.height * 2 + }; + }, + /**@ + * #Crafty.isometric.centerAt + * @comp Crafty.isometric + * @sign public this Crafty.isometric.centerAt(Number x,Number y) + * @param top + * @param left + * + * This method center the Viewport at x/y location or gives the current centerpoint of the viewport + * + * @example + * ~~~ + * var iso = Crafty.isometric.size(128,96).centerAt(10,10); //Viewport is now moved + * //After moving the viewport by another event you can get the new center point + * console.log(iso.centerAt()); + * ~~~ + */ + centerAt: function (x, y) { + if (typeof x == "number" && typeof y == "number") { + var center = this.pos2px(x, y); + Crafty.viewport._x = -center.left + Crafty.viewport.width / 2 - this._tile.width / 2; + Crafty.viewport._y = -center.top + Crafty.viewport.height / 2 - this._tile.height / 2; + return this; + } else { + return { + top: -Crafty.viewport._y + Crafty.viewport.height / 2 - this._tile.height / 2, + left: -Crafty.viewport._x + Crafty.viewport.width / 2 - this._tile.width / 2 + }; + } + }, + /**@ + * #Crafty.isometric.area + * @comp Crafty.isometric + * @sign public this Crafty.isometric.area() + * @return Object {x:{start Number,end Number},y:{start Number,end Number}} + * + * This method get the Area surrounding by the centerpoint depends on viewport height and width + * + * @example + * ~~~ + * var iso = Crafty.isometric.size(128,96).centerAt(10,10); //Viewport is now moved + * var area = iso.area(); //get the area + * for(var y = area.y.start;y <= area.y.end;y++){ + * for(var x = area.x.start ;x <= area.x.end;x++){ + * iso.place(x,y,0,Crafty.e("2D,DOM,gras")); //Display tiles in the Screen + * } + * } + * ~~~ + */ + area: function () { + //Get the center Point in the viewport + var center = this.centerAt(); + var start = this.px2pos(-center.left + Crafty.viewport.width / 2, -center.top + Crafty.viewport.height / 2); + var end = this.px2pos(-center.left - Crafty.viewport.width / 2, -center.top - Crafty.viewport.height / 2); + return { + x: { + start: start.x, + end: end.x + }, + y: { + start: start.y, + end: end.y + } + }; + } + } +}); + +},{"./core.js":9}],17:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +Crafty.extend({ + /**@ + * #Crafty.assets + * @category Assets + * An object containing every asset used in the current Crafty game. + * The key is the URL and the value is the `Audio` or `Image` object. + * + * If loading an asset, check that it is in this object first to avoid loading twice. + * + * @example + * ~~~ + * var isLoaded = !!Crafty.assets["images/sprite.png"]; + * ~~~ + * @see Crafty.loader + */ + assets: {}, + + /**@ + * #Crafty.asset + * @category Assets + * + * @trigger NewAsset - After setting new asset - Object - key and value of new added asset. + * @sign public void Crafty.asset(String key, Object asset) + * @param key - asset url. + * @param asset - Audio` or `Image` object. + * Add new asset to assets object. + * + * @sign public void Crafty.asset(String key) + * @param key - asset url. + * Get asset from assets object. + * + * @example + * ~~~ + * Crafty.asset(key, value); + * var asset = Crafty.asset(key); //object with key and value fields + * ~~~ + * + * @see Crafty.assets + */ + asset: function (key, value) { + if (arguments.length === 1) { + return Crafty.assets[key]; + } + + if (!Crafty.assets[key]) { + Crafty.assets[key] = value; + this.trigger("NewAsset", { + key: key, + value: value + }); + return value; + } + }, + /**@ + * #Crafty.image_whitelist + * @category Assets + * + * + * A list of file extensions that can be loaded as images by Crafty.load + * + * @example + * ~~~ + * Crafty.image_whitelist.push("tif") + * Crafty.load(["images/sprite.tif", "sounds/jump.mp3"], + * function() { + * //when loaded + * Crafty.scene("main"); //go to main scene + * Crafty.audio.play("jump.mp3"); //Play the audio file + * }, + * + * function(e) { + * //progress + * }, + * + * function(e) { + * //uh oh, error loading + * } + * ); + * ~~~ + * + * @see Crafty.asset + * @see Crafty.load + */ + image_whitelist: ["jpg", "jpeg", "gif", "png", "svg"], + /**@ + * #Crafty.loader + * @category Assets + * @sign public void Crafty.load(Array assets, Function onLoad[, Function onProgress, Function onError]) + * @param assets - Array of assets to load (accepts sounds and images) + * @param onLoad - Callback when the assets are loaded + * @param onProgress - Callback when an asset is loaded. Contains information about assets loaded + * @param onError - Callback when an asset fails to load + * + * Preloader for all assets. Takes an array of URLs and + * adds them to the `Crafty.assets` object. + * + * Files with suffixes in `image_whitelist` (case insensitive) will be loaded. + * + * If `Crafty.support.audio` is `true`, files with the following suffixes `mp3`, `wav`, `ogg` and `mp4` (case insensitive) can be loaded. + * + * The `onProgress` function will be passed on object with information about + * the progress including how many assets loaded, total of all the assets to + * load and a percentage of the progress. + * ~~~ + * { loaded: j, total: total, percent: (j / total * 100) ,src:src}) + * ~~~ + * + * `onError` will be passed with the asset that couldn't load. + * + * When `onError` is not provided, the onLoad is loaded even some assets are not successfully loaded. Otherwise, onLoad will be called no matter whether there are errors or not. + * + * @example + * ~~~ + * Crafty.load(["images/sprite.png", "sounds/jump.mp3"], + * function() { + * //when loaded + * Crafty.scene("main"); //go to main scene + * Crafty.audio.play("jump.mp3"); //Play the audio file + * }, + * + * function(e) { + * //progress + * }, + * + * function(e) { + * //uh oh, error loading + * } + * ); + * ~~~ + * + * @see Crafty.assets + * @see Crafty.image_whitelist + */ + load: function (data, oncomplete, onprogress, onerror) { + + var i = 0, + l = data.length, + current, obj, total = l, + j = 0, + ext = ""; + + //Progress function + + function pro() { + var src = this.src; + + //Remove events cause audio trigger this event more than once(depends on browser) + if (this.removeEventListener) { + this.removeEventListener('canplaythrough', pro, false); + } + + ++j; + //if progress callback, give information of assets loaded, total and percent + if (onprogress) + onprogress({ + loaded: j, + total: total, + percent: (j / total * 100), + src: src + }); + + if (j === total && oncomplete) oncomplete(); + } + //Error function + + function err() { + var src = this.src; + if (onerror) + onerror({ + loaded: j, + total: total, + percent: (j / total * 100), + src: src + }); + + j++; + if (j === total && oncomplete) oncomplete(); + } + + for (; i < l; ++i) { + current = data[i]; + ext = current.substr(current.lastIndexOf('.') + 1, 3).toLowerCase(); + + obj = Crafty.asset(current) || null; + + if (Crafty.audio.supports(ext)) { + //Create a new asset if necessary, using the file name as an id + if (!obj) { + var name = current.substr(current.lastIndexOf('/') + 1).toLowerCase(); + obj = Crafty.audio.create(name, current).obj; + } + + //addEventListener is supported on IE9 , Audio as well + if (obj.addEventListener) { + obj.addEventListener('canplaythrough', pro, false); + } + + + } else if (Crafty.image_whitelist.indexOf(ext) >= 0) { + if (!obj) { + obj = new Image(); + Crafty.asset(current, obj); + } + obj.onload = pro; + if (Crafty.support.prefix === 'webkit') { + obj.src = ""; // workaround for webkit bug + } + obj.src = current; //setup src after onload function Opera/IE Bug + + } else { + total--; + continue; //skip if not applicable + } + obj.onerror = err; + } + + // If we aren't trying to handle *any* of the files, that's as complete as it gets! + if (total === 0) + oncomplete(); + + }, + /**@ + * #Crafty.modules + * @category Assets + * @sign public void Crafty.modules([String repoLocation,] Object moduleMap[, Function onLoad]) + * @param modules - Map of name:version pairs for modules to load + * @param onLoad - Callback when the modules are loaded + * + * Browse the selection of community modules on http://craftycomponents.com + * + * It is possible to create your own repository. + * + * + * @example + * ~~~ + * // Loading from default repository + * Crafty.modules({ moveto: 'DEV' }, function () { + * //module is ready + * Crafty.e("MoveTo, 2D, DOM"); + * }); + * + * // Loading from your own server + * Crafty.modules({ 'http://mydomain.com/js/mystuff.js': 'DEV' }, function () { + * //module is ready + * Crafty.e("MoveTo, 2D, DOM"); + * }); + * + * // Loading from alternative repository + * Crafty.modules('http://cdn.crafty-modules.com', { moveto: 'DEV' }, function () { + * //module is ready + * Crafty.e("MoveTo, 2D, DOM"); + * }); + * + * // Loading from the latest component website + * Crafty.modules( + * 'http://cdn.craftycomponents.com' + * , { MoveTo: 'release' } + * , function () { + * Crafty.e("2D, DOM, Color, MoveTo") + * .attr({x: 0, y: 0, w: 50, h: 50}) + * .color("green"); + * }); + * }); + * ~~~ + * + */ + modules: function (modulesRepository, moduleMap, oncomplete) { + + if (arguments.length === 2 && typeof modulesRepository === "object") { + oncomplete = moduleMap; + moduleMap = modulesRepository; + modulesRepository = 'http://cdn.craftycomponents.com'; + } + + /*! + * $script.js Async loader & dependency manager + * https://github.com/ded/script.js + * (c) Dustin Diaz, Jacob Thornton 2011 + * License: MIT + */ + var $script = (function () { + var win = this, + doc = document, + head = doc.getElementsByTagName('head')[0], + validBase = /^https?:\/\//, + old = win.$script, + list = {}, ids = {}, delay = {}, scriptpath, scripts = {}, s = 'string', + f = false, + push = 'push', + domContentLoaded = 'DOMContentLoaded', + readyState = 'readyState', + addEventListener = 'addEventListener', + onreadystatechange = 'onreadystatechange'; + + function every(ar, fn, i) { + for (i = 0, j = ar.length; i < j; ++i) + if (!fn(ar[i])) return f; + return 1; + } + + function each(ar, fn) { + every(ar, function (el) { + return !fn(el); + }); + } + + if (!doc[readyState] && doc[addEventListener]) { + doc[addEventListener](domContentLoaded, function fn() { + doc.removeEventListener(domContentLoaded, fn, f); + doc[readyState] = 'complete'; + }, f); + doc[readyState] = 'loading'; + } + + function $script(paths, idOrDone, optDone) { + paths = paths[push] ? paths : [paths]; + var idOrDoneIsDone = idOrDone && idOrDone.call, + done = idOrDoneIsDone ? idOrDone : optDone, + id = idOrDoneIsDone ? paths.join('') : idOrDone, + queue = paths.length; + + function loopFn(item) { + return item.call ? item() : list[item]; + } + + function callback() { + if (!--queue) { + list[id] = 1; + if (done) + done(); + for (var dset in delay) { + if (every(dset.split('|'), loopFn) && !each(delay[dset], loopFn)) + delay[dset] = []; + } + } + } + setTimeout(function () { + each(paths, function (path) { + if (scripts[path]) { + if (id) + ids[id] = 1; + return scripts[path] == 2 && callback(); + } + scripts[path] = 1; + if (id) + ids[id] = 1; + create(!validBase.test(path) && scriptpath ? scriptpath + path + '.js' : path, callback); + }); + }, 0); + return $script; + } + + function create(path, fn) { + var el = doc.createElement('script'), + loaded = f; + el.onload = el.onerror = el[onreadystatechange] = function () { + if ((el[readyState] && !(/^c|loade/.test(el[readyState]))) || loaded) return; + el.onload = el[onreadystatechange] = null; + loaded = 1; + scripts[path] = 2; + fn(); + }; + el.async = 1; + el.src = path; + head.insertBefore(el, head.firstChild); + } + + $script.get = create; + + $script.order = function (scripts, id, done) { + (function callback(s) { + s = scripts.shift(); + if (!scripts.length) $script(s, id, done); + else $script(s, callback); + }()); + }; + + $script.path = function (p) { + scriptpath = p; + }; + // This function is a tangled mess of conciseness, so suppress warnings here + /* jshint -W030 */ + $script.ready = function (deps, ready, req) { + deps = deps[push] ? deps : [deps]; + var missing = []; + !each(deps, function (dep) { + list[dep] || missing[push](dep); + }) && every(deps, function (dep) { + return list[dep]; + }) ? + ready() : ! function (key) { + delay[key] = delay[key] || []; + delay[key][push](ready); + req && req(missing); + }(deps.join('|')); + return $script; + }; + /* jshint +W030 */ + $script.noConflict = function () { + win.$script = old; + return this; + }; + + return $script; + })(); + + var modules = []; + var validBase = /^(https?|file):\/\//; + for (var i in moduleMap) { + if (validBase.test(i)) + modules.push(i); + else + modules.push(modulesRepository + '/' + i.toLowerCase() + '-' + moduleMap[i].toLowerCase() + '.js'); + } + + $script(modules, function () { + if (oncomplete) oncomplete(); + }); + } +}); + +},{"./core.js":9}],18:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +/**@ + * #Crafty.math + * @category 2D + * Static functions. + */ +Crafty.math = { + /**@ + * #Crafty.math.abs + * @comp Crafty.math + * @sign public this Crafty.math.abs(Number n) + * @param n - Some value. + * @return Absolute value. + * + * Returns the absolute value. + */ + abs: function (x) { + return x < 0 ? -x : x; + }, + + /**@ + * #Crafty.math.amountOf + * @comp Crafty.math + * @sign public Number Crafty.math.amountOf(Number checkValue, Number minValue, Number maxValue) + * @param checkValue - Value that should checked with minimum and maximum. + * @param minValue - Minimum value to check. + * @param maxValue - Maximum value to check. + * @return Amount of checkValue compared to minValue and maxValue. + * + * Returns the amount of how much a checkValue is more like minValue (=0) + * or more like maxValue (=1) + */ + amountOf: function (checkValue, minValue, maxValue) { + if (minValue < maxValue) + return (checkValue - minValue) / (maxValue - minValue); + else + return (checkValue - maxValue) / (minValue - maxValue); + }, + + + /**@ + * #Crafty.math.clamp + * @comp Crafty.math + * @sign public Number Crafty.math.clamp(Number value, Number min, Number max) + * @param value - A value. + * @param max - Maximum that value can be. + * @param min - Minimum that value can be. + * @return The value between minimum and maximum. + * + * Restricts a value to be within a specified range. + */ + clamp: function (value, min, max) { + if (value > max) + return max; + else if (value < min) + return min; + else + return value; + }, + + /**@ + * #Crafty.math.degToRad + * Converts angle from degree to radian. + * @comp Crafty.math + * @param angleInDeg - The angle in degree. + * @return The angle in radian. + */ + degToRad: function (angleInDeg) { + return angleInDeg * Math.PI / 180; + }, + + /**@ + * #Crafty.math.distance + * @comp Crafty.math + * @sign public Number Crafty.math.distance(Number x1, Number y1, Number x2, Number y2) + * @param x1 - First x coordinate. + * @param y1 - First y coordinate. + * @param x2 - Second x coordinate. + * @param y2 - Second y coordinate. + * @return The distance between the two points. + * + * Distance between two points. + */ + distance: function (x1, y1, x2, y2) { + var squaredDistance = Crafty.math.squaredDistance(x1, y1, x2, y2); + return Math.sqrt(parseFloat(squaredDistance)); + }, + + /**@ + * #Crafty.math.lerp + * @comp Crafty.math + * @sign public Number Crafty.math.lerp(Number value1, Number value2, Number amount) + * @param value1 - One value. + * @param value2 - Another value. + * @param amount - Amount of value2 to value1. + * @return Linear interpolated value. + * + * Linear interpolation. Passing amount with a value of 0 will cause value1 to be returned, + * a value of 1 will cause value2 to be returned. + */ + lerp: function (value1, value2, amount) { + return value1 + (value2 - value1) * amount; + }, + + /**@ + * #Crafty.math.negate + * @comp Crafty.math + * @sign public Number Crafty.math.negate(Number percent) + * @param percent - If you pass 1 a -1 will be returned. If you pass 0 a 1 will be returned. + * @return 1 or -1. + * + * Returnes "randomly" -1. + */ + negate: function (percent) { + if (Math.random() < percent) + return -1; + else + return 1; + }, + + /**@ + * #Crafty.math.radToDeg + * @comp Crafty.math + * @sign public Number Crafty.math.radToDeg(Number angle) + * @param angleInRad - The angle in radian. + * @return The angle in degree. + * + * Converts angle from radian to degree. + */ + radToDeg: function (angleInRad) { + return angleInRad * 180 / Math.PI; + }, + + /**@ + * #Crafty.math.randomElementOfArray + * @comp Crafty.math + * @sign public Object Crafty.math.randomElementOfArray(Array array) + * @param array - A specific array. + * @return A random element of a specific array. + * + * Returns a random element of a specific array. + */ + randomElementOfArray: function (array) { + return array[Math.floor(array.length * Math.random())]; + }, + + /**@ + * #Crafty.math.randomInt + * @comp Crafty.math + * @sign public Number Crafty.math.randomInt(Number start, Number end) + * @param start - Smallest int value that can be returned. + * @param end - Biggest int value that can be returned. + * @return A random int. + * + * Returns a random int in within a specific range. + */ + randomInt: function (start, end) { + return start + Math.floor((1 + end - start) * Math.random()); + }, + + /**@ + * #Crafty.math.randomNumber + * @comp Crafty.math + * @sign public Number Crafty.math.randomInt(Number start, Number end) + * @param start - Smallest number value that can be returned. + * @param end - Biggest number value that can be returned. + * @return A random number. + * + * Returns a random number in within a specific range. + */ + randomNumber: function (start, end) { + return start + (end - start) * Math.random(); + }, + + /**@ + * #Crafty.math.squaredDistance + * @comp Crafty.math + * @sign public Number Crafty.math.squaredDistance(Number x1, Number y1, Number x2, Number y2) + * @param x1 - First x coordinate. + * @param y1 - First y coordinate. + * @param x2 - Second x coordinate. + * @param y2 - Second y coordinate. + * @return The squared distance between the two points. + * + * Squared distance between two points. + */ + squaredDistance: function (x1, y1, x2, y2) { + return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + }, + + /**@ + * #Crafty.math.withinRange + * @comp Crafty.math + * @sign public Boolean Crafty.math.withinRange(Number value, Number min, Number max) + * @param value - The specific value. + * @param min - Minimum value. + * @param max - Maximum value. + * @return Returns true if value is within a specific range. + * + * Check if a value is within a specific range. + */ + withinRange: function (value, min, max) { + return (value >= min && value <= max); + } +}; + +Crafty.math.Vector2D = (function () { + /**@ + * #Crafty.math.Vector2D + * @category 2D + * @class This is a general purpose 2D vector class + * + * Vector2D uses the following form: + * + * + * @public + * @sign public {Vector2D} Vector2D(); + * @sign public {Vector2D} Vector2D(Vector2D); + * @sign public {Vector2D} Vector2D(Number, Number); + * @param {Vector2D|Number=0} x + * @param {Number=0} y + */ + + function Vector2D(x, y) { + if (x instanceof Vector2D) { + this.x = x.x; + this.y = x.y; + } else if (arguments.length === 2) { + this.x = x; + this.y = y; + } else if (arguments.length > 0) + throw "Unexpected number of arguments for Vector2D()"; + } // class Vector2D + + Vector2D.prototype.x = 0; + Vector2D.prototype.y = 0; + + /**@ + * #.add + * @comp Crafty.math.Vector2D + * + * Adds the passed vector to this vector + * + * @public + * @sign public {Vector2D} add(Vector2D); + * @param {vector2D} vecRH + * @returns {Vector2D} this after adding + */ + Vector2D.prototype.add = function (vecRH) { + this.x += vecRH.x; + this.y += vecRH.y; + return this; + }; // add + + /**@ + * #.angleBetween + * @comp Crafty.math.Vector2D + * + * Calculates the angle between the passed vector and this vector, using <0,0> as the point of reference. + * Angles returned have the range (−π, π]. + * + * @public + * @sign public {Number} angleBetween(Vector2D); + * @param {Vector2D} vecRH + * @returns {Number} the angle between the two vectors in radians + */ + Vector2D.prototype.angleBetween = function (vecRH) { + return Math.atan2(this.x * vecRH.y - this.y * vecRH.x, this.x * vecRH.x + this.y * vecRH.y); + }; // angleBetween + + /**@ + * #.angleTo + * @comp Crafty.math.Vector2D + * + * Calculates the angle to the passed vector from this vector, using this vector as the point of reference. + * + * @public + * @sign public {Number} angleTo(Vector2D); + * @param {Vector2D} vecRH + * @returns {Number} the angle to the passed vector in radians + */ + Vector2D.prototype.angleTo = function (vecRH) { + return Math.atan2(vecRH.y - this.y, vecRH.x - this.x); + }; + + /**@ + * #.clone + * @comp Crafty.math.Vector2D + * + * Creates and exact, numeric copy of this vector + * + * @public + * @sign public {Vector2D} clone(); + * @returns {Vector2D} the new vector + */ + Vector2D.prototype.clone = function () { + return new Vector2D(this); + }; // clone + + /**@ + * #.distance + * @comp Crafty.math.Vector2D + * + * Calculates the distance from this vector to the passed vector. + * + * @public + * @sign public {Number} distance(Vector2D); + * @param {Vector2D} vecRH + * @returns {Number} the distance between the two vectors + */ + Vector2D.prototype.distance = function (vecRH) { + return Math.sqrt((vecRH.x - this.x) * (vecRH.x - this.x) + (vecRH.y - this.y) * (vecRH.y - this.y)); + }; // distance + + /**@ + * #.distanceSq + * @comp Crafty.math.Vector2D + * + * Calculates the squared distance from this vector to the passed vector. + * This function avoids calculating the square root, thus being slightly faster than .distance( ). + * + * @public + * @sign public {Number} distanceSq(Vector2D); + * @param {Vector2D} vecRH + * @returns {Number} the squared distance between the two vectors + * @see .distance + */ + Vector2D.prototype.distanceSq = function (vecRH) { + return (vecRH.x - this.x) * (vecRH.x - this.x) + (vecRH.y - this.y) * (vecRH.y - this.y); + }; // distanceSq + + /**@ + * #.divide + * @comp Crafty.math.Vector2D + * + * Divides this vector by the passed vector. + * + * @public + * @sign public {Vector2D} divide(Vector2D); + * @param {Vector2D} vecRH + * @returns {Vector2D} this vector after dividing + */ + Vector2D.prototype.divide = function (vecRH) { + this.x /= vecRH.x; + this.y /= vecRH.y; + return this; + }; // divide + + /**@ + * #.dotProduct + * @comp Crafty.math.Vector2D + * + * Calculates the dot product of this and the passed vectors + * + * @public + * @sign public {Number} dotProduct(Vector2D); + * @param {Vector2D} vecRH + * @returns {Number} the resultant dot product + */ + Vector2D.prototype.dotProduct = function (vecRH) { + return this.x * vecRH.x + this.y * vecRH.y; + }; // dotProduct + + /**@ + * #.equals + * @comp Crafty.math.Vector2D + * + * Determines if this vector is numerically equivalent to the passed vector. + * + * @public + * @sign public {Boolean} equals(Vector2D); + * @param {Vector2D} vecRH + * @returns {Boolean} true if the vectors are equivalent + */ + Vector2D.prototype.equals = function (vecRH) { + return vecRH instanceof Vector2D && + this.x == vecRH.x && this.y == vecRH.y; + }; // equals + + /**@ + * #.getNormal + * @comp Crafty.math.Vector2D + * + * Calculates a new right-handed normal vector for the line created by this and the passed vectors. + * + * @public + * @sign public {Vector2D} getNormal([Vector2D]); + * @param {Vector2D=<0,0>} [vecRH] + * @returns {Vector2D} the new normal vector + */ + Vector2D.prototype.getNormal = function (vecRH) { + if (vecRH === undefined) + return new Vector2D(-this.y, this.x); // assume vecRH is <0, 0> + return new Vector2D(vecRH.y - this.y, this.x - vecRH.x).normalize(); + }; // getNormal + + /**@ + * #.isZero + * @comp Crafty.math.Vector2D + * + * Determines if this vector is equal to <0,0> + * + * @public + * @sign public {Boolean} isZero(); + * @returns {Boolean} true if this vector is equal to <0,0> + */ + Vector2D.prototype.isZero = function () { + return this.x === 0 && this.y === 0; + }; // isZero + + /**@ + * #.magnitude + * @comp Crafty.math.Vector2D + * + * Calculates the magnitude of this vector. + * Note: Function objects in JavaScript already have a 'length' member, hence the use of magnitude instead. + * + * @public + * @sign public {Number} magnitude(); + * @returns {Number} the magnitude of this vector + */ + Vector2D.prototype.magnitude = function () { + return Math.sqrt(this.x * this.x + this.y * this.y); + }; // magnitude + + /**@ + * #.magnitudeSq + * @comp Crafty.math.Vector2D + * + * Calculates the square of the magnitude of this vector. + * This function avoids calculating the square root, thus being slightly faster than .magnitude( ). + * + * @public + * @sign public {Number} magnitudeSq(); + * @returns {Number} the square of the magnitude of this vector + * @see .magnitude + */ + Vector2D.prototype.magnitudeSq = function () { + return this.x * this.x + this.y * this.y; + }; // magnitudeSq + + /**@ + * #.multiply + * @comp Crafty.math.Vector2D + * + * Multiplies this vector by the passed vector + * + * @public + * @sign public {Vector2D} multiply(Vector2D); + * @param {Vector2D} vecRH + * @returns {Vector2D} this vector after multiplying + */ + Vector2D.prototype.multiply = function (vecRH) { + this.x *= vecRH.x; + this.y *= vecRH.y; + return this; + }; // multiply + + /**@ + * #.negate + * @comp Crafty.math.Vector2D + * + * Negates this vector (ie. <-x,-y>) + * + * @public + * @sign public {Vector2D} negate(); + * @returns {Vector2D} this vector after negation + */ + Vector2D.prototype.negate = function () { + this.x = -this.x; + this.y = -this.y; + return this; + }; // negate + + /**@ + * #.normalize + * @comp Crafty.math.Vector2D + * + * Normalizes this vector (scales the vector so that its new magnitude is 1) + * For vectors where magnitude is 0, <1,0> is returned. + * + * @public + * @sign public {Vector2D} normalize(); + * @returns {Vector2D} this vector after normalization + */ + Vector2D.prototype.normalize = function () { + var lng = Math.sqrt(this.x * this.x + this.y * this.y); + + if (lng === 0) { + // default due East + this.x = 1; + this.y = 0; + } else { + this.x /= lng; + this.y /= lng; + } // else + + return this; + }; // normalize + + /**@ + * #.scale + * @comp Crafty.math.Vector2D + * + * Scales this vector by the passed amount(s) + * If scalarY is omitted, scalarX is used for both axes + * + * @public + * @sign public {Vector2D} scale(Number[, Number]); + * @param {Number} scalarX + * @param {Number} [scalarY] + * @returns {Vector2D} this after scaling + */ + Vector2D.prototype.scale = function (scalarX, scalarY) { + if (scalarY === undefined) + scalarY = scalarX; + + this.x *= scalarX; + this.y *= scalarY; + + return this; + }; // scale + + /**@ + * #.scaleToMagnitude + * @comp Crafty.math.Vector2D + * + * Scales this vector such that its new magnitude is equal to the passed value. + * + * @public + * @sign public {Vector2D} scaleToMagnitude(Number); + * @param {Number} mag + * @returns {Vector2D} this vector after scaling + */ + Vector2D.prototype.scaleToMagnitude = function (mag) { + var k = mag / this.magnitude(); + this.x *= k; + this.y *= k; + return this; + }; // scaleToMagnitude + + /**@ + * #.setValues + * @comp Crafty.math.Vector2D + * + * Sets the values of this vector using a passed vector or pair of numbers. + * + * @public + * @sign public {Vector2D} setValues(Vector2D); + * @sign public {Vector2D} setValues(Number, Number); + * @param {Number|Vector2D} x + * @param {Number} y + * @returns {Vector2D} this vector after setting of values + */ + Vector2D.prototype.setValues = function (x, y) { + if (x instanceof Vector2D) { + this.x = x.x; + this.y = x.y; + } else { + this.x = x; + this.y = y; + } // else + + return this; + }; // setValues + + /**@ + * #.subtract + * @comp Crafty.math.Vector2D + * + * Subtracts the passed vector from this vector. + * + * @public + * @sign public {Vector2D} subtract(Vector2D); + * @param {Vector2D} vecRH + * @returns {vector2D} this vector after subtracting + */ + Vector2D.prototype.subtract = function (vecRH) { + this.x -= vecRH.x; + this.y -= vecRH.y; + return this; + }; // subtract + + /**@ + * #.toString + * @comp Crafty.math.Vector2D + * + * Returns a string representation of this vector. + * + * @public + * @sign public {String} toString(); + * @returns {String} + */ + Vector2D.prototype.toString = function () { + return "Vector2D(" + this.x + ", " + this.y + ")"; + }; // toString + + /**@ + * #.translate + * @comp Crafty.math.Vector2D + * + * Translates (moves) this vector by the passed amounts. + * If dy is omitted, dx is used for both axes. + * + * @public + * @sign public {Vector2D} translate(Number[, Number]); + * @param {Number} dx + * @param {Number} [dy] + * @returns {Vector2D} this vector after translating + */ + Vector2D.prototype.translate = function (dx, dy) { + if (dy === undefined) + dy = dx; + + this.x += dx; + this.y += dy; + + return this; + }; // translate + + /**@ + * #.tripleProduct + * @comp Crafty.math.Vector2D + * + * Calculates the triple product of three vectors. + * triple vector product = b(a•c) - a(b•c) + * + * @public + * @static + * @sign public {Vector2D} tripleProduct(Vector2D, Vector2D, Vector2D); + * @param {Vector2D} a + * @param {Vector2D} b + * @param {Vector2D} c + * @return {Vector2D} the triple product as a new vector + */ + Vector2D.tripleProduct = function (a, b, c) { + var ac = a.dotProduct(c); + var bc = b.dotProduct(c); + return new Crafty.math.Vector2D(b.x * ac - a.x * bc, b.y * ac - a.y * bc); + }; + + return Vector2D; +})(); + +Crafty.math.Matrix2D = (function () { + /**@ + * #Crafty.math.Matrix2D + * @category 2D + * + * @class This is a 2D Matrix2D class. It is 3x3 to allow for affine transformations in 2D space. + * The third row is always assumed to be [0, 0, 1]. + * + * Matrix2D uses the following form, as per the whatwg.org specifications for canvas.transform(): + * [a, c, e] + * [b, d, f] + * [0, 0, 1] + * + * @public + * @sign public {Matrix2D} new Matrix2D(); + * @sign public {Matrix2D} new Matrix2D(Matrix2D); + * @sign public {Matrix2D} new Matrix2D(Number, Number, Number, Number, Number, Number); + * @param {Matrix2D|Number=1} a + * @param {Number=0} b + * @param {Number=0} c + * @param {Number=1} d + * @param {Number=0} e + * @param {Number=0} f + */ + Matrix2D = function (a, b, c, d, e, f) { + if (a instanceof Matrix2D) { + this.a = a.a; + this.b = a.b; + this.c = a.c; + this.d = a.d; + this.e = a.e; + this.f = a.f; + } else if (arguments.length === 6) { + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.e = e; + this.f = f; + } else if (arguments.length > 0) + throw "Unexpected number of arguments for Matrix2D()"; + }; // class Matrix2D + + Matrix2D.prototype.a = 1; + Matrix2D.prototype.b = 0; + Matrix2D.prototype.c = 0; + Matrix2D.prototype.d = 1; + Matrix2D.prototype.e = 0; + Matrix2D.prototype.f = 0; + + /**@ + * #.apply + * @comp Crafty.math.Matrix2D + * + * Applies the matrix transformations to the passed object + * + * @public + * @sign public {Vector2D} apply(Vector2D); + * @param {Vector2D} vecRH - vector to be transformed + * @returns {Vector2D} the passed vector object after transforming + */ + Matrix2D.prototype.apply = function (vecRH) { + // I'm not sure of the best way for this function to be implemented. Ideally + // support for other objects (rectangles, polygons, etc) should be easily + // addable in the future. Maybe a function (apply) is not the best way to do + // this...? + + var tmpX = vecRH.x; + vecRH.x = tmpX * this.a + vecRH.y * this.c + this.e; + vecRH.y = tmpX * this.b + vecRH.y * this.d + this.f; + // no need to homogenize since the third row is always [0, 0, 1] + + return vecRH; + }; // apply + + /**@ + * #.clone + * @comp Crafty.math.Matrix2D + * + * Creates an exact, numeric copy of the current matrix + * + * @public + * @sign public {Matrix2D} clone(); + * @returns {Matrix2D} + */ + Matrix2D.prototype.clone = function () { + return new Matrix2D(this); + }; // clone + + /**@ + * #.combine + * @comp Crafty.math.Matrix2D + * + * Multiplies this matrix with another, overriding the values of this matrix. + * The passed matrix is assumed to be on the right-hand side. + * + * @public + * @sign public {Matrix2D} combine(Matrix2D); + * @param {Matrix2D} mtrxRH + * @returns {Matrix2D} this matrix after combination + */ + Matrix2D.prototype.combine = function (mtrxRH) { + var tmp = this.a; + this.a = tmp * mtrxRH.a + this.b * mtrxRH.c; + this.b = tmp * mtrxRH.b + this.b * mtrxRH.d; + tmp = this.c; + this.c = tmp * mtrxRH.a + this.d * mtrxRH.c; + this.d = tmp * mtrxRH.b + this.d * mtrxRH.d; + tmp = this.e; + this.e = tmp * mtrxRH.a + this.f * mtrxRH.c + mtrxRH.e; + this.f = tmp * mtrxRH.b + this.f * mtrxRH.d + mtrxRH.f; + return this; + }; // combine + + /**@ + * #.equals + * @comp Crafty.math.Matrix2D + * + * Checks for the numeric equality of this matrix versus another. + * + * @public + * @sign public {Boolean} equals(Matrix2D); + * @param {Matrix2D} mtrxRH + * @returns {Boolean} true if the two matrices are numerically equal + */ + Matrix2D.prototype.equals = function (mtrxRH) { + return mtrxRH instanceof Matrix2D && + this.a == mtrxRH.a && this.b == mtrxRH.b && this.c == mtrxRH.c && + this.d == mtrxRH.d && this.e == mtrxRH.e && this.f == mtrxRH.f; + }; // equals + + /**@ + * #.determinant + * @comp Crafty.math.Matrix2D + * + * Calculates the determinant of this matrix + * + * @public + * @sign public {Number} determinant(); + * @returns {Number} det(this matrix) + */ + Matrix2D.prototype.determinant = function () { + return this.a * this.d - this.b * this.c; + }; // determinant + + /**@ + * #.invert + * @comp Crafty.math.Matrix2D + * + * Inverts this matrix if possible + * + * @public + * @sign public {Matrix2D} invert(); + * @returns {Matrix2D} this inverted matrix or the original matrix on failure + * @see .isInvertible + */ + Matrix2D.prototype.invert = function () { + var det = this.determinant(); + + // matrix is invertible if its determinant is non-zero + if (det !== 0) { + var old = { + a: this.a, + b: this.b, + c: this.c, + d: this.d, + e: this.e, + f: this.f + }; + this.a = old.d / det; + this.b = -old.b / det; + this.c = -old.c / det; + this.d = old.a / det; + this.e = (old.c * old.f - old.e * old.d) / det; + this.f = (old.e * old.b - old.a * old.f) / det; + } // if + + return this; + }; // invert + + /**@ + * #.isIdentity + * @comp Crafty.math.Matrix2D + * + * Returns true if this matrix is the identity matrix + * + * @public + * @sign public {Boolean} isIdentity(); + * @returns {Boolean} + */ + Matrix2D.prototype.isIdentity = function () { + return this.a === 1 && this.b === 0 && this.c === 0 && this.d === 1 && this.e === 0 && this.f === 0; + }; // isIdentity + + /**@ + * #.isInvertible + * @comp Crafty.math.Matrix2D + * + * Determines is this matrix is invertible. + * + * @public + * @sign public {Boolean} isInvertible(); + * @returns {Boolean} true if this matrix is invertible + * @see .invert + */ + Matrix2D.prototype.isInvertible = function () { + return this.determinant() !== 0; + }; // isInvertible + + /**@ + * #.preRotate + * @comp Crafty.math.Matrix2D + * + * Applies a counter-clockwise pre-rotation to this matrix + * + * @public + * @sign public {Matrix2D} preRotate(Number); + * @param {number} rads - angle to rotate in radians + * @returns {Matrix2D} this matrix after pre-rotation + */ + Matrix2D.prototype.preRotate = function (rads) { + var nCos = Math.cos(rads); + var nSin = Math.sin(rads); + + var tmp = this.a; + this.a = nCos * tmp - nSin * this.b; + this.b = nSin * tmp + nCos * this.b; + tmp = this.c; + this.c = nCos * tmp - nSin * this.d; + this.d = nSin * tmp + nCos * this.d; + + return this; + }; // preRotate + + /**@ + * #.preScale + * @comp Crafty.math.Matrix2D + * + * Applies a pre-scaling to this matrix + * + * @public + * @sign public {Matrix2D} preScale(Number[, Number]); + * @param {Number} scalarX + * @param {Number} [scalarY] scalarX is used if scalarY is undefined + * @returns {Matrix2D} this after pre-scaling + */ + Matrix2D.prototype.preScale = function (scalarX, scalarY) { + if (scalarY === undefined) + scalarY = scalarX; + + this.a *= scalarX; + this.b *= scalarY; + this.c *= scalarX; + this.d *= scalarY; + + return this; + }; // preScale + + /**@ + * #.preTranslate + * @comp Crafty.math.Matrix2D + * + * Applies a pre-translation to this matrix + * + * @public + * @sign public {Matrix2D} preTranslate(Vector2D); + * @sign public {Matrix2D} preTranslate(Number, Number); + * @param {Number|Vector2D} dx + * @param {Number} dy + * @returns {Matrix2D} this matrix after pre-translation + */ + Matrix2D.prototype.preTranslate = function (dx, dy) { + if (typeof dx === "number") { + this.e += dx; + this.f += dy; + } else { + this.e += dx.x; + this.f += dx.y; + } // else + + return this; + }; // preTranslate + + /**@ + * #.rotate + * @comp Crafty.math.Matrix2D + * + * Applies a counter-clockwise post-rotation to this matrix + * + * @public + * @sign public {Matrix2D} rotate(Number); + * @param {Number} rads - angle to rotate in radians + * @returns {Matrix2D} this matrix after rotation + */ + Matrix2D.prototype.rotate = function (rads) { + var nCos = Math.cos(rads); + var nSin = Math.sin(rads); + + var tmp = this.a; + this.a = nCos * tmp - nSin * this.b; + this.b = nSin * tmp + nCos * this.b; + tmp = this.c; + this.c = nCos * tmp - nSin * this.d; + this.d = nSin * tmp + nCos * this.d; + tmp = this.e; + this.e = nCos * tmp - nSin * this.f; + this.f = nSin * tmp + nCos * this.f; + + return this; + }; // rotate + + /**@ + * #.scale + * @comp Crafty.math.Matrix2D + * + * Applies a post-scaling to this matrix + * + * @public + * @sign public {Matrix2D} scale(Number[, Number]); + * @param {Number} scalarX + * @param {Number} [scalarY] scalarX is used if scalarY is undefined + * @returns {Matrix2D} this after post-scaling + */ + Matrix2D.prototype.scale = function (scalarX, scalarY) { + if (scalarY === undefined) + scalarY = scalarX; + + this.a *= scalarX; + this.b *= scalarY; + this.c *= scalarX; + this.d *= scalarY; + this.e *= scalarX; + this.f *= scalarY; + + return this; + }; // scale + + /**@ + * #.setValues + * @comp Crafty.math.Matrix2D + * + * Sets the values of this matrix + * + * @public + * @sign public {Matrix2D} setValues(Matrix2D); + * @sign public {Matrix2D} setValues(Number, Number, Number, Number, Number, Number); + * @param {Matrix2D|Number} a + * @param {Number} b + * @param {Number} c + * @param {Number} d + * @param {Number} e + * @param {Number} f + * @returns {Matrix2D} this matrix containing the new values + */ + Matrix2D.prototype.setValues = function (a, b, c, d, e, f) { + if (a instanceof Matrix2D) { + this.a = a.a; + this.b = a.b; + this.c = a.c; + this.d = a.d; + this.e = a.e; + this.f = a.f; + } else { + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.e = e; + this.f = f; + } // else + + return this; + }; // setValues + + /**@ + * #.toString + * @comp Crafty.math.Matrix2D + * + * Returns the string representation of this matrix. + * + * @public + * @sign public {String} toString(); + * @returns {String} + */ + Matrix2D.prototype.toString = function () { + return "Matrix2D([" + this.a + ", " + this.c + ", " + this.e + + "] [" + this.b + ", " + this.d + ", " + this.f + "] [0, 0, 1])"; + }; // toString + + /**@ + * #.translate + * @comp Crafty.math.Matrix2D + * + * Applies a post-translation to this matrix + * + * @public + * @sign public {Matrix2D} translate(Vector2D); + * @sign public {Matrix2D} translate(Number, Number); + * @param {Number|Vector2D} dx + * @param {Number} dy + * @returns {Matrix2D} this matrix after post-translation + */ + Matrix2D.prototype.translate = function (dx, dy) { + if (typeof dx === "number") { + this.e += this.a * dx + this.c * dy; + this.f += this.b * dx + this.d * dy; + } else { + this.e += this.a * dx.x + this.c * dx.y; + this.f += this.b * dx.x + this.d * dx.y; + } // else + + return this; + }; // translate + + return Matrix2D; +})(); +},{"./core.js":9}],19:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +/**@ + * #Particles + * @category Graphics + * @trigger ParticleEnd - when the particle animation has finished + * Based on Parcycle by Mr. Speaker, licensed under the MIT, Ported by Leo Koppelkamm + * **This is canvas only & won't do anything if the browser doesn't support it!** + * To see how this works take a look in https://github.com/craftyjs/Crafty/blob/master/src/particles.js + */ +Crafty.c("Particles", { + init: function () { + //We need to clone it + this._Particles = Crafty.clone(this._Particles); + this._Particles.parentEntity = this; + }, + + /**@ + * #.particles + * @comp Particles + * @sign public this .particles(Object options) + * @param options - Map of options that specify the behavior and look of the particles. + * + * @example + * ~~~ + * var options = { + * maxParticles: 150, + * size: 18, + * sizeRandom: 4, + * speed: 1, + * speedRandom: 1.2, + * // Lifespan in frames + * lifeSpan: 29, + * lifeSpanRandom: 7, + * // Angle is calculated clockwise: 12pm is 0deg, 3pm is 90deg etc. + * angle: 65, + * angleRandom: 34, + * startColour: [255, 131, 0, 1], + * startColourRandom: [48, 50, 45, 0], + * endColour: [245, 35, 0, 0], + * endColourRandom: [60, 60, 60, 0], + * // Only applies when fastMode is off, specifies how sharp the gradients are drawn + * sharpness: 20, + * sharpnessRandom: 10, + * // Random spread from origin + * spread: 10, + * // How many frames should this last + * duration: -1, + * // Will draw squares instead of circle gradients + * fastMode: false, + * gravity: { x: 0, y: 0.1 }, + * // sensible values are 0-3 + * jitter: 0 + * } + * + * Crafty.e("2D,Canvas,Particles").particles(options); + * ~~~ + */ + particles: function (options) { + + if (!Crafty.support.canvas || Crafty.deactivateParticles) return this; + + //If we drew on the main canvas, we'd have to redraw + //potentially huge sections of the screen every frame + //So we create a separate canvas, where we only have to redraw + //the changed particles. + var c, ctx, relativeX, relativeY, bounding; + + c = document.createElement("canvas"); + c.width = Crafty.viewport.width; + c.height = Crafty.viewport.height; + c.style.position = 'absolute'; + c.style.left = "0px"; + c.style.top = "0px"; + + Crafty.stage.elem.appendChild(c); + + ctx = c.getContext('2d'); + + this._Particles.init(options); + + // Clean up the DOM when this component is removed + this.bind('Remove', function () { + Crafty.stage.elem.removeChild(c); + }).bind("RemoveComponent", function (id) { + if (id === "particles") + Crafty.stage.elem.removeChild(c); + }); + + relativeX = this.x + Crafty.viewport.x; + relativeY = this.y + Crafty.viewport.y; + this._Particles.position = this._Particles.vectorHelpers.create(relativeX, relativeY); + + var oldViewport = { + x: Crafty.viewport.x, + y: Crafty.viewport.y + }; + + this.bind('EnterFrame', function () { + relativeX = this.x + Crafty.viewport.x; + relativeY = this.y + Crafty.viewport.y; + this._Particles.viewportDelta = { + x: Crafty.viewport.x - oldViewport.x, + y: Crafty.viewport.y - oldViewport.y + }; + + oldViewport = { + x: Crafty.viewport.x, + y: Crafty.viewport.y + }; + + this._Particles.position = this._Particles.vectorHelpers.create(relativeX, relativeY); + + //Selective clearing + if (typeof Crafty.DrawManager.boundingRect == 'function') { + bounding = Crafty.DrawManager.boundingRect(this._Particles.register); + if (bounding) ctx.clearRect(bounding._x, bounding._y, bounding._w, bounding._h); + } else { + ctx.clearRect(0, 0, Crafty.viewport.width, Crafty.viewport.height); + } + + //This updates all particle colors & positions + this._Particles.update(); + + //This renders the updated particles + this._Particles.render(ctx); + }); + return this; + }, + _Particles: { + presets: { + maxParticles: 150, + size: 18, + sizeRandom: 4, + speed: 1, + speedRandom: 1.2, + // Lifespan in frames + lifeSpan: 29, + lifeSpanRandom: 7, + // Angle is calculated clockwise: 12pm is 0deg, 3pm is 90deg etc. + angle: 65, + angleRandom: 34, + startColour: [255, 131, 0, 1], + startColourRandom: [48, 50, 45, 0], + endColour: [245, 35, 0, 0], + endColourRandom: [60, 60, 60, 0], + // Only applies when fastMode is off, specifies how sharp the gradients are drawn + sharpness: 20, + sharpnessRandom: 10, + // Random spread from origin + spread: 10, + // How many frames should this last + duration: -1, + // Will draw squares instead of circle gradients + fastMode: false, + gravity: { + x: 0, + y: 0.1 + }, + // sensible values are 0-3 + jitter: 0, + + //Don't modify the following + particles: [], + active: true, + particleCount: 0, + elapsedFrames: 0, + emissionRate: 0, + emitCounter: 0, + particleIndex: 0 + }, + + + init: function (options) { + this.position = this.vectorHelpers.create(0, 0); + if (typeof options == 'undefined') options = {}; + + //Create current config by merging given options and presets. + for (var key in this.presets) { + if (typeof options[key] != 'undefined') this[key] = options[key]; + else this[key] = this.presets[key]; + } + + this.emissionRate = this.maxParticles / this.lifeSpan; + this.positionRandom = this.vectorHelpers.create(this.spread, this.spread); + }, + + addParticle: function () { + if (this.particleCount == this.maxParticles) { + return false; + } + + // Take the next particle out of the particle pool we have created and initialize it + var particle = new this.particle(this.vectorHelpers); + this.initParticle(particle); + this.particles[this.particleCount] = particle; + // Increment the particle count + this.particleCount++; + + return true; + }, + RANDM1TO1: function () { + return Math.random() * 2 - 1; + }, + initParticle: function (particle) { + particle.position.x = this.position.x + this.positionRandom.x * this.RANDM1TO1(); + particle.position.y = this.position.y + this.positionRandom.y * this.RANDM1TO1(); + + var newAngle = (this.angle + this.angleRandom * this.RANDM1TO1()) * (Math.PI / 180); // convert to radians + var vector = this.vectorHelpers.create(Math.sin(newAngle), -Math.cos(newAngle)); // Could move to lookup for speed + var vectorSpeed = this.speed + this.speedRandom * this.RANDM1TO1(); + particle.direction = this.vectorHelpers.multiply(vector, vectorSpeed); + + particle.size = this.size + this.sizeRandom * this.RANDM1TO1(); + particle.size = particle.size < 0 ? 0 : ~~particle.size; + particle.timeToLive = this.lifeSpan + this.lifeSpanRandom * this.RANDM1TO1(); + + particle.sharpness = this.sharpness + this.sharpnessRandom * this.RANDM1TO1(); + particle.sharpness = particle.sharpness > 100 ? 100 : particle.sharpness < 0 ? 0 : particle.sharpness; + // internal circle gradient size - affects the sharpness of the radial gradient + particle.sizeSmall = ~~ ((particle.size / 200) * particle.sharpness); //(size/2/100) + var start = [ + this.startColour[0] + this.startColourRandom[0] * this.RANDM1TO1(), + this.startColour[1] + this.startColourRandom[1] * this.RANDM1TO1(), + this.startColour[2] + this.startColourRandom[2] * this.RANDM1TO1(), + this.startColour[3] + this.startColourRandom[3] * this.RANDM1TO1() + ]; + + var end = [ + this.endColour[0] + this.endColourRandom[0] * this.RANDM1TO1(), + this.endColour[1] + this.endColourRandom[1] * this.RANDM1TO1(), + this.endColour[2] + this.endColourRandom[2] * this.RANDM1TO1(), + this.endColour[3] + this.endColourRandom[3] * this.RANDM1TO1() + ]; + + particle.colour = start; + particle.deltaColour[0] = (end[0] - start[0]) / particle.timeToLive; + particle.deltaColour[1] = (end[1] - start[1]) / particle.timeToLive; + particle.deltaColour[2] = (end[2] - start[2]) / particle.timeToLive; + particle.deltaColour[3] = (end[3] - start[3]) / particle.timeToLive; + }, + update: function () { + if (this.active && this.emissionRate > 0) { + var rate = 1 / this.emissionRate; + this.emitCounter++; + while (this.particleCount < this.maxParticles && this.emitCounter > rate) { + this.addParticle(); + this.emitCounter -= rate; + } + this.elapsedFrames++; + if (this.duration != -1 && this.duration < this.elapsedFrames) { + this.stop(); + } + } + + this.particleIndex = 0; + this.register = []; + var draw; + while (this.particleIndex < this.particleCount) { + + var currentParticle = this.particles[this.particleIndex]; + + // If the current particle is alive then update it + if (currentParticle.timeToLive > 0) { + + // Calculate the new direction based on gravity + currentParticle.direction = this.vectorHelpers.add(currentParticle.direction, this.gravity); + currentParticle.position = this.vectorHelpers.add(currentParticle.position, currentParticle.direction); + currentParticle.position = this.vectorHelpers.add(currentParticle.position, this.viewportDelta); + if (this.jitter) { + currentParticle.position.x += this.jitter * this.RANDM1TO1(); + currentParticle.position.y += this.jitter * this.RANDM1TO1(); + } + currentParticle.timeToLive--; + + // Update colours + var r = currentParticle.colour[0] += currentParticle.deltaColour[0]; + var g = currentParticle.colour[1] += currentParticle.deltaColour[1]; + var b = currentParticle.colour[2] += currentParticle.deltaColour[2]; + var a = currentParticle.colour[3] += currentParticle.deltaColour[3]; + + // Calculate the rgba string to draw. + draw = []; + draw.push("rgba(" + (r > 255 ? 255 : r < 0 ? 0 : ~~r)); + draw.push(g > 255 ? 255 : g < 0 ? 0 : ~~g); + draw.push(b > 255 ? 255 : b < 0 ? 0 : ~~b); + draw.push((a > 1 ? 1 : a < 0 ? 0 : a.toFixed(2)) + ")"); + currentParticle.drawColour = draw.join(","); + + if (!this.fastMode) { + draw[3] = "0)"; + currentParticle.drawColourEnd = draw.join(","); + } + + this.particleIndex++; + } else { + // Replace particle with the last active + if (this.particleIndex != this.particleCount - 1) { + this.particles[this.particleIndex] = this.particles[this.particleCount - 1]; + } + this.particleCount--; + } + var rect = {}; + rect._x = ~~currentParticle.position.x; + rect._y = ~~currentParticle.position.y; + rect._w = currentParticle.size; + rect._h = currentParticle.size; + + this.register.push(rect); + } + }, + + stop: function () { + this.active = false; + this.elapsedFrames = 0; + this.emitCounter = 0; + this.parentEntity.trigger("ParticleEnd"); + }, + + render: function (context) { + + for (var i = 0, j = this.particleCount; i < j; i++) { + var particle = this.particles[i]; + var size = particle.size; + var halfSize = size >> 1; + + if (particle.position.x + size < 0 || particle.position.y + size < 0 || particle.position.x - size > Crafty.viewport.width || particle.position.y - size > Crafty.viewport.height) { + //Particle is outside + continue; + } + var x = ~~particle.position.x; + var y = ~~particle.position.y; + + if (this.fastMode) { + context.fillStyle = particle.drawColour; + } else { + var radgrad = context.createRadialGradient(x + halfSize, y + halfSize, particle.sizeSmall, x + halfSize, y + halfSize, halfSize); + radgrad.addColorStop(0, particle.drawColour); + //0.9 to avoid visible boxing + radgrad.addColorStop(0.9, particle.drawColourEnd); + context.fillStyle = radgrad; + } + context.fillRect(x, y, size, size); + } + }, + particle: function (vectorHelpers) { + this.position = vectorHelpers.create(0, 0); + this.direction = vectorHelpers.create(0, 0); + this.size = 0; + this.sizeSmall = 0; + this.timeToLive = 0; + this.colour = []; + this.drawColour = ""; + this.deltaColour = []; + this.sharpness = 0; + }, + vectorHelpers: { + create: function (x, y) { + return { + "x": x, + "y": y + }; + }, + multiply: function (vector, scaleFactor) { + vector.x *= scaleFactor; + vector.y *= scaleFactor; + return vector; + }, + add: function (vector1, vector2) { + vector1.x += vector2.x; + vector1.y += vector2.y; + return vector1; + } + } + } +}); + +},{"./core.js":9}],20:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +Crafty.extend({ + /**@ + * #Crafty.audio + * @category Audio + * + * Add sound files and play them. Chooses best format for browser support. + * Due to the nature of HTML5 audio, three types of audio files will be + * required for cross-browser capabilities. These formats are MP3, Ogg and WAV. + * When sound was not muted on before pause, sound will be unmuted after unpause. + * When sound is muted Crafty.pause() does not have any effect on sound + * + * The maximum number of sounds that can be played simultaneously is defined by Crafty.audio.maxChannels. The default value is 7. + */ + audio: { + + sounds: {}, + supported: null, + codecs: { // Chart from jPlayer + ogg: 'audio/ogg; codecs="vorbis"', //OGG + wav: 'audio/wav; codecs="1"', // PCM + webma: 'audio/webm; codecs="vorbis"', // WEBM + mp3: 'audio/mpeg; codecs="mp3"', //MP3 + m4a: 'audio/mp4; codecs="mp4a.40.2"' // AAC / MP4 + }, + volume: 1, //Global Volume + muted: false, + paused: false, + playCheck: null, + /** + * Function to setup supported formats + **/ + _canPlay: function () { + this.supported = {}; + // Without support, no formats are supported + if (!Crafty.support.audio) + return; + var audio = this.audioElement(), + canplay; + for (var i in this.codecs) { + canplay = audio.canPlayType(this.codecs[i]); + if (canplay !== "" && canplay !== "no") { + this.supported[i] = true; + } else { + this.supported[i] = false; + } + } + + }, + + /**@ + * #Crafty.audio.supports + * @comp Crafty.audio + * @sign public this Crafty.audio.supports(String extension) + * @param extension - A file extension to check audio support for + * + * Return true if the browser thinks it can play the given file type, otherwise false + */ + supports: function (extension) { + // Build cache of supported formats, if necessary + if (this.supported === null) + this._canPlay(); + + if (this.supported[extension]) + return true; + else + return false; + }, + + /** + * Function to get an Audio Element + **/ + audioElement: function () { + //IE does not support Audio Object + return typeof Audio !== 'undefined' ? new Audio("") : document.createElement('audio'); + }, + + /**@ + * #Crafty.audio.create + * @comp Crafty.audio + * @sign public this Crafty.audio.create(String id, String url) + * @param id - A string to refer to sounds + * @param url - A string pointing to the sound file + * + * Creates an audio asset with the given id and resource. `Crafty.audio.add` is a more flexible interface that allows cross-browser compatibility. + * + * If the sound file extension is not supported, returns false; otherwise, returns the audio asset. + */ + create: function (id, path) { + //check extension, return if not supported + var ext = path.substr(path.lastIndexOf('.') + 1).toLowerCase(); + if (!this.supports(ext)) + return false; + + //initiate the audio element + var audio = this.audioElement(); + audio.id = id; + audio.preload = "auto"; + audio.volume = Crafty.audio.volume; + audio.src = path; + + //create an asset and metadata for the audio element + Crafty.asset(path, audio); + this.sounds[id] = { + obj: audio, + played: 0, + volume: Crafty.audio.volume + }; + return this.sounds[id]; + + }, + + /**@ + * #Crafty.audio.add + * @comp Crafty.audio + * @sign public this Crafty.audio.add(String id, String url) + * @param id - A string to refer to sounds + * @param url - A string pointing to the sound file + * @sign public this Crafty.audio.add(String id, Array urls) + * @param urls - Array of urls pointing to different format of the same sound, selecting the first that is playable + * @sign public this Crafty.audio.add(Object map) + * @param map - key-value pairs where the key is the `id` and the value is either a `url` or `urls` + * + * Loads a sound to be played. Due to the nature of HTML5 audio, + * three types of audio files will be required for cross-browser capabilities. + * These formats are MP3, Ogg and WAV. + * + * Passing an array of URLs will determine which format the browser can play and select it over any other. + * + * Accepts an object where the key is the audio name and + * either a URL or an Array of URLs (to determine which type to use). + * + * The ID you use will be how you refer to that sound when using `Crafty.audio.play`. + * + * @example + * ~~~ + * //adding audio from an object + * Crafty.audio.add({ + * shoot: ["sounds/shoot.wav", + * "sounds/shoot.mp3", + * "sounds/shoot.ogg"], + * + * coin: "sounds/coin.mp3" + * }); + * + * //adding a single sound + * Crafty.audio.add("walk", [ + * "sounds/walk.mp3", + * "sounds/walk.ogg", + * "sounds/walk.wav" + * ]); + * + * //only one format + * Crafty.audio.add("jump", "sounds/jump.mp3"); + * ~~~ + */ + add: function (id, url) { + if (!Crafty.support.audio) + return; + + var src; + + if (arguments.length === 1 && typeof id === "object") { + for (var i in id) { + for (src in id[i]) { + if (Crafty.audio.create(i, id[i][src])) + break; + } + } + } + if (typeof id === "string") { + if (typeof url === "string") { + Crafty.audio.create(id, url); + } + + if (typeof url === "object") { + for (src in url) { + if (Crafty.audio.create(id, url[src])) + break; + } + } + + } + }, + /**@ + * #Crafty.audio.play + * @comp Crafty.audio + * @sign public this Crafty.audio.play(String id) + * @sign public this Crafty.audio.play(String id, Number repeatCount) + * @sign public this Crafty.audio.play(String id, Number repeatCount, Number volume) + * @param id - A string to refer to sounds + * @param repeatCount - Repeat count for the file, where -1 stands for repeat forever. + * @param volume - volume can be a number between 0.0 and 1.0 + * @returns The audio element used to play the sound. Null if the call failed due to a lack of open channels. + * + * Will play a sound previously added by using the ID that was used in `Crafty.audio.add`. + * Has a default maximum of 5 channels so that the same sound can play simultaneously unless all of the channels are playing. + + * *Note that the implementation of HTML5 Audio is buggy at best.* + * + * @example + * ~~~ + * Crafty.audio.play("walk"); + * + * //play and repeat forever + * Crafty.audio.play("backgroundMusic", -1); + * Crafty.audio.play("explosion",1,0.5); //play sound once with volume of 50% + * ~~~ + */ + play: function (id, repeat, volume) { + if (repeat === 0 || !Crafty.support.audio || !this.sounds[id]) + return; + var s = this.sounds[id]; + var c = this.getOpenChannel(); + if (!c) + return null; + c.id = id; + c.repeat = repeat; + var a = c.obj; + + + c.volume = s.volume = s.obj.volume = volume || Crafty.audio.volume; + + a.volume = s.volume; + a.src = s.obj.src; + + if (this.muted) + a.volume = 0; + a.play(); + s.played++; + c.onEnd = function () { + if (s.played < c.repeat || repeat == -1) { + if (this.currentTime) + this.currentTime = 0; + this.play(); + s.played++; + } else { + c.active = false; + this.pause(); + this.removeEventListener("ended", c.onEnd, true); + this.currentTime = 0; + Crafty.trigger("SoundComplete", { + id: c.id + }); + } + + }; + a.addEventListener("ended", c.onEnd, true); + + return a; + }, + + + + /**@ + * #Crafty.audio.setChannels + * @comp Crafty.audio + * @sign public this Crafty.audio.setChannels(Number n) + * @param n - The maximum number of channels + */ + maxChannels: 7, + setChannels: function (n) { + this.maxChannels = n; + if (n < this.channels.length) + this.channels.length = n; + }, + + channels: [], + // Finds an unused audio element, marks it as in use, and return it. + getOpenChannel: function () { + for (var i = 0; i < this.channels.length; i++) { + var chan = this.channels[i]; + /* + * Second test looks for stuff that's out of use, + * but fallen foul of Chromium bug 280417 + */ + if (chan.active === false || + chan.obj.ended && chan.repeat <= this.sounds[chan.id].played) { + chan.active = true; + return chan; + } + } + // If necessary, create a new element, unless we've already reached the max limit + if (i < this.maxChannels) { + var c = { + obj: this.audioElement(), + active: true, + // Checks that the channel is being used to play sound id + _is: function (id) { + return this.id === id && this.active; + } + }; + this.channels.push(c); + return c; + } + // In that case, return null + return null; + }, + + /**@ + * #Crafty.audio.remove + * @comp Crafty.audio + * @sign public this Crafty.audio.remove([String id]) + * @param id - A string to refer to sounds + * + * Will stop the sound and remove all references to the audio object allowing the browser to free the memory. + * If no id is given, all sounds will be removed. + * + * @example + * ~~~ + * Crafty.audio.remove("walk"); + * ~~~ + */ + remove: function (id) { + if (!Crafty.support.audio) + return; + + var s; + + if (!id) { + for (var i in this.sounds) { + s = this.sounds[i]; + Crafty.audio.stop(id); + delete Crafty.assets[s.obj.src]; + delete Crafty.audio.sounds[id]; + } + return; + } + if (!this.sounds[id]) + return; + + s = this.sounds[id]; + Crafty.audio.stop(id); + delete Crafty.assets[s.obj.src]; + delete Crafty.audio.sounds[id]; + }, + /**@ + * #Crafty.audio.stop + * @comp Crafty.audio + * @sign public this Crafty.audio.stop([Number ID]) + * + * Stops any playing sound. if id is not set, stop all sounds which are playing + * + * @example + * ~~~ + * //all sounds stopped playing now + * Crafty.audio.stop(); + * + * ~~~ + */ + stop: function (id) { + if (!Crafty.support.audio) + return; + for (var i in this.channels) { + c = this.channels[i]; + if ( (!id && c.active) || c._is(id) ) { + c.active = false; + c.obj.pause(); + } + } + return; + }, + /** + * #Crafty.audio._mute + * @comp Crafty.audio + * @sign public this Crafty.audio._mute([Boolean mute]) + * + * Mute or unmute every Audio instance that is playing. + */ + _mute: function (mute) { + if (!Crafty.support.audio) + return; + var c; + for (var i in this.channels) { + c = this.channels[i]; + c.obj.volume = mute ? 0 : c.volume; + } + this.muted = mute; + }, + /**@ + * #Crafty.audio.toggleMute + * @comp Crafty.audio + * @sign public this Crafty.audio.toggleMute() + * + * Mute or unmute every Audio instance that is playing. Toggles between + * pausing or playing depending on the state. + * + * @example + * ~~~ + * //toggle mute and unmute depending on current state + * Crafty.audio.toggleMute(); + * ~~~ + */ + toggleMute: function () { + if (!this.muted) { + this._mute(true); + } else { + this._mute(false); + } + + }, + /**@ + * #Crafty.audio.mute + * @comp Crafty.audio + * @sign public this Crafty.audio.mute() + * + * Mute every Audio instance that is playing. + * + * @example + * ~~~ + * Crafty.audio.mute(); + * ~~~ + */ + mute: function () { + this._mute(true); + }, + /**@ + * #Crafty.audio.unmute + * @comp Crafty.audio + * @sign public this Crafty.audio.unmute() + * + * Unmute every Audio instance that is playing. + * + * @example + * ~~~ + * Crafty.audio.unmute(); + * ~~~ + */ + unmute: function () { + this._mute(false); + }, + + /**@ + * #Crafty.audio.pause + * @comp Crafty.audio + * @sign public this Crafty.audio.pause(string ID) + * + * Pause the Audio instance specified by id param. + * + * @example + * ~~~ + * Crafty.audio.pause('music'); + * ~~~ + * + * @param {string} id The id of the audio object to pause + */ + pause: function (id) { + if (!Crafty.support.audio || !id || !this.sounds[id]) + return; + var c; + for (var i in this.channels) { + c = this.channels[i]; + if (c._is(id) && !c.obj.paused) + c.obj.pause(); + } + + }, + + /**@ + * #Crafty.audio.unpause + * @comp Crafty.audio + * @sign public this Crafty.audio.unpause(string ID) + * + * Resume playing the Audio instance specified by id param. + * + * @example + * ~~~ + * Crafty.audio.unpause('music'); + * ~~~ + * + * @param {string} id The id of the audio object to unpause + */ + unpause: function (id) { + if (!Crafty.support.audio || !id || !this.sounds[id]) + return; + var c; + for (var i in this.channels) { + c = this.channels[i]; + if (c._is(id) && c.obj.paused) + c.obj.play(); + } + }, + + /**@ + * #Crafty.audio.togglePause + * @comp Crafty.audio + * @sign public this Crafty.audio.togglePause(string ID) + * + * Toggle the pause status of the Audio instance specified by id param. + * + * @example + * ~~~ + * Crafty.audio.togglePause('music'); + * ~~~ + * + * @param {string} id The id of the audio object to pause/unpause + */ + togglePause: function (id) { + if (!Crafty.support.audio || !id || !this.sounds[id]) + return; + var c; + for (var i in this.channels) { + c = this.channels[i]; + if (c._is(id)) { + if (c.obj.paused) { + c.obj.play(); + } else { + c.obj.pause(); + } + } + } + } + } +}); + +},{"./core.js":9}],21:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +/**@ + * #Sprite + * @category Graphics + * @trigger Change - when the sprites change + * Component for using tiles in a sprite map. + */ +Crafty.c("Sprite", { + __image: '', + /* + * #.__tile + * @comp Sprite + * + * Horizontal sprite tile size. + */ + __tile: 0, + /* + * #.__tileh + * @comp Sprite + * + * Vertical sprite tile size. + */ + __tileh: 0, + __padding: null, + __trim: null, + img: null, + //ready is changed to true in Crafty.sprite + ready: false, + + init: function () { + this.__trim = [0, 0, 0, 0]; + + var draw = function (e) { + var co = e.co, + pos = e.pos, + context = e.ctx; + + if (e.type === "canvas") { + //draw the image on the canvas element + context.drawImage(this.img, //image element + co.x, //x position on sprite + co.y, //y position on sprite + co.w, //width on sprite + co.h, //height on sprite + pos._x, //x position on canvas + pos._y, //y position on canvas + pos._w, //width on canvas + pos._h //height on canvas + ); + } else if (e.type === "DOM") { + // Get scale (ratio of entity dimensions to sprite's dimensions) + // If needed, we will scale up the entire sprite sheet, and then modify the position accordingly + var vscale = this._h / co.h, + hscale = this._w / co.w, + style = this._element.style; + + style.background = style.backgroundColor + " url('" + this.__image + "') no-repeat -" + co.x * hscale + "px -" + co.y * vscale + "px"; + // style.backgroundSize must be set AFTER style.background! + if (vscale != 1 || hscale != 1) { + style.backgroundSize = (this.img.width * hscale) + "px" + " " + (this.img.height * vscale) + "px"; + } + } + }; + + this.bind("Draw", draw).bind("RemoveComponent", function (id) { + if (id === "Sprite") this.unbind("Draw", draw); + }); + }, + + /**@ + * #.sprite + * @comp Sprite + * @sign public this .sprite(Number x, Number y, Number w, Number h) + * @param x - X cell position + * @param y - Y cell position + * @param w - Width in cells + * @param h - Height in cells + * + * Uses a new location on the sprite map as its sprite. + * + * Values should be in tiles or cells (not pixels). + * + * @example + * ~~~ + * Crafty.e("2D, DOM, Sprite") + * .sprite(0, 0, 2, 2); + * ~~~ + */ + + /**@ + * #.__coord + * @comp Sprite + * + * The coordinate of the slide within the sprite in the format of [x, y, w, h]. + */ + sprite: function (x, y, w, h) { + this.__coord = [x * (this.__tile + this.__padding[0]) + this.__trim[0], + y * (this.__tileh + this.__padding[1]) + this.__trim[1], + this.__trim[2] || w * this.__tile || this.__tile, + this.__trim[3] || h * this.__tileh || this.__tileh + ]; + + this.trigger("Change"); + return this; + }, + + /**@ + * #.crop + * @comp Sprite + * @sign public this .crop(Number x, Number y, Number w, Number h) + * @param x - Offset x position + * @param y - Offset y position + * @param w - New width + * @param h - New height + * + * If the entity needs to be smaller than the tile size, use this method to crop it. + * + * The values should be in pixels rather than tiles. + * + * @example + * ~~~ + * Crafty.e("2D, DOM, Sprite") + * .crop(40, 40, 22, 23); + * ~~~ + */ + crop: function (x, y, w, h) { + var old = this._mbr || this.pos(); + this.__trim = []; + this.__trim[0] = x; + this.__trim[1] = y; + this.__trim[2] = w; + this.__trim[3] = h; + + this.__coord[0] += x; + this.__coord[1] += y; + this.__coord[2] = w; + this.__coord[3] = h; + this._w = w; + this._h = h; + + this.trigger("Change", old); + return this; + } +}); +},{"./core.js":9}],22:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +// Directive for jshint to ignore evals +// eval is used to support IE8, so this can be removed if we decide to drop that +/* jshint evil:true */ + +/**@ + * #Storage + * @category Utilities + * Utility to allow data to be saved to a permanent storage solution: IndexedDB, WebSql, localstorage or cookies + */ +/**@ + * #.open + * @comp Storage + * @sign .open(String gameName) + * @param gameName - a machine readable string to uniquely identify your game + * + * Opens a connection to the database. If the best they have is localstorage or lower, it does nothing + * + * @example + * Open a database + * ~~~ + * Crafty.storage.open('MyGame'); + * ~~~ + */ +/**@ + * #.save + * @comp Storage + * @sign .save(String key, String type, Mixed data) + * @param key - A unique key for identifying this piece of data + * @param type - 'save' or 'cache' + * @param data - Some kind of data. + * + * Saves a piece of data to the database. Can be anything, although entities are preferred. + * For all storage methods but IndexedDB, the data will be serialized as a string + * During serialization, an entity's SaveData event will be triggered. + * Components should implement a SaveData handler and attach the necessary information to the passed object + * + * @example + * Saves an entity to the database + * ~~~ + * var ent = Crafty.e("2D, DOM") + * .attr({x: 20, y: 20, w: 100, h:100}); + * Crafty.storage.open('MyGame'); + * Crafty.storage.save('MyEntity', 'save', ent); + * ~~~ + */ +/**@ + * #.load + * @comp Storage + * @sign .load(String key, String type) + * @param key - A unique key to search for + * @param type - 'save' or 'cache' + * @param callback - Do things with the data you get back + * + * Loads a piece of data from the database. + * Entities will be reconstructed from the serialized string + + * @example + * Loads an entity from the database + * ~~~ + * Crafty.storage.open('MyGame'); + * Crafty.storage.load('MyEntity', 'save', function (data) { // do things }); + * ~~~ + */ +/**@ + * #.getAllKeys + * @comp Storage + * @sign .getAllKeys(String type) + * @param type - 'save' or 'cache' + * Gets all the keys for a given type + + * @example + * Gets all the save games saved + * ~~~ + * Crafty.storage.open('MyGame'); + * var saves = Crafty.storage.getAllKeys('save'); + * ~~~ + */ +/**@ + * #.external + * @comp Storage + * @sign .external(String url) + * @param url - URL to an external to save games too + * + * Enables and sets the url for saving games to an external server + * + * @example + * Save an entity to an external server + * ~~~ + * Crafty.storage.external('http://somewhere.com/server.php'); + * Crafty.storage.open('MyGame'); + * var ent = Crafty.e('2D, DOM') + * .attr({x: 20, y: 20, w: 100, h:100}); + * Crafty.storage.save('save01', 'save', ent); + * ~~~ + */ +/**@ + * #SaveData event + * @comp Storage + * @param data - An object containing all of the data to be serialized + * @param prepare - The function to prepare an entity for serialization + * + * Any data a component wants to save when it's serialized should be added to this object. + * Straight attribute should be set in data.attr. + * Anything that requires a special handler should be set in a unique property. + * + * @example + * Saves the innerHTML of an entity + * ~~~ + * Crafty.e("2D DOM").bind("SaveData", function (data, prepare) { + * data.attr.x = this.x; + * data.attr.y = this.y; + * data.dom = this.element.innerHTML; + * }); + * ~~~ + */ +/**@ + * #LoadData event + * @comp Storage + * @param data - An object containing all the data that been saved + * @param process - The function to turn a string into an entity + * + * Handlers for processing any data that needs more than straight assignment + * + * Note that data stored in the .attr object is automatically added to the entity. + * It does not need to be handled here + * + * @example + * ~~~ + * Sets the innerHTML from a saved entity + * Crafty.e("2D DOM").bind("LoadData", function (data, process) { + * this.element.innerHTML = data.dom; + * }); + * ~~~ + */ +Crafty.storage = (function () { + var db = null, + url, gameName, timestamps = {}, + transactionType = { + READ: "readonly", + READ_WRITE: "readwrite" + }; + + /* + * Processes a retrieved object. + * Creates an entity if it is one + */ + + function process(obj) { + if (obj.c) { + var d = Crafty.e(obj.c) + .attr(obj.attr) + .trigger('LoadData', obj, process); + return d; + } else if (typeof obj == 'object') { + for (var prop in obj) { + obj[prop] = process(obj[prop]); + } + } + return obj; + } + + function unserialize(str) { + if (typeof str != 'string') return null; + var data = (JSON ? JSON.parse(str) : eval('(' + str + ')')); + return process(data); + } + + /* recursive function + * searches for entities in an object and processes them for serialization + */ + + function prep(obj) { + if (obj.__c) { + // object is entity + var data = { + c: [], + attr: {} + }; + obj.trigger("SaveData", data, prep); + for (var i in obj.__c) { + data.c.push(i); + } + data.c = data.c.join(', '); + obj = data; + } else if (typeof obj == 'object') { + // recurse and look for entities + for (var prop in obj) { + obj[prop] = prep(obj[prop]); + } + } + return obj; + } + + function serialize(e) { + if (JSON) { + var data = prep(e); + return JSON.stringify(data); + } else { + alert("Crafty does not support saving on your browser. Please upgrade to a newer browser."); + return false; + } + } + + // for saving a game to a central server + + function external(setUrl) { + url = setUrl; + } + + function openExternal() { + if (1 && typeof url == "undefined") return; + // get the timestamps for external saves and compare them to local + // if the external is newer, load it + + var xml = new XMLHttpRequest(); + xhr.open("POST", url); + xhr.onreadystatechange = function (evt) { + if (xhr.readyState == 4) { + if (xhr.status == 200) { + var data = eval("(" + xhr.responseText + ")"); + for (var i in data) { + if (Crafty.storage.check(data[i].key, data[i].timestamp)) { + loadExternal(data[i].key); + } + } + } + } + }; + xhr.send("mode=timestamps&game=" + gameName); + } + + function saveExternal(key, data, ts) { + if (1 && typeof url == "undefined") return; + var xhr = new XMLHttpRequest(); + xhr.open("POST", url); + xhr.send("mode=save&key=" + key + "&data=" + encodeURIComponent(data) + "&ts=" + ts + "&game=" + gameName); + } + + function loadExternal(key) { + if (1 && typeof url == "undefined") return; + var xhr = new XMLHttpRequest(); + xhr.open("POST", url); + xhr.onreadystatechange = function (evt) { + if (xhr.readyState == 4) { + if (xhr.status == 200) { + var data = eval("(" + xhr.responseText + ")"); + Crafty.storage.save(key, 'save', data); + } + } + }; + xhr.send("mode=load&key=" + key + "&game=" + gameName); + } + + /** + * get timestamp + */ + + function ts() { + var d = new Date(); + return d.getTime(); + } + + // everyone names their object different. Fix that nonsense. + if (typeof indexedDB != 'object') { + window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; + window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction; + window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange; + + /* Numeric constants for transaction type are deprecated + * Ensure that the script will work consistenly for recent and legacy browser versions + */ + if (typeof IDBTransaction == 'object') { + transactionType.READ = IDBTransaction.READ || IDBTransaction.readonly || transactionType.READ || 'read'; + transactionType.READ_WRITE = IDBTransaction.READ_WRITE || IDBTransaction.readwrite || transactionType.READ_WRITE || 'readwrite'; + } + } + + if (typeof indexedDB == 'object') { + + return { + open: function (gameName_n) { + gameName = gameName_n; + var stores = []; + + if (arguments.length == 1) { + stores.push('save'); + stores.push('cache'); + } else { + stores = arguments; + stores.shift(); + stores.push('save'); + stores.push('cache'); + } + if (db === null) { + var request = indexedDB.open(gameName); + request.onsuccess = function (e) { + db = e.target.result; + getTimestamps(); + openExternal(); + }; + request.onupgradeneeded = function (e) { + createStores(); + }; + } else { + createStores(); + getTimestamps(); + openExternal(); + } + + // get all the timestamps for existing keys + + function getTimestamps() { + try { + var trans = db.transaction(['save'], "read"), + store = trans.objectStore('save'), + request = store.getAll(); + request.onsuccess = function (e) { + var i = 0, + a = event.target.result, + l = a.length; + for (; i < l; i++) { + timestamps[a[i].key] = a[i].timestamp; + } + }; + } catch (e) {} + } + + function createStores() { + var request = db.setVersion("1.0"); + request.onsuccess = function (e) { + for (var i = 0; i < stores.length; i++) { + var st = stores[i]; + if (db.objectStoreNames.contains(st)) continue; + var store = db.createObjectStore(st, { + keyPath: "key" + }); + } + }; + } + }, + + save: function (key, type, data, callback) { + if (db === null) { + setTimeout(function () { + Crafty.storage.save(key, type, data); + }, 1); + return; + } + + var str = serialize(data), + t = ts(); + if (type == 'save') saveExternal(key, str, t); + try { + var request = db.transaction([type], transactionType.READ_WRITE).objectStore(type).add({ + "data": str, + "timestamp": t, + "key": key + }); + if (typeof callback == 'function') { + request.onsuccess = callback; + } + } catch (e) { + console.error(e); + } + }, + + load: function (key, type, callback) { + if (db === null) { + setTimeout(function () { + Crafty.storage.load(key, type, callback); + }, 1); + return; + } + try { + var request = db.transaction([type], transactionType.READ).objectStore(type).get(key); + request.onsuccess = function (e) { + callback(unserialize(e.target.result.data)); + }; + } catch (e) { + console.error(e); + } + }, + + getAllKeys: function (type, callback) { + if (db === null) { + setTimeout(function () { + Crafty.storage.getAllkeys(type, callback); + }, 1); + } + try { + var request = db.transaction([type], transactionType.READ).objectStore(type).openCursor(), + res = []; + request.onsuccess = function (e) { + var cursor = e.target.result; + if (cursor) { + res.push(cursor.key); + // 'continue' is a reserved word, so .continue() causes IE8 to completely bark with "SCRIPT1010: Expected identifier". + cursor['continue'](); + } else { + callback(res); + } + }; + } catch (e) { + console.error(e); + } + }, + + check: function (key, timestamp) { + return (timestamps[key] > timestamp); + }, + + external: external + }; + } else if (typeof openDatabase == 'function') { + return { + open: function (gameName_n) { + gameName = gameName_n; + if (arguments.length == 1) { + db = { + save: openDatabase(gameName_n + '_save', '1.0', 'Saves games for ' + gameName_n, 5 * 1024 * 1024), + cache: openDatabase(gameName_n + '_cache', '1.0', 'Cache for ' + gameName_n, 5 * 1024 * 1024) + }; + } else { + // allows for any other types that can be thought of + var args = arguments, + i = 0; + args.shift(); + for (; i < args.length; i++) { + if (typeof db[args[i]] == 'undefined') + db[args[i]] = openDatabase(gameName + '_' + args[i], '1.0', type, 5 * 1024 * 1024); + } + } + + db.save.transaction(function (tx) { + tx.executeSql('SELECT key, timestamp FROM data', [], function (tx, res) { + var i = 0, + a = res.rows, + l = a.length; + for (; i < l; i++) { + timestamps[a.item(i).key] = a.item(i).timestamp; + } + }); + }); + }, + + save: function (key, type, data) { + if (typeof db[type] == 'undefined' && gameName !== '') { + this.open(gameName, type); + } + + var str = serialize(data), + t = ts(); + if (type == 'save') saveExternal(key, str, t); + db[type].transaction(function (tx) { + tx.executeSql('CREATE TABLE IF NOT EXISTS data (key unique, text, timestamp)'); + tx.executeSql('SELECT * FROM data WHERE key = ?', [key], function (tx, results) { + if (results.rows.length) { + tx.executeSql('UPDATE data SET text = ?, timestamp = ? WHERE key = ?', [str, t, key]); + } else { + tx.executeSql('INSERT INTO data VALUES (?, ?, ?)', [key, str, t]); + } + }); + }); + }, + + load: function (key, type, callback) { + if (typeof db[type] === 'undefined') { + setTimeout(function () { + Crafty.storage.load(key, type, callback); + }, 1); + return; + } + db[type].transaction(function (tx) { + tx.executeSql('SELECT text FROM data WHERE key = ?', [key], function (tx, results) { + if (results.rows.length) { + res = unserialize(results.rows.item(0).text); + callback(res); + } + }); + }); + }, + + getAllKeys: function (type, callback) { + if (typeof db[type] === 'undefined') { + setTimeout(function () { + Crafty.storage.getAllKeys(type, callback); + }, 1); + return; + } + db[type].transaction(function (tx) { + tx.executeSql('SELECT key FROM data', [], function (tx, results) { + callback(results.rows); + }); + }); + }, + + check: function (key, timestamp) { + return (timestamps[key] > timestamp); + }, + + external: external + }; + } else if (typeof window.localStorage == 'object') { + return { + open: function (gameName_n) { + gameName = gameName_n; + }, + + save: function (key, type, data) { + var k = gameName + '.' + type + '.' + key, + str = serialize(data), + t = ts(); + if (type == 'save') saveExternal(key, str, t); + window.localStorage[k] = str; + if (type == 'save') + window.localStorage[k + '.ts'] = t; + }, + + load: function (key, type, callback) { + var k = gameName + '.' + type + '.' + key, + str = window.localStorage[k]; + + callback(unserialize(str)); + }, + + getAllKeys: function (type, callback) { + var res = {}, output = [], + header = gameName + '.' + type; + for (var i in window.localStorage) { + if (i.indexOf(header) != -1) { + var key = i.replace(header, '').replace('.ts', ''); + res[key] = true; + } + } + for (i in res) { + output.push(i); + } + callback(output); + }, + + check: function (key, timestamp) { + var ts = window.localStorage[gameName + '.save.' + key + '.ts']; + + return (parseInt(timestamp, 10) > parseInt(ts, 10)); + }, + + external: external + }; + } else { + // default fallback to cookies + return { + open: function (gameName_n) { + gameName = gameName_n; + }, + + save: function (key, type, data) { + // cookies are very limited in space. we can only keep saves there + if (type != 'save') return; + var str = serialize(data), + t = ts(); + if (type == 'save') saveExternal(key, str, t); + document.cookie = gameName + '_' + key + '=' + str + '; ' + gameName + '_' + key + '_ts=' + t + '; expires=Thur, 31 Dec 2099 23:59:59 UTC; path=/'; + }, + + load: function (key, type, callback) { + if (type != 'save') return; + var reg = new RegExp(gameName + '_' + key + '=[^;]*'), + result = reg.exec(document.cookie), + data = unserialize(result[0].replace(gameName + '_' + key + '=', '')); + + callback(data); + }, + + getAllKeys: function (type, callback) { + if (type != 'save') return; + var reg = new RegExp(gameName + '_[^_=]', 'g'), + matches = reg.exec(document.cookie), + i = 0, + l = matches.length, + res = {}, output = []; + for (; i < l; i++) { + var key = matches[i].replace(gameName + '_', ''); + res[key] = true; + } + for (i in res) { + output.push(i); + } + callback(output); + }, + + check: function (key, timestamp) { + var header = gameName + '_' + key + '_ts', + reg = new RegExp(header + '=[^;]'), + result = reg.exec(document.cookie), + ts = result[0].replace(header + '=', ''); + + return (parseInt(timestamp, 10) > parseInt(ts, 10)); + }, + + external: external + }; + } + /* template + return { + open: function (gameName) { + }, + save: function (key, type, data) { + }, + load: function (key, type, callback) { + }, + }*/ +})(); +},{"./core.js":9}],23:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +/**@ + * #Text + * @category Graphics + * @trigger Change - when the text is changed + * @requires Canvas or DOM + * Component to make a text entity. + * + * By default, text will have the style "10px sans-serif". + * + * Note 1: An entity with the text component is just text! If you want to write text + * inside an image, you need one entity for the text and another entity for the image. + * More tips for writing text inside an image: (1) Use the z-index (from 2D component) + * to ensure that the text is on top of the image, not the other way around; (2) + * use .attach() (from 2D component) to glue the text to the image so they move and + * rotate together. + * + * Note 2: For DOM (but not canvas) text entities, various font settings (like + * text-decoration and text-align) can be set using `.css()` (see DOM component). But + * you cannot use `.css()` to set the properties which are controlled by `.textFont()` + * or `.textColor()` -- the settings will be ignored. + * + * Note 3: If you use canvas text with glyphs that are taller than standard letters, portions of the glyphs might be cut off. + */ +Crafty.c("Text", { + _text: "", + defaultSize: "10px", + defaultFamily: "sans-serif", + ready: true, + + init: function () { + this.requires("2D"); + this._textFont = { + "type": "", + "weight": "", + "size": this.defaultSize, + "family": this.defaultFamily + }; + + this.bind("Draw", function (e) { + var font = this._fontString(); + + if (e.type === "DOM") { + var el = this._element, + style = el.style; + + style.color = this._textColor; + style.font = font; + el.innerHTML = this._text; + } else if (e.type === "canvas") { + var context = e.ctx; + + context.save(); + + context.textBaseline = "top"; + context.fillStyle = this._textColor || "rgb(0,0,0)"; + context.font = font; + + context.fillText(this._text, this._x, this._y); + + context.restore(); + } + }); + }, + + // takes a CSS font-size string and gets the height of the resulting font in px + _getFontHeight: (function(){ + // regex for grabbing the first string of letters + var re = /([a-zA-Z]+)\b/; + // From the CSS spec. "em" and "ex" are undefined on a canvas. + var multipliers = { + "px": 1, + "pt": 4/3, + "pc": 16, + "cm": 96/2.54, + "mm": 96/25.4, + "in": 96, + "em": undefined, + "ex": undefined + }; + return function (font){ + var number = parseFloat(font); + var match = re.exec(font); + var unit = match ? match[1] : "px"; + if (multipliers[unit] !== undefined) + return Math.ceil(number * multipliers[unit]); + else + return Math.ceil(number); + }; + })(), + + /**@ + * #.text + * @comp Text + * @sign public this .text(String text) + * @sign public this .text(Function textgenerator) + * @param text - String of text that will be inserted into the DOM or Canvas element. + * + * This method will update the text inside the entity. + * + * If you need to reference attributes on the entity itself you can pass a function instead of a string. + * + * @example + * ~~~ + * Crafty.e("2D, DOM, Text").attr({ x: 100, y: 100 }).text("Look at me!!"); + * + * Crafty.e("2D, DOM, Text").attr({ x: 100, y: 100 }) + * .text(function () { return "My position is " + this._x }); + * + * Crafty.e("2D, Canvas, Text").attr({ x: 100, y: 100 }).text("Look at me!!"); + * + * Crafty.e("2D, Canvas, Text").attr({ x: 100, y: 100 }) + * .text(function () { return "My position is " + this._x }); + * ~~~ + */ + text: function (text) { + if (!(typeof text !== "undefined" && text !== null)) return this._text; + if (typeof (text) == "function") + this._text = text.call(this); + else + this._text = text; + + if (this.has("Canvas") ) + this._resizeForCanvas(); + + this.trigger("Change"); + return this; + }, + + // Calculates the height and width of text on the canvas + // Width is found by using the canvas measureText function + // Height is only estimated -- it calculates the font size in pixels, and sets the height to 110% of that. + _resizeForCanvas: function(){ + var ctx = Crafty.canvas.context; + ctx.font = this._fontString(); + this.w = ctx.measureText(this._text).width; + + var size = (this._textFont.size || this.defaultSize); + this.h = 1.1 * this._getFontHeight(size); + }, + + // Returns the font string to use + _fontString: function(){ + return this._textFont.type + ' ' + this._textFont.weight + ' ' + this._textFont.size + ' ' + this._textFont.family; + }, + + /**@ + * #.textColor + * @comp Text + * @sign public this .textColor(String color, Number strength) + * @param color - The color in hexadecimal + * @param strength - Level of opacity + * + * Modify the text color and level of opacity. + * + * @example + * ~~~ + * Crafty.e("2D, DOM, Text").attr({ x: 100, y: 100 }).text("Look at me!!") + * .textColor('#FF0000'); + * + * Crafty.e("2D, Canvas, Text").attr({ x: 100, y: 100 }).text('Look at me!!') + * .textColor('#FF0000', 0.6); + * ~~~ + * @see Crafty.toRGB + */ + textColor: function (color, strength) { + this._strength = strength; + this._textColor = Crafty.toRGB(color, this._strength); + this.trigger("Change"); + return this; + }, + + /**@ + * #.textFont + * @comp Text + * @triggers Change + * @sign public this .textFont(String key, * value) + * @param key - Property of the entity to modify + * @param value - Value to set the property to + * + * @sign public this .textFont(Object map) + * @param map - Object where the key is the property to modify and the value as the property value + * + * Use this method to set font property of the text entity. + * + * @example + * ~~~ + * Crafty.e("2D, DOM, Text").textFont({ type: 'italic', family: 'Arial' }); + * Crafty.e("2D, Canvas, Text").textFont({ size: '20px', weight: 'bold' }); + * + * Crafty.e("2D, Canvas, Text").textFont("type", "italic"); + * Crafty.e("2D, Canvas, Text").textFont("type"); // italic + * ~~~ + */ + textFont: function (key, value) { + if (arguments.length === 1) { + //if just the key, return the value + if (typeof key === "string") { + return this._textFont[key]; + } + + if (typeof key === "object") { + for (var propertyKey in key) { + if(propertyKey == 'family'){ + this._textFont[propertyKey] = "'" + key[propertyKey] + "'"; + } else { + this._textFont[propertyKey] = key[propertyKey]; + } + } + } + } else { + this._textFont[key] = value; + } + + if (this.has("Canvas") ) + this._resizeForCanvas(); + + this.trigger("Change"); + return this; + }, + /**@ + * #.unselectable + * @comp Text + * @triggers Change + * @sign public this .unselectable() + * + * This method sets the text so that it cannot be selected (highlighted) by dragging. + * (Canvas text can never be highlighted, so this only matters for DOM text.) + * Works by changing the css property "user-select" and its variants. + * + * @example + * ~~~ + * Crafty.e("2D, DOM, Text").text('This text cannot be highlighted!').unselectable(); + * ~~~ + */ + unselectable: function () { + // http://stackoverflow.com/questions/826782/css-rule-to-disable-text-selection-highlighting + if (this.has("DOM")) { + this.css({ + '-webkit-touch-callout': 'none', + '-webkit-user-select': 'none', + '-khtml-user-select': 'none', + '-moz-user-select': 'none', + '-ms-user-select': 'none', + 'user-select': 'none' + }); + this.trigger("Change"); + } + return this; + } + +}); +},{"./core.js":9}],24:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +/**@ + * #Delay + * @category Utilities + */ +Crafty.c("Delay", { + init: function () { + this._delays = []; + this.bind("EnterFrame", function () { + var now = new Date().getTime(); + var index = this._delays.length; + while (--index >= 0) { + var item = this._delays[index]; + if (item.start + item.delay + item.pause < now) { + item.func.call(this); + if (item.repeat > 0) { + // reschedule item + item.start = now; + item.pause = 0; + item.pauseBuffer = 0; + item.repeat--; + } else if (item.repeat <= 0) { + // remove item from array + this._delays.splice(index, 1); + } + } + } + }); + this.bind("Pause", function () { + var now = new Date().getTime(); + for (var index in this._delays) { + this._delays[index].pauseBuffer = now; + } + }); + this.bind("Unpause", function () { + var now = new Date().getTime(); + for (var index in this._delays) { + var item = this._delays[index]; + item.pause += now - item.pauseBuffer; + } + }); + }, + /**@ + * #.delay + * @comp Delay + * @sign public this.delay(Function callback, Number delay) + * @param callback - Method to execute after given amount of milliseconds + * @param delay - Amount of milliseconds to execute the method + * @param repeat - How often to repeat the delayed function. A value of 0 triggers the delayed + * function exactly once. A value n > 0 triggers the delayed function exactly n+1 times. A + * value of -1 triggers the delayed function indefinitely. + * + * The delay method will execute a function after a given amount of time in milliseconds. + * + * It is not a wrapper for `setTimeout`. + * + * If Crafty is paused, the delay is interrupted with the pause and then resume when unpaused + * + * If the entity is destroyed, the delay is also destroyed and will not have effect. + * + * @example + * ~~~ + * console.log("start"); + * Crafty.e("Delay").delay(function() { + * console.log("100ms later"); + * }, 100, 0); + * ~~~ + */ + delay: function (func, delay, repeat) { + this._delays.push({ + start: new Date().getTime(), + func: func, + delay: delay, + repeat: (repeat < 0 ? Infinity : repeat) || 0, + pauseBuffer: 0, + pause: 0 + }); + return this; + } +}); +},{"./core.js":9}],25:[function(require,module,exports){ +module.exports = "0.6.0"; +},{}],26:[function(require,module,exports){ +var Crafty = require('./core.js'), + document = window.document; + +Crafty.extend({ + /**@ + * #Crafty.viewport + * @category Stage + * @trigger ViewportScroll - when the viewport's x or y coordinates change + * @trigger ViewportScale - when the viewport's scale changes + * @trigger InvalidateViewport - when the viewport changes + * + * Viewport is essentially a 2D camera looking at the stage. Can be moved which + * in turn will react just like a camera moving in that direction. + */ + viewport: { + /**@ + * #Crafty.viewport.clampToEntities + * @comp Crafty.viewport + * + * Decides if the viewport functions should clamp to game entities. + * When set to `true` functions such as Crafty.viewport.mouselook() will not allow you to move the + * viewport over areas of the game that has no entities. + * For development it can be useful to set this to false. + */ + clampToEntities: true, + width: 0, + height: 0, + /**@ + * #Crafty.viewport.x + * @comp Crafty.viewport + * + * Will move the stage and therefore every visible entity along the `x` + * axis in the opposite direction. + * + * When this value is set, it will shift the entire stage. This means that entity + * positions are not exactly where they are on screen. To get the exact position, + * simply add `Crafty.viewport.x` onto the entities `x` position. + */ + _x: 0, + /**@ + * #Crafty.viewport.y + * @comp Crafty.viewport + * + * Will move the stage and therefore every visible entity along the `y` + * axis in the opposite direction. + * + * When this value is set, it will shift the entire stage. This means that entity + * positions are not exactly where they are on screen. To get the exact position, + * simply add `Crafty.viewport.y` onto the entities `y` position. + */ + _y: 0, + + /**@ + * #Crafty.viewport._scale + * @comp Crafty.viewport + * + * What scale to render the viewport at. This does not alter the size of the stage itself, but the magnification of what it shows. + */ + + _scale: 1, + + /**@ + * #Crafty.viewport.bounds + * @comp Crafty.viewport + * + * A rectangle which defines the bounds of the viewport. + * It should be an object with two properties, `max` and `min`, + * which are each an object with `x` and `y` properties. + * + * If this property is null, Crafty uses the bounding box of all the items + * on the stage. This is the initial value. + * + * @example + * Set the bounds to a 500 by 500 square: + * + * ~~~ + * Crafty.viewport.bounds = {min:{x:0, y:0}, max:{x:500, y:500}}; + * ~~~ + */ + bounds: null, + + /**@ + * #Crafty.viewport.scroll + * @comp Crafty.viewport + * @sign Crafty.viewport.scroll(String axis, Number v) + * @param axis - 'x' or 'y' + * @param v - The new absolute position on the axis + * + * Will move the viewport to the position given on the specified axis + * + * @example + * Will move the camera 500 pixels right of its initial position, in effect + * shifting everything in the viewport 500 pixels to the left. + * + * ~~~ + * Crafty.viewport.scroll('_x', 500); + * ~~~ + */ + scroll: function (axis, v) { + v = Math.floor(v); + this[axis] = v; + Crafty.trigger("ViewportScroll"); + Crafty.trigger("InvalidateViewport"); + }, + + rect: function () { + return { + _x: -this._x / this._scale, + _y: -this._y / this._scale, + _w: this.width / this._scale, + _h: this.height / this._scale + }; + }, + + /**@ + * #Crafty.viewport.pan + * @comp Crafty.viewport + * @sign public void Crafty.viewport.pan(String axis, Number v, Number time) + * @param String axis - 'x' or 'y'. The axis to move the camera on + * @param Number v - the distance to move the camera by + * @param Number time - The duration in frames for the entire camera movement + * + * Pans the camera a given number of pixels over a given number of frames + */ + pan: (function () { + var tweens = {}, i, bound = false; + + function enterFrame(e) { + var l = 0; + for (var i in tweens) { + var prop = tweens[i]; + if (prop.remTime > 0) { + prop.current += prop.diff; + prop.remTime--; + Crafty.viewport[i] = Math.floor(prop.current); + l++; + } else { + delete tweens[i]; + } + } + if (l) Crafty.viewport._clamp(); + } + + return function (axis, v, time) { + Crafty.viewport.follow(); + if (axis == 'reset') { + for (var i in tweens) { + tweens[i].remTime = 0; + } + return; + } + if (time === 0) time = 1; + tweens[axis] = { + diff: -v / time, + current: Crafty.viewport[axis], + remTime: time + }; + if (!bound) { + Crafty.bind("EnterFrame", enterFrame); + bound = true; + } + }; + })(), + + /**@ + * #Crafty.viewport.follow + * @comp Crafty.viewport + * @sign public void Crafty.viewport.follow(Object target, Number offsetx, Number offsety) + * @param Object target - An entity with the 2D component + * @param Number offsetx - Follow target should be offsetx pixels away from center + * @param Number offsety - Positive puts target to the right of center + * + * Follows a given entity with the 2D component. If following target will take a portion of + * the viewport out of bounds of the world, following will stop until the target moves away. + * + * @example + * ~~~ + * var ent = Crafty.e('2D, DOM').attr({w: 100, h: 100:}); + * Crafty.viewport.follow(ent, 0, 0); + * ~~~ + */ + follow: (function () { + var oldTarget, offx, offy; + + function change() { + Crafty.viewport.scroll('_x', -(this.x + (this.w / 2) - (Crafty.viewport.width / 2) - offx)); + Crafty.viewport.scroll('_y', -(this.y + (this.h / 2) - (Crafty.viewport.height / 2) - offy)); + Crafty.viewport._clamp(); + } + + return function (target, offsetx, offsety) { + if (oldTarget) + oldTarget.unbind('Change', change); + if (!target || !target.has('2D')) + return; + Crafty.viewport.pan('reset'); + + oldTarget = target; + offx = (typeof offsetx != 'undefined') ? offsetx : 0; + offy = (typeof offsety != 'undefined') ? offsety : 0; + + target.bind('Change', change); + change.call(target); + }; + })(), + + /**@ + * #Crafty.viewport.centerOn + * @comp Crafty.viewport + * @sign public void Crafty.viewport.centerOn(Object target, Number time) + * @param Object target - An entity with the 2D component + * @param Number time - The number of frames to perform the centering over + * + * Centers the viewport on the given entity + */ + centerOn: function (targ, time) { + var x = targ.x + Crafty.viewport.x, + y = targ.y + Crafty.viewport.y, + mid_x = targ.w / 2, + mid_y = targ.h / 2, + cent_x = Crafty.viewport.width / 2, + cent_y = Crafty.viewport.height / 2, + new_x = x + mid_x - cent_x, + new_y = y + mid_y - cent_y; + + Crafty.viewport.pan('reset'); + Crafty.viewport.pan('x', new_x, time); + Crafty.viewport.pan('y', new_y, time); + }, + /**@ + * #Crafty.viewport._zoom + * @comp Crafty.viewport + * + * This value keeps an amount of viewport zoom, required for calculating mouse position at entity + */ + _zoom: 1, + + /**@ + * #Crafty.viewport.zoom + * @comp Crafty.viewport + * @sign public void Crafty.viewport.zoom(Number amt, Number cent_x, Number cent_y, Number time) + * @param Number amt - amount to zoom in on the target by (eg. 2, 4, 0.5) + * @param Number cent_x - the center to zoom on + * @param Number cent_y - the center to zoom on + * @param Number time - the duration in frames of the entire zoom operation + * + * Zooms the camera in on a given point. amt > 1 will bring the camera closer to the subject + * amt < 1 will bring it farther away. amt = 0 will do nothing. + * Zooming is multiplicative. To reset the zoom amount, pass 0. + */ + zoom: (function () { + var zoom = 1, + zoom_tick = 0, + dur = 0, + prop = Crafty.support.prefix + "Transform", + bound = false, + act = {}, + prct = {}; + // what's going on: + // 1. Get the original point as a percentage of the stage + // 2. Scale the stage + // 3. Get the new size of the stage + // 4. Get the absolute position of our point using previous percentage + // 4. Offset inner by that much + + function enterFrame() { + if (dur > 0) { + if (isFinite(Crafty.viewport._zoom)) zoom = Crafty.viewport._zoom; + var old = { + width: act.width * zoom, + height: act.height * zoom + }; + zoom += zoom_tick; + Crafty.viewport._zoom = zoom; + var new_s = { + width: act.width * zoom, + height: act.height * zoom + }, + diff = { + width: new_s.width - old.width, + height: new_s.height - old.height + }; + Crafty.stage.inner.style[prop] = 'scale(' + zoom + ',' + zoom + ')'; + if (Crafty.canvas._canvas) { + var czoom = zoom / (zoom - zoom_tick); + Crafty.canvas.context.scale(czoom, czoom); + Crafty.trigger("InvalidateViewport"); + } + Crafty.viewport.x -= diff.width * prct.width; + Crafty.viewport.y -= diff.height * prct.height; + dur--; + } + } + + return function (amt, cent_x, cent_y, time) { + var bounds = this.bounds || Crafty.map.boundaries(), + final_zoom = amt ? zoom * amt : 1; + if (!amt) { // we're resetting to defaults + zoom = 1; + this._zoom = 1; + } + + act.width = bounds.max.x - bounds.min.x; + act.height = bounds.max.y - bounds.min.y; + + prct.width = cent_x / act.width; + prct.height = cent_y / act.height; + + if (time === 0) time = 1; + zoom_tick = (final_zoom - zoom) / time; + dur = time; + + Crafty.viewport.pan('reset'); + if (!bound) { + Crafty.bind('EnterFrame', enterFrame); + bound = true; + } + }; + })(), + /**@ + * #Crafty.viewport.scale + * @comp Crafty.viewport + * @sign public void Crafty.viewport.scale(Number amt) + * @param Number amt - amount to zoom/scale in on the element on the viewport by (eg. 2, 4, 0.5) + * + * Zooms/scale the camera. amt > 1 increase all entities on stage + * amt < 1 will reduce all entities on stage. amt = 0 will reset the zoom/scale. + * Zooming/scaling is multiplicative. To reset the zoom/scale amount, pass 0. + * + * @example + * ~~~ + * Crafty.viewport.scale(2); //to see effect add some entities on stage. + * ~~~ + */ + scale: (function () { + return function (amt) { + var bounds = this.bounds || Crafty.map.boundaries(), + final_zoom = amt ? amt : 1; + + + this._zoom = final_zoom; + this._scale = final_zoom; + Crafty.trigger("InvalidateViewport"); + Crafty.trigger("ViewportScale"); + + }; + })(), + /**@ + * #Crafty.viewport.mouselook + * @comp Crafty.viewport + * @sign public void Crafty.viewport.mouselook(Boolean active) + * @param Boolean active - Activate or deactivate mouselook + * + * Toggle mouselook on the current viewport. + * Simply call this function and the user will be able to + * drag the viewport around. + */ + mouselook: (function () { + var active = false, + dragging = false, + lastMouse = {}; + old = {}; + + + return function (op, arg) { + if (typeof op == 'boolean') { + active = op; + if (active) { + Crafty.mouseObjs++; + } else { + Crafty.mouseObjs = Math.max(0, Crafty.mouseObjs - 1); + } + return; + } + if (!active) return; + switch (op) { + case 'move': + case 'drag': + if (!dragging) return; + diff = { + x: arg.clientX - lastMouse.x, + y: arg.clientY - lastMouse.y + }; + + lastMouse.x = arg.clientX; + lastMouse.y = arg.clientY; + + Crafty.viewport.x += diff.x; + Crafty.viewport.y += diff.y; + Crafty.viewport._clamp(); + break; + case 'start': + lastMouse.x = arg.clientX; + lastMouse.y = arg.clientY; + dragging = true; + break; + case 'stop': + dragging = false; + break; + } + }; + })(), + _clamp: function () { + // clamps the viewport to the viewable area + // under no circumstances should the viewport see something outside the boundary of the 'world' + if (!this.clampToEntities) return; + var bound = this.bounds || Crafty.map.boundaries(); + bound.max.x *= this._zoom; + bound.min.x *= this._zoom; + bound.max.y *= this._zoom; + bound.min.y *= this._zoom; + if (bound.max.x - bound.min.x > Crafty.viewport.width) { + bound.max.x -= Crafty.viewport.width; + + if (Crafty.viewport.x < -bound.max.x) { + Crafty.viewport.x = -bound.max.x; + } else if (Crafty.viewport.x > -bound.min.x) { + Crafty.viewport.x = -bound.min.x; + } + } else { + Crafty.viewport.x = -1 * (bound.min.x + (bound.max.x - bound.min.x) / 2 - Crafty.viewport.width / 2); + } + if (bound.max.y - bound.min.y > Crafty.viewport.height) { + bound.max.y -= Crafty.viewport.height; + + if (Crafty.viewport.y < -bound.max.y) { + Crafty.viewport.y = -bound.max.y; + } else if (Crafty.viewport.y > -bound.min.y) { + Crafty.viewport.y = -bound.min.y; + } + } else { + Crafty.viewport.y = -1 * (bound.min.y + (bound.max.y - bound.min.y) / 2 - Crafty.viewport.height / 2); + } + }, + + /**@ + * #Crafty.viewport.init + * @comp Crafty.viewport + * @sign public void Crafty.viewport.init([Number width, Number height, String stage_elem]) + * @sign public void Crafty.viewport.init([Number width, Number height, HTMLElement stage_elem]) + * @param Number width - Width of the viewport + * @param Number height - Height of the viewport + * @param String or HTMLElement stage_elem - the element to use as the stage (either its id or the actual element). + * + * Initialize the viewport. If the arguments 'width' or 'height' are missing, or Crafty.mobile is true, use Crafty.DOM.window.width and Crafty.DOM.window.height (full screen model). + * + * The argument 'stage_elem' is used to specify a stage element other than the default, and can be either a string or an HTMLElement. If a string is provided, it will look for an element with that id and, if none exists, create a div. If an HTMLElement is provided, that is used directly. Omitting this argument is the same as passing an id of 'cr-stage'. + * + * @see Crafty.device, Crafty.DOM, Crafty.stage + */ + init: function (w, h, stage_elem) { + Crafty.DOM.window.init(); + + //fullscreen if mobile or not specified + this.width = (!w || Crafty.mobile) ? Crafty.DOM.window.width : w; + this.height = (!h || Crafty.mobile) ? Crafty.DOM.window.height : h; + + //check if stage exists + if (typeof stage_elem === 'undefined') + stage_elem = "cr-stage"; + + var crstage; + if (typeof stage_elem === 'string') + crstage = document.getElementById(stage_elem); + else if (typeof HTMLElement !== "undefined" ? stage_elem instanceof HTMLElement : stage_elem instanceof Element) + crstage = stage_elem; + else + throw new TypeError("stage_elem must be a string or an HTMLElement"); + + /**@ + * #Crafty.stage + * @category Core + * The stage where all the DOM entities will be placed. + */ + + /**@ + * #Crafty.stage.elem + * @comp Crafty.stage + * The `#cr-stage` div element. + */ + + /**@ + * #Crafty.stage.inner + * @comp Crafty.stage + * `Crafty.stage.inner` is a div inside the `#cr-stage` div that holds all DOM entities. + * If you use canvas, a `canvas` element is created at the same level in the dom + * as the the `Crafty.stage.inner` div. So the hierarchy in the DOM is + * + * `Crafty.stage.elem` + * + * + * - `Crafty.stage.inner` (a div HTMLElement) + * + * - `Crafty.canvas._canvas` (a canvas HTMLElement) + */ + + //create stage div to contain everything + Crafty.stage = { + x: 0, + y: 0, + fullscreen: false, + elem: (crstage ? crstage : document.createElement("div")), + inner: document.createElement("div") + }; + + //fullscreen, stop scrollbars + if ((!w && !h) || Crafty.mobile) { + document.body.style.overflow = "hidden"; + Crafty.stage.fullscreen = true; + } + + Crafty.addEvent(this, window, "resize", Crafty.viewport.reload); + + Crafty.addEvent(this, window, "blur", function () { + if (Crafty.settings.get("autoPause")) { + if (!Crafty._paused) Crafty.pause(); + } + }); + Crafty.addEvent(this, window, "focus", function () { + if (Crafty._paused && Crafty.settings.get("autoPause")) { + Crafty.pause(); + } + }); + + //make the stage unselectable + Crafty.settings.register("stageSelectable", function (v) { + Crafty.stage.elem.onselectstart = v ? function () { + return true; + } : function () { + return false; + }; + }); + Crafty.settings.modify("stageSelectable", false); + + //make the stage have no context menu + Crafty.settings.register("stageContextMenu", function (v) { + Crafty.stage.elem.oncontextmenu = v ? function () { + return true; + } : function () { + return false; + }; + }); + Crafty.settings.modify("stageContextMenu", false); + + Crafty.settings.register("autoPause", function () {}); + Crafty.settings.modify("autoPause", false); + + //add to the body and give it an ID if not exists + if (!crstage) { + document.body.appendChild(Crafty.stage.elem); + Crafty.stage.elem.id = stage_elem; + } + + var elem = Crafty.stage.elem.style, + offset; + + Crafty.stage.elem.appendChild(Crafty.stage.inner); + Crafty.stage.inner.style.position = "absolute"; + Crafty.stage.inner.style.zIndex = "1"; + Crafty.stage.inner.style.transformStyle = "preserve-3d"; // Seems necessary for Firefox to preserve zIndexes? + + //css style + elem.width = this.width + "px"; + elem.height = this.height + "px"; + elem.overflow = "hidden"; + + if (Crafty.mobile) { + elem.position = "absolute"; + elem.left = "0px"; + elem.top = "0px"; + + // remove default gray highlighting after touch + if (typeof elem.webkitTapHighlightColor !== undefined) { + elem.webkitTapHighlightColor = "rgba(0,0,0,0)"; + } + + var meta = document.createElement("meta"), + head = document.getElementsByTagName("HEAD")[0]; + + //stop mobile zooming and scrolling + meta.setAttribute("name", "viewport"); + meta.setAttribute("content", "width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"); + head.appendChild(meta); + + //hide the address bar + meta = document.createElement("meta"); + meta.setAttribute("name", "apple-mobile-web-app-capable"); + meta.setAttribute("content", "yes"); + head.appendChild(meta); + setTimeout(function () { + window.scrollTo(0, 1); + }, 0); + + Crafty.addEvent(this, window, "touchmove", function (e) { + e.preventDefault(); + }); + + Crafty.stage.x = 0; + Crafty.stage.y = 0; + + } else { + elem.position = "relative"; + //find out the offset position of the stage + offset = Crafty.DOM.inner(Crafty.stage.elem); + Crafty.stage.x = offset.x; + Crafty.stage.y = offset.y; + } + + if (Crafty.support.setter) { + //define getters and setters to scroll the viewport + this.__defineSetter__('x', function (v) { + this.scroll('_x', v); + }); + this.__defineSetter__('y', function (v) { + this.scroll('_y', v); + }); + this.__defineGetter__('x', function () { + return this._x; + }); + this.__defineGetter__('y', function () { + return this._y; + }); + + //IE9 + } else if (Crafty.support.defineProperty) { + Object.defineProperty(this, 'x', { + set: function (v) { + this.scroll('_x', v); + }, + get: function () { + return this._x; + }, + configurable : true + }); + Object.defineProperty(this, 'y', { + set: function (v) { + this.scroll('_y', v); + }, + get: function () { + return this._y; + }, + configurable : true + }); + } else { + // IE8 has no getter/setters -- Check for an update each frame. + this.x = this._x; + this.y = this._y; + Crafty.bind("EnterFrame", function () { + if (Crafty.viewport._x !== Crafty.viewport.x) { + Crafty.viewport.scroll('_x', Crafty.viewport.x); + } + + if (Crafty.viewport._y !== Crafty.viewport.y) { + Crafty.viewport.scroll('_y', Crafty.viewport.y); + } + }); + } + }, + + /**@ + * #Crafty.viewport.reload + * @comp Crafty.stage + * + * @sign public Crafty.viewport.reload() + * + * Recalculate and reload stage width, height and position. + * Useful when browser return wrong results on init (like safari on Ipad2). + * + */ + reload: function () { + Crafty.DOM.window.init(); + var w = Crafty.DOM.window.width, + h = Crafty.DOM.window.height, + offset; + + + if (Crafty.stage.fullscreen) { + this.width = w; + this.height = h; + Crafty.stage.elem.style.width = w + "px"; + Crafty.stage.elem.style.height = h + "px"; + + if (Crafty.canvas._canvas) { + Crafty.canvas._canvas.width = w; + Crafty.canvas._canvas.height = h; + Crafty.trigger("InvalidateViewport"); + } + } + + offset = Crafty.DOM.inner(Crafty.stage.elem); + Crafty.stage.x = offset.x; + Crafty.stage.y = offset.y; + }, + + /**@ + * #Crafty.viewport.reset + * @comp Crafty.stage + * + * @sign public Crafty.viewport.reset() + * + * Resets the viewport to starting values + * Called when scene() is run. + */ + reset: function () { + Crafty.viewport.pan('reset'); + Crafty.viewport.follow(); + Crafty.viewport.mouselook('stop'); + Crafty.viewport.scale(); + } + } +}); + +},{"./core.js":9}]},{},[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]) +; \ No newline at end of file From dfc6008ff88be093e4230081c77d01caaa74fe8c Mon Sep 17 00:00:00 2001 From: Tim Martin Date: Fri, 6 Dec 2013 16:05:16 -0500 Subject: [PATCH 2/2] Bump version to 0.6.1 (cherry picked from commit 182b534f8f8ad2f51ed8a57458f75207cacb56c8) Conflicts: bower.json --- bower.json | 4 ++-- package.json | 2 +- src/version.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bower.json b/bower.json index 1ff74172..0ae34de8 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "crafty", - "main": "dist/crafty.js", - "version": "0.6", + "main": "crafty.js", + "version": "0.6.1", "repository": { "type": "git", "url": "git://github.com/craftyjs/Crafty.git" diff --git a/package.json b/package.json index b1fa5766..01b60eb6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crafty", - "version": "0.6.0", + "version": "0.6.1", "title": "Crafty game framework", "author": { "name": "Louis Stowasser", diff --git a/src/version.js b/src/version.js index 218121bd..30bb93b6 100644 --- a/src/version.js +++ b/src/version.js @@ -1 +1 @@ -module.exports = "0.6.0"; \ No newline at end of file +module.exports = "0.6.1"; \ No newline at end of file