Source: particles/ambient/snow_emitter.js

  1. import ParticleEmitter from "../particle_emitter.js";
  2. import SnowParticle from "./snow_particle.js";
  3. import engine from "../../index.js";
  4. class SnowEmitter extends ParticleEmitter{
  5. /**
  6. * @constructor SnowEmitter
  7. * @param {int} num - The number of particles to emit
  8. * @param {float} lifespan - The life of the emitter
  9. */
  10. constructor(num, lifespan) {
  11. super(0, 0, num);
  12. this.mLifespan = Date.now() + lifespan;
  13. this.mWind = 0;
  14. }
  15. /**
  16. * @function getWind() - Gets the current horizontal acceleration
  17. * @returns {float} mWind - The current horizontal acceleration
  18. */
  19. getWind(){
  20. return this.mWind;
  21. }
  22. /**
  23. * @function setWind() - Sets the horizontal acceleration
  24. * @param {float} val - The new horizontal acceleration
  25. */
  26. setWind(val){
  27. this.mWind = val;
  28. }
  29. /**
  30. * @function emitParticles() - Creates and emits particles based on the creator function
  31. * @param {ParticleSet} pSet - The set of particles
  32. */
  33. emitParticles(pSet){
  34. let numToEmit = this.mNumRemains;
  35. let i, p;
  36. for (i = 0; i < numToEmit; i++) {
  37. p = this.createSnow(this.mColorBegin, this.mColorEnd, this.mWind);
  38. pSet.addToSet(p);
  39. }
  40. if (Date.now() > this.mLifespan){
  41. this.mNumRemains = 0;
  42. }
  43. }
  44. /**
  45. * @function createSnow() - Creates snow particles
  46. * @param {vec4} colorStart - The starting color
  47. * @param {vec4} colorEnd - The ending color
  48. * @param {float} wind - The horizontal acceleration of the snow particles
  49. * @returns {SnowParticle} p - The new snow particle to be added to the set
  50. */
  51. createSnow(colorStart, colorEnd, wind) {
  52. let life = 500;
  53. let x = (Math.random()-.5) * 200;
  54. let y = 80 + Math.random();
  55. let p = new SnowParticle(x, y, life);
  56. p.setColor([colorStart[0],colorStart[1],colorStart[2],colorStart[3]]);
  57. // size of the particle
  58. let r = this.size + (Math.random()-.5) * this.variance;
  59. p.setSize(r, r);
  60. // final color
  61. p.setFinalColor(colorEnd);
  62. // velocity on the particle
  63. let fx = wind;
  64. let fy = -10;
  65. p.setVelocity(fx, fy);
  66. p.setAcceleration(0, 0);
  67. // size delta
  68. p.setSizeDelta(1);
  69. p.setDrag(1)
  70. return p;
  71. }
  72. }
  73. export default SnowEmitter;