Files
junisaber/src/components/beams.js

96 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-10-04 16:46:05 -07:00
const ANIME = AFRAME.ANIME || AFRAME.anime;
2018-10-03 02:30:51 +02:00
AFRAME.registerComponent('beams', {
schema: {
2018-10-08 17:30:13 -07:00
isPlaying: {default: false},
2018-10-04 02:32:28 +02:00
poolSize: {default: 3}
2018-10-03 02:30:51 +02:00
},
2018-10-08 17:15:30 -07:00
2018-10-03 02:30:51 +02:00
init: function () {
var redMaterial;
var blueMaterial;
var materialOptions;
var geo;
var beam;
2018-10-08 17:15:30 -07:00
this.beams = [];
2018-10-03 02:30:51 +02:00
this.redBeams = [];
this.blueBeams = [];
this.currentRed = 0;
this.currentBlue = 0;
materialOptions = {
color: 0xaa3333,
map: new THREE.TextureLoader().load('assets/img/beam.png'),
transparent: true,
blending: THREE.AdditiveBlending
};
redMaterial = new THREE.MeshBasicMaterial(materialOptions);
2018-10-09 21:36:35 +02:00
materialOptions.color = 0x3333aa;
2018-10-03 02:30:51 +02:00
blueMaterial = new THREE.MeshBasicMaterial(materialOptions);
geo = new THREE.PlaneBufferGeometry(0.4, 50).translate(0, 25, 0);
for (var j = 0; j < 2; j++) {
for (var i = 0; i < this.data.poolSize; i++) {
beam = new THREE.Mesh(geo, j === 0 ? redMaterial : blueMaterial);
beam.visible = false;
2018-10-08 17:15:30 -07:00
beam.animation = ANIME({
autoplay: false,
2018-10-04 02:32:28 +02:00
targets: beam.scale,
2018-10-03 18:57:02 -07:00
x: 0.00001,
2018-10-04 02:32:28 +02:00
duration: 300,
easing: 'easeInCubic',
2018-10-08 17:15:30 -07:00
complete: () => { beam.visible = false; }
2018-10-04 02:32:28 +02:00
});
2018-10-03 02:30:51 +02:00
this.el.object3D.add(beam);
this[j === 0 ? 'redBeams' : 'blueBeams'].push(beam);
2018-10-08 17:15:30 -07:00
this.beams.push(beam);
2018-10-03 02:30:51 +02:00
}
}
2018-10-08 17:15:30 -07:00
2018-10-08 17:30:13 -07:00
this.clearBeams = this.clearBeams.bind(this);
this.el.sceneEl.addEventListener('cleargame', this.clearBeams);
2018-10-08 17:15:30 -07:00
},
/**
* Use tick for manual ANIME animations, else it will create RAF.
*/
tick: function (t, dt) {
2018-10-08 17:30:13 -07:00
if (!this.data.isPlaying) { return; }
2018-10-08 17:15:30 -07:00
for (let i = 0; i < this.beams.length; i++) {
let beam = this.beams[i];
// Tie animation state to beam visibility.
if (!beam.visible) { continue; }
beam.time += dt;
beam.animation.tick(beam.time);
}
2018-10-03 02:30:51 +02:00
},
newBeam: function (color, position) {
var beam;
if (color === 'red') {
beam = this.redBeams[this.currentRed];
this.currentRed = (this.currentRed + 1) % this.redBeams.length;
2018-10-08 17:15:30 -07:00
// z offset to avoid z-fighting.
beam.position.set(position.x, position.y, position.z + this.currentRed / 50.0);
2018-10-03 02:30:51 +02:00
} else {
beam = this.blueBeams[this.currentBlue];
this.currentBlue = (this.currentBlue + 1) % this.blueBeams.length;
beam.position.set(position.x, position.y, position.z + this.currentBlue / 50.0);
}
beam.visible = true;
beam.scale.x = 1;
2018-10-08 17:15:30 -07:00
beam.time = 0;
},
2018-10-03 02:30:51 +02:00
2018-10-08 17:15:30 -07:00
clearBeams: function () {
for (let i = 0; i < this.beams.length; i++) {
this.beams[i].visible = false;
2018-10-08 17:30:13 -07:00
this.beams[i].time = 0;
this.beams[i].scale.x = 1;
2018-10-08 17:15:30 -07:00
}
}
2018-10-03 18:57:02 -07:00
});