pop.explosion.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. POP.Particle = function(x, y,r, col) {
  2. this.x = x;
  3. this.y = y;
  4. this.r = r;
  5. this.col = col || '#fff';
  6. this.remove = false;
  7. this.dir = ~~(Math.random() * 2);
  8. this.dir = (this.dir) ? 1 : -1;
  9. this.vx = ~~(Math.random() * 4) * this.dir;
  10. this.vy = ~~(Math.random() * 7) * 1;
  11. this.move = function() {
  12. this.x += this.vx;
  13. this.y += this.vy;
  14. this.vx *= 0.99;
  15. this.vy *= 0.99;
  16. this.vy -= 0.25;
  17. POP.draw.circle(this.x, this.y, this.r, this.col);
  18. if (this.y < 0) {
  19. this.remove = true;
  20. }
  21. };
  22. };
  23. POP.Explosion = function(x, y, r, num, cols) {
  24. this.x = x;
  25. this.y = y;
  26. this.finished = false;
  27. this.num = num || 6;
  28. this.cols = cols || ['#69a'];
  29. var totalCols = this.cols.length;
  30. var currCol = 0;
  31. this.r = ~~(r / num);
  32. this.r = 2;
  33. this.particles = [];
  34. for (i=0; i <= this.num; i++) {
  35. this.particles.push( new POP.Particle(x, y, this.r, this.cols[currCol]) );
  36. currCol = (currCol <= totalCols) ? currCol += 1 : 0;
  37. }
  38. this.move = function() {
  39. if (!this.particles.length) {
  40. this.finished = true;
  41. }
  42. for (i = 0; i < this.particles.length; i += 1) {
  43. this.particles[i].move();
  44. }
  45. for (i = 0; i < this.particles.length; i += 1) {
  46. if (this.particles[i].remove === true) {
  47. this.particles.splice(i, 1);
  48. }
  49. }
  50. };
  51. };