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