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