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