-Worldmap cleanups (use DrawingContext transformstack)
[supertux.git] / src / object / player.h
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.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 #ifndef SUPERTUX_PLAYER_H
20 #define SUPERTUX_PLAYER_H
21
22 #include <vector>
23 #include <SDL.h>
24
25 #include "timer.h"
26 #include "direction.h"
27 #include "video/surface.h"
28 #include "special/moving_object.h"
29 #include "special/sprite.h"
30 #include "math/physic.h"
31 #include "player_status.h"
32
33 using namespace SuperTux;
34
35 class BadGuy;
36 class Portable;
37
38 /* Times: */
39
40 #define TUX_SAFE_TIME 1.250
41 #define TUX_INVINCIBLE_TIME 10.0
42 #define TUX_INVINCIBLE_TIME_WARNING 2.0
43 #define TUX_FLAPPING_TIME 1 /* How long Tux can flap his wings to gain additional jump height */
44 #define TIME_WARNING 20     /* When to alert player they're low on time! */
45
46 struct PlayerKeymap
47 {
48 public:
49   int jump;
50   int up;
51   int down;
52   int left;
53   int right;
54   int power;
55   
56   PlayerKeymap();
57 };
58
59 extern PlayerKeymap keymap;
60
61 /** Contains a field of booleans that indicate wheter a button is pressed or
62  * released. The old_ fields contain the state of the button at the previous
63  * frame.
64  */
65 struct PlayerInputType
66 {
67 public:
68   PlayerInputType();
69   void reset();
70
71   bool left;
72   bool right;
73   bool up;
74   bool old_up;
75   bool down;
76   bool fire;
77   bool old_fire;
78   bool activate;
79   bool jump;
80   bool old_jump;
81 };
82
83 class Camera;
84 class PlayerStatus;
85
86 extern Surface* tux_life;
87
88 #define GROWING_TIME 1.0
89 #define GROWING_FRAMES 7
90 extern Surface* growingtux_left[GROWING_FRAMES];
91 extern Surface* growingtux_right[GROWING_FRAMES];
92
93 class TuxBodyParts
94 {
95 public:
96   TuxBodyParts()
97     : head(0), body(0), arms(0), feet(0)
98   { }
99   ~TuxBodyParts() {
100     delete head;
101     delete body;
102     delete arms;
103     delete feet;
104   }
105
106   void set_action(std::string action, int loops = -1);
107   void one_time_animation();
108   void draw(DrawingContext& context, const Vector& pos, int layer,
109                 Uint32 drawing_effect = NONE_EFFECT);
110
111   Sprite* head;
112   Sprite* body;
113   Sprite* arms;
114   Sprite* feet;
115 };
116
117 extern TuxBodyParts* small_tux;
118 extern TuxBodyParts* big_tux;
119 extern TuxBodyParts* fire_tux;
120 extern TuxBodyParts* ice_tux;
121
122 class Player : public MovingObject
123 {
124 public:
125   enum HurtMode { KILL, SHRINK };
126   enum FallMode { ON_GROUND, JUMPING, TRAMPOLINE_JUMP, FALLING };
127
128   PlayerInputType input;
129   PlayerStatus* player_status;
130   bool duck;
131   bool dead;
132
133 private:
134   bool dying;
135 public:
136
137   Direction dir;
138   Direction old_dir;
139
140   float last_ground_y;
141   FallMode fall_mode;
142
143   bool on_ground_flag;
144   bool jumping;
145   bool flapping;
146   bool can_jump;
147   bool can_flap;
148   bool falling_from_flap;
149   bool enable_hover;
150   bool butt_jump;
151   
152   float flapping_velocity;
153
154   // Ricardo's flapping
155   int flaps_nb;
156
157   // temporary to help player's choosing a flapping
158   enum { MAREK_FLAP, RICARDO_FLAP, RYAN_FLAP, NO_FLAP };
159   int flapping_mode;
160
161   Timer2 invincible_timer;
162   Timer2 skidding_timer;
163   Timer2 safe_timer;
164   Timer2 kick_timer;
165   Timer2 shooting_timer;   // used to show the arm when Tux is shooting
166   Timer2 dying_timer;
167   Timer2 growing_timer;
168   Timer2 idle_timer;
169   Timer2 flapping_timer;
170   Physic physic;
171   
172 public:
173   Player(PlayerStatus* player_status);
174   virtual ~Player();
175   
176   bool key_event(SDLKey key, bool state);
177   void handle_input();
178
179   virtual void action(float elapsed_time);
180   virtual void draw(DrawingContext& context);
181   virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
182
183   void make_invincible();
184   bool is_invincible() const
185   {
186     return invincible_timer.started();
187   }
188   bool is_dying() const
189   {
190     return dying;
191   }
192   
193   void kill(HurtMode mode);
194   void player_remove_powerups();
195   void check_bounds(Camera* camera);
196   void move(const Vector& vector);
197   void set_bonus(BonusType type, bool animate = false);
198   PlayerStatus* get_status()
199   {
200     return player_status;
201   }
202
203   void bounce(BadGuy& badguy);
204
205   bool is_dead() const
206   { return dead; }
207   bool is_big();
208   
209 private:
210   bool on_ground();
211   
212   void init();
213   
214   void handle_horizontal_input();
215   void handle_vertical_input();
216
217   Portable* grabbed_object;
218
219   Sprite* smalltux_gameover;
220   Sprite* smalltux_star;
221   Sprite* bigtux_star;
222 };
223
224 #endif /*SUPERTUX_PLAYER_H*/