sprite_particle.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #include "object/sprite_particle.hpp"
  18. #include "object/camera.hpp"
  19. #include "sprite/sprite.hpp"
  20. #include "sprite/sprite_manager.hpp"
  21. #include "supertux/sector.hpp"
  22. #include "video/video_system.hpp"
  23. #include "video/viewport.hpp"
  24. SpriteParticle::SpriteParticle(const std::string& sprite_name, const std::string& action,
  25. const Vector& position_, AnchorPoint anchor, const Vector& velocity_, const Vector& acceleration_,
  26. int drawing_layer_, bool notimeout, Color color_) :
  27. SpriteParticle(SpriteManager::current()->create(sprite_name), action,
  28. position_, anchor, velocity_, acceleration_,
  29. drawing_layer_, notimeout, color_)
  30. {
  31. if (sprite_name == "images/particles/sparkle.sprite")
  32. {
  33. glow = true;
  34. lightsprite->set_blend(Blend::ADD);
  35. if (action=="dark") {
  36. lightsprite->set_color(Color(0.1f, 0.1f, 0.1f));
  37. }
  38. else
  39. {
  40. lightsprite->set_color(color_);
  41. }
  42. }
  43. no_time_out = notimeout;
  44. sprite->set_color(color_);
  45. }
  46. SpriteParticle::SpriteParticle(SpritePtr sprite_, const std::string& action,
  47. const Vector& position_, AnchorPoint anchor, const Vector& velocity_, const Vector& acceleration_,
  48. int drawing_layer_, bool notimeout, Color color_) :
  49. sprite(std::move(sprite_)),
  50. position(position_),
  51. velocity(velocity_),
  52. acceleration(acceleration_),
  53. drawing_layer(drawing_layer_),
  54. lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-tiny.sprite")),
  55. glow(false),
  56. no_time_out(false),
  57. color(Color::WHITE)
  58. {
  59. sprite->set_action(action, 1);
  60. sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
  61. sprite->set_color(color_);
  62. position -= get_anchor_pos(sprite->get_current_hitbox(), anchor);
  63. no_time_out = notimeout;
  64. }
  65. SpriteParticle::~SpriteParticle()
  66. {
  67. remove_me();
  68. }
  69. void
  70. SpriteParticle::update(float dt_sec)
  71. {
  72. // die when animation is complete
  73. if (sprite->animation_done() && !no_time_out) {
  74. remove_me();
  75. return;
  76. }
  77. // calculate new position and velocity
  78. position.x += velocity.x * dt_sec;
  79. position.y += velocity.y * dt_sec;
  80. velocity.x += acceleration.x * dt_sec;
  81. velocity.y += acceleration.y * dt_sec;
  82. // die when too far offscreen
  83. Camera& camera = Sector::get().get_camera();
  84. if (!camera.get_rect().contains(position)) {
  85. remove_me();
  86. }
  87. }
  88. void
  89. SpriteParticle::draw(DrawingContext& context)
  90. {
  91. sprite->draw(context.color(), position, drawing_layer);
  92. //Sparkles glow in the dark
  93. if (glow)
  94. {
  95. sprite->draw(context.light(), position, drawing_layer);
  96. lightsprite->draw(context.light(), position + Vector(12, 12), 0);
  97. }
  98. }
  99. /* EOF */