Stay-on-platform Badguys no longer go crazy when falling down
[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, 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   /** Writes out the badguy into the included lisp::Writer. Useful e.g. when
71    * converting an old-format level to the new format.
72    */
73   virtual void save(lisp::Writer& writer);
74
75   Vector get_start_position() const
76   {
77     return start_position;
78   }
79   void set_start_position(const Vector& vec)
80   {
81     start_position = vec;
82   }
83   
84   /** Count this badguy to the statistics? This value should not be changed
85    * during runtime. */
86   bool countMe;
87
88 protected:
89   enum State {
90     STATE_INIT,
91     STATE_INACTIVE,
92     STATE_ACTIVE,
93     STATE_SQUISHED,
94     STATE_FALLING
95   };
96  
97   /** Called when the badguy collided with a player */
98   virtual HitResponse collision_player(Player& player, const CollisionHit& hit);
99   /** Called when the badguy collided with solid ground */
100   virtual void collision_solid(const CollisionHit& hit);
101   /** Called when the badguy collided with another badguy */
102   virtual HitResponse collision_badguy(BadGuy& other, const CollisionHit& hit);
103  
104   /** Called when the player hit the badguy from above. You should return true
105    * if the badguy was squished, false if squishing wasn't possible
106    */
107   virtual bool collision_squished(Player& player);
108
109   /** Called when the badguy collided with a bullet */
110   virtual HitResponse collision_bullet(Bullet& bullet, const CollisionHit& hit);
111
112   /** called each frame when the badguy is activated. */
113   virtual void active_update(float elapsed_time);
114   /** called each frame when the badguy is not activated. */
115   virtual void inactive_update(float elapsed_time);
116
117   /**
118    * called when the badguy has been activated. (As a side effect the dir
119    * variable might have been changed so that it faces towards the player.
120    */
121   virtual void activate();
122   /** called when the badguy has been deactivated */
123   virtual void deactivate();
124
125   void kill_squished(Player& player);
126
127   void set_state(State state);
128   State get_state() const
129   { return state; }
130     
131   /**
132    * returns a pointer to the nearest player or 0 if no player is available
133    */
134   Player* get_nearest_player();
135   
136   Physic physic;
137
138   /// is the enemy activated
139   bool activated;
140   /**
141    * initial position of the enemy. Also the position where enemy respawns when
142    * after being deactivated.
143    */
144   bool is_offscreen();
145   /**
146    *  Returns true if we might soon fall at least @c height pixels. Minimum
147    *  value for height is 1 pixel
148    */
149   bool might_fall(int height = 1);
150
151   Vector start_position;
152
153   /**
154    * The direction we currently face in
155    */
156   Direction dir;
157
158   /**
159    * The direction we initially faced in
160    */
161   Direction start_dir;
162
163   /**
164    *  Get Direction from String.
165    */ 
166   Direction str2dir( std::string dir_str );
167
168   /**
169    * Update on_ground_flag judging by solid collision @c hit.
170    * This gets called from the base implementation of collision_solid, so call this when overriding collision_solid's default behaviour.
171    */
172   void update_on_ground_flag(const CollisionHit& hit);
173
174   /**
175    * Returns true if we touched ground in the past frame
176    * This only works if update_on_ground_flag() gets called in collision_solid.
177    */
178   bool on_ground();
179
180 private:
181   void try_activate();
182   
183   State state;
184   Timer state_timer;
185   bool on_ground_flag;
186 };
187
188 #endif
189