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