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