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