diff --git a/src/components/haptic-feedback.js b/src/components/haptic-feedback.js index 1be3f792160c44b21dbbcf2c3467bc2de552e5b3..8a58a59e177713f1cea28175cbe3a3322001ad97 100644 --- a/src/components/haptic-feedback.js +++ b/src/components/haptic-feedback.js @@ -1,3 +1,9 @@ +const strengthForIntensity = { + low: 0.07, + medium: 0.2, + high: 1 +}; + AFRAME.registerComponent("haptic-feedback", { schema: { hapticEventName: { default: "haptic_pulse" } @@ -5,22 +11,28 @@ AFRAME.registerComponent("haptic-feedback", { init: function() { this.pulse = this.pulse.bind(this); - this.tryGetActuator = this.tryGetActuator.bind(this); - this.tryGetActuator(); + this.getActuator = this.getActuator.bind(this); + this.getActuator().then(actuator => { + this.actuator = actuator; + }); }, - tryGetActuator() { - var trackedControls = this.el.components["tracked-controls"]; - if ( - trackedControls && - trackedControls.controller && - trackedControls.controller.hapticActuators && - trackedControls.controller.hapticActuators.length > 0 - ) { - this.actuator = trackedControls.controller.hapticActuators[0]; - } else { - setTimeout(this.tryGetActuator, 1000); - } + getActuator() { + return new Promise((resolve, reject) => { + setTimeout(() => { + var trackedControls = this.el.components["tracked-controls"]; + if ( + trackedControls && + trackedControls.controller && + trackedControls.controller.hapticActuators && + trackedControls.controller.hapticActuators.length > 0 + ) { + resolve(trackedControls.controller.hapticActuators[0]); + } else { + return this.getActuator().then(x => resolve(x)); + } + }, 1000); + }); }, play: function() { @@ -31,28 +43,14 @@ AFRAME.registerComponent("haptic-feedback", { }, pulse: function(event) { - let { strength, duration, intensity } = event.detail; - switch (intensity) { - case "low": { - strength = 0.07; - duration = 12; - break; - } - case "medium": { - strength = 0.2; - duration = 12; - break; - } - case "high": { - strength = 1; - duration = 12; - break; - } - case "none": { - return; - } + let { intensity } = event.detail; + if (!strengthForIntensity[intensity]) { + console.warn(`Invalid intensity : ${intensity}`); + return; } - this.actuator.pulse(strength, duration); + if (this.actuator) { + this.actuator.pulse(strengthForIntensity[intensity], 15); + } } }); diff --git a/src/utils.js b/src/utils.js index 1c09c032f010a84eddfa67ad7197a3d3bc92b603..346f0994cf16943b7461a34e778cca16c87eebfc 100644 --- a/src/utils.js +++ b/src/utils.js @@ -191,19 +191,6 @@ export function parseJwt(token) { return JSON.parse(window.atob(base64)); } -export function angleToDirection(angle) { - angle = (angle * THREE.Math.RAD2DEG + 180 + 45) % 360; - if (angle > 0 && angle < 90) { - return "down"; - } else if (angle >= 90 && angle < 180) { - return "left"; - } else if (angle >= 180 && angle < 270) { - return "up"; - } else { - return "right"; - } -} - export function angleTo4Direction(angle) { angle = (angle * THREE.Math.RAD2DEG + 180 + 45) % 360; if (angle > 0 && angle < 90) {