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