Source: particles/particle_set.js

  1. /*
  2. * File: particle_set.js
  3. * a set of Particles
  4. *
  5. * Subclass of GameObjectSet:
  6. * GameObjectSet: a set of objects that support: update() and draw() functions
  7. * Particle satisfies!
  8. */
  9. "use strict";
  10. import * as glSys from "../core/gl.js";
  11. import GameObjectSet from "../game_objects/game_object_set.js";
  12. import FlameEmitter from "./localized/flame_emitter.js";
  13. import ParticleEmitter from "./particle_emitter.js";
  14. import RainEmitter from "./ambient/rain_emitter.js";
  15. import SnowEmitter from "./ambient/snow_emitter.js";
  16. import DustEmitter from "./ambient/dust_emitter.js";
  17. import BurstEmitter from "./localized/burst_emitter.js";
  18. import ElectricityEmitter from "./localized/electricity_emitter.js";
  19. /**
  20. * @constructor ParticleSet
  21. */
  22. class ParticleSet extends GameObjectSet {
  23. constructor() {
  24. super();
  25. this.mEmitterSet = [];
  26. }
  27. /**
  28. * @function draw() - Draws to the given camera.
  29. * @param {Camera} aCamera - The camera to draw to
  30. */
  31. draw(aCamera) {
  32. let gl = glSys.get();
  33. gl.blendFunc(gl.ONE, gl.ONE); // for additive blending!
  34. super.draw(aCamera);
  35. gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); // restore alpha blending
  36. }
  37. /**
  38. * @function addEmitterAt() - Adds a default emitter to the given position
  39. * @param {float} x - The x coordinate of the emitter position
  40. * @param {float} y - The y coordinate of the emitter position
  41. * @param {int} n - The number of particles to create in the emitter
  42. * @returns {ParticleEmitter} e - The newly added default emitter
  43. */
  44. addEmitterAt(x, y, n) {
  45. let e = new ParticleEmitter(x, y, n);
  46. this.mEmitterSet.push(e);
  47. return e;
  48. }
  49. /**
  50. * @function addFlameAt() - Adds a new flame emitter to the given position
  51. * @param {float} x - The x coordinate of the emitter position
  52. * @param {float} y - The y coordinate of the emitter position
  53. * @param {int} n - The number of particles to create in the emitter
  54. * @param {float} life - The lifespan of the emitter
  55. * @returns {FlameEmitter} e - The newly added flame emitter
  56. */
  57. addFlameAt(x,y,n,life) {
  58. let e = new FlameEmitter(x, y, n, life);
  59. this.mEmitterSet.push(e);
  60. return e;
  61. }
  62. /**
  63. * @function addRain() - Creates a new rain emitter
  64. * @param {int} n - The number of particles to emit
  65. * @param {float} life - The lifespan of the emitter
  66. * @returns {RainEmitter} e - The newly added rain emitter
  67. */
  68. addRain(n,life) {
  69. let e = new RainEmitter(n, life);
  70. this.mEmitterSet.push(e);
  71. return e;
  72. }
  73. /**
  74. * @function addSnow() - Creates a new snow emitter
  75. * @param {int} n - The number of particles to emit
  76. * @param {float} life - The lifespan of the emitter
  77. * @returns {SnowEmitter} e - The newly added snow emitter
  78. */
  79. addSnow(n,life) {
  80. let e = new SnowEmitter(n, life);
  81. this.mEmitterSet.push(e);
  82. return e;
  83. }
  84. /**
  85. * @function addDust() - Creates a new dust emitter
  86. * @param {int} n - The number of particles to emit
  87. * @param {float} life - The lifespan of the emitter
  88. * @returns {DustEmitter} e - The newly added dust emitter
  89. */
  90. addDust(n,life) {
  91. let e = new DustEmitter(n, life);
  92. this.mEmitterSet.push(e);
  93. return e;
  94. }
  95. /**
  96. * @function addBurstAt() - Adds a new burst emitter at the given position
  97. * @param {float} x - The x coordinate of the emitter position
  98. * @param {float} y - The y coordinate of the emitter position
  99. * @param {int} n - The number of particles to create in the emitter
  100. * @returns {BurstEmitter} e - The newly created burst emitter
  101. */
  102. addBurstAt(x,y,n) {
  103. let e = new BurstEmitter(x, y, n);
  104. this.mEmitterSet.push(e);
  105. return e;
  106. }
  107. /**
  108. * @function addElectricityAt() - Adds a new electric emitter at the given position
  109. * @param {float} x - The x coordinate of the emitter position
  110. * @param {float} y - The y coordinate of the emitter position
  111. * @param {int} n - The number of particles to create in the emitter
  112. * @returns {ElectricityEmitter} e - The newly created electric emitter
  113. */
  114. addElectricityAt(x,y,n) {
  115. let e = new ElectricityEmitter(x, y, n);
  116. this.mEmitterSet.push(e);
  117. return e;
  118. }
  119. drawMarkers(aCamera) {
  120. let i;
  121. for (i = 0; i < this.mSet.length; i++) {
  122. this.mSet[i].drawMarker(aCamera);
  123. }
  124. }
  125. /**
  126. * @function update() - Cleans up and emits particles in the set
  127. */
  128. update() {
  129. super.update();
  130. // Cleanup Particles
  131. let i, obj;
  132. for (i = 0; i < this.size(); i++) {
  133. obj = this.getObjectAt(i);
  134. if (obj.hasExpired()) {
  135. this.removeFromSet(obj);
  136. }
  137. }
  138. // Emit new particles
  139. for (i = 0; i < this.mEmitterSet.length; i++) {
  140. let e = this.mEmitterSet[i];
  141. e.emitParticles(this);
  142. if (e.expired()) { // delete the emitter when done
  143. this.mEmitterSet.splice(i, 1);
  144. }
  145. }
  146. }
  147. }
  148. export default ParticleSet;