- fixed it so Tux doesn't die on spikes if safety timer is started
[supertux.git] / src / 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
20 #include <math.h>
21 #include <iostream>
22 #include <cassert>
23 #include "gameloop.h"
24 #include "globals.h"
25 #include "player.h"
26 #include "defines.h"
27 #include "scene.h"
28 #include "tile.h"
29 #include "sprite.h"
30 #include "screen.h"
31
32 // behavior definitions:
33 #define TILES_FOR_BUTTJUMP 3
34 // animation times (in ms):
35 #define SHOOTING_TIME 320
36 // others stuff:
37 #define AUTOSCROLL_DEAD_INTERVAL 300
38
39 Surface* tux_life;
40
41 Sprite* smalltux_gameover;
42 Sprite* smalltux_star;
43 Sprite* largetux_star;
44
45 PlayerSprite smalltux;
46 PlayerSprite largetux;
47 PlayerSprite icetux;
48 PlayerSprite firetux;
49
50 PlayerKeymap keymap;
51
52 PlayerKeymap::PlayerKeymap()
53 {
54   keymap.jump  = SDLK_UP;
55   keymap.duck  = SDLK_DOWN;
56   keymap.left  = SDLK_LEFT;
57   keymap.right = SDLK_RIGHT;
58   keymap.fire  = SDLK_LCTRL;
59 }
60
61 void player_input_init(player_input_type* pplayer_input)
62 {
63   pplayer_input->down = UP;
64   pplayer_input->fire = UP;
65   pplayer_input->left = UP;
66   pplayer_input->old_fire = UP;
67   pplayer_input->right = UP;
68   pplayer_input->up = UP;
69   pplayer_input->old_up = UP;
70 }
71
72 Player::Player(DisplayManager& display_manager)
73 {
74   display_manager.add_drawable(this, LAYER_OBJECTS-1); // for tux himself
75   display_manager.add_drawable(this, LAYER_OBJECTS+1); // for the arm
76   init();
77 }
78
79 Player::~Player()
80 {
81 }
82
83 void
84 Player::init()
85 {
86   Level* plevel = World::current()->get_level();
87
88   holding_something = false;
89
90   base.width = 32;
91   base.height = 32;
92
93   size = SMALL;
94   got_power = NONE_POWER;
95
96   base.x = plevel->start_pos_x;
97   base.y = plevel->start_pos_y;
98   previous_base = old_base = base;
99   dir = RIGHT;
100   old_dir = dir;
101   duck = false;
102
103   dying   = DYING_NOT;
104   last_ground_y = 0;
105   fall_mode = ON_GROUND;
106   jumping = false;
107   can_jump = true;
108   butt_jump = false;
109
110   frame_main = 0;
111   frame_ = 0;
112   
113   player_input_init(&input);
114
115   invincible_timer.init(true);
116   skidding_timer.init(true);
117   safe_timer.init(true);
118   frame_timer.init(true);
119   kick_timer.init(true);
120   shooting_timer.init(true);
121
122   physic.reset();
123 }
124
125 int
126 Player::key_event(SDLKey key, int state)
127 {
128   if(key == keymap.right)
129     {
130       input.right = state;
131       return true;
132     }
133   else if(key == keymap.left)
134     {
135       input.left = state;
136       return true;
137     }
138   else if(key == keymap.jump)
139     {
140       input.up = state;
141       return true;
142     }
143   else if(key == keymap.duck)
144     {
145       input.down = state;
146       return true;
147     }
148   else if(key == keymap.fire)
149     {
150       if (state == UP)
151         input.old_fire = UP;
152       input.fire = state;
153       return true;
154     }
155   else
156     return false;
157 }
158
159 void
160 Player::level_begin()
161 {
162   base.x  = 100;
163   base.y  = 170;
164   previous_base = old_base = base;
165   duck = false;
166
167   dying = DYING_NOT;
168
169   player_input_init(&input);
170
171   invincible_timer.init(true);
172   skidding_timer.init(true);
173   safe_timer.init(true);
174   frame_timer.init(true);
175
176   physic.reset();
177 }
178
179 void
180 Player::action(float elapsed_time)
181 {
182   bool jumped_in_solid = false;
183
184   if (input.fire == UP)
185     holding_something = false;
186
187   /* Move tux: */
188   previous_base = base;
189
190   /* --- HANDLE TUX! --- */
191   if(dying == DYING_NOT)
192     handle_input();
193
194   physic.apply(elapsed_time, base.x, base.y);
195
196   if(dying == DYING_NOT) 
197     {
198       base_type target = base;
199
200       collision_swept_object_map(&old_base, &base);
201
202       if ((!invincible_timer.started() && !safe_timer.started())
203           && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
204           ||  isspike(base.x, base.y + base.height)
205           ||  isspike(base.x + base.width, base.y + base.height)))
206       {
207          kill(SHRINK);
208       }
209
210       // Don't accelerate Tux if he is running against a wall
211       if (target.x != base.x)
212         {
213           physic.set_velocity_x(0);
214         }
215
216       // special exception for cases where we're stuck under tiles after
217       // being ducked. In this case we drift out
218       if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
219          && collision_object_map(base))
220         {
221           base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
222           previous_base = old_base = base;
223         }
224
225       // Land:
226       if (!on_ground())
227         {
228           physic.enable_gravity(true);
229           if(under_solid())
230             {
231               // fall down
232               physic.set_velocity_y(0);
233               jumped_in_solid = true;
234             }
235         }
236       else
237         {
238           /* Land: */
239           if (physic.get_velocity_y() < 0)
240             {
241               base.y = (int)(((int)base.y / 32) * 32);
242               physic.set_velocity_y(0);
243             }
244
245           physic.enable_gravity(false);
246           /* Reset score multiplier (for multi-hits): */
247           if (!invincible_timer.started())
248             player_status.score_multiplier = 1;
249         }
250
251       if(jumped_in_solid)
252         {
253           if (isbrick(base.x, base.y) ||
254               isfullbox(base.x, base.y))
255             {
256               World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
257               World::current()->trybumpbadguy(base.x, base.y - 64);
258
259               World::current()->trybreakbrick(base.x, base.y, size == SMALL);
260
261               bumpbrick(base.x, base.y);
262               World::current()->tryemptybox(base.x, base.y, RIGHT);
263             }
264
265           if (isbrick(base.x+ 31, base.y) ||
266               isfullbox(base.x+ 31, base.y))
267             {
268               World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
269               World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
270
271               if(size == BIG)
272                 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
273
274               bumpbrick(base.x+ 31, base.y);
275               World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
276             }
277         }
278
279       grabdistros();
280
281       if (jumped_in_solid)
282         {
283           ++base.y;
284           ++old_base.y;
285           if(on_ground())
286             {
287               /* Make sure jumping is off. */
288               jumping = false;
289             }
290         }
291     }
292
293   /* ---- DONE HANDLING TUX! --- */
294
295   // check some timers
296   skidding_timer.check();
297   invincible_timer.check();
298   safe_timer.check();
299   kick_timer.check();
300 }
301
302 bool
303 Player::on_ground()
304 {
305   return ( issolid(base.x + base.width / 2, base.y + base.height) ||
306            issolid(base.x + 1, base.y + base.height) ||
307            issolid(base.x + base.width - 1, base.y + base.height)  );
308 }
309
310 bool
311 Player::under_solid()
312 {
313   return ( issolid(base.x + base.width / 2, base.y) ||
314            issolid(base.x + 1, base.y) ||
315            issolid(base.x + base.width - 1, base.y)  );
316 }
317
318 bool
319 Player::tiles_on_air(int tiles)
320 {
321   for(int t = 0; t != tiles; t++)
322      {
323      if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
324          issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
325          issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
326        return false;
327      }
328   return true;
329 }
330
331 void
332 Player::handle_horizontal_input()
333 {
334   float vx = physic.get_velocity_x();
335   float vy = physic.get_velocity_y();
336   float ax = physic.get_acceleration_x();
337   float ay = physic.get_acceleration_y();
338
339   float dirsign = 0;
340   if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
341       old_dir = dir;
342       dir = LEFT;
343       dirsign = -1;
344   } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
345       old_dir = dir;
346       dir = RIGHT;
347       dirsign = 1;
348   }
349
350   if (input.fire == UP) {
351       ax = dirsign * WALK_ACCELERATION_X;
352       // limit speed
353       if(vx >= MAX_WALK_XM && dirsign > 0) {
354         vx = MAX_WALK_XM;
355         ax = 0;
356       } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
357         vx = -MAX_WALK_XM;
358         ax = 0;
359       }
360   } else {
361       ax = dirsign * RUN_ACCELERATION_X;
362       // limit speed
363       if(vx >= MAX_RUN_XM && dirsign > 0) {
364         vx = MAX_RUN_XM;
365         ax = 0;
366       } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
367         vx = -MAX_RUN_XM;
368         ax = 0;
369       }
370   }
371
372   // we can reach WALK_SPEED without any acceleration
373   if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
374     vx = dirsign * WALK_SPEED;
375   }
376
377   // changing directions?
378   if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
379       if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
380           skidding_timer.start(SKID_TIME);
381           play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
382           ax *= 2.5;
383       } else {
384           ax *= 2;
385       }
386   }
387
388   // we get slower when not pressing any keys
389   if(dirsign == 0) {
390       if(fabs(vx) < WALK_SPEED) {
391           vx = 0;
392           ax = 0;
393       } else if(vx < 0) {
394           ax = WALK_ACCELERATION_X * 1.5;
395       } else {
396           ax = WALK_ACCELERATION_X * -1.5;
397       }
398   }
399
400   // if we're on ice slow down acceleration or deceleration
401   if (isice(base.x, base.y + base.height))
402   {
403     /* the acceleration/deceleration rate on ice is inversely proportional to
404      * the current velocity.
405      */
406
407     // increasing 1 will increase acceleration/deceleration rate
408     // decreasing 1 will decrease acceleration/deceleration rate
409     //  must stay above zero, though
410     if (ax != 0) ax *= 1 / fabs(vx);
411   }
412
413   physic.set_velocity(vx, vy);
414   physic.set_acceleration(ax, ay);
415 }
416
417 void
418 Player::handle_vertical_input()
419 {
420   // set fall mode...
421   if(on_ground()) {
422     fall_mode = ON_GROUND;
423     last_ground_y = base.y;
424   } else {
425     if(base.y > last_ground_y)
426       fall_mode = FALLING;
427     else if(fall_mode == ON_GROUND)
428       fall_mode = JUMPING;
429   }
430
431   // Press jump key
432   if(input.up == DOWN && can_jump && on_ground())
433     {
434       if(duck) { // only jump a little bit when in duck mode {
435         physic.set_velocity_y(3);
436       } else {
437         // jump higher if we are running
438         if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
439           physic.set_velocity_y(5.8);
440         else
441           physic.set_velocity_y(5.2);
442       }
443
444       --base.y;
445       jumping = true;
446       can_jump = false;
447       if (size == SMALL)
448         play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
449       else
450         play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
451     }
452   // Let go of jump key
453   else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
454     {
455       jumping = false;
456       physic.set_velocity_y(0);
457     }
458
459    /* In case the player has pressed Down while in a certain range of air,
460       enable butt jump action */
461   if (input.down == DOWN && !butt_jump)
462     if(tiles_on_air(TILES_FOR_BUTTJUMP))
463       butt_jump = true;
464
465    /* When Down is not held anymore, disable butt jump */
466   if(butt_jump && input.down == UP)
467     butt_jump = false;
468
469   if (butt_jump && on_ground() && size == BIG)
470   {
471     if(World::current()->trybreakbrick(base.x, base.y + base.height, false)
472       || World::current()->trybreakbrick(
473           base.x + base.width, base.y + base.height, false)) {
474         // make tux jumping a little bit again after breaking the bricks
475         physic.set_velocity_y(2);
476     }
477 //    butt_jump = false;   // comment this, in case you won't to disable the continued use of buttjump
478   }
479
480   if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
481         issolid(base.x + 1, base.y + base.height + 64) ||
482         issolid(base.x + base.width - 1, base.y + base.height + 64))
483        && jumping  == false
484        && can_jump == false
485        && input.up == DOWN
486        && input.old_up == UP)
487     {
488       can_jump = true;
489     }
490
491   if(on_ground())   /* Make sure jumping is off. */
492     jumping = false;
493
494   input.old_up = input.up;
495 }
496
497 void
498 Player::handle_input()
499 {
500   /* Handle horizontal movement: */
501     handle_horizontal_input();
502
503   /* Jump/jumping? */
504
505   if (on_ground() && input.up == UP)
506     can_jump = true;
507   handle_vertical_input();
508
509   /* Shoot! */
510   if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
511     {
512       if(World::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
513           physic.get_velocity_x(), dir))
514         shooting_timer.start(SHOOTING_TIME);
515       input.old_fire = DOWN;
516     }
517
518   /* tux animations: */
519   if(!frame_timer.check())
520     {
521       frame_timer.start(25);
522       if (input.right == UP && input.left == UP)
523         {
524           frame_main = 1;
525           frame_ = 1;
526         }
527       else
528         {
529           if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
530               (global_frame_counter % 4) == 0)
531             frame_main = (frame_main + 1) % 4;
532
533           frame_ = frame_main;
534
535           if (frame_ == 3)
536             frame_ = 1;
537         }
538     }
539
540   /* Duck! */
541   if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
542     {
543       duck = true;
544       base.height = 32;                             
545       base.y += 32;
546       // changing base size confuses collision otherwise
547       old_base = previous_base = base;
548     }
549   else if(input.down == UP && size == BIG && duck)
550     {
551       // try if we can really unduck
552       base.y -= 32;
553       base.height = 64;
554       // when unducking in air we need some space to do so
555       if(on_ground() || !collision_object_map(base)) {
556         duck = false;
557         // changing base size confuses collision otherwise
558         old_base = previous_base = base;                                
559       } else {
560         // undo the ducking changes
561         base.y += 32;
562         base.height = 32;
563       }   
564     }
565 }
566
567 void
568 Player::grow()
569 {
570   if(size == BIG)
571     return;
572   
573   size = BIG;
574   base.height = 64;
575   base.y -= 32;
576
577   old_base = previous_base = base;
578 }
579
580 void
581 Player::grabdistros()
582 {
583   /* Grab distros: */
584   if (!dying)
585     {
586       World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
587       World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
588
589       World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
590       World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
591
592       if(size == BIG)
593         {
594           World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
595           World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
596         }
597
598     }
599
600   /* Enough distros for a One-up? */
601   if (player_status.distros >= DISTROS_LIFEUP)
602     {
603       player_status.distros = player_status.distros - DISTROS_LIFEUP;
604       if(player_status.lives < MAX_LIVES)
605         ++player_status.lives;
606       /*We want to hear the sound even, if MAX_LIVES is reached*/
607       play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
608     }
609 }
610
611 void
612 Player::draw(Camera& viewport, int layer)
613 {
614   PlayerSprite* sprite;
615           
616   if (size == SMALL)
617     sprite = &smalltux;
618   else if (got_power == FIRE_POWER)
619     sprite = &firetux;
620   else if (got_power == ICE_POWER)
621     sprite = &icetux;
622   else
623     sprite = &largetux;
624
625   Vector pos = viewport.world2screen(Vector(base.x, base.y));
626
627   if(layer == LAYER_OBJECTS + 1) {
628     // Draw arm overlay graphics when Tux is holding something
629     if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check() && !duck)
630     {
631       if (dir == RIGHT)
632         sprite->grab_right->draw(pos);
633       else
634         sprite->grab_left->draw(pos);
635     }
636
637     // Draw blinking star overlay
638     if (invincible_timer.started() &&
639         (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
640     {
641       if (size == SMALL || duck)
642         smalltux_star->draw(pos);
643       else
644         largetux_star->draw(pos);
645     }
646     
647     return;
648   }
649   
650   if (!safe_timer.started() || (global_frame_counter % 2) == 0)
651     {
652       if (dying == DYING_SQUISHED)
653         {
654           smalltux_gameover->draw(pos);
655         }
656       else
657         {
658           if (duck && size != SMALL)
659             {
660               if (dir == RIGHT)
661                 sprite->duck_right->draw(pos);
662               else 
663                 sprite->duck_left->draw(pos);
664             }
665           else if (skidding_timer.started())
666             {
667               if (dir == RIGHT)
668                 sprite->skid_right->draw(pos);
669               else
670                 sprite->skid_left->draw(pos); 
671             }
672           else if (kick_timer.started())
673             {
674               if (dir == RIGHT)
675                 sprite->kick_right->draw(pos);
676               else
677                 sprite->kick_left->draw(pos); 
678             }
679           else if (physic.get_velocity_y() != 0)
680             {
681               if (dir == RIGHT)
682                 sprite->jump_right->draw(pos);
683               else
684                 sprite->jump_left->draw(pos);                   
685             }
686           else
687             {
688               if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
689                 {
690                   if (dir == RIGHT)
691                     sprite->stand_right->draw(pos);
692                   else
693                     sprite->stand_left->draw(pos);
694                 }
695               else // moving
696                 {
697                   if (dir == RIGHT)
698                     sprite->walk_right->draw(pos);
699                   else
700                     sprite->walk_left->draw(pos);
701                 }
702             }
703         }
704     }     
705   
706   if (debug_mode)
707     fillrect(base.x - viewport.get_translation().x,
708         base.y - viewport.get_translation().y, 
709              base.width, base.height, 75,75,75, 150);
710 }
711
712 void
713 Player::collision(const MovingObject& other, int collision_type)
714 {
715   (void) other;
716   (void) collision_type;
717   // will be implemented later
718 }
719
720 void
721 Player::collision(void* p_c_object, int c_object)
722 {
723   BadGuy* pbad_c = NULL;
724   Trampoline* ptramp_c = NULL;
725   FlyingPlatform* pplatform_c = NULL;
726
727   switch (c_object)
728     {
729     case CO_BADGUY:
730       pbad_c = (BadGuy*) p_c_object;
731
732      /* Hurt player if he touches a badguy */
733       if (!pbad_c->dying && !dying &&
734           !safe_timer.started() &&
735           pbad_c->mode != BadGuy::HELD)
736         {
737           if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
738                && !holding_something)
739             {
740               holding_something = true;
741               pbad_c->mode = BadGuy::HELD;
742               pbad_c->base.y-=8;
743             }
744           else if (pbad_c->mode == BadGuy::FLAT)
745             {
746               // Don't get hurt if we're kicking a flat badguy!
747             }
748           else if (pbad_c->mode == BadGuy::KICK)
749             {
750               /* Hurt if you get hit by kicked laptop: */
751               if (!invincible_timer.started())
752                 {
753                   kill(SHRINK);
754                 }
755               else
756                 pbad_c->kill_me(20);
757             }
758           else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
759               || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
760               || pbad_c->kind == BAD_SPIKY))
761                 pbad_c->kill_me(20);
762           else
763             {
764               if (!invincible_timer.started())
765                 {
766                   kill(SHRINK);
767                 }
768               else
769                 {
770                   pbad_c->kill_me(25);
771                 }
772             }
773           player_status.score_multiplier++;
774         }
775       break;
776
777     case CO_TRAMPOLINE:
778       ptramp_c = (Trampoline*) p_c_object;
779       
780       // Pick up trampoline
781       if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
782       {
783         holding_something = true;
784         ptramp_c->mode = Trampoline::M_HELD;
785         ptramp_c->base.y -= 8;
786       }
787       // Set down trampoline
788       else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
789       {
790         holding_something = false;
791         ptramp_c->mode = Trampoline::M_NORMAL;
792         ptramp_c->base.y += 8;
793         ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
794
795         //if (dir == RIGHT)
796         //  ptramp_c->base.x = base.x + base.width+1;
797         //else /* LEFT */
798         //  ptramp_c->base.x = base.x - base.width-1;
799       }
800 /*
801       // Don't let tux walk through trampoline
802       else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
803       {
804         if (physic.get_velocity_x() > 0) // RIGHT
805         {
806           physic.set_velocity_x(0);
807           base.x = ptramp_c->base.x - base.width;
808         }
809         else if (physic.get_velocity_x() < 0) // LEFT
810         {
811           physic.set_velocity_x(0);
812           base.x = ptramp_c->base.x + ptramp_c->base.width;
813         }
814       }
815 */
816       break;
817     case CO_FLYING_PLATFORM:
818       pplatform_c = (FlyingPlatform*) p_c_object;
819       
820       base.y = pplatform_c->base.y - base.height;
821       break;
822
823     default:
824       break;
825     }
826
827 }
828
829 /* Kill Player! */
830
831 void
832 Player::kill(HurtMode mode)
833 {
834   play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
835
836   physic.set_velocity_x(0);
837
838   if (mode == SHRINK && size == BIG)
839     {
840       if (got_power != NONE_POWER)
841         {
842           got_power = NONE_POWER;
843         }
844       else
845         {
846           size = SMALL;
847           base.height = 32;
848           duck = false;
849         }
850       safe_timer.start(TUX_SAFE_TIME);
851     }
852   else
853     {
854       physic.enable_gravity(true);
855       physic.set_acceleration(0, 0);
856       physic.set_velocity(0, 7);
857       if(dying != DYING_SQUISHED)
858       --player_status.lives;
859       dying = DYING_SQUISHED;
860     }
861 }
862
863 void
864 Player::is_dying()
865 {
866   remove_powerups();
867   dying = DYING_NOT;
868 }
869
870 bool Player::is_dead()
871 {
872   float scroll_x =
873     World::current()->camera->get_translation().x;
874   float scroll_y =
875     World::current()->camera->get_translation().y;
876   if(base.y > screen->h + scroll_y || base.y > World::current()->get_level()->height*32 ||
877       base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL)  // can happen in auto-scrolling
878     return true;
879   else
880     return false;
881 }
882
883 /* Remove Tux's power ups */
884 void
885 Player::remove_powerups()
886 {
887   got_power = NONE_POWER;
888   size = SMALL;
889   base.height = 32;
890 }
891
892 void
893 Player::check_bounds(Camera& viewport,
894     bool back_scrolling, bool hor_autoscroll)
895 {
896   /* Keep tux in bounds: */
897   if (base.x < 0)
898     { // Lock Tux to the size of the level, so that he doesn't fall of
899       // on the left side
900       base.x = 0;
901     }
902
903   /* Keep in-bounds, vertically: */
904   if (base.y > World::current()->get_level()->height * /*TILE_HEIGHT*/ 32)
905     {
906       kill(KILL);
907     }
908
909   if(base.x < viewport.get_translation().x && (!back_scrolling || hor_autoscroll))  // can happen if back scrolling is disabled
910     base.x = viewport.get_translation().x;
911
912   if(hor_autoscroll)
913     {
914     if(base.x == viewport.get_translation().x)
915       if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32)))
916         kill(KILL);
917
918     if(base.x + base.width > viewport.get_translation().x + screen->w)
919       base.x = viewport.get_translation().x + screen->w - base.width;
920     }
921     
922 }
923
924 // EOF //
925