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