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