Fixed bug that was causing player's dead.
[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 <math.h>
21 #include "gameloop.h"
22 #include "globals.h"
23 #include "player.h"
24 #include "defines.h"
25 #include "scene.h"
26 #include "tile.h"
27 #include "sprite.h"
28 #include "screen.h"
29
30 #define AUTOSCROLL_DEAD_INTERVAL 300
31
32 Surface* tux_life;
33
34 Sprite* smalltux_gameover;
35 Sprite* smalltux_star;
36 Sprite* largetux_star;
37
38 PlayerSprite smalltux;
39 PlayerSprite largetux;
40 PlayerSprite firetux;
41
42 PlayerKeymap keymap;
43
44 PlayerKeymap::PlayerKeymap()
45 {
46   keymap.jump  = SDLK_UP;
47   keymap.duck  = SDLK_DOWN;
48   keymap.left  = SDLK_LEFT;
49   keymap.right = SDLK_RIGHT;
50   keymap.fire  = SDLK_LCTRL;
51 }
52
53 void player_input_init(player_input_type* pplayer_input)
54 {
55   pplayer_input->down = UP;
56   pplayer_input->fire = UP;
57   pplayer_input->left = UP;
58   pplayer_input->old_fire = UP;
59   pplayer_input->right = UP;
60   pplayer_input->up = UP;
61   pplayer_input->old_up = UP;
62 }
63
64 void
65 Player::init()
66 {
67   Level* plevel = World::current()->get_level();
68
69   holding_something = false;
70
71   base.width = 32;
72   base.height = 32;
73
74   size = SMALL;
75   got_coffee = false;
76
77   base.x = plevel->start_pos_x;
78   base.y = plevel->start_pos_y;
79   base.xm = 0;
80   base.ym = 0;
81   previous_base = old_base = base;
82   dir = RIGHT;
83   old_dir = dir;
84   duck = false;
85
86   dying   = DYING_NOT;
87   jumping = false;
88   can_jump = true;
89
90   frame_main = 0;
91   frame_ = 0;
92   
93   player_input_init(&input);
94
95   invincible_timer.init(true);
96   skidding_timer.init(true);
97   safe_timer.init(true);
98   frame_timer.init(true);
99   kick_timer.init(true);
100
101   physic.reset();
102 }
103
104 int
105 Player::key_event(SDLKey key, int state)
106 {
107   if(key == keymap.right)
108     {
109       input.right = state;
110       return true;
111     }
112   else if(key == keymap.left)
113     {
114       input.left = state;
115       return true;
116     }
117   else if(key == keymap.jump)
118     {
119       input.up = state;
120       return true;
121     }
122   else if(key == keymap.duck)
123     {
124       input.down = state;
125       return true;
126     }
127   else if(key == keymap.fire)
128     {
129       if (state == UP)
130         input.old_fire = UP;
131       input.fire = state;
132       return true;
133     }
134   else
135     return false;
136 }
137
138 void
139 Player::level_begin()
140 {
141   base.x  = 100;
142   base.y  = 170;
143   base.xm = 0;
144   base.ym = 0;
145   previous_base = old_base = base;
146   duck = false;
147
148   dying = DYING_NOT;
149
150   player_input_init(&input);
151
152   invincible_timer.init(true);
153   skidding_timer.init(true);
154   safe_timer.init(true);
155   frame_timer.init(true);
156
157   physic.reset();
158 }
159
160 void
161 Player::action(double frame_ratio)
162 {
163   bool jumped_in_solid = false;
164
165   if (input.fire == UP)
166     holding_something = false;
167
168   /* Move tux: */
169   previous_base = base;
170
171   /* --- HANDLE TUX! --- */
172   if(dying == DYING_NOT)
173     handle_input();
174
175   physic.apply(frame_ratio, base.x, base.y);
176
177   if(dying == DYING_NOT) 
178     {
179       base_type target = base;
180
181       collision_swept_object_map(&old_base, &base);
182
183       // Don't accelerate Tux if he is running against a wall
184       if (target.x != base.x)
185         {
186           physic.set_velocity_x(0);
187         }
188
189       // special exception for cases where we're stuck under tiles after
190       // being ducked. In this case we drift out
191       if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
192          && collision_object_map(base))
193         {
194           base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
195           previous_base = old_base = base;
196         }
197       check_bounds();
198
199       // Land:
200       if (!on_ground())
201         {
202           physic.enable_gravity(true);
203           if(under_solid())
204             {
205               // fall down
206               physic.set_velocity_y(0);
207               jumped_in_solid = true;
208             }
209         }
210       else
211         {
212           /* Land: */
213           if (physic.get_velocity_y() < 0)
214             {
215               base.y = (int)(((int)base.y / 32) * 32);
216               physic.set_velocity_y(0);
217             }
218
219           physic.enable_gravity(false);
220           /* Reset score multiplier (for multi-hits): */
221           player_status.score_multiplier = 1;
222         }
223
224       if(jumped_in_solid)
225         {
226           if (isbrick(base.x, base.y) ||
227               isfullbox(base.x, base.y))
228             {
229               World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
230               World::current()->trybumpbadguy(base.x, base.y - 64);
231
232               World::current()->trybreakbrick(base.x, base.y, size == SMALL);
233
234               bumpbrick(base.x, base.y);
235               World::current()->tryemptybox(base.x, base.y, RIGHT);
236             }
237
238           if (isbrick(base.x+ 31, base.y) ||
239               isfullbox(base.x+ 31, base.y))
240             {
241               World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
242               World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
243
244               if(size == BIG)
245                 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
246
247               bumpbrick(base.x+ 31, base.y);
248               World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
249             }
250         }
251
252       grabdistros();
253
254       if (jumped_in_solid)
255         {
256           ++base.y;
257           ++old_base.y;
258           if(on_ground())
259             {
260               /* Make sure jumping is off. */
261               jumping = false;
262             }
263         }
264     }
265
266   /* ---- DONE HANDLING TUX! --- */
267
268   // check some timers
269   skidding_timer.check();
270   invincible_timer.check();
271   safe_timer.check();
272   kick_timer.check();
273 }
274
275 bool
276 Player::on_ground()
277 {
278   return ( issolid(base.x + base.width / 2, base.y + base.height) ||
279            issolid(base.x + 1, base.y + base.height) ||
280            issolid(base.x + base.width - 1, base.y + base.height)  );
281 }
282
283 bool
284 Player::under_solid()
285 {
286   return ( issolid(base.x + base.width / 2, base.y) ||
287            issolid(base.x + 1, base.y) ||
288            issolid(base.x + base.width - 1, base.y)  );
289 }
290
291 void
292 Player::handle_horizontal_input()
293 {
294   float vx = physic.get_velocity_x();
295   float vy = physic.get_velocity_y();
296   float ax = physic.get_acceleration_x();
297   float ay = physic.get_acceleration_y();
298
299   float dirsign = 0;
300   if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
301       old_dir = dir;
302       dir = LEFT;
303       dirsign = -1;
304   } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
305       old_dir = dir;
306       dir = RIGHT;
307       dirsign = 1;
308   }
309
310   if (input.fire == UP) {
311       ax = dirsign * WALK_ACCELERATION_X;
312       // limit speed
313       if(vx >= MAX_WALK_XM && dirsign > 0) {
314         vx = MAX_WALK_XM;
315         ax = 0;
316       } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
317         vx = -MAX_WALK_XM;
318         ax = 0;
319       }
320   } else {
321       ax = dirsign * RUN_ACCELERATION_X;
322       // limit speed
323       if(vx >= MAX_RUN_XM && dirsign > 0) {
324         vx = MAX_RUN_XM;
325         ax = 0;
326       } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
327         vx = -MAX_RUN_XM;
328         ax = 0;
329       }
330   }
331
332   // we can reach WALK_SPEED without any acceleration
333   if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
334     vx = dirsign * WALK_SPEED;
335   }
336
337   // changing directions?
338   if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
339       if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
340           skidding_timer.start(SKID_TIME);
341           play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
342           ax *= 2.5;
343       } else {
344           ax *= 2;
345       }
346   }
347
348   // we get slower when not pressing any keys
349   if(dirsign == 0) {
350       if(fabs(vx) < WALK_SPEED) {
351           vx = 0;
352           ax = 0;
353       } else if(vx < 0) {
354           ax = WALK_ACCELERATION_X * 1.5;
355       } else {
356           ax = WALK_ACCELERATION_X * -1.5;
357       }
358   }
359
360   // if we're on ice slow down acceleration or deceleration
361   if (isice(base.x, base.y + base.height))
362   {
363     /* the acceleration/deceleration rate on ice is inversely proportional to
364      * the current velocity.
365      */
366
367     // increasing 1 will increase acceleration/deceleration rate
368     // decreasing 1 will decrease acceleration/deceleration rate
369     //  must stay above zero, though
370     if (ax != 0) ax *= 1 / fabs(vx);
371   }
372
373   physic.set_velocity(vx, vy);
374   physic.set_acceleration(ax, ay);
375 }
376
377 void
378 Player::handle_vertical_input()
379 {
380   // Press jump key
381   if(input.up == DOWN && can_jump)
382     {
383       if (on_ground())
384         {
385           // jump higher if we are running
386           if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
387             physic.set_velocity_y(5.8);
388           else
389             physic.set_velocity_y(5.2);
390
391           --base.y;
392           jumping = true;
393           can_jump = false;
394           if (size == SMALL)
395             play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
396           else
397             play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
398         }
399     }
400   // Let go of jump key
401   else if(input.up == UP && jumping)
402     {
403       jumping = false;
404       if(physic.get_velocity_y() > 0) {
405         physic.set_velocity_y(0);
406       }
407     }
408
409   if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
410         issolid(base.x + 1, base.y + base.height + 64) ||
411         issolid(base.x + base.width - 1, base.y + base.height + 64))
412        && jumping  == false
413        && can_jump == false
414        && input.up == DOWN
415        && input.old_up == UP)
416     {
417       can_jump = true;
418     }
419
420   input.old_up = input.up;
421 }
422
423 void
424 Player::handle_input()
425 {
426   /* Handle horizontal movement: */
427     handle_horizontal_input();
428
429   /* Jump/jumping? */
430
431   if (on_ground() && input.up == UP)
432     can_jump = true;
433   if (input.up == DOWN || (input.up == UP && jumping))
434     {
435       handle_vertical_input();
436     }
437
438   /* Shoot! */
439
440   if (input.fire == DOWN && input.old_fire == UP && got_coffee)
441     {
442       World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
443       input.old_fire = DOWN;
444     }
445
446   /* tux animations: */
447   if(!frame_timer.check())
448     {
449       frame_timer.start(25);
450       if (input.right == UP && input.left == UP)
451         {
452           frame_main = 1;
453           frame_ = 1;
454         }
455       else
456         {
457           if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
458               (global_frame_counter % 4) == 0)
459             frame_main = (frame_main + 1) % 4;
460
461           frame_ = frame_main;
462
463           if (frame_ == 3)
464             frame_ = 1;
465         }
466     }
467
468   /* Duck! */
469   if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
470     {
471       duck = true;
472       base.height = 32;                             
473       base.y += 32;
474       // changing base size confuses collision otherwise
475       old_base = previous_base = base;
476     }
477   else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
478     {
479       duck = false;
480       base.y -= 32;
481       base.height = 64;
482       // changing base size confuses collision otherwise
483       old_base = previous_base = base;                        
484     }
485 }
486
487 void
488 Player::grow()
489 {
490   if(size == BIG)
491     return;
492   
493   size = BIG;
494   base.height = 64;
495   base.y -= 32;
496
497   old_base = previous_base = base;
498 }
499
500 void
501 Player::grabdistros()
502 {
503   /* Grab distros: */
504   if (!dying)
505     {
506       World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
507       World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
508
509       World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
510       World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
511
512       if(size == BIG)
513         {
514           World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
515           World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
516         }
517
518     }
519
520   /* Enough distros for a One-up? */
521   if (player_status.distros >= DISTROS_LIFEUP)
522     {
523       player_status.distros = player_status.distros - DISTROS_LIFEUP;
524       if(player_status.lives < MAX_LIVES)
525         ++player_status.lives;
526       /*We want to hear the sound even, if MAX_LIVES is reached*/
527       play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
528     }
529 }
530
531 void
532 Player::draw()
533 {
534   if (!safe_timer.started() || (global_frame_counter % 2) == 0)
535     {
536       if (dying == DYING_SQUISHED)
537         {
538           smalltux_gameover->draw(base.x - scroll_x, base.y);
539         }
540       else
541         {
542           PlayerSprite* sprite;
543           
544           if (size == SMALL)
545             sprite = &smalltux;
546           else if (got_coffee)
547             sprite = &firetux;
548           else
549             sprite = &largetux;
550           
551           if (duck && size != SMALL)
552             {
553               if (dir == RIGHT)
554                 sprite->duck_right->draw(base.x - scroll_x, base.y);
555               else 
556                 sprite->duck_left->draw(base.x - scroll_x, base.y);
557             }
558           else if (skidding_timer.started())
559             {
560               if (dir == RIGHT)
561                 sprite->skid_right->draw(base.x - scroll_x, base.y);
562               else
563                 sprite->skid_left->draw(base.x - scroll_x, base.y); 
564             }
565           else if (kick_timer.started())
566             {
567               if (dir == RIGHT)
568                 sprite->kick_right->draw(base.x - scroll_x, base.y);
569               else
570                 sprite->kick_left->draw(base.x - scroll_x, base.y); 
571             }
572           else if (physic.get_velocity_y() != 0)
573             {
574               if (dir == RIGHT)
575                 sprite->jump_right->draw(base.x - scroll_x, base.y);
576               else
577                 sprite->jump_left->draw(base.x - scroll_x, base.y);                   
578             }
579           else
580             {
581               if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
582                 {
583                   if (dir == RIGHT)
584                     sprite->stand_right->draw( base.x - scroll_x, base.y);
585                   else
586                     sprite->stand_left->draw( base.x - scroll_x, base.y);
587                 }
588               else // moving
589                 {
590                   if (dir == RIGHT)
591                     sprite->walk_right->draw(base.x - scroll_x, base.y);
592                   else
593                     sprite->walk_left->draw(base.x - scroll_x, base.y);
594                 }
595             }
596                       
597           // Draw arm overlay graphics when Tux is holding something
598           if (holding_something && physic.get_velocity_y() == 0)
599             {
600               if (dir == RIGHT)
601                 sprite->grab_right->draw(base.x - scroll_x, base.y);
602               else
603                 sprite->grab_left->draw(base.x - scroll_x, base.y);
604             }
605
606           // Draw blinking star overlay
607           if (invincible_timer.started() &&
608              (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
609             {
610               if (size == SMALL || duck)
611                 smalltux_star->draw(base.x - scroll_x, base.y);
612               else
613                 largetux_star->draw(base.x - scroll_x, base.y);
614             }
615         }
616     }     
617   
618   if (debug_mode)
619     fillrect(base.x - scroll_x, base.y, 
620              base.width, base.height, 75,75,75, 150);
621 }
622
623 void
624 Player::collision(void* p_c_object, int c_object)
625 {
626   BadGuy* pbad_c = NULL;
627
628   switch (c_object)
629     {
630     case CO_BADGUY:
631       pbad_c = (BadGuy*) p_c_object;
632
633      /* Hurt player if he touches a badguy */
634       if (!pbad_c->dying && !dying &&
635           !safe_timer.started() &&
636           pbad_c->mode != BadGuy::HELD)
637         {
638           if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
639                && !holding_something)
640             {
641               holding_something = true;
642               pbad_c->mode = BadGuy::HELD;
643               pbad_c->base.y-=8;
644             }
645           else if (pbad_c->mode == BadGuy::FLAT)
646             {
647               // Don't get hurt if we're kicking a flat badguy!
648             }
649           else if (pbad_c->mode == BadGuy::KICK)
650             {
651               /* Hurt if you get hit by kicked laptop: */
652               if (!invincible_timer.started())
653                 {
654                   kill(SHRINK);
655                 }
656               else
657                 {
658                    pbad_c->dying = DYING_FALLING;
659                    play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
660                    World::current()->add_score(pbad_c->base.x - scroll_x,
661                                                pbad_c->base.y,
662                                                25 * player_status.score_multiplier);
663                 }
664             }
665           else
666             {
667               if (!invincible_timer.started())
668                 {
669                   kill(SHRINK);
670                 }
671               else
672                 {
673                   pbad_c->kill_me(25);
674                 }
675             }
676           player_status.score_multiplier++;
677         }
678       break;
679     default:
680       break;
681     }
682
683 }
684
685 /* Kill Player! */
686
687 void
688 Player::kill(HurtMode mode)
689 {
690   play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
691
692   physic.set_velocity_x(0);
693
694   if (mode == SHRINK && size == BIG)
695     {
696       if (got_coffee)
697         {
698           got_coffee = false;
699         }
700       else
701         {
702           size = SMALL;
703           base.height = 32;
704           duck = false;
705         }
706       safe_timer.start(TUX_SAFE_TIME);
707     }
708   else
709     {
710       physic.enable_gravity(true);
711       physic.set_acceleration(0, 0);
712       physic.set_velocity(0, 7);
713       dying = DYING_SQUISHED;
714     }
715 }
716
717 void
718 Player::is_dying()
719 {
720   remove_powerups();
721   dying = DYING_NOT;
722 }
723
724 bool Player::is_dead()
725 {
726   if(base.y > screen->h || base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL)  // last condition can happen in auto-scrolling
727     return true;
728   else
729     return false;
730 }
731
732 /* Remove Tux's power ups */
733 void
734 Player::remove_powerups()
735 {
736   got_coffee = false;
737   size = SMALL;
738   base.height = 32;
739 }
740
741 void
742 Player::check_bounds()
743 {
744   /* Keep tux in bounds: */
745   if (base.x < 0)
746     { // Lock Tux to the size of the level, so that he doesn't fall of
747       // on the left side
748       base.x = 0;
749     }
750
751   /* Keep in-bounds, vertically: */
752   if (base.y > screen->h)
753     {
754       kill(KILL);
755     }
756
757   if(base.x < scroll_x)  // can happen if back scrolling is disabled
758     base.x = scroll_x;
759
760   if(base.x == scroll_x)
761     if(issolid(base.x, base.y) || (size != SMALL && issolid(base.x, base.y+32)))
762       kill(KILL);
763
764   if(base.x + base.width > scroll_x + screen->w)
765     base.x = scroll_x + screen->w - base.width;
766
767     
768 }
769
770 // EOF //
771