moving_sprite.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SuperTux - MovingSprite Base Class
  2. // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "object/moving_sprite.hpp"
  17. #include <math.h>
  18. #include "editor/editor.hpp"
  19. #include "math/random.hpp"
  20. #include "math/util.hpp"
  21. #include "object/sprite_particle.hpp"
  22. #include "sprite/sprite_manager.hpp"
  23. #include "supertux/sector.hpp"
  24. #include "util/reader_mapping.hpp"
  25. #include "util/writer.hpp"
  26. MovingSprite::MovingSprite(const Vector& pos, const std::string& sprite_name_,
  27. int layer_, CollisionGroup collision_group) :
  28. m_sprite_name(sprite_name_),
  29. m_default_sprite_name(sprite_name_),
  30. m_sprite(SpriteManager::current()->create(m_sprite_name)),
  31. m_layer(layer_),
  32. m_flip(NO_FLIP),
  33. m_sprite_found(false),
  34. m_custom_layer(false)
  35. {
  36. m_col.m_bbox.set_pos(pos);
  37. update_hitbox();
  38. set_group(collision_group);
  39. }
  40. MovingSprite::MovingSprite(const ReaderMapping& reader, const Vector& pos, int layer_, CollisionGroup collision_group) :
  41. MovingSprite(reader, layer_, collision_group)
  42. {
  43. m_col.m_bbox.set_pos(pos);
  44. }
  45. MovingSprite::MovingSprite(const ReaderMapping& reader, const std::string& sprite_name_, int layer_, CollisionGroup collision_group) :
  46. MovingObject(reader),
  47. m_sprite_name(sprite_name_),
  48. m_default_sprite_name(sprite_name_),
  49. m_sprite(),
  50. m_layer(layer_),
  51. m_flip(NO_FLIP),
  52. m_sprite_found(false),
  53. m_custom_layer(reader.get("z-pos", m_layer))
  54. {
  55. reader.get("x", m_col.m_bbox.get_left());
  56. reader.get("y", m_col.m_bbox.get_top());
  57. m_sprite_found = reader.get("sprite", m_sprite_name);
  58. //Make the sprite go default when the sprite file is invalid or sprite change fails
  59. if (m_sprite_name.empty() || !change_sprite(m_sprite_name))
  60. {
  61. change_sprite(m_default_sprite_name);
  62. m_sprite_found = false;
  63. }
  64. set_group(collision_group);
  65. }
  66. MovingSprite::MovingSprite(const ReaderMapping& reader, int layer_, CollisionGroup collision_group) :
  67. MovingObject(reader),
  68. m_sprite_name(),
  69. m_default_sprite_name(),
  70. m_sprite(),
  71. m_layer(layer_),
  72. m_flip(NO_FLIP),
  73. m_sprite_found(false),
  74. m_custom_layer(reader.get("z-pos", m_layer))
  75. {
  76. reader.get("x", m_col.m_bbox.get_left());
  77. reader.get("y", m_col.m_bbox.get_top());
  78. m_sprite_found = reader.get("sprite", m_sprite_name);
  79. //m_default_sprite_name = m_sprite_name;
  80. m_sprite = SpriteManager::current()->create(m_sprite_name);
  81. update_hitbox();
  82. set_group(collision_group);
  83. }
  84. void
  85. MovingSprite::draw(DrawingContext& context)
  86. {
  87. m_sprite->draw(context.color(), get_pos(), m_layer, m_flip);
  88. }
  89. void
  90. MovingSprite::update(float )
  91. {
  92. }
  93. void
  94. MovingSprite::on_type_change(int old_type)
  95. {
  96. /** Don't change the sprite/layer to the default one for the current type,
  97. if this is the initial `on_type_change()` call, and a custom sprite/layer has just been loaded. */
  98. if (old_type >= 0 || !m_sprite_found)
  99. change_sprite(get_default_sprite_name());
  100. if (old_type >= 0 || !m_custom_layer)
  101. m_layer = get_layer();
  102. }
  103. bool
  104. MovingSprite::matches_sprite(const std::string& sprite_file) const
  105. {
  106. return m_sprite_name == sprite_file || m_sprite_name == "/" + sprite_file;
  107. }
  108. void
  109. MovingSprite::update_hitbox()
  110. {
  111. m_col.set_size(m_sprite->get_current_hitbox_width(), m_sprite->get_current_hitbox_height());
  112. m_col.set_unisolid(m_sprite->is_current_hitbox_unisolid());
  113. }
  114. void
  115. MovingSprite::set_action(const std::string& name, int loops)
  116. {
  117. m_sprite->set_action(name, loops);
  118. update_hitbox();
  119. }
  120. void
  121. MovingSprite::set_action(const std::string& name, const Direction& dir, int loops)
  122. {
  123. m_sprite->set_action(name, dir, loops);
  124. update_hitbox();
  125. }
  126. void
  127. MovingSprite::set_action(const Direction& dir, const std::string& name, int loops)
  128. {
  129. m_sprite->set_action(dir, name, loops);
  130. update_hitbox();
  131. }
  132. void
  133. MovingSprite::set_action(const Direction& dir, int loops)
  134. {
  135. m_sprite->set_action(dir, loops);
  136. update_hitbox();
  137. }
  138. void
  139. MovingSprite::set_action_centered(const std::string& action, int loops)
  140. {
  141. Vector old_size = m_col.m_bbox.get_size().as_vector();
  142. m_sprite->set_action(action, loops);
  143. update_hitbox();
  144. set_pos(get_pos() - (m_col.m_bbox.get_size().as_vector() - old_size) / 2.0f);
  145. }
  146. void
  147. MovingSprite::set_action(const std::string& action, int loops, AnchorPoint anchorPoint)
  148. {
  149. Rectf old_bbox = m_col.m_bbox;
  150. m_sprite->set_action(action, loops);
  151. update_hitbox();
  152. set_pos(get_anchor_pos(old_bbox, m_sprite->get_current_hitbox_width(),
  153. m_sprite->get_current_hitbox_height(), anchorPoint));
  154. }
  155. bool
  156. MovingSprite::change_sprite(const std::string& new_sprite_name)
  157. {
  158. m_sprite = SpriteManager::current()->create(new_sprite_name);
  159. m_sprite_name = new_sprite_name;
  160. update_hitbox();
  161. return SpriteManager::current()->last_load_successful();
  162. }
  163. ObjectSettings
  164. MovingSprite::get_settings()
  165. {
  166. ObjectSettings result = MovingObject::get_settings();
  167. result.add_sprite(_("Sprite"), &m_sprite_name, "sprite", get_default_sprite_name());
  168. result.add_int(_("Z-pos"), &m_layer, "z-pos");
  169. result.reorder({"sprite", "z-pos", "x", "y"});
  170. return result;
  171. }
  172. void
  173. MovingSprite::after_editor_set()
  174. {
  175. MovingObject::after_editor_set();
  176. std::string current_action = m_sprite->get_action();
  177. if (!change_sprite(m_sprite_name)) // If sprite change fails, change back to default.
  178. {
  179. change_sprite(get_default_sprite_name());
  180. }
  181. m_sprite->set_action(current_action);
  182. update_hitbox();
  183. }
  184. void
  185. MovingSprite::spawn_explosion_sprites(int count, const std::string& sprite_path)
  186. {
  187. for (int i = 0; i < count; i++) {
  188. Vector ppos = m_col.m_bbox.get_middle();
  189. float angle = graphicsRandom.randf(-math::PI_2, math::PI_2);
  190. float velocity = graphicsRandom.randf(350, 400);
  191. float vx = sinf(angle)*velocity;
  192. float vy = -cosf(angle)*velocity;
  193. Vector pspeed = Vector(vx, vy);
  194. Vector paccel = Vector(0, Sector::get().get_gravity()*10);
  195. Sector::get().add<SpriteParticle>(sprite_path,
  196. "default",
  197. ppos, ANCHOR_MIDDLE,
  198. pspeed, paccel,
  199. LAYER_OBJECTS-1);
  200. }
  201. }
  202. /* EOF */