more adjustments to collision detection, tux is correctly placed above badguys again...
[supertux.git] / src / badguy / badguy.hpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 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
19 //  02111-1307, USA.
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 "moving_object.hpp"
27 #include "sprite/sprite.hpp"
28 #include "physic.hpp"
29 #include "object/player.hpp"
30 #include "serializable.hpp"
31 #include "resources.hpp"
32 #include "sector.hpp"
33 #include "direction.hpp"
34 #include "object_factory.hpp"
35 #include "lisp/parser.hpp"
36 #include "lisp/lisp.hpp"
37 #include "lisp/writer.hpp"
38 #include "video/drawing_context.hpp"
39 #include "audio/sound_manager.hpp"
40 #include "audio/sound_source.hpp"
41 #include "sprite/sprite_manager.hpp"
42
43 class BadGuy : public MovingObject, public Serializable
44 {
45 public:
46   BadGuy();
47   ~BadGuy();
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 occured. The default
58    * implemetnation calls collision_player, collision_solid, collision_badguy
59    * and collision_squished
60    */
61   virtual HitResponse collision(GameObject& other,
62       const CollisionHit& hit);
63
64   /** Called when a collision with tile with special attributes occured */
65   virtual void collision_tile(uint32_t tile_attributes);
66
67   /** Set the badguy to kill/falling state, which makes him falling of the
68    * screen (his sprite is turned upside-down)
69    */
70   virtual void kill_fall();
71
72   /** Writes out the badguy into the included lisp::Writer. Useful e.g. when
73    * converting an old-format level to the new format.
74    */
75   virtual void save(lisp::Writer& );
76
77   Vector get_start_position() const
78   {
79     return start_position;
80   }
81   void set_start_position(const Vector& vec)
82   {
83     start_position = vec;
84   }
85   
86   /** Count this badguy to the statistics? This value should not be changed
87    * during runtime. */
88   bool countMe;
89
90 protected:
91   enum State {
92     STATE_INIT,
93     STATE_INACTIVE,
94     STATE_ACTIVE,
95     STATE_SQUISHED,
96     STATE_FALLING
97   };
98  
99   /** Called when the badguy collided with a player */
100   virtual HitResponse collision_player(Player& player,
101       const CollisionHit& hit);
102   /** Called when the badguy collided with solid ground */
103   virtual HitResponse collision_solid(GameObject& other,
104       const CollisionHit& hit);
105   /** Called when the badguy collided with another badguy */
106   virtual HitResponse collision_badguy(BadGuy& other,
107       const CollisionHit& hit);
108  
109   /** Called when the player hit the badguy from above. You should return true
110    * if the badguy was squished, false if squishing wasn't possible
111    */
112   virtual bool collision_squished(Player& player);
113
114   /** called each frame when the badguy is activated. */
115   virtual void active_update(float elapsed_time);
116   /** called each frame when the badguy is not activated. */
117   virtual void inactive_update(float elapsed_time);
118
119   /**
120    * called when the badguy has been activated. (As a side effect the dir
121    * variable might have been changed so that it faces towards the player.
122    */
123   virtual void activate();
124   /** called when the badguy has been deactivated */
125   virtual void deactivate();
126
127   void kill_squished(Player& player);
128
129   void set_state(State state);
130   State get_state() const
131   { return state; }
132     
133   /**
134    * returns a pointer to the player, try to avoid this function to avoid
135    * problems later when we have multiple players or no player in scripted
136    * sequence.
137    */
138   Player* get_player();
139   
140   Sprite* sprite;
141   Physic physic;
142
143   /// is the enemy activated
144   bool activated;
145   /**
146    * initial position of the enemy. Also the position where enemy respawns when
147    * after being deactivated.
148    */
149   bool is_offscreen();
150   
151   Vector start_position;
152
153   Direction dir;
154 private:
155   void try_activate();
156   
157   State state;
158   Timer state_timer;
159 };
160
161 #endif
162