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