Had a bit of time today and worked on supertux:
[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
32 using namespace SuperTux;
33
34 class BadGuy;
35 class Portable;
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 /* Sizes: */
46 #define SMALL 0
47 #define BIG 1
48
49 struct PlayerKeymap
50 {
51 public:
52   int jump;
53   int up;
54   int down;
55   int left;
56   int right;
57   int power;
58   
59   PlayerKeymap();
60 };
61
62 extern PlayerKeymap keymap;
63
64 /** Contains a field of booleans that indicate wheter a button is pressed or
65  * released. The old_ fields contain the state of the button at the previous
66  * frame.
67  */
68 struct PlayerInputType
69 {
70 public:
71   PlayerInputType();
72   void reset();
73
74   bool left;
75   bool right;
76   bool up;
77   bool old_up;
78   bool down;
79   bool fire;
80   bool old_fire;
81   bool activate;
82   bool jump;
83   bool old_jump;
84 };
85
86 class Camera;
87 class PlayerStatus;
88
89 extern Surface* tux_life;
90
91 #define GROWING_TIME 1.0
92 #define GROWING_FRAMES 7
93 extern Surface* growingtux_left[GROWING_FRAMES];
94 extern Surface* growingtux_right[GROWING_FRAMES];
95
96 class TuxBodyParts
97 {
98 public:
99   TuxBodyParts()
100     : head(0), body(0), arms(0), feet(0)
101   { }
102   ~TuxBodyParts() {
103     delete head;
104     delete body;
105     delete arms;
106     delete feet;
107   }
108
109   void set_action(std::string action, int loops = -1);
110   void one_time_animation();
111   void draw(DrawingContext& context, const Vector& pos, int layer,
112                 Uint32 drawing_effect = NONE_EFFECT);
113
114   Sprite* head;
115   Sprite* body;
116   Sprite* arms;
117   Sprite* feet;
118 };
119
120 extern TuxBodyParts* small_tux;
121 extern TuxBodyParts* big_tux;
122 extern TuxBodyParts* fire_tux;
123 extern TuxBodyParts* ice_tux;
124
125 class Player : public MovingObject
126 {
127 public:
128   enum HurtMode { KILL, SHRINK };
129   enum Power { NONE_POWER, FIRE_POWER, ICE_POWER };
130   enum FallMode { ON_GROUND, JUMPING, TRAMPOLINE_JUMP, FALLING };
131
132   PlayerInputType input;
133   int got_power;
134   int size;
135   bool duck;
136   bool dead;
137
138 private:
139   bool dying;
140 public:
141
142   Direction dir;
143   Direction old_dir;
144
145   float last_ground_y;
146   FallMode fall_mode;
147
148   bool on_ground_flag;
149   bool jumping;
150   bool flapping;
151   bool can_jump;
152   bool can_flap;
153   bool falling_from_flap;
154   bool enable_hover;
155   bool butt_jump;
156   
157   float flapping_velocity;
158
159   // Ricardo's flapping
160   int flaps_nb;
161
162   // temporary to help player's choosing a flapping
163   enum { MAREK_FLAP, RICARDO_FLAP, RYAN_FLAP, NONE_FLAP };
164   int flapping_mode;
165
166   Timer2 invincible_timer;
167   Timer2 skidding_timer;
168   Timer2 safe_timer;
169   Timer2 kick_timer;
170   Timer2 shooting_timer;   // used to show the arm when Tux is shooting
171   Timer2 dying_timer;
172   Timer2 growing_timer;
173   Timer2 idle_timer;
174   Timer2 flapping_timer;
175   Physic physic;
176   
177 public:
178   Player();
179   virtual ~Player();
180   
181   bool key_event(SDLKey key, bool state);
182   void level_begin();
183   void handle_input();
184
185   PlayerStatus& get_status();
186
187   virtual void action(float elapsed_time);
188   virtual void draw(DrawingContext& context);
189   virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
190
191   void make_invincible();
192   bool is_invincible() const
193   {
194     return invincible_timer.started();
195   }
196   bool is_dying() const
197   {
198     return dying;
199   }
200   
201   void kill(HurtMode mode);
202   void player_remove_powerups();
203   void check_bounds(Camera* camera);
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   bool on_ground();
214   
215   void init();
216   
217   void handle_horizontal_input();
218   void handle_vertical_input();
219   void remove_powerups();
220
221   Portable* grabbed_object;
222
223   Sprite* smalltux_gameover;
224   Sprite* smalltux_star;
225   Sprite* bigtux_star;
226 };
227
228 #endif /*SUPERTUX_PLAYER_H*/