Renamed JoystickKeyboardController to InputManager
[supertux.git] / src / object / player.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU General Public License
6 //  as published by the Free Software Foundation; either version 2
7 //  of the License, or (at your option) any later version.
8 //
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.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
18 #include "object/player.hpp"
19
20 #include "audio/sound_manager.hpp"
21 #include "badguy/badguy.hpp"
22 #include "control/input_manager.hpp"
23 #include "math/random_generator.hpp"
24 #include "object/bullet.hpp"
25 #include "object/camera.hpp"
26 #include "object/display_effect.hpp"
27 #include "object/falling_coin.hpp"
28 #include "object/particles.hpp"
29 #include "object/portable.hpp"
30 #include "object/sprite_particle.hpp"
31 #include "scripting/squirrel_util.hpp"
32 #include "supertux/game_session.hpp"
33 #include "supertux/globals.hpp"
34 #include "supertux/sector.hpp"
35 #include "supertux/tile.hpp"
36 #include "trigger/climbable.hpp"
37
38 #include <math.h>
39
40 //#define SWIMMING
41
42 namespace {
43 static const int TILES_FOR_BUTTJUMP = 3;
44 static const float BUTTJUMP_MIN_VELOCITY_Y = 400.0f;
45 static const float SHOOTING_TIME = .150f;
46
47 /** number of idle stages, including standing */
48 static const unsigned int IDLE_STAGE_COUNT = 5;
49 /**
50  * how long to play each idle animation in milliseconds
51  * '0' means the sprite action is played once before moving onto the next
52  * animation
53  */
54 static const int IDLE_TIME[] = { 5000, 0, 2500, 0, 2500 };
55 /** idle stages */
56 static const std::string IDLE_STAGES[] =
57 { "stand",
58   "idle",
59   "stand",
60   "idle",
61   "stand" };
62
63 /** acceleration in horizontal direction when walking
64  * (all accelerations are in  pixel/s^2) */
65 static const float WALK_ACCELERATION_X = 300;
66 /** acceleration in horizontal direction when running */ 
67 static const float RUN_ACCELERATION_X = 400;
68 /** acceleration when skidding */
69 static const float SKID_XM = 200;
70 /** time of skidding in seconds */
71 static const float SKID_TIME = .3f;
72 /** maximum walk velocity (pixel/s) */
73 static const float MAX_WALK_XM = 230;
74 /** maximum run velocity (pixel/s) */
75 static const float MAX_RUN_XM = 320;
76 /** maximum horizontal climb velocity */
77 static const float MAX_CLIMB_XM = 96;
78 /** maximum vertical climb velocity */
79 static const float MAX_CLIMB_YM = 128;
80 /** instant velocity when tux starts to walk */
81 static const float WALK_SPEED = 100;
82
83 /** multiplied by WALK_ACCELERATION to give friction */
84 static const float NORMAL_FRICTION_MULTIPLIER = 1.5f;
85 /** multiplied by WALK_ACCELERATION to give friction */
86 static const float ICE_FRICTION_MULTIPLIER = 0.1f;
87 static const float ICE_ACCELERATION_MULTIPLIER = 0.25f;
88
89 /** time of the kick (kicking mriceblock) animation */
90 static const float KICK_TIME = .3f;
91 /** time of tux cheering (currently unused) */
92 static const float CHEER_TIME = 1.0f;
93
94 /** if Tux cannot unduck for this long, he will get hurt */
95 static const float UNDUCK_HURT_TIME = 0.25f;
96 /** gravity is higher after the jump key is released before
97     the apex of the jump is reached */
98 static const float JUMP_EARLY_APEX_FACTOR = 3.0;
99
100 static const float JUMP_GRACE_TIME = 0.25f; /**< time before hitting the ground that the jump button may be pressed (and still trigger a jump) */
101
102 /* Tux's collision rectangle */
103 static const float TUX_WIDTH = 31.8f;
104 static const float RUNNING_TUX_WIDTH = 34;
105 static const float SMALL_TUX_HEIGHT = 30.8f;
106 static const float BIG_TUX_HEIGHT = 62.8f;
107 static const float DUCKED_TUX_HEIGHT = 31.8f;
108
109 bool no_water = true;
110 }
111
112 Player::Player(PlayerStatus* _player_status, const std::string& name) :
113   deactivated(),
114   controller(),
115   scripting_controller(),
116   player_status(_player_status), 
117   duck(),
118   dead(),
119   dying(),
120   backflipping(),
121   backflip_direction(),
122   peekingX(),
123   peekingY(),
124   swimming(),
125   speedlimit(),
126   scripting_controller_old(0),
127   jump_early_apex(),
128   on_ice(),
129   ice_this_frame(),
130   dir(),
131   old_dir(),
132   last_ground_y(),
133   fall_mode(),
134   on_ground_flag(),
135   jumping(),
136   can_jump(),
137   jump_button_timer(), 
138   wants_buttjump(),
139   does_buttjump(),
140   invincible_timer(),
141   skidding_timer(),
142   safe_timer(),
143   kick_timer(),
144   shooting_timer(),
145   dying_timer(),
146   growing(),
147   backflip_timer(),
148   physic(),
149   visible(),
150   grabbed_object(NULL), 
151   sprite(),
152   airarrow(),
153   floor_normal(),
154   ghost_mode(false), 
155   edit_mode(false), 
156   unduck_hurt_timer(),
157   idle_timer(),
158   idle_stage(0),
159   climbing(0)
160 {
161   this->name = name;
162   controller = g_input_manager->get_controller();
163   scripting_controller.reset(new CodeController());
164   // if/when we have complete penny gfx, we can
165   // load those instead of Tux's sprite in the
166   // constructor
167   sprite = sprite_manager->create("images/creatures/tux/tux.sprite");
168   airarrow = Surface::create("images/engine/hud/airarrow.png");
169   idle_timer.start(IDLE_TIME[0]/1000.0f);
170
171   sound_manager->preload("sounds/bigjump.wav");
172   sound_manager->preload("sounds/jump.wav");
173   sound_manager->preload("sounds/hurt.wav");
174   sound_manager->preload("sounds/kill.wav");
175   sound_manager->preload("sounds/skid.wav");
176   sound_manager->preload("sounds/flip.wav");
177   sound_manager->preload("sounds/invincible_start.ogg");
178   sound_manager->preload("sounds/splash.ogg");
179
180   init();
181 }
182
183 Player::~Player()
184 {
185   if (climbing) stop_climbing(*climbing);
186 }
187
188 void
189 Player::init()
190 {
191   if(is_big())
192     set_size(TUX_WIDTH, BIG_TUX_HEIGHT);
193   else
194     set_size(TUX_WIDTH, SMALL_TUX_HEIGHT);
195
196   dir = RIGHT;
197   old_dir = dir;
198   duck = false;
199   dead = false;
200
201   dying = false;
202   winning = false;
203   peekingX = AUTO;
204   peekingY = AUTO;
205   last_ground_y = 0;
206   fall_mode = ON_GROUND;
207   jumping = false;
208   jump_early_apex = false;
209   can_jump = true;
210   wants_buttjump = false;
211   does_buttjump = false;
212   growing = false;
213   deactivated = false;
214   backflipping = false;
215   backflip_direction = 0;
216   sprite->set_angle(0.0f);
217   visible = true;
218   swimming = false;
219   on_ice = false;
220   ice_this_frame = false;
221   speedlimit = 0; //no special limit
222
223   on_ground_flag = false;
224   grabbed_object = NULL;
225
226   climbing = 0;
227
228   physic.reset();
229 }
230
231 void
232 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
233 {
234   if (name.empty())
235     return;
236
237   scripting::expose_object(vm, table_idx, dynamic_cast<scripting::Player *>(this), name, false);
238 }
239
240 void
241 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
242 {
243   if (name.empty())
244     return;
245
246   scripting::unexpose_object(vm, table_idx, name);
247 }
248
249 float
250 Player::get_speedlimit()
251 {
252   return speedlimit;
253 }
254
255 void
256 Player::set_speedlimit(float newlimit)
257 {
258   speedlimit=newlimit;
259 }
260
261 void
262 Player::set_controller(Controller* controller)
263 {
264   this->controller = controller;
265 }
266
267 void
268 Player::set_winning()
269 {
270   if( ! is_winning() ){
271     winning = true;
272     invincible_timer.start(10000.0f);
273   }
274 }
275
276 void 
277 Player::use_scripting_controller(bool use_or_release)
278 {
279   if ((use_or_release == true) && (controller != scripting_controller.get())) {
280     scripting_controller_old = get_controller();
281     set_controller(scripting_controller.get());
282   }
283   if ((use_or_release == false) && (controller == scripting_controller.get())) {
284     set_controller(scripting_controller_old);
285     scripting_controller_old = 0;
286   }
287 }
288
289 void 
290 Player::do_scripting_controller(std::string control, bool pressed)
291 {
292   for(int i = 0; Controller::controlNames[i] != 0; ++i) {
293     if(control == std::string(Controller::controlNames[i])) {
294       scripting_controller->press(Controller::Control(i), pressed);
295     }
296   }
297 }
298
299 bool
300 Player::adjust_height(float new_height)
301 {
302   Rectf bbox2 = bbox;
303   bbox2.move(Vector(0, bbox.get_height() - new_height));
304   bbox2.set_height(new_height);
305
306
307   if(new_height > bbox.get_height()) {
308     Rectf additional_space = bbox2;
309     additional_space.set_height(new_height - bbox.get_height());
310     if(!Sector::current()->is_free_of_statics(additional_space, this, true))
311       return false;
312   }
313
314   // adjust bbox accordingly
315   // note that we use members of moving_object for this, so we can run this during CD, too
316   set_pos(bbox2.p1);
317   set_size(bbox2.get_width(), bbox2.get_height());
318   return true;
319 }
320
321 void
322 Player::trigger_sequence(std::string sequence_name)
323 {
324   if (climbing) stop_climbing(*climbing);
325   backflipping = false;
326   backflip_direction = 0;
327   sprite->set_angle(0.0f);
328   GameSession::current()->start_sequence(sequence_name);
329 }
330
331 void
332 Player::update(float elapsed_time)
333 {
334   if( no_water ){
335     swimming = false;
336   }
337   no_water = true;
338
339   if(dying && dying_timer.check()) {
340     set_bonus(NO_BONUS, true);
341     dead = true;
342     return;
343   }
344
345   if(!dying && !deactivated)
346     handle_input();
347
348 /*
349   // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
350   if (deactivated)
351     apply_friction();
352 */
353
354   // extend/shrink tux collision rectangle so that we fall through/walk over 1
355   // tile holes
356   if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
357     set_width(RUNNING_TUX_WIDTH);
358   } else {
359     set_width(TUX_WIDTH);
360   }
361
362   // on downward slopes, adjust vertical velocity so tux walks smoothly down
363   if (on_ground() && !dying) {
364     if(floor_normal.y != 0) {
365       if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
366         physic.set_velocity_y(250);
367       }
368     }
369   }
370
371   // handle backflipping
372   if (backflipping && !dying) {
373     //prevent player from changing direction when backflipping
374     dir = (backflip_direction == 1) ? LEFT : RIGHT;
375     if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
376     //rotate sprite during flip
377     sprite->set_angle(sprite->get_angle() + (dir==LEFT?1:-1) * elapsed_time * (360.0f / 0.5f));
378   }
379
380   // set fall mode...
381   if(on_ground()) {
382     fall_mode = ON_GROUND;
383     last_ground_y = get_pos().y;
384   } else {
385     if(get_pos().y > last_ground_y)
386       fall_mode = FALLING;
387     else if(fall_mode == ON_GROUND)
388       fall_mode = JUMPING;
389   }
390
391   // check if we landed
392   if(on_ground()) {
393     jumping = false;
394     if (backflipping && (backflip_timer.get_timegone() > 0.15f)) {
395       backflipping = false;
396       backflip_direction = 0;
397       sprite->set_angle(0.0f);
398
399       // if controls are currently deactivated, we take care of standing up ourselves
400       if (deactivated)
401         do_standup();
402     }
403   }
404
405   // calculate movement for this frame
406   movement = physic.get_movement(elapsed_time);
407
408   if(grabbed_object != NULL && !dying) {
409     position_grabbed_object();
410   }
411
412   if(grabbed_object != NULL && dying){
413     grabbed_object->ungrab(*this, dir);
414     grabbed_object = NULL;
415   }
416
417   if(!ice_this_frame && on_ground())
418     on_ice = false;
419
420   on_ground_flag = false;
421   ice_this_frame = false;
422
423   // when invincible, spawn particles
424   if (invincible_timer.started())
425   {
426     if (graphicsRandom.rand(0, 2) == 0) {
427       float px = graphicsRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
428       float py = graphicsRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
429       Vector ppos = Vector(px, py);
430       Vector pspeed = Vector(0, 0);
431       Vector paccel = Vector(0, 0);
432       Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", 
433                                                        // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
434                                                        (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) ?
435                                                        // make every other a longer sparkle to make trail a bit fuzzy
436                                                        (size_t(game_time*20)%2) ? "small" : "medium"
437                                                        :
438                                                        "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
439     }
440   }
441
442   if (growing) {
443     if (sprite->animation_done()) growing = false;
444   }
445
446   // when climbing animate only while moving
447   if(climbing){
448     if((physic.get_velocity_x()==0)&&(physic.get_velocity_y()==0))
449       sprite->stop_animation();
450     else
451       sprite->set_animation_loops(-1);
452   }
453
454 }
455
456 bool
457 Player::on_ground()
458 {
459   return on_ground_flag;
460 }
461
462 bool
463 Player::is_big()
464 {
465   if(player_status->bonus == NO_BONUS)
466     return false;
467
468   return true;
469 }
470
471 void
472 Player::apply_friction()
473 {
474   if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
475     physic.set_velocity_x(0);
476     physic.set_acceleration_x(0);
477   } else {
478     float friction = WALK_ACCELERATION_X * (on_ice ? ICE_FRICTION_MULTIPLIER : NORMAL_FRICTION_MULTIPLIER);
479     if(physic.get_velocity_x() < 0) {
480       physic.set_acceleration_x(friction);
481     } else if(physic.get_velocity_x() > 0) {
482       physic.set_acceleration_x(-friction);
483     } // no friction for physic.get_velocity_x() == 0
484   }
485 }
486
487 void
488 Player::handle_horizontal_input()
489 {
490   float vx = physic.get_velocity_x();
491   float vy = physic.get_velocity_y();
492   float ax = physic.get_acceleration_x();
493   float ay = physic.get_acceleration_y();
494
495   float dirsign = 0;
496   if(!duck || physic.get_velocity_y() != 0) {
497     if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
498       old_dir = dir;
499       dir = LEFT;
500       dirsign = -1;
501     } else if(!controller->hold(Controller::LEFT)
502               && controller->hold(Controller::RIGHT)) {
503       old_dir = dir;
504       dir = RIGHT;
505       dirsign = 1;
506     }
507   }
508
509   // do not run if we're holding something which slows us down
510   if ( grabbed_object && grabbed_object->is_hampering() ) {
511     ax = dirsign * WALK_ACCELERATION_X;
512     // limit speed
513     if(vx >= MAX_WALK_XM && dirsign > 0) {
514       vx = MAX_WALK_XM;
515       ax = 0;
516     } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
517       vx = -MAX_WALK_XM;
518       ax = 0;
519     }
520   } else {
521     if( vx * dirsign < MAX_WALK_XM ) {
522       ax = dirsign * WALK_ACCELERATION_X;
523     } else {
524       ax = dirsign * RUN_ACCELERATION_X;
525     }
526     // limit speed
527     if(vx >= MAX_RUN_XM && dirsign > 0) {
528       vx = MAX_RUN_XM;
529       ax = 0;
530     } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
531       vx = -MAX_RUN_XM;
532       ax = 0;
533     }
534   }
535
536   // we can reach WALK_SPEED without any acceleration
537   if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
538     vx = dirsign * WALK_SPEED;
539   }
540
541   //Check speedlimit.
542   if( speedlimit > 0 &&  vx * dirsign >= speedlimit ) {
543     vx = dirsign * speedlimit;
544     ax = 0;
545   }
546
547   // changing directions?
548   if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
549     // let's skid!
550     if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
551       skidding_timer.start(SKID_TIME);
552       sound_manager->play("sounds/skid.wav");
553       // dust some particles
554       Sector::current()->add_object(
555         new Particles(
556           Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
557           dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
558           Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
559           LAYER_OBJECTS+1));
560
561       ax *= 2.5;
562     } else {
563       ax *= 2;
564     }
565   }
566
567   if(on_ice) {
568     ax *= ICE_ACCELERATION_MULTIPLIER;
569   }
570
571   physic.set_velocity(vx, vy);
572   physic.set_acceleration(ax, ay);
573
574   // we get slower when not pressing any keys
575   if(dirsign == 0) {
576     apply_friction();
577   }
578
579 }
580
581 void
582 Player::do_cheer()
583 {
584   do_duck();
585   do_backflip();
586   do_standup();
587 }
588
589 void
590 Player::do_duck() {
591   if (duck)
592     return;
593   if (!is_big())
594     return;
595
596   if (physic.get_velocity_y() != 0)
597     return;
598   if (!on_ground())
599     return;
600   if (does_buttjump)
601     return;
602
603   if (adjust_height(DUCKED_TUX_HEIGHT)) {
604     duck = true;
605     growing = false;
606     unduck_hurt_timer.stop();
607   } else {
608     // FIXME: what now?
609   }
610 }
611
612 void
613 Player::do_standup() {
614   if (!duck)
615     return;
616   if (!is_big())
617     return;
618   if (backflipping)
619     return;
620
621   if (adjust_height(BIG_TUX_HEIGHT)) {
622     duck = false;
623     unduck_hurt_timer.stop();
624   } else {
625     // if timer is not already running, start it.
626     if (unduck_hurt_timer.get_period() == 0) {
627       unduck_hurt_timer.start(UNDUCK_HURT_TIME);
628     }
629     else if (unduck_hurt_timer.check()) {
630       kill(false);
631     }
632   }
633
634 }
635
636 void
637 Player::do_backflip() {
638   if (!duck)
639     return;
640   if (!on_ground())
641     return;
642
643   backflip_direction = (dir == LEFT)?(+1):(-1);
644   backflipping = true;
645   do_jump(-580);
646   sound_manager->play("sounds/flip.wav");
647   backflip_timer.start(TUX_BACKFLIP_TIME);
648 }
649
650 void
651 Player::do_jump(float yspeed) {
652   if (!on_ground())
653     return;
654
655   physic.set_velocity_y(yspeed);
656   //bbox.move(Vector(0, -1));
657   jumping = true;
658   on_ground_flag = false;
659   can_jump = false;
660
661   // play sound
662   if (is_big()) {
663     sound_manager->play("sounds/bigjump.wav");
664   } else {
665     sound_manager->play("sounds/jump.wav");
666   }
667 }
668
669 void
670 Player::early_jump_apex() 
671 {
672   if (!jump_early_apex)
673   {
674     jump_early_apex = true;
675     physic.set_gravity_modifier(JUMP_EARLY_APEX_FACTOR);
676   }
677 }
678
679 void
680 Player::do_jump_apex()
681 {
682   if (jump_early_apex)
683   {
684     jump_early_apex = false;
685     physic.set_gravity_modifier(1.0f);
686   }
687 }
688
689 void
690 Player::handle_vertical_input()
691 {
692   // Press jump key
693   if(controller->pressed(Controller::JUMP)) jump_button_timer.start(JUMP_GRACE_TIME);
694   if(controller->hold(Controller::JUMP) && jump_button_timer.started() && can_jump) {
695     jump_button_timer.stop();
696     if (duck) {
697       // when running, only jump a little bit; else do a backflip
698       if ((physic.get_velocity_x() != 0) || 
699           (controller->hold(Controller::LEFT)) || 
700           (controller->hold(Controller::RIGHT))) 
701       {
702         do_jump(-300);
703       }
704       else 
705       {
706         do_backflip();
707       }
708     } else {
709       // jump a bit higher if we are running; else do a normal jump
710       if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
711     }
712   }
713   // Let go of jump key
714   else if(!controller->hold(Controller::JUMP)) {
715     if (!backflipping && jumping && physic.get_velocity_y() < 0) {
716       jumping = false;
717       early_jump_apex();
718     }
719   }
720
721   if(jump_early_apex && physic.get_velocity_y() >= 0) {
722     do_jump_apex();
723   }
724
725   /* In case the player has pressed Down while in a certain range of air,
726      enable butt jump action */
727   if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
728     wants_buttjump = true;
729     if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
730   }
731
732   /* When Down is not held anymore, disable butt jump */
733   if(!controller->hold(Controller::DOWN)) {
734     wants_buttjump = false;
735     does_buttjump = false;
736   }
737
738   // swimming
739   physic.set_acceleration_y(0);
740 #ifdef SWIMMING
741   if (swimming) {
742     if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
743       physic.set_acceleration_y(-2000);
744     physic.set_velocity_y(physic.get_velocity_y() * 0.94);
745   }
746 #endif
747 }
748
749 void
750 Player::handle_input()
751 {
752   if (ghost_mode) {
753     handle_input_ghost();
754     return;
755   }
756   if (climbing) {
757     handle_input_climbing();
758     return;
759   }
760
761   /* Peeking */
762   if( controller->released( Controller::PEEK_LEFT ) || controller->released( Controller::PEEK_RIGHT ) ) {
763     peekingX = AUTO;
764   }
765   if( controller->released( Controller::PEEK_UP ) || controller->released( Controller::PEEK_DOWN ) ) {
766     peekingY = AUTO;
767   }
768   if( controller->pressed( Controller::PEEK_LEFT ) ) {
769     peekingX = LEFT;
770   }
771   if( controller->pressed( Controller::PEEK_RIGHT ) ) {
772     peekingX = RIGHT;
773   }
774   if(!backflipping && !jumping && on_ground()) {
775     if( controller->pressed( Controller::PEEK_UP ) ) {
776       peekingY = UP;
777     } else if( controller->pressed( Controller::PEEK_DOWN ) ) {
778       peekingY = DOWN;
779     }
780   }
781
782   /* Handle horizontal movement: */
783   if (!backflipping) handle_horizontal_input();
784
785   /* Jump/jumping? */
786   if (on_ground())
787     can_jump = true;
788
789   /* Handle vertical movement: */
790   handle_vertical_input();
791
792   /* Shoot! */
793   if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
794     if(Sector::current()->add_bullet(
795          get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
796                       : Vector(32, bbox.get_height()/2)),
797          player_status,
798          physic.get_velocity_x(), dir))
799       shooting_timer.start(SHOOTING_TIME);
800   }
801
802   /* Duck or Standup! */
803   if (controller->hold(Controller::DOWN)) {
804     do_duck();
805   } else {
806     do_standup();
807   }
808
809   /* grabbing */
810   try_grab();
811
812   if(!controller->hold(Controller::ACTION) && grabbed_object) {
813     MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
814     if(moving_object) {
815       // move the grabbed object a bit away from tux
816       Rectf grabbed_bbox = moving_object->get_bbox();
817       Rectf dest;
818       dest.p2.y = bbox.get_top() + bbox.get_height()*0.66666;
819       dest.p1.y = dest.p2.y - grabbed_bbox.get_height();
820       if(dir == LEFT) {
821         dest.p2.x = bbox.get_left() - 1;
822         dest.p1.x = dest.p2.x - grabbed_bbox.get_width();
823       } else {
824         dest.p1.x = bbox.get_right() + 1;
825         dest.p2.x = dest.p1.x + grabbed_bbox.get_width();
826       }
827       if(Sector::current()->is_free_of_tiles(dest, true)) {
828         moving_object->set_pos(dest.p1);
829         if(controller->hold(Controller::UP)) {
830           grabbed_object->ungrab(*this, UP);
831         } else {
832           grabbed_object->ungrab(*this, dir);
833         }
834         grabbed_object = NULL;
835       }
836     } else {
837       log_debug << "Non MovingObject grabbed?!?" << std::endl;
838     }
839   }
840
841   /* stop backflipping at will */
842   if( backflipping && ( !controller->hold(Controller::JUMP) && !backflip_timer.started()) ){
843     backflipping = false;
844     backflip_direction = 0;
845     sprite->set_angle(0.0f);
846   }
847 }
848
849 void
850 Player::position_grabbed_object()
851 {
852   MovingObject* moving_object = dynamic_cast<MovingObject*>(grabbed_object);
853   assert(moving_object);
854
855   // Position where we will hold the lower-inner corner
856   Vector pos(get_bbox().get_left() + get_bbox().get_width()/2,
857       get_bbox().get_top() + get_bbox().get_height()*0.66666);
858
859   // Adjust to find the grabbed object's upper-left corner
860   if (dir == LEFT)
861     pos.x -= moving_object->get_bbox().get_width();
862   pos.y -= moving_object->get_bbox().get_height();
863
864   grabbed_object->grab(*this, pos, dir);
865 }
866
867 void
868 Player::try_grab()
869 {
870   if(controller->hold(Controller::ACTION) && !grabbed_object
871      && !duck) {
872     Sector* sector = Sector::current();
873     Vector pos;
874     if(dir == LEFT) {
875       pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
876     } else {
877       pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
878     }
879
880     for(Sector::Portables::iterator i = sector->portables.begin();
881         i != sector->portables.end(); ++i) {
882       Portable* portable = *i;
883       if(!portable->is_portable())
884         continue;
885
886       // make sure the Portable is a MovingObject
887       MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
888       assert(moving_object);
889       if(moving_object == NULL)
890         continue;
891
892       // make sure the Portable isn't currently non-solid
893       if(moving_object->get_group() == COLGROUP_DISABLED) continue;
894
895       // check if we are within reach
896       if(moving_object->get_bbox().contains(pos)) {
897         if (climbing) stop_climbing(*climbing);
898         grabbed_object = portable;
899         position_grabbed_object();
900         break;
901       }
902     }
903   }
904 }
905
906 void
907 Player::handle_input_ghost()
908 {
909   float vx = 0;
910   float vy = 0;
911   if (controller->hold(Controller::LEFT)) {
912     dir = LEFT;
913     vx -= MAX_RUN_XM * 2;
914   }
915   if (controller->hold(Controller::RIGHT)) {
916     dir = RIGHT;
917     vx += MAX_RUN_XM * 2;
918   }
919   if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
920     vy -= MAX_RUN_XM * 2;
921   }
922   if (controller->hold(Controller::DOWN)) {
923     vy += MAX_RUN_XM * 2;
924   }
925   if (controller->hold(Controller::ACTION)) {
926     set_ghost_mode(false);
927   }
928   physic.set_velocity(vx, vy);
929   physic.set_acceleration(0, 0);
930 }
931
932 void
933 Player::add_coins(int count)
934 {
935   player_status->add_coins(count);
936 }
937
938 int
939 Player::get_coins()
940 {
941   return player_status->coins;
942 }
943
944 bool
945 Player::add_bonus(const std::string& bonustype)
946 {
947   BonusType type = NO_BONUS;
948
949   if(bonustype == "grow") {
950     type = GROWUP_BONUS;
951   } else if(bonustype == "fireflower") {
952     type = FIRE_BONUS;
953   } else if(bonustype == "iceflower") {
954     type = ICE_BONUS;
955   } else if(bonustype == "none") {
956     type = NO_BONUS;
957   } else {
958     std::ostringstream msg;
959     msg << "Unknown bonus type "  << bonustype;
960     throw std::runtime_error(msg.str());
961   }
962
963   return add_bonus(type);
964 }
965
966 bool
967 Player::add_bonus(BonusType type, bool animate)
968 {
969   // always ignore NO_BONUS
970   if (type == NO_BONUS) {
971     return true;
972   }
973
974   // ignore GROWUP_BONUS if we're already big
975   if (type == GROWUP_BONUS) {
976     if (player_status->bonus == GROWUP_BONUS)
977       return true;
978     if (player_status->bonus == FIRE_BONUS)
979       return true;
980     if (player_status->bonus == ICE_BONUS)
981       return true;
982   }
983
984   return set_bonus(type, animate);
985 }
986
987 bool
988 Player::set_bonus(BonusType type, bool animate)
989 {
990   if((player_status->bonus == NO_BONUS) && (type != NO_BONUS)) {
991     if (!adjust_height(BIG_TUX_HEIGHT)) {
992       log_debug << "Can't adjust Tux height" << std::endl;
993       return false;
994     }
995     if(animate) {
996       growing = true;
997       sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
998     }
999     if (climbing) stop_climbing(*climbing);
1000   }
1001
1002   if (type == NO_BONUS) {
1003     if (does_buttjump) does_buttjump = false;
1004   }
1005
1006   if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
1007     if ((player_status->bonus == FIRE_BONUS) && (animate)) {
1008       // visually lose helmet
1009       Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
1010       Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
1011       Vector paccel = Vector(0, 1000);
1012       std::string action = (dir==LEFT)?"left":"right";
1013       Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
1014       if (climbing) stop_climbing(*climbing);
1015     }
1016     if ((player_status->bonus == ICE_BONUS) && (animate)) {
1017       // visually lose cap
1018       Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
1019       Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
1020       Vector paccel = Vector(0, 1000);
1021       std::string action = (dir==LEFT)?"left":"right";
1022       Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
1023       if (climbing) stop_climbing(*climbing);
1024     }
1025     player_status->max_fire_bullets = 0;
1026     player_status->max_ice_bullets = 0;
1027   }
1028   if (type == FIRE_BONUS) player_status->max_fire_bullets++;
1029   if (type == ICE_BONUS) player_status->max_ice_bullets++;
1030
1031   player_status->bonus = type;
1032   return true;
1033 }
1034
1035 void
1036 Player::set_visible(bool visible)
1037 {
1038   this->visible = visible;
1039   if( visible )
1040     set_group(COLGROUP_MOVING);
1041   else
1042     set_group(COLGROUP_DISABLED);
1043 }
1044
1045 bool
1046 Player::get_visible()
1047 {
1048   return visible;
1049 }
1050
1051 void
1052 Player::kick()
1053 {
1054   kick_timer.start(KICK_TIME);
1055 }
1056
1057 void
1058 Player::draw(DrawingContext& context)
1059 {
1060   if(!visible)
1061     return;
1062
1063   // if Tux is above camera, draw little "air arrow" to show where he is x-wise
1064   if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
1065     float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
1066     float py = Sector::current()->camera->get_translation().y;
1067     py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
1068     context.draw_surface(airarrow, Vector(px, py), LAYER_HUD - 1);
1069   }
1070
1071   std::string sa_prefix = "";
1072   std::string sa_postfix = "";
1073
1074   if (player_status->bonus == GROWUP_BONUS)
1075     sa_prefix = "big";
1076   else if (player_status->bonus == FIRE_BONUS)
1077     sa_prefix = "fire";
1078   else if (player_status->bonus == ICE_BONUS)
1079     sa_prefix = "ice";
1080   else
1081     sa_prefix = "small";
1082
1083   if(dir == LEFT)
1084     sa_postfix = "-left";
1085   else
1086     sa_postfix = "-right";
1087
1088   /* Set Tux sprite action */
1089   if(dying) {
1090     sprite->set_action("gameover");
1091   }
1092   else if (growing) {
1093     sprite->set_action_continued("grow"+sa_postfix);
1094     // while growing, do not change action
1095     // do_duck() will take care of cancelling growing manually
1096     // update() will take care of cancelling when growing completed
1097   }
1098   else if (climbing) {
1099     sprite->set_action(sa_prefix+"-climbing"+sa_postfix);
1100   }
1101   else if (backflipping) {
1102     sprite->set_action(sa_prefix+"-backflip"+sa_postfix);
1103   }
1104   else if (duck && is_big()) {
1105     sprite->set_action(sa_prefix+"-duck"+sa_postfix);
1106   }
1107   else if (skidding_timer.started() && !skidding_timer.check()) {
1108     sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1109   }
1110   else if (kick_timer.started() && !kick_timer.check()) {
1111     sprite->set_action(sa_prefix+"-kick"+sa_postfix);
1112   }
1113   else if ((wants_buttjump || does_buttjump) && is_big()) {
1114     sprite->set_action(sa_prefix+"-buttjump"+sa_postfix);
1115   }
1116   else if (!on_ground() || fall_mode != ON_GROUND) {
1117     if(physic.get_velocity_x() != 0 || fall_mode != ON_GROUND) {
1118         sprite->set_action(sa_prefix+"-jump"+sa_postfix);
1119     }
1120   }
1121   else {
1122     if (fabsf(physic.get_velocity_x()) < 1.0f) {
1123       // Determine which idle stage we're at
1124       if (sprite->get_action().find("-stand-") == std::string::npos && sprite->get_action().find("-idle-") == std::string::npos) {
1125         idle_stage = 0;
1126         idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1127
1128         sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1129       }
1130       else if (idle_timer.check() || (IDLE_TIME[idle_stage] == 0 && sprite->animation_done())) {
1131         idle_stage++;
1132         if (idle_stage >= IDLE_STAGE_COUNT)
1133           idle_stage = 1;
1134
1135         idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1136
1137         if (IDLE_TIME[idle_stage] == 0)
1138           sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix, 1);
1139         else
1140           sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1141       }
1142       else {
1143         sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1144       }
1145     }
1146     else {
1147       sprite->set_action(sa_prefix+"-walk"+sa_postfix);
1148     }
1149   }
1150
1151   /*
1152   // Tux is holding something
1153   if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1154   (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
1155   if (duck) {
1156   } else {
1157   }
1158   }
1159   */
1160
1161   /* Draw Tux */
1162   if (safe_timer.started() && size_t(game_time*40)%2)
1163     ;  // don't draw Tux
1164   else {
1165     sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1166   }
1167
1168 }
1169
1170 void
1171 Player::collision_tile(uint32_t tile_attributes)
1172 {
1173   if(tile_attributes & Tile::HURTS)
1174     kill(false);
1175
1176 #ifdef SWIMMING
1177   if( swimming ){
1178     if( tile_attributes & Tile::WATER ){
1179       no_water = false;
1180     } else {
1181       swimming = false;
1182     }
1183   } else {
1184     if( tile_attributes & Tile::WATER ){
1185       swimming = true;
1186       no_water = false;
1187       sound_manager->play( "sounds/splash.ogg" );
1188     }
1189   }
1190 #endif
1191
1192   if(tile_attributes & Tile::ICE) {
1193     ice_this_frame = true;
1194     on_ice = true;
1195   }
1196 }
1197
1198 void
1199 Player::collision_solid(const CollisionHit& hit)
1200 {
1201   if(hit.bottom) {
1202     if(physic.get_velocity_y() > 0)
1203       physic.set_velocity_y(0);
1204
1205     on_ground_flag = true;
1206     floor_normal = hit.slope_normal;
1207
1208     // Butt Jump landed    
1209     if (does_buttjump) {
1210       does_buttjump = false;
1211       physic.set_velocity_y(-300);
1212       on_ground_flag = false;
1213       Sector::current()->add_object(new Particles(
1214                                       Vector(get_bbox().p2.x, get_bbox().p2.y),
1215                                       270+20, 270+40,
1216                                       Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1217                                       LAYER_OBJECTS+1));
1218       Sector::current()->add_object(new Particles(
1219                                       Vector(get_bbox().p1.x, get_bbox().p2.y),
1220                                       90-40, 90-20,
1221                                       Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1222                                       LAYER_OBJECTS+1));
1223       Sector::current()->camera->shake(.1f, 0, 5);
1224     }
1225
1226   } else if(hit.top) {
1227     if(physic.get_velocity_y() < 0)
1228       physic.set_velocity_y(.2f);
1229   }
1230
1231   if(hit.left || hit.right) {
1232     physic.set_velocity_x(0);
1233   }
1234
1235   // crushed?
1236   if(hit.crush) {
1237     if(hit.left || hit.right) {
1238       kill(true);
1239     } else if(hit.top || hit.bottom) {
1240       kill(false);
1241     }
1242   }
1243 }
1244
1245 HitResponse
1246 Player::collision(GameObject& other, const CollisionHit& hit)
1247 {
1248   Bullet* bullet = dynamic_cast<Bullet*> (&other);
1249   if(bullet) {
1250     return FORCE_MOVE;
1251   }
1252
1253   Player* player = dynamic_cast<Player*> (&other);
1254   if(player) {
1255     return ABORT_MOVE;
1256   }
1257
1258   if(hit.left || hit.right) {
1259     try_grab(); //grab objects right now, in update it will be too late
1260   }
1261   assert(dynamic_cast<MovingObject*> (&other) != NULL);
1262   MovingObject* moving_object = static_cast<MovingObject*> (&other);
1263   if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1264     TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1265     if(trigger) {
1266       if(controller->pressed(Controller::UP))
1267         trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1268     }
1269
1270     return FORCE_MOVE;
1271   }
1272
1273   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1274   if(badguy != NULL) {
1275     if(safe_timer.started() || invincible_timer.started())
1276       return FORCE_MOVE;
1277
1278     return CONTINUE;
1279   }
1280
1281   return CONTINUE;
1282 }
1283
1284 void
1285 Player::make_invincible()
1286 {
1287   sound_manager->play("sounds/invincible_start.ogg");
1288   invincible_timer.start(TUX_INVINCIBLE_TIME);
1289   Sector::current()->play_music(HERRING_MUSIC);
1290 }
1291
1292 /* Kill Player! */
1293 void
1294 Player::kill(bool completely)
1295 {
1296   if(dying || deactivated || is_winning() )
1297     return;
1298
1299   if(!completely && (safe_timer.started() || invincible_timer.started()))
1300     return;
1301
1302   growing = false;
1303
1304   if (climbing) stop_climbing(*climbing);
1305
1306   physic.set_velocity_x(0);
1307
1308   if(!completely && is_big()) {
1309     sound_manager->play("sounds/hurt.wav");
1310
1311     if(player_status->bonus == FIRE_BONUS
1312        || player_status->bonus == ICE_BONUS) {
1313       safe_timer.start(TUX_SAFE_TIME);
1314       set_bonus(GROWUP_BONUS, true);
1315     } else if(player_status->bonus == GROWUP_BONUS) {
1316       safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1317       adjust_height(SMALL_TUX_HEIGHT);
1318       duck = false;
1319       backflipping = false;
1320       sprite->set_angle(0.0f);
1321       set_bonus(NO_BONUS, true);
1322     } else if(player_status->bonus == NO_BONUS) {
1323       safe_timer.start(TUX_SAFE_TIME);
1324       adjust_height(SMALL_TUX_HEIGHT);
1325       duck = false;
1326     }
1327   } else {
1328     sound_manager->play("sounds/kill.wav");
1329
1330     // do not die when in edit mode
1331     if (edit_mode) {
1332       set_ghost_mode(true);
1333       return;
1334     }
1335
1336     if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1337     {
1338       for (int i = 0; i < 5; i++)
1339       {
1340         // the numbers: starting x, starting y, velocity y
1341         Sector::current()->add_object(new FallingCoin(get_pos() +
1342                                                       Vector(graphicsRandom.rand(5), graphicsRandom.rand(-32,18)),
1343                                                       graphicsRandom.rand(-100,100)));
1344       }
1345       player_status->coins -= std::max(player_status->coins/10, 25);
1346     }
1347     else
1348     {
1349       GameSession::current()->set_reset_point("", Vector());
1350     }
1351     physic.enable_gravity(true);
1352     physic.set_gravity_modifier(1.0f); // Undo jump_early_apex
1353     safe_timer.stop();
1354     invincible_timer.stop();
1355     physic.set_acceleration(0, 0);
1356     physic.set_velocity(0, -700);
1357     set_bonus(NO_BONUS, true);
1358     dying = true;
1359     dying_timer.start(3.0);
1360     set_group(COLGROUP_DISABLED);
1361
1362     // TODO: need nice way to handle players dying in co-op mode
1363     Sector::current()->effect->fade_out(3.0);
1364     sound_manager->stop_music(3.0);
1365   }
1366 }
1367
1368 void
1369 Player::move(const Vector& vector)
1370 {
1371   set_pos(vector);
1372
1373   // Reset size to get correct hitbox if Tux was eg. ducked before moving
1374   if(is_big())
1375     set_size(TUX_WIDTH, BIG_TUX_HEIGHT);
1376   else
1377     set_size(TUX_WIDTH, SMALL_TUX_HEIGHT);
1378   duck = false;
1379   backflipping = false;
1380   sprite->set_angle(0.0f);
1381   last_ground_y = vector.y;
1382   if (climbing) stop_climbing(*climbing);
1383
1384   physic.reset();
1385 }
1386
1387 void
1388 Player::check_bounds()
1389 {
1390   /* Keep tux in sector bounds: */
1391   if (get_pos().x < 0) {
1392     // Lock Tux to the size of the level, so that he doesn't fall off
1393     // the left side
1394     set_pos(Vector(0, get_pos().y));
1395   }
1396
1397   if (get_bbox().get_right() > Sector::current()->get_width()) {
1398     // Lock Tux to the size of the level, so that he doesn't fall off
1399     // the right side
1400     set_pos(Vector(Sector::current()->get_width() - get_bbox().get_width(), get_pos().y));
1401   }
1402
1403   /* fallen out of the level? */
1404   if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1405     kill(true);
1406     return;
1407   }
1408 }
1409
1410 void
1411 Player::add_velocity(const Vector& velocity)
1412 {
1413   physic.set_velocity(physic.get_velocity() + velocity);
1414 }
1415
1416 void
1417 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1418 {
1419   if (end_speed.x > 0)
1420     physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1421   if (end_speed.x < 0)
1422     physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1423   if (end_speed.y > 0)
1424     physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1425   if (end_speed.y < 0)
1426     physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1427 }
1428
1429 Vector 
1430 Player::get_velocity()
1431 {
1432   return physic.get_velocity();
1433 }
1434
1435 void
1436 Player::bounce(BadGuy& )
1437 {
1438   if(controller->hold(Controller::JUMP))
1439     physic.set_velocity_y(-520);
1440   else
1441     physic.set_velocity_y(-300);
1442 }
1443
1444 //scripting Functions Below
1445
1446 void
1447 Player::deactivate()
1448 {
1449   if (deactivated)
1450     return;
1451   deactivated = true;
1452   physic.set_velocity_x(0);
1453   physic.set_velocity_y(0);
1454   physic.set_acceleration_x(0);
1455   physic.set_acceleration_y(0);
1456   if (climbing) stop_climbing(*climbing);
1457 }
1458
1459 void
1460 Player::activate()
1461 {
1462   if (!deactivated)
1463     return;
1464   deactivated = false;
1465 }
1466
1467 void Player::walk(float speed)
1468 {
1469   physic.set_velocity_x(speed);
1470 }
1471
1472 void Player::set_dir(bool right)
1473 {
1474   dir = right ? RIGHT : LEFT;
1475 }
1476
1477 void
1478 Player::set_ghost_mode(bool enable)
1479 {
1480   if (ghost_mode == enable)
1481     return;
1482
1483   if (climbing) stop_climbing(*climbing);
1484
1485   if (enable) {
1486     ghost_mode = true;
1487     set_group(COLGROUP_DISABLED);
1488     physic.enable_gravity(false);
1489     log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1490   } else {
1491     ghost_mode = false;
1492     set_group(COLGROUP_MOVING);
1493     physic.enable_gravity(true);
1494     log_debug << "You feel solid again." << std::endl;
1495   }
1496 }
1497
1498 void
1499 Player::set_edit_mode(bool enable)
1500 {
1501   edit_mode = enable;
1502 }
1503
1504 void 
1505 Player::start_climbing(Climbable& climbable)
1506 {
1507   if (climbing || !&climbable) return;
1508
1509   climbing = &climbable;
1510   physic.enable_gravity(false);
1511   physic.set_velocity(0, 0);
1512   physic.set_acceleration(0, 0);
1513   if (backflipping) {
1514     backflipping = false;
1515     backflip_direction = 0;
1516     sprite->set_angle(0.0f);
1517   }
1518 }
1519
1520 void 
1521 Player::stop_climbing(Climbable& /*climbable*/)
1522 {
1523   if (!climbing) return;
1524
1525   climbing = 0;
1526
1527   if (grabbed_object) {    
1528     grabbed_object->ungrab(*this, dir);
1529     grabbed_object = NULL;
1530   }
1531
1532   physic.enable_gravity(true);
1533   physic.set_velocity(0, 0);
1534   physic.set_acceleration(0, 0);
1535
1536   if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1537     on_ground_flag = true;
1538     // TODO: This won't help. Why?
1539     do_jump(-300);
1540   }
1541 }
1542
1543 void
1544 Player::handle_input_climbing()
1545 {
1546   if (!climbing) {
1547     log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1548     return;
1549   }
1550
1551   float vx = 0;
1552   float vy = 0;
1553   if (controller->hold(Controller::LEFT)) {
1554     dir = LEFT;
1555     vx -= MAX_CLIMB_XM;
1556   }
1557   if (controller->hold(Controller::RIGHT)) {
1558     dir = RIGHT;
1559     vx += MAX_CLIMB_XM;
1560   }
1561   if (controller->hold(Controller::UP)) {
1562     vy -= MAX_CLIMB_YM;
1563   }
1564   if (controller->hold(Controller::DOWN)) {
1565     vy += MAX_CLIMB_YM;
1566   }
1567   if (controller->hold(Controller::JUMP)) {
1568     if (can_jump) {
1569       stop_climbing(*climbing);
1570       return;
1571     }  
1572   } else {
1573     can_jump = true;
1574   }
1575   if (controller->hold(Controller::ACTION)) {
1576     stop_climbing(*climbing);
1577     return;
1578   }
1579   physic.set_velocity(vx, vy);
1580   physic.set_acceleration(0, 0);
1581 }
1582
1583 /* EOF */