-
Notifications
You must be signed in to change notification settings - Fork 1
/
jscf.js
11 lines (11 loc) · 54.9 KB
/
jscf.js
1
2
3
4
5
6
7
8
9
10
11
function AABB(t,e,i,n){this.setTransform=function(t){this.pos=t.pos,this.reset()},this.containsPoint=function(t,e){return t>this.xMin&&t<this.xMax&&e<this.yMax&&e>this.yMin},this.isColliding=function(t){return this.xMin<t.xMax&&this.xMax>t.xMin&&this.yMin<t.yMax&&this.yMax>t.yMin},this.getPenetration=function(t){var e=this.pos.clone();e.subVector(t.pos);var i=Math.abs(Math.abs(e.x)-(this.dims.x+t.dims.x)/2),n=Math.abs(Math.abs(e.y)-(this.dims.y+t.dims.y)/2);return i<n?new Vector2d(e.x>0?i:-i,0):new Vector2d(0,e.y>0?n:-n)},this.getNormal=function(t){var e=this.getPenetration(t);return e.normalize(),e},this.reset=function(){this.xMin=this.pos.x-this.dims.x/2,this.yMin=this.pos.y-this.dims.y/2,this.xMax=this.xMin+this.dims.x,this.yMax=this.yMin+this.dims.y},this.init=function(t,e,i,n){this.pos=new Vector2d(t,e),this.dims=new Vector2d(i,n),this.type="aabb",this.reset()},this.init(t,e,i,n)}function CircleCollider(t,e,i){this.setTransform=function(t){this.pos=t.pos},this.containsPoint=function(t,e){var i=new Point2d(this.pos.x,this.pos.y),n=new Point2d(t,e);return i.distanceTo(n)<=this.R},this.isColliding=function(t){return Vector.subVector(this.pos-t.pos).length()<=this.R+t.R},this.getPenetration=function(t){var e=this.pos.clone();e.subVector(t.pos);var i=Math.abs(Math.abs(e.x)-(this.R+t.R)/2),n=Math.abs(Math.abs(e.y)-(this.R+t.R)/2);return new Vector2d(e.x>0?i:-i,e.y>0?n:-n)},this.getNormal=function(t){var e=this.getPenetration(t);return e.normalize(),e},this.init=function(t,e,i){this.pos=new Vector2d(t,e),this.R=i,this.type="circle",this.reset()},this.init(t,e,i)}const __BUTTON_HANDLER_NAME="[builtin_button_handler]",__BUTTON_HANDLER_HOVER_SPEED=1.1,__BUTTON_HANDLER_HOVER_MAX=1.5,__BUTTON_HANDLER_ESCAPE_BUTTON=17;function ButtonHandler(t,e){this.name=__BUTTON_HANDLER_NAME,this.parent=t,this.hover_speed=e||1.1,this.pressed=!1,this.setDimentions=function(t,e){t>0&&(this.bb.dims.x=t),e>0&&(this.bb.dims.y=e),this.bb.reset()},this.update=function(){if(ButtonHandler.active){var e=t.getGlobalTransform();this.bb.setTransform(e);var i=t.game.inputManager.getMouseX(),n=t.game.inputManager.getMouseY(),s=t.game.inputManager.isMouseDown(),o=t.game.inputManager.isKeyDown(17);this.bb.containsPoint(i,n)&&t.game.guiManager.focus()?(e.scale.length()<1.5&&t.transform.scale.scalarMul(this.hover_speed),s&&(this.pressed=!0)):t.transform.scale=new Vector2d(1,1),this.pressed&&!s&&(this.pressed=!1,o||this.onClick())}},this.onClick=function(){console.log("[JSCF][ButtonHandler] button press")},this.init=function(){var e=t.getDimentions(),i=t.getGlobalTransform();this.bb=new AABB(i,i,e.x,e.y)},this.init()}ButtonHandler.component_name=__BUTTON_HANDLER_NAME,ButtonHandler.active=!0;const __COLLIDER_NAME="[builtin_collider]";var Collider=function(t,e,i){this.getNormal=function(t){return this.resolver.getNormal(t.resolver)},this.update=function(){for(entityName in this.resolver.setTransform(this.parent.getGlobalTransform()),this.others=[],null==this.potential_entities&&(this.potential_entities=this.parent.game.getCurrentScene().entities),this.potential_entities)if(this.parent.name!=entityName&&this.potential_entities.hasOwnProperty(entityName)){var t=this.potential_entities[entityName];if(!t)continue;var e=t.getChild(this.name);e&&this.resolver.isColliding(e.resolver)&&this.others.push(t)}this.potential_entities=null},this.init=function(){if(this.name=__COLLIDER_NAME,this.parent=t,this.normal=new Vector2d(0,0),this.others=[],this.potential_entities=i||null,e)this.resolver=e;else{var n=this.parent.getGlobalTransform().pos,s=this.parent.getDimentions();this.resolver=new AABB(n.x,n.y,s.x,s.y)}},this.init()};Collider.component_name=__COLLIDER_NAME;const __LAYOUT_COMPONENT_NAME="[builtin_layout_handler]";var LayoutHandler=function(t){this.layoutType=Layout,this.name=__LAYOUT_COMPONENT_NAME,this.parent=t,this.init=function(){},this.update=function(){this.layoutType.doLayout(this.parent),this.update=null},this.setDimentions=function(t,e){this.layoutType.doLayout(this.parent)}};LayoutHandler.component_name=__LAYOUT_COMPONENT_NAME;const __RECTANGLE_EDITOR_NAME="[rectangle_editor]",__RECTANGLE_DEFAULT_ACTIVE_STYLE="red",__RECTANGLE_DEFAULT_PASSIVE_STYLE="black",__RECTANGLE_GRAB_BUTTON=17;function RectangleEditor(t,e){this.name="[rectangle_editor]",this.parent=t,this.style=e||"black",this.update=function(){var t=this.parent.getGlobalTransform();this.bb.setTransform(t);var e=this.parent.game.inputManager.getMouseX(),i=this.parent.game.inputManager.getMouseY(),n=new Vector2d(e,i);if(this.parent.game.inputManager.isRMBDown()&&this.isSelected()){var s=Vector.subVector(n,this.parent.transform.pos);s.scalarMul(2),this.parent.setDimentions(s.x,s.y),s=this.parent.getDimentions(),this.bb.dims=s,this.bb.reset(),this.rectangle.width=s.x,this.rectangle.height=s.y}this.bb.containsPoint(e,i)&&this.parent.game.inputManager.isMouseDown()&&this.parent.game.inputManager.isKeyDown(17)&&this.parent.game.inputManager.isLMBDown()&&(this.selectOwner(),this.parent.transform.pos=n)},this.setDimentions=function(t,e){this.bb.dims.x=t,this.bb.dims.y=e,this.bb.reset(),this.rectangle.width=t,this.rectangle.height=e},this.isSelected=function(){return RectangleEditor.currently_selected==this.parent},this.selectOwner=function(){if(RectangleEditor.currently_selected){var t=RectangleEditor.currently_selected.getComponentOfType(RectangleEditor);t&&t.deselectOwner()}RectangleEditor.currently_selected=this.parent,this.style="red",this.rectangle.color=this.style},this.deselectOwner=function(){RectangleEditor.currently_selected=null,this.style="black",this.rectangle.color=this.style},this.render=function(){this.rectangle.render()},this.init=function(){var t=this.parent.getDimentions(),e=this.parent.getGlobalTransform();this.rectangle=new Rectangle(game,t.x,t.y,this.style),this.bb=new AABB(e,e,t.x,t.y)},this.init()}RectangleEditor.currently_selected=null,RectangleEditor.component_name="[rectangle_editor]",RectangleEditor.prototype.toString=function(){return this.isSelected()};const __RIGIDBODY_NAME="[builtin_rigidbody]",__RIGIDBODY_BIAS=1e4,__RIGIDBODY_EPSILON=1e4/Number.MAX_VALUE,__RIGIDBODY_STAIC_MASS=1/__RIGIDBODY_EPSILON;var Rigidbody=function(t){this.setParent=function(t){this.parent=t},this.setStaticBody=function(){this.static=!0,this.auto_gravity=!1,this.mass=__RIGIDBODY_STAIC_MASS},this.update=function(t){this.auto_update&&this.tick_update(t)},this.tick_update=function(t){this.tickDuration=t,this.parent.transform.pos.x+=this.velocity.x*this.tickDuration,this.parent.transform.pos.y+=this.velocity.y*this.tickDuration},this.calcCollision=function(t,e){const i=this.mass,n=t.mass,s=(this.cor+t.cor)/2,o=new Vector2d(-e.y,e.x),r=this.velocity,h=t.velocity;var a=r.dotProduct(e),d=o.dotProduct(r),c=e.dotProduct(h),u=d,l=o.dotProduct(h),f=(a*(i-s*n)+(1+s)*n*c)/(i+n),_=(c*(n-s*i)+(1+s)*i*a)/(i+n),p=new Vector2d(f*e.x,f*e.y),g=new Vector2d(u*o.x,u*o.y),m=new Vector2d(_*e.x,_*e.y),v=new Vector2d(l*o.x,l*o.y),w=new Vector2d(p.x+g.x,p.y+g.y),x=new Vector2d(m.x+v.x,m.y+v.y);this.static||(this.velocity=w),t.static||(t.velocity=x)},this.fixPenetration=function(t,e){e.clone();this.static||this.parent.transform.pos.addVector(e),t.static||t.parent.transform.pos.subVector(e)},this.applyAcceleration=function(t){var e=t.clone();e.scalarMul(this.tickDuration),this.applyVelocity(e)},this.applyVelocity=function(t){this.velocity.addVector(t)},this.init=function(){this.name=__RIGIDBODY_NAME,this.parent=t,this.tickDuration=0,this.displacement=new Vector2d(0,0),this.velocity=new Vector2d(0,0),this.ro=1,this.cor=1,this.mass=this.parent.transform.scale.x*this.parent.transform.scale.y*this.ro,this.auto_gravity=!0,this.auto_update=!1,this.static=!1,this.parent.hasComponentOfType(Collider)||console.error("[jscf/components/rigidbody] parent doesn't have collider."),this.parent.transform||console.error("[jscf/components/rigidbody] parent doesn't have transform! Not an entity?")},this.init()};Rigidbody.component_name=__RIGIDBODY_NAME;const __SCRIPT_COMPONENT_NAME="[builtin_script]";var Script=function(t){this.name=__COMPONENT_NAME,this.parent=t,this.init=function(){},this.update=function(){}};Script.component_name="[builtin_script]";const __COMPONENT_NAME="[builtin_component]";var Component=function(t){this.parent=t,this.name=__COMPONENT_NAME,this.init=function(){},this.update=function(){}};function Entity(t,e,i,n,s,o){this.start_render=function(){var e=t.graphics.context;e.save(),e.translate(this.transform.pos.x,this.transform.pos.y),e.rotate(this.transform.angle),e.scale(this.transform.scale.x,this.transform.scale.y)},this.end_render=function(){t.graphics.context.restore()},this.render=function(){for(var t in this.start_render(),this.children)this.children[t]&&this.children[t].render&&this.children[t].render();this.end_render()},this.update=function(){for(var t in this.children)this.children[t]&&this.children[t].update&&this.children[t].update()},this.hasOwnChild=function(t){return this.children[t]},this.getChild=function(t){var e=this.hasOwnChild(t);if(e)return e;for(var i in this.children)if(this.children.hasOwnProperty(i)&&this.children[i].getChild){var n=this.children[i].getChild(t);if(n)return n}},this.getChildAt=function(t){return Object.values(this.children)[t]},this.getComponent=function(t){return this.getChild("["+t+"]")},this.getBuiltinComponent=function(t){return this.getComponent("builtin_"+t)},this.getComponentOfType=function(t){var e=Component.typeToName(t),i=this.getChild(e);return i||(i=this.getBuiltinComponent(e)),i},this.hasComponentOfType=function(t){var e=Component.typeToName(t);return this.hasOwnChild(e)||this.hasOwnChild("[builtin_"+e+"]")},this.addComponent=function(t){var e=new t(this);this.children[e.name]=e},this.setParent=function(t){this.parent=t},this.clearParent=function(){this.parent=null},this.addChild=function(t,e){e.setParent?e.setParent(this):e.parent&&(e.parent=this),this.children[t]=e},this.insertChild=function(t){t.setParent?t.setParent(this):t.parent&&(t.parent=this),this.children[this.getChildName()]=t},this.delChild=function(t){return t in this.children&&(delete this.children[t],!0)},this.delComponent=function(t){return this.delChild("["+t+"]")},this.delComponentOfType=function(t){var e=this.getComponentOfType(t);return!!e&&this.delChild(e.name)},this.getGlobalTransform=function(){return null==this.parent?this.transform:Transform.add(this.transform,this.parent.getGlobalTransform())},this.getDimentions=function(){var t=new Vector2d(0,0);for(var e in this.children)if(this.children.hasOwnProperty(e)&&this.children[e]){var i=this.children[e];if(i.getDimentions){var n=i.getDimentions();if(n.x=Math.max(n.x,0),n.y=Math.max(n.y,0),i.transform){var s=i.transform.pos.clone();n.x+=Math.abs(s.x),n.y+=Math.abs(s.y)}t.x=Math.max(t.x,n.x),t.y=Math.max(t.y,n.y)}}return t},this.setDimentions=function(t,e){for(var i in this.children)if(this.children.hasOwnProperty(i)&&this.children[i]){var n=this.children[i];n.setDimentions&&n.setDimentions(t,e)}},this.destroy=function(){for(var t in this.children)if(this.children.hasOwnProperty(t)&&this.children[t]){var e=this.children[t];e.destroy&&e.destroy()}},this.getChildName=function(){return this.name+"."+this.max_cid++},this.getEntityChildren=function(){for(var t=Object.values(this.children),e=[],i=0;i<t.length;i++)t[i]instanceof Entity&&e.push(t[i]);return e},this.init=function(){this.name=e,this.children={},this.transform=new Transform(n,s),this.game=t,this.auto_physics=o,this.auto_render=o,this.auto_update=o,this.alive=i,this.max_cid=0,this.parent=null},this.init()}function Game(t,e,i,n){this.init=function(){this.lastDelay=0,this.time=new Time(i),this.renderTime=new Time(i),this.interval=null,this.renderInterval=null,this.graphics=null,this.update=null,this.automated=!0,this.debug=!1,this.state="loading",this.inputManager=null,this.guiManager=null,this.resourceManager=new ResourceManager,this.assetManager=new AssetManager(n),this.sceneManager=new SceneManager(this)},this.init(),this.setup=function(){this.graphics=new Graphics(t,e),this.inputManager=new InputManager(this.graphics.canvas),this.guiManager=new GuiManager(this);var i=this;window.onerror=function(t,e,n,s,o){return i.debug&&i.warn(t+": "+o+"\n "+e),!1}},this.start=function(t,e){"running"!=this.state&&(this.update=t,this.automated=e,this.state="running",this.time.isFixedTime()?this.interval=setInterval(this.handler.bind(this),1e3*this.time.getDeltaTime()):(this.updateLoop(),this.renderLoop()))},this.stop=function(){null!=this.interval&&clearInterval(this.interval),this.renderInterval&&cancelAnimationFrame(this.renderInterval)},this.handler=function(){this.time.update(),this.automated&&(this.sceneManager.update(),this.graphics.clear(),this.sceneManager.render()),this.update&&this.update()},this.updateLoop=function(){this.time.update(),this.automated&&this.sceneManager.update(),this.update&&this.update(),this.automated&&this.guiManager.resetFocus(),this.lastDelay=(this.lastDelay+this.time.getDeltaTime())/2,setTimeout(this.updateLoop.bind(this),this.lastDelay)},this.renderLoop=function(){this.renderTime.update(),this.graphics.clear(),this.sceneManager.render(),this.automated&&(this.renderInterval=requestAnimationFrame(this.renderLoop.bind(this),this.graphics.canvas))},this.getCurrentScene=function(){return this.sceneManager.getCurrentScene()},this.getCanvasWidth=function(){return null!=this.graphics?this.graphics.canvas.width:t},this.getCanvasHeight=function(){return null!=this.graphics?this.graphics.canvas.height:e},this.GetAnimSpeed=function(t){return t*this.time.getDeltaTime()},this.renderText=function(t,e,i,n,s){var o=this.graphics.context;s&&(o.font=s),n&&(o.fillStyle=n),o.fillText(i,t,e)},this.logAndStop=function(t){console.log(t),this.stop()},this.warn=function(t){this.guiManager.errorPopup(t)}}function Time(t){this.getTimeFromStart=function(){return Time.getTime()-this.startTime},this.getDeltaTime=function(){return this.dt},this.update=function(){if(!this.isFixedTime()){var t=Time.getTime();this.dt=(t-this.lastTime)/1e3,this.lastTime=t}},this.isFixedTime=function(){return this.fps&&this.fps>0},this.init=function(){this.startTime=Time.getTime(),this.lastTime=this.startTime,this.fps=t,this.dt=1/this.fps},this.init()}function Transform(t,e,i,n){t=t||0,e=e||0,i=i||1,n=n||1,this.pos=new Vector2d(t,e),this.scale=new Vector2d(i,n),this.angle=0}function AnimFrame(t,e,i,n){this.px=t,this.py=e,this.w=i,this.h=n,this.getAnimationIndex=function(){return this.px/this.w},this.getFrameIndex=function(){return this.py/this.h},this.setAnimationIndex=function(t,e){this.py=(this.py+this.h*t)%e},this.setFrameIndex=function(t,e){this.px=t*this.w%e},this.nextAnimation=function(t){this.py=(this.py+this.h)%t},this.nextFrame=function(t){this.px=(this.px+this.w)%t}}function AnimSprite(t,e,i,n,s,o,r){this.spr=new Sprite(t,e,i,n),this.anim=new Animation(t,this.spr,s,o,r),this.interval=null,this.startAnimation=function(){null==this.interval?this.interval=setInterval(this.anim.nextFrame.bind(this.anim),1/r*1e3):console.warn("JSCF: [startAnimation] animation already started!")},this.stopAnimation=function(){this.interval&&(clearInterval(this.interval),this.interval=null)},this.updateAnim=function(){this.anim.updateFrame()},this.render=function(){this.anim.render()},this.staticRender=function(){this.spr.render()}}function Animation(t,e,i,n,s){this.spr=e,this.frame=new AnimFrame(0,0,i,n),this.frameCounter=0,this.setAnimationIndex=function(t){this.frame.setAnimationIndex(t,this.spr.image.height)},this.setFrameIndex=function(t){this.frame.setFrameIndex(t,this.spr.image.width)},this.nextAnimation=function(){this.frame.nextAnimation(this.spr.image.height)},this.nextFrame=function(){this.frame.nextFrame(this.spr.image.width)},this.updateFrame=function(){this.frameCounter=(this.frameCounter+1)%Math.floor(t.time.getDeltaTime()/s),0==this.frameCounter&&this.nextFrame()},this.render=function(){t.graphics.context.drawImage(this.spr.image,this.frame.px,this.frame.py,this.frame.w,this.frame.h,this.spr.width/-2,this.spr.height/-2,this.spr.width,this.spr.height)}}function Circle(t,e,i){this.radius=e,this.color=i,this.setDimentions=function(t,e){t>0&&(this.radius=t),e>0&&(this.radius=e)},this.getDimentions=function(){return new Vector2d(this.radius,this.radius)},this.render=function(){ctx=t.graphics.context,ctx.fillStyle=this.color,ctx.beginPath(),ctx.arc(0,0,this.radius,0,2*Math.PI),ctx.fill()}}function Effect(t,e){this.pre_render=t,this.post_render=e}Component.component_name=__COMPONENT_NAME,Component.typeToName=function(t){var e=t.component_name;return e||(e=t.name.toLowerCase()),e},Time.getTime=function(){return performance.now()},Transform.add=function(e,i){return t=new Transform,t.pos=Vector.addVector(e.pos,i.pos),t.scale=new Vector2d(e.scale.x*i.scale.x,e.scale.y*i.scale.y),t.angle=e.angle+i.angle,t},Transform.prototype.toString=function(){return this.pos.toString()};var noneFx=new Effect((function(t){}),(function(t){})),shadowFx=new Effect((function(t){t.shadowBlur=10,t.shadowColor="black"}),(function(t){t.shadowBlur=0,t.shadowColor="transparent"}));function ShaderStart(t,e){this.render=function(){e.pre_render(t.graphics.context)}}function ShaderEnd(t,e){this.render=function(){e.post_render(t.graphics.context)}}function Graphics(t,e){this.init=function(){null==document.body&&alert("JSCF: Fatal error!\nCan't initialize graphics before body is loaded!"),this.canvas=document.createElement("canvas"),this.canvas.id="GameCanvas",this.canvas.width=t<0?window.innerWidth:t,this.canvas.height=e<0?window.innerHeight:e,this.context=this.canvas.getContext("2d"),document.body.insertBefore(this.canvas,document.body.childNodes[0])},this.init(),this.clear=function(){this.context.clearRect(0,0,this.canvas.width,this.canvas.height)}}function Plane(t,e,i,n){this.width=e,this.height=i,this.color=n,this.effect=null,this.setDimentions=function(t,e){t>0&&(this.width=t),e>0&&(this.height=e)},this.getDimentions=function(){return new Vector2d(this.width,this.height)},this.render=function(){ctx=t.graphics.context,this.effect&&this.effect.pre_render(ctx),ctx.fillStyle=this.color,ctx.fillRect(this.width/-2,this.height/-2,this.width,this.height),this.effect&&this.effect.post_render(ctx)}}function Rectangle(t,e,i,n){this.width=e,this.height=i,this.color=n,this.setDimentions=function(t,e){t>0&&(this.width=t),e>0&&(this.height=e)},this.getDimentions=function(){return new Vector2d(this.width,this.height)},this.render=function(){ctx=t.graphics.context,ctx.strokeStyle=this.color,ctx.strokeRect(this.width/-2,this.height/-2,this.width,this.height)}}function Sprite(t,e,i,n){this.width=e,this.height=i,this.image=new Image,this.image.src=n,this.render=function(){t.graphics.context.drawImage(this.image,0,0,this.image.width,this.image.height,this.width/-2,this.height/-2,this.width,this.height)},this.setImageSrcFromAsset=function(e){this.image.src=t.assetManager.getAssetPath(e)}}function InputManager(t){var e=[],i=0,n=0,s=!1,o=-1;this.isKeyDownChar=function(t){return e[t.toUpperCase().charCodeAt()]},this.isKeyDown=function(t){return e[t]},this.getMouseX=function(){return i},this.getMouseY=function(){return n},this.isMouseDown=function(){return s},this.isLMBDown=function(){return s&&0==o.button},this.isRMBDown=function(){return s&&2==o.button},this.getMouseEvent=function(){return o},this.setOnMouseUp=function(t){document.addEventListener("mouseup",t)},this.setOnMouseDown=function(t){document.addEventListener("mousedown",t)},this.setOnKeyUp=function(t){document.addEventListener("keyup",t)},this.setOnKeyUpSpec=function(t,e){this.setOnKeyUp((function(i){i.keyCode==t&&e()}))},this.readTextFile=function(t){var e=new XMLHttpRequest;e.open("GET",t,!1),e.onreadystatechange=function(){if(4===e.readyState&&(200===e.status||0==e.status)){var t=e.responseText;alert(t)}},e.send(null)},document.addEventListener("keydown",(function(t){e[t.keyCode]=!0})),document.addEventListener("keyup",(function(t){e[t.keyCode]=!1})),document.addEventListener("mousemove",(function(e){i=e.clientX-t.offsetLeft,n=e.clientY-t.offsetTop})),document.addEventListener("contextmenu",(function(t){t.preventDefault()})),window.addEventListener("dragover",(function(t){(t=t||event).preventDefault()}),!1),window.addEventListener("drop",(function(t){t.preventDefault(),t.stopPropagation();for(var e,i=t.dataTransfer.files,n=0;e=i[n];n++){var s=new FileReader;s.onload=function(t){InputManager.droppedFileCallback(t.target.result)},s.readAsText(e)}}),!1),this.setOnMouseDown((function(t){t.preventDefault(),o=t,s=!0})),this.setOnMouseUp((function(t){t.preventDefault(),o=t,s=!1}))}function Manifold(t,e){this.rigidBody1=t.getBuiltinComponent("rigidbody"),this.rigidBody2=e.getBuiltinComponent("rigidbody"),this.collider1=t.getBuiltinComponent("collider"),this.collider2=e.getBuiltinComponent("collider"),this.valid=this.rigidBody1&&this.rigidBody2&&this.collider1&&this.collider2,this.getPenetration=function(){return this.collider1.resolver.getPenetration(this.collider2.resolver)},this.getNormal=function(){return this.collider1.getNormal(this.collider2)}}function PhysicsEngine(t){this.pixelMeterRatio=50,this.numIterations=7,this.simSpeed=1,this.tickDuration=this.simSpeed*(1/this.numIterations),this.gravity=new Vector2d(0,9.8*this.pixelMeterRatio),void 0===Rigidbody&&(console.warn("[JSCF][PhysicsEngine] Rigidbody component not included! Disabled!"),this.numIterations=0),this.applyNaturalForces=function(t){t.auto_gravity&&!t.static&&t.applyAcceleration(this.gravity)},this.applyCollision=function(t){if(t.valid){var e=t.getNormal();t.rigidBody1.calcCollision(t.rigidBody2,e)}},this.fixPenetration=function(t){if(t.valid){var e=t.getPenetration();t.rigidBody1.fixPenetration(t.rigidBody2,e)}},this.detectCollisions=function(){var e={},i=[];for(entityName in t)if(t.hasOwnProperty(entityName)){var n=t[entityName];if(!n)continue;if(n.auto_physics){var s=n.getComponentOfType(Rigidbody);if(!s)continue;s.tick_update(this.tickDuration);var o=n.getComponentOfType(Collider);if(!o||!o.others)continue;o.update();for(var r=0;r<o.others.length;r++){var h=o.others[r];if(h){if(e[n.name]&&e[n.name][h.name])break;e[h.name]||(e[h.name]={}),e[h.name][n.name]=!0;var a=new Manifold(n,h);i.push(a)}}}}return i},this.resolveNaturalForces=function(){for(entityName in t)if(t.hasOwnProperty(entityName)){var e=t[entityName];if(!e)continue;if(e.auto_physics){var i=e.getComponentOfType(Rigidbody);if(!i)continue;this.applyNaturalForces(i)}}},this.resolveCollisions=function(t){for(var e=0;e<t.length;e++)this.applyCollision(t[e]);for(e=0;e<t.length;e++)this.fixPenetration(t[e])},this.update=function(t){this.tickDuration=this.simSpeed*(t/this.numIterations);for(var e=0;e<this.numIterations;e++){this.resolveNaturalForces();var i=this.detectCollisions();this.resolveCollisions(i)}}}function AssetManager(t){this.rules={},this.getExtention=function(t){return t.substring(t.lastIndexOf("."),t.length)},this.getAssetPath=function(e){return t+"\\"+e},this.getAssetDir=function(){return t},this.getRule=function(t){return this.rules[t]},this.setRule=function(t,e){this.rules[t]=e}}function _clone(t){if(!t)return t;if([Number,String,Boolean].forEach((function(i){t instanceof i&&(e=i(t))})),void 0===e)if("[object Array]"===Object.prototype.toString.call(t))e=[],t.forEach((function(t,i,n){e[i]=clone(t)}));else if("object"==typeof t)if(t.nodeType&&"function"==typeof t.cloneNode)var e=t.cloneNode(!0);else if(t.prototype)e=t;else if(t instanceof Date)e=new Date(t);else for(var i in e={},t)e[i]=_clone(t[i]);else e=t;return e}function ResourceManager(){this.resources={},this.getResourceName=function(t){for(var e=0,i=this.resources.length;e<i;e++)if(t==this.resources.data[e])return this.resources.data[e].name},this.get=function(t){return t&&this.resources[t]?this.resources[t]:(console.warn("[JSCF] resrouce manager get() - got invalid key."),null)},this.getClone=function(t){var e=this.get(t),i=_clone(e);return i.game&&(i.game=e.game),this.add(t+"_clone",i)?i:e},this.add=function(t,e){return t?this.resources[t]?(console.warn("[JSCF] resource manager set() - resource already exists!"),!1):(this.resources[t]=e,!0):(console.warn("[JSCF] resource manager set() - got invalid key."),!1)},this.remove=function(t){return t&&this.resources[t]?(this.resources[t]=null,!0):(console.warn("[JSCF] resrouce manager remove() - got invalid key."),null)},this.removeByValue=function(t){var e=this.getResourceName(t);return!!e&&this.remove(e)}}function Scene(t){this.max_euid=0,this.entities={},this.entities_keys=[],this.paused=!1,this.physicsEngine=new PhysicsEngine(this.entities),this.pause=function(){this.paused=!0},this.resume=function(){this.paused=!1},this.update=function(){if(this.paused)return!1;for(var e=0;e<this.entities_keys.length;++e){var i=this.entities_keys[e],n=this.entities[i];n&&(n.update&&n.auto_update&&n.update())}return this.physicsEngine.update(t.time.getDeltaTime()),!0},this.render=function(){if(this.paused)return!1;for(var t=this.entities_keys.length;t>=0;--t){var e=this.entities_keys[t],i=this.entities[e];i&&i.auto_render&&i.render()}return!0},this.getEntity=function(t){var e=this.entities[t];if(e)return e;for(var i in this.entities)if(this.entities.hasOwnProperty(i)&&this.entities[i].getChild){var n=this.entities[i].getChild(t);if(n)return n}console.warn("[JSCF] scene couldn't find requested object: "+t)},this.addEntity=function(t){return!(t.name in this.entities)&&(this.entities[t.name]=t,this.entities_keys.unshift(t.name),t)},this.delEntity=function(t){return t in this.entities&&(this.entities_keys=__delete_array_element(this.entities_keys,t),this.entities[t].destroy(),delete this.entities[t],!0)},this.createManualEntity=function(e,i,n,s){var o=new Entity(t,e,!0,i,n,!1);return o.addChild(this.getChildName(o,s),s),this.addEntity(o)},this.createEntity=function(e,i,n,s){var o=new Entity(t,e,!0,i,n,!0);return s&&o.addChild(this.getChildName(o,s),s),this.addEntity(o)},this.createNewEntity=function(e){var i=new Entity(t,this.getEntityName(),!0,0,0,!0);return e&&i.addChild(this.getChildName(i,e),e),this.addEntity(i)},this.getChildName=function(t,e){return e.name?e.name:t.getChildName()},this.getEntityName=function(){return this.max_euid++,"entity_"+this.max_euid},this.serialize=function(){this.pause();var t=JSON.stringify(this.entities,__get_circular_replaces());return this.resume(),t},this.deserialize=function(t){this.pause(),this.entities=JSON.parse(t,__func_reviver),this.resume()},this.onFileDrop=function(e){t.debug&&this.deserialize(e)},this.init=function(){InputManager.droppedFileCallback=this.onFileDrop.bind(this)},this.init()}function __delete_array_element(t,e){return t.filter((function(t){return t!==e}))}InputManager.droppedFileCallback=function(){console.log("[JSCF] file dropped!")};const __get_circular_replaces=()=>{const t=new WeakSet;return(e,i)=>{if("function"==typeof i)return i.toString();if("object"==typeof i&&null!==i){if(t.has(i))return;t.add(i)}return i}},__func_reviver=(t,e)=>{if("string"==typeof e&&0===e.indexOf("function ")){return new Function(`(${e}).call(this)`)}return e};function SceneManager(t){this.scenes={splash:new Scene(t)},this.cur_scene=this.scenes.splash,this.update=function(){return this.cur_scene.update()},this.render=function(){return this.cur_scene.render()},this.getCurrentScene=function(){return this.cur_scene},this.setCurrentScene=function(t){return t&&this.scenes[t]?(this.cur_scene=this.scenes[t],!0):(console.warn("[JSCF] tried to change into an invalid scene!"),!1)},this.createScene=function(e){return e?this.scenes[e]?(console.warn("[JSCF] scene "+e+" already exists (scene creation)!"),!1):(this.scenes[e]=new Scene(t),!0):(console.warn("[JSCF] got an invalid scene name in creation!"),!1)},this.deleteScene=function(t){return t?!!this.scenes[t]||(console.warn("[JSCF] scene "+t+" doesn't exists (scene creation)!"),!1):(console.warn("[JSCF] got an invalid scene name in deletion!"),!1)}}var SceneUtils={makeFloor:function(t,e,i,n,s,o){return[]},deleteParent:function(t,e){var i=e.parent;null==i.parent?t.getCurrentScene().delEntity(e.parent.name):i.parent.delChild(i.name)},saveToFile:function(t,e,i){var n=new Blob([t],{type:i});if(window.navigator.msSaveOrOpenBlob)window.navigator.msSaveOrOpenBlob(n,e);else{var s=document.createElement("a"),o=URL.createObjectURL(n);s.href=o,s.download=e,document.body.appendChild(s),s.click(),setTimeout((function(){document.body.removeChild(s),window.URL.revokeObjectURL(o)}),0)}}};function SoundPlayer(t){this.sound=document.createElement("audio"),this.sound.style.display="none",this.sound.src=t,this.sound.setAttribute("preload","auto"),this.sound.setAttribute("controls","none"),document.body.appendChild(this.sound),this.play=function(){this.sound.play()},this.stop=function(){this.sound.pause()}}
/*!
* CanvasInput v1.2.7
* http://goldfirestudios.com/blog/108/CanvasInput-HTML5-Canvas-Text-Input
*
* (c) 2013-2017, James Simpson of GoldFire Studios
* goldfirestudios.com
*
* MIT License
*/
!function(){var t=[];(window.CanvasInput=function(e){var i=this;if(e=e||{},i._canvas=e.canvas||null,i._ctx=i._canvas?i._canvas.getContext("2d"):null,i._x=e.x||0,i._y=e.y||0,i._extraX=e.extraX||0,i._extraY=e.extraY||0,i._fontSize=e.fontSize||14,i._fontFamily=e.fontFamily||"Arial",i._fontColor=e.fontColor||"#000",i._placeHolderColor=e.placeHolderColor||"#bfbebd",i._fontWeight=e.fontWeight||"normal",i._fontStyle=e.fontStyle||"normal",i._fontShadowColor=e.fontShadowColor||"",i._fontShadowBlur=e.fontShadowBlur||0,i._fontShadowOffsetX=e.fontShadowOffsetX||0,i._fontShadowOffsetY=e.fontShadowOffsetY||0,i._readonly=e.readonly||!1,i._maxlength=e.maxlength||null,i._width=e.width||150,i._height=e.height||i._fontSize,i._padding=e.padding>=0?e.padding:5,i._borderWidth=e.borderWidth>=0?e.borderWidth:1,i._borderColor=e.borderColor||"#959595",i._borderRadius=e.borderRadius>=0?e.borderRadius:3,i._backgroundImage=e.backgroundImage||"",i._boxShadow=e.boxShadow||"1px 1px 0px rgba(255, 255, 255, 1)",i._innerShadow=e.innerShadow||"0px 0px 4px rgba(0, 0, 0, 0.4)",i._selectionColor=e.selectionColor||"rgba(179, 212, 253, 0.8)",i._placeHolder=e.placeHolder||"",i._value=(e.value||i._placeHolder)+"",i._onsubmit=e.onsubmit||function(){},i._onkeydown=e.onkeydown||function(){},i._onkeyup=e.onkeyup||function(){},i._onfocus=e.onfocus||function(){},i._onblur=e.onblur||function(){},i._cursor=!1,i._cursorPos=0,i._hasFocus=!1,i._selection=[0,0],i._wasOver=!1,i.manual=!0,i.boxShadow(i._boxShadow,!0),i._calcWH(),i._renderCanvas=document.createElement("canvas"),i._renderCanvas.setAttribute("width",i.outerW),i._renderCanvas.setAttribute("height",i.outerH),i._renderCtx=i._renderCanvas.getContext("2d"),i._shadowCanvas=document.createElement("canvas"),i._shadowCanvas.setAttribute("width",i._width+2*i._padding),i._shadowCanvas.setAttribute("height",i._height+2*i._padding),i._shadowCtx=i._shadowCanvas.getContext("2d"),void 0!==e.backgroundGradient?(i._backgroundColor=i._renderCtx.createLinearGradient(0,0,0,i.outerH),i._backgroundColor.addColorStop(0,e.backgroundGradient[0]),i._backgroundColor.addColorStop(1,e.backgroundGradient[1])):i._backgroundColor=e.backgroundColor||"#fff",i._removals=[],i._canvas)var n=function(t){t=t||window.event,i.mousemove(t,i)},s=function(t){t=t||window.event,i.mousedown(t,i)},o=function(t){t=t||window.event,i.mouseup(t,i)};i._canvas.addEventListener("mousemove",n,!1),i._canvas.addEventListener("mousedown",s,!1),i._canvas.addEventListener("mouseup",o,!1),i._removals.push([i._canvas,"mousemove",n,!1]),i._removals.push([i._canvas,"mousedown",s,!1]),i._removals.push([i._canvas,"mouseup",o,!1]);var r=function(t){t=t||window.event,i._hasFocus&&!i._mouseDown&&i.blur()};window.addEventListener("mouseup",r,!0),window.addEventListener("touchend",r,!0),i._removals.push([window,"mouseup",r,!0]),i._removals.push([window,"touchend",r,!0]),i._hiddenInput=document.createElement("input"),i._hiddenInput.type="text",i._hiddenInput.style.position="absolute",i._hiddenInput.style.opacity=0,i._hiddenInput.style.pointerEvents="none",i._hiddenInput.style.zIndex=0,i._hiddenInput.style.transform="scale(0)",i._updateHiddenInput(),i._maxlength&&(i._hiddenInput.maxLength=i._maxlength),document.body.appendChild(i._hiddenInput),i._hiddenInput.value=i._value,i._hiddenInput.addEventListener("keydown",(function(t){t=t||window.event,i._hasFocus&&(window.focus(),i._hiddenInput.focus(),i.keydown(t,i))})),i._hiddenInput.addEventListener("keyup",(function(t){t=t||window.event,i._value=i._hiddenInput.value,i._cursorPos=i._hiddenInput.selectionStart,i._selection=[i._hiddenInput.selectionStart,i._hiddenInput.selectionEnd],i.render(),i._hasFocus&&i._onkeyup(t,i)})),t.push(i),i._inputsIndex=t.length-1,i.render()}).prototype={canvas:function(t){return void 0!==t?(this._canvas=t,this._ctx=this._canvas.getContext("2d"),this.render()):this._canvas},x:function(t){return void 0!==t?(this._x=t,this._updateHiddenInput(),this.render()):this._x},y:function(t){return void 0!==t?(this._y=t,this._updateHiddenInput(),this.render()):this._y},extraX:function(t){return void 0!==t?(this._extraX=t,this._updateHiddenInput(),this.render()):this._extraX},extraY:function(t){return void 0!==t?(this._extraY=t,this._updateHiddenInput(),this.render()):this._extraY},fontSize:function(t){return void 0!==t?(this._fontSize=t,this.render()):this._fontSize},fontFamily:function(t){return void 0!==t?(this._fontFamily=t,this.render()):this._fontFamily},fontColor:function(t){return void 0!==t?(this._fontColor=t,this.render()):this._fontColor},placeHolderColor:function(t){return void 0!==t?(this._placeHolderColor=t,this.render()):this._placeHolderColor},fontWeight:function(t){return void 0!==t?(this._fontWeight=t,this.render()):this._fontWeight},fontStyle:function(t){return void 0!==t?(this._fontStyle=t,this.render()):this._fontStyle},fontShadowColor:function(t){return void 0!==t?(this._fontShadowColor=t,this.render()):this._fontShadowColor},fontShadowBlur:function(t){return void 0!==t?(this._fontShadowBlur=t,this.render()):this._fontShadowBlur},fontShadowOffsetX:function(t){return void 0!==t?(this._fontShadowOffsetX=t,this.render()):this._fontShadowOffsetX},fontShadowOffsetY:function(t){return void 0!==t?(this._fontShadowOffsetY=t,this.render()):this._fontShadowOffsetY},width:function(t){return void 0!==t?(this._width=t,this._calcWH(),this._updateCanvasWH(),this._updateHiddenInput(),this.render()):this._width},height:function(t){return void 0!==t?(this._height=t,this._calcWH(),this._updateCanvasWH(),this._updateHiddenInput(),this.render()):this._height},padding:function(t){return void 0!==t?(this._padding=t,this._calcWH(),this._updateCanvasWH(),this.render()):this._padding},borderWidth:function(t){return void 0!==t?(this._borderWidth=t,this._calcWH(),this._updateCanvasWH(),this.render()):this._borderWidth},borderColor:function(t){return void 0!==t?(this._borderColor=t,this.render()):this._borderColor},borderRadius:function(t){return void 0!==t?(this._borderRadius=t,this.render()):this._borderRadius},backgroundColor:function(t){return void 0!==t?(this._backgroundColor=t,this.render()):this._backgroundColor},backgroundGradient:function(t){return void 0!==t?(this._backgroundColor=this._renderCtx.createLinearGradient(0,0,0,this.outerH),this._backgroundColor.addColorStop(0,t[0]),this._backgroundColor.addColorStop(1,t[1]),this.render()):this._backgroundColor},boxShadow:function(t,e){if(void 0===t)return this._boxShadow;var i=t.split("px ");return this._boxShadow={x:"none"===this._boxShadow?0:parseInt(i[0],10),y:"none"===this._boxShadow?0:parseInt(i[1],10),blur:"none"===this._boxShadow?0:parseInt(i[2],10),color:"none"===this._boxShadow?"":i[3]},this._boxShadow.x<0?(this.shadowL=Math.abs(this._boxShadow.x)+this._boxShadow.blur,this.shadowR=this._boxShadow.blur+this._boxShadow.x):(this.shadowL=Math.abs(this._boxShadow.blur-this._boxShadow.x),this.shadowR=this._boxShadow.blur+this._boxShadow.x),this._boxShadow.y<0?(this.shadowT=Math.abs(this._boxShadow.y)+this._boxShadow.blur,this.shadowB=this._boxShadow.blur+this._boxShadow.y):(this.shadowT=Math.abs(this._boxShadow.blur-this._boxShadow.y),this.shadowB=this._boxShadow.blur+this._boxShadow.y),this.shadowW=this.shadowL+this.shadowR,this.shadowH=this.shadowT+this.shadowB,this._calcWH(),e?void 0:(this._updateCanvasWH(),this.render())},innerShadow:function(t){return void 0!==t?(this._innerShadow=t,this.render()):this._innerShadow},selectionColor:function(t){return void 0!==t?(this._selectionColor=t,this.render()):this._selectionColor},placeHolder:function(t){return void 0!==t?(this._placeHolder=t,this.render()):this._placeHolder},value:function(t){return void 0!==t?(this._value=t+"",this._hiddenInput.value=t+"",this._cursorPos=this._clipText().length,this.render(),this):this._value===this._placeHolder?"":this._value},onsubmit:function(t){if(void 0!==t)return this._onsubmit=t,this;this._onsubmit()},onkeydown:function(t){if(void 0!==t)return this._onkeydown=t,this;this._onkeydown()},onkeyup:function(t){if(void 0!==t)return this._onkeyup=t,this;this._onkeyup()},focus:function(e){var i=this;if(!i._hasFocus){i._onfocus(i);for(var n=0;n<t.length;n++)t[n]._hasFocus&&t[n].blur()}i._selectionUpdated?delete i._selectionUpdated:i._selection=[0,0],i._hasFocus=!0,i._readonly?i._hiddenInput.readOnly=!0:(i._hiddenInput.readOnly=!1,i._cursorPos="number"==typeof e?e:i._clipText().length,i._placeHolder===i._value&&(i._value="",i._hiddenInput.value=""),i._cursor=!0,i._cursorInterval&&clearInterval(i._cursorInterval),i._cursorInterval=setInterval((function(){i._cursor=!i._cursor,i.render()}),500));var s=i._selection[0]>0||i._selection[1]>0;return i._hiddenInput.focus(),i._hiddenInput.selectionStart=s?i._selection[0]:i._cursorPos,i._hiddenInput.selectionEnd=s?i._selection[1]:i._cursorPos,i.render()},blur:function(t){var e=t||this;return e._onblur(e),e._cursorInterval&&clearInterval(e._cursorInterval),e._hasFocus=!1,e._cursor=!1,e._selection=[0,0],e._hiddenInput.blur(),""===e._value&&(e._value=e._placeHolder),e.render()},keydown:function(e,i){var n=e.which;e.shiftKey;if(!i._readonly&&i._hasFocus){if(i._onkeydown(e,i),65===n&&(e.ctrlKey||e.metaKey))return i.selectText(),e.preventDefault(),i.render();if(17===n||e.metaKey||e.ctrlKey)return i;if(13===n)e.preventDefault(),i._onsubmit(e,i);else if(9===n&&(e.preventDefault(),t.length>1)){var s=t[i._inputsIndex+1]?i._inputsIndex+1:0;i.blur(),setTimeout((function(){t[s].focus()}),10)}return i._value=i._hiddenInput.value,i._cursorPos=i._hiddenInput.selectionStart,i._selection=[0,0],i.render()}},click:function(t,e){var i=e._mousePos(t),n=i.x,s=i.y;return e._endSelection?(delete e._endSelection,void delete e._selectionUpdated):e._canvas&&e._overInput(n,s)||!e._canvas?e._mouseDown?(e._mouseDown=!1,e.click(t,e),e.focus(e._clickPos(n,s))):void 0:e.blur()},mousemove:function(t,e){var i=e._mousePos(t),n=i.x,s=i.y,o=e._overInput(n,s);if(o&&e._canvas?(e._canvas.style.cursor="text",e._wasOver=!0):e._wasOver&&e._canvas&&(e._canvas.style.cursor="default",e._wasOver=!1),e._hasFocus&&e._selectionStart>=0){var r=e._clickPos(n,s),h=Math.min(e._selectionStart,r),a=Math.max(e._selectionStart,r);if(!o)return e._selectionUpdated=!0,e._endSelection=!0,delete e._selectionStart,void e.render();e._selection[0]===h&&e._selection[1]===a||(e._selection=[h,a],e.render())}},mousedown:function(t,e){var i=e._mousePos(t),n=i.x,s=i.y,o=e._overInput(n,s);e._mouseDown=o,e._hasFocus&&o&&(e._selectionStart=e._clickPos(n,s))},mouseup:function(t,e){var i=e._mousePos(t),n=i.x,s=i.y,o=e._clickPos(n,s)!==e._selectionStart;e._hasFocus&&e._selectionStart>=0&&e._overInput(n,s)&&o?(e._selectionUpdated=!0,delete e._selectionStart,e.render()):delete e._selectionStart,e.click(t,e)},selectText:function(t){var e=this;t=t||[0,e._value.length];return setTimeout((function(){e._selection=[t[0],t[1]],e._hiddenInput.selectionStart=t[0],e._hiddenInput.selectionEnd=t[1],e.render()}),1),e},renderCanvas:function(){return this._renderCanvas},render:function(t){var e=this,i=e._renderCtx,n=e.outerW,s=e.outerH,o=e._borderRadius,r=e._borderWidth,h=e.shadowW,a=e.shadowH;e.manual&&!t||i&&(t&&i.clearRect(0,0,i.canvas.width,i.canvas.height),i.shadowOffsetX=e._boxShadow.x,i.shadowOffsetY=e._boxShadow.y,i.shadowBlur=e._boxShadow.blur,i.shadowColor=e._boxShadow.color,e._borderWidth>0&&(i.fillStyle=e._borderColor,e._roundedRect(i,e.shadowL,e.shadowT,n-h,s-a,o),i.fill(),i.shadowOffsetX=0,i.shadowOffsetY=0,i.shadowBlur=0),e._drawTextBox((function(){i.shadowOffsetX=0,i.shadowOffsetY=0,i.shadowBlur=0;var t=e._clipText(),d=e._padding+e._borderWidth+e.shadowT;if(e._selection[1]>0){var c=e._textWidth(t.substring(0,e._selection[0])),u=e._textWidth(t.substring(e._selection[0],e._selection[1]));i.fillStyle=e._selectionColor,i.fillRect(d+c,d,u,e._height)}if(e._cursor){var l=e._textWidth(t.substring(0,e._cursorPos));i.fillStyle=e._fontColor,i.fillRect(d+l,d,1,e._height)}var f=e._padding+e._borderWidth+e.shadowL,_=Math.round(d+e._height/2);t=""===t&&e._placeHolder?e._placeHolder:t,i.fillStyle=""!==e._value&&e._value!==e._placeHolder?e._fontColor:e._placeHolderColor,i.font=e._fontStyle+" "+e._fontWeight+" "+e._fontSize+"px "+e._fontFamily,i.shadowColor=e._fontShadowColor,i.shadowBlur=e._fontShadowBlur,i.shadowOffsetX=e._fontShadowOffsetX,i.shadowOffsetY=e._fontShadowOffsetY,i.textAlign="left",i.textBaseline="middle",i.fillText(t,f,_);var p=e._innerShadow.split("px "),g="none"===e._innerShadow?0:parseInt(p[0],10),m="none"===e._innerShadow?0:parseInt(p[1],10),v="none"===e._innerShadow?0:parseInt(p[2],10),w="none"===e._innerShadow?"":p[3];if(v>0){var x=e._shadowCtx,y=x.canvas.width,b=x.canvas.height;x.clearRect(0,0,y,b),x.shadowBlur=v,x.shadowColor=w,x.shadowOffsetX=0,x.shadowOffsetY=m,x.fillRect(-1*n,-100,3*n,100),x.shadowOffsetX=g,x.shadowOffsetY=0,x.fillRect(y,-1*s,100,3*s),x.shadowOffsetX=0,x.shadowOffsetY=m,x.fillRect(-1*n,b,3*n,100),x.shadowOffsetX=g,x.shadowOffsetY=0,x.fillRect(-100,-1*s,100,3*s),e._roundedRect(i,r+e.shadowL,r+e.shadowT,n-2*r-h,s-2*r-a,o),i.clip(),i.drawImage(e._shadowCanvas,0,0,y,b,r+e.shadowL,r+e.shadowT,y,b)}return e._ctx&&(e._ctx.clearRect(e._x,e._y,i.canvas.width,i.canvas.height),e._ctx.drawImage(e._renderCanvas,e._x,e._y)),e})))},destroy:function(){var e=t.indexOf(this);-1!=e&&t.splice(e,1),this._hasFocus&&this.blur(),document.body.removeChild(this._hiddenInput);for(var i=this._removals,n=0;n<i.length;n++){var s=i[n],o=s[0];o.removeEventListener.apply(o,s.slice(1))}this._renderCanvas=null,this._shadowCanvas=null,this._renderCtx=null},_drawTextBox:function(t){var e=this,i=e._renderCtx,n=e.outerW,s=e.outerH,o=e._borderRadius,r=e._borderWidth,h=e.shadowW,a=e.shadowH;if(""===e._backgroundImage)i.fillStyle=e._backgroundColor,e._roundedRect(i,r+e.shadowL,r+e.shadowT,n-2*r-h,s-2*r-a,o),i.fill(),t();else{var d=new Image;d.src=e._backgroundImage,d.onload=function(){i.drawImage(d,0,0,d.width,d.height,r+e.shadowL,r+e.shadowT,n,s),t()}}},_clearSelection:function(){if(this._selection[1]>0){var t=this._selection[0],e=this._selection[1];return this._value=this._value.substr(0,t)+this._value.substr(e),this._cursorPos=t,this._cursorPos=this._cursorPos<0?0:this._cursorPos,this._selection=[0,0],!0}return!1},_clipText:function(t){t=void 0===t?this._value:t;var e=this._textWidth(t)/(this._width-this._padding);return(e>1?t.substr(-1*Math.floor(t.length/e)):t)+""},_textWidth:function(t){var e=this._renderCtx;return e.font=this._fontStyle+" "+this._fontWeight+" "+this._fontSize+"px "+this._fontFamily,e.textAlign="left",e.measureText(t).width},_calcWH:function(){this.outerW=this._width+2*this._padding+2*this._borderWidth+this.shadowW,this.outerH=this._height+2*this._padding+2*this._borderWidth+this.shadowH},_updateCanvasWH:function(){var t=this._renderCanvas.width,e=this._renderCanvas.height;this._renderCanvas.setAttribute("width",this.outerW),this._renderCanvas.setAttribute("height",this.outerH),this._shadowCanvas.setAttribute("width",this._width+2*this._padding),this._shadowCanvas.setAttribute("height",this._height+2*this._padding),this._ctx&&this._ctx.clearRect(this._x,this._y,t,e)},_updateHiddenInput:function(){this._hiddenInput.style.left=this._x+this._extraX+(this._canvas?this._canvas.offsetLeft:0)+"px",this._hiddenInput.style.top=this._y+this._extraY+(this._canvas?this._canvas.offsetTop:0)+"px",this._hiddenInput.style.width=this._width+2*this._padding+"px",this._hiddenInput.style.height=this._height+2*this._padding+"px"},_roundedRect:function(t,e,i,n,s,o){n<2*o&&(o=n/2),s<2*o&&(o=s/2),t.beginPath(),t.moveTo(e+o,i),t.lineTo(e+n-o,i),t.quadraticCurveTo(e+n,i,e+n,i+o),t.lineTo(e+n,i+s-o),t.quadraticCurveTo(e+n,i+s,e+n-o,i+s),t.lineTo(e+o,i+s),t.quadraticCurveTo(e,i+s,e,i+s-o),t.lineTo(e,i+o),t.quadraticCurveTo(e,i,e+o,i),t.closePath()},_overInput:function(t,e){var i=t>=this._x+this._extraX,n=t<=this._x+this._extraX+this._width+2*this._padding,s=e>=this._y+this._extraY,o=e<=this._y+this._extraY+this._height+2*this._padding;return i&&n&&s&&o},_clickPos:function(t,e){var i=this._value;this._value===this._placeHolder&&(i="");var n=this._clipText(i),s=0,o=n.length;if(t-(this._x+this._extraX)<this._textWidth(n))for(var r=0;r<n.length;r++)if((s+=this._textWidth(n[r]))>=t-(this._x+this._extraX)){o=r;break}return o},_mousePos:function(t){var e=t.target,i=t.pageX,n=t.pageY;t.touches&&t.touches.length?(e=t.touches[0].target,i=t.touches[0].pageX,n=t.touches[0].pageY):t.changedTouches&&t.changedTouches.length&&(e=t.changedTouches[0].target,i=t.changedTouches[0].pageX,n=t.changedTouches[0].pageY);var s=document.defaultView.getComputedStyle(e,void 0),o=parseInt(s.paddingLeft,10)||0,r=parseInt(s.paddingLeft,10)||0,h=parseInt(s.borderLeftWidth,10)||0,a=parseInt(s.borderLeftWidth,10)||0,d=document.body.parentNode.offsetTop||0,c=document.body.parentNode.offsetLeft||0,u=0,l=0;if(void 0!==e.offsetParent)do{u+=e.offsetLeft,l+=e.offsetTop}while(e=e.offsetParent);return{x:i-(u+=o+h+c),y:n-(l+=r+a+d)}}}}();const __GUIMANAGER_DEBUG_PANEL_NAME="debug-panel",__GUIMANAGER_WINDOW_NAME="window",__GUIMANAGER_CONTAINER_NAME="con",__GUIMANAGER_X_SUFFIX="-x-btn",__GUIMANAGER_BG_NAME="bg",__GUIMANAGER_BTN_NAME="btn",__GUIMANAGER_TXT_NAME="txt",__GUIMANAGER_TXTBOX_NAME="tb";function GuiManager(t,e){this.isFocusConsumed=!1,this.eleNum=0,this.theme=e||new Theme(__UI_DEFAULT_THEME,t.getCanvasWidth(),t.getCanvasHeight()),this.setTheme=function(e){e?this.theme=new Theme(e,t.getCanvasWidth(),t.getCanvasHeight()):t.warn("bad theme settings!")},this.getTheme=function(){return this.theme},this.createContainer=function(e,i,n,s,o,r){var h=new Entity(t,e,!0,i,n,!0),a=new Plane(t,s,o,r);return a.effect=this.theme.getProperty("container","effect"),h.addChild("bg",a),h},this.createDefaultContainer=function(t,e){var i=this.theme.getSize("container","height");return this.createContainer(this.generateUIName("con"),t,e,this.theme.getSize("container","width"),i,this.theme.getProperty("container","color"))},this.createButton=function(e,i,n,s,o,r,h,a,d){var c=this.createContainer(e,i,n,s,o,r),u=(c.getChildAt(0),new Text(t,h,a,d));return c.addChild("txt",u),void 0===ButtonHandler?console.warn("[JSCF][GuiManager] ButtonHandler component not loaded! Some functionality is disabled!"):c.addComponent(ButtonHandler),c},this.createDefaultButton=function(t,e,i,n){var s=this.theme.settings.button,o=this.createButton(this.generateUIName("btn"),t,e,this.theme.getSize("button","width"),this.theme.getSize("button","height"),s.color,i,s.font_color,this.theme.getFontDesc("button"));return n&&(o.getComponentOfType(ButtonHandler).onClick=n),o},this.createTextBox=function(e,i,n,s,o,r,h){var a=new Entity(t,e,!0,i,n,!0),d=new Textbox(a,s,o,r);return h&&(d.effect=h),a.insertChild(d),a.addComponent(ButtonHandler),a},this.createDefaultTextBox=function(t,e,i){return i||(i=""),this.createTextBox(this.generateUIName("tb"),t,e,this.theme.getSize("textbox","width"),this.theme.getSize("textbox","height"),i,this.theme.getProperty("textbox","effect"))},this.createLabel=function(e,i,n){var s=new Text(t,n,this.theme.getProperty("label","font_color"),this.theme.getFontDesc("label"));s.enabled=!0;var o=new Entity(t,"txt",!0,e,i,!0);return o.insertChild(s),o},this.createDefaultWindow=function(e,i){var n=this.createDefaultContainer(e,i);n.name=this.generateUIName("window");var s=this.theme.getSize("window","ctl_size"),o=this.theme.getSize("container","width")*s,r=this.theme.getSize("container","height")*s,h=n.getChild("bg"),a=this.createButton(n.name+"-x-btn",(o-h.width)/2,(r-h.height)/2,o,r,this.theme.getProperty("button","color"),"X",this.theme.getProperty("button","font_color"),this.theme.getFontDesc("button"));return a.setDimentions=function(){a.transform.pos.x=(o-h.width)/2,a.transform.pos.y=(r-h.height)/2},a.getComponentOfType(ButtonHandler).onClick=function(){SceneUtils.deleteParent(t,a)},n.insertChild(a),n.addComponent(RectangleEditor),n},this.createRectEditor=function(e,i,n){var s=new Entity(t,"rect-editor",!0,e,i,!0);return s.insertChild(new RectangleEditor(s,n)),s},this.errorPopup=function(e){var i=this.createDefaultWindow(t.getCanvasWidth()/2,t.getCanvasHeight()/2);i.insertChild(this.createLabel(0,0,e)),t.getCurrentScene().addEntity(i)},this.insertRectangleEditor=function(){var e=t.getCurrentScene().entities;for(var i in e)e.hasOwnProperty(i)&&(e[i].hasComponentOfType(RectangleEditor)||e[i].addComponent(RectangleEditor))},this.delRectangleEditor=function(){var e=t.getCurrentScene().entities;for(var i in e)e.hasOwnProperty(i)&&"debug-panel"!=e[i].name&&e[i].delComponentOfType(RectangleEditor)},this.beautifyObjectString=function(t){var e="";return t.constructor.component_name?e+=t.constructor.component_name+"\n(Component)":(t.name&&(e+=t.name+" "),e+="("+t.constructor.name+")"),e},this.buildString=function(t,e){var i="";for(var n in i+=t.name+" (Entity)",t.children)if(t.children.hasOwnProperty(n)){var s=t.children[n];if(s instanceof Entity)i+="\n\t\t\t",i+=this.buildString(s,e);else{var o="\n\t\t\t";s.constructor.component_name?(o+="\t\t\t- ",s.constructor.component_name==Script.component_name&&(o+=n+" "),o+=s.constructor.component_name+" (Component)"):o+="\t\t\t- "+n+" ("+s.constructor.name+")",(!e||e&&-1!=o.search(e))&&(i+=o)}}return i},this.generateUIName=function(t){return t+this.eleNum++},this.focus=function(){return!this.isFocusConsumed&&(this.isFocusConsumed=!0,!0)},this.resetFocus=function(){this.isFocusConsumed=!1}}var Layout={calcPreferredSize:function(t){},doLayout:function(t){}},LinedLayout={calcPreferredSize:function(t){},doLayout:function(t){for(var e=t.getDimentions(),i=t.getEntityChildren(),n=e.x-10,s=10,o=0;o<i.length;o++){var r=i[o],h=r.getDimentions().y+10;s+=h/2,r.transform&&(r.transform.pos=new Vector2d(0,-e.y/2+s),r.setDimentions(n,-1)),s+=h/2}}},FitLayout={calcPreferredSize:function(t){},doLayout:function(t){for(var e=t.getDimentions(),i=t.getEntityChildren(),n=e.x-10,s=10,o=Math.floor(e.y/i.length-10),r=0;r<i.length;r++){var h=i[r];s+=(o+10)/2,h.transform&&(h.transform.pos=new Vector2d(0,-e.y/2+s),h.setDimentions(n,o)),s+=o/2}}};function Text(t,e,i,n){this.txt="",this.style=i,this.font=n,this.lines=[],this.maxWidth=0,this.maxHeight=20,this.lineMargin=5,this.setText=function(e){this.txt=e,this.lines=String(this.txt).split("\n");var i=t.graphics.context;i.font=this.font;for(var n=0;n<this.lines.length;n++){var s=i.measureText(this.lines[n]).width;this.maxWidth=Math.max(this.maxWidth,s)}this.maxHeight=parseInt(i.font.match(/\d+/),10)+this.lineMargin},this.getText=function(){return this.txt},this.getDimentions=function(){return new Vector2d(this.maxWidth,this.maxHeight)},this.setDimentions=function(t,e){},this.render=function(){for(var e=this.getDimentions(),i=0;i<this.lines.length;i++)t.renderText(-e.x/2,i*e.y,this.lines[i],this.style,this.font)},this.init=function(){this.setText(e)},this.init()}function Textbox(t,e,i,n){this.parent=t,this.effect=shadowFx,this.textBox=new CanvasInput({canvas:t.game.graphics.canvas,width:e,height:i,value:n||"",fontSize:.8*i,borderRadius:0,innerShadow:"0px 0px 0px #fff",borderColor:"#000",boxShadow:"0px 0px 0px #fff"}),this.setDimentions=function(t,e){t>0&&this.textBox.width(.97*t),e>0&&this.textBox.height(.5*e)},this.getDimentions=function(){return new Vector2d(this.textBox.width()/.97,this.textBox.height()/.5)},this.setDimentions=function(t,e){t>0&&this.textBox.width(.97*t),e>0&&this.textBox.height(.5*e)},this.getDimentions=function(){return new Vector2d(this.textBox.width()/.97,this.textBox.height()/.5)},this.update=function(){this.parent.transform||console.warn("[JSCF][Textbox] No transform! Parent isn't entity?");var t=this.parent.getGlobalTransform(),e=this.getDimentions(),i=Math.round(t.pos.x-e.x/2),n=Math.round(t.pos.y-e.y/2);this.textBox.x(i),this.textBox.y(n)},this.render=function(){var t=this.parent.game.graphics.context;this.effect.pre_render(t),t.save(),t.setTransform(1,0,0,1,0,0);var e=this.textBox.render(!0);return t.restore(),this.effect.post_render(t),e},this.toggleEnabled=function(){this.textBox._readonly=!this.textBox._readonly},this.destroy=function(){this.textBox.destroy(),game.graphics.canvas.style.cursor="default"}}const __UI_LIGHT_THEME={panel:{width:"25%",height:"100%",margin:"50px"},button:{color:"#bebebe",width:"50px",height:"50px",font_color:"#000",font_size:"15px",font:"arial"},container:{color:"#a6a6a6cc",width:"250px",height:"250px",font:"arial",effect:shadowFx},textbox:{width:"100px",height:"23px"},label:{font_color:"white",font_size:"15px",font:"arial"},window:{ctl_size:"17%"}},__UI_DARK_THEME={panel:{width:"25%",height:"100%",margin:"50px"},button:{color:"#333",width:"50px",height:"50px",font_color:"#eee",font_size:"15px",font:"arial"},container:{color:"#555555cc",width:"250px",height:"250px",font:"arial",effect:noneFx},textbox:{width:"100px",height:"23px",effect:noneFx},label:{font_color:"white",font_size:"15px",font:"arial"},window:{ctl_size:"17%"}},__UI_DEFAULT_THEME=__UI_DARK_THEME;function __UI_SIZE(t,e){var i=t,n=1;return i.endsWith("%")?(i=i.replace("%",""),isNaN(e)?(console.warn("max size isn't numeric! Assuming fraction!"),n=.01):n=e/100):i.endsWith("px")&&(i=i.replace("px","")),isNaN(i)?(console.warn("[__UI_STRING_TO_INT] string size isn't numeric!"),i):parseInt(i)*n}function __UI_FONT(t){return t.font_color+" "+t.font_size+" "+t.font}function Theme(t,e,i){this.canvasWidth=e,this.canvasHeight=i,this.settings=t,this.getSize=function(t,n){var s=this.settings[t][n];return"width"==n?__UI_SIZE(s,e):"height"==n?__UI_SIZE(s,i):__UI_SIZE(s)},this.getFontDesc=function(t){var e="",i=(this.settings[t].font_color,this.settings[t].font_size),n=this.settings[t].font;return i&&(e+=" "+i),n&&(e+=" "+n),e},this.getProperty=function(t,e){return this.settings[t][e]}}const __EPSILON=1e-8;var MathUtils={toRad:function(t){return t*Math.PI/180},square:function(t){return t*t},greaterThan:function(t,e){return Math.abs(t-e)>=1e-8},lesserThan:function(t,e){return MathUtils.greaterThan(e,t)},sign:function(t){return 0==t?0:t/Math.abs(t)}};function Point2d(t,e){this.vec=new Vector2d(t,e),this.distanceTo=function(t){return Vector.subVector(this.vec,t.vec).length()},this.setX=function(t){this.vec.x=t},this.setY=function(t){this.vec.y=t},this.getX=function(){return this.vec.x},this.getY=function(){return this.vec.y}}function Vector2d(t,e){this.x=t||0,this.y=e||0,this.add=function(t,e){return this.x+=t,this.y+=e,this},this.sub=function(t,e){return this.add(-t,-e)},this.scalarAdd=function(t){return this.add(t,t)},this.scalarSub=function(t){return this.sub(t,t)},this.scalarMul=function(t){this.x*=t,this.y*=t},this.scalarDiv=function(t){0!=t&&(this.x/=t,this.y/=t)},this.addVector=function(t){return this.add(t.x,t.y)},this.subVector=function(t){return this.sub(t.x,t.y)},this.dotProduct=function(t){return this.x*t.x+this.y*t.y},this.length=function(){return Math.sqrt(MathUtils.square(this.x)+MathUtils.square(this.y))},this.getNormal=function(){var t=this.length();return 0!=t?new Vector2d(this.x/t,this.y/t):new Vector2d(0,0)},this.normalize=function(){var t=this.length();if(!t)return this.x=0,void(this.y=0);this.x/=t,this.y/=t},this.makeArray=function(){return[this.x,this.y]},this.clone=function(){return new Vector2d(this.x,this.y)}}const Vector={addVector:function(t,e){return t.clone().addVector(e)},subVector:function(t,e){return t.clone().subVector(e)}};Vector2d.prototype.toString=function(){return[this.x,this.y].toString()};