- some indentation
[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   /* Set Tux sprite action */
738   if (duck && size != SMALL)
739     tux_body->set_action("duck");
740   else if (skidding_timer.started())
741     tux_body->set_action("skid");
742   else if (kick_timer.started())
743     tux_body->set_action("kick");
744   else if (butt_jump)
745     tux_body->set_action("buttjump");
746   else if (physic.get_velocity_y() != 0)
747     tux_body->set_action("jump");
748   else
749     {
750     if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
751       {
752       tux_body->set_action("stand");
753       }
754     else // moving
755       {
756       tux_body->set_action("walk");
757       }
758     }
759
760   if(idle_timer.get_left() < 0)
761     {
762     tux_body->head->set_action("idle");
763     tux_body->head->start_animation(1);
764
765     idle_timer.start(IDLE_TIME);
766     }
767
768   // Tux is holding something
769   if ((holding_something && physic.get_velocity_y() == 0) ||
770       shooting_timer.check() && !duck)
771     {
772     tux_body->arms->set_action("grab");
773     }
774
775   /* Draw Tux */
776   if (dying == DYING_SQUISHED)
777     {
778     smalltux_gameover->draw(context, pos, LAYER_FOREGROUNDTILES+1);
779     }
780   else if(growing_timer.check())
781     {
782     if(size == SMALL)
783       {
784       if (dir == RIGHT)
785         context.draw_surface(growingtux_right[GROWING_FRAMES-1 - 
786                  ((growing_timer.get_gone() *
787                  GROWING_FRAMES) / GROWING_TIME)], pos, layer);
788       else
789         context.draw_surface(growingtux_left[GROWING_FRAMES-1 - 
790                 ((growing_timer.get_gone() *
791                 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
792       }
793     else
794       {
795       if (dir == RIGHT)
796         context.draw_surface(growingtux_right[(growing_timer.get_gone() *
797                 GROWING_FRAMES) / GROWING_TIME], pos, layer);
798       else
799         context.draw_surface(growingtux_left[(growing_timer.get_gone() *
800                              GROWING_FRAMES) / GROWING_TIME], pos, layer);
801       }
802     }
803   else if (safe_timer.started() && global_frame_counter%2)
804     ;  // don't draw Tux
805   else
806     tux_body->draw(context, pos, layer, dir == LEFT ? HORIZONTAL_FLIP : NONE_EFFECT);
807
808   // Draw blinking star overlay
809   if (invincible_timer.started() &&
810      (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
811   {
812     if (size == SMALL || duck)
813       smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
814     else
815       bigtux_star->draw(context, pos, LAYER_OBJECTS + 2);
816   }
817  
818   if (debug_mode)
819     context.draw_filled_rect(Vector(base.x, base.y),
820         Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
821 }
822
823 void
824 Player::collision(const MovingObject& other, int collision_type)
825 {
826   (void) other;
827   (void) collision_type;
828   // will be implemented later
829 }
830
831 void
832 Player::collision(void* p_c_object, int c_object)
833 {
834   BadGuy* pbad_c = NULL;
835   Trampoline* ptramp_c = NULL;
836   FlyingPlatform* pplatform_c = NULL;
837
838   switch (c_object)
839     {
840     case CO_BADGUY:
841       pbad_c = (BadGuy*) p_c_object;
842
843      /* Hurt player if he touches a badguy */
844       if (!pbad_c->dying && !dying &&
845           !safe_timer.started() &&
846           pbad_c->mode != BadGuy::HELD)
847         {
848           if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
849                && !holding_something)
850             {
851               holding_something = true;
852               pbad_c->mode = BadGuy::HELD;
853               pbad_c->base.y-=8;
854             }
855           else if (pbad_c->mode == BadGuy::FLAT)
856             {
857               // Don't get hurt if we're kicking a flat badguy!
858             }
859           else if (pbad_c->mode == BadGuy::KICK)
860             {
861               /* Hurt if you get hit by kicked laptop: */
862               if (!invincible_timer.started())
863                 {
864                   kill(SHRINK);
865                 }
866               else
867                 pbad_c->kill_me(20);
868             }
869           else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
870               || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
871               || pbad_c->kind == BAD_SPIKY))
872                 pbad_c->kill_me(20);
873           else
874             {
875               if (!invincible_timer.started())
876                 {
877                   kill(SHRINK);
878                 }
879               else
880                 {
881                   pbad_c->kill_me(25);
882                 }
883             }
884           player_status.score_multiplier++;
885         }
886       break;
887
888     case CO_TRAMPOLINE:
889       ptramp_c = (Trampoline*) p_c_object;
890       
891       // Pick up trampoline
892       if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
893       {
894         holding_something = true;
895         ptramp_c->mode = Trampoline::M_HELD;
896         ptramp_c->base.y -= 8;
897       }
898       // Set down trampoline
899       else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
900       {
901         holding_something = false;
902         ptramp_c->mode = Trampoline::M_NORMAL;
903         ptramp_c->base.y += 8;
904         ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
905
906         //if (dir == RIGHT)
907         //  ptramp_c->base.x = base.x + base.width+1;
908         //else /* LEFT */
909         //  ptramp_c->base.x = base.x - base.width-1;
910       }
911 /*
912       // Don't let tux walk through trampoline
913       else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
914       {
915         if (physic.get_velocity_x() > 0) // RIGHT
916         {
917           physic.set_velocity_x(0);
918           base.x = ptramp_c->base.x - base.width;
919         }
920         else if (physic.get_velocity_x() < 0) // LEFT
921         {
922           physic.set_velocity_x(0);
923           base.x = ptramp_c->base.x + ptramp_c->base.width;
924         }
925       }
926 */
927       break;
928     case CO_FLYING_PLATFORM:
929       pplatform_c = (FlyingPlatform*) p_c_object;
930       
931       base.y = pplatform_c->base.y - base.height;
932       physic.set_velocity_x(pplatform_c->get_vel_x());
933       
934       physic.enable_gravity(false);
935       can_jump = true;
936       fall_mode = ON_GROUND;
937       break;
938
939     default:
940       break;
941     }
942
943 }
944
945 /* Kill Player! */
946
947 void
948 Player::kill(HurtMode mode)
949 {
950   if(dying)
951     return;
952   
953   SoundManager::get()->play_sound(IDToSound(SND_HURT));
954
955   physic.set_velocity_x(0);
956
957   if (mode == SHRINK && size == BIG)
958     {
959       if (got_power != NONE_POWER)
960         {
961           safe_timer.start(TUX_SAFE_TIME);
962           got_power = NONE_POWER;
963         }
964       else
965         {
966           growing_timer.start(GROWING_TIME);
967           safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
968           size = SMALL;
969           base.height = 32;
970           duck = false;
971         }
972     }
973   else
974     {
975       physic.enable_gravity(true);
976       physic.set_acceleration(0, 0);
977       physic.set_velocity(0, 7);
978       --player_status.lives;
979       dying = DYING_SQUISHED;
980       dying_timer.start(3000);
981     }
982 }
983
984 /* Remove Tux's power ups */
985 void
986 Player::remove_powerups()
987 {
988   got_power = NONE_POWER;
989   size = SMALL;
990   base.height = 32;
991 }
992
993 void
994 Player::move(const Vector& vector)
995 {
996   base.x = vector.x;
997   base.y = vector.y;
998   old_base = previous_base = base;
999 }
1000
1001 void
1002 Player::check_bounds(Camera* camera)
1003 {
1004   /* Keep tux in bounds: */
1005   if (base.x < 0)
1006     { // Lock Tux to the size of the level, so that he doesn't fall of
1007       // on the left side
1008       base.x = 0;
1009     }
1010
1011   /* Keep in-bounds, vertically: */
1012   if (base.y > Sector::current()->solids->get_height() * 32)
1013     {
1014       kill(KILL);
1015       return;
1016     }
1017
1018   bool adjust = false;
1019   // can happen if back scrolling is disabled
1020   if(base.x < camera->get_translation().x) {
1021     base.x = camera->get_translation().x;
1022     adjust = true;
1023   }
1024   if(base.x >= camera->get_translation().x + screen->w - base.width) {
1025     base.x = camera->get_translation().x + screen->w - base.width;
1026     adjust = true;
1027   }
1028
1029   if(adjust) {
1030     // squished now?
1031     if(collision_object_map(base)) {
1032       kill(KILL);
1033       return;
1034     }
1035   }
1036 }
1037
1038 void
1039 Player::bounce(BadGuy* badguy)
1040 {
1041   if (input.up)
1042     physic.set_velocity_y(5.2);
1043   else
1044     physic.set_velocity_y(2);
1045
1046   // Move the player a little bit above the badguy to avoid collision
1047   // between badguy and player directly after the bounce has happend
1048   base.y = badguy->base.y - base.height - 2;
1049 }
1050
1051 /* EOF */
1052