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