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