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