craters.js
Advanced tools
Comparing version 1.2.2 to 1.3.0
// Craters.js micro game framework | ||
// This module contains the core craters.js framework fundamentals | ||
// it loads modules and exports them | ||
import * as Maths from './maths/common.js' | ||
import './Polyfill.js' | ||
import { | ||
Canvas, | ||
Loop | ||
} from './system.js' | ||
import { | ||
Entity, | ||
Game, | ||
Sprite | ||
} from './entity.js' | ||
import { | ||
Loader | ||
} from './loader.js' | ||
import { | ||
Sound | ||
} from './sound.js' | ||
import Entity from './Entity.js' | ||
import Game from './Game.js' | ||
import {Maths} from './Geometry/Geometry.js' | ||
import {Vector} from './Geometry/Geometry.js' | ||
import Fixtures from './Fixture/Fixtures.js' | ||
if (typeof window === 'undefined' && global) { | ||
global.window = { | ||
performance: { | ||
now: function(start) { | ||
if (!start) return Date.now() | ||
var end = Date.now(start) | ||
return Math.round((end[0] * 1000) + (end[1] / 1000000)) | ||
} | ||
}, | ||
requestAnimationFrame: function(f) { | ||
setImmediate(() => f(this.performance.now())) | ||
} | ||
} | ||
} | ||
class Craters { | ||
static version() { | ||
return '1.2.2' | ||
return '1.3.0' | ||
} | ||
} | ||
export { | ||
Craters, | ||
Loader, | ||
Game, | ||
Canvas, | ||
Loop, | ||
Entity, | ||
Sprite, | ||
Sound, | ||
Maths | ||
} | ||
export { Craters, Game, Entity, Fixtures, Vector, Maths } |
@@ -1,1 +0,1 @@ | ||
!function(t){var e={};function n(i){if(e[i])return e[i].exports;var s=e[i]={i:i,l:!1,exports:{}};return t[i].call(s.exports,s,s.exports,n),s.l=!0,s.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 s in t)n.d(i,s,function(e){return t[e]}.bind(null,s));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=1)}([function(t,e){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(t){"object"==typeof window&&(n=window)}t.exports=n},function(t,e,n){(function(t,e){!function(n){"use strict";const i=function(t){return 180*t/Math.PI},s=function(t,e){this.x=t||0,this.y=e||0};s.prototype={clone:function(){return new s(this.x,this.y)},set:function(t,e){return"x"===t?this.x=e:"y"===t&&(this.y=e),this},add:function(){let t,e,n=arguments;if(1===n.length){let i=n[0];"object"==typeof i&&(t=i.x,e=i.y)}else 2===n.length&&"number"==typeof n[0]&&"number"==typeof n[1]&&(t=n[0],e=n[1]);return this.x+=t,this.y+=e,this},subtract:function(t){return this.x-=t.x,this.y-=t.y,this},multiply:function(t){return"object"==typeof t?(this.x*=t.x,this.y*=t.y):"number"==typeof t&&(this.x*=t,this.y*=t),this},magnitude:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},magnitudeSq:function(){return this.x*this.x+this.y*this.y},negate:function(){return this.x=-this.x,this.y=-this.y,this},rotate:function(t){let e=Math.sin(t),n=Math.cos(t),i=this.x*n-this.y*e,s=this.x*e+this.y*n;return this.x=i,this.y=s,this},dot:function(t){return this.x*t.x+this.y*t.y},cross:function(t){return this.x*t.y-this.y*t.x},angleTo:function(t){let e=this.magnitude(),n=t.magnitude(),i=this.dot(t);return Math.acos(i/(e*n))},getAngle:function(t){if("DEGREES"===t)return i(Math.atan(this.y/this.x));let e=Math.atan2(this.y,this.x);return e<0?2*Math.PI+e:e},normalize:function(){let t=this.magnitude();return this.x/=t,this.y/=t,this},perp:function(t){if("right"===t){let t=this.x;this.x=this.y,this.y=-t}else{let t=this.x;this.x=-this.y,this.y=t}return this},distanceTo:function(t){return Math.sqrt((t.x-this.x)*(t.x-this.x)+(t.y-this.y)*(t.y-this.y))},scalarProject:function(t){return this.dot(t)/t.magnitude()},scalarProjectUnit:function(t){return this.dot(t)},vectorProject:function(t){let e=this.dot(t)/t.magnitudeSq();return this.x=t.x*e,this.y=t.y*e,this},vectorProjectUnit:function(t){let e=this.dot(t);return this.x=t.x*e,this.y=t.y*e,this}},s.prototype.translate=s.prototype.add,s.add=function(t,e){return new s(t.x+e.x,t.y+e.y)},s.subtract=function(t,e){return new s(t.x-e.x,t.y-e.y)},s.multiply=function(t,e){return"number"==typeof t&&"number"==typeof e?t*e:"object"==typeof t&&"number"==typeof e?new s(t.x*e,t.y*e):"object"==typeof e&&"number"==typeof t?new s(t*e.x,t*e.y):new s(t.x*e.x,t.y*e.y)},s.dot=function(t,e){return t.x*e.x+t.y*e.y},s.angleBetween=function(t,e){let n=t.magnitude(),i=e.magnitude(),s=t.dot(e);return Math.acos(s/(n*i))},s.perp=function(t,e){switch(e){case"right":return new s(t.y,-t.x);default:return new s(-t.y,t.x)}},s.negate=function(t){return new s(-t.x,-t.y)};var r=Object.freeze({Vector:s,boundary:function(t,e,n){return Math.min(Math.max(t,e),n)},degToRad:function(t){return t*Math.PI/180},radToDeg:i,distance:function(t,e,n,i){return Math.sqrt((n-t)*(n-t)+(i-e)*(i-e))},map:function(t,e,n,i,s){return i+(s-i)*(t-e)/(n-e)}});class o{constructor(){this.entities=[],this.state={size:new s(1024,512),gravity:new s(0,0),friction:new s(0,0)}}addObject(t){return this.entities.push(t)}removeObject(t){return this.entities.splice(t,1)}update(t,e){this.entities.forEach(t=>{switch(t.state.loop=this.state.loop,t.type){case"dynamic":t.state.vel.x+=t.state.accel.x+(t.state.gravity.x+this.state.gravity.x),t.state.vel.y+=t.state.accel.y+(t.state.gravity.y+this.state.gravity.y),t.state.pos.x+=t.state.vel.x*(1/((t.state.loop.delta+t.state.loop.nframe)/2)),t.state.pos.y+=t.state.vel.y*(1/((t.state.loop.delta+t.state.loop.nframe)/2));var e=t.state.friction.x,n=t.state.vel.x+e,i=t.state.vel.x-e,s=t.state.friction.y,r=t.state.vel.y+s,o=t.state.vel.y-s;t.state.vel.x=n<0?n:i>0?i:0,t.state.vel.y=r<0?r:o>0?o:0;break;case"static":t.state.vel.x+=t.state.accel.x,t.state.vel.y+=t.state.accel.y,t.state.pos.x+=t.state.vel.x,t.state.pos.y+=t.state.vel.y}t.update()})}render(){this.entities.forEach(t=>{t.render()})}}class a extends o{constructor(){super(),this.state.size=new s(20,20),this.state.pos=new s(0,0),this.state.vel=new s(0,0),this.state.accel=new s(0,0),this.state.radius=20,this.state.angle=0,this.type="dynamic"}}"undefined"==typeof window&&t&&(t.window={performance:{now:function(t){if(!t)return Date.now();var e=Date.now(t);return Math.round(1e3*e[0]+e[1]/1e6)}},requestAnimationFrame:function(t){e(()=>t(this.performance.now()))}});n.Canvas=class{constructor(t,e,n){n=document.querySelector(n||"body");var i=document.createElement("canvas"),s=i.getContext("2d"),r=window.devicePixelRatio/["webkitBackingStorePixelRatio","mozBackingStorePixelRatio","msBackingStorePixelRatio","oBackingStorePixelRatio","backingStorePixelRatio"].reduce(function(t,e){return Object.prototype.hasOwnProperty.call(s,e)?s[e]:1});return i.width=Math.round(t*r),i.height=Math.round(e*r),i.style.width=t+"px",i.style.height=e+"px",s.setTransform(r,0,0,r,0,0),n.insertBefore(i,n.firstChild),i.context=i.getContext("2d"),i.resize=(t,e)=>{i.style.width=e.x+"px",i.style.height=e.y+"px",i.width=Math.round(e.x*r),i.height=Math.round(e.y*r),s.setTransform(r,0,0,r,0,0),t.state.size.x=e.x,t.state.size.y=e.y},i.clear=(t,e,n,s,r)=>{t=t||null,e=e||0,n=n||0,s=s||i.width,r=r||i.height,t?(i.context.save(),i.context.fillStyle=t,i.context.fillRect(e,n,s,r),i.context.fill(),i.context.restore()):i.context.clearRect(e,n,s,r)},i.camera=(t,e)=>{i.context.setTransform(1,0,0,1,0,0),i.context.translate(-t,-e)},i}},n.Craters=class{static version(){return"1.2.2"}},n.Entity=a,n.Game=o,n.Loader=class{constructor(){this.rescache={}}load(t,e){var n=this;t instanceof Array?t.forEach(function(t){n.rescache[t]=!1,n.fetch(t,e)}):(n.rescache[t]=!1,n.fetch(t,e))}fetch(t,e){var n=this;if(n.rescache[t])return n.rescache[t];var i=new Image;i.onload=function(){n.rescache[t]=i,n.ready(e)},i.src=t}ready(t){if("function"==typeof t){var e=!0;for(var n in this.rescache)Object.prototype.hasOwnProperty.call(this.rescache,n)&&!this.rescache[n]&&(e=!1);e&&t()}}},n.Loop=class{constructor(t,e){var n={elapsed:0,tframe:1e3/e,nframe:e,before:window.performance.now(),main:function(){n.startLoop=window.requestAnimationFrame(n.main),n.delta=Math.round(1e3/(window.performance.now()-n.before)*100/100),window.performance.now()<n.before+n.tframe||(n.before=window.performance.now(),n.stopLoop=()=>{window.cancelAnimationFrame(n.startLoop)},t.state.loop=n,t.update(n.elapsed,n.delta),t.render(n.elapsed,n.delta),n.elapsed++)}};return n.main(),n}},n.Maths=r,n.Sound=class{constructor(){this.sounds={},this.instances=[],this.defaultVolume=1}load(t,e,n){if(this.sounds[t]=new Audio(e),"function"!=typeof n)return new Promise((e,n)=>{this.sounds[t].addEventListener("canplaythrough",e),this.sounds[t].addEventListener("error",n)});this.sounds[t].addEventListener("canplaythrough",n)}remove(t){void 0!==this.sounds&&delete this.sounds[t]}unlock(t,e,n,i){var s=this,r=["touchstart","touchend","mousedown","keydown"],o=function o(){r.forEach(function(t){document.body.removeEventListener(t,o)}),s.play(t,e,n,i)};r.forEach(function(t){document.body.addEventListener(t,o,!1)})}play(t,e,n,i){if(i=i||!1,void 0===this.sounds[t])return console.error("Can't find sound called '"+t+"'."),!1;var s=this.sounds[t].cloneNode(!0);return s.volume="number"==typeof n?n:this.defaultVolume,s.loop=i,s.play(),this.instances.push(s),s.addEventListener("ended",()=>{var t=this.instances.indexOf(s);-1!==t&&this.instances.splice(t,1)}),"function"==typeof e?(s.addEventListener("ended",e),!0):new Promise((t,e)=>s.addEventListener("ended",t))}stopAll(){var t=this.instances.slice();for(var e of t)e.pause(),e.dispatchEvent(new Event("ended"))}},n.Sprite=class extends a{constructor(t,e){super(),this.scope=t,this.state.pos=e.pos||new s(0,0),this.state.crop=new s(0,0),this.state.size=e.size||new s(0,0),this.state.frames=e.frames||[],this.state.angle=e.angle||0,this.state.image=e.image||new Image,this.state.delay=e.delay||5,this.state.tick=e.tick||0,this.state.orientation=e.orientation||"horizontal"}update(){super.update(),this.state.tick<=0&&("vertical"===this.orientation?(this.state.crop.y=this.state.frames.shift(),this.state.frames.push(this.state.crop.y)):(this.state.crop.x=this.state.frames.shift(),this.state.frames.push(this.state.crop.x)),this.state.tick=this.state.delay),this.state.tick--}render(){super.render(),this.scope.context.save(),this.scope.context.translate(this.state.crop.x+this.state.size.x/2,this.state.crop.y+this.state.size.y/2),this.scope.context.rotate(this.state.angle*(Math.PI/180)),this.scope.context.translate(-(this.state.crop.x+this.state.size.x/2),-(this.state.crop.y+this.state.size.y/2)),this.scope.context.drawImage(this.state.image,this.state.crop.x*this.state.size.x,this.state.crop.y*this.state.size.y,this.state.size.x,this.state.size.y,this.state.pos.x,this.state.pos.y,this.state.size.x,this.state.size.y),this.scope.context.restore()}}}({})}).call(this,n(0),n(2).setImmediate)},function(t,e,n){(function(t){var i=void 0!==t&&t||"undefined"!=typeof self&&self||window,s=Function.prototype.apply;function r(t,e){this._id=t,this._clearFn=e}e.setTimeout=function(){return new r(s.call(setTimeout,i,arguments),clearTimeout)},e.setInterval=function(){return new r(s.call(setInterval,i,arguments),clearInterval)},e.clearTimeout=e.clearInterval=function(t){t&&t.close()},r.prototype.unref=r.prototype.ref=function(){},r.prototype.close=function(){this._clearFn.call(i,this._id)},e.enroll=function(t,e){clearTimeout(t._idleTimeoutId),t._idleTimeout=e},e.unenroll=function(t){clearTimeout(t._idleTimeoutId),t._idleTimeout=-1},e._unrefActive=e.active=function(t){clearTimeout(t._idleTimeoutId);var e=t._idleTimeout;e>=0&&(t._idleTimeoutId=setTimeout(function(){t._onTimeout&&t._onTimeout()},e))},n(3),e.setImmediate="undefined"!=typeof self&&self.setImmediate||void 0!==t&&t.setImmediate||this&&this.setImmediate,e.clearImmediate="undefined"!=typeof self&&self.clearImmediate||void 0!==t&&t.clearImmediate||this&&this.clearImmediate}).call(this,n(0))},function(t,e,n){(function(t,e){!function(t,n){"use strict";if(!t.setImmediate){var i,s,r,o,a,c=1,u={},h=!1,l=t.document,f=Object.getPrototypeOf&&Object.getPrototypeOf(t);f=f&&f.setTimeout?f:t,"[object process]"==={}.toString.call(t.process)?i=function(t){e.nextTick(function(){p(t)})}:!function(){if(t.postMessage&&!t.importScripts){var e=!0,n=t.onmessage;return t.onmessage=function(){e=!1},t.postMessage("","*"),t.onmessage=n,e}}()?t.MessageChannel?((r=new MessageChannel).port1.onmessage=function(t){p(t.data)},i=function(t){r.port2.postMessage(t)}):l&&"onreadystatechange"in l.createElement("script")?(s=l.documentElement,i=function(t){var e=l.createElement("script");e.onreadystatechange=function(){p(t),e.onreadystatechange=null,s.removeChild(e),e=null},s.appendChild(e)}):i=function(t){setTimeout(p,0,t)}:(o="setImmediate$"+Math.random()+"$",a=function(e){e.source===t&&"string"==typeof e.data&&0===e.data.indexOf(o)&&p(+e.data.slice(o.length))},t.addEventListener?t.addEventListener("message",a,!1):t.attachEvent("onmessage",a),i=function(e){t.postMessage(o+e,"*")}),f.setImmediate=function(t){"function"!=typeof t&&(t=new Function(""+t));for(var e=new Array(arguments.length-1),n=0;n<e.length;n++)e[n]=arguments[n+1];var s={callback:t,args:e};return u[c]=s,i(c),c++},f.clearImmediate=d}function d(t){delete u[t]}function p(t){if(h)setTimeout(p,0,t);else{var e=u[t];if(e){h=!0;try{!function(t){var e=t.callback,i=t.args;switch(i.length){case 0:e();break;case 1:e(i[0]);break;case 2:e(i[0],i[1]);break;case 3:e(i[0],i[1],i[2]);break;default:e.apply(n,i)}}(e)}finally{d(t),h=!1}}}}}("undefined"==typeof self?void 0===t?this:t:self)}).call(this,n(0),n(4))},function(t,e){var n,i,s=t.exports={};function r(){throw new Error("setTimeout has not been defined")}function o(){throw new Error("clearTimeout has not been defined")}function a(t){if(n===setTimeout)return setTimeout(t,0);if((n===r||!n)&&setTimeout)return n=setTimeout,setTimeout(t,0);try{return n(t,0)}catch(e){try{return n.call(null,t,0)}catch(e){return n.call(this,t,0)}}}!function(){try{n="function"==typeof setTimeout?setTimeout:r}catch(t){n=r}try{i="function"==typeof clearTimeout?clearTimeout:o}catch(t){i=o}}();var c,u=[],h=!1,l=-1;function f(){h&&c&&(h=!1,c.length?u=c.concat(u):l=-1,u.length&&d())}function d(){if(!h){var t=a(f);h=!0;for(var e=u.length;e;){for(c=u,u=[];++l<e;)c&&c[l].run();l=-1,e=u.length}c=null,h=!1,function(t){if(i===clearTimeout)return clearTimeout(t);if((i===o||!i)&&clearTimeout)return i=clearTimeout,clearTimeout(t);try{i(t)}catch(e){try{return i.call(null,t)}catch(e){return i.call(this,t)}}}(t)}}function p(t,e){this.fun=t,this.array=e}function y(){}s.nextTick=function(t){var e=new Array(arguments.length-1);if(arguments.length>1)for(var n=1;n<arguments.length;n++)e[n-1]=arguments[n];u.push(new p(t,e)),1!==u.length||h||a(d)},p.prototype.run=function(){this.fun.apply(null,this.array)},s.title="browser",s.browser=!0,s.env={},s.argv=[],s.version="",s.versions={},s.on=y,s.addListener=y,s.once=y,s.off=y,s.removeListener=y,s.removeAllListeners=y,s.emit=y,s.prependListener=y,s.prependOnceListener=y,s.listeners=function(t){return[]},s.binding=function(t){throw new Error("process.binding is not supported")},s.cwd=function(){return"/"},s.chdir=function(t){throw new Error("process.chdir is not supported")},s.umask=function(){return 0}}]); | ||
!function(t){var e={};function i(s){if(e[s])return e[s].exports;var o=e[s]={i:s,l:!1,exports:{}};return t[s].call(o.exports,o,o.exports,i),o.l=!0,o.exports}i.m=t,i.c=e,i.d=function(t,e,s){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:s})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var s=Object.create(null);if(i.r(s),Object.defineProperty(s,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)i.d(s,o,function(e){return t[e]}.bind(null,o));return s},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=5)}([function(t,e){var i;i=function(){return this}();try{i=i||new Function("return this")()}catch(t){"object"==typeof window&&(i=window)}t.exports=i},function(t,e,i){(function(t,e){var i=new Object;"undefined"==typeof window&&t&&(t.window={performance:{now:function(t){if(!t)return Date.now();var e=Date.now(t);return Math.round(1e3*e[0]+e[1]/1e6)}},requestAnimationFrame:function(t){e(()=>t(this.performance.now()))}}),window.cg=i}).call(this,i(0),i(2).setImmediate)},function(t,e,i){(function(t){var s=void 0!==t&&t||"undefined"!=typeof self&&self||window,o=Function.prototype.apply;function n(t,e){this._id=t,this._clearFn=e}e.setTimeout=function(){return new n(o.call(setTimeout,s,arguments),clearTimeout)},e.setInterval=function(){return new n(o.call(setInterval,s,arguments),clearInterval)},e.clearTimeout=e.clearInterval=function(t){t&&t.close()},n.prototype.unref=n.prototype.ref=function(){},n.prototype.close=function(){this._clearFn.call(s,this._id)},e.enroll=function(t,e){clearTimeout(t._idleTimeoutId),t._idleTimeout=e},e.unenroll=function(t){clearTimeout(t._idleTimeoutId),t._idleTimeout=-1},e._unrefActive=e.active=function(t){clearTimeout(t._idleTimeoutId);var e=t._idleTimeout;e>=0&&(t._idleTimeoutId=setTimeout(function(){t._onTimeout&&t._onTimeout()},e))},i(3),e.setImmediate="undefined"!=typeof self&&self.setImmediate||void 0!==t&&t.setImmediate||this&&this.setImmediate,e.clearImmediate="undefined"!=typeof self&&self.clearImmediate||void 0!==t&&t.clearImmediate||this&&this.clearImmediate}).call(this,i(0))},function(t,e,i){(function(t,e){!function(t,i){"use strict";if(!t.setImmediate){var s,o,n,r,a,l=1,h={},c=!1,u=t.document,p=Object.getPrototypeOf&&Object.getPrototypeOf(t);p=p&&p.setTimeout?p:t,"[object process]"==={}.toString.call(t.process)?s=function(t){e.nextTick(function(){y(t)})}:!function(){if(t.postMessage&&!t.importScripts){var e=!0,i=t.onmessage;return t.onmessage=function(){e=!1},t.postMessage("","*"),t.onmessage=i,e}}()?t.MessageChannel?((n=new MessageChannel).port1.onmessage=function(t){y(t.data)},s=function(t){n.port2.postMessage(t)}):u&&"onreadystatechange"in u.createElement("script")?(o=u.documentElement,s=function(t){var e=u.createElement("script");e.onreadystatechange=function(){y(t),e.onreadystatechange=null,o.removeChild(e),e=null},o.appendChild(e)}):s=function(t){setTimeout(y,0,t)}:(r="setImmediate$"+Math.random()+"$",a=function(e){e.source===t&&"string"==typeof e.data&&0===e.data.indexOf(r)&&y(+e.data.slice(r.length))},t.addEventListener?t.addEventListener("message",a,!1):t.attachEvent("onmessage",a),s=function(e){t.postMessage(r+e,"*")}),p.setImmediate=function(t){"function"!=typeof t&&(t=new Function(""+t));for(var e=new Array(arguments.length-1),i=0;i<e.length;i++)e[i]=arguments[i+1];var o={callback:t,args:e};return h[l]=o,s(l),l++},p.clearImmediate=d}function d(t){delete h[t]}function y(t){if(c)setTimeout(y,0,t);else{var e=h[t];if(e){c=!0;try{!function(t){var e=t.callback,s=t.args;switch(s.length){case 0:e();break;case 1:e(s[0]);break;case 2:e(s[0],s[1]);break;case 3:e(s[0],s[1],s[2]);break;default:e.apply(i,s)}}(e)}finally{d(t),c=!1}}}}}("undefined"==typeof self?void 0===t?this:t:self)}).call(this,i(0),i(4))},function(t,e){var i,s,o=t.exports={};function n(){throw new Error("setTimeout has not been defined")}function r(){throw new Error("clearTimeout has not been defined")}function a(t){if(i===setTimeout)return setTimeout(t,0);if((i===n||!i)&&setTimeout)return i=setTimeout,setTimeout(t,0);try{return i(t,0)}catch(e){try{return i.call(null,t,0)}catch(e){return i.call(this,t,0)}}}!function(){try{i="function"==typeof setTimeout?setTimeout:n}catch(t){i=n}try{s="function"==typeof clearTimeout?clearTimeout:r}catch(t){s=r}}();var l,h=[],c=!1,u=-1;function p(){c&&l&&(c=!1,l.length?h=l.concat(h):u=-1,h.length&&d())}function d(){if(!c){var t=a(p);c=!0;for(var e=h.length;e;){for(l=h,h=[];++u<e;)l&&l[u].run();u=-1,e=h.length}l=null,c=!1,function(t){if(s===clearTimeout)return clearTimeout(t);if((s===r||!s)&&clearTimeout)return s=clearTimeout,clearTimeout(t);try{s(t)}catch(e){try{return s.call(null,t)}catch(e){return s.call(this,t)}}}(t)}}function y(t,e){this.fun=t,this.array=e}function f(){}o.nextTick=function(t){var e=new Array(arguments.length-1);if(arguments.length>1)for(var i=1;i<arguments.length;i++)e[i-1]=arguments[i];h.push(new y(t,e)),1!==h.length||c||a(d)},y.prototype.run=function(){this.fun.apply(null,this.array)},o.title="browser",o.browser=!0,o.env={},o.argv=[],o.version="",o.versions={},o.on=f,o.addListener=f,o.once=f,o.off=f,o.removeListener=f,o.removeAllListeners=f,o.emit=f,o.prependListener=f,o.prependOnceListener=f,o.listeners=function(t){return[]},o.binding=function(t){throw new Error("process.binding is not supported")},o.cwd=function(){return"/"},o.chdir=function(t){throw new Error("process.chdir is not supported")},o.umask=function(){return 0}},function(t,e,i){"use strict";i.r(e);i(1);var s={abs:function(t){return t<0?-t:t},boundary:function(t,e,i){return Math.min(Math.max(t,e),i)},degToRad:function(t){return t*Math.PI/180},radToDeg:function(t){return 180*t/Math.PI},distance:function(t,e,i,s){return Math.sqrt((i-t)*(i-t)+(s-e)*(s-e))},map:function(t,e,i,s,o){return s+(o-s)*(t-e)/(i-e)}};function o(t,e,i,s){var o;o=e?new n(t,0,i,s):new r(t,0,i,s),this.root=o}function n(t,e,i,s){this._bounds=t,this.children=[],this.nodes=[],s&&(this._maxChildren=s),i&&(this._maxDepth=i),e&&(this._depth=e)}function r(t,e,i,s){n.call(this,t,e,i,s),this._stuckChildren=[]}function a(t,e){this.x=t||0,this.y=e||0}o.prototype.root=null,o.prototype.insert=function(t){if(t instanceof Array){var e,i=t.length;for(e=0;e<i;e++)this.root.insert(t[e])}else this.root.insert(t)},o.prototype.clear=function(){this.root.clear()},o.prototype.retrieve=function(t){return this.root.retrieve(t).slice(0)},n.prototype.nodes=null,n.prototype._classConstructor=n,n.prototype.children=null,n.prototype._bounds=null,n.prototype._depth=0,n.prototype._maxChildren=4,n.prototype._maxDepth=4,n.TOP_LEFT=0,n.TOP_RIGHT=1,n.BOTTOM_LEFT=2,n.BOTTOM_RIGHT=3,n.prototype.insert=function(t){if(this.nodes.length){var e=this._findIndex(t);this.nodes[e].insert(t)}else{this.children.push(t);var i=this.children.length;if(!(this._depth>=this._maxDepth)&&i>this._maxChildren){var s;for(this.subdivide(),s=0;s<i;s++)this.insert(this.children[s]);this.children.length=0}}},n.prototype.retrieve=function(t){if(this.nodes.length){var e=this._findIndex(t);return this.nodes[e].retrieve(t)}return this.children},n.prototype._findIndex=function(t){var e=this._bounds,i=!(t.x>e.x+e.width/2),s=!(t.y>e.y+e.height/2),o=n.TOP_LEFT;return i?s||(o=n.BOTTOM_LEFT):o=s?n.TOP_RIGHT:n.BOTTOM_RIGHT,o},n.prototype.subdivide=function(){var t=this._depth+1,e=this._bounds.x,i=this._bounds.y,s=this._bounds.width/2,o=this._bounds.height/2,r=e+s,a=i+o;this.nodes[n.TOP_LEFT]=new this._classConstructor({x:e,y:i,width:s,height:o},t,this._maxDepth,this._maxChildren),this.nodes[n.TOP_RIGHT]=new this._classConstructor({x:r,y:i,width:s,height:o},t,this._maxDepth,this._maxChildren),this.nodes[n.BOTTOM_LEFT]=new this._classConstructor({x:e,y:a,width:s,height:o},t,this._maxDepth,this._maxChildren),this.nodes[n.BOTTOM_RIGHT]=new this._classConstructor({x:r,y:a,width:s,height:o},t,this._maxDepth,this._maxChildren)},n.prototype.clear=function(){this.children.length=0;var t,e=this.nodes.length;for(t=0;t<e;t++)this.nodes[t].clear();this.nodes.length=0},r.prototype=new n,r.prototype._classConstructor=r,r.prototype._stuckChildren=null,r.prototype._out=[],r.prototype.insert=function(t){if(this.nodes.length){var e=this._findIndex(t),i=this.nodes[e];t.x>=i._bounds.x&&t.x+t.width<=i._bounds.x+i._bounds.width&&t.y>=i._bounds.y&&t.y+t.height<=i._bounds.y+i._bounds.height?this.nodes[e].insert(t):this._stuckChildren.push(t)}else{this.children.push(t);var s=this.children.length;if(!(this._depth>=this._maxDepth)&&s>this._maxChildren){var o;for(this.subdivide(),o=0;o<s;o++)this.insert(this.children[o]);this.children.length=0}}},r.prototype.getChildren=function(){return this.children.concat(this._stuckChildren)},r.prototype.retrieve=function(t){var e=this._out;if(e.length=0,this.nodes.length){var i=this._findIndex(t),s=this.nodes[i];t.x>=s._bounds.x&&t.x+t.width<=s._bounds.x+s._bounds.width&&t.y>=s._bounds.y&&t.y+t.height<=s._bounds.y+s._bounds.height?e.push.apply(e,this.nodes[i].retrieve(t)):(t.x<=this.nodes[n.TOP_RIGHT]._bounds.x&&(t.y<=this.nodes[n.BOTTOM_LEFT]._bounds.y&&e.push.apply(e,this.nodes[n.TOP_LEFT].getAllContent()),t.y+t.height>this.nodes[n.BOTTOM_LEFT]._bounds.y&&e.push.apply(e,this.nodes[n.BOTTOM_LEFT].getAllContent())),t.x+t.width>this.nodes[n.TOP_RIGHT]._bounds.x&&(t.y<=this.nodes[n.BOTTOM_RIGHT]._bounds.y&&e.push.apply(e,this.nodes[n.TOP_RIGHT].getAllContent()),t.y+t.height>this.nodes[n.BOTTOM_RIGHT]._bounds.y&&e.push.apply(e,this.nodes[n.BOTTOM_RIGHT].getAllContent())))}return e.push.apply(e,this._stuckChildren),e.push.apply(e,this.children),e},r.prototype.getAllContent=function(){var t,e=this._out;if(this.nodes.length)for(t=0;t<this.nodes.length;t++)this.nodes[t].getAllContent();return e.push.apply(e,this._stuckChildren),e.push.apply(e,this.children),e},r.prototype.clear=function(){this._stuckChildren.length=0,this.children.length=0;var t=this.nodes.length;if(t){var e;for(e=0;e<t;e++)this.nodes[e].clear();this.nodes.length=0}},a.prototype.copy=a.prototype.copy=function(t){return this.x=t.x,this.y=t.y,this},a.prototype.clone=a.prototype.clone=function(){return new a(this.x,this.y)},a.prototype.perp=a.prototype.perp=function(){var t=this.x;return this.x=this.y,this.y=-t,this},a.prototype.rotate=a.prototype.rotate=function(t){var e=this.x,i=this.y;return this.x=e*Math.cos(t)-i*Math.sin(t),this.y=e*Math.sin(t)+i*Math.cos(t),this},a.prototype.reverse=a.prototype.reverse=function(){return this.x=-this.x,this.y=-this.y,this},a.prototype.normalize=a.prototype.normalize=function(){var t=this.len();return t>0&&(this.x=this.x/t,this.y=this.y/t),this},a.prototype.add=a.prototype.add=function(t){return this.x+=t.x,this.y+=t.y,this},a.prototype.sub=a.prototype.sub=function(t){return this.x-=t.x,this.y-=t.y,this},a.prototype.scale=a.prototype.scale=function(t,e){return this.x*=t,this.y*=void 0!==e?e:t,this},a.prototype.project=a.prototype.project=function(t){var e=this.dot(t)/t.len2();return this.x=e*t.x,this.y=e*t.y,this},a.prototype.projectN=a.prototype.projectN=function(t){var e=this.dot(t);return this.x=e*t.x,this.y=e*t.y,this},a.prototype.reflect=a.prototype.reflect=function(t){var e=this.x,i=this.y;return this.project(t).scale(2),this.x-=e,this.y-=i,this},a.prototype.reflectN=a.prototype.reflectN=function(t){var e=this.x,i=this.y;return this.projectN(t).scale(2),this.x-=e,this.y-=i,this},a.prototype.dot=a.prototype.dot=function(t){return this.x*t.x+this.y*t.y},a.prototype.len2=a.prototype.len2=function(){return this.dot(this)},a.prototype.len=a.prototype.len=function(){return Math.sqrt(this.len2())};var l={Solid:class{constructor(t,e){this.body=t,this.style=e.style,this.fixture=t.fixture,this.position=t.state.position}update(){}render(){let t=this.body,e=t.context;switch(e.fillStyle=this.style.fillStyle,e.lineWidth=this.style.lineWidth,e.strokeStyle=this.style.strokeStyle,e.lineJoin="miter",t.fixture.type){case"circle":e.beginPath(),e.ellipse(t.state.position.x,t.state.position.y,t.fixture.r,t.fixture.r,0,0,2*Math.PI),e.closePath(),e.stroke(),e.fill();break;case"polygon":e.beginPath(),e.moveTo(t.state.position.x+t.fixture.points[0].x,t.state.position.y+t.fixture.points[0].y);for(let i=1;i<t.fixture.points.length;i++){let s=t.fixture.points[i];e.lineTo(t.state.position.x+s.x,t.state.position.y+s.y)}e.lineTo(t.state.position.x+t.fixture.points[0].x,t.state.position.y+t.fixture.points[0].y),e.fill(),e.stroke()}}},Sprite:class{constructor(t,e){if(this.size={x:e.tilewidth,y:e.tileheight},this.frame,this.grid=[],this.image=e.image,this.object=t,this.style=e.style,this.fixture=t.fixture,this.position=t.state.position,e.frames=e.frames||[0],this.image instanceof Image)for(let t=0;t<this.image.height;t+=this.size.y)for(let e=0;e<this.image.width;e+=this.size.x)this.grid.push({x:e,y:t});this.animation={frames:e.frames}}update(){this.frame=this.animation.frames.shift(),this.animation.frames.push(this.frame)}render(){let t=this.object,e=this.image,i=t.context;switch(i.fillStyle=this.style.fillStyle,i.lineWidth=this.style.lineWidth,i.strokeStyle=this.style.strokeStyle,i.lineJoin="miter",t.fixture.type){case"circle":{i.save(),i.beginPath(),i.ellipse(t.state.position.x,t.state.position.y,t.fixture.r,t.fixture.r,0,0,2*Math.PI),i.clip();let s=this.frame;e instanceof Image?i.drawImage(this.image,this.grid[s].x,this.grid[s].y,this.size.x,this.size.y,t.state.position.x-t.fixture.r,t.state.position.y-t.fixture.r,this.size.x,this.size.y):(i.fill(),i.stroke()),i.closePath(),i.restore();break}case"polygon":{i.save(),i.beginPath(),i.moveTo(t.state.position.x+t.fixture.points[0].x,t.state.position.y+t.fixture.points[0].y);for(let e=1;e<t.fixture.points.length;e++){let s=t.fixture.points[e];i.lineTo(t.state.position.x+s.x,t.state.position.y+s.y)}i.lineTo(t.state.position.x+t.fixture.points[0].x,t.state.position.y+t.fixture.points[0].y),i.clip();let s=this.frame;e instanceof Image?i.drawImage(this.image,this.grid[s].x,this.grid[s].y,this.size.x,this.size.y,t.state.position.x,t.state.position.y,this.size.x,this.size.y):(i.fill(),i.stroke()),i.closePath(),i.restore();break}}}}};class h{constructor(t){let e=a,i=Number,s=Object,o=String,n=Array;t.debug=t.debug||!1,t.position=t.position||new e,t.angle=t.angle||new i,t.maxVelocity=t.maxVelocity||new e(200,200),t.velocity=t.velocity||new e,t.acceleration=t.acceleration||new e,t.mass=t.mass||new i(1),t.force=t.force||new e,t.texture=t.texture||new s({}),t.texture.style=t.texture.style||new s({}),t.texture.style.fillStyle=t.texture.style.fillStyle||new o("#ddd"),t.texture.style.lineWidth=t.texture.style.lineWidth||new i(2),t.texture.style.strokeStyle=t.texture.style.strokeStyle||new o("#333"),this.debug=t.debug,this.state={},this.state.position=t.position,this.state.angle=t.angle,t.collision=t.collision||new s({}),t.collision.collides=t.collision.collides||!0,t.collision.group=t.collision.group||new n(["everyone"]),t.collision.checkAgenist=t.collision.checkAgenist||new n(["everyone"]),t.gravityFactor=t.gravityFactor||new e(1,1),this.collision={collides:t.collision.collides,group:t.collision.group,checkAgenist:t.collision.checkAgenist},this.state.force=t.force,this.gravityFactor=t.gravityFactor,this.state.mass=t.mass,this.state.maxVelocity=t.maxVelocity,this.state.velocity=t.velocity,this.state.acceleration=t.acceleration,this.types={kinematic:"kinematic",dynamic:"dynamic"},this.type=t.type,this.fixture={},this.texture=new l.Sprite(this,t.texture)}update(){}render(){let t=this.texture.style,e=this.context;if(this.debug){e.save(),e.beginPath(),e.strokeStyle="red",e.lineWidth="1";let t=this.fixture.getAABB().edges,i=(t[0].x-t[2].x)/2,s=(t[1].y-t[3].y)/2,o=this.state.position,n=o.x,r=o.y;"circle"==this.fixture.type&&(n=o.x-i/2,r=o.y-s/2),e.rect(n,r,i,s),e.stroke(),e.restore()}e.fillStyle=t.fillStyle,e.lineWidth=t.lineWidth,e.strokeStyle=t.strokeStyle,e.lineJoin="miter"}}class c{constructor(t,e){var i={delta:1e3/e,elapsed:0,tframe:1e3/e,nframe:e,before:window.performance.now(),main:function(){i.startLoop=window.requestAnimationFrame(i.main),i.delta=Math.round(1e3/(window.performance.now()-i.before)*100/100),window.performance.now()<i.before+i.tframe||(i.before=window.performance.now(),i.stopLoop=()=>{window.cancelAnimationFrame(i.startLoop)},t.state&&(t.state.loop=i),t.update(i.elapsed,i.delta),t.render(i.elapsed,i.delta),i.elapsed++)}};return i.main(),i}}class u{constructor(t,e,i){i=document.querySelector(i||"body");var s=document.createElement("canvas"),o=s.getContext("2d"),n=window.devicePixelRatio/["webkitBackingStorePixelRatio","mozBackingStorePixelRatio","msBackingStorePixelRatio","oBackingStorePixelRatio","backingStorePixelRatio"].reduce(function(t,e){return Object.prototype.hasOwnProperty.call(o,e)?o[e]:1});return s.width=Math.round(t*n),s.height=Math.round(e*n),s.style.width=t+"px",s.style.height=e+"px",o.setTransform(n,0,0,n,0,0),i.insertBefore(s,i.firstChild),s.context=s.getContext("2d"),s.resize=(t,e)=>{s.style.width=e.x+"px",s.style.height=e.y+"px",s.width=Math.round(e.x*n),s.height=Math.round(e.y*n),o.setTransform(n,0,0,n,0,0),t.state.size.x=e.x,t.state.size.y=e.y},s.clear=(t,e,i,o,n)=>{t=t||null,e=e||0,i=i||0,o=o||s.width,n=n||s.height,t?(s.context.save(),s.context.fillStyle=t,s.context.fillRect(e,i,o,n),s.context.fill(),s.context.restore()):s.context.clearRect(e,i,o,n)},s.camera=(t,e)=>{s.context.setTransform(1,0,0,1,0,0),s.context.translate(-t,-e)},s}}class p{constructor(t,e){this.type="polygon",this.pos=t||new a,this.angle=0,this.offset=new a,this.setPoints(e||[])}setPoints(t){if(!this.points||this.points.length!==t.length){var e,i=this.calcPoints=[],s=this.edges=[],o=this.normals=[];for(e=0;e<t.length;e++){var n=t[e],r=e<t.length-1?t[e+1]:t[0];n===r||n.x!==r.x||n.y!==r.y?(i.push(new a),s.push(new a),o.push(new a)):(t.splice(e,1),e-=1)}}return this.points=t,this._recalc(),this}setAngle(t){return this.angle=t,this._recalc(),this}setOffset(t){return this.offset=t,this._recalc(),this}rotate(t){for(var e=this.points,i=e.length,s=0;s<i;s++)e[s].rotate(t);return this._recalc(),this}translate(t,e){for(var i=this.points,s=i.length,o=0;o<s;o++)i[o].x+=t,i[o].y+=e;return this._recalc(),this}_recalc(){var t,e=this.calcPoints,i=this.edges,s=this.normals,o=this.points,n=this.offset,r=this.angle,a=o.length;for(t=0;t<a;t++){var l=e[t].copy(o[t]);l.x+=n.x,l.y+=n.y,0!==r&&l.rotate(r)}for(t=0;t<a;t++){var h=e[t],c=t<a-1?e[t+1]:e[0],u=i[t].copy(c).sub(h);s[t].copy(u).perp().normalize()}return this}getAABB(){for(var t=this.calcPoints,e=t.length,i=t[0].x,s=t[0].y,o=t[0].x,n=t[0].y,r=1;r<e;r++){var l=t[r];l.x<i?i=l.x:l.x>o&&(o=l.x),l.y<s?s=l.y:l.y>n&&(n=l.y)}return new d(this.pos.clone().add(new a(i,s)),o-i,n-s).toPolygon()}getCentroid(){for(var t=this.calcPoints,e=t.length,i=0,s=0,o=0,n=0;n<e;n++){var r=t[n],l=n===e-1?t[0]:t[n+1],h=r.x*l.y-l.x*r.y;i+=(r.x+l.x)*h,s+=(r.y+l.y)*h,o+=h}return new a(i/=o*=3,s/=o)}}class d{constructor(t,e,i){this.pos=t||new a,this.w=e||0,this.h=i||0}toPolygon(){var t=this.pos,e=this.w,i=this.h;return new p(new a(t.x,t.y),[new a,new a(e,0),new a(e,i),new a(0,i)])}}class y{constructor(t,e){this.type="circle",this.pos=t||new a,this.r=e||0,this.offset=new a}getAABB(){var t=this.r,e=this.pos.clone().add(this.offset).sub(new a(t,t));return new d(e,2*t,2*t).toPolygon()}getCentroid(){let t=this.getAABB(),e=t.edges[3].x,i=t.edges[2].y;return new a((t.edges[0].x-e)/2,(t.edges[1].y-i)/2)}setOffset(t){return this.offset=t,this}}var f={Circle:y,Polygon:p};class x{constructor(t,e,i){let s=Number,o=Object,n=String;this.state={},this.collision={collides:!0},this.type="kinematic",this.context=t,(e=e||new o({})).style=e.style||new o({}),e.style.fillStyle=e.style.fillStyle||new n("#ddd"),e.style.lineWidth=e.style.lineWidth||new s(2),e.style.strokeStyle=e.style.strokeStyle||new n("#333"),e.frames=[Math.floor(i[0])],this.state.position=new a(e.tilewidth*i[1],e.tileheight*i[2]),this.fixture=new f.Polygon(this.state.position,[{x:0,y:0},{x:0,y:e.tileheight},{x:e.tilewidth,y:e.tileheight},{x:e.tilewidth,y:0}]),this.texture=new l.Sprite(this,e)}}class g{constructor(t,e,i){this.tilemaps=[],this.level=null,this.quad=i,this.pool=[],this.context=e}__load(t){let e=[];this.pool=[],t.tilesets.forEach(t=>{e.push(t)}),t.layers.forEach(t=>{t.data.forEach(t=>{let i=e[t[3]],s=new Image;s.src=i.image,s.onload=()=>{i.image=s;let e=new x(this.context,i,t);this.quad.insert(e),this.pool.push(e)}})})}load(t,e){this.tilemaps[e]=t}set(t){this.tilemaps[t]&&this.__load(this.tilemaps[t])}update(t){this.pool.forEach(t=>{let e=t,i=e.fixture.getAABB().edges,s=(i[0].x-i[2].x)/2,o=(i[1].y-i[3].y)/2,n=e.state.position,r=n.x,a=n.y;"circle"==e.fixture.type&&(r=n.x-s/2,a=n.y-o/2),this.quad.insert({x:r,y:a,width:s,height:o,item:e}),e.texture.update()})}render(t){this.pool.forEach(t=>{t.texture.render()})}}class m{static collides(t,e){return!(!t.collision.collides||!e.collision.collides)}static query(t,e){let i=[];return e.retrieve(t).forEach(t=>{i.push(t.item)}),i}}var v={AABB:d,Circle:y,Polygon:p};function w(){this.a=null,this.b=null,this.overlapN=new a,this.overlapV=new a,this.clear()}v.Response=w,w.prototype.clear=w.prototype.clear=function(){return this.aInB=!0,this.bInA=!0,this.overlap=Number.MAX_VALUE,this};for(var b=[],_=0;_<10;_++)b.push(new a);var T=[];for(_=0;_<5;_++)T.push([]);var I=new w,P=new d(new a,1e-6,1e-6).toPolygon();function O(t,e,i){for(var s=Number.MAX_VALUE,o=-Number.MAX_VALUE,n=t.length,r=0;r<n;r++){var a=t[r].dot(e);a<s&&(s=a),a>o&&(o=a)}i[0]=s,i[1]=o}function M(t,e,i,s,o,n){var r=T.pop(),a=T.pop(),l=b.pop().copy(e).sub(t),h=l.dot(o);if(O(i,o,r),O(s,o,a),a[0]+=h,a[1]+=h,r[0]>a[1]||a[0]>r[1])return b.push(l),T.push(r),T.push(a),!0;if(n){var c,u,p=0;if(r[0]<a[0])if(n.aInB=!1,r[1]<a[1])p=r[1]-a[0],n.bInA=!1;else p=(c=r[1]-a[0])<(u=a[1]-r[0])?c:-u;else if(n.bInA=!1,r[1]>a[1])p=r[0]-a[1],n.aInB=!1;else p=(c=r[1]-a[0])<(u=a[1]-r[0])?c:-u;var d=Math.abs(p);d<n.overlap&&(n.overlap=d,n.overlapN.copy(o),p<0&&n.overlapN.reverse())}return b.push(l),T.push(r),T.push(a),!1}function k(t,e){var i=t.len2(),s=e.dot(t);return s<0?A:s>i?S:C}v.isSeparatingAxis=M;var A=-1,C=0,S=1;function B(t,e,i){for(var s=b.pop().copy(e.pos).add(e.offset).sub(t.pos),o=e.r,n=o*o,r=t.calcPoints,a=r.length,l=b.pop(),h=b.pop(),c=0;c<a;c++){var u=c===a-1?0:c+1,p=0===c?a-1:c-1,d=0,y=null;l.copy(t.edges[c]),h.copy(s).sub(r[c]),i&&h.len2()>n&&(i.aInB=!1);var f=k(l,h);if(f===A){l.copy(t.edges[p]);var x=b.pop().copy(s).sub(r[p]);if((f=k(l,x))===S){if((m=h.len())>o)return b.push(s),b.push(l),b.push(h),b.push(x),!1;i&&(i.bInA=!1,y=h.normalize(),d=o-m)}b.push(x)}else if(f===S){if(l.copy(t.edges[u]),h.copy(s).sub(r[u]),(f=k(l,h))===A){if((m=h.len())>o)return b.push(s),b.push(l),b.push(h),!1;i&&(i.bInA=!1,y=h.normalize(),d=o-m)}}else{var g=l.perp().normalize(),m=h.dot(g),v=Math.abs(m);if(m>0&&v>o)return b.push(s),b.push(g),b.push(h),!1;i&&(y=g,d=o-m,(m>=0||d<2*o)&&(i.bInA=!1))}y&&i&&Math.abs(d)<Math.abs(i.overlap)&&(i.overlap=d,i.overlapN.copy(y))}return i&&(i.a=t,i.b=e,i.overlapV.copy(i.overlapN).scale(i.overlap)),b.push(s),b.push(l),b.push(h),!0}function E(t,e,i){for(var s=t.calcPoints,o=s.length,n=e.calcPoints,r=n.length,a=0;a<o;a++)if(M(t.pos,e.pos,s,n,t.normals[a],i))return!1;for(a=0;a<r;a++)if(M(t.pos,e.pos,s,n,e.normals[a],i))return!1;return i&&(i.a=t,i.b=e,i.overlapV.copy(i.overlapN).scale(i.overlap)),!0}v.pointInCircle=function(t,e){var i=b.pop().copy(t).sub(e.pos).sub(e.offset),s=e.r*e.r,o=i.len2();return b.push(i),o<=s},v.pointInPolygon=function(t,e){P.pos.copy(t),I.clear();var i=E(P,e,I);return i&&(i=I.aInB),i},v.testCircleCircle=function(t,e,i){var s=b.pop().copy(e.pos).add(e.offset).sub(t.pos).sub(t.offset),o=t.r+e.r,n=o*o,r=s.len2();if(r>n)return b.push(s),!1;if(i){var a=Math.sqrt(r);i.a=t,i.b=e,i.overlap=o-a,i.overlapN.copy(s.normalize()),i.overlapV.copy(s).scale(i.overlap),i.aInB=t.r<=e.r&&a<=e.r-t.r,i.bInA=e.r<=t.r&&a<=t.r-e.r}return b.push(s),!0},v.testPolygonCircle=B,v.testCirclePolygon=function(t,e,i){var s=B(e,t,i);if(s&&i){var o=i.a,n=i.aInB;i.overlapN.reverse(),i.overlapV.reverse(),i.a=i.b,i.b=o,i.aInB=i.bInA,i.bInA=n}return s},v.testPolygonPolygon=E;var z=v;class j{static overlap(t,e){return!0}static query(t,e){let i=[];return e.forEach(e=>{this.overlap(e,t)&&i.push(e)}),i}}class F{constructor(){this.response=new z.Response}resolve(t,e){e.forEach(e=>{let i;(i=e.fixture instanceof y?t.fixture instanceof y?z.testCircleCircle(e.fixture,t.fixture,this.response):z.testCirclePolygon(e.fixture,t.fixture,this.response):t.fixture instanceof y?z.testPolygonCircle(e.fixture,t.fixture,this.response):z.testPolygonPolygon(e.fixture,t.fixture,this.response))&&("kinematic"==e.type?(t.state.position.add(this.response.overlapV),t.state.velocity.add(this.response.overlapV)):"kinematic"==t.type?(e.state.position.sub(this.response.overlapV),e.state.velocity.sub(this.response.overlapV)):(this.response.overlapV.scale(.5),e.state.position.sub(this.response.overlapV),t.state.position.add(this.response.overlapV),e.state.velocity.sub(this.response.overlapV),t.state.velocity.add(this.response.overlapV))),this.response.clear()})}}class V{constructor(t){t.entities=t.entities||[],t.size=t.size||new a(1e3,500),t.gravity=t.gravity||new a,t.quad=t.quad||new a,t.resources=t.resources||{},t.resources.image=t.resources.image||[],t.resources.audio=t.resources.audio||[],t.resources.data=t.resources.data||[],t.fps=t.fps||60,t.debug=t.debug||!1,this.entities=t.entities,this.debug=t.debug,this.state={size:t.size,gravity:t.gravity},this.viewport=new u(this.state.size.x,this.state.size.y,t.container),this.context=this.viewport.context,this.quad=new o({x:t.quad.x,y:t.quad.y,width:t.size.x,height:t.size.y}),this.solver=new F,this.tilemap=new g(t.tilemap,this.context,this.quad),this.loop=new c(this,t.fps),window.cg=this}addObject(t){return t.context=this.context,this.entities.push(t)}removeObject(t){return this.entities.splice(t,1)}update(){this.quad.clear(),this.tilemap.update(),this.entities.forEach(t=>{let e=t,i=e.fixture.getAABB().edges,s=(i[0].x-i[2].x)/2,o=(i[1].y-i[3].y)/2,n=e.state.position,r=n.x,a=n.y;"circle"==e.fixture.type&&(r=n.x-s/2,a=n.y-o/2),this.quad.insert({x:r,y:a,width:s,height:o,item:e});let l=m.query(t,this.quad),h=j.query(t,l);this.solver.resolve(t,h);let c=this.state.gravity.clone().scale(t.gravityFactor.x,t.gravityFactor.y);t.state.mass;t.state.acceleration.add(c),t.state.velocity.add(t.state.acceleration),t.state.velocity.x=Math.min(t.state.velocity.x,t.state.maxVelocity.x),t.state.velocity.y=Math.min(t.state.velocity.y,t.state.maxVelocity.y),t.state.position.add(t.state.velocity.clone().scale(1/this.state.loop.nframe)),t.update()})}render(){if(this.viewport.clear(),this.tilemap.render(),this.debug){let t=this.quad.root,e=t=>{let i=t._bounds,o=(s.abs,this.context);o.save(),o.strokeStyle="green",o.lineWidth="2",o.beginPath(),o.rect(i.x,i.y,i.width,i.height);let n,r=t.getChildren(),a=r.length;if(a)for(let t=0;t<a;t++)n=r[t],o.beginPath(),o.rect(n.x,n.y,n.width,n.height);let l=t.nodes.length;for(let i=0;i<l;i++)e(t.nodes[i]);o.stroke(),o.restore()};e(t)}this.entities.forEach(t=>{t.render(),t.texture.update(),t.texture.render()})}}i.d(e,"Craters",function(){return q}),i.d(e,"Game",function(){return V}),i.d(e,"Entity",function(){return h}),i.d(e,"Fixtures",function(){return f}),i.d(e,"Vector",function(){return a}),i.d(e,"Maths",function(){return s});class q{static version(){return"1.2.2"}}}]); |
{ | ||
"name": "craters.js", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "A Compact Game Engine that helps you build fast, modern HTML5 Games", | ||
@@ -38,6 +38,6 @@ "main": "./index.js", | ||
"devDependencies": { | ||
"@purtuga/esm-webpack-plugin": "^1.1.1", | ||
"chai": "^4.2.0", | ||
"esm": "^3.2.25", | ||
"sat-js": "^0.8.0" | ||
"esm": "^3.2.25" | ||
} | ||
} |
@@ -6,3 +6,3 @@ # Craters.js | ||
![craters.js logo](https://swashvirus.github.io/craters.js/craters.gif) | ||
[craters.js documentation](https://swashvirus.github.io/documentation-craters.js/) | ||
[craters.js documentation](https://swashvirus.github.io/craters.js/docs/index.html) | ||
@@ -13,62 +13,62 @@ #### Short description | ||
* [Panda Breakout sample game](https://swashvirus.github.io/craters.js/examples/breakout-game/index.html) | ||
* [Tilemap sample game](https://swashvirus.github.io/craters.js/tests/tilemap.test/index.html) | ||
#### Features ✨ | ||
- The Changelog outlines more features [Read changelog](CHANGELOG.md) | ||
- ES modules | ||
* reduces bundle size you only import what you need | ||
- Sound | ||
* a sound system used for loading and playing audio files | ||
- Sprite | ||
* a sprite system it draws and animates images as sprites | ||
#### Rigid Body Physics. | ||
- Loader | ||
* a file loading utility used for pre-loading image files and caching them so that they can be used by i.e the sprite system | ||
- QuadTrees Spatial Subdivision | ||
* Broadphase collision detection | ||
- Entity | ||
* a base class for deriving your game entities from, with update and render methods as well as the state object with predefined variables properties velocity, acceleration, position and size | ||
- Separating Axis Theorem | ||
* Narrow phase collision detection | ||
- Game | ||
* a base class for deriving your games from it has a state object just like the entity it also has methods update and render. | ||
- Input | ||
* a helper for input ie keyboard | ||
#### Structure | ||
game is a method used to create an instance of game world | ||
the game and entities both have methods update and render | ||
#### Tile Engine | ||
#### Let's make a game 🚀 | ||
more example games included in the examples and test folder alternatively you can build the games on your own using webpack | ||
### installation | ||
#### Clone repository | ||
- Sprite system | ||
* Renders animated images and solid color. | ||
#### Additional features | ||
- Emscript6 modules | ||
* Reduces your package size | ||
#### Additional Modules | ||
- Assets module | ||
* Loads images, Audio and json files. | ||
- Input module | ||
* Captures keyboard input | ||
- Sound module | ||
* Creates instances of audio files a fork of Soundbox.js | ||
### Installation | ||
- Clone git repository | ||
```bash | ||
git clone https://github.com/swashvirus/craters.js.git | ||
# import app/craters/craters.js | ||
``` | ||
#### Npm install | ||
- Npm package | ||
```bash | ||
npm install craters.js | ||
``` | ||
#### Global object | ||
```bash | ||
Craters is the global Object used in the iife build | ||
``` | ||
##### Writing the demo game yourself | ||
#### Let's make a game 🚀 | ||
example games are included in the examples and test directory | ||
##### Writing an example "it's working" game. | ||
```javascript | ||
'use strict'; | ||
import { Game, Canvas, Loop } from './craters/craters.js' | ||
import {Game, Vector} from '../../craters/craters' | ||
class mygame extends Game { | ||
constructor() { | ||
super({ | ||
fps: 60, | ||
container: '#container', | ||
size: new Vector(1024, 512) | ||
}); | ||
} | ||
class mygame extends Game { | ||
constructor (container, width, height){ | ||
super (); | ||
this.state.size = {x: width, y: height} | ||
this.viewport = new Canvas(this.state.size.x, this.state.size.y, container); | ||
this.context = this.viewport.context | ||
this.loop = new Loop(this, 60) | ||
} | ||
render () { | ||
this.viewport.clear() | ||
super.render() | ||
render() { | ||
super.render(); | ||
// draw some text on the screen | ||
this.context.fillStyle = "#fff"; | ||
this.context.font = '2em Arial' | ||
@@ -78,5 +78,4 @@ this.context.fillText('It\'s working.️', 65, (this.state.size.y / 2), (this.state.size.x)) | ||
} | ||
window.game = new mygame('#container', window.innerWidth, window.innerHeight, 60, true) | ||
let game = new mygame(); | ||
``` | ||
Submit Issues, fixes and Contributions `PR` | ||
Submit Issues, fixes and Contributions. |
@@ -1,117 +0,90 @@ | ||
import { | ||
Game, | ||
Loop, | ||
Canvas, | ||
Entity, | ||
Maths | ||
} from '../../index.js' | ||
// experimental | ||
// workinprogress++ | ||
import Box2D from './Box2d.js' | ||
import {Game, Entity, Fixtures, Vector} from '../../craters/craters' | ||
// game | ||
class mygame extends Game { | ||
constructor(container, width, height) { | ||
super(); | ||
const b2Vec2 = Box2D.Common.Math.b2Vec2, | ||
b2BodyDef = Box2D.Dynamics.b2BodyDef, | ||
b2Body = Box2D.Dynamics.b2Body, | ||
b2FixtureDef = Box2D.Dynamics.b2FixtureDef, | ||
b2Fixture = Box2D.Dynamics.b2Fixture, | ||
b2World = Box2D.Dynamics.b2World, | ||
b2MassData = Box2D.Collision.Shapes.b2MassData, | ||
b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape, | ||
b2CircleShape = Box2D.Collision.Shapes.b2CircleShape, | ||
b2DebugDraw = Box2D.Dynamics.b2DebugDraw; | ||
this.state.size = { | ||
x: width, | ||
y: height | ||
} | ||
this.loop = new Loop(this, 60) | ||
this.viewport = new Canvas(this.state.size.x, this.state.size.y, container); | ||
this.context = this.viewport.context; | ||
this.viewport.style.background = "#eee"; | ||
this.viewport.resize(this, { | ||
x: window.innerWidth, | ||
y: window.innerHeight | ||
}) | ||
class Box2dGame extends Game { | ||
constructor(params) { | ||
super(params); | ||
let gravity = new b2Vec2( | ||
this.state.gravity.x, | ||
this.state.gravity.y | ||
); | ||
// create box2d world | ||
this.world = new b2World(gravity); | ||
} | ||
// Todo figure out fixture | ||
addObject(obj) { | ||
this.world.CreateBody(obj.body) | ||
Box2D.SCALE = 1; | ||
this.state.gravity.y = 100; | ||
var gravity = new Box2D.Common.Math.b2Vec2(0, this.state.gravity.y); | ||
this.world = new Box2D.Dynamics.b2World(gravity, true); | ||
// ground | ||
var bodyDef = new Box2D.Dynamics.b2BodyDef(); | ||
bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody; | ||
// fixture | ||
var fixDef = new Box2D.Dynamics.b2FixtureDef; | ||
fixDef.density = 1.0; | ||
fixDef.friction = 0.5; | ||
fixDef.restitution = 0.2; | ||
// shape | ||
fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape(); | ||
fixDef.shape.SetAsBox(this.state.size.x, 10); | ||
bodyDef.position = new Box2D.Common.Math.b2Vec2(0, this.state.size.y); | ||
// add ground | ||
this.world.CreateBody(bodyDef).CreateFixture(fixDef); | ||
// add marbles | ||
for (var i = 0; i < 25; i++) { | ||
let id = this.addObject(new marble(this)) - 1 | ||
this.entities[id].id = id; | ||
} | ||
super.addObject(obj) | ||
} | ||
update(elapsed, fps) { | ||
this.world.Step(1 / fps, 10, 10); | ||
this.world.Step((1 / fps), 10, 10); | ||
this.world.ClearForces(); | ||
super.update() | ||
super.update () | ||
} | ||
render() { | ||
this.viewport.clear() | ||
super.render() | ||
} | ||
} | ||
// body | ||
class marble extends Entity { | ||
constructor(scope) { | ||
super(); | ||
this.scope = scope | ||
this.state.pos = new Maths.Vector((this.scope.state.size.x / 2) + ((Math.random() - 0.5) * 50), (this.scope.state.size.x / 2) + ((Math.random() - 0.5) * 50)) | ||
this.state.radius = (Math.random() * 50) + 10; | ||
this.type = "static"; | ||
class Box2dEntity extends Entity { | ||
constructor(params) { | ||
super(params); | ||
var bodyDef = new Box2D.Dynamics.b2BodyDef(); | ||
bodyDef.position = new Box2D.Common.Math.b2Vec2( | ||
(this.state.pos.x + this.state.size.x / 2) * Box2D.SCALE, | ||
(this.state.pos.y + this.state.size.y / 2) * Box2D.SCALE | ||
(this.state.position.x) + 70 / 2, | ||
(this.state.position.y) + 70 / 2 | ||
); | ||
switch (body.type) { | ||
case 'dynamic': | ||
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; | ||
break; | ||
case 'static': | ||
bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody; | ||
break; | ||
} | ||
this.body = bodyDef; | ||
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; | ||
this.body = this.scope.world.CreateBody(bodyDef); | ||
var fixture = new Box2D.Dynamics.b2FixtureDef; | ||
fixture.shape = new Box2D.Collision.Shapes.b2CircleShape( | ||
this.state.radius | ||
); | ||
switch (body.fixture.type) { | ||
case 'polygon': | ||
fixture.shape = new b2PolygonShape; | ||
let points = []; | ||
for (let i = 0; i < this.fixture.points.length; i++) { | ||
var vec = new Box2D.Common.Math.b2Vec2(this.fixture.points[i].x, this.fixture.points[i].y); | ||
points[i] = vec; | ||
} | ||
fixture.shape.SetAsArray(points, points.length); | ||
break; | ||
case 'circle': | ||
var fixture = new Box2D.Dynamics.b2FixtureDef; | ||
fixture.shape = new Box2D.Collision.Shapes.b2CircleShape( | ||
this.fixture.r | ||
); | ||
break; | ||
} | ||
fixture.density = 1.0; | ||
fixture.friction = 10.5; | ||
fixture.friction = 1.5; | ||
fixture.restitution = 0.3; | ||
this.body.fixture = fixture; | ||
this.body.CreateFixture(fixture); | ||
this.body.ApplyImpulse(new Box2D.Common.Math.b2Vec2(((Math.random() - 0.5) * 100), ((Math.random() - 0.7) * 100)), this.body.GetPosition()); | ||
update() { | ||
this.state.position = this.body.GetPosition(); | ||
super.update() | ||
} | ||
} | ||
render() { | ||
this.state.pos = this.body.GetPosition(); | ||
this.scope.context.beginPath(); | ||
this.scope.context.arc((this.state.pos.x), (this.state.pos.y), (this.state.radius), 0, Math.PI * 2); | ||
this.scope.context.lineWidth = 1; | ||
this.scope.context.strokeStyle = '#000'; | ||
this.scope.context.fillStyle = 'green'; | ||
this.scope.context.fill(); | ||
this.scope.context.stroke(); | ||
} | ||
} | ||
let game = new mygame('#container', 1024, 512) | ||
} |
@@ -1,22 +0,15 @@ | ||
import { | ||
Game, | ||
Loop, | ||
Canvas, | ||
Entity, | ||
Maths | ||
} from '../../index.js' | ||
import {Game, Entity, Fixtures, Vector} from '../../craters/craters' | ||
import {loadImage} from '../../craters/Modules/Assets/Image.js' | ||
// Game | ||
class mygame extends Game { | ||
constructor(container, width, height) { | ||
super(); | ||
this.state.size = { | ||
x: width, | ||
y: height | ||
} | ||
super({ | ||
debug: false, | ||
fps: 60, | ||
container: '#container', | ||
size: new Vector(500, 500), | ||
hash: new Vector(50, 50), | ||
resources: {image: ['./bug.png'], data: ['./map.json']} | ||
}); | ||
this.loop = new Loop(this, 60) | ||
this.viewport = new Canvas(this.state.size.x, this.state.size.y, container); | ||
this.context = this.viewport.context; | ||
this.viewport.style.background = "#eee"; | ||
@@ -26,6 +19,7 @@ this.viewport.resize(this, { | ||
y: window.innerHeight | ||
}) | ||
}); | ||
// this.state.gravity = new Vector(0, 0.01) | ||
// add some marbles | ||
for (var i = 0; i < 25; i++) { | ||
for (var i = 0; i < 10; i++) { | ||
let id = this.addObject(new marble(this)) - 1 | ||
@@ -37,3 +31,2 @@ this.entities[id].id = id; | ||
render() { | ||
this.viewport.clear() | ||
super.render() | ||
@@ -46,64 +39,55 @@ } | ||
constructor(scope) { | ||
super() | ||
let params = { | ||
debug: false, | ||
texture: { | ||
style: { | ||
fillStyle: "green", | ||
strokeStyle: "white" | ||
}, | ||
/*frames: [0], | ||
tileheight: '196', | ||
tilewidth: '218', | ||
image: sprite.fetch('./bug.png')*/ | ||
}, | ||
velocity: new Vector(((Math.random() - 0.5) * 300), ((Math.random() - 0.5) * 300)) | ||
} | ||
super(params); | ||
this.scope = scope; | ||
this.type = 'dynamic'; | ||
this.shape = new SAT.Circle(new SAT.Vector((this.scope.state.size.x / 2), (this.scope.state.size.x / 2)), (Math.random() * 50) + 10); | ||
this.state.pos = this.shape.pos | ||
this.state.gravity.y = 1; | ||
this.state.friction = { | ||
x: 0.0125, | ||
y: 0.0125 | ||
} | ||
this.state.vel.x = ((Math.random() - 0.5)); | ||
this.state.vel.y = ((Math.random() - 0.5)); | ||
this.state.position.x = ((Math.random()) * this.scope.state.size.x) | ||
this.state.position.y = ((Math.random()) * this.scope.state.size.y) | ||
this.fixture = (Math.random() < 0.5) ? new Fixtures.Circle(this.state.position, 50) : new Fixtures.Polygon(this.state.position, [{x:0, y:0}, {x:0, y:50}, {x:50, y:50}, {x:50, y:0}]); | ||
this.fixture.r = this.fixture.r || 0; | ||
} | ||
update() { | ||
super.update(); | ||
// X-axis collision | ||
if ((this.state.pos.x + this.shape.r > this.scope.state.size.x) || (this.state.pos.x < 0)) { | ||
this.state.vel.x *= 0 | ||
} | ||
// Y-axis collision | ||
if ((this.state.pos.y + this.shape.r > this.scope.state.size.y) || (this.state.pos.y < 0)) { | ||
this.state.vel.y *= 0 | ||
} | ||
for (var i = 0; i < this.scope.entities.length; i++) { | ||
if (this.scope.entities[i].id == this.id) continue; | ||
var response = new SAT.Response(); | ||
var collided = SAT.testCircleCircle(this.shape, this.scope.entities[i].shape, response); | ||
if (collided) { | ||
response.overlapV.scale(0.5); | ||
this.state.pos.sub(response.overlapV); | ||
this.scope.entities[i].state.pos.add(response.overlapV); | ||
this.state.vel.subtract(response.overlapN) | ||
this.scope.entities[i].state.vel.add(response.overlapN) | ||
} | ||
} | ||
this.state.pos.y = Maths.boundary((this.state.pos.y), (this.shape.r), (this.scope.state.size.y - (this.shape.r))) | ||
// X-axis collision | ||
if ((this.state.position.x + this.fixture.r > this.scope.state.size.x)) { | ||
if (this.state.velocity.x < 0) return | ||
this.state.velocity.x *= -1 | ||
} | ||
// Y-axis collision | ||
if ((this.state.position.y + this.fixture.r > this.scope.state.size.y)) { | ||
if (this.state.velocity.y < 0) return | ||
this.state.velocity.y *= -1 | ||
} | ||
if ((this.state.position.x - this.fixture.r < 0)) { | ||
if (this.state.velocity.x > 0) return | ||
this.state.velocity.x *= -1 | ||
} | ||
// Y-axis collision | ||
if ((this.state.position.y - this.fixture.r < 0)) { | ||
if (this.state.velocity.y > 0) return | ||
this.state.velocity.y *= -1 | ||
} | ||
} | ||
render() { | ||
this.scope.context.beginPath(); | ||
this.scope.context.arc((this.state.pos.x), (this.state.pos.y), (this.shape.r), 0, Math.PI * 2); | ||
this.scope.context.lineWidth = 1; | ||
this.scope.context.strokeStyle = '#000'; | ||
this.scope.context.fillStyle = 'green'; | ||
this.scope.context.fill(); | ||
this.scope.context.stroke(); | ||
super.render(); | ||
} | ||
} | ||
let game = new mygame('#container', 1024, 512) | ||
let sprite = new loadImage() | ||
let image = sprite.load('./bug.png', () => { | ||
let game = new mygame('#container', 1024, 512) | ||
}); |
@@ -1,20 +0,12 @@ | ||
import { | ||
Game, | ||
Loop, | ||
Canvas | ||
} from '../../index.js' | ||
import {Game, Vector} from '../../craters/craters' | ||
// game | ||
class mygame extends Game { | ||
constructor(container, width, height) { | ||
super(); | ||
this.state.size = { | ||
x: width, | ||
y: height | ||
} | ||
this.loop = new Loop(this, 60) | ||
this.viewport = new Canvas(this.state.size.x, this.state.size.y, container); | ||
this.context = this.viewport.context; | ||
constructor() { | ||
super({ | ||
fps: 60, | ||
container: '#container', | ||
size: new Vector(1000, 500) | ||
}); | ||
this.viewport.style.background = "#eee"; | ||
@@ -28,2 +20,3 @@ this.viewport.resize(this, { | ||
update(elapsed, fps) { | ||
super.update() | ||
// console.log('tick ' + elapsed + ' at:' + fps + ' fps') | ||
@@ -33,4 +26,3 @@ } | ||
render() { | ||
this.viewport.clear() | ||
super.render() | ||
super.render(); | ||
@@ -43,2 +35,2 @@ this.context.fillStyle = "#fff"; | ||
let game = new mygame('#container', 1024, 512) | ||
let game = new mygame(); |
@@ -1,682 +0,4 @@ | ||
! function(t) { | ||
var e = {}; | ||
function n(i) { | ||
if (e[i]) return e[i].exports; | ||
var s = e[i] = { | ||
i: i, | ||
l: !1, | ||
exports: {} | ||
}; | ||
return t[i].call(s.exports, s, s.exports, n), s.l = !0, s.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 s in t) n.d(i, s, function(e) { | ||
return t[e] | ||
}.bind(null, s)); | ||
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 = 7) | ||
}([function(t, e, n) { | ||
"use strict"; | ||
n.d(e, "a", function() { | ||
return s | ||
}), n.d(e, "b", function() { | ||
return i | ||
}), n.d(e, "c", function() { | ||
return r | ||
}); | ||
class i { | ||
constructor(t, e, n, i, s) { | ||
this.entities = [], this.state = { | ||
container: t, | ||
size: { | ||
x: 512, | ||
y: 512 | ||
}, | ||
gravity: { | ||
x: 0, | ||
y: 0 | ||
}, | ||
friction: { | ||
x: 0, | ||
y: 0 | ||
} | ||
} | ||
} | ||
addObject(t) { | ||
return this.entities.push(t) | ||
} | ||
removeObject(t) { | ||
return this.entities.splice(t, 1) | ||
} | ||
update() { | ||
this.entities.forEach(t => { | ||
switch (t.update(), t.type) { | ||
case "dynamic": | ||
t.state.vel.x += t.state.accel.x + (t.state.gravity.x + this.state.gravity.x), t.state.vel.y += t.state.accel.y + (t.state.gravity.y + this.state.gravity.y), t.state.pos.x += t.state.vel.x, t.state.pos.y += t.state.vel.y; | ||
var e = t.state.friction.x, | ||
n = t.state.vel.x + e, | ||
i = t.state.vel.x - e, | ||
s = t.state.friction.y, | ||
r = t.state.vel.y + s, | ||
o = t.state.vel.y - s; | ||
t.state.vel.x = n < 0 ? n : i > 0 ? i : 0, t.state.vel.y = r < 0 ? r : o > 0 ? o : 0; | ||
break; | ||
case "kinematic": | ||
t.state.vel.x += t.state.accel.x, t.state.vel.y += t.state.accel.y, t.state.pos.x += t.state.vel.x, t.state.pos.y += t.state.vel.y | ||
} | ||
}) | ||
} | ||
render() { | ||
this.entities.forEach(t => { | ||
t.render() | ||
}) | ||
} | ||
} | ||
class s extends i { | ||
constructor() { | ||
super(), this.state.size = { | ||
x: 10, | ||
y: 10 | ||
}, this.state.pos = { | ||
x: 0, | ||
y: 0 | ||
}, this.state.vel = { | ||
x: 0, | ||
y: 0 | ||
}, this.state.accel = { | ||
x: 0, | ||
y: 0 | ||
}, this.state.radius = 10, this.state.angle = 0, this.type = "dynamic" | ||
} | ||
} | ||
class r extends s { | ||
constructor(t, e) { | ||
super(), this.scope = t, this.state.pos = e.pos || { | ||
x: 0, | ||
y: 0 | ||
}, this.state.crop = { | ||
x: 0, | ||
y: 0 | ||
}, this.state.size = e.size || { | ||
x: 0, | ||
y: 0 | ||
}, this.state.frames = e.frames || [], this.state.angle = e.angle || 0, this.state.image = e.image || new Image, this.state.delay = e.delay || 5, this.state.tick = e.tick || 0, this.state.orientation = e.orientation || "horizontal" | ||
} | ||
update() { | ||
super.update(), this.state.tick <= 0 && ("vertical" === this.orientation ? (this.state.crop.y = this.state.frames.shift(), this.state.frames.push(this.state.crop.y)) : (this.state.crop.x = this.state.frames.shift(), this.state.frames.push(this.state.crop.x)), this.state.tick = this.state.delay), this.state.tick-- | ||
} | ||
render() { | ||
super.render(), this.scope.context.save(), this.scope.context.translate(this.state.crop.x + this.state.size.x / 2, this.state.crop.y + this.state.size.y / 2), this.scope.context.rotate(this.state.angle * (Math.PI / 180)), this.scope.context.translate(-(this.state.crop.x + this.state.size.x / 2), -(this.state.crop.y + this.state.size.y / 2)), this.scope.context.drawImage(this.state.image, this.state.crop.x * this.state.size.x, this.state.crop.y * this.state.size.y, this.state.size.x, this.state.size.y, this.state.pos.x, this.state.pos.y, this.state.size.x, this.state.size.y), this.scope.context.restore() | ||
} | ||
} | ||
}, function(t, e, n) { | ||
t.exports = n(8) | ||
}, function(t, e) { | ||
var n; | ||
n = function() { | ||
return this | ||
}(); | ||
try { | ||
n = n || new Function("return this")() | ||
} catch (t) { | ||
"object" == typeof window && (n = window) | ||
} | ||
t.exports = n | ||
}, function(t, e, n) { | ||
"use strict"; | ||
n.d(e, "a", function() { | ||
return s | ||
}), n.d(e, "b", function() { | ||
return i | ||
}); | ||
class i { | ||
constructor(t, e) { | ||
var n = { | ||
elapsed: 0, | ||
tframe: 1e3 / e, | ||
before: window.performance.now(), | ||
main: function() { | ||
n.startLoop = window.requestAnimationFrame(n.main), n.fps = Math.round(1e3 / (window.performance.now() - n.before) * 100 / 100), window.performance.now() < n.before + n.tframe || (n.before = window.performance.now(), n.stopLoop = () => { | ||
window.cancelAnimationFrame(n.startLoop) | ||
}, t.update(n.elapsed, n.fps), t.render(n.elapsed, n.fps), n.elapsed++) | ||
} | ||
}; | ||
return n.main(), n | ||
} | ||
} | ||
class s { | ||
constructor(t, e, n) { | ||
n = document.querySelector(n || "body"); | ||
var i = document.createElement("canvas"), | ||
s = i.getContext("2d"), | ||
r = window.devicePixelRatio / ["webkitBackingStorePixelRatio", "mozBackingStorePixelRatio", "msBackingStorePixelRatio", "oBackingStorePixelRatio", "backingStorePixelRatio"].reduce(function(t, e) { | ||
return Object.prototype.hasOwnProperty.call(s, e) ? s[e] : 1 | ||
}); | ||
return i.width = Math.round(t * r), i.height = Math.round(e * r), i.style.width = t + "px", i.style.height = e + "px", s.setTransform(r, 0, 0, r, 0, 0), n.insertBefore(i, n.firstChild), i.context = i.getContext("2d"), i.resize = (t, e) => { | ||
i.style.width = e.x + "px", i.style.height = e.y + "px", i.width = Math.round(e.x * r), i.height = Math.round(e.y * r), s.setTransform(r, 0, 0, r, 0, 0), t.state.size.x = e.x, t.state.size.y = e.y | ||
}, i.clear = (t, e, n, s) => { | ||
t = t || 0, e = e || 0, n = n || i.width, s = s || i.height, i.context.clearRect(t, e, n, s) | ||
}, i.camera = (t, e) => { | ||
i.context.setTransform(1, 0, 0, 1, 0, 0), i.context.translate(-t, -e) | ||
}, i | ||
} | ||
} | ||
}, function(t, e, n) { | ||
"use strict"; | ||
n.r(e), n.d(e, "Vector", function() { | ||
return c | ||
}), n.d(e, "boundary", function() { | ||
return a | ||
}), n.d(e, "degToRad", function() { | ||
return i | ||
}), n.d(e, "radToDeg", function() { | ||
return s | ||
}), n.d(e, "distance", function() { | ||
return r | ||
}), n.d(e, "map", function() { | ||
return o | ||
}); | ||
const i = function(t) { | ||
return t * Math.PI / 180 | ||
}, | ||
s = function(t) { | ||
return 180 * t / Math.PI | ||
}, | ||
r = function(t, e, n, i) { | ||
return Math.sqrt((n - t) * (n - t) + (i - e) * (i - e)) | ||
}, | ||
o = function(t, e, n, i, s) { | ||
return i + (s - i) * (t - e) / (n - e) | ||
}, | ||
a = function(t, e, n) { | ||
return Math.min(Math.max(t, e), n) | ||
}, | ||
c = function(t, e) { | ||
this.x = t || 0, this.y = e || 0 | ||
}; | ||
c.prototype = { | ||
clone: function() { | ||
return new c(this.x, this.y) | ||
}, | ||
set: function(t, e) { | ||
return "x" === t ? this.x = e : "y" === t && (this.y = e), this | ||
}, | ||
add: function() { | ||
let t, e, n = arguments; | ||
if (1 === n.length) { | ||
let i = n[0]; | ||
"object" == typeof i && (t = i.x, e = i.y) | ||
} else 2 === n.length && "number" == typeof n[0] && "number" == typeof n[1] && (t = n[0], e = n[1]); | ||
return this.x += t, this.y += e, this | ||
}, | ||
subtract: function(t) { | ||
return this.x -= t.x, this.y -= t.y, this | ||
}, | ||
multiply: function(t) { | ||
return "object" == typeof t ? (this.x *= t.x, this.y *= t.y) : "number" == typeof t && (this.x *= t, this.y *= t), this | ||
}, | ||
magnitude: function() { | ||
return Math.sqrt(this.x * this.x + this.y * this.y) | ||
}, | ||
magnitudeSq: function() { | ||
return this.x * this.x + this.y * this.y | ||
}, | ||
negate: function() { | ||
return this.x = -this.x, this.y = -this.y, this | ||
}, | ||
rotate: function(t) { | ||
let e = Math.sin(t), | ||
n = Math.cos(t), | ||
i = this.x * n - this.y * e, | ||
s = this.x * e + this.y * n; | ||
return this.x = i, this.y = s, this | ||
}, | ||
dot: function(t) { | ||
return this.x * t.x + this.y * t.y | ||
}, | ||
cross: function(t) { | ||
return this.x * t.y - this.y * t.x | ||
}, | ||
angleTo: function(t) { | ||
let e = this.magnitude(), | ||
n = t.magnitude(), | ||
i = this.dot(t); | ||
return Math.acos(i / (e * n)) | ||
}, | ||
getAngle: function(t) { | ||
if ("DEGREES" === t) return s(Math.atan(this.y / this.x)); | ||
let e = Math.atan2(this.y, this.x); | ||
return e < 0 ? 2 * Math.PI + e : e | ||
}, | ||
normalize: function() { | ||
let t = this.magnitude(); | ||
return this.x /= t, this.y /= t, this | ||
}, | ||
perp: function(t) { | ||
if ("right" === t) { | ||
let t = this.x; | ||
this.x = this.y, this.y = -t | ||
} else { | ||
let t = this.x; | ||
this.x = -this.y, this.y = t | ||
} | ||
return this | ||
}, | ||
distanceTo: function(t) { | ||
return Math.sqrt((t.x - this.x) * (t.x - this.x) + (t.y - this.y) * (t.y - this.y)) | ||
}, | ||
scalarProject: function(t) { | ||
return this.dot(t) / t.magnitude() | ||
}, | ||
scalarProjectUnit: function(t) { | ||
return this.dot(t) | ||
}, | ||
vectorProject: function(t) { | ||
let e = this.dot(t) / t.magnitudeSq(); | ||
return this.x = t.x * e, this.y = t.y * e, this | ||
}, | ||
vectorProjectUnit: function(t) { | ||
let e = this.dot(t); | ||
return this.x = t.x * e, this.y = t.y * e, this | ||
} | ||
}, c.prototype.translate = c.prototype.add; | ||
c.add = function(t, e) { | ||
return new c(t.x + e.x, t.y + e.y) | ||
}, c.subtract = function(t, e) { | ||
return new c(t.x - e.x, t.y - e.y) | ||
}, c.multiply = function(t, e) { | ||
return "number" == typeof t && "number" == typeof e ? t * e : "object" == typeof t && "number" == typeof e ? new c(t.x * e, t.y * e) : "object" == typeof e && "number" == typeof t ? new c(t * e.x, t * e.y) : new c(t.x * e.x, t.y * e.y) | ||
}, c.dot = function(t, e) { | ||
return t.x * e.x + t.y * e.y | ||
}, c.angleBetween = function(t, e) { | ||
let n = t.magnitude(), | ||
i = e.magnitude(), | ||
s = t.dot(e); | ||
return Math.acos(s / (n * i)) | ||
}, c.perp = function(t, e) { | ||
switch (e) { | ||
case "right": | ||
return new c(t.y, -t.x); | ||
default: | ||
return new c(-t.y, t.x) | ||
} | ||
}, c.negate = function(t) { | ||
return new c(-t.x, -t.y) | ||
} | ||
}, function(t, e, n) { | ||
"use strict"; | ||
n.d(e, "a", function() { | ||
return i | ||
}); | ||
class i { | ||
constructor() { | ||
this.rescache = {} | ||
} | ||
load(t, e) { | ||
var n = this; | ||
t instanceof Array ? t.forEach(function(t) { | ||
n.rescache[t] = !1, n.fetch(t, e) | ||
}) : (n.rescache[t] = !1, n.fetch(t, e)) | ||
} | ||
fetch(t, e) { | ||
var n = this; | ||
if (n.rescache[t]) return n.rescache[t]; | ||
var i = new Image; | ||
i.onload = function() { | ||
n.rescache[t] = i, n.ready(e) | ||
}, i.src = t | ||
} | ||
ready(t) { | ||
if ("function" == typeof t) { | ||
var e = !0; | ||
for (var n in this.rescache) Object.prototype.hasOwnProperty.call(this.rescache, n) && !this.rescache[n] && (e = !1); | ||
e && t() | ||
} | ||
} | ||
} | ||
}, function(t, e, n) { | ||
"use strict"; | ||
n.d(e, "a", function() { | ||
return i | ||
}); | ||
class i { | ||
constructor() { | ||
this.sounds = {}, this.instances = [], this.defaultVolume = 1 | ||
} | ||
load(t, e, n) { | ||
if (this.sounds[t] = new Audio(e), "function" != typeof n) return new Promise((e, n) => { | ||
this.sounds[t].addEventListener("canplaythrough", e), this.sounds[t].addEventListener("error", n) | ||
}); | ||
this.sounds[t].addEventListener("canplaythrough", n) | ||
} | ||
remove(t) { | ||
void 0 !== this.sounds && delete this.sounds[t] | ||
} | ||
unlock(t, e, n, i) { | ||
var s = this, | ||
r = ["touchstart", "touchend", "mousedown", "keydown"], | ||
o = function o() { | ||
r.forEach(function(t) { | ||
document.body.removeEventListener(t, o) | ||
}), s.play(t, e, n, i) | ||
}; | ||
r.forEach(function(t) { | ||
document.body.addEventListener(t, o, !1) | ||
}) | ||
} | ||
play(t, e, n, i) { | ||
if (i = i || !1, void 0 === this.sounds[t]) return console.error("Can't find sound called '" + t + "'."), !1; | ||
var s = this.sounds[t].cloneNode(!0); | ||
return s.volume = "number" == typeof n ? n : this.defaultVolume, s.loop = i, s.play(), this.instances.push(s), s.addEventListener("ended", () => { | ||
var t = this.instances.indexOf(s); - 1 !== t && this.instances.splice(t, 1) | ||
}), "function" == typeof e ? (s.addEventListener("ended", e), !0) : new Promise((t, e) => s.addEventListener("ended", t)) | ||
} | ||
stopAll() { | ||
var t = this.instances.slice(); | ||
for (var e of t) e.pause(), e.dispatchEvent(new Event("ended")) | ||
} | ||
} | ||
}, function(t, e, n) { | ||
"use strict"; | ||
n.r(e); | ||
var i = n(1); | ||
new class extends i.Game { | ||
constructor(t, e, n) { | ||
super(), this.state.size = { | ||
x: e, | ||
y: n | ||
}, this.loop = new i.Loop(this, 60), this.viewport = new i.Canvas(this.state.size.x, this.state.size.y, t), this.context = this.viewport.context, this.viewport.style.background = "#eee", this.viewport.resize(this, { | ||
x: window.innerWidth, | ||
y: window.innerHeight | ||
}) | ||
} | ||
update(t, e) {} | ||
render() { | ||
super.render(), this.viewport.clear(), this.context.fillStyle = "#fff", this.context.font = "2em Arial", this.context.fillText("It's working.️", 65, this.state.size.y / 2, this.state.size.x) | ||
} | ||
}("#container", 1024, 512) | ||
}, function(t, e, n) { | ||
"use strict"; | ||
n.r(e), | ||
function(t, i) { | ||
n.d(e, "Craters", function() { | ||
return u | ||
}); | ||
var s = n(4); | ||
n.d(e, "Maths", function() { | ||
return s | ||
}); | ||
var r = n(3); | ||
n.d(e, "Canvas", function() { | ||
return r.a | ||
}), n.d(e, "Loop", function() { | ||
return r.b | ||
}); | ||
var o = n(0); | ||
n.d(e, "Game", function() { | ||
return o.b | ||
}), n.d(e, "Entity", function() { | ||
return o.a | ||
}), n.d(e, "Sprite", function() { | ||
return o.c | ||
}); | ||
var a = n(5); | ||
n.d(e, "Loader", function() { | ||
return a.a | ||
}); | ||
var c = n(6); | ||
n.d(e, "Sound", function() { | ||
return c.a | ||
}), "undefined" == typeof window && t && (t.window = { | ||
performance: { | ||
now: function(t) { | ||
if (!t) return Date.now(); | ||
var e = Date.now(t); | ||
return Math.round(1e3 * e[0] + e[1] / 1e6) | ||
} | ||
}, | ||
requestAnimationFrame: function(t) { | ||
i(() => t(this.performance.now())) | ||
} | ||
}); | ||
class u { | ||
static version() { | ||
return "1.2.2" | ||
} | ||
} | ||
}.call(this, n(2), n(9).setImmediate) | ||
}, function(t, e, n) { | ||
(function(t) { | ||
var i = void 0 !== t && t || "undefined" != typeof self && self || window, | ||
s = Function.prototype.apply; | ||
function r(t, e) { | ||
this._id = t, this._clearFn = e | ||
} | ||
e.setTimeout = function() { | ||
return new r(s.call(setTimeout, i, arguments), clearTimeout) | ||
}, e.setInterval = function() { | ||
return new r(s.call(setInterval, i, arguments), clearInterval) | ||
}, e.clearTimeout = e.clearInterval = function(t) { | ||
t && t.close() | ||
}, r.prototype.unref = r.prototype.ref = function() {}, r.prototype.close = function() { | ||
this._clearFn.call(i, this._id) | ||
}, e.enroll = function(t, e) { | ||
clearTimeout(t._idleTimeoutId), t._idleTimeout = e | ||
}, e.unenroll = function(t) { | ||
clearTimeout(t._idleTimeoutId), t._idleTimeout = -1 | ||
}, e._unrefActive = e.active = function(t) { | ||
clearTimeout(t._idleTimeoutId); | ||
var e = t._idleTimeout; | ||
e >= 0 && (t._idleTimeoutId = setTimeout(function() { | ||
t._onTimeout && t._onTimeout() | ||
}, e)) | ||
}, n(10), e.setImmediate = "undefined" != typeof self && self.setImmediate || void 0 !== t && t.setImmediate || this && this.setImmediate, e.clearImmediate = "undefined" != typeof self && self.clearImmediate || void 0 !== t && t.clearImmediate || this && this.clearImmediate | ||
}).call(this, n(2)) | ||
}, function(t, e, n) { | ||
(function(t, e) { | ||
! function(t, n) { | ||
"use strict"; | ||
if (!t.setImmediate) { | ||
var i, s, r, o, a, c = 1, | ||
u = {}, | ||
h = !1, | ||
f = t.document, | ||
l = Object.getPrototypeOf && Object.getPrototypeOf(t); | ||
l = l && l.setTimeout ? l : t, "[object process]" === {}.toString.call(t.process) ? i = function(t) { | ||
e.nextTick(function() { | ||
y(t) | ||
}) | ||
} : ! function() { | ||
if (t.postMessage && !t.importScripts) { | ||
var e = !0, | ||
n = t.onmessage; | ||
return t.onmessage = function() { | ||
e = !1 | ||
}, t.postMessage("", "*"), t.onmessage = n, e | ||
} | ||
}() ? t.MessageChannel ? ((r = new MessageChannel).port1.onmessage = function(t) { | ||
y(t.data) | ||
}, i = function(t) { | ||
r.port2.postMessage(t) | ||
}) : f && "onreadystatechange" in f.createElement("script") ? (s = f.documentElement, i = function(t) { | ||
var e = f.createElement("script"); | ||
e.onreadystatechange = function() { | ||
y(t), e.onreadystatechange = null, s.removeChild(e), e = null | ||
}, s.appendChild(e) | ||
}) : i = function(t) { | ||
setTimeout(y, 0, t) | ||
} : (o = "setImmediate$" + Math.random() + "$", a = function(e) { | ||
e.source === t && "string" == typeof e.data && 0 === e.data.indexOf(o) && y(+e.data.slice(o.length)) | ||
}, t.addEventListener ? t.addEventListener("message", a, !1) : t.attachEvent("onmessage", a), i = function(e) { | ||
t.postMessage(o + e, "*") | ||
}), l.setImmediate = function(t) { | ||
"function" != typeof t && (t = new Function("" + t)); | ||
for (var e = new Array(arguments.length - 1), n = 0; n < e.length; n++) e[n] = arguments[n + 1]; | ||
var s = { | ||
callback: t, | ||
args: e | ||
}; | ||
return u[c] = s, i(c), c++ | ||
}, l.clearImmediate = d | ||
} | ||
function d(t) { | ||
delete u[t] | ||
} | ||
function y(t) { | ||
if (h) setTimeout(y, 0, t); | ||
else { | ||
var e = u[t]; | ||
if (e) { | ||
h = !0; | ||
try { | ||
! function(t) { | ||
var e = t.callback, | ||
i = t.args; | ||
switch (i.length) { | ||
case 0: | ||
e(); | ||
break; | ||
case 1: | ||
e(i[0]); | ||
break; | ||
case 2: | ||
e(i[0], i[1]); | ||
break; | ||
case 3: | ||
e(i[0], i[1], i[2]); | ||
break; | ||
default: | ||
e.apply(n, i) | ||
} | ||
}(e) | ||
} finally { | ||
d(t), h = !1 | ||
} | ||
} | ||
} | ||
} | ||
}("undefined" == typeof self ? void 0 === t ? this : t : self) | ||
}).call(this, n(2), n(11)) | ||
}, function(t, e) { | ||
var n, i, s = t.exports = {}; | ||
function r() { | ||
throw new Error("setTimeout has not been defined") | ||
} | ||
function o() { | ||
throw new Error("clearTimeout has not been defined") | ||
} | ||
function a(t) { | ||
if (n === setTimeout) return setTimeout(t, 0); | ||
if ((n === r || !n) && setTimeout) return n = setTimeout, setTimeout(t, 0); | ||
try { | ||
return n(t, 0) | ||
} catch (e) { | ||
try { | ||
return n.call(null, t, 0) | ||
} catch (e) { | ||
return n.call(this, t, 0) | ||
} | ||
} | ||
}! function() { | ||
try { | ||
n = "function" == typeof setTimeout ? setTimeout : r | ||
} catch (t) { | ||
n = r | ||
} | ||
try { | ||
i = "function" == typeof clearTimeout ? clearTimeout : o | ||
} catch (t) { | ||
i = o | ||
} | ||
}(); | ||
var c, u = [], | ||
h = !1, | ||
f = -1; | ||
function l() { | ||
h && c && (h = !1, c.length ? u = c.concat(u) : f = -1, u.length && d()) | ||
} | ||
function d() { | ||
if (!h) { | ||
var t = a(l); | ||
h = !0; | ||
for (var e = u.length; e;) { | ||
for (c = u, u = []; ++f < e;) c && c[f].run(); | ||
f = -1, e = u.length | ||
} | ||
c = null, h = !1, | ||
function(t) { | ||
if (i === clearTimeout) return clearTimeout(t); | ||
if ((i === o || !i) && clearTimeout) return i = clearTimeout, clearTimeout(t); | ||
try { | ||
i(t) | ||
} catch (e) { | ||
try { | ||
return i.call(null, t) | ||
} catch (e) { | ||
return i.call(this, t) | ||
} | ||
} | ||
}(t) | ||
} | ||
} | ||
function y(t, e) { | ||
this.fun = t, this.array = e | ||
} | ||
function p() {} | ||
s.nextTick = function(t) { | ||
var e = new Array(arguments.length - 1); | ||
if (arguments.length > 1) | ||
for (var n = 1; n < arguments.length; n++) e[n - 1] = arguments[n]; | ||
u.push(new y(t, e)), 1 !== u.length || h || a(d) | ||
}, y.prototype.run = function() { | ||
this.fun.apply(null, this.array) | ||
}, s.title = "browser", s.browser = !0, s.env = {}, s.argv = [], s.version = "", s.versions = {}, s.on = p, s.addListener = p, s.once = p, s.off = p, s.removeListener = p, s.removeAllListeners = p, s.emit = p, s.prependListener = p, s.prependOnceListener = p, s.listeners = function(t) { | ||
return [] | ||
}, s.binding = function(t) { | ||
throw new Error("process.binding is not supported") | ||
}, s.cwd = function() { | ||
return "/" | ||
}, s.chdir = function(t) { | ||
throw new Error("process.chdir is not supported") | ||
}, s.umask = function() { | ||
return 0 | ||
} | ||
}]); | ||
!function(t){var e={};function r(o){if(e[o])return e[o].exports;var n=e[o]={i:o,l:!1,exports:{}};return t[o].call(n.exports,n,n.exports,r),n.l=!0,n.exports}r.m=t,r.c=e,r.d=function(t,e,o){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:o})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(r.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)r.d(o,n,function(e){return t[e]}.bind(null,n));return o},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=1)}([function(t,e,r){var o,n; | ||
/** @preserve SAT.js - Version 0.8.0 - Copyright 2012 - 2018 - Jim Riecken <jimr@jimr.ca> - released under the MIT License. https://github.com/jriecken/sat-js */ | ||
/** @preserve SAT.js - Version 0.8.0 - Copyright 2012 - 2018 - Jim Riecken <jimr@jimr.ca> - released under the MIT License. https://github.com/jriecken/sat-js */ | ||
!function(i,s){"use strict";void 0===(n="function"==typeof(o=function(){var t={};function e(t,e){this.x=t||0,this.y=e||0}function r(t,r){this.pos=t||new e,this.r=r||0,this.offset=new e}function o(t,r){this.pos=t||new e,this.angle=0,this.offset=new e,this.setPoints(r||[])}function n(t,r,o){this.pos=t||new e,this.w=r||0,this.h=o||0}function i(){this.a=null,this.b=null,this.overlapN=new e,this.overlapV=new e,this.clear()}t.Vector=e,t.V=e,e.prototype.copy=e.prototype.copy=function(t){return this.x=t.x,this.y=t.y,this},e.prototype.clone=e.prototype.clone=function(){return new e(this.x,this.y)},e.prototype.perp=e.prototype.perp=function(){var t=this.x;return this.x=this.y,this.y=-t,this},e.prototype.rotate=e.prototype.rotate=function(t){var e=this.x,r=this.y;return this.x=e*Math.cos(t)-r*Math.sin(t),this.y=e*Math.sin(t)+r*Math.cos(t),this},e.prototype.reverse=e.prototype.reverse=function(){return this.x=-this.x,this.y=-this.y,this},e.prototype.normalize=e.prototype.normalize=function(){var t=this.len();return t>0&&(this.x=this.x/t,this.y=this.y/t),this},e.prototype.add=e.prototype.add=function(t){return this.x+=t.x,this.y+=t.y,this},e.prototype.sub=e.prototype.sub=function(t){return this.x-=t.x,this.y-=t.y,this},e.prototype.scale=e.prototype.scale=function(t,e){return this.x*=t,this.y*=void 0!==e?e:t,this},e.prototype.project=e.prototype.project=function(t){var e=this.dot(t)/t.len2();return this.x=e*t.x,this.y=e*t.y,this},e.prototype.projectN=e.prototype.projectN=function(t){var e=this.dot(t);return this.x=e*t.x,this.y=e*t.y,this},e.prototype.reflect=e.prototype.reflect=function(t){var e=this.x,r=this.y;return this.project(t).scale(2),this.x-=e,this.y-=r,this},e.prototype.reflectN=e.prototype.reflectN=function(t){var e=this.x,r=this.y;return this.projectN(t).scale(2),this.x-=e,this.y-=r,this},e.prototype.dot=e.prototype.dot=function(t){return this.x*t.x+this.y*t.y},e.prototype.len2=e.prototype.len2=function(){return this.dot(this)},e.prototype.len=e.prototype.len=function(){return Math.sqrt(this.len2())},t.Circle=r,r.prototype.getAABB=r.prototype.getAABB=function(){var t=this.r,r=this.pos.clone().add(this.offset).sub(new e(t,t));return new n(r,2*t,2*t).toPolygon()},r.prototype.setOffset=r.prototype.setOffset=function(t){return this.offset=t,this},t.Polygon=o,o.prototype.setPoints=o.prototype.setPoints=function(t){var r=!this.points||this.points.length!==t.length;if(r){var o,n=this.calcPoints=[],i=this.edges=[],s=this.normals=[];for(o=0;o<t.length;o++){var a=t[o],h=o<t.length-1?t[o+1]:t[0];a===h||a.x!==h.x||a.y!==h.y?(n.push(new e),i.push(new e),s.push(new e)):(t.splice(o,1),o-=1)}}return this.points=t,this._recalc(),this},o.prototype.setAngle=o.prototype.setAngle=function(t){return this.angle=t,this._recalc(),this},o.prototype.setOffset=o.prototype.setOffset=function(t){return this.offset=t,this._recalc(),this},o.prototype.rotate=o.prototype.rotate=function(t){for(var e=this.points,r=e.length,o=0;o<r;o++)e[o].rotate(t);return this._recalc(),this},o.prototype.translate=o.prototype.translate=function(t,e){for(var r=this.points,o=r.length,n=0;n<o;n++)r[n].x+=t,r[n].y+=e;return this._recalc(),this},o.prototype._recalc=function(){var t,e=this.calcPoints,r=this.edges,o=this.normals,n=this.points,i=this.offset,s=this.angle,a=n.length;for(t=0;t<a;t++){var h=e[t].copy(n[t]);h.x+=i.x,h.y+=i.y,0!==s&&h.rotate(s)}for(t=0;t<a;t++){var p=e[t],c=t<a-1?e[t+1]:e[0],u=r[t].copy(c).sub(p);o[t].copy(u).perp().normalize()}return this},o.prototype.getAABB=o.prototype.getAABB=function(){for(var t=this.calcPoints,r=t.length,o=t[0].x,i=t[0].y,s=t[0].x,a=t[0].y,h=1;h<r;h++){var p=t[h];p.x<o?o=p.x:p.x>s&&(s=p.x),p.y<i?i=p.y:p.y>a&&(a=p.y)}return new n(this.pos.clone().add(new e(o,i)),s-o,a-i).toPolygon()},o.prototype.getCentroid=o.prototype.getCentroid=function(){for(var t=this.calcPoints,r=t.length,o=0,n=0,i=0,s=0;s<r;s++){var a=t[s],h=s===r-1?t[0]:t[s+1],p=a.x*h.y-h.x*a.y;o+=(a.x+h.x)*p,n+=(a.y+h.y)*p,i+=p}return new e(o/=i*=3,n/=i)},t.Box=n,n.prototype.toPolygon=n.prototype.toPolygon=function(){var t=this.pos,r=this.w,n=this.h;return new o(new e(t.x,t.y),[new e,new e(r,0),new e(r,n),new e(0,n)])},t.Response=i,i.prototype.clear=i.prototype.clear=function(){return this.aInB=!0,this.bInA=!0,this.overlap=Number.MAX_VALUE,this};for(var s=[],a=0;a<10;a++)s.push(new e);for(var h=[],a=0;a<5;a++)h.push([]);var p=new i,c=new n(new e,1e-6,1e-6).toPolygon();function u(t,e,r){for(var o=Number.MAX_VALUE,n=-Number.MAX_VALUE,i=t.length,s=0;s<i;s++){var a=t[s].dot(e);a<o&&(o=a),a>n&&(n=a)}r[0]=o,r[1]=n}function l(t,e,r,o,n,i){var a=h.pop(),p=h.pop(),c=s.pop().copy(e).sub(t),l=c.dot(n);if(u(r,n,a),u(o,n,p),p[0]+=l,p[1]+=l,a[0]>p[1]||p[0]>a[1])return s.push(c),h.push(a),h.push(p),!0;if(i){var y=0;if(a[0]<p[0])if(i.aInB=!1,a[1]<p[1])y=a[1]-p[0],i.bInA=!1;else{var f=a[1]-p[0],x=p[1]-a[0];y=f<x?f:-x}else if(i.bInA=!1,a[1]>p[1])y=a[0]-p[1],i.aInB=!1;else{var f=a[1]-p[0],x=p[1]-a[0];y=f<x?f:-x}var d=Math.abs(y);d<i.overlap&&(i.overlap=d,i.overlapN.copy(n),y<0&&i.overlapN.reverse())}return s.push(c),h.push(a),h.push(p),!1}function y(t,e){var r=t.len2(),o=e.dot(t);return o<0?f:o>r?d:x}t.isSeparatingAxis=l;var f=-1,x=0,d=1;function v(t,e,r){for(var o=s.pop().copy(e.pos).add(e.offset).sub(t.pos),n=e.r,i=n*n,a=t.calcPoints,h=a.length,p=s.pop(),c=s.pop(),u=0;u<h;u++){var l=u===h-1?0:u+1,x=0===u?h-1:u-1,v=0,g=null;p.copy(t.edges[u]),c.copy(o).sub(a[u]),r&&c.len2()>i&&(r.aInB=!1);var w=y(p,c);if(w===f){p.copy(t.edges[x]);var b=s.pop().copy(o).sub(a[x]);if((w=y(p,b))===d){var m=c.len();if(m>n)return s.push(o),s.push(p),s.push(c),s.push(b),!1;r&&(r.bInA=!1,g=c.normalize(),v=n-m)}s.push(b)}else if(w===d){if(p.copy(t.edges[l]),c.copy(o).sub(a[l]),(w=y(p,c))===f){var m=c.len();if(m>n)return s.push(o),s.push(p),s.push(c),!1;r&&(r.bInA=!1,g=c.normalize(),v=n-m)}}else{var P=p.perp().normalize(),m=c.dot(P),M=Math.abs(m);if(m>0&&M>n)return s.push(o),s.push(P),s.push(c),!1;r&&(g=P,v=n-m,(m>=0||v<2*n)&&(r.bInA=!1))}g&&r&&Math.abs(v)<Math.abs(r.overlap)&&(r.overlap=v,r.overlapN.copy(g))}return r&&(r.a=t,r.b=e,r.overlapV.copy(r.overlapN).scale(r.overlap)),s.push(o),s.push(p),s.push(c),!0}function g(t,e,r){for(var o=t.calcPoints,n=o.length,i=e.calcPoints,s=i.length,a=0;a<n;a++)if(l(t.pos,e.pos,o,i,t.normals[a],r))return!1;for(var a=0;a<s;a++)if(l(t.pos,e.pos,o,i,e.normals[a],r))return!1;return r&&(r.a=t,r.b=e,r.overlapV.copy(r.overlapN).scale(r.overlap)),!0}return t.pointInCircle=function(t,e){var r=s.pop().copy(t).sub(e.pos).sub(e.offset),o=e.r*e.r,n=r.len2();return s.push(r),n<=o},t.pointInPolygon=function(t,e){c.pos.copy(t),p.clear();var r=g(c,e,p);return r&&(r=p.aInB),r},t.testCircleCircle=function(t,e,r){var o=s.pop().copy(e.pos).add(e.offset).sub(t.pos).sub(t.offset),n=t.r+e.r,i=n*n,a=o.len2();if(a>i)return s.push(o),!1;if(r){var h=Math.sqrt(a);r.a=t,r.b=e,r.overlap=n-h,r.overlapN.copy(o.normalize()),r.overlapV.copy(o).scale(r.overlap),r.aInB=t.r<=e.r&&h<=e.r-t.r,r.bInA=e.r<=t.r&&h<=t.r-e.r}return s.push(o),!0},t.testPolygonCircle=v,t.testCirclePolygon=function(t,e,r){var o=v(e,t,r);if(o&&r){var n=r.a,i=r.aInB;r.overlapN.reverse(),r.overlapV.reverse(),r.a=r.b,r.b=n,r.aInB=r.bInA,r.bInA=i}return o},t.testPolygonPolygon=g,t})?o.call(e,r,e,t):o)||(t.exports=n)}()},function(t,e,r){"use strict";r.r(e);class o{constructor(t,e){var r={elapsed:0,tframe:1e3/e,nframe:e,before:window.performance.now(),main:function(){r.startLoop=window.requestAnimationFrame(r.main),r.delta=Math.round(1e3/(window.performance.now()-r.before)*100/100),window.performance.now()<r.before+r.tframe||(r.before=window.performance.now(),r.stopLoop=()=>{window.cancelAnimationFrame(r.startLoop)},t.state.loop=r,t.update(r.elapsed,r.delta),t.render(r.elapsed,r.delta),r.elapsed++)}};return r.main(),r}}class n{constructor(t,e,r){r=document.querySelector(r||"body");var o=document.createElement("canvas"),n=o.getContext("2d"),i=window.devicePixelRatio/["webkitBackingStorePixelRatio","mozBackingStorePixelRatio","msBackingStorePixelRatio","oBackingStorePixelRatio","backingStorePixelRatio"].reduce(function(t,e){return Object.prototype.hasOwnProperty.call(n,e)?n[e]:1});return o.width=Math.round(t*i),o.height=Math.round(e*i),o.style.width=t+"px",o.style.height=e+"px",n.setTransform(i,0,0,i,0,0),r.insertBefore(o,r.firstChild),o.context=o.getContext("2d"),o.resize=(t,e)=>{o.style.width=e.x+"px",o.style.height=e.y+"px",o.width=Math.round(e.x*i),o.height=Math.round(e.y*i),n.setTransform(i,0,0,i,0,0),t.state.size.x=e.x,t.state.size.y=e.y},o.clear=(t,e,r,n,i)=>{t=t||null,e=e||0,r=r||0,n=n||o.width,i=i||o.height,t?(o.context.save(),o.context.fillStyle=t,o.context.fillRect(e,r,n,i),o.context.fill(),o.context.restore()):o.context.clearRect(e,r,n,i)},o.camera=(t,e)=>{o.context.setTransform(1,0,0,1,0,0),o.context.translate(-t,-e)},o}}class i{constructor(){this.rescache={}}load(t,e){var r=this;t instanceof Array?t.forEach(function(t){r.rescache[t]=!1,r.fetch(t,e)}):(r.rescache[t]=!1,r.fetch(t,e))}fetch(t,e){var r=this;if(r.rescache[t])return r.rescache[t];var o=new Image;o.onload=function(){r.rescache[t]=o,r.ready(e)},o.src=t}ready(t){if("function"==typeof t){var e=!0;for(var r in this.rescache)Object.prototype.hasOwnProperty.call(this.rescache,r)&&!this.rescache[r]&&(e=!1);e&&t()}}}const s=function(t,e){this.x=t||0,this.y=e||0};(s.prototype={clone:function(){return new s(this.x,this.y)},set:function(t,e){return"x"===t?this.x=e:"y"===t&&(this.y=e),this},add:function(){let t,e,r=arguments;if(1===r.length){let o=r[0];"object"==typeof o&&(t=o.x,e=o.y)}else 2===r.length&&"number"==typeof r[0]&&"number"==typeof r[1]&&(t=r[0],e=r[1]);return this.x+=t,this.y+=e,this},subtract:function(t){return this.x-=t.x,this.y-=t.y,this},multiply:function(t){return"object"==typeof t?(this.x*=t.x,this.y*=t.y):"number"==typeof t&&(this.x*=t,this.y*=t),this},magnitude:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},magnitudeSq:function(){return this.x*this.x+this.y*this.y},negate:function(){return this.x=-this.x,this.y=-this.y,this},rotate:function(t){let e=Math.sin(t),r=Math.cos(t),o=this.x*r-this.y*e,n=this.x*e+this.y*r;return this.x=o,this.y=n,this},dot:function(t){return this.x*t.x+this.y*t.y},cross:function(t){return this.x*t.y-this.y*t.x},angleTo:function(t){let e=this.magnitude(),r=t.magnitude(),o=this.dot(t);return Math.acos(o/(e*r))},getAngle:function(t){if("DEGREES"===t)return 180*Math.atan(this.y/this.x)/Math.PI;let e=Math.atan2(this.y,this.x);return e<0?2*Math.PI+e:e},normalize:function(){let t=this.magnitude();return this.x/=t,this.y/=t,this},perp:function(t){if("right"===t){let t=this.x;this.x=this.y,this.y=-t}else{let t=this.x;this.x=-this.y,this.y=t}return this},distanceTo:function(t){return Math.sqrt((t.x-this.x)*(t.x-this.x)+(t.y-this.y)*(t.y-this.y))},scalarProject:function(t){return this.dot(t)/t.magnitude()},scalarProjectUnit:function(t){return this.dot(t)},vectorProject:function(t){let e=this.dot(t)/t.magnitudeSq();return this.x=t.x*e,this.y=t.y*e,this},vectorProjectUnit:function(t){let e=this.dot(t);return this.x=t.x*e,this.y=t.y*e,this}}).translate=s.prototype.add;s.add=function(t,e){return new s(t.x+e.x,t.y+e.y)},s.subtract=function(t,e){return new s(t.x-e.x,t.y-e.y)},s.multiply=function(t,e){return"number"==typeof t&&"number"==typeof e?t*e:"object"==typeof t&&"number"==typeof e?new s(t.x*e,t.y*e):"object"==typeof e&&"number"==typeof t?new s(t*e.x,t*e.y):new s(t.x*e.x,t.y*e.y)},s.dot=function(t,e){return t.x*e.x+t.y*e.y},s.angleBetween=function(t,e){let r=t.magnitude(),o=e.magnitude(),n=t.dot(e);return Math.acos(n/(r*o))},s.perp=function(t,e){switch(e){case"right":return new s(t.y,-t.x);default:return new s(-t.y,t.x)}},s.negate=function(t){return new s(-t.x,-t.y)};var a=r(0);a.Vector,a.Circle,a.Polygon;class h{constructor(t){this.entities=[],this.state={size:{width:t.size.width,height:t.size.height},gravity:t.gravity},this.resources=new i(t.resources,()=>{console.info("resources loaded")}),this.loop=new o(this,t.fps),this.viewport=new n(100,100,"body"),this.context=this.viewport.context}addObject(t){return this.entities.push(t)}removeObject(t){return this.entities.splice(t,1)}update(t,e){this.entities.forEach(t=>{this.applyforces(t),t.update()})}render(){this.viewport.clear(),this.entities.forEach(t=>{t.render()})}applyforces(t){switch(t.type){case"static":case"dynamic":case"kinematic":t.state.vel.x+=t.state.accel.x,t.state.vel.y+=t.state.accel.y,t.state.pos.x+=t.state.vel.x,t.state.pos.y+=t.state.vel.y;break;default:throw new Error("invalid body type")}}}new class extends h{constructor(){super({fps:60,container:"#container",size:{width:1024,height:512}}),this.viewport.style.background="#eee",this.viewport.resize(this,{x:window.innerWidth,y:window.innerHeight})}update(t,e){super.update()}render(){super.render(),this.context.fillStyle="#fff",this.context.font="2em Arial",this.context.fillText("It's working.️",65,this.state.size.y/2,this.state.size.x)}}}]); |
@@ -1,15 +0,14 @@ | ||
import * as cg from '../../index.js' | ||
class mygame extends cg.Game { | ||
import {Game, Entity, Fixtures, Vector} from '../../craters/craters' | ||
import {loadImage} from '../../craters/Modules/Assets/Image.js' | ||
// Game | ||
class mygame extends Game { | ||
constructor(container, width, height) { | ||
super(); | ||
this.state.size = { | ||
x: width, | ||
y: height | ||
} | ||
this.loop = new cg.Loop(this, 60) | ||
this.viewport = new cg.Canvas(this.state.size.x, this.state.size.y, container); | ||
this.context = this.viewport.context; | ||
super({ | ||
fps: 60, | ||
debug: false, | ||
container: '#container', | ||
size: new Vector(500, 500), | ||
resources: {image:['./tilemap.png'], data: ['./map.json']} | ||
}); | ||
this.viewport.style.background = "#eee"; | ||
@@ -19,160 +18,65 @@ this.viewport.resize(this, { | ||
y: window.innerHeight | ||
}) | ||
this.Tilemap = new Tilemap(this, { | ||
image: media.fetch('./tilemap.png'), | ||
map: { | ||
"t": 32, | ||
"w": 32, | ||
"h": 32, | ||
"z": 1, | ||
"m": [ | ||
[0, 14, 2], | ||
[1, 14, 2], | ||
[2, 14, 2], | ||
[3, 14, 2], | ||
[4, 14, 2], | ||
[5, 14, 2], | ||
[6, 14, 2], | ||
[7, 14, 2], | ||
[14, 14, 2], | ||
[15, 14, 2], | ||
[16, 14, 2], | ||
[17, 14, 2], | ||
[18, 14, 2], | ||
[33, 14, 2], | ||
[34, 14, 2], | ||
[35, 14, 2], | ||
[36, 14, 2], | ||
[37, 14, 2], | ||
[38, 14, 2], | ||
[39, 14, 2], | ||
[39, 13, 2], | ||
[38, 13, 2], | ||
[37, 13, 2], | ||
[36, 13, 2], | ||
[35, 13, 2], | ||
[34, 13, 2], | ||
[32, 14, 2], | ||
[31, 14, 2], | ||
[30, 14, 2], | ||
[29, 14, 2], | ||
[28, 14, 2], | ||
[27, 14, 2], | ||
[26, 14, 2], | ||
[25, 14, 2], | ||
[24, 14, 2], | ||
[23, 14, 2], | ||
[22, 14, 2], | ||
[21, 14, 2], | ||
[20, 14, 2], | ||
[19, 14, 2], | ||
[19, 13, 2], | ||
[18, 13, 2], | ||
[17, 13, 2], | ||
[16, 13, 2], | ||
[15, 13, 2], | ||
[14, 13, 2], | ||
[20, 13, 2], | ||
[21, 13, 2], | ||
[22, 13, 2], | ||
[23, 13, 2], | ||
[24, 13, 2], | ||
[25, 13, 2], | ||
[26, 13, 2], | ||
[27, 13, 2], | ||
[28, 13, 2], | ||
[29, 13, 2], | ||
[30, 13, 2], | ||
[31, 13, 2], | ||
[32, 13, 2], | ||
[33, 13, 2], | ||
[7, 13, 2], | ||
[6, 13, 2], | ||
[5, 13, 2], | ||
[4, 13, 2], | ||
[3, 13, 2], | ||
[2, 13, 2], | ||
[1, 13, 2], | ||
[0, 13, 2], | ||
[8, 13, 2], | ||
[8, 14, 2], | ||
[9, 14, 2], | ||
[9, 13, 2], | ||
[16, 10, 10], | ||
[17, 10, 11], | ||
[16, 11, 26], | ||
[16, 12, 26], | ||
[17, 11, 27], | ||
[17, 12, 27], | ||
[4, 9, 4], | ||
[7, 9, 4], | ||
[34, 6, 12], | ||
[34, 7, 28], | ||
[34, 8, 28], | ||
[34, 9, 28], | ||
[34, 10, 28], | ||
[34, 11, 28], | ||
[34, 12, 28], | ||
[37, 12, 30], | ||
[36, 11, 16], | ||
[36, 12, 16], | ||
[38, 11, 16], | ||
[38, 12, 16], | ||
[37, 11, 14], | ||
[36, 10, 13], | ||
[38, 10, 13], | ||
[37, 10, 15], | ||
[37, 9, 16], | ||
[37, 8, 13], | ||
[1, 12, 17], | ||
[2, 12, 18], | ||
[3, 12, 19], | ||
[21, 12, 23], | ||
[22, 11, 23], | ||
[23, 10, 23], | ||
[24, 11, 24], | ||
[25, 10, 25], | ||
[26, 11, 25], | ||
[27, 12, 25], | ||
[24, 9, 9], | ||
[24, 10, 8], | ||
[23, 11, 8], | ||
[22, 12, 8], | ||
[23, 12, 8], | ||
[24, 12, 8], | ||
[25, 12, 8], | ||
[25, 11, 8], | ||
[26, 12, 8], | ||
[29, 12, 1], | ||
[30, 12, 1], | ||
[30, 11, 1], | ||
[31, 12, 1], | ||
[31, 11, 1], | ||
[31, 10, 1], | ||
[32, 12, 1], | ||
[32, 11, 1], | ||
[32, 10, 1], | ||
[32, 9, 1], | ||
[19, 4, 20], | ||
[20, 4, 21], | ||
[21, 4, 21], | ||
[22, 4, 21], | ||
[23, 4, 22], | ||
[19, 3, 5], | ||
[20, 3, 6], | ||
[21, 3, 6], | ||
[22, 3, 6], | ||
[23, 3, 7] | ||
], | ||
"M": [] | ||
} | ||
}) | ||
for (var i = 0; i < 25; i++) { | ||
this.addObject(new ball(this)) | ||
} | ||
}); | ||
let tilemap = { | ||
"type": "map2d", | ||
"tilesets": [{ | ||
"name": "platformer.png", | ||
"image": "platformer.png", | ||
"fixture": "Polygon", | ||
"imagewidth": 980, | ||
"imageheight": 910, | ||
"tilewidth": 70, | ||
"tileheight": 70, | ||
"tileradius": 35 | ||
}], | ||
"layers": [{ | ||
"id": "event_layer", | ||
"width": 11.428571428571429, | ||
"height": 8.685714285714285, | ||
"data": [] | ||
}, { | ||
"name": "background", | ||
"width": 11.428571428571429, | ||
"height": 8.685714285714285, | ||
"data": [] | ||
}, { | ||
"name": "world", | ||
"width": 11.428571428571429, | ||
"height": 8.685714285714285, | ||
"data": [ | ||
[133, 3, 4, 0], | ||
[119, 4, 4, 0], | ||
[105, 5, 4, 0], | ||
[21, 1, 5, 0], | ||
[35, 1, 6, 0], | ||
[35, 1, 7, 0], | ||
[119, 0, 8, 0], | ||
[119, 1, 8, 0], | ||
[119, 2, 8, 0], | ||
[119, 3, 8, 0], | ||
[119, 4, 8, 0], | ||
[119, 5, 8, 0], | ||
[119, 6, 8, 0], | ||
[119, 7, 8, 0], | ||
[119, 8, 8, 0], | ||
[119, 9, 8, 0], | ||
[119, 10, 8, 0] | ||
] | ||
}], | ||
"canvas": { | ||
"width": 800, | ||
"height": 608 | ||
}, | ||
"events": "[]" | ||
} | ||
this.tilemap.load(tilemap, 'bridge') | ||
this.tilemap.set('bridge') | ||
for (var i = 0; i < 10; i++) { | ||
let id = this.addObject(new marble(this)) - 1 | ||
this.entities[id].id = id; | ||
} | ||
this.state.gravity = new Vector(0, 0.01); | ||
} | ||
render() { | ||
this.viewport.clear() | ||
super.render() | ||
@@ -182,91 +86,51 @@ } | ||
class ball extends cg.Sprite { | ||
class marble extends Entity { | ||
constructor(scope) { | ||
super(scope, { | ||
frames: [12], | ||
image: media.fetch('./tilemap.png') | ||
}) | ||
super({ | ||
debug: false, | ||
texture: { | ||
style: { | ||
fillStyle: "green", | ||
strokeStyle: "white" | ||
}, | ||
frames: [2], | ||
tileheight: '70', | ||
tilewidth: '70', | ||
image: sprite.fetch('./platformer.png') | ||
}, | ||
velocity: new Vector(((Math.random() - 0.5) * 500), ((Math.random() - 0.5) * 500)) | ||
}); | ||
this.scope = scope; | ||
this.state.gravity.y = 0.5; | ||
this.state.friction = { | ||
x: 0.005, | ||
y: 0.005 | ||
} | ||
this.state.vel = { | ||
x: ((Math.random() - 0.5) * 5), | ||
y: ((Math.random() - 0.5) * 5) | ||
} // initiatal velocity | ||
this.state.size = { | ||
x: 32, | ||
y: 32 | ||
} // dimensions | ||
this.state.pos = { | ||
x: (scope.state.size.x / 2) - (this.state.size.x / 2), | ||
y: (scope.state.size.y / 2 - this.state.size.y) | ||
} // initial position | ||
this.state.position.x = ((Math.random()) * this.scope.state.size.x) | ||
this.state.position.y = ((Math.random()) * (this.scope.state.size.y / 3)) | ||
this.fixture = new Fixtures.Polygon(this.state.position, [{x:0, y:0}, {x:0, y:70}, {x:70, y:70}, {x:70, y:0}]); | ||
} | ||
update() { | ||
super.update(); | ||
// X-axis collision | ||
if ((this.state.pos.x + this.state.size.x > this.scope.state.size.x) || (this.state.pos.x < 0)) { | ||
this.state.vel.x *= -1 | ||
} | ||
// Y-axis collision | ||
if ((this.state.pos.y + this.state.size.y > this.scope.state.size.y) || (this.state.pos.y < 0)) { | ||
this.state.vel.y *= -1 | ||
} | ||
this.state.pos.y = cg.Maths.boundary((this.state.pos.y), (this.state.size.y), (this.scope.state.size.y - (this.state.size.y))) | ||
// X-axis collision | ||
if ((this.state.position.x + this.fixture.r > this.scope.state.size.x)) { | ||
if (this.state.velocity.x < 0) return | ||
this.state.velocity.x *= -1 | ||
} | ||
// Y-axis collision | ||
if ((this.state.position.y + this.fixture.r > this.scope.state.size.y)) { | ||
if (this.state.velocity.y < 0) return | ||
this.state.velocity.y *= -1 | ||
} | ||
if ((this.state.position.x - this.fixture.r < 0)) { | ||
if (this.state.velocity.x > 0) return | ||
this.state.velocity.x *= -1 | ||
} | ||
// Y-axis collision | ||
if ((this.state.position.y - this.fixture.r < 0)) { | ||
if (this.state.velocity.y > 0) return | ||
this.state.velocity.y *= -1 | ||
} | ||
} | ||
render() { | ||
super.render(); | ||
// this.scope.viewport.camera((this.state.pos.x - this.scope.state.size.x / 3), (this.state.pos.y - this.scope.state.size.y / 3)) | ||
} | ||
} | ||
class Tilemap { | ||
constructor(scope, data) { | ||
this.scope = scope; | ||
this.map = data.map || {}; | ||
this.image = data.image || new Image(); | ||
class cell extends cg.Sprite { | ||
constructor(scope, args) { | ||
super(scope, { | ||
frames: args.frames | ||
}) | ||
this.scope = scope; | ||
this.state.image = args.image | ||
this.state.size = args.size; | ||
this.state.pos = args.pos; | ||
} | ||
} | ||
this.map.m.forEach((tile) => { | ||
console.log(tile[0] * this.map.w + " " + tile[1] * this.map.h + " " + tile[2]) | ||
this.scope.entities.push(new cell(this.scope, { | ||
image: this.image, | ||
pos: { | ||
x: tile[0] * this.map.w, | ||
y: tile[1] * this.map.h | ||
}, | ||
frames: [tile[2]], | ||
size: { | ||
x: this.map.w, | ||
y: this.map.h | ||
} | ||
})); | ||
}) | ||
} | ||
} | ||
var media = new cg.Loader() | ||
media.load([ | ||
'./tilemap.png' | ||
], function() { | ||
let game = new mygame('#container', 1024, 512) | ||
let sprite = new loadImage() | ||
let image = sprite.load('./platformer.png', () => { | ||
let game = new mygame('#container', 1024, 512) | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
2601652
88
25042