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