diff --git a/package-lock.json b/package-lock.json index acbabb4..2439051 100644 --- a/package-lock.json +++ b/package-lock.json @@ -92,9 +92,9 @@ } }, "aframe-aabb-collider-component": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/aframe-aabb-collider-component/-/aframe-aabb-collider-component-2.2.1.tgz", - "integrity": "sha512-DejvmaP+1Em660oMEPU8C4SYmiRkn7bpq0ttBwqw/N3In8/QODI2S1RYuJHEkZWde6XCfAiSuBpOBcRTnbSn/w==" + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/aframe-aabb-collider-component/-/aframe-aabb-collider-component-3.0.0.tgz", + "integrity": "sha512-xWkE5S8+pET9IYOS95KV8ChrIIJxg3dCpiXS4j7SOKn0Yn3UZ5HHbI8mvQYtq95xuxi7kHD7kjloGrJ89FEQVg==" }, "aframe-atlas-uvs-component": { "version": "2.1.0", @@ -102,9 +102,9 @@ "integrity": "sha512-+zUnGMcj20Lkbth1/nWJjJoKsoc3jOTj8tuD3vy5BjWDha3cpQhBslxekvFL8LcAl88yv0WwD1kfkmLKQKcdvw==" }, "aframe-audioanalyser-component": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/aframe-audioanalyser-component/-/aframe-audioanalyser-component-5.2.0.tgz", - "integrity": "sha512-RuRvY24t/+lYIeisk6Yko7qXjoW9sfrRiV/QRR1m9W94YeXfSHtK9Zbc8jMlk25VApRpQtrr57iGOS+tru6K7A==" + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/aframe-audioanalyser-component/-/aframe-audioanalyser-component-5.2.1.tgz", + "integrity": "sha512-/nmpBL0j8+RQhxru0aLuuFD3wnLYm5XOKvOFFkjbTPCeLq9qaaKYrUZVqZVqkSOdu9yr3EUiGPlcBC3KXqPTYQ==" }, "aframe-event-decorators": { "version": "1.0.2", diff --git a/package.json b/package.json index 6a67abb..3dae9f9 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,9 @@ "start": "webpack-dev-server --host 0.0.0.0 --progress --colors --hot --inline --port 3000" }, "dependencies": { - "aframe-aabb-collider-component": "^2.2.1", + "aframe-aabb-collider-component": "^3.0.0", "aframe-atlas-uvs-component": "^2.1.0", - "aframe-audioanalyser-component": "^5.2.0", + "aframe-audioanalyser-component": "^5.2.1", "aframe-event-decorators": "^1.0.2", "aframe-event-set-component": "^4.0.1", "aframe-geometry-merger-component": "^2.0.0-beta1", diff --git a/src/components/wall-haptics.js b/src/components/wall-haptics.js new file mode 100644 index 0000000..7696fdc --- /dev/null +++ b/src/components/wall-haptics.js @@ -0,0 +1,37 @@ +/** + * Listen to aabb-collider event for wall haptics. + */ +AFRAME.registerComponent('wall-haptics', { + dependencies: ['aabb-collider'], + + init: function () { + const el = this.el; + this.isHittingWall = false; + el.setAttribute('haptics__wall', {dur: 50, force: 0.025}); + + this.checkIfHittingWall = this.checkIfHittingWall.bind(this); + el.addEventListener('hitstart', this.checkIfHittingWall); + el.addEventListener('hitend', this.checkIfHittingWall); + + this.tick = AFRAME.utils.throttleTick(this.tick.bind(this), 50); + }, + + /** + * On aabb-collider event, check if we are still hitting a wall. + */ + checkIfHittingWall: function () { + const intersectedEls = this.el.components['aabb-collider'].intersectedEls; + this.isHittingWall = false; + for (let i = 0; i < intersectedEls.length; i++) { + if (intersectedEls[i].components.wall) { + this.isHittingWall = true; + return; + } + } + }, + + tick: function () { + if (!this.isHittingWall) { return; } + this.el.components.haptics__wall.pulse(); + } +}); diff --git a/src/components/wall.js b/src/components/wall.js index 2eee555..3092dd3 100644 --- a/src/components/wall.js +++ b/src/components/wall.js @@ -1,3 +1,6 @@ +/** + * Wall speed and haptics. + */ AFRAME.registerComponent('wall', { schema: { speed: {default: 1.0} @@ -9,23 +12,29 @@ AFRAME.registerComponent('wall', { pause: function () { this.el.object3D.visible = false; - this.el.removeAttribute('collidable'); + this.el.removeAttribute('data-collidable-head'); + this.el.removeAttribute('data-collidable-saber'); }, play: function () { this.el.object3D.visible = true; - this.el.setAttribute('collidable', ''); + this.el.setAttribute('data-collidable-head', ''); + this.el.setAttribute('data-collidable-saber', ''); }, tock: function (time, delta) { this.el.object3D.position.z += this.data.speed * (delta / 1000); - if (this.el.object3D.position.z > this.maxZ) { this.returnToPool(); } + if (this.el.object3D.position.z > this.maxZ) { + this.returnToPool(); + return; + } }, returnToPool: function () { this.el.sceneEl.components.pool__wall.returnEntity(this.el); this.el.object3D.position.z = 9999; this.el.pause(); - this.el.removeAttribute('collidable'); + this.el.removeAttribute('data-collidable-head'); + this.el.removeAttribute('data-collidable-saber'); } }); diff --git a/src/index.html b/src/index.html index dc3bc45..0121217 100644 --- a/src/index.html +++ b/src/index.html @@ -93,7 +93,7 @@ + trail="color: {{ bladeColor }}; hand: {{ hand }}" + wall-haptics>