UNPKG

aframe-extras

Version:

Add-ons and examples for A-Frame VR.

87 lines (68 loc) 2.33 kB
const EPS = 0.1; AFRAME.registerComponent('checkpoint-controls', { schema: { enabled: {default: true}, mode: {default: 'teleport', oneOf: ['teleport', 'animate']}, animateSpeed: {default: 3.0} }, init: function () { this.active = true; this.checkpoint = null; this.isNavMeshConstrained = false; this.offset = new THREE.Vector3(); this.position = new THREE.Vector3(); this.targetPosition = new THREE.Vector3(); }, play: function () { this.active = true; }, pause: function () { this.active = false; }, setCheckpoint: function (checkpoint) { const el = this.el; if (!this.active) return; if (this.checkpoint === checkpoint) return; if (this.checkpoint) { el.emit('navigation-end', {checkpoint: this.checkpoint}); } this.checkpoint = checkpoint; this.sync(); // Ignore new checkpoint if we're already there. if (this.position.distanceTo(this.targetPosition) < EPS) { this.checkpoint = null; return; } el.emit('navigation-start', {checkpoint: checkpoint}); if (this.data.mode === 'teleport') { this.el.setAttribute('position', this.targetPosition); this.checkpoint = null; el.emit('navigation-end', {checkpoint: checkpoint}); el.components['movement-controls'].updateNavLocation(); } }, isVelocityActive: function () { return !!(this.active && this.checkpoint); }, getVelocity: function () { if (!this.active) return; const data = this.data; const offset = this.offset; const position = this.position; const targetPosition = this.targetPosition; const checkpoint = this.checkpoint; this.sync(); if (position.distanceTo(targetPosition) < EPS) { this.checkpoint = null; this.el.emit('navigation-end', {checkpoint: checkpoint}); return offset.set(0, 0, 0); } offset.setLength(data.animateSpeed); return offset; }, sync: function () { const offset = this.offset; const position = this.position; const targetPosition = this.targetPosition; position.copy(this.el.getAttribute('position')); this.checkpoint.object3D.getWorldPosition(targetPosition); targetPosition.add(this.checkpoint.components.checkpoint.getOffset()); offset.copy(targetPosition).sub(position); } });