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