Source: particles/particle.js

  1. /*
  2. * File: particle.js
  3. * Defines a particle
  4. */
  5. "use strict";
  6. import * as loop from "../core/loop.js";
  7. import * as particleSystem from "../components/particle_system.js";
  8. import PixelRenderable from "../renderables/pixel_renderable.js";
  9. import * as debugDraw from "../core/debug_draw.js";
  10. let kSizeFactor = 0.2;
  11. class Particle {
  12. /**
  13. * @constructor Particle
  14. * @param {float} x - The x coordinate of the position
  15. * @param {float} y - The y coordinate of the position
  16. * @param {float} life - The lifespan of the particle
  17. */
  18. constructor(x, y, life) {
  19. //this.mRenderComponent = new ParticleRenderable(texture);
  20. this.mRenderComponent = new PixelRenderable();
  21. this.setPosition(x, y);
  22. // position control
  23. this.mVelocity = vec2.fromValues(0, 0);
  24. this.mAcceleration = particleSystem.getSystemAcceleration();
  25. this.mDrag = .95;
  26. // Color control
  27. this.mDeltaColor = [0, 0, 0, 0];
  28. // Size control
  29. this.mSizeDelta = 0;
  30. // Life control
  31. this.mCyclesToLive = life;
  32. }
  33. drawMarker(aCamera) {
  34. let size = this.getSize();
  35. debugDraw.drawCrossMarker(aCamera, this.getPosition(), size[0] * kSizeFactor, [0, 1, 0, 1]);
  36. }
  37. /**
  38. * @function draw() - Draws the particle to the given camera
  39. * @param {Camera} aCamera - The camera to be drawn to
  40. */
  41. draw(aCamera) {
  42. this.mRenderComponent.draw(aCamera);
  43. }
  44. /**
  45. * @function terminate() - Sets the number of cycles to 0
  46. */
  47. terminate(){
  48. this.mCyclesToLive = 0;
  49. }
  50. /**
  51. * @function update() - Uses the position and color control variables to update the particle based on the update interval
  52. */
  53. update() {
  54. this.mCyclesToLive--;
  55. let dt = loop.getUpdateIntervalInSeconds();
  56. // Symplectic Euler
  57. // v += a * dt
  58. // x += v * dt
  59. let p = this.getPosition();
  60. vec2.scaleAndAdd(this.mVelocity, this.mVelocity, this.mAcceleration, dt);
  61. vec2.scale(this.mVelocity, this.mVelocity, this.mDrag);
  62. vec2.scaleAndAdd(p, p, this.mVelocity, dt);
  63. // update color
  64. let c = this.mRenderComponent.getColor();
  65. vec4.add(c, c, this.mDeltaColor);
  66. // update size
  67. let size = this.mRenderComponent.getSize();
  68. let s = size * this.mSizeDelta;
  69. this.mRenderComponent.setSize(s);
  70. }
  71. /**
  72. * @function hit() - Behavior of the particle upon collision
  73. */
  74. hit() {
  75. //Default behavior is to do nothing.
  76. }
  77. /**
  78. * @function setFinalColor() - Sets the final color of the particle
  79. * @param {vec2} f - the vector to subtract from the current color
  80. */
  81. setFinalColor = function(f) {
  82. vec4.sub(this.mDeltaColor, f, this.getColor());
  83. if (this.mCyclesToLive !== 0) {
  84. vec4.scale(this.mDeltaColor, this.mDeltaColor, 1/this.mCyclesToLive);
  85. }
  86. }
  87. /**
  88. * @function setColor() - Sets the current color to a new one
  89. * @param {vec4} c - The new color
  90. */
  91. setColor(c) { this.mRenderComponent.setColor(c); }
  92. /**
  93. * @function getColor() - Gets the current color
  94. * @returns {vec4} The current color
  95. */
  96. getColor() { return this.mRenderComponent.getColor(); }
  97. /**
  98. * @function getDrawBounds() - Returns whether or not the bounds are drawn
  99. * @returns {bool} - True if bounds are drawn, false otherwise
  100. */
  101. getDrawBounds() { return this.mDrawBounds; }
  102. /**
  103. * @function setDrawBounds() - Sets whether or not the drawing bounds are drawn
  104. * @param {bool} d - True if the bounds are to be drawn, false otherwise
  105. */
  106. setDrawBounds(d) { this.mDrawBounds = d; }
  107. /**
  108. * @function getPosition() - Gets the current position of the particle
  109. * @returns {vec2} The x and y coordinates of the particle's position
  110. */
  111. getPosition() { return this.mRenderComponent.getXform().getPosition(); }
  112. /**
  113. * @function setPosition() - Sets the position of the particle
  114. * @param {float} xPos - the x coordinate of the position
  115. * @param {float} yPos - the y coordinate of the position
  116. */
  117. setPosition(xPos, yPos) {
  118. this.mRenderComponent.getXform().setXPos(xPos);
  119. this.mRenderComponent.getXform().setYPos(yPos);
  120. }
  121. /**
  122. * @function getSize() - gets the size of the particle
  123. * @returns {vec2} The width and height of the particle
  124. */
  125. getSize() { return this.mRenderComponent.getSize(); }
  126. /**
  127. * @function setSize() - Sets the size of the particle
  128. * @param {vec2} s - The new size to assign to the particle
  129. */
  130. setSize(s) { this.mRenderComponent.setSize(s); }
  131. /**
  132. * @function getVelocity() - gets the current velocity
  133. * @returns {vec2} mVelocity - The current velocity
  134. */
  135. getVelocity() { return this.mVelocity; }
  136. /**
  137. * @function setVelocity() - Sets the current velocity
  138. * @param {float} x - Horizontal velocity
  139. * @param {float} y - Vertical velocity
  140. */
  141. setVelocity(x, y) {
  142. this.mVelocity[0] = x;
  143. this.mVelocity[1] = y;
  144. }
  145. /**
  146. * @function getAcceleration() - Gets the current acceleration
  147. * @returns {vec2} The current acceleration
  148. */
  149. getAcceleration() { return this.mAcceleration; }
  150. /**
  151. * @function setAcceleration() - Sets the current acceleration
  152. * @param {float} x - The horizontal acceleration
  153. * @param {float} y - The vertical acceleration
  154. */
  155. setAcceleration(x, y) {
  156. this.mAcceleration[0] = x;
  157. this.mAcceleration[1] = y;
  158. }
  159. /**
  160. * @function setDrag() - Sets the current drag to a new value
  161. * @param {float} d - The new drag
  162. */
  163. setDrag(d) { this.mDrag = d; }
  164. /**
  165. * @function getDrag() - Gets the current drag
  166. * @returns {float} mDrag - The current drag
  167. */
  168. getDrag() { return this.mDrag; }
  169. /**
  170. * @function setSizeDelta() - Sets the size delta
  171. * @param {float} d - the size delta
  172. */
  173. setSizeDelta(d) { this.mSizeDelta = d; }
  174. /**
  175. * @function hasExpired() - Checks if the particle has expired or not
  176. * @returns {bool} true if cycles to live ar eless than 0, false otherwise
  177. */
  178. hasExpired() { return (this.mCyclesToLive < 0); }
  179. }
  180. export default Particle;