53ed624c0d64f6ca377fa29f408944f74b121b8a
[supertux.git] / src / badguy / badguy.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #ifndef __BADGUY_H__
21 #define __BADGUY_H__
22
23 // moved them here to make it less typing when implementing new badguys
24 #include <math.h>
25 #include "timer.hpp"
26 #include "object/moving_sprite.hpp"
27 #include "physic.hpp"
28 #include "object/player.hpp"
29 #include "serializable.hpp"
30 #include "sector.hpp"
31 #include "direction.hpp"
32 #include "object_factory.hpp"
33 #include "lisp/lisp.hpp"
34 #include "lisp/writer.hpp"
35 #include "video/drawing_context.hpp"
36 #include "audio/sound_manager.hpp"
37 #include "audio/sound_source.hpp"
38
39 /**
40  * Base class for moving sprites that can hurt the Player.
41  */
42 class BadGuy : public MovingSprite, protected UsesPhysic, public Serializable
43 {
44 public:
45   BadGuy(const Vector& pos, const std::string& sprite_name, int layer = LAYER_OBJECTS);
46   BadGuy(const Vector& pos, Direction direction, const std::string& sprite_name, int layer = LAYER_OBJECTS);
47   BadGuy(const lisp::Lisp& reader, const std::string& sprite_name, int layer = LAYER_OBJECTS);
48
49   /** Called when the badguy is drawn. The default implementation simply draws
50    * the badguy sprite on screen
51    */
52   virtual void draw(DrawingContext& context);
53   /** Called each frame. The default implementation checks badguy state and
54    * calls active_update and inactive_update
55    */
56   virtual void update(float elapsed_time);
57   /** Called when a collision with another object occurred. The default
58    * implementation calls collision_player, collision_solid, collision_badguy
59    * and collision_squished
60    */
61   virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
62
63   /** Called when a collision with tile with special attributes occurred */
64   virtual void collision_tile(uint32_t tile_attributes);
65
66   /** Set the badguy to kill/falling state, which makes him falling of the
67    * screen (his sprite is turned upside-down)
68    */
69   virtual void kill_fall();
70   
71   /** Call this, if you use custom kill_fall() or kill_squashed(GameObject& object) */
72   virtual void run_dead_script();
73
74   /** Writes out the badguy into the included lisp::Writer. Useful e.g. when
75    * converting an old-format level to the new format.
76    */
77   virtual void write(lisp::Writer& writer);
78
79   /**
80    * True if this badguy can break bricks or open bonusblocks in his current form.
81    */
82   virtual bool can_break()
83   {
84     return false;
85   }
86
87   Vector get_start_position() const
88   {
89     return start_position;
90   }
91   void set_start_position(const Vector& vec)
92   {
93     start_position = vec;
94   }
95
96   /** Count this badguy to the statistics? This value should not be changed
97    * during runtime. */
98   bool countMe;
99
100   /**
101    * Called when hit by a fire bullet, and is_flammable() returns true
102    */
103   virtual void ignite();
104
105   /**
106    * Called to revert a badguy when is_ignited() returns true
107    */
108   virtual void extinguish();
109
110   /**
111    * Returns whether to call ignite() when a badguy gets hit by a fire bullet
112    */
113   virtual bool is_flammable() const;
114
115   /**
116    * Returns whether this badguys is currently on fire
117    */
118   bool is_ignited() const;
119
120   /**
121    * Called when hit by an ice bullet, and is_freezable() returns true.
122    */
123   virtual void freeze();
124
125   /**
126    * Called to unfreeze the badguy.
127    */
128   virtual void unfreeze();
129
130   virtual bool is_freezable() const;
131
132   bool is_frozen() const;
133
134 protected:
135   enum State {
136     STATE_INIT,
137     STATE_INACTIVE,
138     STATE_ACTIVE,
139     STATE_SQUISHED,
140     STATE_FALLING
141   };
142
143   /** Called when the badguy collided with a player */
144   virtual HitResponse collision_player(Player& player, const CollisionHit& hit);
145   /** Called when the badguy collided with solid ground */
146   virtual void collision_solid(const CollisionHit& hit);
147   /** Called when the badguy collided with another badguy */
148   virtual HitResponse collision_badguy(BadGuy& other, const CollisionHit& hit);
149
150   /** Called when the player hit the badguy from above. You should return true
151    * if the badguy was squished, false if squishing wasn't possible
152    */
153   virtual bool collision_squished(GameObject& object);
154
155   /** Called when the badguy collided with a bullet */
156   virtual HitResponse collision_bullet(Bullet& bullet, const CollisionHit& hit);
157
158   /** called each frame when the badguy is activated. */
159   virtual void active_update(float elapsed_time);
160   /** called each frame when the badguy is not activated. */
161   virtual void inactive_update(float elapsed_time);
162
163   bool is_initialized; /**< true if initialize() has already been called */
164   /** called immediately before the first call to initialize */
165   virtual void initialize();
166   /**
167    * called when the badguy has been activated. (As a side effect the dir
168    * variable might have been changed so that it faces towards the player.
169    */
170   virtual void activate();
171   /** called when the badguy has been deactivated */
172   virtual void deactivate();
173
174   void kill_squished(GameObject& object);
175
176   void set_state(State state);
177   State get_state() const
178   { return state; }
179
180   /**
181    * returns a pointer to the nearest player or 0 if no player is available
182    */
183   Player* get_nearest_player();
184
185   /**
186    * initial position of the enemy. Also the position where enemy respawns when
187    * after being deactivated.
188    */
189   bool is_offscreen();
190   /**
191    *  Returns true if we might soon fall at least @c height pixels. Minimum
192    *  value for height is 1 pixel
193    */
194   bool might_fall(int height = 1);
195
196   Vector start_position;
197
198   /**
199    * The direction we currently face in
200    */
201   Direction dir;
202
203   /**
204    * The direction we initially faced in
205    */
206   Direction start_dir;
207
208   /**
209    *  Get Direction from String.
210    */
211   Direction str2dir( std::string dir_str );
212
213   /**
214    * Update on_ground_flag judging by solid collision @c hit.
215    * This gets called from the base implementation of collision_solid, so call this when overriding collision_solid's default behaviour.
216    */
217   void update_on_ground_flag(const CollisionHit& hit);
218
219   /**
220    * Returns true if we touched ground in the past frame
221    * This only works if update_on_ground_flag() gets called in collision_solid.
222    */
223   bool on_ground();
224
225   /**
226    * Returns floor normal stored the last time when update_on_ground_flag was called and we touched something solid from above.
227    */
228   Vector get_floor_normal();
229
230   bool frozen;
231   bool ignited; /**< true if this badguy is currently on fire */
232
233   std::string dead_script; /**< script to execute when badguy is killed */
234
235   /**
236    * Returns true if we were in STATE_ACTIVE at the beginning of the last call to update()
237    */
238   bool is_active();
239
240   void set_colgroup_active(CollisionGroup group); /**< changes colgroup_active. Also calls set_group when badguy is in STATE_ACTIVE */
241
242 private:
243   void try_activate();
244
245   State state;
246   bool is_active_flag; /**< true if state was STATE_ACTIVE at the beginning of the last call to update() */
247   Timer state_timer;
248   bool on_ground_flag; /**< true if we touched something solid from above and update_on_ground_flag was called last frame */
249   Vector floor_normal; /**< floor normal stored the last time when update_on_ground_flag was called and we touched something solid from above */
250   CollisionGroup colgroup_active; /**< CollisionGroup the badguy should be in while active */
251
252 };
253
254 #endif