This repository has been archived on 2022-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
dwelibs/dist/dwelibs.min.js

1 line
15 KiB
JavaScript

!function(t){var e={};function n(i){if(e[i])return e[i].exports;var r=e[i]={i:i,l:!1,exports:{}};return t[i].call(r.exports,r,r.exports,n),r.l=!0,r.exports}n.m=t,n.c=e,n.d=function(t,e,i){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(n.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)n.d(i,r,function(e){return t[e]}.bind(null,r));return i},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";window.DWE={};n(1),n(2),n(3),n(4),n(5),n(6),n(7),n(8)},function(t,e,n){"use strict";DWE.context=function(t){return t.star=function(e,n,i,r,o){var a=Math.PI/2*3,s=e,h=n,u=Math.PI/i;t.beginPath(),t.moveTo(e,n-r);for(var l=0;l<i;l++)s=e+Math.cos(a)*r,h=n+Math.sin(a)*r,t.lineTo(s,h),a+=u,s=e+Math.cos(a)*o,h=n+Math.sin(a)*o,t.lineTo(s,h),a+=u;t.lineTo(e,n-r),t.closePath()},t.fillStar=function(e,n,i,r,o){t.star(e,n,i,r,o),t.fill()},t.circle=function(e,n,i,r){r||(r=0),t.beginPath(),t.arc(e,n,i,r,2*Math.PI,!0),t.closePath()},t.fillCircle=function(e,n,i,r){t.circle(e,n,i,r),t.fill()},t}},function(t,e,n){"use strict";var i=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}();function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}DWE.Math={},DWE.Math.random=function(t,e){return Math.floor(Math.random()*e)+t},DWE.Math.lerp=function(t,e,n){return(1-n)*t+n*e},DWE.Math.reverseLerp=function(t,e,n){return 1/((e-t)*n)},DWE.Math.intersectsBox=function(t,e,n,i,r,o,a,s){return 2*Math.abs(t-r)<=n+a&&2*Math.abs(e-o)<=i+s},DWE.Math.clamp=function(t,e,n){return Math.max(e,Math.min(n,t))},DWE.Math.Vector2=function(){function t(e,n){r(this,t),this.x=e,this.y=n}return i(t,[{key:"normalize",value:function(){var t=this.magnitude;return this.x/=t,this.y/=t,this}},{key:"distance",value:function(t){if(!(t instanceof DWE.Math.Vector2))throw new Error("Distance function takes another Vector2 as an argument.");return Math.abs(Math.sqrt(Math.pow(t.x-this.x,2)+Math.pow(t.y-this.y,2)))}},{key:"add",value:function(t){if(!(t instanceof DWE.Math.Vector2))throw new Error("Add function takes another Vector2 as an argument.");return this.x+=t.x,this.y+=t.y,this}},{key:"subtract",value:function(t){if(!(t instanceof DWE.Math.Vector2))throw new Error("Subtract function takes another Vector2 as an argument.");return this.x-=t.x,this.y-=t.y,this}},{key:"multiply",value:function(t){if("number"==typeof t&&!isNaN(t))return this.x*=t,this.y*=t,this;if(!(t instanceof DWE.Math.Vector2))throw new Error("Multiply function takes a Vector2 or Number as an argument.");return this.x*=t.x,this.y*=t.y,this}},{key:"divide",value:function(t){if("number"==typeof t&&!isNaN(t))return this.x/=t,this.y/=t,this;if(!(t instanceof DWE.Math.Vector2))throw new Error("Division function takes a Vector2 or Number as an argument.");return this.x/=t.x,this.y/=t.y,this}},{key:"magnitude",get:function(){return Math.abs(Math.sqrt(this.x*this.x+this.y*this.y))}},{key:"isNormal",get:function(){return 1===Math.floor(this.magnitude)}},{key:"angle",get:function(){return this.isNormal?Math.atan2(this.x,this.y):NaN}}]),t}(),DWE.Math.Box2=function(t){function e(t,n,i,o){r(this,e);var a=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n));return a.width=i,a.height=o,a}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}(e,DWE.Math.Vector2),i(e,[{key:"intersectsBox",value:function(t){if(!(t instanceof DWE.Math.Box2))throw new Error("Intersection function takes another Box2 as an argument.");var e=t.x+t.width/2-(this.x+this.width/2),n=t.y+t.height/2-(this.y+this.height/2),i=t.width/2+this.width/2,r=t.height/2+this.height/2,o=null;if(Math.abs(e)<i&&Math.abs(n)<r){var a=i-Math.abs(e),s=r-Math.abs(n);o=a>=s?n>0?{dir:"t",y:s,x:0}:{dir:"b",y:-s,x:0}:e>0?{dir:"l",y:0,x:a}:{dir:"r",y:0,x:-a}}return o}},{key:"intersectsCircle",value:function(t){if(!(t instanceof DWE.Math.Circle))throw new Error("Intersection function takes another Circle as an argument.");var e=new DWE.Math.Vector2(Math.abs(t.x-this.x),Math.abs(t.y-this.y));return!(e.x>this.width/2+t.r)&&(!(e.y>this.height/2+t.r)&&(e.x<=this.width/2||(e.y<=this.height/2||Math.pow(e.x-this.width/2,2)+Math.pow(e.y-this.height/2,2)<=Math.pow(t.r,2))))}}]),e}(),DWE.Math.Circle=function(){function t(e,n,i){r(this,t),this.x=e,this.y=n,this.r=i}return i(t,[{key:"radius",get:function(){return this.r}}]),t}()},function(t,e,n){"use strict";DWE.getMousePosition=function(t,e){var n=void 0,i=void 0;if(t.changedTouches){var r=t.changedTouches[0];r&&(t.pageX=r.pageX,t.pageY=r.pageY)}return t.pageX||t.pageY?(n=t.pageX,i=t.pageY):(n=t.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,i=t.clientY+document.body.scrollTop+document.documentElement.scrollTop),n-=e.offsetLeft,i-=e.offsetTop,new DWE.Math.Vector2(n,i)}},function(t,e,n){"use strict";DWE.Keyboard={};var i={},r={},o={backspace:8,tab:9,enter:13,shift:16,ctrl:17,alt:18,pausebreak:19,capslock:20,escape:27,pgup:33,pgdown:34,end:35,home:36,left:37,up:38,right:39,down:40,insert:45,delete:46,"left-window":91,"right-window":92,select:93,numpad0:96,numpad1:97,numpad2:98,numpad3:99,numpad4:100,numpad5:101,numpad6:102,numpad7:103,numpad8:104,numpad9:105,multiply:106,add:107,subtract: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,numlock:144,scrolllock:145,"semi-colon":186,equals:187,comma:188,dash:189,period:190,fwdslash:191,grave:192,"open-bracket":219,bkslash:220,"close-braket":221,"single-quote":222};function a(t){return null!=i[t]&&i[t]}function s(t){return null!=r[t]&&r[t]}DWE.Keyboard.toggleKey=function(t,e){var n=null;for(var r in o){if(t===o[r]){n=r;break}}n||(n=String.fromCharCode(t).toLowerCase()),i[n]=!0===e},DWE.Keyboard.keyDown=function(t){var e;window.event?e=window.event.keyCode:t&&(e=t.which),DWE.Keyboard.toggleKey(e,!0)},DWE.Keyboard.keyUp=function(t){var e;window.event?e=window.event.keyCode:t&&(e=t.which),DWE.Keyboard.toggleKey(e,!1)},DWE.Keyboard.isDown=function(t){return a(t)&&s(t)},DWE.Keyboard.isUp=function(t){return!DWE.Keyboard.isDown(t)},DWE.Keyboard.isPressed=function(t){return!0===a(t)&&!1===s(t)},DWE.Keyboard.nextIteration=function(){for(var t in r={},i)!0===i[t]&&(r[t]=!0)}},function(t,e,n){"use strict";var i=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}();DWE.Image={},DWE.Image.Image=function(t){var e=this;this.src=t,this.img=new Image,this.img.src=this.src,this.width=0,this.height=0,this.loaded=!1,this.errored=!1,this.img.onload=function(){e.loaded=!0,e.width=e.img.width,e.height=e.img.height,e.onload&&e.onload.apply(e,[])},this.img.onerror=function(t){e.loaded=!1,e.errored=!0,e.onerror&&e.onerror.apply(e,[t])}},DWE.Image.TileMap=function(t){function e(t,n,i){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e);var r=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return r.aspectWidth=n,r.aspectHeight=i,r.tiles=[],r}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}(e,DWE.Image.Image),i(e,[{key:"onload",value:function(){for(var t=0;t<this.height/this.aspectHeight;t++)for(var e=0;e<this.width/this.aspectWidth;e++)this.tiles.push(new DWE.Math.Vector2(e,t));this.onready&&this.onready.call(this,this)}},{key:"draw",value:function(t,e,n,i,r,o){var a=this.tiles[e];a&&t.drawImage(this.img,a.x*this.aspectWidth,a.y*this.aspectHeight,this.aspectWidth,this.aspectHeight,n,i,r,o)}}]),e}(),DWE.Image.loadImageArray=function(t){return new Promise(function(e,n){var i=0,r=t.length,o=[];!function a(s){var h=t[s],u=new DWE.Image.Image(h);u.onload=function(){o.push(u),++i==r?e(o):a(i)},u.onerror=function(t){n(t)}}(i)})}},function(t,e,n){"use strict";DWE.File={},DWE.File.httpGET=function(t){return new Promise(function(e,n){var i=new XMLHttpRequest;i.onreadystatechange=function(){4==i.readyState&&200==i.status?e(i.responseText):4==i.readyState&&i.status>=400&&n(i.responseText)},i.open("GET",t,!0),i.responseType="text",i.send(null)})},DWE.File.loadJson=function(t){return new Promise(function(e,n){DWE.File.httpGET(t).then(function(t){var i;try{i=JSON.parse(t)}catch(t){return n(t)}e(i)},n)})}},function(t,e,n){"use strict";var i=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}();DWE.Tiled={},DWE.Tiled.Map=function(){function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.file=e;var n=e.split("/");this.name=n[n.length-1],this.dir=n.slice(0,-1).join("/"),this.loaded=!1,this.tileMaps=[],this.layers={},this.tileWidth=0,this.tileHeight=0,this.scale=1,this.loadTiledMap()}return i(t,[{key:"done",value:function(){this.onready&&this.onready.apply(this,[this]),this.loaded=!0}},{key:"error",value:function(t){this.onerror?this.onerror.apply(this,[t]):console.error(t),this.loaded=!1}},{key:"loadTiledMap",value:function(){var t=this;DWE.File.loadJson(this.file).then(function(e){if("orthogonal"!==e.orientation||"right-down"!==e.renderorder)return t.error(new Error("Currently, DWE Libs only allows orthogonal orientation and right-down render order."));var n=t;!function t(i){if(i===e.tilesets.length)return function(){var t=e.width,i=e.height;for(var r in e.layers){var o=e.layers[r],a=o.width||t,s=o.height||i,h=[];if("tilelayer"==o.type&&o.visible){for(var u=0;u<a;u++)for(var l=0;l<s;l++){var c=o.data[l*a+u];0!=c&&h.push({x:u,y:l,tile:c})}n.layers[o.name]={tiles:h,x:parseInt(o.x),y:parseInt(o.y)}}}n.width=t,n.height=i,n.tileWidth=e.tilewidth,n.tileHeight=e.tileheight,n.done()}();var r=e.tilesets[i],o=n.dir+"/"+r.source;DWE.File.httpGET(o).then(function(e){var o=(new DOMParser).parseFromString(e,"application/xml").getElementsByTagName("tileset");for(var a in o){var s=o[a];if(s instanceof Element){var h=parseInt(s.getAttribute("tilewidth")),u=parseInt(s.getAttribute("tileheight")),l=s.getElementsByTagName("image")[0];if(l){var c=new DWE.Image.TileMap(n.dir+"/"+l.getAttribute("source"),h,u);c.offset=r.firstgid,c.tileCount=parseInt(s.getAttribute("tilecount")),n.tileMaps.push(c),c.onready=function(){t(i+1)}}}}},function(t){n.error.apply(n,[t])})}(0)},function(t){map.error.apply(map,[t])})}},{key:"drawLayer",value:function(t,e,n,i){if(!this.layers[i])return null;var r=this.layers[i];for(var o in this.tileMaps){var a=this.tileMaps[o];for(var s in r.tiles){var h=r.tiles[s],u=a.tileCount||a.tiles.length;h.tile>=a.offset&&h.tile<=u&&a.draw(t,h.tile-a.offset,h.x*this.tileWidth*this.scale+e.x+r.x+n.x,h.y*this.tileHeight*this.scale+e.y+r.y+n.y,this.tileWidth*this.scale,this.tileHeight*this.scale)}}}},{key:"collideLayer",value:function(t,e,n,i,r){if(!this.layers[i])return null;var o=this.layers[i],a=null,s=new DWE.Math.Box2(t.x+e.x+o.x-n.x,t.y+e.y+o.y-n.y,t.width*this.scale,t.height*this.scale);for(var h in o.tiles){var u=o.tiles[h],l=new DWE.Math.Box2(u.x*this.tileWidth*this.scale+e.x+o.x+n.x,u.y*this.tileHeight*this.scale+e.y+o.y+n.y,this.tileWidth*this.scale,this.tileHeight*this.scale);if(!(l.distance(s)>r)&&(a=l.intersectsBox(s)))break}return a}},{key:"setScale",value:function(t){this.scale=t}}]),t}()},function(t,e,n){"use strict";var i=function(){function t(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,n,i){return n&&t(e.prototype,n),i&&t(e,i),e}}();function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function a(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}DWE.Controller={},DWE.Controller.Entity=function(t){function e(t,n,i,a){r(this,e);var s=o(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n,i,a));return s.velocity=new DWE.Math.Vector2(0,0),s.scale=1,s}return a(e,DWE.Math.Box2),i(e,[{key:"collide",value:function(t,e,n,i){var r=t.collideLayer(this,n,i,e,2*this.scale*t.tileWidth);"l"===r||"r"===r?this.velocity.x=0:"b"===r&&this.velocity.y<0?this.velocity.y=0:"t"===r&&this.velocity.y>0&&(this.velocity.y=0)}},{key:"update",value:function(t){this.x+=this.velocity.x*t,this.y+=this.velocity.y*t}}]),e}(),DWE.Controller.OrthoEntity=function(t){function e(t,n,i,a){r(this,e);var s=o(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t,n,i,a));return s.grounded=!1,s.jumping=!1,s}return a(e,DWE.Controller.Entity),i(e,[{key:"collide",value:function(t,e,n,i){var r=t.collideLayer(this,n,i,e,2*this.scale*t.tileWidth);this.grounded=!1,r&&("l"===r.dir||"r"===r.dir?(this.velocity.x=0,this.jumping=!1):"b"===r.dir?(this.grounded=!0,this.jumping=!1):"t"===r.dir&&(this.velocity.y*=-1,this.jumping=!1),this.x-=r.x,this.y-=r.y)}},{key:"update",value:function(t){this.grounded&&(this.velocity.y=0),this.x+=this.velocity.x*t,this.y+=this.velocity.y*t}}]),e}()}]);