supertux
badguy.hpp
1 // SuperTux
2 // Copyright (C) 2006 Matthias Braun <matze@braunis.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_BADGUY_BADGUY_HPP
18 #define HEADER_SUPERTUX_BADGUY_BADGUY_HPP
19 
20 #include "editor/object_option.hpp"
21 #include "object/moving_sprite.hpp"
22 #include "object/portable.hpp"
23 #include "supertux/physic.hpp"
24 #include "supertux/timer.hpp"
25 
26 enum class Direction;
27 class Player;
28 class Bullet;
29 
38 class BadGuy : public MovingSprite,
39  public Portable
40 {
41 public:
42  static void register_class(ssq::VM& vm);
43 
44 public:
45  BadGuy(const Vector& pos, const std::string& sprite_name, int layer = LAYER_OBJECTS,
46  const std::string& light_sprite_name = "images/objects/lightmap_light/lightmap_light-medium.sprite",
47  const std::string& ice_sprite_name = "images/creatures/overlays/iceoverlay/iceoverlay.sprite");
48  BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer = LAYER_OBJECTS,
49  const std::string& light_sprite_name = "images/objects/lightmap_light/lightmap_light-medium.sprite",
50  const std::string& ice_sprite_name = "images/creatures/overlays/iceoverlay/iceoverlay.sprite");
51  BadGuy(const ReaderMapping& reader, const std::string& sprite_name, int layer = LAYER_OBJECTS,
52  const std::string& light_sprite_name = "images/objects/lightmap_light/lightmap_light-medium.sprite",
53  const std::string& ice_sprite_name = "images/creatures/overlays/iceoverlay/iceoverlay.sprite");
54  BadGuy(const ReaderMapping& reader, const std::string& sprite_name, Direction default_direction, int layer = LAYER_OBJECTS,
55  const std::string& light_sprite_name = "images/objects/lightmap_light/lightmap_light-medium.sprite",
56  const std::string& ice_sprite_name = "images/creatures/overlays/iceoverlay/iceoverlay.sprite");
57 
60  virtual void draw(DrawingContext& context) override;
61 
64  virtual void update(float dt_sec) override;
65 
66  static std::string class_name() { return "badguy"; }
67  virtual std::string get_class_name() const override { return class_name(); }
68  virtual std::string get_exposed_class_name() const override { return "BadGuy"; }
69  static std::string display_name() { return _("Badguy"); }
70  virtual std::string get_display_name() const override { return display_name(); }
71  virtual GameObjectClasses get_class_types() const override { return MovingSprite::get_class_types().add(typeid(Portable)).add(typeid(BadGuy)); }
72 
73  virtual std::string get_overlay_size() const { return "1x1"; }
74 
75  virtual ObjectSettings get_settings() override;
76  virtual void after_editor_set() override;
77 
81  virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;
82 
85  virtual void collision_tile(uint32_t tile_attributes) override;
86 
89  virtual void kill_fall();
90 #ifdef DOXYGEN_SCRIPTING
91 
95  void kill();
96 #endif
97 
99  virtual void run_dead_script();
100 
103  virtual bool can_break() const { return false; }
104 
105  Vector get_start_position() const { return m_start_position; }
106  void set_start_position(const Vector& vec) { m_start_position = vec; }
107 
108  virtual void grab(MovingObject& object, const Vector& pos, Direction dir) override;
109  virtual void ungrab(MovingObject& object, Direction dir) override;
110  virtual bool is_portable() const override;
111 
116  virtual void ignite();
117 
119  virtual void extinguish();
120 
122  virtual bool is_flammable() const;
123 
125  bool is_ignited() const;
126 
128  virtual void freeze();
129 
131  virtual void unfreeze(bool melt = true);
132 
133  virtual bool is_freezable() const;
134 
137  virtual bool is_hurtable() const { return true; }
138 
141  virtual bool is_snipable() const { return false; }
142 
143  virtual bool always_active() const { return false; }
144 
145  bool is_frozen() const;
146 
147  bool is_in_water() const;
148 
150  virtual std::string get_water_sprite() const {
151  return "images/objects/water_drop/water_drop.sprite";
152  }
153 
155  virtual bool can_be_affected_by_wind() const;
156 
158  virtual void add_wind_velocity(const Vector& velocity, const Vector& end_speed);
159 
160  Physic& get_physic() { return m_physic; }
161 
162 protected:
163  enum State {
164  STATE_INIT,
165  STATE_INACTIVE,
166  STATE_ACTIVE,
167  STATE_SQUISHED,
168  STATE_FALLING,
169  STATE_BURNING,
170  STATE_MELTING,
171  STATE_GROUND_MELTING,
172  STATE_INSIDE_MELTING,
173  STATE_GEAR
174  };
175 
176 protected:
178  virtual HitResponse collision_player(Player& player, const CollisionHit& hit);
179 
181  virtual void collision_solid(const CollisionHit& hit) override;
182 
183  virtual void on_flip(float height) override;
184 
186  virtual HitResponse collision_badguy(BadGuy& other, const CollisionHit& hit);
187 
191  virtual bool collision_squished(GameObject& object);
192 
194  virtual HitResponse collision_bullet(Bullet& bullet, const CollisionHit& hit);
195 
197  virtual void active_update(float dt_sec);
198 
200  virtual void inactive_update(float dt_sec);
201 
203  virtual void initialize();
204 
208  virtual void activate();
209 
211  virtual void deactivate();
212 
214  virtual std::vector<Direction> get_allowed_directions() const;
215 
216  void kill_squished(GameObject& object);
217 
218  void set_state(State state);
219  State get_state() const { return m_state; }
220 
221  bool check_state_timer() {
222  return m_state_timer.check();
223  }
224 
226  Player* get_nearest_player() const;
227 
230  bool is_offscreen() const;
231 
234  bool might_fall(int height = 1) const;
235 
240  void update_on_ground_flag(const CollisionHit& hit);
241 
245  bool on_ground() const;
246 
250  Vector get_floor_normal() const;
251 
254  bool is_active() const;
255 
257  void set_colgroup_active(CollisionGroup group);
258 
259 private:
260  void try_activate();
261 
262 protected:
263  Physic m_physic;
264 
265 public:
268  bool m_countMe;
269 
270 protected:
273 
274  Vector m_start_position;
275 
277  Direction m_dir;
278 
280  Direction m_start_dir;
281 
282  bool m_frozen;
283  bool m_ignited;
284  bool m_in_water;
286  std::string m_dead_script;
288  float m_melting_time;
289 
290  SpritePtr m_lightsprite;
291  SpritePtr m_freezesprite;
292  bool m_glowing;
293  bool m_water_affected;
294 
295  Timer m_unfreeze_timer;
296 
297 private:
298  State m_state;
299 
302  bool m_is_active_flag;
303 
304  Timer m_state_timer;
305 
308  bool m_on_ground_flag;
309 
312  Vector m_floor_normal;
313 
315  CollisionGroup m_colgroup_active;
316 
317 private:
318  BadGuy(const BadGuy&) = delete;
319  BadGuy& operator=(const BadGuy&) = delete;
320 };
321 
322 #endif
323 
324 /* EOF */
virtual HitResponse collision_player(Player &player, const CollisionHit &hit)
Called when the badguy collided with a player.
Definition: badguy.cpp:552
Base class for moving sprites that can hurt the Player.
Definition: badguy.hpp:38
virtual HitResponse collision_badguy(BadGuy &other, const CollisionHit &hit)
Called when the badguy collided with another badguy.
Definition: badguy.cpp:589
virtual void deactivate()
called when the badguy has been deactivated
Definition: badguy.cpp:372
virtual void add_wind_velocity(const Vector &velocity, const Vector &end_speed)
Adds velocity from wind.
Definition: badguy.cpp:1191
virtual std::string get_display_name() const override
Returns the display name of the object, translated to the user&#39;s locale.
Definition: badguy.hpp:70
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: badguy.hpp:71
bool on_ground() const
Returns true if we touched ground in the past frame This only works if update_on_ground_flag() gets c...
Definition: badguy.cpp:873
virtual void ignite()
Kills the badguy by igniting it.
Definition: badguy.cpp:1057
virtual void on_flip(float height) override
When level is flipped vertically.
Definition: badguy.cpp:543
bool m_is_initialized
true if initialize() has already been called
Definition: badguy.hpp:272
virtual HitResponse collision_bullet(Bullet &bullet, const CollisionHit &hit)
Called when the badguy collided with a bullet.
Definition: badguy.cpp:613
virtual void initialize()
called immediately before the first call to initialize
Definition: badguy.cpp:362
virtual void activate()
called when the badguy has been activated.
Definition: badguy.cpp:367
virtual bool can_break() const
True if this badguy can break bricks or open bonusblocks in his current form.
Definition: badguy.hpp:103
Definition: bullet.hpp:29
Physics engine.
Definition: physic.hpp:27
bool is_active() const
Returns true if we were in STATE_ACTIVE at the beginning of the last call to update() ...
Definition: badguy.cpp:879
virtual void extinguish()
Called to revert a badguy when is_ignited() returns true.
Definition: badguy.cpp:1107
virtual void freeze()
Called when hit by an ice bullet, and is_freezable() returns true.
Definition: badguy.cpp:968
std::string m_dead_script
< true if the badguy is currently in water
Definition: badguy.hpp:286
virtual bool is_flammable() const
Returns whether to call ignite() when a badguy gets hit by a fire bullet.
Definition: badguy.cpp:1112
Player * get_nearest_player() const
returns a pointer to the nearest player or 0 if no player is available
Definition: badguy.cpp:858
An object that inherits from this object is considered "portable" and can be carried around by the pl...
Definition: portable.hpp:29
void set_colgroup_active(CollisionGroup group)
changes colgroup_active.
Definition: badguy.cpp:1124
Definition: object_settings.hpp:39
virtual bool is_snipable() const
Can enemy be sniped by sliding or swimboosting Tux? Returns false if enemy is spiky or too large...
Definition: badguy.hpp:141
virtual std::string get_water_sprite() const
Get melting particle sprite filename.
Definition: badguy.hpp:150
bool m_countMe
Count this badguy to the statistics? This value should not be changed during runtime.
Definition: badguy.hpp:268
virtual std::vector< Direction > get_allowed_directions() const
Returns a vector of directions the badguy can be set to.
Definition: badguy.cpp:377
virtual HitResponse collision(GameObject &other, const CollisionHit &hit) override
Called when a collision with another object occurred.
Definition: badguy.cpp:446
virtual void inactive_update(float dt_sec)
called each frame when the badguy is not activated.
Definition: badguy.cpp:407
bool m_ignited
true if this badguy is currently on fire
Definition: badguy.hpp:283
virtual void collision_tile(uint32_t tile_attributes) override
Called when a collision with tile with special attributes occurred.
Definition: badguy.cpp:412
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: moving_sprite.hpp:60
Vector get_floor_normal() const
Returns floor normal stored the last time when update_on_ground_flag was called and we touched someth...
Definition: badguy.cpp:885
This class is responsible for: Updating and drawing the object.
Definition: game_object.hpp:83
bool check()
returns true if a period (or more) passed since start call or last successful check ...
Definition: timer.cpp:39
bool is_offscreen() const
initial position of the enemy.
Definition: badguy.cpp:781
virtual bool can_be_affected_by_wind() const
Returns true if the badguy can currently be affected by wind.
Definition: badguy.cpp:1185
Base class for all dynamic/moving game objects.
Definition: moving_object.hpp:35
virtual bool is_hurtable() const
Return true if this badguy can be hurt by tiles with the attribute "hurts".
Definition: badguy.hpp:137
virtual void kill_fall()
Set the badguy to kill/falling state, which makes him falling of the screen (his sprite is turned ups...
Definition: badguy.cpp:678
Direction m_dir
The direction we currently face in.
Definition: badguy.hpp:277
virtual void active_update(float dt_sec)
called each frame when the badguy is activated.
Definition: badguy.cpp:383
virtual void update(float dt_sec) override
Called each frame.
Definition: badguy.cpp:183
virtual void grab(MovingObject &object, const Vector &pos, Direction dir) override
called each frame when the object has been grabbed.
Definition: badguy.cpp:891
Direction m_start_dir
The direction we initially faced in.
Definition: badguy.hpp:280
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
virtual void collision_solid(const CollisionHit &hit) override
Called when the badguy collided with solid ground.
Definition: badguy.cpp:516
bool is_ignited() const
Returns whether this badguys is currently on fire.
Definition: badguy.cpp:1118
virtual bool collision_squished(GameObject &object)
Called when the player hit the badguy from above.
Definition: badguy.cpp:597
Simple timer designed to be used in the update functions of objects.
Definition: timer.hpp:24
Definition: reader_mapping.hpp:32
Abstract base class for ""MovingObject""s, that are represented by a sprite.
Definition: moving_sprite.hpp:33
bool might_fall(int height=1) const
Returns true if we might soon fall at least height pixels.
Definition: badguy.cpp:836
This module contains methods controlling the player.
Definition: player.hpp:47
virtual void draw(DrawingContext &context) override
Called when the badguy is drawn.
Definition: badguy.cpp:140
void update_on_ground_flag(const CollisionHit &hit)
Update on_ground_flag judging by solid collision hit.
Definition: badguy.cpp:864
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
virtual void unfreeze(bool melt=true)
Called to unfreeze the badguy.
Definition: badguy.cpp:1006
virtual void run_dead_script()
Call this, if you use custom kill_fall() or kill_squashed(GameObject& object)
Definition: badguy.cpp:720
This class collects data about a collision.
Definition: collision_hit.hpp:44