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