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