added a powerup object that can be placed in levels and represent various powerups...
[supertux.git] / src / object / player.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include <config.h>
20
21 #include <typeinfo>
22 #include <cmath>
23 #include <iostream>
24 #include <cassert>
25
26 #include "gettext.h"
27 #include "sprite/sprite_manager.h"
28 #include "player.h"
29 #include "tile.h"
30 #include "sprite/sprite.h"
31 #include "sector.h"
32 #include "resources.h"
33 #include "video/screen.h"
34 #include "statistics.h"
35 #include "game_session.h"
36 #include "object/tilemap.h"
37 #include "object/camera.h"
38 #include "object/gameobjs.h"
39 #include "object/portable.h"
40 #include "trigger/trigger_base.h"
41 #include "control/joystickkeyboardcontroller.h"
42 #include "main.h"
43
44 static const int TILES_FOR_BUTTJUMP = 3;
45 static const float SHOOTING_TIME = .150;
46 /// time before idle animation starts
47 static const float IDLE_TIME = 2.5;
48
49 static const float WALK_ACCELERATION_X = 300;
50 static const float RUN_ACCELERATION_X = 400;
51 static const float SKID_XM = 200;
52 static const float SKID_TIME = .3;
53 static const float MAX_WALK_XM = 230;
54 static const float MAX_RUN_XM = 320;
55 static const float WALK_SPEED = 100;
56
57 // growing animation
58 Surface* growingtux_left[GROWING_FRAMES];
59 Surface* growingtux_right[GROWING_FRAMES];
60
61 Surface* tux_life = 0;
62
63 TuxBodyParts* small_tux = 0;
64 TuxBodyParts* big_tux = 0;
65 TuxBodyParts* fire_tux = 0;
66 TuxBodyParts* ice_tux = 0;
67
68 void
69 TuxBodyParts::set_action(std::string action, int loops)
70 {
71   if(head != NULL)
72     head->set_action(action, loops);
73   if(body != NULL)
74     body->set_action(action, loops);
75   if(arms != NULL)
76     arms->set_action(action, loops);
77   if(feet != NULL)
78     feet->set_action(action, loops);
79 }
80
81 void
82 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
83 {
84   if(head != NULL)
85     head->draw(context, pos, layer-1);
86   if(body != NULL)
87     body->draw(context, pos, layer-3);
88   if(arms != NULL)
89     arms->draw(context, pos, layer);
90   if(feet != NULL)
91     feet->draw(context, pos, layer-2);
92 }
93
94 Player::Player(PlayerStatus* _player_status)
95   : player_status(_player_status), grabbed_object(0)
96 {
97   controller = main_controller;
98   smalltux_gameover = sprite_manager->create("smalltux-gameover");
99   smalltux_star = sprite_manager->create("smalltux-star");
100   bigtux_star = sprite_manager->create("bigtux-star");
101   init();
102 }
103
104 Player::~Player()
105 {
106   delete smalltux_gameover;
107   delete smalltux_star;
108   delete bigtux_star;
109 }
110
111 void
112 Player::init()
113 {
114   if(is_big())
115     bbox.set_size(31.8, 63.8);
116   else
117     bbox.set_size(31.8, 31.8);
118
119   dir = RIGHT;
120   old_dir = dir;
121   duck = false;
122   dead = false;
123
124   dying = false;
125   last_ground_y = 0;
126   fall_mode = ON_GROUND;
127   jumping = false;
128   flapping = false;
129   can_jump = true;
130   can_flap = false;
131   falling_from_flap = false;
132   enable_hover = false;
133   butt_jump = false;
134   
135   flapping_velocity = 0;
136
137   // temporary to help player's choosing a flapping
138   flapping_mode = NO_FLAP;
139
140   // Ricardo's flapping
141   flaps_nb = 0;
142
143   on_ground_flag = false;
144   grabbed_object = 0;
145
146   physic.reset();
147 }
148
149 void
150 Player::set_controller(Controller* controller)
151 {
152   this->controller = controller;
153 }
154
155 void
156 Player::update(float elapsed_time)
157 {
158   if(dying && dying_timer.check()) {
159     dead = true;
160     return;
161   }
162
163   if(!controller->hold(Controller::ACTION) && grabbed_object) {
164     grabbed_object = 0;
165     // move the grabbed object a bit away from tux
166     Vector pos = get_pos() + 
167         Vector(dir == LEFT ? -bbox.get_width() : bbox.get_width(),
168                 bbox.get_height()*0.66666 - 32);
169     MovingObject* object = dynamic_cast<MovingObject*> (grabbed_object);
170     if(object) {
171       object->set_pos(pos);
172     } else {
173 #ifdef DEBUG
174       std::cout << "Non MovingObjetc grabbed?!?\n";
175 #endif
176     }
177   }
178
179   if(!dying)
180     handle_input();
181
182   movement = physic.get_movement(elapsed_time);
183   on_ground_flag = false;
184
185 #if 0
186   // special exception for cases where we're stuck under tiles after
187   // being ducked. In this case we drift out
188   if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
189      && collision_object_map(base)) {
190     base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
191     previous_base = old_base = base;
192   }
193 #endif
194
195   if(grabbed_object != 0) {
196     Vector pos = get_pos() + 
197       Vector(dir == LEFT ? -16 : 16,
198              bbox.get_height()*0.66666 - 32);
199     grabbed_object->grab(*this, pos);
200   }
201 }
202
203 bool
204 Player::on_ground()
205 {
206   return on_ground_flag;
207 }
208
209 bool
210 Player::is_big()
211 {
212   if(player_status->bonus == NO_BONUS)
213     return false;
214
215   return true;
216 }
217
218 void
219 Player::handle_horizontal_input()
220 {
221   float vx = physic.get_velocity_x();
222   float vy = physic.get_velocity_y();
223   float ax = physic.get_acceleration_x();
224   float ay = physic.get_acceleration_y();
225
226   float dirsign = 0;
227   if(!duck || physic.get_velocity_y() != 0) {
228     if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
229       old_dir = dir;
230       dir = LEFT;
231       dirsign = -1;
232     } else if(!controller->hold(Controller::LEFT)
233               && controller->hold(Controller::RIGHT)) {
234       old_dir = dir;
235       dir = RIGHT;
236       dirsign = 1;
237     }
238   }
239
240   if (!controller->hold(Controller::ACTION)) {
241     ax = dirsign * WALK_ACCELERATION_X;
242     // limit speed
243     if(vx >= MAX_WALK_XM && dirsign > 0) {
244       vx = MAX_WALK_XM;
245       ax = 0;
246     } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
247       vx = -MAX_WALK_XM;
248       ax = 0;
249     }
250   } else {
251     ax = dirsign * RUN_ACCELERATION_X;
252     // limit speed
253     if(vx >= MAX_RUN_XM && dirsign > 0) {
254       vx = MAX_RUN_XM;
255       ax = 0;
256     } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
257       vx = -MAX_RUN_XM;
258       ax = 0;
259     }
260   }
261
262   // we can reach WALK_SPEED without any acceleration
263   if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
264     vx = dirsign * WALK_SPEED;
265   }
266
267   // changing directions?
268   if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
269     // let's skid!
270     if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
271       skidding_timer.start(SKID_TIME);
272       sound_manager->play_sound("skid");
273       // dust some partcles
274       Sector::current()->add_object(
275         new Particles(
276           Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
277                  bbox.p2.y),
278           dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
279           Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
280           LAYER_OBJECTS+1));
281       
282       ax *= 2.5;
283     } else {
284       ax *= 2;
285     }
286   }
287
288   // we get slower when not pressing any keys
289   if(dirsign == 0) {
290     if(fabs(vx) < WALK_SPEED) {
291       vx = 0;
292       ax = 0;
293     } else if(vx < 0) {
294       ax = WALK_ACCELERATION_X * 1.5;
295     } else {
296       ax = WALK_ACCELERATION_X * -1.5;
297     }
298   }
299
300 #if 0
301   // if we're on ice slow down acceleration or deceleration
302   if (isice(base.x, base.y + base.height))
303   {
304     /* the acceleration/deceleration rate on ice is inversely proportional to
305      * the current velocity.
306      */
307
308     // increasing 1 will increase acceleration/deceleration rate
309     // decreasing 1 will decrease acceleration/deceleration rate
310     //  must stay above zero, though
311     if (ax != 0) ax *= 1 / fabs(vx);
312   }
313 #endif
314
315   // extend/shrink tux collision rectangle so that we fall through/walk over 1
316   // tile holes
317   if(fabsf(vx) > MAX_WALK_XM) {
318     bbox.set_width(33);
319   } else {
320     bbox.set_width(31.8);
321   }
322
323   physic.set_velocity(vx, vy);
324   physic.set_acceleration(ax, ay);
325 }
326
327 void
328 Player::handle_vertical_input()
329 {
330   // set fall mode...
331   if(on_ground()) {
332     fall_mode = ON_GROUND;
333     last_ground_y = get_pos().y;
334   } else {
335     if(get_pos().y > last_ground_y)
336       fall_mode = FALLING;
337     else if(fall_mode == ON_GROUND)
338       fall_mode = JUMPING;
339   }
340
341   if(on_ground()) { /* Make sure jumping is off. */
342     jumping = false;
343     flapping = false;
344     falling_from_flap = false;
345     if (flapping_timer.started()) {
346       flapping_timer.stop();
347     }
348
349     physic.set_acceleration_y(0); //for flapping
350   }
351
352   // Press jump key
353   if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
354     if (duck) // only jump a little bit when in duck mode
355       physic.set_velocity_y(300);
356     else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
357       physic.set_velocity_y(580);
358     else
359       physic.set_velocity_y(520);
360     
361     //bbox.move(Vector(0, -1));
362     jumping = true;
363     flapping = false;
364     can_jump = false;
365     can_flap = false;
366     flaps_nb = 0; // Ricardo's flapping
367     if (is_big())
368       sound_manager->play_sound("bigjump");
369     else
370       sound_manager->play_sound("jump");
371   } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
372     if (!flapping && !duck && !falling_from_flap && !on_ground()) {
373       can_flap = true;
374     }
375     if (jumping && physic.get_velocity_y() > 0) {
376       jumping = false;
377       physic.set_velocity_y(0);
378     }
379   }
380 #if CHOOSEFLAPSTYLE
381   // temporary to help players choosing a flapping
382   if(flapping_mode == RICARDO_FLAP) {
383     // Flapping, Ricardo's version
384     // similar to SM3 Fox
385     if(controller->pressed(Controller::JUMP) && can_flap && flaps_nb < 3) {
386       physic.set_velocity_y(350);
387       physic.set_velocity_x(physic.get_velocity_x() * 35);
388       flaps_nb++;
389     }
390   } else if(flapping_mode == MAREK_FLAP) {
391 #endif
392     // Flapping, Marek and Ondra's version
393     if (controller->hold(Controller::JUMP) && can_flap)
394     {
395       if (flapping_timer.check()) 
396       {
397         can_flap = false;
398         //flapping = false;
399         falling_from_flap = true;
400       }
401       else if (!flapping_timer.started())
402       {
403         flapping_timer.start(TUX_FLAPPING_TIME);
404         flapping_velocity = physic.get_velocity_x();
405       }
406       else
407       {
408         jumping = true;
409         flapping = true;
410         float cv = flapping_velocity * sqrt(
411           TUX_FLAPPING_TIME - flapping_timer.get_timegone() 
412           / TUX_FLAPPING_TIME);
413       
414         //Handle change of direction while flapping
415         if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
416           cv *= (-1);
417         }
418         else if (cv == 0) {
419           if (controller->hold(Controller::LEFT)) {
420                 cv = -TUX_FLAPPING_LEAST_X;
421           }
422           else if (controller->hold(Controller::RIGHT)) {
423             cv = TUX_FLAPPING_LEAST_X;
424           }
425         }
426         physic.set_velocity_x(cv);
427         physic.set_velocity_y(flapping_timer.get_timegone()
428             * TUX_FLAPPING_STRENGTH);
429         //std::cout << "Timegone: " << flapping_timer.get_timegone() << ", Y velocity: " << physic.get_velocity_y() << "\n";
430       }
431     }
432 #if CHOOSEFLAPSTYLE
433   } else if(flapping_mode == RYAN_FLAP) {
434     // Flapping, Ryan's version
435     if (controller->hold(Controller::JUMP) && can_flap)
436     {
437       if (!flapping_timer.started())
438       {
439         flapping_timer.start(TUX_FLAPPING_TIME);
440       }
441       if (flapping_timer.check()) 
442       {
443         can_flap = false;
444         falling_from_flap = true;
445       }
446       jumping = true;
447       flapping = true;
448       if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
449           && physic.get_velocity_y() < 0)
450       {
451         float gravity = Sector::current()->gravity;
452         (void)gravity;
453         float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
454         
455         // XXX: magic numbers. should be a percent of gravity
456         //      gravity is (by default) -0.1f
457         physic.set_acceleration_y(12 + 1*xr);
458         
459 #if 0
460         // To slow down x-vel when flapping (not working)
461         if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
462         {
463           if (physic.get_velocity_x() < 0)
464             physic.set_acceleration_x(1.0f);
465           else if (physic.get_velocity_x() > 0)
466             physic.set_acceleration_x(-1.0f);
467         }
468 #endif
469       }
470     } else {
471       physic.set_acceleration_y(0);
472     }
473   }
474 #endif
475
476   /* In case the player has pressed Down while in a certain range of air,
477      enable butt jump action */
478   if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
479     //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
480     butt_jump = true;
481   
482   /* When Down is not held anymore, disable butt jump */
483   if(butt_jump && !controller->hold(Controller::DOWN))
484     butt_jump = false;
485   
486 #if 0
487   // Do butt jump
488   if (butt_jump && on_ground() && is_big()) {
489     // Add a smoke cloud
490     if (duck) 
491       Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
492     else 
493       Sector::current()->add_smoke_cloud(
494         Vector(get_pos().x - 32, get_pos().y + 32));
495     
496     butt_jump = false;
497     
498     // Break bricks beneath Tux
499     if(Sector::current()->trybreakbrick(
500          Vector(base.x + 1, base.y + base.height), false)
501        || Sector::current()->trybreakbrick(
502          Vector(base.x + base.width - 1, base.y + base.height), false)) {
503       physic.set_velocity_y(2);
504       butt_jump = true;
505     }
506     
507     // Kill nearby badguys
508     std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
509     for (std::vector<GameObject*>::iterator i = gameobjects.begin();
510          i != gameobjects.end();
511          i++) {
512       BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
513       if(badguy) {
514         // don't kill when badguys are already dying or in a certain mode
515         if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
516            badguy->mode != BadGuy::BOMB_EXPLODE) {
517           if (fabsf(base.x - badguy->base.x) < 96 &&
518               fabsf(base.y - badguy->base.y) < 64)
519             badguy->kill_me(25);
520         }
521       }
522     }
523   }
524 #endif
525
526   /** jumping is only allowed if we're about to touch ground soon and if the
527    * button has been up in between the last jump
528    */
529   // FIXME
530 #if 0
531   if ( (issolid(get_pos().x + bbox.get_width() / 2,
532           get_pos().y + bbox.get_height() + 64) ||
533         issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
534         issolid(get_pos().x + bbox.get_width() - 1,
535           get_pos().y + bbox.get_height() + 64))
536        && jumping  == false
537        && can_jump == false
538        && input.jump && !input.old_jump)
539     {
540       can_jump = true;
541     }
542 #endif
543 }
544
545 void
546 Player::handle_input()
547 {
548   /* Handle horizontal movement: */
549   handle_horizontal_input();
550
551   /* Jump/jumping? */
552   if (on_ground() && !controller->hold(Controller::JUMP))
553     can_jump = true;
554   handle_vertical_input();
555
556   /* Shoot! */
557   if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
558     if(Sector::current()->add_bullet(
559          get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2) 
560                       : Vector(32, bbox.get_height()/2)),
561          physic.get_velocity_x(), dir))
562       shooting_timer.start(SHOOTING_TIME);
563   }
564   
565   /* Duck! */
566   if (controller->hold(Controller::DOWN) && is_big() && !duck 
567       && physic.get_velocity_y() == 0 && on_ground()) {
568     duck = true;
569     bbox.move(Vector(0, 32));
570     bbox.set_height(31.8);
571   } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
572     // try if we can really unduck
573     bbox.move(Vector(0, -32));
574     bbox.set_height(63.8);
575     duck = false;
576     // FIXME
577 #if 0
578     // when unducking in air we need some space to do so
579     if(on_ground() || !collision_object_map(bbox)) {
580       duck = false;
581     } else {
582       // undo the ducking changes
583       bbox.move(Vector(0, 32));
584       bbox.set_height(31.8);
585     }
586 #endif
587   }
588 }
589
590 void
591 Player::set_bonus(BonusType type, bool animate)
592 {
593   if(player_status->bonus >= type)
594     return;
595   
596   if(player_status->bonus == NO_BONUS) {
597     bbox.set_height(63.8);
598     bbox.move(Vector(0, -32));
599     if(animate)
600       growing_timer.start(GROWING_TIME);
601   }
602   
603   player_status->bonus = type;
604 }
605
606 void
607 Player::draw(DrawingContext& context)
608 {
609   TuxBodyParts* tux_body;
610           
611   if (player_status->bonus == GROWUP_BONUS)
612     tux_body = big_tux;
613   else if (player_status->bonus == FIRE_BONUS)
614     tux_body = fire_tux;
615   else if (player_status->bonus == ICE_BONUS)
616     tux_body = ice_tux;
617   else
618     tux_body = small_tux;
619
620   int layer = LAYER_OBJECTS + 10;
621
622   /* Set Tux sprite action */
623   if (duck && is_big())
624     {
625     if(dir == LEFT)
626       tux_body->set_action("duck-left");
627     else // dir == RIGHT
628       tux_body->set_action("duck-right");
629     }
630   else if (skidding_timer.started() && !skidding_timer.check())
631     {
632     if(dir == LEFT)
633       tux_body->set_action("skid-left");
634     else // dir == RIGHT
635       tux_body->set_action("skid-right");
636     }
637   else if (kick_timer.started() && !kick_timer.check())
638     {
639     if(dir == LEFT)
640       tux_body->set_action("kick-left");
641     else // dir == RIGHT
642       tux_body->set_action("kick-right");
643     }
644   else if (butt_jump && is_big())
645     {
646     if(dir == LEFT)
647       tux_body->set_action("buttjump-left");
648     else // dir == RIGHT
649       tux_body->set_action("buttjump-right");
650     }
651   else if (physic.get_velocity_y() != 0)
652     {
653     if(dir == LEFT)
654       tux_body->set_action("jump-left");
655     else // dir == RIGHT
656       tux_body->set_action("jump-right");
657     }
658   else
659     {
660     if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
661       {
662       if(dir == LEFT)
663         tux_body->set_action("stand-left");
664       else // dir == RIGHT
665         tux_body->set_action("stand-right");
666       }
667     else // moving
668       {
669       if(dir == LEFT)
670         tux_body->set_action("walk-left");
671       else // dir == RIGHT
672         tux_body->set_action("walk-right");
673       }
674     }
675
676   if(idle_timer.check())
677     {
678     if(is_big())
679       {
680       if(dir == LEFT)
681         tux_body->head->set_action("idle-left", 1);
682       else // dir == RIGHT
683         tux_body->head->set_action("idle-right", 1);
684       }
685
686     }
687
688   // Tux is holding something
689   if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
690       (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
691     {
692     if (duck)
693       {
694       if(dir == LEFT)
695         tux_body->arms->set_action("duck+grab-left");
696       else // dir == RIGHT
697         tux_body->arms->set_action("duck+grab-right");
698       }
699     else
700       {
701       if(dir == LEFT)
702         tux_body->arms->set_action("grab-left");
703       else // dir == RIGHT
704         tux_body->arms->set_action("grab-right");
705       }
706     }
707
708   /* Draw Tux */
709   if(dying) {
710     smalltux_gameover->draw(context, get_pos(), layer);
711   } else if(growing_timer.get_timeleft() > 0) {
712     if(!is_big())
713       {
714       if (dir == RIGHT)
715         context.draw_surface(growingtux_right[GROWING_FRAMES-1 - 
716                  int((growing_timer.get_timegone() *
717                  GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
718       else
719         context.draw_surface(growingtux_left[GROWING_FRAMES-1 - 
720                 int((growing_timer.get_timegone() *
721                 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
722       }
723     else
724       {
725       if (dir == RIGHT)
726         context.draw_surface(growingtux_right[
727             int((growing_timer.get_timegone() *
728                 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
729       else
730         context.draw_surface(growingtux_left[
731             int((growing_timer.get_timegone() *
732                              GROWING_FRAMES) / GROWING_TIME)],
733             get_pos(), layer);
734       }
735     }
736   else if (safe_timer.started() && size_t(global_time*40)%2)
737     ;  // don't draw Tux
738   else
739     tux_body->draw(context, get_pos(), layer);
740
741   // Draw blinking star overlay
742   if (invincible_timer.started() &&
743      (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
744       || size_t(global_time*20)%2)
745      && !dying)
746   {
747     if (!is_big() || duck)
748       smalltux_star->draw(context, get_pos(), layer + 5);
749     else
750       bigtux_star->draw(context, get_pos(), layer + 5);
751   } 
752 }
753
754 HitResponse
755 Player::collision(GameObject& other, const CollisionHit& hit)
756 {
757   Portable* portable = dynamic_cast<Portable*> (&other);
758   if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
759      && fabsf(hit.normal.x) > .9) {
760     grabbed_object = portable;
761     return CONTINUE;
762   }
763  
764   if(other.get_flags() & FLAG_SOLID) {
765     if(hit.normal.y < 0) { // landed on floor?
766       if (physic.get_velocity_y() < 0)
767         physic.set_velocity_y(0);
768       on_ground_flag = true;
769     } else if(hit.normal.y > 0) { // bumped against the roof
770       physic.set_velocity_y(.1);
771     }
772     
773     if(fabsf(hit.normal.x) > .9) { // hit on the side?
774       physic.set_velocity_x(0);
775     }
776
777     return CONTINUE;
778   }
779
780   TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
781   if(trigger) {
782     if(controller->pressed(Controller::UP))
783       trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
784   }
785
786   return FORCE_MOVE;
787 }
788
789 void
790 Player::make_invincible()
791 {
792   sound_manager->play_sound("invincible");
793   invincible_timer.start(TUX_INVINCIBLE_TIME);
794   Sector::current()->play_music(HERRING_MUSIC);               
795 }
796
797 /* Kill Player! */
798 void
799 Player::kill(HurtMode mode)
800 {
801   if(dying)
802     return;
803
804   if(mode != KILL && 
805           safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
806     return;                          
807   
808   sound_manager->play_sound("hurt");
809
810   physic.set_velocity_x(0);
811
812   if (mode == SHRINK && is_big())
813     {
814       if (player_status->bonus == FIRE_BONUS
815           || player_status->bonus == ICE_BONUS)
816         {
817           safe_timer.start(TUX_SAFE_TIME);
818           player_status->bonus = GROWUP_BONUS;
819         }
820       else 
821         {
822           growing_timer.start(GROWING_TIME);
823           safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
824           bbox.set_height(31.8);
825           duck = false;
826           player_status->bonus = NO_BONUS;
827         }
828     }
829   else
830     {
831       physic.enable_gravity(true);
832       physic.set_acceleration(0, 0);
833       physic.set_velocity(0, 700);
834       player_status->lives -= 1;
835       player_status->bonus = NO_BONUS;
836       dying = true;
837       dying_timer.start(3.0);
838       flags |= FLAG_NO_COLLDET;
839     }
840 }
841
842 void
843 Player::move(const Vector& vector)
844 {
845   bbox.set_pos(vector);
846   if(is_big())
847     bbox.set_size(31.8, 63.8);
848   else
849     bbox.set_size(31.8, 31.8);
850   on_ground_flag = false;
851   duck = false;
852   last_ground_y = vector.y;
853
854   physic.reset();
855 }
856
857 void
858 Player::check_bounds(Camera* camera)
859 {
860   /* Keep tux in bounds: */
861   if (get_pos().x < 0)
862     { // Lock Tux to the size of the level, so that he doesn't fall of
863       // on the left side
864       bbox.set_pos(Vector(0, get_pos().y));
865     }
866
867   /* Keep in-bounds, vertically: */
868   if (get_pos().y > Sector::current()->solids->get_height() * 32)
869     {
870       kill(KILL);
871       return;
872     }
873
874   bool adjust = false;
875   // can happen if back scrolling is disabled
876   if(get_pos().x < camera->get_translation().x) {
877     bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
878     adjust = true;
879   }
880   if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
881   {
882     bbox.set_pos(Vector(
883           camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
884           get_pos().y));
885     adjust = true;
886   }
887
888   if(adjust) {
889     // FIXME
890 #if 0
891     // squished now?
892     if(collision_object_map(bbox)) {
893       kill(KILL);
894       return;
895     }
896 #endif
897   }
898 }
899
900 void
901 Player::bounce(BadGuy& )
902 {
903   //Make sure we stopped flapping
904   flapping = false;
905   falling_from_flap = false;
906   
907   if(controller->hold(Controller::JUMP))
908     physic.set_velocity_y(520);
909   else
910     physic.set_velocity_y(300);
911 }
912