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