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