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