2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef HEADER_SUPERTUX_OBJECT_PLAYER_HPP
18 #define HEADER_SUPERTUX_OBJECT_PLAYER_HPP
20 #include "scripting/player.hpp"
21 #include "sprite/sprite_ptr.hpp"
22 #include "supertux/direction.hpp"
23 #include "supertux/moving_object.hpp"
24 #include "supertux/physic.hpp"
25 #include "supertux/player_status.hpp"
26 #include "supertux/script_interface.hpp"
27 #include "supertux/timer.hpp"
38 static const float TUX_SAFE_TIME = 1.8f;
39 static const float TUX_INVINCIBLE_TIME = 14.0f;
40 static const float TUX_INVINCIBLE_TIME_WARNING = 2.0f;
41 static const float GROWING_TIME = 0.35f;
42 static const int GROWING_FRAMES = 7;
47 class Player : public MovingObject,
48 public scripting::Player,
49 public ScriptInterface
52 enum FallMode { ON_GROUND, JUMPING, TRAMPOLINE_JUMP, FALLING };
53 //Tux can only go this fast. If set to 0 no special limit is used, only the default limits.
54 void set_speedlimit(float newlimit);
55 float get_speedlimit();
58 Player(PlayerStatus* player_status, const std::string& name);
61 virtual void expose(HSQUIRRELVM vm, SQInteger table_idx);
62 virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx);
64 void set_controller(Controller* controller);
65 Controller* get_controller()
70 void use_scripting_controller(bool use_or_release);
71 void do_scripting_controller(std::string control, bool pressed);
73 virtual void update(float elapsed_time);
74 virtual void draw(DrawingContext& context);
75 virtual void collision_solid(const CollisionHit& hit);
76 virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
77 virtual void collision_tile(uint32_t tile_attributes);
79 void make_invincible();
80 bool is_invincible() const
82 return invincible_timer.started();
88 Direction peeking_direction_x() const
93 Direction peeking_direction_y() const
98 void kill(bool completely);
99 void check_bounds(Camera* camera);
100 void move(const Vector& vector);
102 virtual bool add_bonus(const std::string& bonus);
103 virtual void add_coins(int count);
104 virtual int get_coins();
107 * picks up a bonus, taking care not to pick up lesser bonus items than we already have
109 * @returns true if the bonus has been set (or was already good enough)
110 * false if the bonus could not be set (for example no space for big tux)
112 bool add_bonus(BonusType type, bool animate = false);
114 * like add_bonus, but can also downgrade the bonus items carried
116 bool set_bonus(BonusType type, bool animate = false);
118 PlayerStatus* get_status()
120 return player_status;
122 // set kick animation
126 * play cheer animation.
127 * This might need some space and behave in an unpredictable way. Best to use this at level end.
132 * duck down if possible.
133 * this won't last long as long as input is enabled.
138 * stand back up if possible.
143 * do a backflip if possible.
148 * jump in the air if possible
149 * sensible values for yspeed are negative - unless we want to jump into the ground of course
151 void do_jump(float yspeed);
154 * Adds velocity to the player (be careful when using this)
156 void add_velocity(const Vector& velocity);
159 * Adds velocity to the player until given end speed is reached
161 void add_velocity(const Vector& velocity, const Vector& end_speed);
164 * Returns the current velocity of the player
166 Vector get_velocity();
168 void bounce(BadGuy& badguy);
174 void set_visible(bool visible);
179 Portable* get_grabbed_object() const
181 return grabbed_object;
185 grabbed_object = NULL;
189 * Switches ghost mode on/off.
190 * Lets Tux float around and through solid objects.
192 void set_ghost_mode(bool enable);
195 * Switches edit mode on/off.
196 * In edit mode, Tux will enter ghost_mode instead of dying.
198 void set_edit_mode(bool enable);
201 * Returns whether ghost mode is currently enabled
203 bool get_ghost_mode() { return ghost_mode; }
206 * Changes height of bounding box.
207 * Returns true if successful, false otherwise
209 bool adjust_height(float new_height);
212 * Orders the current GameSession to start a sequence
214 void trigger_sequence(std::string sequence_name);
217 * Requests that the player start climbing the given Climbable
219 void start_climbing(Climbable& climbable);
222 * Requests that the player stop climbing the given Climbable
224 void stop_climbing(Climbable& climbable);
226 Physic& get_physic() { return physic; }
230 void handle_input_ghost(); /**< input handling while in ghost mode */
231 void handle_input_climbing(); /**< input handling while climbing */
235 void handle_horizontal_input();
236 void handle_vertical_input();
240 void walk(float speed);
243 void early_jump_apex();
246 * slows Tux down a little, based on where he's standing
248 void apply_friction();
253 Controller* controller;
254 std::auto_ptr<CodeController> scripting_controller; /**< This controller is used when the Player is controlled via scripting */
255 PlayerStatus* player_status;
262 int backflip_direction;
267 Controller* scripting_controller_old; /**< Saves the old controller while the scripting_controller is used */
268 bool jump_early_apex;
282 Timer jump_button_timer; /**< started when player presses the jump button; runs until Tux jumps or JUMP_GRACE_TIME runs out */
286 Timer invincible_timer;
287 Timer skidding_timer;
290 Timer shooting_timer; // used to show the arm when Tux is shooting
293 Timer backflip_timer;
299 Portable* grabbed_object;
301 SpritePtr sprite; /**< The main sprite representing Tux */
303 SurfacePtr airarrow; /**< arrow indicating Tux' position when he's above the camera */
308 bool ghost_mode; /**< indicates if Tux should float around and through solid objects */
309 bool edit_mode; /**< indicates if Tux should switch to ghost mode rather than dying */
311 Timer unduck_hurt_timer; /**< if Tux wants to stand up again after ducking and cannot, this timer is started */
314 unsigned int idle_stage;
316 Climbable* climbing; /**< Climbable object we are currently climbing, null if none */
319 Player(const Player&);
320 Player& operator=(const Player&);
323 #endif /*SUPERTUX_PLAYER_H*/