bb31e718d90a0f97e81350473b1a576c320ceaba
[supertux.git] / src / badguy / badguy.h
1 #ifndef __BADGUY_H__
2 #define __BADGUY_H__
3
4 // moved them here to make it less typing when implementing new badguys
5 #include <math.h>
6 #include "timer.h"
7 #include "special/moving_object.h"
8 #include "special/sprite.h"
9 #include "math/physic.h"
10 #include "object/player.h"
11 #include "serializable.h"
12 #include "resources.h"
13 #include "sector.h"
14 #include "direction.h"
15 #include "object_factory.h"
16 #include "lisp/parser.h"
17 #include "lisp/lisp.h"
18 #include "lisp/writer.h"
19 #include "video/drawing_context.h"
20 #include "special/sprite_manager.h"
21
22 using namespace SuperTux;
23
24 class BadGuy : public MovingObject, public Serializable
25 {
26 public:
27   BadGuy();
28   ~BadGuy();
29
30   /** Called when the badguy is drawn. The default implementation simply draws
31    * the badguy sprite on screen
32    */
33   virtual void draw(DrawingContext& context);
34   /** Called each frame. The default implementation checks badguy state and
35    * calls active_action and inactive_action
36    */
37   virtual void action(float elapsed_time);
38   /** Called when a collision with another object occured. The default
39    * implemetnation calls collision_player, collision_solid, collision_badguy
40    * and collision_squished
41    */
42   virtual HitResponse collision(GameObject& other,
43       const CollisionHit& hit);
44
45   /** Set the badguy to kill/falling state, which makes him falling of the
46    * screen (his sprite is turned upside-down)
47    */
48   virtual void kill_fall();
49
50   Vector get_start_position() const
51   {
52     return start_position;
53   }
54   void set_start_position(const Vector& vec)
55   {
56     start_position = vec;
57   }
58
59 protected:
60   enum State {
61     STATE_INIT,
62     STATE_INACTIVE,
63     STATE_ACTIVE,
64     STATE_SQUISHED,
65     STATE_FALLING
66   };
67  
68   /** Called when the badguy collided with a player */
69   virtual HitResponse collision_player(Player& player,
70       const CollisionHit& hit);
71   /** Called when the badguy collided with solid ground */
72   virtual HitResponse collision_solid(GameObject& other,
73       const CollisionHit& hit);
74   /** Called when the badguy collided with another badguy */
75   virtual HitResponse collision_badguy(BadGuy& other,
76       const CollisionHit& hit);
77  
78   /** Called when the player hit the badguy from above. You should return true
79    * if the badguy was squished, false if squishing wasn't possible
80    */
81   virtual bool collision_squished(Player& player);
82
83   /** called each frame when the badguy is activated. */
84   virtual void active_action(float elapsed_time);
85   /** called each frame when the badguy is not activated. */
86   virtual void inactive_action(float elapsed_time);
87
88   /**
89    * called when the badguy has been activated. (As a side effect the dir
90    * variable might have been changed so that it faces towards the player.
91    */
92   virtual void activate();
93   /** caleed when the badguy has been deactivated */
94   virtual void deactivate();
95
96   void kill_squished(Player& player);
97
98   void set_state(State state);
99   State get_state() const
100   { return state; }
101     
102   /**
103    * returns a pointer to the player, try to avoid this function to avoid
104    * problems later when we have multiple players or no player in scripted
105    * sequence.
106    */
107   Player* get_player();
108   
109   Sprite* sprite;
110   Physic physic;
111
112   /// is the enemy activated
113   bool activated;
114   /**
115    * initial position of the enemy. Also the position where enemy respawns when
116    * after being deactivated.
117    */
118   bool is_offscreen();
119   
120   Vector start_position;
121
122   Direction dir;
123
124   int hitpoints;
125 private:
126   void try_activate();
127   
128   State state;
129   Timer2 state_timer;
130 };
131
132 #endif
133