22fb0f295a9463e6eed3f20d22b2e0c46da3c0b4
[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 "moving_object.h"
29 #include "sprite/sprite.h"
30 #include "physic.h"
31 #include "control/controller.h"
32 #include "player_status.h"
33
34 class BadGuy;
35 class Portable;
36
37 /* Times: */
38 static const float TUX_SAFE_TIME = 1.250;
39 static const float TUX_INVINCIBLE_TIME = 10.0;
40 static const float TUX_INVINCIBLE_TIME_WARNING = 2.0;
41 static const float TUX_FLAPPING_TIME = 1; /* How long Tux can flap his wings to gain additional jump height */
42 static const float GROWING_TIME = 1.0;
43 static const int GROWING_FRAMES = 7;
44
45 class Camera;
46 class PlayerStatus;
47
48 extern Surface* growingtux_left[GROWING_FRAMES];
49 extern Surface* growingtux_right[GROWING_FRAMES];
50
51 class TuxBodyParts
52 {
53 public:
54   TuxBodyParts()
55     : head(0), body(0), arms(0), feet(0)
56   { }
57   ~TuxBodyParts() {
58     delete head;
59     delete body;
60     delete arms;
61     delete feet;
62   }
63
64   void set_action(std::string action, int loops = -1);
65   void one_time_animation();
66   void draw(DrawingContext& context, const Vector& pos, int layer);
67
68   Sprite* head;
69   Sprite* body;
70   Sprite* arms;
71   Sprite* feet;
72 };
73
74 extern TuxBodyParts* small_tux;
75 extern TuxBodyParts* big_tux;
76 extern TuxBodyParts* fire_tux;
77 extern TuxBodyParts* ice_tux;
78
79 class Player : public MovingObject
80 {
81 public:
82   enum HurtMode { KILL, SHRINK };
83   enum FallMode { ON_GROUND, JUMPING, TRAMPOLINE_JUMP, FALLING };
84
85   Controller* controller;
86   PlayerStatus* player_status;
87   bool duck;
88   bool dead;
89
90 private:
91   bool dying;
92 public:
93
94   Direction dir;
95   Direction old_dir;
96
97   float last_ground_y;
98   FallMode fall_mode;
99
100   bool on_ground_flag;
101   bool jumping;
102   bool flapping;
103   bool can_jump;
104   bool can_flap;
105   bool falling_from_flap;
106   bool enable_hover;
107   bool butt_jump;
108   
109   float flapping_velocity;
110
111   // Ricardo's flapping
112   int flaps_nb;
113
114   // temporary to help player's choosing a flapping
115   enum { MAREK_FLAP, RICARDO_FLAP, RYAN_FLAP, NO_FLAP };
116   int flapping_mode;
117
118   Timer invincible_timer;
119   Timer skidding_timer;
120   Timer safe_timer;
121   Timer kick_timer;
122   Timer shooting_timer;   // used to show the arm when Tux is shooting
123   Timer dying_timer;
124   Timer growing_timer;
125   Timer idle_timer;
126   Timer flapping_timer;
127   Physic physic;
128   
129 public:
130   Player(PlayerStatus* player_status);
131   virtual ~Player();
132
133   void set_controller(Controller* controller);  
134
135   virtual void update(float elapsed_time);
136   virtual void draw(DrawingContext& context);
137   virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
138
139   void make_invincible();
140   bool is_invincible() const
141   {
142     return invincible_timer.started();
143   }
144   bool is_dying() const
145   {
146     return dying;
147   }
148   
149   void kill(HurtMode mode);
150   void check_bounds(Camera* camera);
151   void move(const Vector& vector);
152   void set_bonus(BonusType type, bool animate = false);
153   PlayerStatus* get_status()
154   {
155     return player_status;
156   }
157
158   void bounce(BadGuy& badguy);
159
160   bool is_dead() const
161   { return dead; }
162   bool is_big();
163   
164 private:
165   void handle_input();
166   bool on_ground();
167   
168   void init();
169   
170   void handle_horizontal_input();
171   void handle_vertical_input();
172
173   Portable* grabbed_object;
174
175   Sprite* smalltux_gameover;
176   Sprite* smalltux_star;
177   Sprite* bigtux_star;
178 };
179
180 #endif /*SUPERTUX_PLAYER_H*/