-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.min.js
More file actions
1 lines (1 loc) · 7.61 KB
/
script.min.js
File metadata and controls
1 lines (1 loc) · 7.61 KB
1
let CanvasModule,Canvas;try{Canvas=(CanvasModule=require("canvas")).Canvas}catch(t){}function __loadImage(t,e,s){let i=new Image(e,s);const h=new Promise(t=>i.onload=(()=>t(i)));return i.src=t,h}class Event{constructor(t,e){this._name=t,this._source=e,this._cancelled=!1}isCancelled(){return this._cancelled}setCancelled(t=!0){return this._cancelled=t,this}call(){this._source.events[this._name]||(this._source.events[this._name]=[]),this._source.events[this._name].forEach(t=>t(this))}}let __uuid=0;class Vector2{constructor(t=0,e=0){this.setComponents(t,e)}setComponents(t,e){return this.x=t,this.y=e,this}distance(t){return Math.sqrt(Math.pow(t.x-this.x,2)+Math.pow(t.y-this.y,2))}add(t,e){return t instanceof Vector2?this.add(t.x,t.y):(this.x+=t,this.y+=e,this)}multiply(t,e){return t instanceof Vector2?this.multiply(t.x,t.y):(this.x*=t,this.y*=e,this)}divide(t,e){return this.multiply(1/t,1/e)}subtract(t,e){return this.add(-t,-e)}new(t,e){return new Vector2(t,e)}clone(){return this.new(this.x,this.y)}equals(t){return t.x===this.x&&t.y===this.y}}class _Model{constructor(t,e){this.width=t,this.height=e}render(t,e,s){}}class _ShapeModel extends _Model{setColor(t){return this.color=t,this}}class ImageModel extends _Model{setURL(t){return this.url=t,CanvasModule?CanvasModule.loadImage(t).then(t=>this.setImage(t)):__loadImage(t,this.width,this.height).then(t=>this.setImage(t)),this}setImage(t){return this.image=t,this}render(t,e,s){s.rotate(t.angle||0,e.x,e.y,this.width,this.height);const i=()=>{s.ctx.drawImage(this.image,e.x,e.y,this.width,this.height),s.ctx.setTransform(1,0,0,1,0,0)};if(!this.image&&this.url){console.info("Image couldn't loaded so loading it again... (Image: "+this.url+")");const{url:t}=this;CanvasModule?CanvasModule.loadImage(t).then(t=>{this.setImage(t),i()}):__loadImage(t,this.width,this.height).then(t=>{this.setImage(t),i()})}else i()}}class SquareModel extends _ShapeModel{render(t,e,s){s.rotate(t.angle||0,e.x,e.y,this.width,this.height);const i=new Path2D;s.ctx.fillStyle=this.color||"#000000",i.rect(e.x,e.y,this.width,this.height),s.ctx.fill(i),s.ctx.setTransform(1,0,0,1,0,0)}}class CircleModel extends _ShapeModel{constructor(t){super(0,0),this.width=t,this.height=t}render(t,e,s){s.ctx.fillStyle=this.color||"#000000";const i=new Path2D;i.arc(e.x+this.width/2,e.y+this.width/2,this.width/2,0,2*Math.PI),s.ctx.fill(i),t.collides=(e=>e.distance(t)<=this.width)}}class TextModel extends _Model{constructor(){super(0,0)}updateWidth(){const t=document.createElement("canvas"),e=t.getContext("2d");this.pixels&&(e.font=this.pixels+"px "+this.font);const s=e.measureText(this.text||"");t.remove(),this.width=s.width,this.height=this.pixels}setText(t){return this.text=t,this.updateWidth(),this}setPixels(t){return this.pixels=t,this.updateWidth(),this}setFont(t){return this.font=t,this.updateWidth(),this}setColor(t){return this.color=t,this}setTextAlign(t){return this.align=t,this}setMaxWidth(t){return this.maxWidth=t,this}render(t,e,s){s.rotate(t.angle||0,e.x,e.y,this.width,this.height),s.ctx.font=this.pixels+"px "+this.font,s.ctx.fillStyle=this.color||"#000000",this.align&&(s.ctx.textAlign=this.align),this.text.split("\n").forEach((t,i)=>{s.ctx.fillText(t,e.x,e.y+i*this.pixels,this.maxWidth)}),s.ctx.setTransform(1,0,0,1,0,0)}}class CustomModel extends _Model{constructor(t,e,s){super(t,e),this.renderCallback=s}render(t,e,s){this.renderCallback(t,e,s)}}class EntityData{constructor(t){this._data=t||{x:0,y:0,motion:new Vector2(0,0),angle:0,model:null}}setX(t){return this._data.x=t,this}setY(t){return this._data.y=t,this}setMotion(t){return this._data.motion=t,this}setAngle(t){return this._data.angle=t,this}setModel(t){return this._data.model=t,this}}class Entity extends Vector2{constructor(t){if(t instanceof EntityData||(t=new EntityData(t)),!t._data.model)throw new Error("Entities should have valid model!");super(t._data.x,t._data.y),this._data=t._data,this.events={},this.uuid=__uuid++,this.motion=t._data.motion||new Vector2(0,0),this.angle=t._data.angle||0,this.closed=!0,this.model=t._data.model}on(t,e){return this.events[t]||(this.events[t]=[]),this.events[t].push(e),this}lookAt(t){let e=t.x-this.x,s=t.y-this.y;return this.angle=Math.atan2(s,e)/Math.PI*180,this.angle<0&&(this.angle+=360),this}getDirection(){const t=t=>t*Math.PI/180;return new Vector2(-Math.cos(t(this.angle-90)-Math.PI/2),-Math.sin(t(this.angle-90)-Math.PI/2))}collides(t){return t instanceof Entity?t.x+t.model.width-1>=this.x&&t.x<=this.x+this.model.width-1&&t.y<=this.y+this.model.height-1&&t.y+t.model.height-1>=this.y:t.x>=this.x&&t.x<=this.x+this.model.width&&t.y<=this.y+this.model.height&&t.y>=this.y}getCollidingEntities(t,e=null){const s=[];return Array.from(t.entities).map(t=>t[0]).filter(t=>!t.closed).filter(t=>t.uuid!==this.uuid).filter(t=>!e||e.some(e=>t instanceof e)).forEach(t=>{t.collides(this)&&s.push(t)}),s}getNearBorder(){return[this.collides(new Vector2(0,this.y)),this.collides(new Vector2(this.x,0)),this.collides(new Vector2(scene.canvas.width-1,this.y)),this.collides(new Vector2(this.x,scene.canvas.height-1))].filter(t=>t).map((t,e)=>e)[0]}isNearToBorder(){return!!this.getNearBorder()}preventBorder(t){let e=!1;return this.x<0&&(this.x=0,e=!0),this.y<0&&(this.y=0,e=!0),this.x+this.model.width>t.canvas.width&&(this.x=t.canvas.width-this.model.width,e=!0),this.y+this.model.height>t.canvas.height&&(this.y=t.canvas.height-this.model.height,e=!0),e}move(t,e){let s=new Event("onMove",this);s.entity=this,s.from=this.clone(),s.to=this.clone().add(t,e),s.from.equals(s.to)||(s.call(),s.isCancelled()||(this.x=s.to.x,this.y=s.to.y))}onUpdate(t){this.motion.x>0&&this.motion.x<1e-5&&(this.motion.x=0),this.motion.y>0&&this.motion.y<1e-5&&(this.motion.y=0);const e=this.motion.x/10,s=this.motion.y/10;return this.move(e,s),this.motion.x-=e,this.motion.y-=s,!1}setMotion(t=0,e=0){return t instanceof Vector2?this.motion=t:this.setMotion(new Vector2(t,e)),this}addMotion(t=0,e=0){return this.motion.add(t,e),this}close(){return this.closed=!0,this}cloneEntity(t,e=null){const s=new t(this._data),{...i}=this;return Array.from(Object.entries(i)).forEach(t=>s[t[0]]=t[1]),e&&e.addEntity(s),s}}class Scene{constructor(t){this.running=!0,this.canvas=t,this.ctx=this.canvas.getContext("2d"),this.entities=new Map,this._fps=0,this.fps=0,this.ticks=0,this.events={},this.max_fps=200,this.camera=new Vector2,setInterval(()=>this._fps++),setInterval(()=>{this.fps=this._fps,this._fps=0},1e3);const e=()=>{this.running&&this.onTick(this.ticks++),setTimeout(e,1e3/(this.max_fps>1e3?1e3:this.max_fps))};e()}on(t,e){return this.events[t]||(this.events[t]=[]),this.events[t].push(e),this}setRunning(t=!0){const e=new Event("onSetRunning",this);return e.value=t,e.call(),e.isCancelled()?this:(this.running=t,this)}rotate(t,e,s,i,h){return this.ctx.translate(e+i/2,s+h/2),this.ctx.rotate(t*Math.PI/180),this.ctx.translate(-(e+i/2),-(s+h/2)),this}addEntity(t,e=9){return t.closed=!1,this.entities.set(t,e),this}removeEntity(t){return t.closed=!1,this.entities.delete(t),this}setEntityPriority(t,e){return this.entities.has(t)?(this.entities.set(t,e),this):this}onTick(t){const e=new Event("onTickStart",this);return e.call(),e.isCancelled()?this:(this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),Array.from(this.entities).sort((t,e)=>t[1]>e[1]?-1:t[1]===e[1]?0:1).map(t=>t[0]).filter(t=>!t.closed).forEach(e=>{e.onUpdate(t),e.model.render(e,e.clone().subtract(this.camera.x,this.camera.y),this)}),new Event("onTickEnd",this).call(),this)}}try{module&&(module.exports={Event:Event,Vector2:Vector2,_Model:_Model,_ShapeModel:_ShapeModel,ImageModel:ImageModel,SquareModel:SquareModel,CircleModel:CircleModel,TextModel:TextModel,CustomModel:CustomModel,EntityData:EntityData,Entity:Entity,Scene:Scene,__loadImage:__loadImage})}catch(t){}