-
Notifications
You must be signed in to change notification settings - Fork 33
/
boxbox.min.js
2 lines (2 loc) · 15 KB
/
boxbox.min.js
1
2
/* Copyright 2012 Greg Smith. Licensed under the MIT License. http://incompl.github.com/boxbox/ */
(function(){var a=0,b=["ms","moz","webkit","o"];for(var c=0;c<b.length&&!window.requestAnimationFrame;++c)window.requestAnimationFrame=window[b[c]+"RequestAnimationFrame"],window.cancelRequestAnimationFrame=window[b[c]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||(window.requestAnimationFrame=function(b,c){var d=(new Date).getTime(),e=Math.max(0,16-(d-a)),f=window.setTimeout(function(){b(d+e)},e);return a=d+e,f}),window.cancelAnimationFrame||(window.cancelAnimationFrame=function(a){clearTimeout(a)})})(),function(){function b(a){function b(){}return b.prototype=a,new b}function c(a,b){a===undefined&&(a={});if(b!==undefined)for(var c in b)b.hasOwnProperty(c)&&a[c]===undefined&&(a[c]=b[c]);return a}var a=57.2957795;window.boxbox={};if(Box2D===undefined){console.error("boxbox needs Box2d to work");return}var d=Box2D.Common.Math.b2Vec2,e=Box2D.Common.Math.b2Math,f=Box2D.Dynamics.b2BodyDef,g=Box2D.Dynamics.b2Body,h=Box2D.Dynamics.b2FixtureDef,i=Box2D.Dynamics.b2Fixture,j=Box2D.Dynamics.b2World,k=Box2D.Collision.Shapes,l=Box2D.Dynamics.b2DebugDraw,m=Box2D.Collision.b2AABB;window.boxbox.createWorld=function(a,c){var d=b(p);return d._init(a,c),d};var n={gravity:{x:0,y:10},allowSleep:!0,scale:30,tickFrequency:50,collisionOutlines:!1},o={type:"distance",allowCollisions:!1},p={_ops:null,_world:null,_canvas:null,_keydownHandlers:{},_keyupHandlers:{},_startContactHandlers:{},_finishContactHandlers:{},_impactHandlers:{},_destroyQueue:[],_impulseQueue:[],_constantVelocities:{},_constantForces:{},_entities:{},_nextEntityId:0,_cameraX:0,_cameraY:0,_onRender:[],_onTick:[],_creationQueue:[],_positionQueue:[],_init:function(a,b){var e=this,f,g,h,i;this._ops=c(b,n),this._world=new j(new d(this._ops.gravity.x,this._ops.gravity.y),!0),h=this._world,this._canvas=a,this._ctx=this._canvas.getContext("2d"),this._scale=this._ops.scale;if(this._canvas!==undefined){if(this._ops.debugDraw){var k=new l;k.SetSprite(this._canvas.getContext("2d")),k.SetDrawScale(this._scale),k.SetFillAlpha(.3),k.SetLineThickness(1),k.SetFlags(l.e_shapeBit|l.e_jointBit),h.SetDebugDraw(k)}window.setInterval(function(){var a,b;for(a=0;a<e._onTick.length;a++)b=e._onTick[a].ctx,b._destroyed||e._onTick[a].fun.call(b)},this._ops.tickFrequency),function m(){var a,b,c,f,i,j,k,l;for(a in e._constantVelocities)c=e._constantVelocities[a],c.body.SetLinearVelocity(new d(c.x,c.y),c.body.GetWorldCenter());for(g=0;g<e._impulseQueue.length;g++)f=e._impulseQueue.pop(),f.body.ApplyImpulse(new d(f.x,f.y),f.body.GetWorldCenter());for(a in e._constantForces)i=e._constantForces[a],i.body.ApplyForce(new d(i.x,i.y),i.body.GetWorldCenter());for(a in e._entities)b=e._entities[a],c=b._body.GetLinearVelocity(),c.x>b._ops.maxVelocityX&&(c.x=b._ops.maxVelocityX),c.x<-b._ops.maxVelocityX&&(c.x=-b._ops.maxVelocityX),c.y>b._ops.maxVelocityY&&(c.y=b._ops.maxVelocityY),c.y<-b._ops.maxVelocityY&&(c.y=-b._ops.maxVelocityY);for(g=0;g<e._destroyQueue.length;g++)j=e._destroyQueue.pop(),k=j._id,h.DestroyBody(j._body),j._destroyed=!0,delete e._keydownHandlers[k],delete e._startContactHandlers[k],delete e._finishContactHandlers[k],delete e._impactHandlers[k],e._destroyQueue.splice(k,1),e._impulseQueue.splice(k,1),delete e._constantVelocities[k],delete e._constantForces[k],delete e._entities[k];h.Step(1/60,10,10);for(g=0;g<e._creationQueue.length;g++)e.createEntity(e._creationQueue.pop());for(g=0;g<e._positionQueue.length;g++)l=e._positionQueue.pop(),l.o.position.call(l.o,l.val);e._canvas.width=e._canvas.width;for(a in e._entities)b=e._entities[a],b._draw(e._ctx,b.canvasPosition().x,b.canvasPosition().y);for(g=0;g<e._onRender.length;g++)e._onRender[g].fun.call(e._onRender[g].ctx,e._ctx);h.ClearForces(),h.DrawDebugData(),window.requestAnimationFrame(m)}(),window.addEventListener("keydown",function(a){for(var b in e._keydownHandlers)e._entities[b]._destroyed||e._keydownHandlers[b].call(e._entities[b],a)},!1),window.addEventListener("keyup",function(a){for(var b in e._keyupHandlers)e._entities[b]._destroyed||e._keyupHandlers[b].call(e._entities[b],a)},!1),i=new Box2D.Dynamics.b2ContactListener,i.BeginContact=function(a){var b=e._entities[a.GetFixtureA().GetBody()._bbid],c=e._entities[a.GetFixtureB().GetBody()._bbid];for(var d in e._startContactHandlers)b._id===Number(d)&&!b._destroyed&&e._startContactHandlers[d].call(e._entities[d],c),c._id===Number(d)&&!c._destroyed&&e._startContactHandlers[d].call(e._entities[d],b)},i.EndContact=function(a){var b=e._entities[a.GetFixtureA().GetBody()._bbid],c=e._entities[a.GetFixtureB().GetBody()._bbid];for(var d in e._finishContactHandlers)b._id===Number(d)&&!b._destroyed&&e._finishContactHandlers[d].call(e._entities[d],c),c._id===Number(d)&&!c._destroyed&&e._finishContactHandlers[d].call(e._entities[d],b)},i.PostSolve=function(a,b){var c=e._entities[a.GetFixtureA().GetBody()._bbid],d=e._entities[a.GetFixtureB().GetBody()._bbid];for(var f in e._impactHandlers)c._id===Number(f)&&!c._destroyed&&e._impactHandlers[f].call(e._entities[f],d,b.normalImpulses[0],b.tangentImpulses[0]),d._id===Number(f)&&!d._destroyed&&e._impactHandlers[f].call(e._entities[f],c,b.normalImpulses[0],b.tangentImpulses[0])},h.SetContactListener(i)}},_addKeydownHandler:function(a,b){this._keydownHandlers[a]=b},_addKeyupHandler:function(a,b){this._keyupHandlers[a]=b},_addStartContactHandler:function(a,b){this._startContactHandlers[a]=b},_addFinishContactHandler:function(a,b){this._finishContactHandlers[a]=b},_addImpactHandler:function(a,b){this._impactHandlers[a]=b},_destroy:function(a){this._destroyQueue.push(a)},_applyImpulse:function(a,b,c,d){this._impulseQueue.push({id:a,body:b,x:c,y:d})},_setConstantVelocity:function(a,b,c,d,e){this._constantVelocities[a+b]={id:b,body:c,x:d,y:e}},_clearConstantVelocity:function(a,b){delete this._constantVelocities[a+b]},_setConstantForce:function(a,b,c,d,e){this._constantForces[a+b]={id:b,body:c,x:d,y:e}},_clearConstantForce:function(a,b){delete this._constantForces[a+b]},gravity:function(a){a!==undefined&&this._world.SetGravity(new d(0,a));var b=this._world.GetGravity();return{x:b.x,y:b.y}},createEntity:function(){var a={},d=Array.prototype.slice.call(arguments);d.reverse();for(var e in d)c(a,d[e]);if(this._world.IsLocked()){this._creationQueue.push(a);return}var f=b(r),g=this._nextEntityId++;return f._init(this,a,g),this._entities[g]=f,f},createJoint:function(a,b,d){d=d||{},d=c(d,o);var e=d.type,f;e==="distance"?f=new Box2D.Dynamics.Joints.b2DistanceJointDef:e==="revolute"?f=new Box2D.Dynamics.Joints.b2RevoluteJointDef:e==="gear"?f=new Box2D.Dynamics.Joints.b2GearJointDef:e==="friction"?f=new Box2D.Dynamics.Joints.b2FrictionJointDef:e==="prismatic"?f=new Box2D.Dynamics.Joints.b2PrismaticJointDef:e==="weld"?f=new Box2D.Dynamics.Joints.b2WeldJointDef:e==="pulley"?f=new Box2D.Dynamics.Joints.b2PulleyJointDef:e==="mouse"?f=new Box2D.Dynamics.Joints.b2MouseJointDef:e==="line"&&(f=new Box2D.Dynamics.Joints.b2LineJointDef),d.enableMotor&&(f.enableMotor=!0);var g=a._body.GetWorldCenter();d.jointPositionOnEntity1&&(g.x+=d.jointPositionOnEntity1.x,g.y+=d.jointPositionOnEntity1.y);var h=b._body.GetWorldCenter();d.jointPositionOnEntity2&&(h.x+=d.jointPositionOnEntity2.x,h.y+=d.jointPositionOnEntity2.y),e==="mouse"?(f.bodyA=a._body,f.bodyB=b._body):f.Initialize&&f.Initialize(a._body,b._body,g,h),d.allowCollisions&&(f.collideConnected=!0),this._world.CreateJoint(f)},find:function(a,b,c,d){c===undefined&&(c=a),d===undefined&&(d=b);var e=this,f=[],g=new m;return g.lowerBound.Set(a,b),g.upperBound.Set(c,d),this._world.QueryAABB(function(a){return f.push(e._entities[a.GetBody()._bbid]),!0},g),f},camera:function(a){a=a||{};if(a.x===undefined&&a.y===undefined)return{x:this._cameraX,y:this._cameraY};a.x!==undefined&&(this._cameraX=a.x),a.y!==undefined&&(this._cameraY=a.y)},onRender:function(a){this._onRender.push({fun:a,ctx:this})},unbindOnRender:function(a){var b=[],c;for(c=0;c<this._onRender.length;c++)this._onRender[c].fun!==a&&b.push(this._onRender[c]);this._onRender=b},onTick:function(a){this._onTick.push({fun:a,ctx:this})},unbindOnTick:function(a){var b=[],c;for(c=0;c<this._onTick.length;c++)this._onTick[c].fun!==a&&b.push(this._onTick[c]);this._onTick=b},scale:function(a){return a!==undefined&&(this._scale=a),this._scale},canvasPositionAt:function(a,b){var c=this.camera(),d=this.scale();return{x:Math.round((a+ -c.x)*d),y:Math.round((b+ -c.y)*d)}}},q={name:"unnamed object",x:10,y:5,type:"dynamic",shape:"square",height:1,width:1,radius:1,points:[{x:0,y:0},{x:2,y:0},{x:0,y:2}],density:2,friction:1,restitution:.2,active:!0,rotation:null,fixedRotation:!1,bullet:!1,maxVelocityX:1e3,maxVelocityY:1e3,image:null,imageOffsetX:0,imageOffsetY:0,imageStretchToFit:null,color:"gray",borderColor:"black",borderWidth:1,spriteSheet:!1,spriteWidth:16,spriteHeight:16,spriteX:0,spriteY:0,init:null,draw:function(a,b,c){var d=-this._world._cameraX,f=-this._world._cameraY;a.fillStyle=this._ops.color,a.strokeStyle=this._ops.borderColor,a.lineWidth=this._ops.borderWidth;var g,h=this._world._scale,i=this._world._ops.collisionOutlines,j=this._ops.imageOffsetX||0,k=this._ops.imageOffsetY||0;j*=h,k*=h;if(this._sprite!==undefined){var l,m;this._ops.shape==="circle"&&this._ops.imageStretchToFit?(l=m=this._ops.radius*2,b-=this._ops.radius/2*h,c-=this._ops.radius/2*h):this._ops.imageStretchToFit?(l=this._ops.width,m=this._ops.height):this._ops.spriteSheet?(l=this._ops.spriteWidth/30,m=this._ops.spriteHeight/30):(l=this._sprite.width/30,m=this._sprite.height/30);var n=j+(b+l/4*h),o=k+(c+m/4*h);a.translate(n,o),a.rotate(this._body.GetAngle()),this._ops.spriteSheet?a.drawImage(this._sprite,this._ops.spriteX*this._ops.spriteWidth,this._ops.spriteY*this._ops.spriteHeight,this._ops.spriteWidth,this._ops.spriteHeight,-(l/2*h),-(m/2*h),l*h,m*h):a.drawImage(this._sprite,-(l/2*h),-(m/2*h),l*h,m*h),a.rotate(0-this._body.GetAngle()),a.translate(-n,-o)}if(this._sprite&&!i)return;i&&(this._sprite!==undefined&&(a.fillStyle="transparent"),a.strokeStyle="rgb(255, 0, 255)",a.lineWidth=2);if(this._ops.shape==="polygon"||this._ops.shape==="square"){var p=this._body.GetFixtureList().GetShape(),q=parseInt(p.GetVertexCount(),10),r=p.GetVertices(),s=new Vector(q),t=this._body.m_xf;for(g=0;g<q;++g)s[g]=e.MulX(t,r[g]);a.beginPath(),a.moveTo((d+s[0].x)*h,(f+s[0].y)*h);for(g=1;g<s.length;g++)a.lineTo((d+s[g].x)*h,(f+s[g].y)*h);a.closePath(),(this._ops.borderWidth!==0||i)&&a.stroke(),a.fill()}else if(this._ops.shape==="circle"){var u=this.position();a.beginPath(),a.arc((d+u.x)*h,(f+u.y)*h,this._ops.radius*h,0,Math.PI*2,!0),a.closePath(),(this._ops.borderWidth!==0||i)&&a.stroke(),a.fill()}}},r={_id:null,_ops:null,_body:null,_world:null,_init:function(b,d,e){var i,j;d&&d.components!==undefined&&(d.components.reverse(),d.components.forEach(function(a){c(d,a)})),this._ops=c(d,q),i=this._ops,this._body=new f;var l=this._body;this._world=b,this._id=e;for(j in this._ops)j.match(/^\$/)&&(this[j]=this._ops[j]);var m=new h;m.density=i.density,m.friction=i.friction,m.restitution=i.restitution,l.position.x=i.x,l.position.y=i.y,this._name=i.name,i.type==="static"?l.type=g.b2_staticBody:i.type==="dynamic"&&(l.type=g.b2_dynamicBody),i.shape==="square"?(m.shape=new k.b2PolygonShape,m.shape.SetAsBox(i.width/2,i.height/2)):i.shape==="circle"?m.shape=new k.b2CircleShape(i.radius):i.shape==="polygon"&&(m.shape=new k.b2PolygonShape,m.shape.SetAsArray(i.points,i.points.length)),i.rotation&&(l.angle=i.rotation/a),i.draw&&(this._draw=i.draw),i.image&&(this._sprite=new Image,this._sprite.src=i.image),l.active=i.active,l.fixedRotation=i.fixedRotation,l.bullet=i.bullet,this._body=b._world.CreateBody(l),this._body.CreateFixture(m),this._body._bbid=e,i.onStartContact&&this._world._addStartContactHandler(e,i.onStartContact),i.onFinishContact&&this._world._addFinishContactHandler(e,i.onFinishContact),i.onImpact&&this._world._addImpactHandler(e,i.onImpact),i.onKeyDown&&this._world._addKeydownHandler(e,i.onKeyDown),i.onKeyUp&&this._world._addKeyupHandler(e,i.onKeyUp),i.onRender&&this.onRender(i.onRender),i.onTick&&this.onTick(i.onTick),i.init&&i.init.call(this)},_toVector:function(a,b,c){var d,e;return b=b||0,c===undefined?(b-=90,d=Math.cos(b*(Math.PI/180))*a,e=Math.sin(b*(Math.PI/180))*a):(d=b*a,e=c*a),{x:d,y:e}},name:function(a){return a!==undefined&&(this._name=a),this._name},position:function(a){a!==undefined&&(this._world._world.IsLocked()?this._world._positionQueue.push({o:this,val:a}):this._body.SetPosition(new d(a.x,a.y)));var b=this._body.GetPosition();return{x:b.x,y:b.y}},canvasPosition:function(a){a===undefined;var b=this.position();return this._world.canvasPositionAt(b.x,b.y)},rotation:function(b){return b!==undefined&&this._body.SetAngle(b/a),this._body.GetAngle()*a},friction:function(a){return a!==undefined&&this._body.GetFixtureList().SetFriction(a),this._body.GetFixtureList().GetFriction()},restitution:function(a){return a!==undefined&&this._body.GetFixtureList().SetRestitution(a),this._body.GetFixtureList().GetRestitution()},maxVelocityX:function(a){return a!==undefined&&(this._ops.maxVelocityX=a),this._ops.maxVelocityX},maxVelocityY:function(a){return a!==undefined&&(this._ops.maxVelocityY=a),this._ops.maxVelocityY},image:function(a){return a!==undefined&&(this._sprite=new Image,this._sprite.src=a),this._sprite.src},imageOffsetX:function(a){return a!==undefined&&(this._ops.imageOffsetX=a),this._ops.imageOffsetX},imageOffsetY:function(a){return a!==undefined&&(this._ops.imageOffsetY=a),this._ops.imageOffsetY},imageStretchToFit:function(a){return a!==undefined&&(this._ops.imageStretchToFit=a),this._ops.imageStretchToFit},color:function(a){return a!==undefined&&(this._ops.color=a),this._ops.color},borderColor:function(a){return a!==undefined&&(this._ops.borderColor=a),this._ops.borderColor},borderWidth:function(a){return a!==undefined&&(this._ops.borderWidth=a),this._ops.borderWidth},spriteSheet:function(a){return a!==undefined&&(this._ops.spriteSheet=a),this._ops.spriteSheet},spriteWidth:function(a){return a!==undefined&&(this._ops.spriteWidth=a),this._ops.spriteWidth},spriteHeight:function(a){return a!==undefined&&(this._ops.spriteHeight=a),this._ops.spriteHeight},draw:function(a){return a!==undefined&&(this._draw=a),this._draw},destroy:function(){this._destroyed=!0,this._world._destroy(this)},applyImpulse:function(a,b,c){var d=this._toVector(a,b,c);this._world._applyImpulse(this._id,this._body,d.x,d.y)},setForce:function(a,b,c,d){var e=this._toVector(b,c,d);this._world._setConstantForce(a,this._id,this._body,e.x,e.y)},setVelocity:function(a,b,c,d){var e=this._toVector(b,c,d);this._world._setConstantVelocity(a,this._id,this._body,e.x,e.y)},clearForce:function(a){this._world._clearConstantForce(a,this._id)},clearVelocity:function(a){this._world._clearConstantVelocity(a,this._id)},onKeydown:function(a){this._world._addKeydownHandler(this._id,a)},onKeyup:function(a){this._world._addKeyupHandler(this._id,a)},onStartContact:function(a){this._world._addStartContactHandler(this._id,a)},onFinishContact:function(a){this._world._addFinishContactHandler(this._id,a)},onImpact:function(a){this._world._addImpactHandler(this._id,a)},onRender:function(a){this._world._onRender.push({fun:a,ctx:this})},onTick:function(a){this._world._onTick.push({fun:a,ctx:this})},sprite:function(a,b){this._ops.spriteX=a,this._ops.spriteY=b}}}();