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