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