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