Source: particles/localized/electricity_emitter.js

  1. /*
  2. * File: burst_emitter.js
  3. *
  4. */
  5. import ParticleEmitter from "../particle_emitter.js";
  6. import ElectricParticle from "./electric_particle.js";
  7. import engine from "../../../engine/index.js";
  8. "use strict";
  9. /**
  10. * @constructor ElectricityEmitter
  11. * @param {float} px - The x coordinate of the emitter's position
  12. * @param {float} py - the y coordinate of the emitter's position
  13. * @param {int} num - The number of particles to emit
  14. */
  15. class ElectricityEmitter extends ParticleEmitter {
  16. constructor(px, py, num) {
  17. super(px, py, num);
  18. this.mRadius = 60;
  19. this.mNumParticles = num;
  20. this.mNumRemains = 1;
  21. this.mSpacing = 6;
  22. this.mCount = 0;
  23. this.mDrag = 1;
  24. this.mAcceleration = vec2.fromValues(0, 0);
  25. this.mSpread = 2;
  26. }
  27. /**
  28. * @function getRadius() - Returns the current radius
  29. * @returns {float} mRadius - The radius of the electricity being emitted
  30. */
  31. getRadius() {
  32. return this.mRadius;
  33. }
  34. /**
  35. * @function setRadius() - Sets the radius to a new value
  36. * @param {float} r - The new value to assign to the radius
  37. */
  38. setRadius(r) {
  39. this.mRadius = r;
  40. }
  41. /**
  42. * @function getNumParticles() - gets the current number of particles to be emitted
  43. * @returns {int} mNumParticles - The number of particles to emit
  44. */
  45. getNumParticles() {
  46. return this.mNumParticles;
  47. }
  48. /**
  49. * @function setNumParticles() - Sets the number of particles to emit to a new value
  50. * @param {int} num - the new value to assign to mNumParticles
  51. */
  52. setNumParticles(num) {
  53. this.mNumParticles = num;
  54. }
  55. /**
  56. * @function getPulses() - gets the number of pulses for the electricity
  57. * @returns {int} mNumRemains - The number of pulses left
  58. */
  59. getPulses() {
  60. return this.mNumRemains;
  61. }
  62. /**
  63. * @function setPulses(pulses) - Sets the number of pulses
  64. * @param {int} pulses - the number of pulses to assign mNumRemains to
  65. */
  66. setPulses(pulses) {
  67. this.mNumRemains = pulses;
  68. }
  69. /**
  70. * @function getSpacing() - gets the spacing
  71. * @returns {int} mSpacing - The space between each ring in the burst
  72. */
  73. getSpacing() {
  74. return this.mSpacing;
  75. }
  76. /**
  77. * @function setSpacing() - sets the space between each ring in the burst
  78. * @param {int} s - the new value to set mSpacing to
  79. */
  80. setSpacing(s) {
  81. this.mSpacing = s;
  82. }
  83. /**
  84. * @function getDrag() - gets the current drag
  85. * @returns {float} mDrag - The drag for each particle
  86. */
  87. getDrag() {
  88. return this.mDrag;
  89. }
  90. /**
  91. * @function setDrag() - sets the drag for each particle being emitted
  92. * @param {float} d
  93. */
  94. setDrag(d) {
  95. this.mDrag = d;
  96. }
  97. /**
  98. * @function getAcceleration() - gets the current acceleration
  99. * @returns {float} mAcceleration - The current acceleration
  100. */
  101. getAcceleration() {
  102. return this.mAcceleration;
  103. }
  104. /**
  105. * @function setAcceleration() - updates the current acceleration
  106. * @param {float} a - The new value for acceleration
  107. */
  108. setAcceleration(a) {
  109. this.mAcceleration = a;
  110. }
  111. /**
  112. * @function getSpread() - Gets the spread of the particles
  113. * @returns {float} mSpread - the spread of the particles
  114. */
  115. getSpread(){
  116. return this.mSpread;
  117. }
  118. /**
  119. * @function setSpread() - Sets the current particle spread to a new value
  120. * @param {float} spread - The new particle spread
  121. */
  122. setSpread(spread){
  123. this.mSpread = spread;
  124. }
  125. /**
  126. * @function emitParticles() - uses the creator function to create and emit particles
  127. * @param {ParticleSet} pSet - The set of particles to emit
  128. */
  129. emitParticles(pSet) {
  130. if (this.mCount % this.mSpacing == 0){
  131. let i, p;
  132. let theta = 0;
  133. for (i = 0; i < this.mNumParticles; i++) {
  134. p = this.createBurst(this.mEmitPosition[0], this.mEmitPosition[1],this.mColorBegin,
  135. this.mColorEnd, theta, this.mRadius);
  136. pSet.addToSet(p);
  137. theta += (2*Math.PI) / (this.mNumParticles);
  138. }
  139. this.mNumRemains -= 1;
  140. }
  141. this.mCount++;
  142. }
  143. /**
  144. * @function createBurst() - Creator function used by the emitter, creates a particle and returns it.
  145. * Used for electricity effects.
  146. * @param {float} atX - x coordinate of the particle position
  147. * @param {float} atY - y coordinate of the particle position
  148. * @param {vec4} colorStart - the particle's initial color
  149. * @param {vec4} colorEnd - the particle's final color
  150. * @param {float} theta - the angle in which the particle will travel
  151. * @param {float} radius - radius of the burst
  152. * @returns {ElectricParticle} p - The newly created particle
  153. */
  154. createBurst(atX, atY, colorStart, colorEnd, theta, radius) {
  155. let life = 1 + Math.random() * 20;
  156. // size of the particle
  157. let r = this.size + (Math.random()-.5) * this.variance;
  158. let p = new ElectricParticle(atX, atY, life);
  159. p.setColor([colorStart[0],colorStart[1],colorStart[2],colorStart[3]]);
  160. p.setSize(r, r);
  161. // final color
  162. // let fr = 3.5 + Math.random();
  163. p.setFinalColor(colorEnd);
  164. let fx = Math.cos(theta) * radius;
  165. let fy = Math.sin(theta) * radius;
  166. // velocity on the particle
  167. p.setVelocity(fx, fy);
  168. p.setDrag(.9);
  169. p.setAcceleration(0, 0);
  170. // size delta
  171. p.setSizeDelta(.9);
  172. p.setSpread(this.mSpread);
  173. return p;
  174. }
  175. }
  176. export default ElectricityEmitter;