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