aframe-gamepad-controls
Advanced tools
Comparing version 0.3.4 to 0.3.5
@@ -160,2 +160,11 @@ /******/ (function(modules) { // webpackBootstrap | ||
/** | ||
* Called on each iteration of main render loop. | ||
*/ | ||
tick: function () { | ||
this.updateRotation(); | ||
this.updatePosition(); | ||
this.updateButtonState(); | ||
}, | ||
/** | ||
* Called when a component is removed (e.g., via removeAttribute). | ||
@@ -162,0 +171,0 @@ * Generally undoes all modifications to the entity. |
@@ -1,1 +0,1 @@ | ||
!function(t){function e(n){if(i[n])return i[n].exports;var o=i[n]={exports:{},id:n,loaded:!1};return t[n].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var i={};return e.m=t,e.c=i,e.p="",e(0)}([function(t,e,i){!function(t){return t?void(t.aframeCore||t).registerComponent("gamepad-controls",i(1)):void console.error("Component attempted to register before AFRAME was available.")}(window.AFRAME)},function(t,e,i){var n=i(2),o=i(3),a=.2,s=Math.PI/2,r=.2;t.exports={GamepadButton:n,schema:{controller:{"default":0,oneOf:[0,1,2,3]},enabled:{"default":!0},movementEnabled:{"default":!0},lookEnabled:{"default":!0},flyEnabled:{"default":!1},easing:{"default":20},acceleration:{"default":65},sensitivity:{"default":.04},pitchAxis:{"default":"x",oneOf:["x","y","z"]},yawAxis:{"default":"y",oneOf:["x","y","z"]},rollAxis:{"default":"z",oneOf:["x","y","z"]},debug:{"default":!1}},init:function(){var t=this.el.sceneEl;this.prevTime=window.performance.now(),this.velocity=new THREE.Vector3(0,0,0),this.direction=new THREE.Vector3(0,0,0),this.pitch=new THREE.Object3D,this.yaw=new THREE.Object3D,this.yaw.position.y=10,this.yaw.add(this.pitch),this.buttons={},t.addBehavior(this),this.getGamepad()||console.warn("Gamepad #%d not found. Connect controller and press any button to continue.",this.data.controller)},update:function(t){this.updateRotation(),this.updatePosition(!!t),this.updateButtonState()},remove:function(){},updatePosition:function(t){var e=this.data,i=e.acceleration,n=e.easing,o=this.velocity,s=window.performance.now(),d=(s-this.prevTime)/1e3,u=e.rollAxis,c=e.pitchAxis,h=this.el,p=this.getGamepad();if(this.prevTime=s,t||d>a)return o[u]=0,void(o[c]=0);o[u]-=o[u]*n*d,o[c]-=o[c]*n*d;var l=h.getComputedAttribute("position");if(e.enabled&&e.movementEnabled&&p){var f=this.getDpad(),E=f.x||this.getJoystick(0).x,y=f.y||this.getJoystick(0).y;Math.abs(E)>r&&(o[c]+=E*i*d),Math.abs(y)>r&&(o[u]+=y*i*d)}var b=this.getMovementVector(d);h.object3D.translateX(b.x),h.object3D.translateY(b.y),h.object3D.translateZ(b.z),h.setAttribute("position",{x:l.x+b.x,y:l.y+b.y,z:l.z+b.z})},getMovementVector:function(){var t=new THREE.Euler(0,0,0,"YXZ");return function(e){var i=this.el.getAttribute("rotation");return this.direction.copy(this.velocity),this.direction.multiplyScalar(e),i?(this.data.flyEnabled||(i.x=0),t.set(THREE.Math.degToRad(i.x),THREE.Math.degToRad(i.y),0),this.direction.applyEuler(t),this.direction):this.direction}}(),updateRotation:function(){var t,e=new THREE.Vector3,i=new THREE.Vector3,n=new THREE.Vector3,o=0,a=0,d=1e-4,u=500;return function(){if(this.data.lookEnabled&&this.getGamepad()){if(t=Date.now(),e.copy(this.el.getAttribute("rotation")||e),e.distanceToSquared(i)>d&&e.distanceToSquared(n)>d)return i.copy(e),void(a=t);if(i.copy(e),!(u>t-a)){var c=this.getJoystick(1);Math.abs(c.x)<=r&&(c.x=0),Math.abs(c.y)<=r&&(c.y=0),a>o&&!c.lengthSq()||(c.multiplyScalar(this.data.sensitivity),this.yaw.rotation.y-=c.x,this.pitch.rotation.x-=c.y,this.pitch.rotation.x=Math.max(-s,Math.min(s,this.pitch.rotation.x)),this.el.setAttribute("rotation",{x:THREE.Math.radToDeg(this.pitch.rotation.x),y:THREE.Math.radToDeg(this.yaw.rotation.y),z:0}),n.copy(this.el.getAttribute("rotation")),o=t)}}}}(),updateButtonState:function(){var t=this.getGamepad();if(this.data.enabled&&t)for(var e=0;e<t.buttons.length;e++)t.buttons[e].pressed&&!this.buttons[e]?this.emit(new o("gamepadbuttondown",e,t.buttons[e])):!t.buttons[e].pressed&&this.buttons[e]&&this.emit(new o("gamepadbuttonup",e,t.buttons[e])),this.buttons[e]=t.buttons[e].pressed;else Object.keys(this.buttons)&&(this.buttons={})},emit:function(t){this.el.emit(t.type,t),this.el.emit(t.type+":"+t.index,new o(t.type,t.index,t))},getGamepad:function(){var t=navigator.getGamepads&&navigator.getGamepads()[this.data.controller],e=this.el.sceneEl.components["proxy-controls"],i=e&&e.isConnected()&&e.getGamepad(this.data.controller);return i||t},getButton:function(t){return this.getGamepad().buttons[t]},getAxis:function(t){return this.getGamepad().axes[t]},getJoystick:function(t){var e=this.getGamepad();switch(t){case 0:return new THREE.Vector2(e.axes[0],e.axes[1]);case 1:return new THREE.Vector2(e.axes[2],e.axes[3]);default:throw new Error('Unexpected joystick index "%d".',t)}},getDpad:function(){var t=this.getGamepad();return t.buttons[n.DPAD_RIGHT]?new THREE.Vector2((t.buttons[n.DPAD_RIGHT].pressed?1:0)+(t.buttons[n.DPAD_LEFT].pressed?-1:0),(t.buttons[n.DPAD_UP].pressed?-1:0)+(t.buttons[n.DPAD_DOWN].pressed?1:0)):new THREE.Vector2},isConnected:function(){var t=this.getGamepad();return!(!t||!t.connected)},getID:function(){return this.getGamepad().id}}},function(t,e){t.exports=Object.assign(function(){},{FACE_1:0,FACE_2:1,FACE_3:2,FACE_4:3,L_SHOULDER_1:4,R_SHOULDER_1:5,L_SHOULDER_2:6,R_SHOULDER_2:7,SELECT:8,START:9,DPAD_UP:12,DPAD_DOWN:13,DPAD_LEFT:14,DPAD_RIGHT:15,VENDOR:16})},function(t,e){function i(t,e,i){this.type=t,this.index=e,this.pressed=i.pressed,this.value=i.value}t.exports=i}]); | ||
!function(t){function e(n){if(i[n])return i[n].exports;var o=i[n]={exports:{},id:n,loaded:!1};return t[n].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var i={};return e.m=t,e.c=i,e.p="",e(0)}([function(t,e,i){!function(t){return t?void(t.aframeCore||t).registerComponent("gamepad-controls",i(1)):void console.error("Component attempted to register before AFRAME was available.")}(window.AFRAME)},function(t,e,i){var n=i(2),o=i(3),a=.2,s=Math.PI/2,r=.2;t.exports={GamepadButton:n,schema:{controller:{"default":0,oneOf:[0,1,2,3]},enabled:{"default":!0},movementEnabled:{"default":!0},lookEnabled:{"default":!0},flyEnabled:{"default":!1},easing:{"default":20},acceleration:{"default":65},sensitivity:{"default":.04},pitchAxis:{"default":"x",oneOf:["x","y","z"]},yawAxis:{"default":"y",oneOf:["x","y","z"]},rollAxis:{"default":"z",oneOf:["x","y","z"]},debug:{"default":!1}},init:function(){var t=this.el.sceneEl;this.prevTime=window.performance.now(),this.velocity=new THREE.Vector3(0,0,0),this.direction=new THREE.Vector3(0,0,0),this.pitch=new THREE.Object3D,this.yaw=new THREE.Object3D,this.yaw.position.y=10,this.yaw.add(this.pitch),this.buttons={},t.addBehavior(this),this.getGamepad()||console.warn("Gamepad #%d not found. Connect controller and press any button to continue.",this.data.controller)},update:function(t){this.updateRotation(),this.updatePosition(!!t),this.updateButtonState()},tick:function(){this.updateRotation(),this.updatePosition(),this.updateButtonState()},remove:function(){},updatePosition:function(t){var e=this.data,i=e.acceleration,n=e.easing,o=this.velocity,s=window.performance.now(),d=(s-this.prevTime)/1e3,u=e.rollAxis,c=e.pitchAxis,h=this.el,p=this.getGamepad();if(this.prevTime=s,t||d>a)return o[u]=0,void(o[c]=0);o[u]-=o[u]*n*d,o[c]-=o[c]*n*d;var l=h.getComputedAttribute("position");if(e.enabled&&e.movementEnabled&&p){var f=this.getDpad(),E=f.x||this.getJoystick(0).x,y=f.y||this.getJoystick(0).y;Math.abs(E)>r&&(o[c]+=E*i*d),Math.abs(y)>r&&(o[u]+=y*i*d)}var b=this.getMovementVector(d);h.object3D.translateX(b.x),h.object3D.translateY(b.y),h.object3D.translateZ(b.z),h.setAttribute("position",{x:l.x+b.x,y:l.y+b.y,z:l.z+b.z})},getMovementVector:function(){var t=new THREE.Euler(0,0,0,"YXZ");return function(e){var i=this.el.getAttribute("rotation");return this.direction.copy(this.velocity),this.direction.multiplyScalar(e),i?(this.data.flyEnabled||(i.x=0),t.set(THREE.Math.degToRad(i.x),THREE.Math.degToRad(i.y),0),this.direction.applyEuler(t),this.direction):this.direction}}(),updateRotation:function(){var t,e=new THREE.Vector3,i=new THREE.Vector3,n=new THREE.Vector3,o=0,a=0,d=1e-4,u=500;return function(){if(this.data.lookEnabled&&this.getGamepad()){if(t=Date.now(),e.copy(this.el.getAttribute("rotation")||e),e.distanceToSquared(i)>d&&e.distanceToSquared(n)>d)return i.copy(e),void(a=t);if(i.copy(e),!(u>t-a)){var c=this.getJoystick(1);Math.abs(c.x)<=r&&(c.x=0),Math.abs(c.y)<=r&&(c.y=0),a>o&&!c.lengthSq()||(c.multiplyScalar(this.data.sensitivity),this.yaw.rotation.y-=c.x,this.pitch.rotation.x-=c.y,this.pitch.rotation.x=Math.max(-s,Math.min(s,this.pitch.rotation.x)),this.el.setAttribute("rotation",{x:THREE.Math.radToDeg(this.pitch.rotation.x),y:THREE.Math.radToDeg(this.yaw.rotation.y),z:0}),n.copy(this.el.getAttribute("rotation")),o=t)}}}}(),updateButtonState:function(){var t=this.getGamepad();if(this.data.enabled&&t)for(var e=0;e<t.buttons.length;e++)t.buttons[e].pressed&&!this.buttons[e]?this.emit(new o("gamepadbuttondown",e,t.buttons[e])):!t.buttons[e].pressed&&this.buttons[e]&&this.emit(new o("gamepadbuttonup",e,t.buttons[e])),this.buttons[e]=t.buttons[e].pressed;else Object.keys(this.buttons)&&(this.buttons={})},emit:function(t){this.el.emit(t.type,t),this.el.emit(t.type+":"+t.index,new o(t.type,t.index,t))},getGamepad:function(){var t=navigator.getGamepads&&navigator.getGamepads()[this.data.controller],e=this.el.sceneEl.components["proxy-controls"],i=e&&e.isConnected()&&e.getGamepad(this.data.controller);return i||t},getButton:function(t){return this.getGamepad().buttons[t]},getAxis:function(t){return this.getGamepad().axes[t]},getJoystick:function(t){var e=this.getGamepad();switch(t){case 0:return new THREE.Vector2(e.axes[0],e.axes[1]);case 1:return new THREE.Vector2(e.axes[2],e.axes[3]);default:throw new Error('Unexpected joystick index "%d".',t)}},getDpad:function(){var t=this.getGamepad();return t.buttons[n.DPAD_RIGHT]?new THREE.Vector2((t.buttons[n.DPAD_RIGHT].pressed?1:0)+(t.buttons[n.DPAD_LEFT].pressed?-1:0),(t.buttons[n.DPAD_UP].pressed?-1:0)+(t.buttons[n.DPAD_DOWN].pressed?1:0)):new THREE.Vector2},isConnected:function(){var t=this.getGamepad();return!(!t||!t.connected)},getID:function(){return this.getGamepad().id}}},function(t,e){t.exports=Object.assign(function(){},{FACE_1:0,FACE_2:1,FACE_3:2,FACE_4:3,L_SHOULDER_1:4,R_SHOULDER_1:5,L_SHOULDER_2:6,R_SHOULDER_2:7,SELECT:8,START:9,DPAD_UP:12,DPAD_DOWN:13,DPAD_LEFT:14,DPAD_RIGHT:15,VENDOR:16})},function(t,e){function i(t,e,i){this.type=t,this.index=e,this.pressed=i.pressed,this.value=i.value}t.exports=i}]); |
@@ -97,2 +97,11 @@ /** | ||
/** | ||
* Called on each iteration of main render loop. | ||
*/ | ||
tick: function () { | ||
this.updateRotation(); | ||
this.updatePosition(); | ||
this.updateButtonState(); | ||
}, | ||
/** | ||
* Called when a component is removed (e.g., via removeAttribute). | ||
@@ -99,0 +108,0 @@ * Generally undoes all modifications to the entity. |
{ | ||
"name": "aframe-gamepad-controls", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"description": "Gamepad controls for A-Frame.", | ||
@@ -5,0 +5,0 @@ "main": "gamepad-controls.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
193628
952