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