Free javascript Hosting


enjine-min.js

Uploaded on Apr 25 2022 16:12 by scaphoide

/*
_____ _ _
| ____| _ __ | |(_) _ __ ___
| _| | '_ \ _ | || || '_ \ / _ \
| |___ | | | || |_| || || | | || __/
|_____||_| |_| \___/ |_||_| |_| \___|

made with ❤️ by Kenneth Gibson
*/

var Keys={backspace:8,tab:9,enter:13,shift:16,ctrl:17,alt:18,pause:19,capsLock:20,escape:27,pageUp:33,pageDown:34,end:35,home:36,leftArrow:37,upArrow:38,rightArrow:39,downArrow:40,insert:45,delete:46,number0:48,number1:49,number2:50,number3:51,number4:52,number5:53,number6:54,number7:55,number8:56,number9:57,a:65,b:66,c:67,d:68,e:69,f:70,g:71,h:72,i:73,j:74,k:75,l:76,m:77,n:78,o:79,p:80,q:81,r:82,s:83,t:84,u:85,v:86,w:87,x:88,y:89,z:90,leftWindowKey:91,rightWindowKey:92,selectKey:93,numpad0:96,numpad1:97,numpad2:98,numpad3:99,numpad4:100,numpad5:101,numpad6:102,numpad7:103,numpad8:104,numpad9:105,multiply:106,add:107,subtract:109,decimalPoint:110,divide:111,f1:112,f2:113,f3:114,f4:115,f5:116,f6:117,f7:118,f8:119,f9:120,f10:121,f11:122,f12:123,numLock:144,scrollLock:145,semicolon:186,equal:187,comma:188,dash:189,period:190,forwardSlash:191,graveAccent:192,openBracket:219,backslash:220,closeBraket:221,singleQuote:222};var degToRad=Math.PI/180,radToDeg=180/Math.PI;class vec{x;y;constructor(t=0,r=0){this.x=t,this.y=r}translate(t){return this.x+=t.x,this.y+=t.y,this}add(t){var{x:r,y:h}=t;return new vec(this.x+r,this.y+h)}translate(t){var{x:r,y:h}=t;this.x+=r,this.y+=h}dotProduct(t){var{x:r,y:h}=t;return new vec(this.x*r,this.y*h)}fromPolar(t,r,h=1){this.x=Math.cos(degToRad*r)*h+t.x,this.y=Math.sin(degToRad*r)*h+t.y}normalize(){var t=this.length();return t=0==t?1:t,new vec(this.x/t,this.y/t)}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}rotate(t){var r=this.x*Math.cos(t*degToRad)-this.y*Math.sin(t*degToRad),h=this.x*Math.sin(t*degToRad)+this.y*Math.cos(t*degToRad);return new vec(r,h)}rotateToward(t){var{x:r,y:h}=t;return this.rotate((-Math.atan(h/r)-(r>=this.x?0:Math.PI))*radToDeg)}invert(){return new vec(-this.x,-this.y)}multiply(t){return new vec(this.x*t,this.y*t)}lerp(t,r){var{x:h,y:e}=t;return new vec(this.x*(1-r)+h*r,this.y*(1-r)+e*r)}toString(){return`{x: ${Math.round(100*this.x)/100}; y: ${Math.round(100*this.y)/100}}`}}function lerp(t,r,h){return t*(1-h)+r*h}function LoadContent(n){if(null!=n){for(const[e,o]of Object.entries(n)){var t=new Image;t.src=o,n[e]=t}Content=n}}class Animation{spriteSheet=void 0;spriteSize=new vec;animation=0;frame=0;frameCount=0;animationSpeed=1;mirror=new vec(1,1);constructor(i){if(null!=i){var{spriteSheet:t,spriteSize:s,animation:o,animationSpeed:e,frameCount:n,frame:h}=i;this.spriteSheet=t??this.spriteSheet,this.spriteSize=s??this.spriteSize,this.animation=o??this.animation,this.frameCount=n??this.frameCount,this.animationSpeed=e??this.animationSpeed,this.frame=h??this.frame}}timer=0;Update(){this.frame==this.frameCount-1?(this.timer=0,this.frame=0):(this.timer+=this.animationSpeed,this.frame=Math.floor(this.timer))}}class Physics{velocity=new vec;momentum=new vec;slippery=.5;Update(){}}class Transform{position=new vec;rotation=0;scale=new vec;depth=0;pivot=new vec}class Camera{position=new vec;resolution=new vec;aspect=1;UpdateAspectRatio(){this.aspect=this.resolution.x/this.resolution.y}ScreenPosition(i){return i.add(this.position.invert())}}class Collider{offset=new vec;scale=new vec(32,32);position=new vec;color="#00000077";constructor(i){if(null!=i){var{offset:t,scale:s}=i;this.offset=t??this.offset,this.scale=s??this.scale}}IsOverlapping(i){var{position:t,scale:s}=i.collider;return t.x<this.position.x+this.scale.x&&t.x+s.x>this.position.x&&t.y<this.position.y+this.scale.y&&t.y+s.y>this.position.y}}class GameObject{animation=new Animation;transform=new Transform;collider=new Collider;physics=new Physics;Left=0;Right=0;Top=0;Bottom=0;collisionOffset=10;_colisionState=!1;constructor(){}OnCollide(i){var t=this.IsCollideLeft(i)||this.IsCollideTop(i)||this.IsCollideRight(i)||this.IsCollideBottom(i);return t&&!this._colisionState?(this._colisionState=!0,this._colisionState):(t||(this._colisionState=!1),!1)}IsCollideLeft(i){var t=this.physics.velocity;return this.Right+t.x>i.Left&&this.Left<i.Left&&this.Bottom-this.collisionOffset>i.Top+this.collisionOffset&&this.Top+this.collisionOffset<i.Bottom-this.collisionOffset}IsCollideRight(i){var t=this.physics.velocity;return this.Left+t.x<i.Right&&this.Right>i.Right&&this.Bottom-this.collisionOffset>i.Top+this.collisionOffset&&this.Top+this.collisionOffset<i.Bottom-this.collisionOffset}IsCollideTop(i){var t=this.physics.velocity;return this.Bottom+t.y>i.Top&&this.Top<i.Top&&this.Left+this.collisionOffset<i.Right-this.collisionOffset&&this.Right-this.collisionOffset>i.Left+this.collisionOffset}IsCollideBottom(i){var t=this.physics.velocity;return this.Top+t.y<i.Bottom&&this.Bottom>i.Bottom&&this.Left+this.collisionOffset<i.Right-this.collisionOffset&&this.Right-this.collisionOffset>i.Left+this.collisionOffset}Draw(){var i=MainCamera.ScreenPosition(this.transform.position);if(!(i.x+this.transform.scale.x<0&&i.y+this.transform.scale.y<0&&i.x>MainCamera.resolution.x&&i.y>MainCamera.resolution.y)&&null!=this.animation.spriteSheet){context.save(),context.translate(i.x+this.transform.pivot.x,i.y+this.transform.pivot.y),context.rotate(this.transform.rotation*degToRad),context.scale(this.animation.mirror.x,this.animation.mirror.y),context.translate(-i.x-this.transform.pivot.x,-i.y-this.transform.pivot.y),context.drawImage(this.animation.spriteSheet,this.animation.frame*this.animation.spriteSize.x,this.animation.animation*this.animation.spriteSize.y,this.animation.spriteSize.x,this.animation.spriteSize.y,i.x,i.y,this.transform.scale.x,this.transform.scale.y),context.restore();var t=MainCamera.ScreenPosition(this.collider.position);GameSettings.ShowColliders&&(context.fillStyle=this.collider.color,context.fillRect(t.x,t.y,this.collider.scale.x,this.collider.scale.y),context.fillStyle="#00f6",context.fillRect(t.x+this.physics.velocity.x,t.y+this.physics.velocity.y,this.collider.scale.x,this.collider.scale.y)),(this.animation.frameCount>0||this.animationSpeed>0)&&this.animation.Update(),this.physics.momentum=this.physics.momentum.lerp(this.physics.velocity,this.physics.slippery),this.physics.Update(),this.transform.position.translate(this.physics.momentum)}}Update(){this.Left=this.collider.position.x,this.Right=this.collider.position.x+this.collider.scale.x,this.Top=this.collider.position.y,this.Bottom=this.collider.position.y+this.collider.scale.y,this.collider.position=this.transform.position.add(this.collider.offset)}}var canvas,context;const startEvent=new Event("start"),updateEvent=new Event("update"),drawEvent=new Event("draw");var MainCamera=new Camera;document.body.addEventListener("keydown",(e=>{Input.Keyboard.keys[e.keyCode]=!0})),document.body.addEventListener("keyup",(e=>{Input.Keyboard.keys[e.keyCode]=!1}));var Events=[],GameSettings={EnableSmoothing:!1,canvas:"#canvas",ClearColor:"#8dcaff",ShowColliders:!1},GameState={ContentLoaded:!1},Input={Keyboard:{keys:[],IsKeyDown:e=>Input.Keyboard.keys[e]},Mouse:{m0:!1,m1:!1,m2:!1,position:new vec}},Content={},Time={deltaTime:0,elapsed:0,lag:0,fps:0};function EventHandler(e,t){Events.push({event:e,func:t})}window.onload=()=>{canvas=document.querySelector(GameSettings.canvas),context=canvas.getContext("2d"),canvas.width=window.innerWidth,canvas.height=window.innerHeight,MainCamera.resolution=new vec(canvas.width,canvas.height),MainCamera.UpdateAspectRatio(),Events.forEach((e=>{canvas.addEventListener(e.event,e.func)})),canvas.dispatchEvent(startEvent),context.webkitImageSmoothingEnabled=GameSettings.EnableSmoothing,context.mozImageSmoothingEnabled=GameSettings.EnableSmoothing,context.imageSmoothingEnabled=GameSettings.EnableSmoothing,gameLoop()},document.body.addEventListener("mousemove",(e=>{Input.Mouse.position=new vec(e.clientX,e.clientY)}));let fps=60,start=Date.now(),frameDuration=1e3/fps,lag=0;function gameLoop(){requestAnimationFrame(gameLoop,canvas);var e=Date.now(),t=e-start;for(start=e,lag+=t;lag>=frameDuration;)lag-=frameDuration,canvas.dispatchEvent(updateEvent);context.fillStyle=GameSettings.ClearColor,context.fillRect(0,0,canvas.width,canvas.height),canvas.dispatchEvent(drawEvent),Time.deltaTime=lag/frameDuration,Time.lag=Math.floor(lag),Time.fps=Math.floor(1e3/t),Time.elapsed=t}

Back to list