Skip to content

Instantly share code, notes, and snippets.

@BadgerBadgerBadgerBadger
Created December 12, 2018 13:25
Show Gist options
  • Save BadgerBadgerBadgerBadger/531321243045d9f8ee834c1a0cb7c825 to your computer and use it in GitHub Desktop.
Save BadgerBadgerBadgerBadger/531321243045d9f8ee834c1a0cb7c825 to your computer and use it in GitHub Desktop.
/*
* Kontra.js v5.0.4 (Custom Build on 2018-12-12) | MIT
* Build: https://straker.github.io/kontra/download?files=gameLoop+keyboard+sprite+assets+pool+pointer+quadtree+spriteSheet+tileEngine+store
*/
kontra={init(n){var t=this.canvas=document.getElementById(n)||n||document.querySelector("canvas");this.context=t.getContext("2d"),this._init()},_noop:new Function,_tick:new Function,_init:new Function};
kontra.gameLoop=function(e){let t,n,a,r,o=(e=e||{}).fps||60,i=0,p=1e3/o,c=1/o,s=!1===e.clearCanvas?kontra._noop:function(){kontra.context.clearRect(0,0,kontra.canvas.width,kontra.canvas.height)};function d(){if(n=requestAnimationFrame(d),a=performance.now(),r=a-t,t=a,!(r>1e3)){for(kontra._tick(),i+=r;i>=p;)m.update(c),i-=p;s(),m.render()}}let m={update:e.update,render:e.render,isStopped:!0,start(){t=performance.now(),this.isStopped=!1,requestAnimationFrame(d)},stop(){this.isStopped=!0,cancelAnimationFrame(n)}};return m};
!function(){let n={},t={},e={13:"enter",27:"esc",32:"space",37:"left",38:"up",39:"right",40:"down"};for(let n=0;n<26;n++)e[65+n]=(10+n).toString(36);for(i=0;i<10;i++)e[48+i]=""+i;addEventListener("keydown",function(i){let c=e[i.which];t[c]=!0,n[c]&&n[c](i)}),addEventListener("keyup",function(n){t[e[n.which]]=!1}),addEventListener("blur",function(n){t={}}),kontra.keys={bind(t,e){[].concat(t).map(function(t){n[t]=e})},unbind(t,e){[].concat(t).map(function(t){n[t]=e})},pressed:n=>!!t[n]}}();
!function(){class t{constructor(t,i){this._x=t||0,this._y=i||0}add(t,i){this.x+=(t.x||0)*(i||1),this.y+=(t.y||0)*(i||1)}clamp(t,i,h,s){this._c=!0,this._a=t,this._b=i,this._d=h,this._e=s}get x(){return this._x}get y(){return this._y}set x(t){this._x=this._c?Math.min(Math.max(this._a,t),this._d):t}set y(t){this._y=this._c?Math.min(Math.max(this._b,t),this._e):t}}kontra.vector=((i,h)=>new t(i,h)),kontra.vector.prototype=t.prototype;class i{init(t,i,h,s){for(i in t=t||{},this.position=kontra.vector(t.x,t.y),this.velocity=kontra.vector(t.dx,t.dy),this.acceleration=kontra.vector(t.ddx,t.ddy),this.width=this.height=this.rotation=0,this.ttl=1/0,this.anchor={x:0,y:0},this.context=kontra.context,t)this[i]=t[i];if(h=t.image)this.image=h,this.width=h.width,this.height=h.height;else if(h=t.animations){for(i in h)this.animations[i]=h[i].clone(),s=s||h[i];this._ca=s,this.width=s.width,this.height=s.height}return this}get x(){return this.position.x}get y(){return this.position.y}get dx(){return this.velocity.x}get dy(){return this.velocity.y}get ddx(){return this.acceleration.x}get ddy(){return this.acceleration.y}set x(t){this.position.x=t}set y(t){this.position.y=t}set dx(t){this.velocity.x=t}set dy(t){this.velocity.y=t}set ddx(t){this.acceleration.x=t}set ddy(t){this.acceleration.y=t}isAlive(){return this.ttl>0}collidesWith(t){if(this.rotation||t.rotation)return null;let i=this.x-this.width*this.anchor.x,h=this.y-this.height*this.anchor.y,s=t.x,e=t.y;return t.anchor&&(s-=t.width*t.anchor.x,e-=t.height*t.anchor.y),i<s+t.width&&i+this.width>s&&h<e+t.height&&h+this.height>e}update(t){this.advance(t)}render(){this.draw()}playAnimation(t){this._ca=this.animations[t],this._ca.loop||this._ca.reset()}advance(t){this.velocity.add(this.acceleration,t),this.position.add(this.velocity,t),this.ttl--,this._ca&&this._ca.update(t)}draw(){let t=-this.width*this.anchor.x,i=-this.height*this.anchor.y;this.context.save(),this.context.translate(this.x,this.y),this.rotation&&this.context.rotate(this.rotation),this.image?this.context.drawImage(this.image,t,i):this._ca?this._ca.render({x:t,y:i,context:this.context}):(this.context.fillStyle=this.color,this.context.fillRect(t,i,this.width,this.height)),this.context.restore()}}kontra.sprite=(t=>(new i).init(t)),kontra.sprite.prototype=i.prototype}();
!function(){let e,n=/(jpeg|jpg|gif|png)$/,t=/(wav|mp3|ogg|aac)$/,a=/^no$/,o=/^\//,i=/\/$/,r=new Audio,c={wav:"",mp3:r.canPlayType("audio/mpeg;").replace(a,""),ogg:r.canPlayType('audio/ogg; codecs="vorbis"').replace(a,""),aac:r.canPlayType("audio/aac;").replace(a,"")};function u(e,n){return[e.replace(i,""),e?n.replace(o,""):n].filter(e=>e).join("/")}function l(e){return e.split(".").pop()}function d(e){let n=e.replace("."+l(e),"");return 2==n.split("/").length?n.replace(o,""):n}function f(n,t){return new Promise(function(a,o){let i=new Image;t=u(e.imagePath,n),i.onload=function(){let o=e._u(t,window.location.href);e.images[d(n)]=e.images[t]=e.images[o]=this,a(this)},i.onerror=function(){o(t)},i.src=t})}function s(n,t,a){return new Promise(function(o,i){if(n=[].concat(n).reduce(function(e,n){return c[l(n)]?n:e},a)){let a=new Audio;t=u(e.audioPath,n),a.addEventListener("canplay",function(){let a=e._u(t,window.location.href);e.audio[d(n)]=e.audio[t]=e.audio[a]=this,o(this)}),a.onerror=function(){i(t)},a.src=t,a.load()}else i(n)})}function p(n,t){return t=u(e.dataPath,n),fetch(t).then(function(e){if(!e.ok)throw e;return e.clone().json().catch(function(){return e.text()})}).then(function(a){let o=e._u(t,window.location.href);return"object"==typeof a&&e._d.set(a,o),e.data[d(n)]=e.data[t]=e.data[o]=a,a})}e=kontra.assets={images:{},audio:{},data:{},_d:new WeakMap,_u:(e,n)=>new URL(e,n).href,imagePath:"",audioPath:"",dataPath:"",load(){let e,a,o,i,r,c=[];for(i=0;o=arguments[i];i++)r=(a=l(e=[].concat(o)[0])).match(n)?f(o):a.match(t)?s(o):p(o),c.push(r);return Promise.all(c)}}}();
kontra.pool=function(t){let e=0;return{_c:(t=t||{}).create,objects:[t.create()],size:1,maxSize:t.maxSize||1/0,get(t){if(t=t||{},this.objects[0].isAlive()){if(this.size===this.maxSize)return;for(let t=0;t<this.size&&this.objects.length<this.maxSize;t++)this.objects.unshift(this._c());this.size=this.objects.length}let s=this.objects.shift();s.init(t),this.objects.push(s),e++},getAliveObjects(){return this.objects.slice(this.objects.length-e)},clear(){e=this.objects.length=0,this.size=1,this.objects.push(this._c())},update(t){let s,i=this.size-1,h=Math.max(this.objects.length-e,0);for(;i>=h;)(s=this.objects[i]).update(t),s.isAlive()?i--:(this.objects=this.objects.splice(i,1).concat(this.objects),e--,h++)},render(){let t=Math.max(this.objects.length-e,0);for(let e=this.size-1;e>=t;e--)this.objects[e].render()}}};
!function(){let t,n=[],e=[],o={},a=[],r={},i={0:"left",1:"middle",2:"right"};function c(n){let e=n.x,o=n.y;n.anchor&&(e-=n.width*n.anchor.x,o-=n.height*n.anchor.y);let a=t.x-Math.max(e,Math.min(t.x,e+n.width)),r=t.y-Math.max(o,Math.min(t.y,o+n.height));return a*a+r*r<t.radius*t.radius}function s(){let o,a,r=e.length?e:n;for(let n=r.length-1;n>=0;n--)if(a=(o=r[n]).collidesWithPointer?o.collidesWithPointer(t):c(o))return o}function u(t){let n=void 0!==t.button?i[t.button]:"left";r[n]=!0,v(t,"onDown")}function f(t){let n=void 0!==t.button?i[t.button]:"left";r[n]=!1,v(t,"onUp")}function h(t){v(t,"onOver")}function d(t){r={}}function v(n,e){if(!kontra.canvas)return;let a,r;-1!==n.type.indexOf("mouse")?(a=n.pageX,r=n.pageY):(a=(n.touches[0]||n.changedTouches[0]).pageX,r=(n.touches[0]||n.changedTouches[0]).pageY);let i=a-kontra.canvas.offsetLeft,c=r-kontra.canvas.offsetTop,u=kontra.canvas;for(;u=u.offsetParent;)i-=u.offsetLeft,c-=u.offsetTop;let f,h=kontra.canvas.offsetHeight/kontra.canvas.height;i/=h,c/=h,t.x=i,t.y=c,n.target===kontra.canvas&&(n.preventDefault(),(f=s())&&f[e]&&f[e](n)),o[e]&&o[e](n,f)}t=kontra.pointer={x:0,y:0,radius:5,track(t){[].concat(t).map(function(t){t._r||(t._r=t.render,t.render=function(){n.push(this),this._r()},a.push(t))})},untrack(t,n){[].concat(t).map(function(t){t.render=t._r,t._r=n;let e=a.indexOf(t);-1!==e&&a.splice(e,1)})},over:t=>-1!==a.indexOf(t)&&s()===t,onDown(t){o.onDown=t},onUp(t){o.onUp=t},pressed:t=>!!r[t]},kontra._tick=function(){e.length=0,n.map(function(t){e.push(t)}),n.length=0},kontra._init=function(){kontra.canvas.addEventListener("mousedown",u),kontra.canvas.addEventListener("touchstart",u),kontra.canvas.addEventListener("mouseup",f),kontra.canvas.addEventListener("touchend",f),kontra.canvas.addEventListener("blur",d),kontra.canvas.addEventListener("mousemove",h),kontra.canvas.addEventListener("touchmove",h)}}();
kontra.quadtree=function(t){return{maxDepth:(t=t||{}).maxDepth||3,maxObjects:t.maxObjects||25,_b:!1,_d:t.depth||0,bounds:t.bounds||{x:0,y:0,width:kontra.canvas.width,height:kontra.canvas.height},objects:[],subnodes:[],clear(){this.subnodes.map(function(t){t.clear()}),this._b=!1,this.objects.length=0},get(t){let s,h,i=[];for(;this.subnodes.length&&this._b;){for(s=this._g(t),h=0;h<s.length;h++)i.push.apply(i,this.subnodes[s[h]].get(t));return i}return this.objects},add(){let t,s,h,i;for(s=0;s<arguments.length;s++)if(h=arguments[s],Array.isArray(h))this.add.apply(this,h);else if(this._b)this._a(h);else if(this.objects.push(h),this.objects.length>this.maxObjects&&this._d<this.maxDepth){for(this._s(),t=0;i=this.objects[t];t++)this._a(i);this.objects.length=0}},_a(t,s,h){for(s=this._g(t),h=0;h<s.length;h++)this.subnodes[s[h]].add(t)},_g(t){let s=[],h=this.bounds.x+this.bounds.width/2,i=this.bounds.y+this.bounds.height/2,e=t.y<i&&t.y+t.height>=this.bounds.y,n=t.y+t.height>=i&&t.y<this.bounds.y+this.bounds.height;return t.x<h&&t.x+t.width>=this.bounds.x&&(e&&s.push(0),n&&s.push(2)),t.x+t.width>=h&&t.x<this.bounds.x+this.bounds.width&&(e&&s.push(1),n&&s.push(3)),s},_s(t,s,h){if(this._b=!0,!this.subnodes.length)for(t=this.bounds.width/2|0,s=this.bounds.height/2|0,h=0;h<4;h++)this.subnodes[h]=kontra.quadtree({bounds:{x:this.bounds.x+(h%2==1?t:0),y:this.bounds.y+(h>=2?s:0),width:t,height:s},depth:this._d+1,maxDepth:this.maxDepth,maxObjects:this.maxObjects})}}};
!function(){class t{constructor(t,i){t=t||{},this.spriteSheet=t.spriteSheet,this.frames=t.frames,this.frameRate=t.frameRate,this.loop=void 0===t.loop||t.loop,i=t.spriteSheet.frame,this.width=i.width,this.height=i.height,this.margin=i.margin||0,this._f=0,this._a=0}clone(){return kontra.animation(this)}reset(){this._f=0,this._a=0}update(t){if(this.loop||this._f!=this.frames.length-1)for(t=t||1/60,this._a+=t;this._a*this.frameRate>=1;)this._f=++this._f%this.frames.length,this._a-=1/this.frameRate}render(t){t=t||{};let i=this.frames[this._f]/this.spriteSheet._f|0,e=this.frames[this._f]%this.spriteSheet._f|0;(t.context||kontra.context).drawImage(this.spriteSheet.image,e*this.width+(2*e+1)*this.margin,i*this.height+(2*i+1)*this.margin,this.width,this.height,t.x,t.y,this.width,this.height)}}kontra.animation=function(i){return new t(i)},kontra.animation.prototype=t.prototype;class i{constructor(t){t=t||{},this.animations={},this.image=t.image,this.frame={width:t.frameWidth,height:t.frameHeight,margin:t.frameMargin},this._f=t.image.width/t.frameWidth|0,this.createAnimations(t.animations)}createAnimations(t){let i,e,h,s;for(s in t)e=(i=t[s]).frames,h=[],[].concat(e).map(function(t){h=h.concat(this._p(t))},this),this.animations[s]=kontra.animation({spriteSheet:this,frames:h,frameRate:i.frameRate,loop:i.loop})}_p(t,i){if(+t===t)return t;let e=[],h=t.split(".."),s=i=+h[0],a=+h[1];if(s<a)for(;i<=a;i++)e.push(i);else for(;i>=a;i--)e.push(i);return e}}kontra.spriteSheet=function(t){return new i(t)},kontra.spriteSheet.prototype=i.prototype}();
kontra.tileEngine=function(t){let e=t.width*t.tilewidth,a=t.height*t.tileheight,i=document.createElement("canvas"),s=i.getContext("2d");i.width=e,i.height=a;let r={},n={},h=Object.assign({mapwidth:e,mapheight:a,_sx:0,_sy:0,get sx(){return this._sx},get sy(){return this._sy},set sx(t){this._sx=Math.min(Math.max(0,t),e-kontra.canvas.width)},set sy(t){this._sy=Math.min(Math.max(0,t),a-kontra.canvas.height)},render(){d(i)},renderLayer(t){let i=n[t],s=r[t];i||((i=document.createElement("canvas")).width=e,i.height=a,n[t]=i,h._r(s,i.getContext("2d"))),d(i)},layerCollidesWith(t,e){let a=o(e.y),i=l(e.x),s=o(e.y+e.height),n=l(e.x+e.width),h=r[t];for(let t=a;t<=s;t++)for(let e=i;e<=n;e++)if(h.data[e+t*this.width])return!0;return!1},tileAtLayer(t,e){let a=e.row||o(e.y),i=e.col||l(e.x);return r[t]?r[t].data[i+a*h.width]:-1},_r:function(t,e){e.save(),e.globalAlpha=t.opacity,t.data.forEach((t,a)=>{if(!t)return;let i;for(let e=h.tilesets.length-1;e>=0&&(i=h.tilesets[e],!(t/i.firstgid>=1));e--);let s=i.tilewidth||h.tilewidth,r=i.tileheight||h.tileheight,n=i.margin||0,o=i.image,l=t-i.firstgid,d=i.columns||o.width/(s+n)|0,g=a%h.width*s,c=(a/h.width|0)*r,w=l%d*(s+n),m=(l/d|0)*(r+n);e.drawImage(o,w,m,s,r,g,c,s,r)}),e.restore()}},t);function o(t){return(h.sy+t)/h.tileheight|0}function l(t){return(h.sx+t)/h.tilewidth|0}function d(t){(h.context||kontra.context).drawImage(t,h.sx,h.sy,kontra.canvas.width,kontra.canvas.height,0,0,kontra.canvas.width,kontra.canvas.height)}return h.tilesets.forEach(e=>{let a=(kontra.assets?kontra.assets._d.get(t):"")||window.location.href;if(e.source){let t=kontra.assets.data[kontra.assets._u(e.source,a)];Object.keys(t).forEach(a=>{e[a]=t[a]})}if(""+e.image===e.image){let t=kontra.assets.images[kontra.assets._u(e.image,a)];e.image=t}}),h.layers&&h.layers.forEach(t=>{r[t.name]=t,!1!==t.visible&&h._r(t,s)}),h};
kontra.store={set(t,e){void 0===e?localStorage.removeItem(t):localStorage.setItem(t,JSON.stringify(e))},get(t){let e=localStorage.getItem(t);try{e=JSON.parse(e)}catch(t){}return e}};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment