fix menu width, didn't committ the tileset fix, miscelaneous small fixes
[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 "scripting/player.hpp"
33 #include "player_status.hpp"
34 #include "display_effect.hpp"
35 #include "script_interface.hpp"
36 #include "console.hpp"
37 #include "coin.hpp"
38
39 class BadGuy;
40 class Portable;
41
42 /* Times: */
43 static const float TUX_SAFE_TIME = 1.8;
44 static const float TUX_INVINCIBLE_TIME = 10.0;
45 static const float TUX_INVINCIBLE_TIME_WARNING = 2.0;
46 static const float GROWING_TIME = 1.0;
47 static const int GROWING_FRAMES = 7;
48
49 class Camera;
50 class PlayerStatus;
51
52 extern Surface* growingtux_left[GROWING_FRAMES];
53 extern Surface* growingtux_right[GROWING_FRAMES];
54
55 class TuxBodyParts
56 {
57 public:
58   TuxBodyParts()
59     : head(0), body(0), arms(0), feet(0)
60   { }
61   ~TuxBodyParts() {
62     delete head;
63     delete body;
64     delete arms;
65     delete feet;
66   }
67
68   void set_action(std::string action, int loops = -1);
69   void one_time_animation();
70   void draw(DrawingContext& context, const Vector& pos, int layer);
71
72   Sprite* head;
73   Sprite* body;
74   Sprite* arms;
75   Sprite* feet;
76 };
77
78 extern TuxBodyParts* small_tux;
79 extern TuxBodyParts* big_tux;
80 extern TuxBodyParts* fire_tux;
81 extern TuxBodyParts* ice_tux;
82
83 class FallingCoin : public GameObject
84 {
85 public:
86   FallingCoin(const Vector& start_position, const int x_vel);
87   ~FallingCoin();
88
89   void draw(DrawingContext& context);
90   void update(float elapsed_time);
91 private:
92   Vector  pos;
93   Sprite* sprite;
94   Physic  physic;
95 };
96
97 class Player : public MovingObject, public Scripting::Player, public ScriptInterface
98 {
99 public:
100   enum HurtMode { KILL, SHRINK };
101   enum FallMode { ON_GROUND, JUMPING, TRAMPOLINE_JUMP, FALLING };
102
103   Controller* controller;
104   PlayerStatus* player_status;
105   bool duck;
106   bool dead;
107
108 private:
109   bool dying;
110   bool backflipping;
111   int  backflip_direction;
112   
113 public:
114   Direction dir;
115   Direction old_dir;
116
117   float last_ground_y;
118   FallMode fall_mode;
119
120   bool on_ground_flag;
121   bool jumping;
122   bool can_jump;
123   bool butt_jump;
124
125   Timer invincible_timer;
126   Timer skidding_timer;
127   Timer safe_timer;
128   Timer kick_timer;
129   Timer shooting_timer;   // used to show the arm when Tux is shooting
130   Timer dying_timer;
131   Timer growing_timer;
132   Timer idle_timer;
133   Timer backflip_timer;
134   Physic physic;
135   
136 public:
137   Player(PlayerStatus* player_status);
138   virtual ~Player();
139
140   virtual void expose(HSQUIRRELVM vm, int table_idx);
141   virtual void unexpose(HSQUIRRELVM vm, int table_idx);
142
143   void set_controller(Controller* controller);  
144
145   virtual void update(float elapsed_time);
146   virtual void draw(DrawingContext& context);
147   virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
148   virtual void collision_tile(uint32_t tile_attributes);
149
150   void make_invincible();
151   bool is_invincible() const
152   {
153     return invincible_timer.started();
154   }
155   bool is_dying() const
156   {
157     return dying;
158   }
159   
160   void kill(HurtMode mode);
161   void check_bounds(Camera* camera);
162   void move(const Vector& vector);
163
164   virtual void set_bonus(const std::string& bonus);
165   virtual void add_coins(int count);
166   void set_bonus(BonusType type, bool animate = false);
167   PlayerStatus* get_status()
168   {
169     return player_status;
170   }
171   // set kick animation
172   void kick();
173
174   /**
175    * Adds velocity to the player (be carefull when using this)
176    */
177   void add_velocity(const Vector& velocity);
178
179   void bounce(BadGuy& badguy);
180
181   bool is_dead() const
182   { return dead; }
183   bool is_big();
184
185   void set_visible(bool visible);
186   bool get_visible();
187
188   bool on_ground();
189
190 private:
191   void handle_input();
192   bool deactivated;
193   
194   void init();
195   
196   void handle_horizontal_input();
197   void handle_vertical_input();
198
199   void activate();
200   void deactivate();
201   void walk(float speed);
202
203   bool visible;
204
205   float adjust_height;
206
207   Portable* grabbed_object;
208
209   Sprite* smalltux_gameover;
210   Sprite* smalltux_star;
211   Sprite* bigtux_star;
212   Vector floor_normal;
213 };
214
215 #endif /*SUPERTUX_PLAYER_H*/