forked from wise9/enchant.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenchant.min.js
75 lines (75 loc) · 33.4 KB
/
enchant.min.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
enchant.js v0.4.3
Copyright (c) Ubiquitous Entertainment Inc.
Dual licensed under the MIT or GPL Version 3 licenses
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl-3.0.html
*/
"function"!=typeof Object.defineProperty&&(Object.defineProperty=function(g,l,f){"value"in f&&(g[l]=f.value);"get"in f&&g.__defineGetter__(l,f.get);"set"in f&&g.__defineSetter__(l,f.set);return g});"function"!=typeof Object.defineProperties&&(Object.defineProperties=function(g,l){for(var f in l)l.hasOwnProperty(f)&&Object.defineProperty(g,f,l[f]);return g});
"function"!=typeof Object.create&&(Object.create=function(g,l){function f(){}f.prototype=g;var c=new f;null!=l&&Object.defineProperties(c,l);return c});"function"!=typeof Object.getPrototypeOf&&(Object.getPrototypeOf=function(g){return g.__proto__});
var enchant=function(g){null!=g&&(g instanceof Array||(g=Array.prototype.slice.call(arguments)),g=g.filter(function(g){return""+g}));(function f(c,n){var a=[],b;for(b in c)c.hasOwnProperty(b)&&("function"==typeof c[b]?window[b]=c[b]:"object"==typeof c[b]&&Object.getPrototypeOf(c[b])==Object.prototype&&(null==g?a.push(b):(e=g.indexOf(n+b),-1!=e&&(a.push(b),g.splice(e,1)))));var e=0;for(b=a.length;e<b;e++)f(c[a[e]],n+a[e]+".")})(enchant,"");if(null!=g&&g.length)throw Error("Cannot load module: "+g.join(", "));
};
(function(){var g=function(){var a=navigator.userAgent;return-1!=a.indexOf("Opera")?"O":-1!=a.indexOf("MSIE")?"ms":-1!=a.indexOf("WebKit")?"webkit":"Gecko"==navigator.product?"Moz":""}(),l=function(){var a=document.createElement("div");a.setAttribute("ontouchstart","return");return"function"==typeof a.ontouchstart}(),f=function(){if(-1!=navigator.userAgent.indexOf("iPhone")&&2==window.devicePixelRatio){var a=document.querySelector('meta[name="viewport"]');null==a&&(a=document.createElement("meta"),document.head.appendChild(a));
a.setAttribute("content","width=640px");return!0}return!1}(),c;enchant.Class=function(a,b){return enchant.Class.create(a,b)};enchant.Class.create=function(a,b){if(0==arguments.length)return enchant.Class.create(Object,b);if(1==arguments.length&&"function"!=typeof arguments[0])return enchant.Class.create(Object,arguments[0]);for(var e in b)b.hasOwnProperty(e)&&("object"==typeof b[e]&&Object.getPrototypeOf(b[e])==Object.prototype?"enumerable"in b[e]||(b[e].enumerable=!0):b[e]={value:b[e],enumerable:!0,
writable:!0});var d=function(){if(this instanceof d)d.prototype.initialize.apply(this,arguments);else return new d};d.prototype=Object.create(a.prototype,b);d.prototype.constructor=d;null==d.prototype.initialize&&(d.prototype.initialize=function(){a.apply(this,arguments)});return d};enchant.Event=enchant.Class.create({initialize:function(a){this.type=a;this.target=null;this.localY=this.localX=this.y=this.x=0},_initPosition:function(a,b){this.x=this.localX=(a-c._pageX)/c.scale;this.y=this.localY=(b-
c._pageY)/c.scale}});enchant.Event.LOAD="load";enchant.Event.PROGRESS="progress";enchant.Event.ENTER_FRAME="enterframe";enchant.Event.EXIT_FRAME="exitframe";enchant.Event.ENTER="enter";enchant.Event.EXIT="exit";enchant.Event.ADDED="added";enchant.Event.ADDED_TO_SCENE="addedtoscene";enchant.Event.REMOVED="removed";enchant.Event.REMOVED_FROM_SCENE="removedfromscene";enchant.Event.TOUCH_START="touchstart";enchant.Event.TOUCH_MOVE="touchmove";enchant.Event.TOUCH_END="touchend";enchant.Event.RENDER="render";
enchant.Event.INPUT_START="inputstart";enchant.Event.INPUT_CHANGE="inputchange";enchant.Event.INPUT_END="inputend";enchant.Event.LEFT_BUTTON_DOWN="leftbuttondown";enchant.Event.LEFT_BUTTON_UP="leftbuttonup";enchant.Event.RIGHT_BUTTON_DOWN="rightbuttondown";enchant.Event.RIGHT_BUTTON_UP="rightbuttonup";enchant.Event.UP_BUTTON_DOWN="upbuttondown";enchant.Event.UP_BUTTON_UP="upbuttonup";enchant.Event.DOWN_BUTTON_DOWN="downbuttondown";enchant.Event.DOWN_BUTTON_UP="downbuttonup";enchant.Event.A_BUTTON_DOWN=
"abuttondown";enchant.Event.A_BUTTON_UP="abuttonup";enchant.Event.B_BUTTON_DOWN="bbuttondown";enchant.Event.B_BUTTON_UP="bbuttonup";enchant.EventTarget=enchant.Class.create({initialize:function(){this._listeners={}},addEventListener:function(a,b){var e=this._listeners[a];null==e?this._listeners[a]=[b]:-1==e.indexOf(b)&&e.unshift(b)},removeEventListener:function(a,b){var e=this._listeners[a];if(null!=e){var d=e.indexOf(b);-1!=d&&e.splice(d,1)}},clearEventListener:function(a){null!=a?delete this._listeners[a]:
this._listeners={}},dispatchEvent:function(a){a.target=this;a.localX=a.x-this._offsetX;a.localY=a.y-this._offsetY;if(null!=this["on"+a.type])this["on"+a.type]();var b=this._listeners[a.type];if(null!=b)for(var b=b.slice(),e=0,d=b.length;e<d;e++)b[e].call(this,a)}});enchant.Game=enchant.Class.create(enchant.EventTarget,{initialize:function(a,b){if(null===window.document.body)throw Error("document.body is null. Please excute 'new Game()' in window.onload.");enchant.EventTarget.call(this);var e=!0;c&&
(e=!1,c.stop());c=enchant.Game.instance=this;this.width=a||320;this.height=b||320;this.scale=1;var d=document.getElementById("enchant-stage");if(d){var m=window.getComputedStyle(d),a=parseInt(m.width),b=parseInt(m.height);a&&b?this.scale=Math.min(a/this.width,b/this.height):(d.style.width=this.width+"px",d.style.height=this.height+"px");for(;d.firstChild;)d.removeChild(d.firstChild);d.style.position="relative";m=d.getBoundingClientRect();this._pageX=Math.round(window.scrollX+m.left);this._pageY=Math.round(window.scrollY+
m.top)}else d=document.createElement("div"),d.id="enchant-stage",d.style.width=window.innerWidth+"px",d.style.height=window.innerHeight+"px",d.style.position="absolute",document.body.firstChild?document.body.insertBefore(d,document.body.firstChild):document.body.appendChild(d),this.scale=Math.min(window.innerWidth/this.width,window.innerHeight/this.height),this._pageY=this._pageX=0;this.scale||(this.scale=1);d.style.fontSize="12px";d.style.webkitTextSizeAdjust="none";this._element=d;this.fps=30;this.frame=
0;this.ready=null;this.running=!1;this.assets={};var h=this._assets=[];(function q(a){a.assets instanceof Array&&[].push.apply(h,a.assets);for(var b in a)a.hasOwnProperty(b)&&"object"==typeof a[b]&&Object.getPrototypeOf(a[b])==Object.prototype&&q(a[b])})(enchant);this._scenes=[];this.currentScene=null;this.rootScene=new enchant.Scene;this.pushScene(this.rootScene);this.loadingScene=new enchant.Scene;this.loadingScene.backgroundColor="#000";var i=0.9*this.width|0,o=0.3*this.width|0,k=0.05*i|0,d=new enchant.Sprite(i,
o);d.x=(this.width-i)/2;d.y=(this.height-o)/2;var j=new enchant.Surface(i,o);j.context.fillStyle="#fff";j.context.fillRect(0,0,i,o);j.context.fillStyle="#000";j.context.fillRect(k,k,i-2*k,o-2*k);d.image=j;var g=0,f=0;this.addEventListener("progress",function(a){g=a.loaded/a.total});d.addEventListener("enterframe",function(){f*=0.9;f+=0.1*g;j.context.fillStyle="#fff";j.context.fillRect(k,0,(i-2*k)*f,o)});this.loadingScene.addChild(d);this._soundID=this._surfaceID=this._mousedownID=0;this._intervalID=
null;this.input={};this._keybind={};this.keybind(37,"left");this.keybind(38,"up");this.keybind(39,"right");this.keybind(40,"down");var n=0;"left,right,up,down,a,b".split(",").forEach(function(a){this.addEventListener(a+"buttondown",function(b){this.input[a]||(this.input[a]=!0,this.dispatchEvent(new enchant.Event(n++?"inputchange":"inputstart")));this.currentScene.dispatchEvent(b)});this.addEventListener(a+"buttonup",function(b){this.input[a]&&(this.input[a]=!1,this.dispatchEvent(new enchant.Event(--n?
"inputchange":"inputend")));this.currentScene.dispatchEvent(b)})},this);e&&(document.addEventListener("keydown",function(a){c.dispatchEvent(new enchant.Event("keydown"));if(37<=a.keyCode&&40>=a.keyCode||32==a.keyCode)a.preventDefault(),a.stopPropagation();if(c.running&&(a=c._keybind[a.keyCode]))a=new enchant.Event(a+"buttondown"),c.dispatchEvent(a)},!0),document.addEventListener("keyup",function(a){if(c.running&&(a=c._keybind[a.keyCode]))a=new enchant.Event(a+"buttonup"),c.dispatchEvent(a)},!0),l?
(document.addEventListener("touchstart",function(a){a.preventDefault()},!0),document.addEventListener("touchmove",function(a){a.preventDefault();c.running||a.stopPropagation()},!0),document.addEventListener("touchend",function(a){a.preventDefault();c.running||a.stopPropagation()},!0)):(document.addEventListener("mousedown",function(a){a.preventDefault();c._mousedownID++;c.running||a.stopPropagation()},!0),document.addEventListener("mousemove",function(a){a.preventDefault();c.running||a.stopPropagation()},
!0),document.addEventListener("mouseup",function(a){a.preventDefault();c.running||a.stopPropagation()},!0)))},preload:function(a){a instanceof Array||(a=Array.prototype.slice.call(arguments));[].push.apply(this._assets,a)},load:function(a,b){null==b&&(b=function(){});var e=a.match(/\.\w+$/)[0];e&&(e=e.slice(1).toLowerCase());switch(e){case "jpg":case "gif":case "png":c.assets[a]=enchant.Surface.load(a);c.assets[a].addEventListener("load",b);break;case "mp3":case "aac":case "m4a":case "wav":case "ogg":c.assets[a]=
enchant.Sound.load(a,"audio/"+e);c.assets[a].addEventListener("load",b);break;default:var d=new XMLHttpRequest;d.open("GET",a,!0);d.onreadystatechange=function(){if(4==d.readyState){if(200!=d.status&&0!=d.status)throw Error(d.status+": Cannot load an asset: "+a);var e=d.getResponseHeader("Content-Type")||"";e.match(/^image/)?(c.assets[a]=enchant.Surface.load(a),c.assets[a].addEventListener("load",b)):e.match(/^audio/)?(c.assets[a]=enchant.Sound.load(a,e),c.assets[a].addEventListener("load",b)):(c.assets[a]=
d.responseText,b())}};d.send(null)}},start:function(){if(this._intervalID)window.clearInterval(this._intervalID);else if(this._assets.length){if(enchant.Sound.enabledInMobileSafari&&!c._touched&&"webkit"==g&&l){var a=new enchant.Scene;a.backgroundColor="#000";var b=Math.round(c.width/10),e=new enchant.Sprite(c.width,b);e.y=(c.height-b)/2;e.image=new enchant.Surface(c.width,b);e.image.context.fillStyle="#fff";e.image.context.font=b-1+"px bold Helvetica,Arial,sans-serif";var d=e.image.context.measureText("Touch to Start").width;
e.image.context.fillText("Touch to Start",(c.width-d)/2,b-1);a.addChild(e);document.addEventListener("touchstart",function(){c._touched=!0;c.removeScene(a);c.start()},!0);c.pushScene(a);return}var m={},b=this._assets.filter(function(a){return a in m?!1:m[a]=!0}),h=0,e=0,i=b.length;for(;e<i;e++)this.load(b[e],function(){var a=new enchant.Event("progress");a.loaded=++h;a.total=i;c.dispatchEvent(a);h==i&&(c.removeScene(c.loadingScene),c.dispatchEvent(new enchant.Event("load")))});this.pushScene(this.loadingScene)}else this.dispatchEvent(new enchant.Event("load"));
this.currentTime=Date.now();this._intervalID=window.setInterval(function(){c._tick()},1E3/this.fps);this.running=!0},debug:function(){this._debug=!0;this.rootScene.addEventListener("enterframe",function(a){this._actualFps=1/a});this.start()},actualFps:{get:function(){return this._actualFps||this.fps}},_tick:function(){var a=Date.now(),b=new enchant.Event("enterframe");b.elapsed=a-this.currentTime;this.currentTime=a;for(var a=this.currentScene.childNodes.slice(),e=Array.prototype.push;a.length;){var d=
a.pop();d.dispatchEvent(b);d.age++;d.childNodes&&e.apply(a,d.childNodes)}this.currentScene.dispatchEvent(b);this.dispatchEvent(b);this.dispatchEvent(new enchant.Event("exitframe"));this.frame++},stop:function(){this._intervalID&&(window.clearInterval(this._intervalID),this._intervalID=null);this.running=!1},pause:function(){this._intervalID&&(window.clearInterval(this._intervalID),this._intervalID=null)},resume:function(){this.currentTime=Date.now();this._intervalID=window.setInterval(function(){c._tick()},
1E3/this.fps);this.running=!0},pushScene:function(a){this._element.appendChild(a._element);this.currentScene&&this.currentScene.dispatchEvent(new enchant.Event("exit"));this.currentScene=a;this.currentScene.dispatchEvent(new enchant.Event("enter"));return this._scenes.push(a)},popScene:function(){if(this.currentScene==this.rootScene)return this.currentScene;this._element.removeChild(this.currentScene._element);this.currentScene.dispatchEvent(new enchant.Event("exit"));this.currentScene=this._scenes[this._scenes.length-
2];this.currentScene.dispatchEvent(new enchant.Event("enter"));return this._scenes.pop()},replaceScene:function(a){this.popScene();return this.pushScene(a)},removeScene:function(a){if(this.currentScene==a)return this.popScene();var b=this._scenes.indexOf(a);if(-1!=b)return this._scenes.splice(b,1),this._element.removeChild(a._element),a},keybind:function(a,b){this._keybind[a]=b}});enchant.Game.instance=null;enchant.Node=enchant.Class.create(enchant.EventTarget,{initialize:function(){enchant.EventTarget.call(this);
this.age=this._offsetY=this._offsetX=this._y=this._x=0;this.scene=this.parentNode=null;this.addEventListener("touchstart",function(a){this.parentNode&&this.parentNode!=this.scene&&this.parentNode.dispatchEvent(a)});this.addEventListener("touchmove",function(a){this.parentNode&&this.parentNode!=this.scene&&this.parentNode.dispatchEvent(a)});this.addEventListener("touchend",function(a){this.parentNode&&this.parentNode!=this.scene&&this.parentNode.dispatchEvent(a)})},moveTo:function(a,b){this._x=a;this._y=
b;this._updateCoordinate()},moveBy:function(a,b){this._x+=a;this._y+=b;this._updateCoordinate()},x:{get:function(){return this._x},set:function(a){this._x=a;this._updateCoordinate()}},y:{get:function(){return this._y},set:function(a){this._y=a;this._updateCoordinate()}},_updateCoordinate:function(){this.parentNode?(this._offsetX=this.parentNode._offsetX+this._x,this._offsetY=this.parentNode._offsetY+this._y):(this._offsetX=this._x,this._offsetY=this._y)},remove:function(){this._listener&&this.clearEventListener();
this.parentNode&&this.removeChild(this)}});enchant.Entity=enchant.Class.create(enchant.Node,{initialize:function(){enchant.Node.call(this);this._element=document.createElement("div");this._style=this._element.style;this._style.position="absolute";this._height=this._width=0;this._backgroundColor=null;this._opacity=1;this._visible=!0;this._buttonMode=null;enchant.Game.instance._debug&&(this._style.border="1px solid blue",this._style.margin="-1px");this.buttonMode=null;this.buttonPressed=!1;this.addEventListener("touchstart",
function(){if(this.buttonMode){this.buttonPressed=!0;var a=new enchant.Event(this.buttonMode+"buttondown");this.dispatchEvent(a);c.dispatchEvent(a)}});this.addEventListener("touchend",function(){if(this.buttonMode){this.buttonPressed=!1;var a=new enchant.Event(this.buttonMode+"buttonup");this.dispatchEvent(a);c.dispatchEvent(a)}});var a=this,b=function(){a.dispatchEvent(new enchant.Event("render"))};this.addEventListener("addedtoscene",function(){b();c.addEventListener("exitframe",b)});this.addEventListener("removedfromscene",
function(){c.removeEventListener("exitframe",b)});this.addEventListener("render",function(){this._offsetX!=this._previousOffsetX&&(this._style.left=this._offsetX+"px");this._offsetY!=this._previousOffsetY&&(this._style.top=this._offsetY+"px");this._previousOffsetX=this._offsetX;this._previousOffsetY=this._offsetY});a=this;l?(this._element.addEventListener("touchstart",function(b){for(var d=b.touches,c=0,h=d.length;c<h;c++)b=new enchant.Event("touchstart"),b.identifier=d[c].identifier,b._initPosition(d[c].pageX,
d[c].pageY),a.dispatchEvent(b)},!1),this._element.addEventListener("touchmove",function(b){for(var d=b.touches,c=0,h=d.length;c<h;c++)b=new enchant.Event("touchmove"),b.identifier=d[c].identifier,b._initPosition(d[c].pageX,d[c].pageY),a.dispatchEvent(b)},!1),this._element.addEventListener("touchend",function(b){for(var d=b.changedTouches,c=0,h=d.length;c<h;c++)b=new enchant.Event("touchend"),b.identifier=d[c].identifier,b._initPosition(d[c].pageX,d[c].pageY),a.dispatchEvent(b)},!1)):(this._element.addEventListener("mousedown",
function(b){var d=b.pageX,m=b.pageY,b=new enchant.Event("touchstart");b.identifier=c._mousedownID;b._initPosition(d,m);a.dispatchEvent(b);a._mousedown=!0},!1),c._element.addEventListener("mousemove",function(b){if(a._mousedown){var d=b.pageX,m=b.pageY,b=new enchant.Event("touchmove");b.identifier=c._mousedownID;b._initPosition(d,m);a.dispatchEvent(b)}},!1),c._element.addEventListener("mouseup",function(b){if(a._mousedown){var d=b.pageX,m=b.pageY,b=new enchant.Event("touchend");b.identifier=c._mousedownID;
b._initPosition(d,m);a.dispatchEvent(b);a._mousedown=!1}},!1))},id:{get:function(){return this._element.id},set:function(a){this._element.id=a}},className:{get:function(){return this._element.className},set:function(a){this._element.className=a}},width:{get:function(){return this._width},set:function(a){this._style.width=(this._width=a)+"px"}},height:{get:function(){return this._height},set:function(a){this._style.height=(this._height=a)+"px"}},backgroundColor:{get:function(){return this._backgroundColor},
set:function(a){this._element.style.backgroundColor=this._backgroundColor=a}},opacity:{get:function(){return this._opacity},set:function(a){this._style.opacity=this._opacity=a}},visible:{get:function(){return this._visible},set:function(a){this._style.display=(this._visible=a)?"block":"none"}},touchEnabled:{get:function(){return this._touchEnabled},set:function(a){this._style.pointerEvents=(this._touchEnabled=a)?"all":"none"}},intersect:function(a){return this.x<a.x+a.width&&a.x<this.x+this.width&&
this.y<a.y+a.height&&a.y<this.y+this.height},within:function(a,b){null==b&&(b=(this.width+this.height+a.width+a.height)/4);var e;return(e=this.x-a.x+(this.width-a.width)/2)*e+(e=this.y-a.y+(this.height-a.height)/2)*e<b*b}});enchant.Sprite=enchant.Class.create(enchant.Entity,{initialize:function(a,b){enchant.Entity.call(this);this.width=a;this.height=b;this._scaleY=this._scaleX=1;this._rotation=0;this._dirty=!1;this._image=null;this._frame=0;this._frameSequence=[];this._style.overflow="hidden";this.addEventListener("render",
function(){this._dirty&&(this._style[g+"Transform"]=["rotate(",this._rotation,"deg)scale(",this._scaleX,",",this._scaleY,")"].join(""),this._dirty=!1)});this.addEventListener("enterframe",function(){if(0!==this._frameSequence.length){var a=this._frameSequence.shift();null===a?this._frameSequence=[]:(this._setFrame(a),this._frameSequence.push(a))}});enchant.Game.instance._debug&&(this._style.border="1px solid red",this._style.margin="-1px",this.addEventListener("touchstart",function(){enchant.Game.instance.running||
console.log("touchstart",this)}))},image:{get:function(){return this._image},set:function(a){if(a!=this._image){null!=this._image&&(this._image.css?this._style.backgroundImage="":this._element.firstChild&&(this._element.removeChild(this._element.firstChild),this._dirtyListener?(this.removeEventListener("render",this._dirtyListener),this._dirtyListener=null):this._image._parent=null));if(null!=a)if(a._css)this._style.backgroundImage=a._css;else if(a._parent){var b=document.createElement("canvas"),
e=b.getContext("2d");b.width=a.width;b.height=a.height;e.drawImage(a._element,0,0);this._dirtyListener=function(){a._dirty&&(e.drawImage(a._element),a._dirty=!1)};this.addEventListener("render",this._dirtyListener);this._element.appendChild(b)}else a._parent=this,this._element.appendChild(a._element);this._image=a;this.frame=this.frame}}},frame:{get:function(){return this._frame},set:function(a){if(a instanceof Array){console.log("sequence");var b=a.shift();this._setFrame(b);a.push(b);this._frameSequence=
a}else this._setFrame(a),this._frameSequence=[],this._frame=a}},_setFrame:function(a){if(null!=this._image){this._frame=a;var b=this._image.width/this._width|0;if(this._image._css)this._style.backgroundPosition=[-(a%b)*this._width,"px ",-(a/b|0)*this._height,"px"].join("");else if(this._element.firstChild){var e=this._element.firstChild.style;e.left=-(a%b)*this._width+"px";e.top=-(a/b|0)*this._height+"px"}}},scale:function(a,b){null==b&&(b=a);this._scaleX*=a;this._scaleY*=b;this._dirty=!0},rotate:function(a){this._rotation+=
a;this._dirty=!0},scaleX:{get:function(){return this._scaleX},set:function(a){this._scaleX=a;this._dirty=!0}},scaleY:{get:function(){return this._scaleY},set:function(a){this._scaleY=a;this._dirty=!0}},rotation:{get:function(){return this._rotation},set:function(a){this._rotation=a;this._dirty=!0}}});enchant.Label=enchant.Class.create(enchant.Entity,{initialize:function(a){enchant.Entity.call(this);this.width=300;this.text=a},text:{get:function(){return this._element.innerHTML},set:function(a){this._element.innerHTML=
a}},font:{get:function(){return this._style.font},set:function(a){this._style.font=a}},color:{get:function(){return this._style.color},set:function(a){this._style.color=a}}});enchant.Map=enchant.Class.create(enchant.Entity,{initialize:function(a,b){enchant.Entity.call(this);var e=document.createElement("canvas");f&&2==c.scale?(e.width=2*c.width,e.height=2*c.height,this._style.webkitTransformOrigin="0 0",this._style.webkitTransform="scale(0.5)"):(e.width=c.width,e.height=c.height);this._element.appendChild(e);
this._context=e.getContext("2d");this._tileWidth=a||0;this._tileHeight=b||0;this._image=null;this._data=[[[]]];this.touchEnabled=this._tight=this._dirty=!1;this.collisionData=null;this._listeners.render=null;this.addEventListener("render",function(){if(this._dirty||null==this._previousOffsetX)this._dirty=!1,this.redraw(0,0,c.width,c.height);else if(this._offsetX!=this._previousOffsetX||this._offsetY!=this._previousOffsetY)if(this._tight){var a=-this._offsetX,b=-this._offsetY,e=-this._previousOffsetX,
i=-this._previousOffsetY,g=a-e+c.width,k=e-a+c.width,j=b-i+c.height,f=i-b+c.height;if(g>this._tileWidth&&k>this._tileWidth&&j>this._tileHeight&&f>this._tileHeight){var l;g<k?(l=0,a=e-a,k=g):(l=a-e,a=0);j<f?(g=0,b=i-b):(g=b-i,b=0,j=f);null==c._buffer&&(c._buffer=document.createElement("canvas"),c._buffer.width=this._context.canvas.width,c._buffer.height=this._context.canvas.height);f=c._buffer.getContext("2d");this._doubledImage?(f.clearRect(0,0,2*k,2*j),f.drawImage(this._context.canvas,2*l,2*g,2*
k,2*j,0,0,2*k,2*j),f=this._context,f.clearRect(2*a,2*b,2*k,2*j),f.drawImage(c._buffer,0,0,2*k,2*j,2*a,2*b,2*k,2*j)):(f.clearRect(0,0,k,j),f.drawImage(this._context.canvas,l,g,k,j,0,0,k,j),f=this._context,f.clearRect(a,b,k,j),f.drawImage(c._buffer,0,0,k,j,a,b,k,j));0==a?this.redraw(k,0,c.width-k,c.height):this.redraw(0,0,c.width-k,c.height);0==b?this.redraw(0,j,c.width,c.height-j):this.redraw(0,0,c.width,c.height-j)}else this.redraw(0,0,c.width,c.height)}else this.redraw(0,0,c.width,c.height);this._previousOffsetX=
this._offsetX;this._previousOffsetY=this._offsetY})},loadData:function(a){this._data=Array.prototype.slice.apply(arguments);this._dirty=!0;this._tight=!1;for(var b=0,e=this._data.length;b<e;b++){for(var d=0,a=this._data[b],c=0,h=a.length;c<h;c++)for(var i=0,g=a[c].length;i<g;i++)0<=a[c][i]&&d++;if(0.2<d/(a.length*a[0].length)){this._tight=!0;break}}},hitTest:function(a,b){if(0>a||this.width<=a||0>b||this.height<=b)return!1;var e=this._image.width,d=this._image.height,c=this._tileWidth||e,h=this._tileHeight||
d,a=a/c|0,b=b/h|0;if(null!=this.collisionData)return this.collisionData[b]&&!!this.collisionData[b][a];for(var i=0,g=this._data.length;i<g;i++){var f=this._data[i],j;if(null!=f[b]&&null!=(j=f[b][a])&&0<=j&&j<(e/c|0)*(d/h|0))return!0}return!1},image:{get:function(){return this._image},set:function(a){this._image=a;if(f&&2==c.scale){for(var b=new enchant.Surface(2*a.width,2*a.height),e=this._tileWidth||a.width,d=this._tileHeight||a.height,m=a.width/e|0,h=a.height/d|0,i=0;i<h;i++)for(var g=0;g<m;g++)b.draw(a,
g*e,i*d,e,d,2*g*e,2*i*d,2*e,2*d);this._doubledImage=b}this._dirty=!0}},tileWidth:{get:function(){return this._tileWidth},set:function(a){this._tileWidth=a;this._dirty=!0}},tileHeight:{get:function(){return this._tileHeight},set:function(a){this._tileHeight=a;this._dirty=!0}},width:{get:function(){return this._tileWidth*this._data[0][0].length}},height:{get:function(){return this._tileHeight*this._data[0].length}},redraw:function(a,b,e,d){if(null!=this._image){var c,h,i,g,f;this._doubledImage?(c=this._doubledImage,
h=2*this._tileWidth,i=2*this._tileHeight,g=2*-this._offsetX,f=2*-this._offsetY,a*=2,b*=2,e*=2,d*=2):(c=this._image,h=this._tileWidth,i=this._tileHeight,g=-this._offsetX,f=-this._offsetY);var j=c.width/h|0,l=c.height/i|0,n=Math.max((a+g)/h|0,0),t=Math.max((b+f)/i|0,0),u=Math.ceil((a+g+e)/h),q=Math.ceil((b+f+d)/i);c=c._element;var s=this._context;s.clearRect(a,b,e,d);e=0;for(d=this._data.length;e<d;e++)for(var r=this._data[e],v=Math.min(u,r[0].length),w=Math.min(q,r.length),b=t;b<w;b++)for(a=n;a<v;a++){var p=
r[b][a];0<=p&&p<j*l&&s.drawImage(c,p%j*h,(p/j|0)*i,h,i,a*h-g,b*i-f,h,i)}}}});enchant.Group=enchant.Class.create(enchant.Node,{initialize:function(){enchant.Node.call(this);this.childNodes=[];this._y=this._x=0},addChild:function(a){this.childNodes.push(a);a.parentNode=this;a.dispatchEvent(new enchant.Event("added"));if(this.scene){var b=new enchant.Event("addedtoscene");a.scene=this.scene;a.dispatchEvent(b);a._updateCoordinate();var e=document.createDocumentFragment(),d,c=Array.prototype.push;if(a._element)e.appendChild(a._element);
else if(a.childNodes)for(d=a.childNodes.slice().reverse();d.length;)a=d.pop(),a.scene=this.scene,a.dispatchEvent(b),a._element?e.appendChild(a._element):a.childNodes&&c.apply(d,a.childNodes.reverse());if(e.childNodes.length){for(var h,b=this;b.parentNode;){d=b.parentNode.childNodes;for(d=d.slice(d.indexOf(b)+1).reverse();d.length;)if(a=d.pop(),a._element){h=a._element;break}else a.childNodes&&c.apply(d,a.childNodes.slice().reverse());b=b.parentNode}h?this.scene._element.insertBefore(e,h):this.scene._element.appendChild(e)}}},
insertBefore:function(a,b){var e=this.childNodes.indexOf(b);if(-1!=e){if(this.childNodes.splice(e,0,a),a.parentNode=this,a.dispatchEvent(new enchant.Event("added")),this.scene){var d=new enchant.Event("addedtoscene");a.scene=this.scene;a.dispatchEvent(d);a._updateCoordinate();var c=document.createDocumentFragment(),h,g=Array.prototype.push;if(a._element)c.appendChild(a._element);else if(a.childNodes)for(h=a.childNodes.slice().reverse();h.length;)a=h.pop(),a.scene=this.scene,a.dispatchEvent(d),a._element?
c.appendChild(a._element):a.childNodes&&g.apply(h,a.childNodes.reverse());if(c.childNodes.length){for(var f,d=b;d.parentNode;){null!=e?(h=this.childNodes.slice(e+1).reverse(),e=null):(h=d.parentNode.childNodes,h=h.slice(h.indexOf(d)+1).reverse());for(;h.length;)if(a=h.pop(),a._element){f=a._element;break}else a.childNodes&&g.apply(h,a.childNodes.slice().reverse());d=d.parentNode}f?this.scene._element.insertBefore(c,f):this.scene._element.appendChild(c)}}}else this.addChild(a)},removeChild:function(a){var b=
this.childNodes.indexOf(a);if(-1!=b&&(this.childNodes.splice(b,1),a.parentNode=null,a.dispatchEvent(new enchant.Event("removed")),this.scene))if(b=new enchant.Event("removedfromscene"),a.scene=null,a.dispatchEvent(b),a._element)this.scene._element.removeChild(a._element);else if(a.childNodes)for(var e=a.childNodes.slice(),d=Array.prototype.push;e.length;)a=e.pop(),a.scene=null,a.dispatchEvent(b),a._element?this.scene._element.removeChild(a._element):a.childNodes&&d.apply(e,a.childNodes)},firstChild:{get:function(){return this.childNodes[0]}},
lastChild:{get:function(){return this.childNodes[this.childNodes.length-1]}},_updateCoordinate:function(){this.parentNode?(this._offsetX=this.parentNode._offsetX+this._x,this._offsetY=this.parentNode._offsetY+this._y):(this._offsetX=this._x,this._offsetY=this._y);for(var a=0,b=this.childNodes.length;a<b;a++)this.childNodes[a]._updateCoordinate()}});enchant.Scene=enchant.Class.create(enchant.Group,{initialize:function(){enchant.Group.call(this);this._element=document.createElement("div");this._element.style.position=
"absolute";this._element.style.overflow="hidden";this._element.style.width=(this.width=c.width)+"px";this._element.style.height=(this.height=c.height)+"px";this._element.style[g+"TransformOrigin"]="0 0";this._element.style[g+"Transform"]="scale("+c.scale+")";this.scene=this;var a=this;l?(this._element.addEventListener("touchstart",function(b){for(var e=b.touches,d=0,c=e.length;d<c;d++)b=new enchant.Event("touchstart"),b.identifier=e[d].identifier,b._initPosition(e[d].pageX,e[d].pageY),a.dispatchEvent(b)},
!1),this._element.addEventListener("touchmove",function(b){for(var e=b.touches,d=0,c=e.length;d<c;d++)b=new enchant.Event("touchmove"),b.identifier=e[d].identifier,b._initPosition(e[d].pageX,e[d].pageY),a.dispatchEvent(b)},!1),this._element.addEventListener("touchend",function(b){for(var c=b.changedTouches,d=0,g=c.length;d<g;d++)b=new enchant.Event("touchend"),b.identifier=c[d].identifier,b._initPosition(c[d].pageX,c[d].pageY),a.dispatchEvent(b)},!1)):(this._element.addEventListener("mousedown",function(b){var e=
b.pageX,d=b.pageY,b=new enchant.Event("touchstart");b.identifier=c._mousedownID;b._initPosition(e,d);a.dispatchEvent(b);a._mousedown=!0},!1),c._element.addEventListener("mousemove",function(b){if(a._mousedown){var e=b.pageX,d=b.pageY,b=new enchant.Event("touchmove");b.identifier=c._mousedownID;b._initPosition(e,d);a.dispatchEvent(b)}},!1),c._element.addEventListener("mouseup",function(b){if(a._mousedown){var e=b.pageX,d=b.pageY,b=new enchant.Event("touchend");b.identifier=c._mousedownID;b._initPosition(e,
d);a.dispatchEvent(b);a._mousedown=!1}},!1))},backgroundColor:{get:function(){return this._backgroundColor},set:function(a){this._element.style.backgroundColor=this._backgroundColor=a}},_updateCoordinate:function(){this._offsetX=this._x;this._offsetY=this._y;for(var a=0,b=this.childNodes.length;a<b;a++)this.childNodes[a]._updateCoordinate()}});var n="putImageData,drawImage,drawFocusRing,fill,stroke,clearRect,fillRect,strokeRect,fillText,strokeText".split(",");enchant.Surface=enchant.Class.create(enchant.EventTarget,
{initialize:function(a,b){enchant.EventTarget.call(this);this.width=a;this.height=b;this.context=null;var e="enchant-surface"+c._surfaceID++;document.getCSSCanvasContext?(this.context=document.getCSSCanvasContext("2d",e,a,b),this._element=this.context.canvas,this._css="-webkit-canvas("+e+")"):document.mozSetImageElement?(this._element=document.createElement("canvas"),this._element.width=a,this._element.height=b,this._css="-moz-element(#"+e+")",this.context=this._element.getContext("2d"),document.mozSetImageElement(e,
this._element)):(this._element=document.createElement("canvas"),this._element.width=a,this._element.height=b,this._element.style.position="absolute",this.context=this._element.getContext("2d"),n.forEach(function(a){var b=this.context[a];this.context[a]=function(){b.apply(this,arguments);this._dirty=!0}},this))},getPixel:function(a,b){return this.context.getImageData(a,b,1,1).data},setPixel:function(a,b,c,d,g,h){var f=this.context.createImageData(1,1);f.data[0]=c;f.data[1]=d;f.data[2]=g;f.data[3]=
h;this.context.putImageData(f,a,b,1,1)},clear:function(){this.context.clearRect(0,0,this.width,this.height)},draw:function(a){arguments[0]=a=a._element;1==arguments.length?this.context.drawImage(a,0,0):this.context.drawImage.apply(this.context,arguments)},clone:function(){var a=new enchant.Surface(this.width,this.height);a.draw(this);return a},toDataURL:function(){var a=this._element.src;return a?"data:"==a.slice(0,5)?a:this.clone().toDataURL():this._element.toDataURL()}});enchant.Surface.load=function(a){var b=
new Image,c=Object.create(enchant.Surface.prototype,{context:{value:null},_css:{value:"url("+a+")"},_element:{value:b}});enchant.EventTarget.call(c);b.src=a;b.onerror=function(){throw Error("Cannot load an asset: "+b.src);};b.onload=function(){c.width=b.width;c.height=b.height;c.dispatchEvent(new enchant.Event("load"))};return c};enchant.Sound=enchant.Class.create(enchant.EventTarget,{initialize:function(){enchant.EventTarget.call(this);throw Error("Illegal Constructor");},play:function(){this._element&&
this._element.play()},pause:function(){this._element&&this._element.pause()},stop:function(){this.pause();this.currentTime=0},clone:function(){var a;a=this._element instanceof Audio?Object.create(enchant.Sound.prototype,{_element:{value:this._element.cloneNode(!1)},duration:{value:this.duration}}):Object.create(enchant.Sound.prototype);enchant.EventTarget.call(a);return a},currentTime:{get:function(){return this._element?this._element.currentTime:0},set:function(a){this._element&&(this._element.currentTime=
a)}},volume:{get:function(){return this._element?this._element.volume:1},set:function(a){this._element&&(this._element.volume=a)}}});enchant.Sound.load=function(a,b){if(null==b)var e=a.match(/\.\w+$/)[0],b=e?"audio/"+e.slice(1).toLowerCase():"";var b=b.replace("mp3","mpeg"),d=Object.create(enchant.Sound.prototype);enchant.EventTarget.call(d);var f=new Audio;if(!enchant.Sound.enabledInMobileSafari&&"webkit"==g&&l)window.setTimeout(function(){d.dispatchEvent(new enchant.Event("load"))},0);else if(f.canPlayType(b))f.src=
a,f.load(),f.autoplay=!1,f.onerror=function(){throw Error("Cannot load an asset: "+f.src);},f.addEventListener("canplaythrough",function(){d.duration=f.duration;d.dispatchEvent(new enchant.Event("load"))},!1),d._element=f;else if("audio/mpeg"==b){var h=document.createElement("embed"),e="enchant-audio"+c._soundID++;h.width=h.height=1;h.name=e;h.src="sound.swf?id="+e+"&src="+a;h.allowscriptaccess="always";h.style.position="absolute";h.style.left="-1px";d.addEventListener("load",function(){Object.defineProperties(h,
{currentTime:{get:function(){return h.getCurrentTime()},set:function(a){h.setCurrentTime(a)}},volume:{get:function(){return h.getVolume()},set:function(a){h.setVolume(a)}}});d._element=h;d.duration=h.getDuration()});c._element.appendChild(h);enchant.Sound[e]=d}else window.setTimeout(function(){d.dispatchEvent(new enchant.Event("load"))},0);return d};window.addEventListener("message",function(a){a=JSON.parse(a.data);if("event"==a.type)c.dispatchEvent(new Event(a.value));else if("debug"==a.type)switch(a.value){case "start":enchant.Game.instance.start();
break;case "pause":enchant.Game.instance.pause();break;case "resume":enchant.Game.instance.resume();break;case "tick":enchant.Game.instance._tick()}},!1);enchant.Sound.enabledInMobileSafari=!1})();