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