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