-Started to move stuff from library back to main game
[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
21 #ifndef __BADGUY_H__
22 #define __BADGUY_H__
23
24 // moved them here to make it less typing when implementing new badguys
25 #include <math.h>
26 #include "timer.h"
27 #include "moving_object.h"
28 #include "sprite/sprite.h"
29 #include "math/physic.h"
30 #include "object/player.h"
31 #include "serializable.h"
32 #include "resources.h"
33 #include "sector.h"
34 #include "direction.h"
35 #include "object_factory.h"
36 #include "lisp/parser.h"
37 #include "lisp/lisp.h"
38 #include "lisp/writer.h"
39 #include "video/drawing_context.h"
40 #include "sprite/sprite_manager.h"
41
42 using namespace SuperTux;
43
44 class BadGuy : public MovingObject, public Serializable
45 {
46 public:
47   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_action and inactive_action
56    */
57   virtual void action(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   /** 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   Vector get_start_position() const
71   {
72     return start_position;
73   }
74   void set_start_position(const Vector& vec)
75   {
76     start_position = vec;
77   }
78
79 protected:
80   enum State {
81     STATE_INIT,
82     STATE_INACTIVE,
83     STATE_ACTIVE,
84     STATE_SQUISHED,
85     STATE_FALLING
86   };
87  
88   /** Called when the badguy collided with a player */
89   virtual HitResponse collision_player(Player& player,
90       const CollisionHit& hit);
91   /** Called when the badguy collided with solid ground */
92   virtual HitResponse collision_solid(GameObject& other,
93       const CollisionHit& hit);
94   /** Called when the badguy collided with another badguy */
95   virtual HitResponse collision_badguy(BadGuy& other,
96       const CollisionHit& hit);
97  
98   /** Called when the player hit the badguy from above. You should return true
99    * if the badguy was squished, false if squishing wasn't possible
100    */
101   virtual bool collision_squished(Player& player);
102
103   /** called each frame when the badguy is activated. */
104   virtual void active_action(float elapsed_time);
105   /** called each frame when the badguy is not activated. */
106   virtual void inactive_action(float elapsed_time);
107
108   /**
109    * called when the badguy has been activated. (As a side effect the dir
110    * variable might have been changed so that it faces towards the player.
111    */
112   virtual void activate();
113   /** called when the badguy has been deactivated */
114   virtual void deactivate();
115
116   void kill_squished(Player& player);
117
118   void set_state(State state);
119   State get_state() const
120   { return state; }
121     
122   /**
123    * returns a pointer to the player, try to avoid this function to avoid
124    * problems later when we have multiple players or no player in scripted
125    * sequence.
126    */
127   Player* get_player();
128   
129   Sprite* sprite;
130   Physic physic;
131
132   /// is the enemy activated
133   bool activated;
134   /**
135    * initial position of the enemy. Also the position where enemy respawns when
136    * after being deactivated.
137    */
138   bool is_offscreen();
139   
140   Vector start_position;
141
142   Direction dir;
143
144   int hitpoints;
145   int bullet_hitpoints;
146 private:
147   void try_activate();
148   
149   State state;
150   Timer2 state_timer;
151 };
152
153 #endif
154