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