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