r3-legacy/build/game-lib-min.js

3 lines
68 KiB
JavaScript
Raw Normal View History

2016-11-01 09:43:36 +01:00
function GameLib(){}if("undefined"==typeof GameLib.D3&&(GameLib.D3=function(){}),"undefined"==typeof Q){if("undefined"==typeof require)throw console.warn("You need the Q promise library for the GameLib.D3"),new Error("You need the Q promise library for the GameLib.D3");var Q=require("q")}GameLib.D3.BoneWeight=function(e,t){this.boneIndex=e,this.weight=t},GameLib.D3.Bone=function(e,t,i,n,s,o,a,r,h,c){this.id=e,this.name=i,this.boneId=t,"undefined"==typeof n&&(n=[]),this.childBoneIds=n,"undefined"==typeof s&&(s=null),this.parentBoneId=s,"undefined"==typeof o&&(o=new GameLib.D3.Vector4),this.quaternion=o,"undefined"==typeof a&&(a=new GameLib.D3.Vector3(0,0,0)),this.position=a,"undefined"==typeof r&&(r=new GameLib.D3.Vector3(0,0,0)),this.rotation=r,"undefined"==typeof h&&(h=new GameLib.D3.Vector3(1,1,1)),this.scale=h,"undefined"==typeof c&&(c=new GameLib.D3.Vector3(0,1,0)),this.up=c},GameLib.D3.Broadphase=function(e,t,i,n){this.id=e,"undefined"==typeof t&&(t="broadphase-"+i),this.name=t,"undefined"==typeof i&&(i=GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE),this.broadphaseType=i,this.engine=n,this.engine.isNotCannonThrow(),this.instance=this.createInstance()},GameLib.D3.Broadphase.prototype.createInstance=function(){var e=null;if(this.broadphaseType==GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE)e=new this.engine.instance.NaiveBroadphase;else if(this.broadphaseType==GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID)e=new this.engine.instance.GridBroadphase;else{if(this.broadphaseType!=GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP)throw console.warn("Unsupported broadphase type: "+this.broadphaseType),new Error("Unsupported broadphase type: "+this.broadphaseType);e=new this.engine.instance.SAPBroadphase}return e},GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE=1,GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID=2,GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP=3,GameLib.D3.Color=function(e,t,i,n){this.r=e,this.g=t,this.b=i,this.a=n},GameLib.D3.ComponentInterface=function(e){this.componentId=e||GameLib.D3.Tools.RandomId(),this.parentEntity=null},GameLib.D3.ComponentInterface.prototype.setParentEntity=function(e){this.parentEntity=e},GameLib.D3.ComponentInterface.prototype.onUpdate=function(e,t){},GameLib.D3.ComponentInterface.prototype.onLateUpdate=function(e,t){},GameLib.D3.ComponentInterface.prototype.onRegistered=function(e){},GameLib.D3.ComponentInterface.prototype.onSetParentEntity=function(e,t){},GameLib.D3.Engine=function(e,t){this.engineType=e,this.instance=t},GameLib.D3.Engine.prototype.isCannon=function(){return this.engineType==GameLib.D3.Engine.ENGINE_TYPE_CANNON},GameLib.D3.Engine.prototype.isNotCannonThrow=function(){if(this.engineType!=GameLib.D3.Engine.ENGINE_TYPE_CANNON)throw console.warn("Only CANNON supported for this function"),new Error("Only CANNON supported for this function")},GameLib.D3.Engine.prototype.isAmmo=function(){return this.engineType==GameLib.D3.Engine.ENGINE_TYPE_AMMO},GameLib.D3.Engine.prototype.isGoblin=function(){return this.engineType==GameLib.D3.Engine.ENGINE_TYPE_GOBLIN},GameLib.D3.Engine.ENGINE_TYPE_CANNON=1,GameLib.D3.Engine.ENGINE_TYPE_AMMO=2,GameLib.D3.Engine.ENGINE_TYPE_GOBLIN=3,GameLib.D3.Entity=function(e,t){this.meshId=e,"undefined"==typeof t&&(t=[]),this.componentIds=t,this.parentScene=null,this.mesh=null},GameLib.D3.Entity.prototype.onUpdate=function(e){for(var t in this.componentIds){var i=this.componentIds[t];this.parentScene.componentIdToComponent[i]&&this.parentScene.componentIdToComponent[i].onUpdate(e,this)}},GameLib.D3.Entity.prototype.onLateUpdate=function(e){for(var t in this.componentIds){var i=this.componentIds[t];this.parentScene.componentIdToComponent[i]&&this.parentScene.componentIdToComponent[i].onLateUpdate(e,this)}},GameLib.D3.Entity.prototype.onRegistered=function(e){this.parentScene=e,null!=this.meshId&&e.meshIdToMesh[this.meshId]&&(e.threeScene.add(e.meshIdToMesh[this.meshId]),this.mesh=e.meshIdToMesh[this.meshId])},GameLib.D3.Entity.prototype.addComponentId=function(e){this.componentIds.push(e)},GameLib.D3.Entity.prototype.addComponent=function(e){this.parentScene.registerComponent(e)
this.maxSuspensionTravel=E,"undefined"==typeof x&&(x=!1),this.useCustomSlidingRotationalSpeed=x,"undefined"==typeof _&&(_=-.1),this.customSlidingRotationalSpeed=_,this.instance=this.createInstance()},GameLib.D3.RaycastWheel.prototype.createInstance=function(){return new this.engine.instance.WheelInfo({chassisConnectionPointLocal:this.chassisConnectionPointLocal,chassisConnectionPointWorld:this.chassisConnectionPointWorld,directionLocal:this.directionLocal,directionWorld:this.directionWorld,axleLocal:this.axleLocal,axleWorld:this.axleWorld,suspensionRestLength:this.suspensionRestLength,suspensionMaxLength:this.suspensionMaxLength,radius:this.radius,suspensionStiffness:this.suspensionStiffness,dampingCompression:this.dampingCompression,dampingRelaxation:this.dampingRelaxation,frictionSlip:this.frictionSlip,steering:this.steering,rotation:this.rotation,deltaRotation:this.deltaRotation,rollInfluence:this.rollInfluence,maxSuspensionForce:this.maxSuspensionForce,isFrontWheel:this.isFrontWheel,clippedInvContactDotSuspension:this.clippedInvContactDotSuspension,suspensionRelativeVelocity:this.suspensionRelativeVelocity,suspensionForce:this.suspensionForce,skidInfo:this.skidInfo,suspensionLength:this.suspensionLength,maxSuspensionTravel:this.maxSuspensionTravel,useCustomSlidingRotationalSpeed:this.useCustomSlidingRotationalSpeed,customSlidingRotationalSpeed:this.customSlidingRotationalSpeed})},GameLib.D3.RigidBodyVehicle=function(e,t,i){this.componentId=GameLib.D3.Tools.RandomId(),this.engine=e,this.engine.isNotCannonThrow(),this.chassisBody=t,"undefined"==typeof i&&(i=[]),this.wheels=i,this.instance=this.createInstance()},GameLib.D3.RigidBodyVehicle.prototype.getWheelInfo=function(){return this.instance.wheelBodies},GameLib.D3.RigidBodyVehicle.prototype.createInstance=function(){return new this.engine.instance.RigidVehicle({chassisBody:this.chassisBody.instance})},GameLib.D3.RigidBodyVehicle.prototype.addWheel=function(e){this.wheels.push(e),this.instance.addWheel({body:e.body.instance,position:new this.engine.instance.Vec3(e.position.x,e.position.y,e.position.z),axis:new this.engine.instance.Vec3(e.axis.x,e.axis.y,e.axis.z),direction:new this.engine.instance.Vec3(e.direction.x,e.direction.y,e.direction.z)})},GameLib.D3.RigidBody=function(e,t,i,n,s,o,a,r,h,c,p,l,d,m,u,f){this.componentId=GameLib.D3.Tools.RandomId(),this.position=n||new GameLib.D3.Vector3,this.velocity=o||new GameLib.D3.Vector3,this.angularVelocity=a||new GameLib.D3.Vector3,this.quaternion=s||new GameLib.D3.Vector4(0,0,0,1),this.mass="undefined"==typeof t?0:t,this.friction="undefined"==typeof i?5:i,this.linearDamping="undefined"==typeof r?.01:r,this.angularDamping="undefined"==typeof h?.01:h,this.allowSleep="undefined"==typeof c||c,this.sleepSpeedLimit="undefined"==typeof p?.1:p,this.sleepTimeLimit="undefined"==typeof l?1:l,this.collisionFilterGroup="undefined"==typeof d?1:d,this.collisionFilterMask="undefined"==typeof m?1:m,this.fixedRotation="undefined"!=typeof u&&u,this.shape="undefined"==typeof f?null:f,this.engine=e,this.engine.isNotCannonThrow(),this.instance=this.createInstance()},GameLib.D3.RigidBody.prototype.createInstance=function(){var e=new this.engine.instance.Body({mass:mass,friction:this.friction,position:new this.engine.instance.Vec3(this.position.x,this.position.y,this.position.z),velocity:new this.engine.instance.Vec3(this.velocity.x,this.velocity.y,this.velocity.z),quaternion:new this.engine.instance.Quaternion(this.quaternion.x,this.quaternion.y,this.quaternion.z,this.quaternion.w),angularVelocity:new this.engine.instance.Vec3(this.angularVelocity.x,this.angularVelocity.y,this.angularVelocity.z),linearDamping:this.linearDamping,angularDamping:this.angularDamping,allowSleep:this.allowSleep,sleepSpeedLimit:this.sleepSpeedLimit,sleepTimeLimit:this.sleepTimeLimit,collisionFilterGroup:this.collisionFilterGroup,collisionFilterMask:this.collisionFilterMask,fixedRotation:this.fixedRotation,shape:this.shape.instance});return this.instance=e,e},GameLib.D3.RigidBody.prototype.addShape=function(e,t,i){t||(t=new GameLib.D3.Vector3(0,0,0)),i||(i=new GameLi
t.instance.getNormal(p,f);var y=new e.instance.ArrowHelper(new e.instance.Vector3(f.x,f.y,f.z),u,i,new e.instance.Color(f.x,f.y,f.z));r.add(y)}return r.scale.x=n.x,r.scale.y=n.y,r.scale.z=n.z,r},GameLib.D3.World.prototype.generateWireframeViewConvexPolyMesh=function(e,t,i,n,s,o){e.isNotThreeThrow(),this.engine.isNotCannonThrow(),"undefined"==typeof i&&(i=10),"undefined"==typeof n&&(n=new e.instance.Vector3(1,1,1)),"undefined"==typeof s&&(s=.5),"undefined"==typeof o&&(o=16711422);for(var a=new e.instance.Geometry,r=new e.instance.Mesh(a,new e.instance.MeshBasicMaterial({color:o,wireframe:!0,opacity:s})),h=0,c=t.instance.vertices.length;h<c;h++){var p=t.instance.vertices[h];a.vertices.push(new e.instance.Vector3(p.x,p.y,p.z))}for(var h=0,c=t.instance.faces.length;h<c;h++){var l=t.instance.faces[h],d=l[0],m=l[1],u=l[2];a.faces.push(new e.instance.Face3(d,m,u));var f=(new e.instance.Vector3).add(a.vertices[d]).add(a.vertices[m]).add(a.vertices[u]).divideScalar(3),y=t.instance.faceNormals[h],v=new e.instance.Vector3(y.x,y.y,y.z),L=new e.instance.ArrowHelper(v,f,i,new e.instance.Color(v.x,v.y,v.z));r.add(L)}return r.scale.x=n.x,r.scale.y=n.y,r.scale.z=n.z,r},GameLib.D3.World.prototype.generateTriangleMeshShapeDivided=function(e,t,i,n,s,o,a){e.isNotThreeThrow(),this.engine.isNotCannonThrow(),null!=i&&"undefined"!=typeof i||(i=0),null!=n&&"undefined"!=typeof n||(n=10),null!=s&&"undefined"!=typeof s||(s=!1);for(var r=0,h=o||0,c=a||0,p=t.geometry.faces.length,l=s?c*h:p,d=[],m=[],u=[],f=0;f<=p;f++){if(r==l||f==p){var y=null,v=new this.engine.instance.Trimesh(m,u);if(v.setScale(new this.engine.instance.Vec3(t.scale.x,t.scale.y,t.scale.z)),v.updateAABB(),v.updateNormals(),v.updateEdges(),v.updateBoundingSphereRadius(),v.updateTree(),y=new this.engine.instance.Body({mass:i,friction:n}),y.addShape(v),d.push({threeObject:s?null:t,physicsObject:y}),m=[],u=[],r=0,f==p)return d}var L=t.geometry.faces[f];u.push(u.length),u.push(u.length),u.push(u.length);var b=t.geometry.vertices[L.a],g=t.geometry.vertices[L.b],D=t.geometry.vertices[L.c];m.push(b.x,b.y,b.z),m.push(g.x,g.y,g.z),m.push(D.x,D.y,D.z),r++}},GameLib.D3.World.prototype.generateTriangleMeshShape=function(e,t){for(var i=[],n=0,s=t.geometry.vertices.length;n<s;n++){var o=t.geometry.vertices[n];i.push(new this.engine.instance.Vec3(o.x*t.scale.x,o.y*t.scale.y,o.z*t.scale.z))}for(var a=[],r=0,h=t.geometry.faces.length;r<h;r++){var c=t.geometry.faces[r].a,p=t.geometry.faces[r].b,l=t.geometry.faces[r].c;a.push([c,p,l])}for(var d={},m=[],u=[],f=0,y=a.length,v=0,r=0;r<y;r++){var c=a[r][0],p=a[r][1],l=a[r][2];"undefined"==typeof d[c]&&(m.push(i[c].x,i[c].y,i[c].z),d[c]=v,v++),"undefined"==typeof d[p]&&(m.push(i[p].x,i[p].y,i[p].z),d[p]=v,v++),"undefined"==typeof d[l]&&(m.push(i[l].x,i[l].y,i[l].z),d[l]=v,v++),u.push(d[c],d[p],d[l]),f++}var L=GameLib.D3.Shape(this.engine,GameLib.D3.Shape.SHAPE_TYPE_TRIMESH,t.scale,m,u),b=GameLib.D3.RigidBody(this.engine,0,12);b.addShape(L),this.addRigidBody(b);for(var g in t.children)this.generateTriangleMeshShape(e,t.children[g]);return b},GameLib.D3.World.prototype.fixupTriangleMeshShape=function(e,t,i,n,s,o,a){this.engine.isNotCannonThrow(),o.isNotThreeThrow(),null!=i&&"undefined"!=typeof i&&0!=i||(i=10),null!=n&&"undefined"!=typeof n||(n=13);var r=this.instance,h=new this.engine.instance.RaycastResult,c=[],p=[],l=0,d=0,m=0,u=[];for(var f in t){var y=t[f],v=[];l+=y.indices.length/3;for(var L=0;L<y.indices.length/3;L++){var b=y.indices[3*L],g=y.indices[3*L+1],D=y.indices[3*L+2],G=(new o.instance.Vector3).add(new o.instance.Vector3(y.vertices[3*b],y.vertices[3*b+1],y.vertices[3*b+2])).add(new o.instance.Vector3(y.vertices[3*g],y.vertices[3*g+1],y.vertices[3*g+2])).add(new o.instance.Vector3(y.vertices[3*D],y.vertices[3*D+1],y.vertices[3*D+2])).divideScalar(3),w=new this.engine.instance.Vec3;y.getNormal(L,w);var T=new this.engine.instance.Vec3(G.x+w.x,G.y+w.y,G.z+w.z),E=new this.engine.instance.Vec3(T.x-w.x*i,T.y-w.y*i,T.z-w.z*i);if(r.raycastClosest(T,E,{},h),a){var x=new o.instance.Geometry,_=new o.instance.Mesh(x,new o.instance.MeshBasicMaterial({color:167