emitter.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. var Emitter = function(options) {
  2. var defaults = {
  3. list: [],
  4. maxCount: 10,
  5. freq: .5,
  6. minLife: 2,
  7. maxLife: 4,
  8. objConst: null,
  9. emitTimer: 0,
  10. position: pt(0,0),
  11. constOpts: {},
  12. paused: false,
  13. minY: 50,// complete hack
  14. };
  15. var e = $.extend({}, defaults, options);
  16. for(x in e) this[x] = e[x];
  17. this.type = 'Emitter';
  18. this.init();
  19. }
  20. Emitter.prototype.render = function(ctx) {
  21. ctx.save();
  22. for(var i = 0; i < this.list.length; i++) {
  23. var o = this.list[i];
  24. ctx.setTransform(1, 0, 0, 1, 0, 0);
  25. ctx.translate(o.oPos.x, o.oPos.y);
  26. o.obj.render(ctx);
  27. }
  28. ctx.restore();
  29. };
  30. Emitter.prototype.frameMove = function(te) {
  31. this.emitTimer += te;
  32. this.checkEmit();
  33. var nl = [];
  34. for(var i = 0; i < this.list.length; i++) {
  35. var o = this.list[i];
  36. o.paramt += o.rate * te;
  37. if(o.paramt <= 1) {
  38. o.obj.frameMove(te, o.paramt);
  39. // hack
  40. if(o.obj.position.y + o.oPos.y < 50) continue;
  41. nl.push(o);
  42. }
  43. }
  44. this.list = nl;
  45. };
  46. Emitter.prototype.checkEmit = function() {
  47. //paused
  48. if(this.paused) return;
  49. // maxed out
  50. if(this.list.length >= this.maxCount) return;
  51. // not time yet
  52. if(this.emitTimer < this.freq) return;
  53. // emit one
  54. this.emitTimer -= this.freq;
  55. this.emit();
  56. }
  57. Emitter.prototype.emit = function() {
  58. var tlife = this.minLife + (Math.random() * (this.maxLife - this.minLife));
  59. this.list.unshift({
  60. obj: new this.objConst(this.constOpts),
  61. oPos: ptc(this.position),
  62. paramt: 0,
  63. rate: 1/tlife,
  64. });
  65. };
  66. Emitter.prototype.init = function() {
  67. };
  68. var ImageSprite = function(options) {
  69. var defaults = {
  70. image: null,
  71. position: pt(0,0),
  72. scale: 1,
  73. };
  74. var e = $.extend({}, defaults, options);
  75. for(x in e) this[x] = e[x];
  76. this.type = 'ImageSprite';
  77. // this.init();
  78. }
  79. ImageSprite.prototype.render = function(ctx) {
  80. ctx.save();
  81. ctx.translate(this.position.x, this.position.y);
  82. ctx.scale(this.scale, this.scale);
  83. if(this.image)
  84. ctx.drawImage(this.image, 0, 0);
  85. else
  86. console.log(this.image);
  87. ctx.restore();
  88. };
  89. ImageSprite.prototype.frameMove = function(te, paramt) {
  90. this.position.y -= 80 * te;
  91. this.scale = .3 + (paramt * .7);
  92. };