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 "supertux/direction.hpp"
23 #include "supertux/physic.hpp"
24 #include "supertux/timer.hpp"
25 
26 class Dispenser;
27 class Player;
28 class Bullet;
29 
31 class BadGuy : public MovingSprite
32 {
33 public:
34  BadGuy(const Vector& pos, const std::string& sprite_name, int layer = LAYER_OBJECTS,
35  const std::string& light_sprite_name = "images/objects/lightmap_light/lightmap_light-medium.sprite");
36  BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer = LAYER_OBJECTS,
37  const std::string& light_sprite_name = "images/objects/lightmap_light/lightmap_light-medium.sprite");
38  BadGuy(const ReaderMapping& reader, const std::string& sprite_name, int layer = LAYER_OBJECTS,
39  const std::string& light_sprite_name = "images/objects/lightmap_light/lightmap_light-medium.sprite");
40 
43  virtual void draw(DrawingContext& context) override;
44 
47  virtual void update(float dt_sec) override;
48 
49  virtual std::string get_class() const override { return "badguy"; }
50  virtual std::string get_display_name() const override { return _("Badguy"); }
51 
52  virtual ObjectSettings get_settings() override;
53  virtual void after_editor_set() override;
54 
58  virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;
59 
62  virtual void collision_tile(uint32_t tile_attributes) override;
63 
66  virtual void kill_fall();
67 
69  virtual void run_dead_script();
70 
73  virtual bool can_break() const { return false; }
74 
75  Vector get_start_position() const { return m_start_position; }
76  void set_start_position(const Vector& vec) { m_start_position = vec; }
77 
79  virtual void ignite();
80 
82  virtual void extinguish();
83 
85  virtual bool is_flammable() const;
86 
88  bool is_ignited() const;
89 
91  virtual void freeze();
92 
94  virtual void unfreeze();
95 
96  virtual bool is_freezable() const;
97 
100  virtual bool is_hurtable() const { return true; }
101 
102  bool is_frozen() const;
103 
104  bool is_in_water() const;
105 
107  virtual std::string get_water_sprite() const {
108  return "images/objects/water_drop/water_drop.sprite";
109  }
110 
113  void set_parent_dispenser(Dispenser* parent) { m_parent_dispenser = parent; }
114 
116  Dispenser* get_parent_dispenser() const { return m_parent_dispenser; }
117 
118 protected:
119  enum State {
120  STATE_INIT,
121  STATE_INACTIVE,
122  STATE_ACTIVE,
123  STATE_SQUISHED,
124  STATE_FALLING,
125  STATE_BURNING,
126  STATE_MELTING,
127  STATE_GROUND_MELTING,
128  STATE_INSIDE_MELTING,
129  STATE_GEAR
130  };
131 
132 protected:
134  virtual HitResponse collision_player(Player& player, const CollisionHit& hit);
135 
137  virtual void collision_solid(const CollisionHit& hit) override;
138 
140  virtual HitResponse collision_badguy(BadGuy& other, const CollisionHit& hit);
141 
145  virtual bool collision_squished(GameObject& object);
146 
148  virtual HitResponse collision_bullet(Bullet& bullet, const CollisionHit& hit);
149 
151  virtual void active_update(float dt_sec);
152 
154  virtual void inactive_update(float dt_sec);
155 
157  virtual void initialize();
158 
162  virtual void activate();
163 
165  virtual void deactivate();
166 
167  void kill_squished(GameObject& object);
168 
169  void set_state(State state);
170  State get_state() const { return m_state; }
171 
172  bool check_state_timer() {
173  return m_state_timer.check();
174  }
175 
177  Player* get_nearest_player() const;
178 
181  bool is_offscreen() const;
182 
185  bool might_fall(int height = 1) const;
186 
188  Direction str2dir(const std::string& dir_str) const;
189 
194  void update_on_ground_flag(const CollisionHit& hit);
195 
199  bool on_ground() const;
200 
204  Vector get_floor_normal() const;
205 
208  bool is_active() const;
209 
211  void set_colgroup_active(CollisionGroup group);
212 
213 private:
214  void try_activate();
215 
216 protected:
217  Physic m_physic;
218 
219 public:
222  bool m_countMe;
223 
224 protected:
227 
228  Vector m_start_position;
229 
231  Direction m_dir;
232 
234  Direction m_start_dir;
235 
236  bool m_frozen;
237  bool m_ignited;
238  bool m_in_water;
240  std::string m_dead_script;
242  float m_melting_time;
243 
244  SpritePtr m_lightsprite;
245  bool m_glowing;
246 
247 private:
248  State m_state;
249 
252  bool m_is_active_flag;
253 
254  Timer m_state_timer;
255 
258  bool m_on_ground_flag;
259 
262  Vector m_floor_normal;
263 
265  CollisionGroup m_colgroup_active;
266 
269  Dispenser* m_parent_dispenser;
270 
271 private:
272  BadGuy(const BadGuy&) = delete;
273  BadGuy& operator=(const BadGuy&) = delete;
274 };
275 
276 #endif
277 
278 /* EOF */
virtual HitResponse collision_player(Player &player, const CollisionHit &hit)
Called when the badguy collided with a player.
Definition: badguy.cpp:384
Base class for moving sprites that can hurt the Player.
Definition: badguy.hpp:31
Direction str2dir(const std::string &dir_str) const
Get Direction from String.
Definition: badguy.cpp:254
virtual HitResponse collision_badguy(BadGuy &other, const CollisionHit &hit)
Called when the badguy collided with another badguy.
Definition: badguy.cpp:413
virtual void deactivate()
called when the badguy has been deactivated
Definition: badguy.cpp:279
Dispenser * get_parent_dispenser() const
Returns the dispenser this badguys was spawned by.
Definition: badguy.hpp:116
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:681
virtual void ignite()
Called when hit by a fire bullet, and is_flammable() returns true.
Definition: badguy.cpp:753
bool m_is_initialized
true if initialize() has already been called
Definition: badguy.hpp:226
virtual HitResponse collision_bullet(Bullet &bullet, const CollisionHit &hit)
Called when the badguy collided with a bullet.
Definition: badguy.cpp:435
virtual void initialize()
called immediately before the first call to initialize
Definition: badguy.cpp:269
virtual void activate()
called when the badguy has been activated.
Definition: badguy.cpp:274
virtual bool can_break() const
True if this badguy can break bricks or open bonusblocks in his current form.
Definition: badguy.hpp:73
Simple two dimensional vector.
Definition: vector.hpp:24
Definition: bullet.hpp:26
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:687
virtual void extinguish()
Called to revert a badguy when is_ignited() returns true.
Definition: badguy.cpp:801
virtual void freeze()
Called when hit by an ice bullet, and is_freezable() returns true.
Definition: badguy.cpp:699
std::string m_dead_script
< true if the badguy is currently in water
Definition: badguy.hpp:240
void set_parent_dispenser(Dispenser *parent)
Sets the dispenser that spawns this badguy.
Definition: badguy.hpp:113
virtual bool is_flammable() const
Returns whether to call ignite() when a badguy gets hit by a fire bullet.
Definition: badguy.cpp:806
Player * get_nearest_player() const
returns a pointer to the nearest player or 0 if no player is available
Definition: badguy.cpp:666
void set_colgroup_active(CollisionGroup group)
changes colgroup_active.
Definition: badguy.cpp:818
Definition: object_settings.hpp:35
Definition: dispenser.hpp:24
virtual std::string get_water_sprite() const
Get melting particle sprite filename.
Definition: badguy.hpp:107
bool m_countMe
Count this badguy to the statistics? This value should not be changed during runtime.
Definition: badguy.hpp:222
virtual HitResponse collision(GameObject &other, const CollisionHit &hit) override
Called when a collision with another object occurred.
Definition: badguy.cpp:326
virtual void inactive_update(float dt_sec)
called each frame when the badguy is not activated.
Definition: badguy.cpp:292
bool m_ignited
true if this badguy is currently on fire
Definition: badguy.hpp:237
virtual void collision_tile(uint32_t tile_attributes) override
Called when a collision with tile with special attributes occurred.
Definition: badguy.cpp:297
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:693
Base class for all the things that make up Levels&#39; Sectors.
Definition: game_object.hpp:46
bool check()
returns true if a period (or more) passed since start call or last successful check ...
Definition: timer.cpp:37
bool is_offscreen() const
initial position of the enemy.
Definition: badguy.cpp:596
virtual bool is_hurtable() const
Return true if this badguy can be hurt by tiles with the attribute "hurts".
Definition: badguy.hpp:100
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:501
Direction m_dir
The direction we currently face in.
Definition: badguy.hpp:231
virtual void active_update(float dt_sec)
called each frame when the badguy is activated.
Definition: badguy.cpp:284
virtual void update(float dt_sec) override
Called each frame.
Definition: badguy.cpp:152
Direction m_start_dir
The direction we initially faced in.
Definition: badguy.hpp:234
virtual void collision_solid(const CollisionHit &hit) override
Called when the badguy collided with solid ground.
Definition: badguy.cpp:376
bool is_ignited() const
Returns whether this badguys is currently on fire.
Definition: badguy.cpp:812
virtual void unfreeze()
Called to unfreeze the badguy.
Definition: badguy.cpp:721
virtual bool collision_squished(GameObject &object)
Called when the player hit the badguy from above.
Definition: badguy.cpp:419
Simple timer designed to be used in the update functions of objects.
Definition: timer.hpp:24
Definition: reader_mapping.hpp:31
Abstract base class for MovingObjects that are represented by a Sprite.
Definition: moving_sprite.hpp:29
bool might_fall(int height=1) const
Returns true if we might soon fall at least height pixels.
Definition: badguy.cpp:646
Definition: player.hpp:39
virtual void draw(DrawingContext &context) override
Called when the badguy is drawn.
Definition: badguy.cpp:124
void update_on_ground_flag(const CollisionHit &hit)
Update on_ground_flag judging by solid collision hit.
Definition: badguy.cpp:672
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
virtual void run_dead_script()
Call this, if you use custom kill_fall() or kill_squashed(GameObject& object)
Definition: badguy.cpp:540
This class collects data about a collision.
Definition: collision_hit.hpp:44