(function(s,c){typeof exports=="object"&&typeof module<"u"?module.exports=c(require("planck")):typeof define=="function"&&define.amd?define(["planck"],c):(s=typeof globalThis<"u"?globalThis:s||self,s.KaPlanck=c(s.Planck))})(this,function(s){"use strict";function c(r){return r*s.Settings.lengthUnitsPerMeter}function w(r){return r/s.Settings.lengthUnitsPerMeter}function U(r){const t=r.center();return new s.Vec2(w(t.x),w(t.y))}function b(r){return new s.Vec2(w(r.x),w(r.y))}function p(r,t){return r.vec2(c(t.x),c(t.y))}function W(r){let t=null,e=r.parent;for(;e;){const n=e.c("kpWorld");if(n){t=n.world;break}e=e.parent}return t}function y(r){return{color:r.color,opacity:r.opacity,outline:r.outline,shader:r.shader,uniform:r.uniform}}function P(r){let t=null;const e=r.get("*");for(const n of e)if(n.c("kpWorld")){t=n;break}return t}function E(r){return{id:"kpBody",require:["kpPos","kpRotate"],body:null,add(){const t=W(this);if(!t)throw new Error("kpBody requires to be a descendant of kpWorld");this.body=t.createBody({...r,position:this.kpPos,angle:this.kpAngle})},fixedUpdate(){this.body&&(this.kpPos=this.body.getPosition(),this.kpAngle=this.body.getAngle())},destroy(){if(!this.body)return;this.body.getWorld().destroyBody(this.body)},get inspectColor(){return this.body?this.body.isDynamic()?{r:0,g:191,b:255}:this.body.isKinematic()?{r:238,g:130,b:238}:{r:255,g:255,b:255}:{r:0,g:0,b:0}}}}function F(r,t){return{id:"kpShape",shape:new s.BoxShape(t.halfWidth,t.halfHeight,t==null?void 0:t.center,t==null?void 0:t.angle),draw(){x(r,t,this)}}}function x(r,t,e){var u,a;if(!t.draw)return;const n=y(e),i=c(t.halfWidth)*2,o=c(t.halfHeight)*2,h=r.vec2(c(((u=t.center)==null?void 0:u.x)??0),c(((a=t.center)==null?void 0:a.y)??0));r.drawRect({...n,width:i,height:o,pos:h,anchor:"center"})}function D(r,t){return{id:"kpShape",shape:new s.ChainShape(t==null?void 0:t.vertices,t==null?void 0:t.loop),draw(){S(r,t,this,this.shape)}}}function S(r,t,e,n){if(!(t!=null&&t.draw))return;const i=y(e),o=n.m_vertices,h=[];for(let u=0;u{n?n=this.isHovering():this.isHovering()&&(n=!0,e())})},onHoverUpdate(e){return this.onUpdate(()=>{this.isHovering()&&e()})},onHoverEnd(e){let n=!1;return this.onUpdate(()=>{n?this.isHovering()||(n=!1,e()):n=this.isHovering()})},onClick(e,n="left"){const i=r.onMousePress(n,()=>{this.isHovering()&&e()});return this.onDestroy(()=>i.cancel()),i},add(){if(!this.body)throw new Error("kpBody is required");const e=(t==null?void 0:t.userData)??{};this.fixture=this.body.createFixture({...t,shape:this.shape,userData:{...e,gameObj:this}})}}}function M(r,t){return{id:"kpShape",shape:new s.PolygonShape(t==null?void 0:t.vertices),draw(){m(r,t,this,this.shape)}}}function m(r,t,e,n){if(!(t!=null&&t.draw))return;const i=y(e),o=n.m_vertices.map(h=>p(r,h));r.drawPolygon({...i,pts:o})}function V(r,...t){return{id:"kpPos",kpPos:v(...t),kpMove(...e){this.kpMoveBy(v(...e).mul(r.dt()))},kpMoveBy(...e){this.kpPos=this.kpPos.add(v(...e))},kpMoveTo(...e){const n=new s.Vec2;let i=1;e.length>=1&&typeof e[0]!="number"&&(n.set(e[0].x,e[0].y),e.length===2&&typeof e[1]=="number"&&(i=e[1])),e.length>=2&&typeof e[0]=="number"&&typeof e[1]=="number"&&(n.set(e[0],e[1]),e.length>=3&&typeof e[2]=="number"&&(i=e[2]));const o=n.sub(this.kpPos);if(o.length()>i*r.dt()){this.kpPos.set(n);return}o.normalize(),this.kpMove(n.mul(i))},add(){this.use(r.pos());const e=this.c("pos");e&&(e.pos=p(r,this.kpPos))},update(){const e=this.c("pos");e&&(e.pos=p(r,this.kpPos))}}}function v(...r){return r.length===2?new s.Vec2(r[0],r[1]):r.length===1?new s.Vec2(r[0]):new s.Vec2}function q(r,t){return{id:"kpWorld",world:new s.World(t),add(){this.world.on("begin-contact",e=>{const n=e.getFixtureA().getUserData(),i=e.getFixtureB().getUserData();n&&i&&this.trigger("contactBegin",n.gameObj,i.gameObj,e)}),this.world.on("end-contact",e=>{const n=e.getFixtureA().getUserData(),i=e.getFixtureB().getUserData();n&&i&&this.trigger("contactEnd",n.gameObj,i.gameObj,e)}),this.world.on("pre-solve",e=>{const n=e.getFixtureA().getUserData(),i=e.getFixtureB().getUserData();n&&i&&this.trigger("contactPreSolve",n.gameObj,i.gameObj,e)}),this.world.on("post-solve",e=>{const n=e.getFixtureA().getUserData(),i=e.getFixtureB().getUserData();n&&i&&this.trigger("contactPostSolve",n.gameObj,i.gameObj,e)})},onContactBegin(e){this.on("contactBegin",e)},onContactEnd(e){this.on("contactEnd",e)},onContactPreSolve(e){this.on("contactPreSolve",e)},onContactPostSolve(e){this.on("contactPostSolve",e)},fixedUpdate(){this.world.step(r.fixedDt())}}}function R(r){return{id:"kpFixtures",require:["kpBody","kpShapes"],fixtures:[],add(){if(!this.body)throw new Error("kpBody is required");if(!this.shapes)throw new Error("kpShapes is required");if(this.shapes.lengtht=>(s.Settings.lengthUnitsPerMeter=(r==null?void 0:r.lengthUnitsPerMeter)??10,{kpPos(...e){return V(t,...e)},kpRotate(e){return K(t,e)},kpWorld(e){return q(t,e)},kpBody(e){return E(e)},kpFixture(e){return O(t,e)},kpFixtures(e){return R(e)},kpBoxShape(e){return F(t,e)},kpChainShape(e){return D(t,e)},kpCircleShape(e){return B(t,e)},kpEdgeShape(e){return H(t,e)},kpPolygonShape(e){return M(t,e)},kpShapes(e){return T(t,e)},onKPCollide(e,n,i,o){const h=o||P(t);if(!h)throw new Error("No pkWorld found");h.onContactPreSolve((u,a,l)=>{!u.tags.includes(e)&&!a.tags.includes(e)||!u.tags.includes(n)&&!a.tags.includes(n)||i(u,a,l)})},onKPCollideUpdate(e,n,i,o){const h=o||P(t);if(!h)throw new Error("No pkWorld found");let u=!1,a=null,l=null,d;h.onContactBegin((f,g,_)=>{!f.tags.includes(e)&&!g.tags.includes(e)||!f.tags.includes(n)&&!g.tags.includes(n)||(u=!0,d=_,a=f,l=g)}),h.onContactEnd((f,g)=>{!f.tags.includes(e)&&!g.tags.includes(e)||!f.tags.includes(n)&&!g.tags.includes(n)||(u=!1,d=void 0,a=null,l=null)}),t.onFixedUpdate(()=>{!u||!a||!l||!d||i(a,l,d)})},onKPCollideEnd(e,n,i,o){const h=o||P(t);if(!h)throw new Error("No pkWorld found");h.onContactEnd((u,a,l)=>{!u.tags.includes(e)&&!a.tags.includes(e)||!u.tags.includes(n)&&!a.tags.includes(n)||i(u,a,l)})},kpCenter(){return U(t)},kpMousePos(){return b(t.mousePos())},u2p:c,p2u:w,k2pVec2:b,p2kVec2(e){return p(t,e)}})}); //# sourceMappingURL=kaplanck.umd.cjs.map