e4d8c6efbe22b7650c8d172410b4cebc8d2c412c
[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 #if 0
554    /* In case the player has pressed Down while in a certain range of air,
555       enable butt jump action */
556   if (input.down && !butt_jump && !duck)
557     if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
558       butt_jump = true;
559 #endif
560
561    /* When Down is not held anymore, disable butt jump */
562   if(butt_jump && !input.down)
563     butt_jump = false;
564
565   // Do butt jump
566   if (butt_jump && on_ground() && is_big())
567   {
568     // Add a smoke cloud
569     if (duck) 
570       Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
571     else 
572       Sector::current()->add_smoke_cloud(
573           Vector(get_pos().x - 32, get_pos().y + 32));
574     
575     butt_jump = false;
576
577 #if 0
578     // Break bricks beneath Tux
579     if(Sector::current()->trybreakbrick(
580           Vector(base.x + 1, base.y + base.height), false)
581         || Sector::current()->trybreakbrick(
582            Vector(base.x + base.width - 1, base.y + base.height), false))
583     {
584       physic.set_velocity_y(2);
585       butt_jump = true;
586     }
587 #endif
588
589 #if 0
590     // Kill nearby badguys
591     std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
592     for (std::vector<GameObject*>::iterator i = gameobjects.begin();
593          i != gameobjects.end();
594          i++)
595     {
596       BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
597       if(badguy)
598       {
599         // don't kill when badguys are already dying or in a certain mode
600         if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
601            badguy->mode != BadGuy::BOMB_EXPLODE)
602           {
603             if (fabsf(base.x - badguy->base.x) < 96 &&
604                 fabsf(base.y - badguy->base.y) < 64)
605               badguy->kill_me(25);
606           }
607       }
608     }
609 #endif
610   }
611
612   /** jumping is only allowed if we're about to touch ground soon and if the
613    * button has been up in between the last jump
614    */
615   // FIXME
616 #if 0
617   if ( (issolid(get_pos().x + bbox.get_width() / 2,
618           get_pos().y + bbox.get_height() + 64) ||
619         issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
620         issolid(get_pos().x + bbox.get_width() - 1,
621           get_pos().y + bbox.get_height() + 64))
622        && jumping  == false
623        && can_jump == false
624        && input.jump && !input.old_jump)
625     {
626       can_jump = true;
627     }
628 #endif
629
630   // FIXME: why the heck is this here and not somewhere where the keys are
631   // checked?!?
632   input.old_jump = input.jump;
633 }
634
635 void
636 Player::handle_input()
637 {
638   /* Handle horizontal movement: */
639   handle_horizontal_input();
640
641   /* Jump/jumping? */
642   if (on_ground() && !input.jump)
643     can_jump = true;
644   handle_vertical_input();
645
646   /* Shoot! */
647   if (input.fire && !input.old_fire && player_status->bonus == FIRE_BONUS) {
648     if(Sector::current()->add_bullet(
649 //           get_pos() + Vector(0, bbox.get_height()/2),
650            get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2) 
651            : Vector(32, bbox.get_height()/2)),
652           physic.get_velocity_x(), dir))
653       shooting_timer.start(SHOOTING_TIME);
654     // FIXME: why the heck is this here
655     input.old_fire = false;
656   }
657
658   /* Duck! */
659   if (input.down && is_big() && !duck 
660       && physic.get_velocity_y() == 0 && on_ground())
661     {
662       duck = true;
663       bbox.move(Vector(0, 32));
664       bbox.set_height(31.8);
665     }
666   else if(!input.down && is_big() && duck)
667     {
668       // try if we can really unduck
669       bbox.move(Vector(0, -32));
670       bbox.set_height(63.8);
671       duck = false;
672       // FIXME
673 #if 0
674       // when unducking in air we need some space to do so
675       if(on_ground() || !collision_object_map(bbox)) {
676         duck = false;
677       } else {
678         // undo the ducking changes
679         bbox.move(Vector(0, 32));
680         bbox.set_height(31.8);
681       }
682 #endif
683     }
684 }
685
686 void
687 Player::set_bonus(BonusType type, bool animate)
688 {
689   if(player_status->bonus == type)
690     return;
691   
692   if(player_status->bonus == NO_BONUS) {
693       bbox.set_height(63.8);
694       bbox.move(Vector(0, -32));
695       if(animate)
696         growing_timer.start(GROWING_TIME);
697   }
698       
699   player_status->bonus = type;
700 }
701
702 void
703 Player::draw(DrawingContext& context)
704 {
705   TuxBodyParts* tux_body;
706           
707   if (player_status->bonus == GROWUP_BONUS)
708     tux_body = big_tux;
709   else if (player_status->bonus == FIRE_BONUS)
710     tux_body = fire_tux;
711   else if (player_status->bonus == ICE_BONUS)
712     tux_body = ice_tux;
713   else
714     tux_body = small_tux;
715
716   int layer = LAYER_OBJECTS + 10;
717
718   /* Set Tux sprite action */
719   if (duck && is_big())
720     {
721     if(dir == LEFT)
722       tux_body->set_action("duck-left");
723     else // dir == RIGHT
724       tux_body->set_action("duck-right");
725     }
726   else if (skidding_timer.started() && !skidding_timer.check())
727     {
728     if(dir == LEFT)
729       tux_body->set_action("skid-left");
730     else // dir == RIGHT
731       tux_body->set_action("skid-right");
732     }
733   else if (kick_timer.started() && !kick_timer.check())
734     {
735     if(dir == LEFT)
736       tux_body->set_action("kick-left");
737     else // dir == RIGHT
738       tux_body->set_action("kick-right");
739     }
740   else if (butt_jump && is_big())
741     {
742     if(dir == LEFT)
743       tux_body->set_action("buttjump-left");
744     else // dir == RIGHT
745       tux_body->set_action("buttjump-right");
746     }
747   else if (physic.get_velocity_y() != 0)
748     {
749     if(dir == LEFT)
750       tux_body->set_action("jump-left");
751     else // dir == RIGHT
752       tux_body->set_action("jump-right");
753     }
754   else
755     {
756     if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
757       {
758       if(dir == LEFT)
759         tux_body->set_action("stand-left");
760       else // dir == RIGHT
761         tux_body->set_action("stand-right");
762       }
763     else // moving
764       {
765       if(dir == LEFT)
766         tux_body->set_action("walk-left");
767       else // dir == RIGHT
768         tux_body->set_action("walk-right");
769       }
770     }
771
772   if(idle_timer.check())
773     {
774     if(is_big())
775       {
776       if(dir == LEFT)
777         tux_body->head->set_action("idle-left", 1);
778       else // dir == RIGHT
779         tux_body->head->set_action("idle-right", 1);
780       }
781
782     }
783
784   // Tux is holding something
785   if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
786       (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
787     {
788     if (duck)
789       {
790       if(dir == LEFT)
791         tux_body->arms->set_action("duck+grab-left");
792       else // dir == RIGHT
793         tux_body->arms->set_action("duck+grab-right");
794       }
795     else
796       {
797       if(dir == LEFT)
798         tux_body->arms->set_action("grab-left");
799       else // dir == RIGHT
800         tux_body->arms->set_action("grab-right");
801       }
802     }
803
804   /* Draw Tux */
805   if(dying) {
806     smalltux_gameover->draw(context, get_pos(), layer);
807   } else if(growing_timer.get_timeleft() > 0) {
808     if(!is_big())
809       {
810       if (dir == RIGHT)
811         context.draw_surface(growingtux_right[GROWING_FRAMES-1 - 
812                  int((growing_timer.get_timegone() *
813                  GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
814       else
815         context.draw_surface(growingtux_left[GROWING_FRAMES-1 - 
816                 int((growing_timer.get_timegone() *
817                 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
818       }
819     else
820       {
821       if (dir == RIGHT)
822         context.draw_surface(growingtux_right[
823             int((growing_timer.get_timegone() *
824                 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
825       else
826         context.draw_surface(growingtux_left[
827             int((growing_timer.get_timegone() *
828                              GROWING_FRAMES) / GROWING_TIME)],
829             get_pos(), layer);
830       }
831     }
832   else if (safe_timer.started() && size_t(global_time*40)%2)
833     ;  // don't draw Tux
834   else
835     tux_body->draw(context, get_pos(), layer);
836
837   // Draw blinking star overlay
838   if (invincible_timer.started() &&
839      (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
840       || size_t(global_time*20)%2)
841      && !dying)
842   {
843     if (!is_big() || duck)
844       smalltux_star->draw(context, get_pos(), layer + 5);
845     else
846       bigtux_star->draw(context, get_pos(), layer + 5);
847   }
848  
849   if (debug_mode)
850     context.draw_filled_rect(get_pos(),
851         Vector(bbox.get_width(), bbox.get_height()),
852         Color(75,75,75, 150), LAYER_OBJECTS+20);
853 }
854
855 HitResponse
856 Player::collision(GameObject& other, const CollisionHit& hit)
857 {
858   Portable* portable = dynamic_cast<Portable*> (&other);
859   if(portable && grabbed_object == 0 && input.fire
860         && fabsf(hit.normal.x) > .9) {
861     grabbed_object = portable;
862     return CONTINUE;
863   }
864  
865   if(other.get_flags() & FLAG_SOLID) {
866     if(hit.normal.y < 0) { // landed on floor?
867       if (physic.get_velocity_y() < 0)
868         physic.set_velocity_y(0);
869       on_ground_flag = true;
870     } else if(hit.normal.y > 0) { // bumped against the roof
871       physic.set_velocity_y(.1);
872     }
873     
874     if(fabsf(hit.normal.x) > .9) { // hit on the side?
875       physic.set_velocity_x(0);
876     }
877
878     return CONTINUE;
879   }
880
881   TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
882   if(trigger) {
883     if(input.up && !input.old_up)
884       trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
885   }
886
887   return FORCE_MOVE;
888 }
889
890 void
891 Player::make_invincible()
892 {
893   SoundManager::get()->play_sound(IDToSound(SND_HERRING));
894   invincible_timer.start(TUX_INVINCIBLE_TIME);
895   Sector::current()->play_music(HERRING_MUSIC);               
896 }
897
898 /* Kill Player! */
899 void
900 Player::kill(HurtMode mode)
901 {
902   if(dying)
903     return;
904
905   if(safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
906     return;                          
907   
908   SoundManager::get()->play_sound(IDToSound(SND_HURT));
909
910   physic.set_velocity_x(0);
911
912   if (mode == SHRINK && is_big())
913     {
914       if (player_status->bonus == FIRE_BONUS
915           || player_status->bonus == ICE_BONUS)
916         {
917           safe_timer.start(TUX_SAFE_TIME);
918           player_status->bonus = GROWUP_BONUS;
919         }
920       else 
921         {
922           growing_timer.start(GROWING_TIME);
923           safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
924           bbox.set_height(31.8);
925           duck = false;
926           player_status->bonus = NO_BONUS;
927         }
928     }
929   else
930     {
931       physic.enable_gravity(true);
932       physic.set_acceleration(0, 0);
933       physic.set_velocity(0, 700);
934       player_status->lives -= 1;
935       dying = true;
936       dying_timer.start(3.0);
937       flags |= FLAG_NO_COLLDET;
938     }
939 }
940
941 void
942 Player::move(const Vector& vector)
943 {
944   bbox.set_pos(vector);
945   if(is_big())
946     bbox.set_size(31.8, 63.8);
947   else
948     bbox.set_size(31.8, 31.8);
949   on_ground_flag = false;
950   duck = false;
951
952   input.reset();
953   physic.reset();
954 }
955
956 void
957 Player::check_bounds(Camera* camera)
958 {
959   /* Keep tux in bounds: */
960   if (get_pos().x < 0)
961     { // Lock Tux to the size of the level, so that he doesn't fall of
962       // on the left side
963       bbox.set_pos(Vector(0, get_pos().y));
964     }
965
966   /* Keep in-bounds, vertically: */
967   if (get_pos().y > Sector::current()->solids->get_height() * 32)
968     {
969       kill(KILL);
970       return;
971     }
972
973   bool adjust = false;
974   // can happen if back scrolling is disabled
975   if(get_pos().x < camera->get_translation().x) {
976     bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
977     adjust = true;
978   }
979   if(get_pos().x >= camera->get_translation().x + screen->w - bbox.get_width())
980   {
981     bbox.set_pos(Vector(
982           camera->get_translation().x + screen->w - bbox.get_width(),
983           get_pos().y));
984     adjust = true;
985   }
986
987   if(adjust) {
988     // FIXME
989 #if 0
990     // squished now?
991     if(collision_object_map(bbox)) {
992       kill(KILL);
993       return;
994     }
995 #endif
996   }
997 }
998
999 void
1000 Player::bounce(BadGuy& )
1001 {
1002   //Make sure we stopped flapping
1003   flapping = false;
1004   falling_from_flap = false;
1005   
1006   if (input.jump)
1007     physic.set_velocity_y(520);
1008   else
1009     physic.set_velocity_y(200);
1010 }
1011