ef17b751c28c1a230e613cfe5266dea64dd88368
[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_action and inactive_action
53    */
54   virtual void action(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 protected:
77   enum State {
78     STATE_INIT,
79     STATE_INACTIVE,
80     STATE_ACTIVE,
81     STATE_SQUISHED,
82     STATE_FALLING
83   };
84  
85   /** Called when the badguy collided with a player */
86   virtual HitResponse collision_player(Player& player,
87       const CollisionHit& hit);
88   /** Called when the badguy collided with solid ground */
89   virtual HitResponse collision_solid(GameObject& other,
90       const CollisionHit& hit);
91   /** Called when the badguy collided with another badguy */
92   virtual HitResponse collision_badguy(BadGuy& other,
93       const CollisionHit& hit);
94  
95   /** Called when the player hit the badguy from above. You should return true
96    * if the badguy was squished, false if squishing wasn't possible
97    */
98   virtual bool collision_squished(Player& player);
99
100   /** called each frame when the badguy is activated. */
101   virtual void active_action(float elapsed_time);
102   /** called each frame when the badguy is not activated. */
103   virtual void inactive_action(float elapsed_time);
104
105   /**
106    * called when the badguy has been activated. (As a side effect the dir
107    * variable might have been changed so that it faces towards the player.
108    */
109   virtual void activate();
110   /** called when the badguy has been deactivated */
111   virtual void deactivate();
112
113   void kill_squished(Player& player);
114
115   void set_state(State state);
116   State get_state() const
117   { return state; }
118     
119   /**
120    * returns a pointer to the player, try to avoid this function to avoid
121    * problems later when we have multiple players or no player in scripted
122    * sequence.
123    */
124   Player* get_player();
125   
126   Sprite* sprite;
127   Physic physic;
128
129   /// is the enemy activated
130   bool activated;
131   /**
132    * initial position of the enemy. Also the position where enemy respawns when
133    * after being deactivated.
134    */
135   bool is_offscreen();
136   
137   Vector start_position;
138
139   Direction dir;
140
141   int hitpoints;
142   int bullet_hitpoints;
143 private:
144   void try_activate();
145   
146   State state;
147   Timer state_timer;
148 };
149
150 #endif
151