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);
66 * Level solved. Don't kill Tux any more.
74 Controller* get_controller()
79 void use_scripting_controller(bool use_or_release);
80 void do_scripting_controller(std::string control, bool pressed);
82 virtual void update(float elapsed_time);
83 virtual void draw(DrawingContext& context);
84 virtual void collision_solid(const CollisionHit& hit);
85 virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
86 virtual void collision_tile(uint32_t tile_attributes);
88 void make_invincible();
89 bool is_invincible() const
91 return invincible_timer.started();
97 Direction peeking_direction_x() const
102 Direction peeking_direction_y() const
107 void kill(bool completely);
109 void move(const Vector& vector);
111 virtual bool add_bonus(const std::string& bonus);
112 virtual void add_coins(int count);
113 virtual int get_coins();
116 * picks up a bonus, taking care not to pick up lesser bonus items than we already have
118 * @returns true if the bonus has been set (or was already good enough)
119 * false if the bonus could not be set (for example no space for big tux)
121 bool add_bonus(BonusType type, bool animate = false);
123 * like add_bonus, but can also downgrade the bonus items carried
125 bool set_bonus(BonusType type, bool animate = false);
127 PlayerStatus* get_status()
129 return player_status;
131 // set kick animation
135 * play cheer animation.
136 * This might need some space and behave in an unpredictable way. Best to use this at level end.
141 * duck down if possible.
142 * this won't last long as long as input is enabled.
147 * stand back up if possible.
152 * do a backflip if possible.
157 * jump in the air if possible
158 * sensible values for yspeed are negative - unless we want to jump into the ground of course
160 void do_jump(float yspeed);
163 * Adds velocity to the player (be careful when using this)
165 void add_velocity(const Vector& velocity);
168 * Adds velocity to the player until given end speed is reached
170 void add_velocity(const Vector& velocity, const Vector& end_speed);
173 * Returns the current velocity of the player
175 Vector get_velocity();
177 void bounce(BadGuy& badguy);
183 void set_visible(bool visible);
188 Portable* get_grabbed_object() const
190 return grabbed_object;
194 grabbed_object = NULL;
198 * Switches ghost mode on/off.
199 * Lets Tux float around and through solid objects.
201 void set_ghost_mode(bool enable);
204 * Switches edit mode on/off.
205 * In edit mode, Tux will enter ghost_mode instead of dying.
207 void set_edit_mode(bool enable);
210 * Returns whether ghost mode is currently enabled
212 bool get_ghost_mode() { return ghost_mode; }
215 * Changes height of bounding box.
216 * Returns true if successful, false otherwise
218 bool adjust_height(float new_height);
221 * Orders the current GameSession to start a sequence
223 void trigger_sequence(std::string sequence_name);
226 * Requests that the player start climbing the given Climbable
228 void start_climbing(Climbable& climbable);
231 * Requests that the player stop climbing the given Climbable
233 void stop_climbing(Climbable& climbable);
235 Physic& get_physic() { return physic; }
239 void handle_input_ghost(); /**< input handling while in ghost mode */
240 void handle_input_climbing(); /**< input handling while climbing */
244 void handle_horizontal_input();
245 void handle_vertical_input();
249 void walk(float speed);
252 void early_jump_apex();
255 * slows Tux down a little, based on where he's standing
257 void apply_friction();
262 Controller* controller;
263 std::auto_ptr<CodeController> scripting_controller; /**< This controller is used when the Player is controlled via scripting */
264 PlayerStatus* player_status;
272 int backflip_direction;
277 Controller* scripting_controller_old; /**< Saves the old controller while the scripting_controller is used */
278 bool jump_early_apex;
292 Timer jump_button_timer; /**< started when player presses the jump button; runs until Tux jumps or JUMP_GRACE_TIME runs out */
296 Timer invincible_timer;
297 Timer skidding_timer;
300 Timer shooting_timer; // used to show the arm when Tux is shooting
303 Timer backflip_timer;
309 Portable* grabbed_object;
311 SpritePtr sprite; /**< The main sprite representing Tux */
313 SurfacePtr airarrow; /**< arrow indicating Tux' position when he's above the camera */
316 void position_grabbed_object();
319 bool ghost_mode; /**< indicates if Tux should float around and through solid objects */
320 bool edit_mode; /**< indicates if Tux should switch to ghost mode rather than dying */
322 Timer unduck_hurt_timer; /**< if Tux wants to stand up again after ducking and cannot, this timer is started */
325 unsigned int idle_stage;
327 Climbable* climbing; /**< Climbable object we are currently climbing, null if none */
330 Player(const Player&);
331 Player& operator=(const Player&);
334 #endif /*SUPERTUX_PLAYER_H*/