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