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