You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

94 rivejä
2.1 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #include <lol/engine-internal.h>
  11. namespace lol
  12. {
  13. /*
  14. * Emitter implementation class
  15. */
  16. class EmitterData
  17. {
  18. friend class Emitter;
  19. static const int MAX_PARTICLES = 1000;
  20. private:
  21. TileSet *tileset;
  22. vec3 gravity;
  23. int particles[MAX_PARTICLES];
  24. vec3 positions[MAX_PARTICLES];
  25. vec3 velocities[MAX_PARTICLES];
  26. int nparticles;
  27. };
  28. /*
  29. * Public Emitter class
  30. */
  31. Emitter::Emitter(TileSet *tileset, vec3 gravity)
  32. : data(new EmitterData())
  33. {
  34. data->tileset = tileset;
  35. data->gravity = gravity;
  36. data->nparticles = 0;
  37. }
  38. void Emitter::TickGame(float seconds)
  39. {
  40. for (int i = 0; i < data->nparticles; i++)
  41. {
  42. vec3 oldvelocity = data->velocities[i];
  43. data->velocities[i] += seconds * data->gravity;
  44. data->positions[i] += seconds * 0.5f
  45. * (oldvelocity + data->velocities[i]);
  46. if (data->positions[i].y < -100)
  47. {
  48. data->particles[i] = data->particles[data->nparticles - 1];
  49. data->positions[i] = data->positions[data->nparticles - 1];
  50. data->velocities[i] = data->velocities[data->nparticles - 1];
  51. data->nparticles--;
  52. }
  53. }
  54. Entity::TickGame(seconds);
  55. }
  56. void Emitter::TickDraw(float seconds, Scene &scene)
  57. {
  58. Entity::TickDraw(seconds, scene);
  59. for (int i = 0; i < data->nparticles; i++)
  60. scene.AddTile(data->tileset, data->particles[i],
  61. data->positions[i], 0, vec2(1.0f), 0.0f);
  62. }
  63. void Emitter::AddParticle(int id, vec3 pos, vec3 vel)
  64. {
  65. if (data->nparticles >= EmitterData::MAX_PARTICLES)
  66. return;
  67. data->particles[data->nparticles] = id;
  68. data->positions[data->nparticles] = pos;
  69. data->velocities[data->nparticles] = vel;
  70. data->nparticles++;
  71. }
  72. Emitter::~Emitter()
  73. {
  74. delete data;
  75. }
  76. } /* namespace lol */