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