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