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