supertux
moving_sprite.hpp
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 
17 #ifndef HEADER_SUPERTUX_OBJECT_MOVING_SPRITE_HPP
18 #define HEADER_SUPERTUX_OBJECT_MOVING_SPRITE_HPP
19 
20 #include "math/anchor_point.hpp"
21 #include "sprite/sprite.hpp"
22 #include "sprite/sprite_ptr.hpp"
23 #include "supertux/moving_object.hpp"
24 #include "video/drawing_context.hpp"
25 #include "video/flip.hpp"
26 
27 class ReaderMapping;
28 
33 class MovingSprite : public MovingObject
34 {
35 public:
36  static void register_class(ssq::VM& vm);
37 
38 public:
39  MovingSprite(const Vector& pos,
40  const std::string& sprite_name,
41  int layer = LAYER_OBJECTS,
42  CollisionGroup collision_group = COLGROUP_MOVING);
43  MovingSprite(const ReaderMapping& reader,
44  const Vector& pos,
45  int layer = LAYER_OBJECTS,
46  CollisionGroup collision_group = COLGROUP_MOVING);
47  MovingSprite(const ReaderMapping& reader,
48  const std::string& sprite_name,
49  int layer = LAYER_OBJECTS,
50  CollisionGroup collision_group = COLGROUP_MOVING);
51  MovingSprite(const ReaderMapping& reader,
52  int layer = LAYER_OBJECTS,
53  CollisionGroup collision_group = COLGROUP_MOVING);
54 
55  virtual void draw(DrawingContext& context) override;
56  virtual void update(float dt_sec) override;
57  static std::string class_name() { return "moving-sprite"; }
58  virtual std::string get_class_name() const override { return class_name(); }
59  virtual std::string get_exposed_class_name() const override { return "MovingSprite"; }
60  virtual GameObjectClasses get_class_types() const override { return MovingObject::get_class_types().add(typeid(MovingSprite)); }
61 
62  virtual ObjectSettings get_settings() override;
63  virtual void after_editor_set() override;
64  virtual void on_type_change(int old_type) override;
65 
66  virtual int get_layer() const override { return m_layer; }
67 
68  bool has_found_sprite() const { return m_sprite_found; }
69  const std::string& get_sprite_name() const { return m_sprite_name; }
70  virtual std::string get_default_sprite_name() const { return m_default_sprite_name; }
71 
72  bool matches_sprite(const std::string& sprite_file) const;
73  bool change_sprite(const std::string& new_sprite_name);
74  void spawn_explosion_sprites(int count, const std::string& sprite_path);
75 
77  Sprite* get_sprite() const { return m_sprite.get(); }
78 
79 #ifdef DOXYGEN_SCRIPTING
80 
85  void set_sprite(const std::string& file);
90  std::string get_sprite() const;
91 #endif
92 
96  std::string get_action() const;
103  void set_action(const std::string& name);
111  void set_action_loops(const std::string& name, int loops);
112 
114  void set_action(const std::string& name, int loops);
115 
119  void set_action(const std::string& name, const Direction& dir, int loops = -1);
120 
124  void set_action(const Direction& dir, const std::string& name, int loops = -1);
125 
129  void set_action(const Direction& dir, int loops = -1);
130 
134  void set_action_centered(const std::string& action, int loops = -1);
135 
139  void set_action(const std::string& action, int loops, AnchorPoint anchorPoint);
140 
141 protected:
143  virtual void update_hitbox();
144 
145 protected:
146  std::string m_sprite_name;
147 
152  SpritePtr m_sprite;
153  int m_layer;
155  Flip m_flip;
156 
157 private:
159  bool m_sprite_found;
160 
162  const bool m_custom_layer;
163 
164 private:
165  MovingSprite(const MovingSprite&) = delete;
166  MovingSprite& operator=(const MovingSprite&) = delete;
167 };
168 
169 #endif
170 
171 /* EOF */
virtual void on_type_change(int old_type) override
Definition: moving_sprite.cpp:110
Definition: sprite.hpp:26
int m_layer
Sprite&#39;s z-position.
Definition: moving_sprite.hpp:153
void set_action_centered(const std::string &action, int loops=-1)
Set new action for sprite and re-center bounding box.
Definition: moving_sprite.cpp:181
std::string get_action() const
Returns the name of the current action of the sprite.
Definition: moving_sprite.cpp:127
Definition: object_settings.hpp:39
virtual void update(float dt_sec) override
This function is called once per frame and allows the object to update it&#39;s state.
Definition: moving_sprite.cpp:105
virtual void update_hitbox()
Update hitbox, based on sprite.
Definition: moving_sprite.cpp:133
std::string m_default_sprite_name
The default sprite for this MovingObject.
Definition: moving_sprite.hpp:151
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: moving_sprite.hpp:60
Sprite * get_sprite() const
Get various sprite properties.
Definition: moving_sprite.hpp:77
virtual void draw(DrawingContext &context) override
The GameObject should draw itself onto the provided DrawingContext if this function is called...
Definition: moving_sprite.cpp:99
void set_action(const std::string &name)
Sets the current action of the sprite and resizes the bounding box.
Definition: moving_sprite.cpp:140
Base class for all dynamic/moving game objects.
Definition: moving_object.hpp:35
void set_action_loops(const std::string &name, int loops)
Sets the current action of the sprite, as well as the number of times it should loop, and resizes the bounding box.
Definition: moving_sprite.cpp:147
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: moving_object.hpp:49
A helper structure to list all the type_indexes of the classes in the type hierarchy of a given class...
Definition: game_object.hpp:57
Definition: reader_mapping.hpp:32
Abstract base class for ""MovingObject""s, that are represented by a sprite.
Definition: moving_sprite.hpp:33
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42