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