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