UNPKG

sundrop

Version:
8 lines (7 loc) 13.2 kB
/** * @license * Copyright 2023 GappleCider * MIT */ !function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((t="undefined"!=typeof globalThis?globalThis:t||self).SUN={})}(this,(function(t){"use strict";class s{constructor(t="",s=(()=>{})){this.url=t,this.texture=new Image,this.texture.src=t,this.onload=s,this.load=!1,this.texture.onload=()=>{this.load=!0,this.onload()}}}function o(t=0,s=0,o=0){return t+(s-t)*o}function i(t=0,s=1){return Math.random()*(s-t)+t}class e{constructor(t=0,s=0){this.x=t,this.y=s}get xPos(){return this.x}get yPos(){return this.y}set xPos(t=0){return this.x=t,this}set yPos(t=0){return this.y=t,this}set(t=0,s=0){return this.x=t,this.y=s,this}setPostion(t=0){return this.x=t,this.y=t,this}clone(){return this.constructor(this.x,this.y)}scale(t=0){return this.x*=t,this.y*=t,this}getPosition(){return{x:this.x,y:this.y}}divide(t=0){return this.x=this.x/t,this.y=this.y/t,this}add(t=0){this.x=this.x+t,this.y=this.y+t}random(t,s){return this.x=i(t,s),this.y=i(t,s),this}}class h{constructor(t=0,s=0,o=0,i=0){this.pos={x:t,y:s},this.width=o,this.height=i,this.props={visible:!0},this.tags={},this.class="polygon",this.type="box",this.polygon=this.createPolygon()}createPolygon(){let t=[];return t.push({x:this.pos.x-this.width/2,y:this.pos.y-this.height/2}),t.push({x:this.pos.x+this.width/2,y:this.pos.y-this.height/2}),t.push({x:this.pos.x+this.width/2,y:this.pos.y+this.height/2}),t.push({x:this.pos.x-this.width/2,y:this.pos.y+this.height/2}),t}update(){this.polygon=this.createPolygon()}}class r{constructor(t=0,s=0,o=0){this.pos={x:t,y:s},this.radius=o,this.type="circle",this.props={visible:!0},this.tags={}}}class l{constructor(t=[]){this.props={visible:!0},this.polygon=t,this.tags={},this.class="polygon",this.points.forEach((t=>{t.push(new e(t[0],t[1])),t.splice(0,2)}))}}var p=Object.freeze({__proto__:null,Box2D:h,Circle2D:r,Line:class{constructor(t=0,s=0,o=0,i=0){this.startPoint=new e(t,s),this.endPoint=new e(o,i),this.props={visible:!0},this.tags={},this.type="line"}},Polygon:l});var n=Object.freeze({__proto__:null,TexturedBox2D:class extends h{constructor(t=0,o=0,i=0,e=0,h=new s){super(t,o,i,e),this.texture=h,this.type="texturedbox"}},TexturedCircle2D:class extends r{constructor(t=0,o=0,i=0,e=new s){super(t,o,i),this.texture=e,this.type="texturedcircle"}},TexturedPolygon:class extends l{constructor(t=[],o=new s){super(t),this.texture=o,this.class="texturedpoly"}}});var c=Object.freeze({__proto__:null,Hexagon2D:class extends h{constructor(t,s,o,i){super(t,s,o,i)}createPolygon(){const t=[],s=this.width-this.width/4,o=this.height-this.height/8;return t.push({x:this.pos.x-s,y:this.pos.y-this.height/2}),t.push({x:this.pos.x,y:this.pos.y-o}),t.push({x:this.pos.x+s,y:this.pos.y-this.height/2}),t.push({x:this.pos.x+s,y:this.pos.y+this.height/2}),t.push({x:this.pos.x,y:this.pos.y+o}),t.push({x:this.pos.x-s,y:this.pos.y+this.height/2}),t}},Pentagon2D:class extends h{constructor(t=0,s=0,o=0,i=0){super(t,s,o,i)}createPolygon(){const t=[],s=this.width-this.width/4;return t.push({x:this.pos.x-s,y:this.pos.y-this.height/2}),t.push({x:this.pos.x,y:this.pos.y-this.height}),t.push({x:this.pos.x+s,y:this.pos.y-this.height/2}),t.push({x:this.pos.x+this.width/2,y:this.pos.y+this.height/2}),t.push({x:this.pos.x-this.width/2,y:this.pos.y+this.height/2}),t}}});t.AdvancedGeometry=c,t.BasicGeometry=p,t.BasicMap=class{constructor(t=[[]],s={},o="1x1"){this.map=t,this.key=s,this.value=o,this.type="map";const i=[];for(let t=0;t<this.map.length;t++){const s=this.map[t];for(let o=0;o<s.length;o++){const e=s[o];for(let s=0;s<e.length;s++){let o=e[s],h=o;if(" "!=o){const e=this.value.split("x"),r=s*Number(e[0]),l=t*Number(e[1]),p=Number(e[0]),n=Number(e[1]),c=()=>{for(let t=0;t<=Object.keys(this.key).length;t++){if(this.key[o]){return this.key[o]}t==Object.keys(this.key).length&&console.error("No key found")}},x=c(),a=this.generatePolygon(r,l,x.width?x.width:p,x.height?x.height:n);o=class{constructor(t,s,o,i,e,h){this.x=t,this.y=s,this.width=o,this.height=i,this.polygon=e,this.props=h}},i.push({pos:{x:r,y:l},type:x.type,class:"box"==x.type?"polygon":null,width:"box"==x.type?x.width?null:Number(e[0]):null,height:"box"==x.type?x.width?null:Number(e[1]):null,radius:"circle"==x.type?x.radius:null,charName:h,width:p,height:n,polygon:a,props:x})}}}}this.map=i}generatePolygon(t,s,o,i){const e=[];return e.push({x:t-o/2,y:s-i/2}),e.push({x:t+o/2,y:s-i/2}),e.push({x:t+o/2,y:s+i/2}),e.push({x:t-o/2,y:s+i/2}),e}},t.BasicRenderer2D=class{constructor(){this.objs=[],this.domElement=document.createElement("canvas"),this.ctx=this.domElement.getContext("2d"),this._canvasWidth=this.domElement.width=window.innerWidth,this._canvasHeight=this.domElement.height=window.innerHeight}render(t=null){this.ctx.clearRect(0,0,this._canvasWidth,this._canvasHeight),t&&(this.ctx.save(),this.ctx.translate(t.pos.x,t.pos.y));for(let t=0;t<this.objs.length;t++){if("map"!=this.objs[t].type&&(null!=this.objs[t].props.color?this.ctx.fillStyle=this.objs[t].props.color:this.ctx.fillStyle="black"),"polygon"===this.objs[t].class){if("texturedbox"===this.objs[t].type&&this.objs[t].texture.load){const s=this.ctx.createPattern(this.objs[t].texture.texture,"repeat");this.ctx.fillStyle=s}if(this.objs[t].props.visible){this.ctx.beginPath(),this.ctx.moveTo(this.objs[t].polygon[0].x,this.objs[t].polygon[0].y);for(let s=0;s<this.objs[t].polygon.length-1;s++)this.ctx.lineTo(this.objs[t].polygon[s+1].x,this.objs[t].polygon[s+1].y);this.ctx.lineTo(this.objs[t].polygon[0].x,this.objs[t].polygon[0].y),this.ctx.closePath(),"fill"===this.objs[t].props.drawStyle?this.ctx.fill():"stroke"===this.objs[t].props.drawStyle&&(null!=this.props.stroke.color?this.ctx.strokeStyle=this.props.stroke.color:this.ctx.strokeStyle="black",null!=this.props.stroke.width?this.ctx.lineWidth=this.props.stroke.width:this.ctx.lineWidth=1,this.ctx.stroke())}}if("line"===this.objs[t].type&&this.objs[t].props.visible&&(null!=this.objs[t].props.stroke.color?this.ctx.strokeStyle=this.objs[t].props.stroke.color:this.ctx.strokeStyle="black",null!=this.objs[t].props.stroke.width?this.ctx.lineWidth=this.objs[t].props.stroke.width:this.ctx.lineWidth=1,this.ctx.beginPath(),this.ctx.moveTo(this.objs[t].startPoint.x,this.objs[t].startPoint.y),this.ctx.lineTo(this.objs[t].endPoint.x,this.objs[t].endPoint.y),this.ctx.closePath(),this.ctx.stroke()),"circle"===this.objs[t].type||"texturedcircle"===this.objs[t].type){if("texturedcircle"===this.objs[t].type&&this.objs[t].texture.load){const s=this.ctx.createPattern(this.objs[t].texture.texture,"repeat");this.ctx.fillStyle=s}this.objs[t].props.visible&&(this.ctx.beginPath(),this.ctx.arc(this.objs[t].pos.x,this.objs[t].pos.y,this.objs[t].radius,0,2*Math.PI),this.ctx.closePath(),"fill"===this.objs[t].props.drawStyle&&this.ctx.fill(),"stroke"===this.objs[t].props.drawStyle&&(null!=this.objs[t].props.stroke.color?this.ctx.strokeStyle=this.objs[t].props.stroke.color:this.ctx.strokeStyle="black",null!=this.objs[t].props.stroke.width?this.ctx.lineWidth=this.objs[t].props.stroke.width:this.ctx.lineWidth=1,this.ctx.stroke()))}if("map"===this.objs[t].type)for(let s=0;s<this.objs[t].map.length;s++){const o=this.objs[t].map[s];if(o.props.visible){if(null!=o.props.color?this.ctx.fillStyle=o.props.color:this.ctx.fillStyle="black","box"==o.props.type){this.ctx.beginPath(),this.ctx.moveTo(o.polygon[0].x,o.polygon[0].y);for(let t=0;t<o.polygon.length-1;t++)this.ctx.lineTo(o.polygon[t+1].x,o.polygon[t+1].y);this.ctx.lineTo(o.polygon[0].x,o.polygon[0].y),this.ctx.closePath(),"fill"===o.props.drawStyle&&this.ctx.fill(),"stroke"===o.props.drawStyle&&(null!=o.props.stroke.color?this.ctx.strokeStyle=o.props.stroke.color:this.ctx.strokeStyle="black",null!=o.props.stroke.width?this.ctx.lineWidth=o.props.stroke.width:this.ctx.lineWidth=1,this.ctx.stroke())}"circle"==o.props.type&&(this.ctx.beginPath(),this.ctx.arc(o.pos.x,o.pos.y,o.props.radius,0,2*Math.PI),this.ctx.closePath(),"fill"===o.props.drawStyle&&this.ctx.fill(),"stroke"===o.props.drawStyle&&(null!=o.props.stroke.color?this.ctx.strokeStyle=o.props.stroke.color:this.ctx.strokeStyle="black",null!=o.props.stroke.width?this.ctx.lineWidth=o.props.stroke.width:this.ctx.lineWidth=1,this.ctx.stroke())),"line"===o.props.type&&(null!=o.props.stroke.color?this.ctx.strokeStyle=o.props.stroke.color:this.ctx.strokeStyle="black",null!=o.props.stroke.width?this.ctx.lineWidth=o.props.stroke.width:this.ctx.lineWidth=1,this.ctx.beginPath(),this.ctx.moveTo(o.props.startPoint.x,o.props.startPoint.y),this.ctx.lineTo(o.props.endPoint.x,o.props.endPoint.y),this.ctx.closePath(),this.ctx.stroke()),"text"===o.props.type&&(null!=o.props.color?this.ctx.fillStyle=o.props.color:this.ctx.fillStyle="black",null!=o.props.font?this.ctx.font=o.props.font.size+"px"+o.props.font.fontFamily:this.ctx.font="48px Arial",this.ctx.fillText(o.props.text.toString(),o.pos.x,o.pos.y))}}"text"===this.objs[t].type&&(null!=this.objs[t].props.font?this.ctx.font=this.objs[t].props.font.size+"px"+this.objs[t].props.font.fontFamily:this.ctx.font="48px Arial",this.ctx.fillText(this.objs[t].text.toString(),this.objs[t].pos.x,this.objs[t].pos.y))}t&&this.ctx.restore()}saveImage(t="",s=0,o=0,i=""){const e=new Image;e.src=t,e.onload=()=>{const t=this.ctx.getImageData(s,o,e.width,e.height);localStorage.setItem(i,t)}}loadImage(t){const s=localStorage.getItem(t);return s||console.error("Image not found: "+t)}add(t=Object){this.objs.push(t)}remove(t=Object){this.objs.splice(this.objs.indexOf(t),1)}clear(){this.objs=[],this.ctx.clearRect(0,0,this._canvasWidth,this._canvasHeight)}get(t=Object){return this.objs.indexOf(t)}update(){for(let t=0;t<this.objs.length;t++)if("box"!==this.objs[t].type&&"texturedbox"!==this.objs[t].type||this.objs[t].update(),"map"==this.objs[t].type)for(let s=0;s<this.objs[t].map.length;s++){const o=this.objs[t].map[s];for(let s=0;s<Object.keys(o.props).length;s++)"function"==typeof o.props[Object.keys(o.props)[s]]&&("collision"==o.props[Object.keys(o.props)[s]].name?o.props[Object.keys(o.props)[s]](o,this.objs[t].map):o.props[Object.keys(o.props)[s]](o))}}createGradient(t="",s=[],o=0,i=0){if("linear"===t){const t=this.ctx.createLinearGradient(0,0,o,i);for(let o=0;o<s.length;o++){const i=s[o];t.addColorStop(o/s.length,i)}return t}}createRGBAColor(t=0,s=0,o=0,i=1){return"rgba("+t+","+s+","+o+","+i+")"}createHexColor(t=""){let s=0;for(let o=0;o<t.length;o++)s=t.charCodeAt(o)+((s<<5)-s);let o="#";for(var i=0;i<3;i++){o+=("00"+(s>>8*i&255).toString(16)).substr(-2)}return o}convertHexToRGBA(t=""){let s;if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(t))return s=t.substring(1).split(""),3==s.length&&(s=[s[0],s[0],s[1],s[1],s[2],s[2]]),s="0x"+s.join(""),"rgba("+[s>>16&255,s>>8&255,255&s].join(",")+",1)"}setSize(t=0,s=0){this._canvasWidth=this.domElement.width=t,this._canvasHeight=this.domElement.height=s}setColor(t=""){this.domElement.style.backgroundColor=t}setAlpha(t=1){this.domElement.style.opacity=t}},t.PerspectiveCamera2D=class{constructor(t=0,s=0){this.pos={x:t,y:s}}setPos(t=0,s=0){this.pos={x:t,y:s}}scale(t=0){this.pos.x*=t,this.pos.y*=t}},t.Sound=class{constructor(t){this.sound=document.createElement("audio"),this.sound.src=t,this.sound.setAttribute("preload","auto"),this.sound.setAttribute("controls","none"),this.sound.style.display="none",document.body.appendChild(this.sound)}play(){this.sound.play()}stop(){this.sound.pause()}},t.TextObject=class{constructor(t="",s=0,o=0){this.text=t,this.pos={x:s,y:o},this.props={},this.type="text"}},t.Texture2D=s,t.TexturedGeometry=n,t.Vec2=e,t.addKeyListeners=function(t=""){document.addEventListener("keydown",(s=>{if(s.key==t)return!0}))},t.checkCollision=function(t=Object,s=Object){if("circle"===t.type&&"circle"===s.type){const i=t.radius+s.radius,e=t.pos.x-s.pos.x,h=t.pos.y-s.pos.y;return i>Math.sqrt(e*e+h*h)}if("polygon"===t.class&&"polygon"===s.class){function r(t,s,i,e){const h=(e.x-i.x)*(t.y-i.y)-(e.y-i.y)*(t.x-i.x),r=(i.y-t.y)*(t.x-s.x)-(i.x-t.x)*(t.y-s.y),l=(e.y-i.y)*(s.x-t.x)-(e.x-i.x)*(s.y-t.y);if(0!=l){const i=h/l,e=r/l;if(i>=0&&i<=1&&e>=0&&e<=1)return{x:o(t.x,s.x,i),y:o(t.y,s.y,i),offset:i}}return null}for(let l=0;l<t.polyon;l++)for(let p=0;p<s.polyon;p++){if(r(t.points[l],s.points[p],t.points[(l+1)%t.polyon],s.points[(p+1)%s.polyon]))return!0}return!1}if("polygon"===t.class&&"circle"===s.type){const n=Math.abs(s.pos.x-t.pos.x),c=Math.abs(s.pos.y-t.pos.y);if(n>t.width/2+s.radius||c>t.height/2+s.radius)return!1;if(n<=t.width/2||c<=t.height/2)return!0;return(n-t.width/2^c-t.height+2^2)<=(2^s.radius)}if("circle"===t.type&&"polygon"===s.class){const x=Math.abs(t.pos.x-s.pos.x),a=Math.abs(t.pos.y-s.pos.y);if(x>s.width/2+t.radius||a>s.height/2+t.radius)return!1;if(x<=s.width/2||a<=s.height/2)return!0;return(x-s.width^a-s.height+2^2)<=(2^t.radius)}return console.error("shapes do not have a proper type or class")},t.lerp=o,t.randomFloat=i,t.randomInt=function(t=0,s=1){return Math.floor(Math.random()*(s-t))+t}})); //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3VuZHJvcC5taW4uanMiLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OzsifQ==