Fixes death in modes different than the autoscrolling.
[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
198       // Land:
199       if (!on_ground())
200         {
201           physic.enable_gravity(true);
202           if(under_solid())
203             {
204               // fall down
205               physic.set_velocity_y(0);
206               jumped_in_solid = true;
207             }
208         }
209       else
210         {
211           /* Land: */
212           if (physic.get_velocity_y() < 0)
213             {
214               base.y = (int)(((int)base.y / 32) * 32);
215               physic.set_velocity_y(0);
216             }
217
218           physic.enable_gravity(false);
219           /* Reset score multiplier (for multi-hits): */
220           player_status.score_multiplier = 1;
221         }
222
223       if(jumped_in_solid)
224         {
225           if (isbrick(base.x, base.y) ||
226               isfullbox(base.x, base.y))
227             {
228               World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
229               World::current()->trybumpbadguy(base.x, base.y - 64);
230
231               World::current()->trybreakbrick(base.x, base.y, size == SMALL);
232
233               bumpbrick(base.x, base.y);
234               World::current()->tryemptybox(base.x, base.y, RIGHT);
235             }
236
237           if (isbrick(base.x+ 31, base.y) ||
238               isfullbox(base.x+ 31, base.y))
239             {
240               World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
241               World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
242
243               if(size == BIG)
244                 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
245
246               bumpbrick(base.x+ 31, base.y);
247               World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
248             }
249         }
250
251       grabdistros();
252
253       if (jumped_in_solid)
254         {
255           ++base.y;
256           ++old_base.y;
257           if(on_ground())
258             {
259               /* Make sure jumping is off. */
260               jumping = false;
261             }
262         }
263     }
264
265   /* ---- DONE HANDLING TUX! --- */
266
267   // check some timers
268   skidding_timer.check();
269   invincible_timer.check();
270   safe_timer.check();
271   kick_timer.check();
272 }
273
274 bool
275 Player::on_ground()
276 {
277   return ( issolid(base.x + base.width / 2, base.y + base.height) ||
278            issolid(base.x + 1, base.y + base.height) ||
279            issolid(base.x + base.width - 1, base.y + base.height)  );
280 }
281
282 bool
283 Player::under_solid()
284 {
285   return ( issolid(base.x + base.width / 2, base.y) ||
286            issolid(base.x + 1, base.y) ||
287            issolid(base.x + base.width - 1, base.y)  );
288 }
289
290 void
291 Player::handle_horizontal_input()
292 {
293   float vx = physic.get_velocity_x();
294   float vy = physic.get_velocity_y();
295   float ax = physic.get_acceleration_x();
296   float ay = physic.get_acceleration_y();
297
298   float dirsign = 0;
299   if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
300       old_dir = dir;
301       dir = LEFT;
302       dirsign = -1;
303   } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
304       old_dir = dir;
305       dir = RIGHT;
306       dirsign = 1;
307   }
308
309   if (input.fire == UP) {
310       ax = dirsign * WALK_ACCELERATION_X;
311       // limit speed
312       if(vx >= MAX_WALK_XM && dirsign > 0) {
313         vx = MAX_WALK_XM;
314         ax = 0;
315       } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
316         vx = -MAX_WALK_XM;
317         ax = 0;
318       }
319   } else {
320       ax = dirsign * RUN_ACCELERATION_X;
321       // limit speed
322       if(vx >= MAX_RUN_XM && dirsign > 0) {
323         vx = MAX_RUN_XM;
324         ax = 0;
325       } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
326         vx = -MAX_RUN_XM;
327         ax = 0;
328       }
329   }
330
331   // we can reach WALK_SPEED without any acceleration
332   if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
333     vx = dirsign * WALK_SPEED;
334   }
335
336   // changing directions?
337   if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
338       if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
339           skidding_timer.start(SKID_TIME);
340           play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
341           ax *= 2.5;
342       } else {
343           ax *= 2;
344       }
345   }
346
347   // we get slower when not pressing any keys
348   if(dirsign == 0) {
349       if(fabs(vx) < WALK_SPEED) {
350           vx = 0;
351           ax = 0;
352       } else if(vx < 0) {
353           ax = WALK_ACCELERATION_X * 1.5;
354       } else {
355           ax = WALK_ACCELERATION_X * -1.5;
356       }
357   }
358
359   // if we're on ice slow down acceleration or deceleration
360   if (isice(base.x, base.y + base.height))
361   {
362     /* the acceleration/deceleration rate on ice is inversely proportional to
363      * the current velocity.
364      */
365
366     // increasing 1 will increase acceleration/deceleration rate
367     // decreasing 1 will decrease acceleration/deceleration rate
368     //  must stay above zero, though
369     if (ax != 0) ax *= 1 / fabs(vx);
370   }
371
372   physic.set_velocity(vx, vy);
373   physic.set_acceleration(ax, ay);
374 }
375
376 void
377 Player::handle_vertical_input()
378 {
379   // Press jump key
380   if(input.up == DOWN && can_jump)
381     {
382       if (on_ground())
383         {
384           // jump higher if we are running
385           if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
386             physic.set_velocity_y(5.8);
387           else
388             physic.set_velocity_y(5.2);
389
390           --base.y;
391           jumping = true;
392           can_jump = false;
393           if (size == SMALL)
394             play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
395           else
396             play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
397         }
398     }
399   // Let go of jump key
400   else if(input.up == UP && jumping)
401     {
402       jumping = false;
403       if(physic.get_velocity_y() > 0) {
404         physic.set_velocity_y(0);
405       }
406     }
407
408   if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
409         issolid(base.x + 1, base.y + base.height + 64) ||
410         issolid(base.x + base.width - 1, base.y + base.height + 64))
411        && jumping  == false
412        && can_jump == false
413        && input.up == DOWN
414        && input.old_up == UP)
415     {
416       can_jump = true;
417     }
418
419   input.old_up = input.up;
420 }
421
422 void
423 Player::handle_input()
424 {
425   /* Handle horizontal movement: */
426     handle_horizontal_input();
427
428   /* Jump/jumping? */
429
430   if (on_ground() && input.up == UP)
431     can_jump = true;
432   if (input.up == DOWN || (input.up == UP && jumping))
433     {
434       handle_vertical_input();
435     }
436
437   /* Shoot! */
438
439   if (input.fire == DOWN && input.old_fire == UP && got_coffee)
440     {
441       World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
442       input.old_fire = DOWN;
443     }
444
445   /* tux animations: */
446   if(!frame_timer.check())
447     {
448       frame_timer.start(25);
449       if (input.right == UP && input.left == UP)
450         {
451           frame_main = 1;
452           frame_ = 1;
453         }
454       else
455         {
456           if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
457               (global_frame_counter % 4) == 0)
458             frame_main = (frame_main + 1) % 4;
459
460           frame_ = frame_main;
461
462           if (frame_ == 3)
463             frame_ = 1;
464         }
465     }
466
467   /* Duck! */
468   if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
469     {
470       duck = true;
471       base.height = 32;                             
472       base.y += 32;
473       // changing base size confuses collision otherwise
474       old_base = previous_base = base;
475     }
476   else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
477     {
478       duck = false;
479       base.y -= 32;
480       base.height = 64;
481       // changing base size confuses collision otherwise
482       old_base = previous_base = base;                        
483     }
484 }
485
486 void
487 Player::grow()
488 {
489   if(size == BIG)
490     return;
491   
492   size = BIG;
493   base.height = 64;
494   base.y -= 32;
495
496   old_base = previous_base = base;
497 }
498
499 void
500 Player::grabdistros()
501 {
502   /* Grab distros: */
503   if (!dying)
504     {
505       World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
506       World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
507
508       World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
509       World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
510
511       if(size == BIG)
512         {
513           World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
514           World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
515         }
516
517     }
518
519   /* Enough distros for a One-up? */
520   if (player_status.distros >= DISTROS_LIFEUP)
521     {
522       player_status.distros = player_status.distros - DISTROS_LIFEUP;
523       if(player_status.lives < MAX_LIVES)
524         ++player_status.lives;
525       /*We want to hear the sound even, if MAX_LIVES is reached*/
526       play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
527     }
528 }
529
530 void
531 Player::draw()
532 {
533   if (!safe_timer.started() || (global_frame_counter % 2) == 0)
534     {
535       if (dying == DYING_SQUISHED)
536         {
537           smalltux_gameover->draw(base.x - scroll_x, base.y);
538         }
539       else
540         {
541           PlayerSprite* sprite;
542           
543           if (size == SMALL)
544             sprite = &smalltux;
545           else if (got_coffee)
546             sprite = &firetux;
547           else
548             sprite = &largetux;
549           
550           if (duck && size != SMALL)
551             {
552               if (dir == RIGHT)
553                 sprite->duck_right->draw(base.x - scroll_x, base.y);
554               else 
555                 sprite->duck_left->draw(base.x - scroll_x, base.y);
556             }
557           else if (skidding_timer.started())
558             {
559               if (dir == RIGHT)
560                 sprite->skid_right->draw(base.x - scroll_x, base.y);
561               else
562                 sprite->skid_left->draw(base.x - scroll_x, base.y); 
563             }
564           else if (kick_timer.started())
565             {
566               if (dir == RIGHT)
567                 sprite->kick_right->draw(base.x - scroll_x, base.y);
568               else
569                 sprite->kick_left->draw(base.x - scroll_x, base.y); 
570             }
571           else if (physic.get_velocity_y() != 0)
572             {
573               if (dir == RIGHT)
574                 sprite->jump_right->draw(base.x - scroll_x, base.y);
575               else
576                 sprite->jump_left->draw(base.x - scroll_x, base.y);                   
577             }
578           else
579             {
580               if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
581                 {
582                   if (dir == RIGHT)
583                     sprite->stand_right->draw( base.x - scroll_x, base.y);
584                   else
585                     sprite->stand_left->draw( base.x - scroll_x, base.y);
586                 }
587               else // moving
588                 {
589                   if (dir == RIGHT)
590                     sprite->walk_right->draw(base.x - scroll_x, base.y);
591                   else
592                     sprite->walk_left->draw(base.x - scroll_x, base.y);
593                 }
594             }
595                       
596           // Draw arm overlay graphics when Tux is holding something
597           if (holding_something && physic.get_velocity_y() == 0)
598             {
599               if (dir == RIGHT)
600                 sprite->grab_right->draw(base.x - scroll_x, base.y);
601               else
602                 sprite->grab_left->draw(base.x - scroll_x, base.y);
603             }
604
605           // Draw blinking star overlay
606           if (invincible_timer.started() &&
607              (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
608             {
609               if (size == SMALL || duck)
610                 smalltux_star->draw(base.x - scroll_x, base.y);
611               else
612                 largetux_star->draw(base.x - scroll_x, base.y);
613             }
614         }
615     }     
616   
617   if (debug_mode)
618     fillrect(base.x - scroll_x, base.y, 
619              base.width, base.height, 75,75,75, 150);
620 }
621
622 void
623 Player::collision(void* p_c_object, int c_object)
624 {
625   BadGuy* pbad_c = NULL;
626
627   switch (c_object)
628     {
629     case CO_BADGUY:
630       pbad_c = (BadGuy*) p_c_object;
631
632      /* Hurt player if he touches a badguy */
633       if (!pbad_c->dying && !dying &&
634           !safe_timer.started() &&
635           pbad_c->mode != BadGuy::HELD)
636         {
637           if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
638                && !holding_something)
639             {
640               holding_something = true;
641               pbad_c->mode = BadGuy::HELD;
642               pbad_c->base.y-=8;
643             }
644           else if (pbad_c->mode == BadGuy::FLAT)
645             {
646               // Don't get hurt if we're kicking a flat badguy!
647             }
648           else if (pbad_c->mode == BadGuy::KICK)
649             {
650               /* Hurt if you get hit by kicked laptop: */
651               if (!invincible_timer.started())
652                 {
653                   kill(SHRINK);
654                 }
655               else
656                 {
657                    pbad_c->dying = DYING_FALLING;
658                    play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
659                    World::current()->add_score(pbad_c->base.x - scroll_x,
660                                                pbad_c->base.y,
661                                                25 * player_status.score_multiplier);
662                 }
663             }
664           else
665             {
666               if (!invincible_timer.started())
667                 {
668                   kill(SHRINK);
669                 }
670               else
671                 {
672                   pbad_c->kill_me(25);
673                 }
674             }
675           player_status.score_multiplier++;
676         }
677       break;
678     default:
679       break;
680     }
681
682 }
683
684 /* Kill Player! */
685
686 void
687 Player::kill(HurtMode mode)
688 {
689   play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
690
691   physic.set_velocity_x(0);
692
693   if (mode == SHRINK && size == BIG)
694     {
695       if (got_coffee)
696         {
697           got_coffee = false;
698         }
699       else
700         {
701           size = SMALL;
702           base.height = 32;
703           duck = false;
704         }
705       safe_timer.start(TUX_SAFE_TIME);
706     }
707   else
708     {
709       physic.enable_gravity(true);
710       physic.set_acceleration(0, 0);
711       physic.set_velocity(0, 7);
712       dying = DYING_SQUISHED;
713     }
714 }
715
716 void
717 Player::is_dying()
718 {
719   remove_powerups();
720   dying = DYING_NOT;
721 }
722
723 bool Player::is_dead()
724 {
725   if(base.y > screen->h || base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL)  // last condition can happen in auto-scrolling
726     return true;
727   else
728     return false;
729 }
730
731 /* Remove Tux's power ups */
732 void
733 Player::remove_powerups()
734 {
735   got_coffee = false;
736   size = SMALL;
737   base.height = 32;
738 }
739
740 void
741 Player::check_bounds(bool back_scrolling, bool hor_autoscroll)
742 {
743   /* Keep tux in bounds: */
744   if (base.x < 0)
745     { // Lock Tux to the size of the level, so that he doesn't fall of
746       // on the left side
747       base.x = 0;
748     }
749
750   /* Keep in-bounds, vertically: */
751   if (base.y > screen->h)
752     {
753       kill(KILL);
754     }
755
756   if(base.x < scroll_x && (!back_scrolling || hor_autoscroll))  // can happen if back scrolling is disabled
757     base.x = scroll_x;
758
759   if(base.x == scroll_x && hor_autoscroll)
760     if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32)))
761       kill(KILL);
762
763   if(base.x + base.width > scroll_x + screen->w && hor_autoscroll)
764     base.x = scroll_x + screen->w - base.width;
765
766     
767 }
768
769 // EOF //
770