- Remove ManagedSoundSource
[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 lisp::Lisp& reader, const std::string& sprite_name, int layer = LAYER_OBJECTS);
46
47   /** Called when the badguy is drawn. The default implementation simply draws
48    * the badguy sprite on screen
49    */
50   virtual void draw(DrawingContext& context);
51   /** Called each frame. The default implementation checks badguy state and
52    * calls active_update and inactive_update
53    */
54   virtual void update(float elapsed_time);
55   /** Called when a collision with another object occured. The default
56    * implemetnation calls collision_player, collision_solid, collision_badguy
57    * and collision_squished
58    */
59   virtual HitResponse collision(GameObject& other,
60       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,
99       const CollisionHit& hit);
100   /** Called when the badguy collided with solid ground */
101   virtual HitResponse collision_solid(GameObject& other,
102       const CollisionHit& hit);
103   /** Called when the badguy collided with another badguy */
104   virtual HitResponse collision_badguy(BadGuy& other,
105       const CollisionHit& hit);
106  
107   /** Called when the player hit the badguy from above. You should return true
108    * if the badguy was squished, false if squishing wasn't possible
109    */
110   virtual bool collision_squished(Player& player);
111
112   /** Called when the badguy collided with a bullet */
113   virtual HitResponse collision_bullet(Bullet& bullet, 
114       const CollisionHit& hit);
115
116   /** called each frame when the badguy is activated. */
117   virtual void active_update(float elapsed_time);
118   /** called each frame when the badguy is not activated. */
119   virtual void inactive_update(float elapsed_time);
120
121   /**
122    * called when the badguy has been activated. (As a side effect the dir
123    * variable might have been changed so that it faces towards the player.
124    */
125   virtual void activate();
126   /** called when the badguy has been deactivated */
127   virtual void deactivate();
128
129   void kill_squished(Player& player);
130
131   void set_state(State state);
132   State get_state() const
133   { return state; }
134     
135   /**
136    * returns a pointer to the nearest player or 0 if no player is available
137    */
138   Player* get_nearest_player();
139   
140   Physic physic;
141
142   /// is the enemy activated
143   bool activated;
144   /**
145    * initial position of the enemy. Also the position where enemy respawns when
146    * after being deactivated.
147    */
148   bool is_offscreen();
149   /**
150    *  Returns true if we might soon fall at least @c height pixels. Minimum
151    *  value for height is 1 pixel
152    */
153   bool might_fall(int height = 1);
154
155   Vector start_position;
156
157   Direction dir;
158
159 private:
160   void try_activate();
161   
162   State state;
163   Timer state_timer;
164 };
165
166 #endif
167