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