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