Skip to content

Commit

Permalink
refactoring Quest-related code
Browse files Browse the repository at this point in the history
  • Loading branch information
stemkoski committed Feb 24, 2022
1 parent efc7d2e commit b738012
Show file tree
Hide file tree
Showing 10 changed files with 596 additions and 559 deletions.
153 changes: 82 additions & 71 deletions js/controller-listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,99 @@ AFRAME.registerComponent('controller-listener', {

schema:
{
hand: {type: 'string', default: "right"},
leftControllerId: {type: 'string', default: "#left-controller"},
rightControllerId: {type: 'string', default: "#right-controller"},
},

init: function()
{
// common to both controllers
this.leftController = document.querySelector(this.data.leftControllerId);
this.rightController = document.querySelector(this.data.rightControllerId);

this.leftAxisX = 0;
this.leftAxisY = 0;
this.leftTrigger = {pressed: false, pressing: false, released: false, value: 0};
this.leftGrip = {pressed: false, pressing: false, released: false, value: 0};

this.axisX = 0;
this.axisY = 0;
this.trigger = {pressed: false, pressing: false, released: false, value: 0};
this.grip = {pressed: false, pressing: false, released: false, value: 0};
this.rightAxisX = 0;
this.rightAxisY = 0;
this.rightTrigger = {pressed: false, pressing: false, released: false, value: 0};
this.rightGrip = {pressed: false, pressing: false, released: false, value: 0};

// only on one controller
this.buttonA = {pressed: false, pressing: false, released: false};
this.buttonB = {pressed: false, pressing: false, released: false};
this.buttonX = {pressed: false, pressing: false, released: false};
this.buttonY = {pressed: false, pressing: false, released: false};

if (this.data.hand == "right")
{
this.buttonA = {pressed: false, pressing: false, released: false};
this.buttonB = {pressed: false, pressing: false, released: false};
}
// event listeners
let self = this;

if (this.data.hand == "left")
{
this.buttonX = {pressed: false, pressing: false, released: false};
this.buttonY = {pressed: false, pressing: false, released: false};
}
// left controller

// event listeners
this.leftController.addEventListener('thumbstickmoved', function(event)
{ self.leftAxisX = event.detail.x;
self.leftAxisY = event.detail.y; } );

let self = this;
this.leftController.addEventListener("triggerdown", function(event)
{ self.leftTrigger.pressed = true; } );
this.leftController.addEventListener("triggerup", function(event)
{ self.leftTrigger.released = true; } );
this.leftController.addEventListener('triggerchanged', function (event)
{ self.leftTrigger.value = event.detail.value; } );

this.el.addEventListener('thumbstickmoved', function(event)
{ self.axisX = event.detail.x;
self.axisY = event.detail.y; } );

this.el.addEventListener("triggerdown", function(event)
{ self.trigger.pressed = true; } );
this.el.addEventListener("triggerup", function(event)
{ self.trigger.released = true; } );
this.el.addEventListener('triggerchanged', function (event)
{ self.trigger.value = event.detail.value; } );

this.el.addEventListener("gripdown", function(event)
{ self.grip.pressed = true; } );
this.el.addEventListener("gripup", function(event)
{ self.grip.released = true; } );
this.el.addEventListener('gripchanged', function (event)
{ self.grip.value = event.detail.value; } );

if (this.data.hand == "right")
{
this.el.addEventListener("abuttondown", function(event)
{ self.buttonA.pressed = true; } );
this.el.addEventListener("abuttonup", function(event)
{ self.buttonA.released = true; } );

this.el.addEventListener("bbuttondown", function(event)
{ self.buttonB.pressed = true; } );
this.el.addEventListener("bbuttonup", function(event)
{ self.buttonB.released = true; } );
}

if (this.data.hand == "left")
{
this.el.addEventListener("xbuttondown", function(event)
this.leftController.addEventListener("gripdown", function(event)
{ self.leftGrip.pressed = true; } );
this.leftController.addEventListener("gripup", function(event)
{ self.leftGrip.released = true; } );
this.leftController.addEventListener('gripchanged', function (event)
{ self.leftGrip.value = event.detail.value; } );

this.leftController.addEventListener("xbuttondown", function(event)
{ self.buttonX.pressed = true; } );
this.el.addEventListener("xbuttonup", function(event)
this.leftController.addEventListener("xbuttonup", function(event)
{ self.buttonX.released = true; } );

this.el.addEventListener("ybuttondown", function(event)
this.leftController.addEventListener("ybuttondown", function(event)
{ self.buttonY.pressed = true; } );
this.el.addEventListener("ybuttonup", function(event)
this.leftController.addEventListener("ybuttonup", function(event)
{ self.buttonY.released = true; } );
}

// right controller

this.rightController.addEventListener('thumbstickmoved', function(event)
{ self.rightAxisX = event.detail.x;
self.rightAxisY = event.detail.y; } );

this.rightController.addEventListener("triggerdown", function(event)
{ self.rightTrigger.pressed = true; } );
this.rightController.addEventListener("triggerup", function(event)
{ self.rightTrigger.released = true; } );
this.rightController.addEventListener('triggerchanged', function (event)
{ self.rightTrigger.value = event.detail.value; } );

this.rightController.addEventListener("gripdown", function(event)
{ self.rightGrip.pressed = true; } );
this.rightController.addEventListener("gripup", function(event)
{ self.rightGrip.released = true; } );
this.rightController.addEventListener('gripchanged', function (event)
{ self.rightGrip.value = event.detail.value; } );

this.rightController.addEventListener("abuttondown", function(event)
{ self.buttonA.pressed = true; } );
this.rightController.addEventListener("abuttonup", function(event)
{ self.buttonA.released = true; } );

this.rightController.addEventListener("bbuttondown", function(event)
{ self.buttonB.pressed = true; } );
this.rightController.addEventListener("bbuttonup", function(event)
{ self.buttonB.released = true; } );
},

updateButtonState: function( stateObject )
{
// if button was recently pressed:
// first pressing becomes true, then on neck tick, pressed becomes false.
// on first tick, pressing becomes true,
// then on next tick, pressed becomes false.
if (stateObject.pressed)
{
if (!stateObject.pressing)
Expand All @@ -90,7 +104,8 @@ AFRAME.registerComponent('controller-listener', {
}

// if button was recently released:
// first pressing becomes false, then on next tick, released becomes false.
// on first tick, pressing becomes false,
// then on next tick, released becomes false.
if (stateObject.released)
{
if (stateObject.pressing)
Expand All @@ -102,20 +117,16 @@ AFRAME.registerComponent('controller-listener', {

tick: function()
{
this.updateButtonState( this.trigger );
this.updateButtonState( this.grip );
this.updateButtonState( this.leftTrigger );
this.updateButtonState( this.leftGrip );

if (this.data.hand == "right")
{
this.updateButtonState( this.buttonA );
this.updateButtonState( this.buttonB );
}
this.updateButtonState( this.rightTrigger );
this.updateButtonState( this.rightGrip );

if (this.data.hand == "left")
{
this.updateButtonState( this.buttonX );
this.updateButtonState( this.buttonY );
}
this.updateButtonState( this.buttonA );
this.updateButtonState( this.buttonB );
this.updateButtonState( this.buttonX );
this.updateButtonState( this.buttonY );
}

});
Expand Down
56 changes: 31 additions & 25 deletions js/player-move.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
AFRAME.registerComponent("player-move", {

schema:
{
controllerListenerId: {type: 'string', default: "#controller-data"},
},

init: function()
{
this.controllerData = document.querySelector(this.data.controllerListenerId).components["controller-listener"];

this.clock = new THREE.Clock();

this.moveSpeed = 1; // units per second
Expand All @@ -12,26 +19,25 @@ AFRAME.registerComponent("player-move", {
// used when getting world position of controllers
this.tempVector1 = new THREE.Vector3();
this.tempVector2 = new THREE.Vector3();

this.enabled = true;
},

tick: function()
{
// always update deltaTime!
let deltaTime = this.clock.getDelta();

// get data from quest controllers;
// requires the "controller-listener" component
let leftController = document.querySelector("#left-controller-entity");
let leftData = leftController.components["controller-listener"];

let rightController = document.querySelector("#right-controller-entity");
let rightData = rightController.components["controller-listener"];
if ( !this.enabled )
return;

// =====================================================================
// moving on horizontal (XZ) plane
// =====================================================================

// move with left joystick
let leftJoystickLength = Math.sqrt(leftData.axisX * leftData.axisX + leftData.axisY * leftData.axisY);
let leftJoystickLength = Math.sqrt(this.controllerData.leftAxisX * this.controllerData.leftAxisX +
this.controllerData.leftAxisY * this.controllerData.leftAxisY );

if ( leftJoystickLength > 0.001 )
{
Expand All @@ -40,14 +46,14 @@ AFRAME.registerComponent("player-move", {
this.el.sceneEl.camera.getWorldDirection(cameraDirection);
let cameraAngle = Math.atan2(cameraDirection.z, cameraDirection.x);

let leftJoystickAngle = Math.atan2(leftData.axisY, leftData.axisX);
let leftJoystickAngle = Math.atan2(this.controllerData.leftAxisY, this.controllerData.leftAxisX);

let moveAngle = cameraAngle + leftJoystickAngle;

let moveDistance = this.moveSpeed * deltaTime;

// move faster if pressing trigger at same time
moveDistance *= (1 + 9 * leftData.trigger.value);
moveDistance *= (1 + 9 * this.controllerData.leftTrigger.value);

// convert move distance and angle to right and forward amounts
// scale by magnitude of joystick press (smaller press moves player slower)
Expand All @@ -66,21 +72,21 @@ AFRAME.registerComponent("player-move", {

// press right joystick left/right to turn left/right by N degrees;
// joystick must return to rest/center position before turning again
if ( Math.abs(rightData.axisX) < 0.10 )
if ( Math.abs(this.controllerData.rightAxisX) < 0.10 )
{
this.turnReady = true;
}

if ( this.turnReady )
{
if ( rightData.axisX > 0.90 )
if ( this.controllerData.rightAxisX > 0.90 )
{
let rot = this.el.getAttribute("rotation");
rot.y -= this.turnAngle;
this.el.setAttribute("rotation", rot);
this.turnReady = false;
}
if ( rightData.axisX < -0.90 )
if ( this.controllerData.rightAxisX < -0.90 )
{
let rot = this.el.getAttribute("rotation");
rot.y += this.turnAngle;
Expand All @@ -95,20 +101,20 @@ AFRAME.registerComponent("player-move", {
// =====================================================================

// hold trigger + grab, then hold A/X to pull player
if ( rightData.trigger.pressing && rightData.grip.pressing )
if ( this.controllerData.rightTrigger.pressing && this.controllerData.rightGrip.pressing )
{
// let rightHandCurrentPos = rightController.getAttribute("position");
rightController.object3D.getWorldPosition(this.tempVector1);
// store rightHandCurrentPosition in tempVector1
this.controllerData.rightController.object3D.getWorldPosition(this.tempVector1);

if ( !rightData.buttonA.pressing )
if ( !this.controllerData.buttonA.pressing )
{
this.rightHandPreviousX = this.tempVector1.x;
this.rightHandPreviousY = this.tempVector1.y;
this.rightHandPreviousZ = this.tempVector1.z;
}
else // if ( rightData.buttonA.pressing )
else // if ( this.controllerData.buttonA.pressing )
{
// let playerPos = this.el.getAttribute("position");
// store playerPosition in tempVector2
this.el.object3D.getWorldPosition(this.tempVector2);

this.tempVector2.x -= (this.tempVector1.x - this.rightHandPreviousX);
Expand All @@ -126,20 +132,20 @@ AFRAME.registerComponent("player-move", {
}
}

if ( leftData.trigger.pressing && leftData.grip.pressing )
if ( this.controllerData.leftTrigger.pressing && this.controllerData.leftGrip.pressing )
{
// let leftHandCurrentPos = leftController.getAttribute("position");
leftController.object3D.getWorldPosition(this.tempVector1);
// store leftHandCurrentPosition in tempVector1
this.controllerData.leftController.object3D.getWorldPosition(this.tempVector1);

if ( !leftData.buttonX.pressing )
if ( !this.controllerData.buttonX.pressing )
{
this.leftHandPreviousX = this.tempVector1.x;
this.leftHandPreviousY = this.tempVector1.y;
this.leftHandPreviousZ = this.tempVector1.z;
}
else // if ( rightData.buttonX.pressing )
else // if ( this.controllerData..buttonX.pressing )
{
// let playerPos = this.el.getAttribute("position")
// store playerPosition in tempVector2
this.el.object3D.getWorldPosition(this.tempVector2);

this.tempVector2.x -= (this.tempVector1.x - this.leftHandPreviousX);
Expand Down
Loading

0 comments on commit b738012

Please sign in to comment.