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