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