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