First attempts at making BadGuys cloneable
[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 "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(const BadGuy& badguy);
48   ~BadGuy();
49
50   /** Called when the badguy is drawn. The default implementation simply draws
51    * the badguy sprite on screen
52    */
53   virtual void draw(DrawingContext& context);
54   /** Called each frame. The default implementation checks badguy state and
55    * calls active_update and inactive_update
56    */
57   virtual void update(float elapsed_time);
58   /** Called when a collision with another object occured. The default
59    * implemetnation calls collision_player, collision_solid, collision_badguy
60    * and collision_squished
61    */
62   virtual HitResponse collision(GameObject& other,
63       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   /** 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   Vector get_start_position() const
79   {
80     return start_position;
81   }
82   void set_start_position(const Vector& vec)
83   {
84     start_position = vec;
85   }
86   
87   /** Count this badguy to the statistics? This value should not be changed
88    * during runtime. */
89   bool countMe;
90
91   virtual BadGuy* clone() const = 0;
92
93 protected:
94   enum State {
95     STATE_INIT,
96     STATE_INACTIVE,
97     STATE_ACTIVE,
98     STATE_SQUISHED,
99     STATE_FALLING
100   };
101  
102   /** Called when the badguy collided with a player */
103   virtual HitResponse collision_player(Player& player,
104       const CollisionHit& hit);
105   /** Called when the badguy collided with solid ground */
106   virtual HitResponse collision_solid(GameObject& other,
107       const CollisionHit& hit);
108   /** Called when the badguy collided with another badguy */
109   virtual HitResponse collision_badguy(BadGuy& other,
110       const CollisionHit& hit);
111  
112   /** Called when the player hit the badguy from above. You should return true
113    * if the badguy was squished, false if squishing wasn't possible
114    */
115   virtual bool collision_squished(Player& player);
116
117   /** Called when the badguy collided with a bullet */
118   virtual HitResponse collision_bullet(Bullet& bullet, 
119       const CollisionHit& hit);
120
121   /** called each frame when the badguy is activated. */
122   virtual void active_update(float elapsed_time);
123   /** called each frame when the badguy is not activated. */
124   virtual void inactive_update(float elapsed_time);
125
126   /**
127    * called when the badguy has been activated. (As a side effect the dir
128    * variable might have been changed so that it faces towards the player.
129    */
130   virtual void activate();
131   /** called when the badguy has been deactivated */
132   virtual void deactivate();
133
134   void kill_squished(Player& player);
135
136   void set_state(State state);
137   State get_state() const
138   { return state; }
139     
140   /**
141    * returns a pointer to the nearest player or 0 if no player is available
142    */
143   Player* get_nearest_player();
144   
145   Sprite* sprite;
146   Physic physic;
147
148   /// is the enemy activated
149   bool activated;
150   /**
151    * initial position of the enemy. Also the position where enemy respawns when
152    * after being deactivated.
153    */
154   bool is_offscreen();
155   /**
156    *  Returns true if we might soon fall at least @c height pixels. Minimum
157    *  value for height is 1 pixel
158    */
159   bool might_fall(int height = 1);
160
161   Vector start_position;
162
163   Direction dir;
164
165   /**
166    * z-position at which to draw the sprite.
167    * e.g. LAYER_OBJECTS, LAYER_OBJECTS - 1, LAYER_FLOATINGOBJECTS
168    */
169   int layer;
170
171 private:
172   void try_activate();
173   
174   State state;
175   Timer state_timer;
176 };
177
178 #endif
179