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