From 423bb55454a22884e7120964edb12c5a46e9399a Mon Sep 17 00:00:00 2001 From: Kevin Ngo Date: Tue, 30 Oct 2018 00:13:00 -0700 Subject: [PATCH] fix delayed haptics --- src/components/beat.js | 18 +++++++++++++++--- src/index.html | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/components/beat.js b/src/components/beat.js index f20e7c6..27455a7 100644 --- a/src/components/beat.js +++ b/src/components/beat.js @@ -587,6 +587,7 @@ AFRAME.registerComponent('beat', { if (!saberBoundingBox) { break; } const hand = saberEls[i].getAttribute('saber-controls').hand; + if (hitBoundingBox && saberBoundingBox.intersectsBox(hitBoundingBox)) { if (saberEls[i].components['saber-controls'].swinging && this.data.color === saberColors[hand]) { @@ -608,8 +609,14 @@ AFRAME.registerComponent('beat', { } else { this.wrongHit(hand); } + + // Notify for haptics. + this.el.emit(`beatcollide${this.hitHand}`, null, true); + + // Sound. this.el.parentNode.components['beat-hit-sound'].playSound( this.el, this.data.cutDirection); + if (this.data.type === 'mine') { this.destroyMine(); } else { @@ -619,6 +626,10 @@ AFRAME.registerComponent('beat', { } if (saberBoundingBox.intersectsBox(beatBoundingBox)) { + // Notify for haptics. + this.el.emit(`beatcollide${this.hitHand}`, null, true); + + // Sound. this.el.parentNode.components['beat-hit-sound'].playSound(this.el); if (this.data.type === 'mine') { @@ -650,10 +661,11 @@ AFRAME.registerComponent('beat', { }, onEndStroke: function () { - var saberControls = this.hitSaberEl.components['saber-controls']; - var maxAngle; var cutDirection = this.data.cutDirection; var hitEventDetail = this.hitEventDetail; + var maxAngle; + var saberControls = this.hitSaberEl.components['saber-controls']; + if (cutDirection === 'up' || cutDirection === 'down') { maxAngle = saberControls.maxAnglePlaneX; } else if (cutDirection === 'left' || cutDirection === 'right') { @@ -663,8 +675,8 @@ AFRAME.registerComponent('beat', { } hitEventDetail.angleBeforeHit = this.angleBeforeHit * 180 / Math.PI; hitEventDetail.angleAfterHit = maxAngle * 180 / Math.PI; + this.el.emit('beathit', hitEventDetail, true); - this.el.emit(`beathit${this.hitHand}`, null, true); }, /** diff --git a/src/index.html b/src/index.html index 2a17400..8a22139 100644 --- a/src/index.html +++ b/src/index.html @@ -118,7 +118,7 @@ bind-toggle__data-collidable-saber="isPlaying" aabb-collider="objects: [data-collidable-saber]; interval: 50; collideNonVisible: false" haptics="events: mouseenter; dur: 35; force: 0.075" - haptics__beat="eventsFrom: #beatContainer; events: beathit{{ hand }}; dur: 80; force: 0.2" + haptics__beat="eventsFrom: #beatContainer; events: beatcollide{{ hand }}; dur: 80; force: 0.2" haptics__draw="events: drawblade; dur: 750; force: 0.025" saber-controls="hand: {{ hand }}" haptics-saber