moving_sprite.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #ifndef HEADER_SUPERTUX_OBJECT_MOVING_SPRITE_HPP
  17. #define HEADER_SUPERTUX_OBJECT_MOVING_SPRITE_HPP
  18. #include "math/anchor_point.hpp"
  19. #include "sprite/sprite.hpp"
  20. #include "sprite/sprite_ptr.hpp"
  21. #include "supertux/moving_object.hpp"
  22. #include "video/drawing_context.hpp"
  23. #include "video/flip.hpp"
  24. class ReaderMapping;
  25. /** Abstract base class for MovingObjects that are represented by a Sprite */
  26. class MovingSprite : public MovingObject
  27. {
  28. public:
  29. MovingSprite(const Vector& pos,
  30. const std::string& sprite_name,
  31. int layer = LAYER_OBJECTS,
  32. CollisionGroup collision_group = COLGROUP_MOVING);
  33. MovingSprite(const ReaderMapping& reader,
  34. const Vector& pos,
  35. int layer = LAYER_OBJECTS,
  36. CollisionGroup collision_group = COLGROUP_MOVING);
  37. MovingSprite(const ReaderMapping& reader,
  38. const std::string& sprite_name,
  39. int layer = LAYER_OBJECTS,
  40. CollisionGroup collision_group = COLGROUP_MOVING);
  41. MovingSprite(const ReaderMapping& reader,
  42. int layer = LAYER_OBJECTS,
  43. CollisionGroup collision_group = COLGROUP_MOVING);
  44. virtual void draw(DrawingContext& context) override;
  45. virtual void update(float dt_sec) override;
  46. static std::string class_name() { return "moving-sprite"; }
  47. virtual std::string get_class_name() const override { return class_name(); }
  48. virtual ObjectSettings get_settings() override;
  49. virtual void after_editor_set() override;
  50. virtual void on_type_change(int old_type) override;
  51. virtual int get_layer() const override { return m_layer; }
  52. bool has_found_sprite() const { return m_sprite_found; }
  53. const std::string& get_sprite_name() const { return m_sprite_name; }
  54. virtual std::string get_default_sprite_name() const { return m_default_sprite_name; }
  55. bool matches_sprite(const std::string& sprite_file) const;
  56. bool change_sprite(const std::string& new_sprite_name);
  57. void spawn_explosion_sprites(int count, const std::string& sprite_path);
  58. /** Get various sprite properties. **/
  59. Sprite* get_sprite() const { return m_sprite.get(); }
  60. const std::string& get_action() const { return m_sprite->get_action(); }
  61. /** Set new action for sprite and resize bounding box. use with
  62. care as you can easily get stuck when resizing the bounding box. */
  63. void set_action(const std::string& name, int loops = -1);
  64. /** Sets the action from an action name and a particular direction
  65. in the form of "name-direction", eg. "walk-left".
  66. */
  67. void set_action(const std::string& name, const Direction& dir, int loops = -1);
  68. /** Sets the action from an action name and a particular direction
  69. in the form of "direction-name", eg. "left-up".
  70. */
  71. void set_action(const Direction& dir, const std::string& name, int loops = -1);
  72. /** Sets the action from a string from a particular direction
  73. in the form of "direction", e.g. "left".
  74. */
  75. void set_action(const Direction& dir, int loops = -1);
  76. /** Set new action for sprite and re-center bounding box. use with
  77. care as you can easily get stuck when resizing the bounding
  78. box. */
  79. void set_action_centered(const std::string& action, int loops = -1);
  80. /** Set new action for sprite and align bounding boxes at
  81. anchorPoint. use with care as you can easily get stuck when
  82. resizing the bounding box. */
  83. void set_action(const std::string& action, int loops, AnchorPoint anchorPoint);
  84. protected:
  85. /** Update hitbox, based on sprite. */
  86. virtual void update_hitbox();
  87. protected:
  88. std::string m_sprite_name;
  89. /** The default sprite for this MovingObject.
  90. NOTE: Unless in a constructor, use get_default_sprite_name() instead,
  91. so support for sprite switching for object types is retained. */
  92. std::string m_default_sprite_name;
  93. SpritePtr m_sprite;
  94. int m_layer; /**< Sprite's z-position. Refer to video/drawing_context.hpp for sensible values. */
  95. Flip m_flip;
  96. private:
  97. /** A custom sprite has been successfully found and set on initialization. */
  98. bool m_sprite_found;
  99. /** A custom layer has been specified. */
  100. const bool m_custom_layer;
  101. private:
  102. MovingSprite(const MovingSprite&) = delete;
  103. MovingSprite& operator=(const MovingSprite&) = delete;
  104. };
  105. #endif
  106. /* EOF */