Rudimentary approach at water splash effect for badguys
[supertux.git] / src / badguy / 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 "object/moving_sprite.hpp"
21 #include "supertux/direction.hpp"
22 #include "supertux/physic.hpp"
23 #include "supertux/timer.hpp"
24
25 class Player;
26 class Bullet;
27
28 /** Base class for moving sprites that can hurt the Player. */
29 class BadGuy : public MovingSprite
30 {
31 public:
32   BadGuy(const Vector& pos, const std::string& sprite_name, int layer = LAYER_OBJECTS);
33   BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer = LAYER_OBJECTS);
34   BadGuy(const Reader& reader, const std::string& sprite_name, int layer = LAYER_OBJECTS);
35
36   /** Called when the badguy is drawn. The default implementation
37       simply draws the badguy sprite on screen */
38   virtual void draw(DrawingContext& context);
39
40   /** Called each frame. The default implementation checks badguy
41       state and calls active_update and inactive_update */
42   virtual void update(float elapsed_time);
43
44   /** Called when a collision with another object occurred. The
45       default implementation calls collision_player, collision_solid,
46       collision_badguy and collision_squished */
47   virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
48
49   /** Called when a collision with tile with special attributes
50       occurred */
51   virtual void collision_tile(uint32_t tile_attributes);
52
53   /** Set the badguy to kill/falling state, which makes him falling of
54       the screen (his sprite is turned upside-down) */
55   virtual void kill_fall();
56
57   /** Call this, if you use custom kill_fall() or kill_squashed(GameObject& object) */
58   virtual void run_dead_script();
59
60   /** True if this badguy can break bricks or open bonusblocks in his
61       current form. */
62   virtual bool can_break()
63   {
64     return false;
65   }
66
67   Vector get_start_position() const
68   {
69     return start_position;
70   }
71
72   void set_start_position(const Vector& vec)
73   {
74     start_position = vec;
75   }
76
77   /** Called when hit by a fire bullet, and is_flammable() returns true */
78   virtual void ignite();
79
80   /** Called to revert a badguy when is_ignited() returns true */
81   virtual void extinguish();
82
83   /** Returns whether to call ignite() when a badguy gets hit by a fire bullet */
84   virtual bool is_flammable() const;
85
86   /** Returns whether this badguys is currently on fire */
87   bool is_ignited() const;
88
89   /** Called when hit by an ice bullet, and is_freezable() returns true. */
90   virtual void freeze();
91
92   /** Called to unfreeze the badguy. */
93   virtual void unfreeze();
94
95   virtual bool is_freezable() const;
96
97   bool is_frozen() const;
98
99   bool is_in_water() const;
100
101 protected:
102   enum State {
103     STATE_INIT,
104     STATE_INACTIVE,
105     STATE_ACTIVE,
106     STATE_SQUISHED,
107     STATE_FALLING
108   };
109
110 protected:
111   /** Called when the badguy collided with a player */
112   virtual HitResponse collision_player(Player& player, const CollisionHit& hit);
113
114   /** Called when the badguy collided with solid ground */
115   virtual void collision_solid(const CollisionHit& hit);
116
117   /** Called when the badguy collided with another badguy */
118   virtual HitResponse collision_badguy(BadGuy& other, const CollisionHit& hit);
119
120   /** Called when the player hit the badguy from above. You should
121       return true if the badguy was squished, false if squishing
122       wasn't possible */
123   virtual bool collision_squished(GameObject& object);
124
125   /** Called when the badguy collided with a bullet */
126   virtual HitResponse collision_bullet(Bullet& bullet, const CollisionHit& hit);
127
128   /** called each frame when the badguy is activated. */
129   virtual void active_update(float elapsed_time);
130
131   /** called each frame when the badguy is not activated. */
132   virtual void inactive_update(float elapsed_time);
133
134   /** called immediately before the first call to initialize */
135   virtual void initialize();
136
137   /** called when the badguy has been activated. (As a side effect the
138       dir variable might have been changed so that it faces towards
139       the player. */
140   virtual void activate();
141
142   /** called when the badguy has been deactivated */
143   virtual void deactivate();
144
145   void kill_squished(GameObject& object);
146
147   void set_state(State state);
148   State get_state() const
149   { return state; }
150
151   bool check_state_timer() {
152     return state_timer.check();
153   }
154
155   /** returns a pointer to the nearest player or 0 if no player is available */
156   Player* get_nearest_player();
157
158   /** initial position of the enemy. Also the position where enemy
159       respawns when after being deactivated. */
160   bool is_offscreen();
161
162   /** Returns true if we might soon fall at least @c height
163       pixels. Minimum value for height is 1 pixel */
164   bool might_fall(int height = 1);
165
166   /** Get Direction from String. */
167   Direction str2dir( std::string dir_str );
168
169   /** Update on_ground_flag judging by solid collision @c hit. This
170       gets called from the base implementation of collision_solid, so
171       call this when overriding collision_solid's default
172       behaviour. */
173   void update_on_ground_flag(const CollisionHit& hit);
174
175   /** Returns true if we touched ground in the past frame This only
176       works if update_on_ground_flag() gets called in
177       collision_solid. */
178   bool on_ground();
179
180   /** Returns floor normal stored the last time when
181       update_on_ground_flag was called and we touched something solid
182       from above. */
183   Vector get_floor_normal();
184
185   /** Returns true if we were in STATE_ACTIVE at the beginning of the
186       last call to update() */
187   bool is_active();
188
189   /** changes colgroup_active. Also calls set_group when badguy is in STATE_ACTIVE */
190   void set_colgroup_active(CollisionGroup group);
191
192 private:
193   void try_activate();
194
195 protected:
196   Physic physic;
197
198 public:
199   /** Count this badguy to the statistics? This value should not be
200       changed during runtime. */
201   bool countMe;
202
203 protected:
204   /** true if initialize() has already been called */
205   bool is_initialized;
206
207   Vector start_position;
208
209   /** The direction we currently face in */
210   Direction dir;
211
212   /** The direction we initially faced in */
213   Direction start_dir;
214
215   bool frozen;
216   bool ignited; /**< true if this badguy is currently on fire */
217   bool in_water; /** < true if the badguy is currently in water */
218
219   std::string dead_script; /**< script to execute when badguy is killed */
220
221 private:
222   State state;
223
224   /** true if state was STATE_ACTIVE at the beginning of the last call
225       to update() */
226   bool is_active_flag;
227
228   Timer state_timer;
229
230   /** true if we touched something solid from above and
231       update_on_ground_flag was called last frame */
232   bool on_ground_flag;
233
234   /** floor normal stored the last time when update_on_ground_flag was
235       called and we touched something solid from above */
236   Vector floor_normal;
237
238   /** CollisionGroup the badguy should be in while active */
239   CollisionGroup colgroup_active;
240
241 private:
242   BadGuy(const BadGuy&);
243   BadGuy& operator=(const BadGuy&);
244 };
245
246 #endif
247
248 /* EOF */