-Started to move stuff from library back to main game
[supertux.git] / src / object / 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 #include <config.h>
20
21 #include <typeinfo>
22 #include <cmath>
23 #include <iostream>
24 #include <cassert>
25
26 #include "app/globals.h"
27 #include "app/gettext.h"
28 #include "sprite/sprite_manager.h"
29 #include "player.h"
30 #include "tile.h"
31 #include "sprite/sprite.h"
32 #include "sector.h"
33 #include "resources.h"
34 #include "video/screen.h"
35 #include "statistics.h"
36 #include "game_session.h"
37 #include "object/tilemap.h"
38 #include "object/camera.h"
39 #include "object/gameobjs.h"
40 #include "object/portable.h"
41 #include "trigger/trigger_base.h"
42 #include "control/joystickkeyboardcontroller.h"
43 #include "main.h"
44
45 static const int TILES_FOR_BUTTJUMP = 3;
46 static const float SHOOTING_TIME = .150;
47 /// time before idle animation starts
48 static const float IDLE_TIME = 2.5;
49
50 static const float WALK_ACCELERATION_X = 300;
51 static const float RUN_ACCELERATION_X = 400;
52 static const float SKID_XM = 200;
53 static const float SKID_TIME = .3;
54 static const float MAX_WALK_XM = 230;
55 static const float MAX_RUN_XM = 320;
56 static const float WALK_SPEED = 100;
57
58 // growing animation
59 Surface* growingtux_left[GROWING_FRAMES];
60 Surface* growingtux_right[GROWING_FRAMES];
61
62 Surface* tux_life = 0;
63
64 TuxBodyParts* small_tux = 0;
65 TuxBodyParts* big_tux = 0;
66 TuxBodyParts* fire_tux = 0;
67 TuxBodyParts* ice_tux = 0;
68
69 void
70 TuxBodyParts::set_action(std::string action, int loops)
71 {
72   if(head != NULL)
73     head->set_action(action, loops);
74   if(body != NULL)
75     body->set_action(action, loops);
76   if(arms != NULL)
77     arms->set_action(action, loops);
78   if(feet != NULL)
79     feet->set_action(action, loops);
80 }
81
82 void
83 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer,
84                   Uint32 drawing_effect)
85 {
86   if(head != NULL)
87     head->draw(context, pos, layer-1, drawing_effect);
88   if(body != NULL)
89     body->draw(context, pos, layer-3, drawing_effect);
90   if(arms != NULL)
91     arms->draw(context, pos, layer,   drawing_effect);
92   if(feet != NULL)
93     feet->draw(context, pos, layer-2, drawing_effect);
94 }
95
96 Player::Player(PlayerStatus* _player_status)
97   : player_status(_player_status), grabbed_object(0)
98 {
99   controller = main_controller;
100   smalltux_gameover = sprite_manager->create("smalltux-gameover");
101   smalltux_star = sprite_manager->create("smalltux-star");
102   bigtux_star = sprite_manager->create("bigtux-star");
103   init();
104 }
105
106 Player::~Player()
107 {
108   delete smalltux_gameover;
109   delete smalltux_star;
110   delete bigtux_star;
111 }
112
113 void
114 Player::init()
115 {
116   if(is_big())
117     bbox.set_size(31.8, 63.8);
118   else
119     bbox.set_size(31.8, 31.8);
120
121   dir = RIGHT;
122   old_dir = dir;
123   duck = false;
124   dead = false;
125
126   dying = false;
127   last_ground_y = 0;
128   fall_mode = ON_GROUND;
129   jumping = false;
130   flapping = false;
131   can_jump = true;
132   can_flap = false;
133   falling_from_flap = false;
134   enable_hover = false;
135   butt_jump = false;
136   
137   flapping_velocity = 0;
138
139   // temporary to help player's choosing a flapping
140   flapping_mode = NO_FLAP;
141
142   // Ricardo's flapping
143   flaps_nb = 0;
144
145   on_ground_flag = false;
146   grabbed_object = 0;
147
148   physic.reset();
149 }
150
151 void
152 Player::set_controller(Controller* controller)
153 {
154   this->controller = controller;
155 }
156
157 void
158 Player::action(float elapsed_time)
159 {
160   if(dying && dying_timer.check()) {
161     dead = true;
162     return;
163   }
164
165   if(!controller->hold(Controller::ACTION) && grabbed_object) {
166     grabbed_object = 0;
167     // move the grabbed object a bit away from tux
168     Vector pos = get_pos() + 
169         Vector(dir == LEFT ? -bbox.get_width() : bbox.get_width(),
170                 bbox.get_height()*0.66666 - 32);
171     MovingObject* object = dynamic_cast<MovingObject*> (grabbed_object);
172     if(object) {
173       object->set_pos(pos);
174     } else {
175 #ifdef DEBUG
176       std::cout << "Non MovingObjetc grabbed?!?\n";
177 #endif
178     }
179   }
180
181   if(!dying)
182     handle_input();
183
184   movement = physic.get_movement(elapsed_time);
185   on_ground_flag = false;
186
187 #if 0
188   // special exception for cases where we're stuck under tiles after
189   // being ducked. In this case we drift out
190   if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
191      && collision_object_map(base)) {
192     base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
193     previous_base = old_base = base;
194   }
195 #endif
196
197   if(grabbed_object != 0) {
198     Vector pos = get_pos() + 
199       Vector(dir == LEFT ? -16 : 16,
200              bbox.get_height()*0.66666 - 32);
201     grabbed_object->grab(*this, pos);
202   }
203 }
204
205 bool
206 Player::on_ground()
207 {
208   return on_ground_flag;
209 }
210
211 bool
212 Player::is_big()
213 {
214   if(player_status->bonus == NO_BONUS)
215     return false;
216
217   return true;
218 }
219
220 void
221 Player::handle_horizontal_input()
222 {
223   float vx = physic.get_velocity_x();
224   float vy = physic.get_velocity_y();
225   float ax = physic.get_acceleration_x();
226   float ay = physic.get_acceleration_y();
227
228   float dirsign = 0;
229   if(!duck || physic.get_velocity_y() != 0) {
230     if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
231       old_dir = dir;
232       dir = LEFT;
233       dirsign = -1;
234     } else if(!controller->hold(Controller::LEFT)
235               && controller->hold(Controller::RIGHT)) {
236       old_dir = dir;
237       dir = RIGHT;
238       dirsign = 1;
239     }
240   }
241
242   if (!controller->hold(Controller::ACTION)) {
243     ax = dirsign * WALK_ACCELERATION_X;
244     // limit speed
245     if(vx >= MAX_WALK_XM && dirsign > 0) {
246       vx = MAX_WALK_XM;
247       ax = 0;
248     } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
249       vx = -MAX_WALK_XM;
250       ax = 0;
251     }
252   } else {
253     ax = dirsign * RUN_ACCELERATION_X;
254     // limit speed
255     if(vx >= MAX_RUN_XM && dirsign > 0) {
256       vx = MAX_RUN_XM;
257       ax = 0;
258     } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
259       vx = -MAX_RUN_XM;
260       ax = 0;
261     }
262   }
263
264   // we can reach WALK_SPEED without any acceleration
265   if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
266     vx = dirsign * WALK_SPEED;
267   }
268
269   // changing directions?
270   if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
271     // let's skid!
272     if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
273       skidding_timer.start(SKID_TIME);
274       sound_manager->play_sound("skid");
275       // dust some partcles
276       Sector::current()->add_object(
277         new Particles(
278           Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
279                  bbox.p2.y),
280           dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
281           Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
282           LAYER_OBJECTS+1));
283       
284       ax *= 2.5;
285     } else {
286       ax *= 2;
287     }
288   }
289
290   // we get slower when not pressing any keys
291   if(dirsign == 0) {
292     if(fabs(vx) < WALK_SPEED) {
293       vx = 0;
294       ax = 0;
295     } else if(vx < 0) {
296       ax = WALK_ACCELERATION_X * 1.5;
297     } else {
298       ax = WALK_ACCELERATION_X * -1.5;
299     }
300   }
301
302 #if 0
303   // if we're on ice slow down acceleration or deceleration
304   if (isice(base.x, base.y + base.height))
305   {
306     /* the acceleration/deceleration rate on ice is inversely proportional to
307      * the current velocity.
308      */
309
310     // increasing 1 will increase acceleration/deceleration rate
311     // decreasing 1 will decrease acceleration/deceleration rate
312     //  must stay above zero, though
313     if (ax != 0) ax *= 1 / fabs(vx);
314   }
315 #endif
316
317   // extend/shrink tux collision rectangle so that we fall through/walk over 1
318   // tile holes
319   if(fabsf(vx) > MAX_WALK_XM) {
320     bbox.set_width(33);
321   } else {
322     bbox.set_width(31.8);
323   }
324
325   physic.set_velocity(vx, vy);
326   physic.set_acceleration(ax, ay);
327 }
328
329 void
330 Player::handle_vertical_input()
331 {
332   // set fall mode...
333   if(on_ground()) {
334     fall_mode = ON_GROUND;
335     last_ground_y = get_pos().y;
336   } else {
337     if(get_pos().y > last_ground_y)
338       fall_mode = FALLING;
339     else if(fall_mode == ON_GROUND)
340       fall_mode = JUMPING;
341   }
342
343   if(on_ground()) { /* Make sure jumping is off. */
344     jumping = false;
345     flapping = false;
346     falling_from_flap = false;
347     if (flapping_timer.started()) {
348       flapping_timer.start(0);
349     }
350
351     physic.set_acceleration_y(0); //for flapping
352   }
353
354   // Press jump key
355   if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
356     if(duck) { // only jump a little bit when in duck mode {
357       physic.set_velocity_y(300);
358     } else {
359       // jump higher if we are running
360       if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
361         physic.set_velocity_y(580);
362       else
363         physic.set_velocity_y(520);
364     }
365     
366     //bbox.move(Vector(0, -1));
367     jumping = true;
368     flapping = false;
369     can_jump = false;
370     can_flap = false;
371     flaps_nb = 0; // Ricardo's flapping
372     if (is_big())
373       sound_manager->play_sound("bigjump");
374     else
375       sound_manager->play_sound("jump");
376   } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
377     if (!flapping && !duck && !falling_from_flap && !on_ground()) {
378       can_flap = true;
379     }
380     if (jumping && physic.get_velocity_y() > 0) {
381       jumping = false;
382       physic.set_velocity_y(0);
383     }
384   }
385
386   // temporary to help player's choosing a flapping
387   if(flapping_mode == RICARDO_FLAP) {
388     // Flapping, Ricardo's version
389     // similar to SM3 Fox
390     if(controller->pressed(Controller::JUMP) && can_flap && flaps_nb < 3) {
391       physic.set_velocity_y(350);
392       physic.set_velocity_x(physic.get_velocity_x() * 35);
393       flaps_nb++;
394     }
395   } else if(flapping_mode == MAREK_FLAP) {
396     // Flapping, Marek's version
397     if (controller->hold(Controller::JUMP) && can_flap)
398     {
399       if (!flapping_timer.started())
400       {
401         flapping_timer.start(TUX_FLAPPING_TIME);
402         flapping_velocity = physic.get_velocity_x();
403       }
404       if (flapping_timer.check()) 
405       {
406         can_flap = false;
407         falling_from_flap = true;
408       }
409       jumping = true;
410       flapping = true;
411       if (!flapping_timer.check()) {
412         float cv = flapping_velocity * sqrt(
413           TUX_FLAPPING_TIME - flapping_timer.get_timegone() 
414           / TUX_FLAPPING_TIME);
415         
416         //Handle change of direction while flapping
417         if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
418           cv *= (-1);
419         }
420         physic.set_velocity_x(cv);
421         physic.set_velocity_y(
422           flapping_timer.get_timegone()/.850);
423       }
424     }
425   } else if(flapping_mode == RYAN_FLAP) {
426     // Flapping, Ryan's version
427     if (controller->hold(Controller::JUMP) && can_flap)
428     {
429       if (!flapping_timer.started())
430       {
431         flapping_timer.start(TUX_FLAPPING_TIME);
432       }
433       if (flapping_timer.check()) 
434       {
435         can_flap = false;
436         falling_from_flap = true;
437       }
438       jumping = true;
439       flapping = true;
440       if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
441           && physic.get_velocity_y() < 0)
442       {
443         float gravity = Sector::current()->gravity;
444         (void)gravity;
445         float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
446         
447         // XXX: magic numbers. should be a percent of gravity
448         //      gravity is (by default) -0.1f
449         physic.set_acceleration_y(12 + 1*xr);
450         
451 #if 0
452         // To slow down x-vel when flapping (not working)
453         if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
454         {
455           if (physic.get_velocity_x() < 0)
456             physic.set_acceleration_x(1.0f);
457           else if (physic.get_velocity_x() > 0)
458             physic.set_acceleration_x(-1.0f);
459         }
460 #endif
461       }
462     } else {
463       physic.set_acceleration_y(0);
464     }
465   }
466
467   /* In case the player has pressed Down while in a certain range of air,
468      enable butt jump action */
469   if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
470     //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
471     butt_jump = true;
472   
473   /* When Down is not held anymore, disable butt jump */
474   if(butt_jump && !controller->hold(Controller::DOWN))
475     butt_jump = false;
476   
477 #if 0
478   // Do butt jump
479   if (butt_jump && on_ground() && is_big()) {
480     // Add a smoke cloud
481     if (duck) 
482       Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
483     else 
484       Sector::current()->add_smoke_cloud(
485         Vector(get_pos().x - 32, get_pos().y + 32));
486     
487     butt_jump = false;
488     
489     // Break bricks beneath Tux
490     if(Sector::current()->trybreakbrick(
491          Vector(base.x + 1, base.y + base.height), false)
492        || Sector::current()->trybreakbrick(
493          Vector(base.x + base.width - 1, base.y + base.height), false)) {
494       physic.set_velocity_y(2);
495       butt_jump = true;
496     }
497     
498     // Kill nearby badguys
499     std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
500     for (std::vector<GameObject*>::iterator i = gameobjects.begin();
501          i != gameobjects.end();
502          i++) {
503       BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
504       if(badguy) {
505         // don't kill when badguys are already dying or in a certain mode
506         if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
507            badguy->mode != BadGuy::BOMB_EXPLODE) {
508           if (fabsf(base.x - badguy->base.x) < 96 &&
509               fabsf(base.y - badguy->base.y) < 64)
510             badguy->kill_me(25);
511         }
512       }
513     }
514   }
515 #endif
516
517   /** jumping is only allowed if we're about to touch ground soon and if the
518    * button has been up in between the last jump
519    */
520   // FIXME
521 #if 0
522   if ( (issolid(get_pos().x + bbox.get_width() / 2,
523           get_pos().y + bbox.get_height() + 64) ||
524         issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
525         issolid(get_pos().x + bbox.get_width() - 1,
526           get_pos().y + bbox.get_height() + 64))
527        && jumping  == false
528        && can_jump == false
529        && input.jump && !input.old_jump)
530     {
531       can_jump = true;
532     }
533 #endif
534 }
535
536 void
537 Player::handle_input()
538 {
539   /* Handle horizontal movement: */
540   handle_horizontal_input();
541
542   /* Jump/jumping? */
543   if (on_ground() && !controller->hold(Controller::JUMP))
544     can_jump = true;
545   handle_vertical_input();
546
547   /* Shoot! */
548   if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
549     if(Sector::current()->add_bullet(
550          get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2) 
551                       : Vector(32, bbox.get_height()/2)),
552          physic.get_velocity_x(), dir))
553       shooting_timer.start(SHOOTING_TIME);
554   }
555   
556   /* Duck! */
557   if (controller->hold(Controller::DOWN) && is_big() && !duck 
558       && physic.get_velocity_y() == 0 && on_ground()) {
559     duck = true;
560     bbox.move(Vector(0, 32));
561     bbox.set_height(31.8);
562   } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
563     // try if we can really unduck
564     bbox.move(Vector(0, -32));
565     bbox.set_height(63.8);
566     duck = false;
567     // FIXME
568 #if 0
569     // when unducking in air we need some space to do so
570     if(on_ground() || !collision_object_map(bbox)) {
571       duck = false;
572     } else {
573       // undo the ducking changes
574       bbox.move(Vector(0, 32));
575       bbox.set_height(31.8);
576     }
577 #endif
578   }
579 }
580
581 void
582 Player::set_bonus(BonusType type, bool animate)
583 {
584   if(player_status->bonus == type)
585     return;
586   
587   if(player_status->bonus == NO_BONUS) {
588     bbox.set_height(63.8);
589     bbox.move(Vector(0, -32));
590     if(animate)
591       growing_timer.start(GROWING_TIME);
592   }
593   
594   player_status->bonus = type;
595 }
596
597 void
598 Player::draw(DrawingContext& context)
599 {
600   TuxBodyParts* tux_body;
601           
602   if (player_status->bonus == GROWUP_BONUS)
603     tux_body = big_tux;
604   else if (player_status->bonus == FIRE_BONUS)
605     tux_body = fire_tux;
606   else if (player_status->bonus == ICE_BONUS)
607     tux_body = ice_tux;
608   else
609     tux_body = small_tux;
610
611   int layer = LAYER_OBJECTS + 10;
612
613   /* Set Tux sprite action */
614   if (duck && is_big())
615     {
616     if(dir == LEFT)
617       tux_body->set_action("duck-left");
618     else // dir == RIGHT
619       tux_body->set_action("duck-right");
620     }
621   else if (skidding_timer.started() && !skidding_timer.check())
622     {
623     if(dir == LEFT)
624       tux_body->set_action("skid-left");
625     else // dir == RIGHT
626       tux_body->set_action("skid-right");
627     }
628   else if (kick_timer.started() && !kick_timer.check())
629     {
630     if(dir == LEFT)
631       tux_body->set_action("kick-left");
632     else // dir == RIGHT
633       tux_body->set_action("kick-right");
634     }
635   else if (butt_jump && is_big())
636     {
637     if(dir == LEFT)
638       tux_body->set_action("buttjump-left");
639     else // dir == RIGHT
640       tux_body->set_action("buttjump-right");
641     }
642   else if (physic.get_velocity_y() != 0)
643     {
644     if(dir == LEFT)
645       tux_body->set_action("jump-left");
646     else // dir == RIGHT
647       tux_body->set_action("jump-right");
648     }
649   else
650     {
651     if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
652       {
653       if(dir == LEFT)
654         tux_body->set_action("stand-left");
655       else // dir == RIGHT
656         tux_body->set_action("stand-right");
657       }
658     else // moving
659       {
660       if(dir == LEFT)
661         tux_body->set_action("walk-left");
662       else // dir == RIGHT
663         tux_body->set_action("walk-right");
664       }
665     }
666
667   if(idle_timer.check())
668     {
669     if(is_big())
670       {
671       if(dir == LEFT)
672         tux_body->head->set_action("idle-left", 1);
673       else // dir == RIGHT
674         tux_body->head->set_action("idle-right", 1);
675       }
676
677     }
678
679   // Tux is holding something
680   if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
681       (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
682     {
683     if (duck)
684       {
685       if(dir == LEFT)
686         tux_body->arms->set_action("duck+grab-left");
687       else // dir == RIGHT
688         tux_body->arms->set_action("duck+grab-right");
689       }
690     else
691       {
692       if(dir == LEFT)
693         tux_body->arms->set_action("grab-left");
694       else // dir == RIGHT
695         tux_body->arms->set_action("grab-right");
696       }
697     }
698
699   /* Draw Tux */
700   if(dying) {
701     smalltux_gameover->draw(context, get_pos(), layer);
702   } else if(growing_timer.get_timeleft() > 0) {
703     if(!is_big())
704       {
705       if (dir == RIGHT)
706         context.draw_surface(growingtux_right[GROWING_FRAMES-1 - 
707                  int((growing_timer.get_timegone() *
708                  GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
709       else
710         context.draw_surface(growingtux_left[GROWING_FRAMES-1 - 
711                 int((growing_timer.get_timegone() *
712                 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
713       }
714     else
715       {
716       if (dir == RIGHT)
717         context.draw_surface(growingtux_right[
718             int((growing_timer.get_timegone() *
719                 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
720       else
721         context.draw_surface(growingtux_left[
722             int((growing_timer.get_timegone() *
723                              GROWING_FRAMES) / GROWING_TIME)],
724             get_pos(), layer);
725       }
726     }
727   else if (safe_timer.started() && size_t(global_time*40)%2)
728     ;  // don't draw Tux
729   else
730     tux_body->draw(context, get_pos(), layer);
731
732   // Draw blinking star overlay
733   if (invincible_timer.started() &&
734      (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
735       || size_t(global_time*20)%2)
736      && !dying)
737   {
738     if (!is_big() || duck)
739       smalltux_star->draw(context, get_pos(), layer + 5);
740     else
741       bigtux_star->draw(context, get_pos(), layer + 5);
742   } 
743 }
744
745 HitResponse
746 Player::collision(GameObject& other, const CollisionHit& hit)
747 {
748   Portable* portable = dynamic_cast<Portable*> (&other);
749   if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
750      && fabsf(hit.normal.x) > .9) {
751     grabbed_object = portable;
752     return CONTINUE;
753   }
754  
755   if(other.get_flags() & FLAG_SOLID) {
756     if(hit.normal.y < 0) { // landed on floor?
757       if (physic.get_velocity_y() < 0)
758         physic.set_velocity_y(0);
759       on_ground_flag = true;
760     } else if(hit.normal.y > 0) { // bumped against the roof
761       physic.set_velocity_y(.1);
762     }
763     
764     if(fabsf(hit.normal.x) > .9) { // hit on the side?
765       physic.set_velocity_x(0);
766     }
767
768     return CONTINUE;
769   }
770
771   TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
772   if(trigger) {
773     if(controller->pressed(Controller::UP))
774       trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
775   }
776
777   return FORCE_MOVE;
778 }
779
780 void
781 Player::make_invincible()
782 {
783   sound_manager->play_sound("invincible");
784   invincible_timer.start(TUX_INVINCIBLE_TIME);
785   Sector::current()->play_music(HERRING_MUSIC);               
786 }
787
788 /* Kill Player! */
789 void
790 Player::kill(HurtMode mode)
791 {
792   if(dying)
793     return;
794
795   if(mode != KILL && 
796           safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
797     return;                          
798   
799   sound_manager->play_sound("hurt");
800
801   physic.set_velocity_x(0);
802
803   if (mode == SHRINK && is_big())
804     {
805       if (player_status->bonus == FIRE_BONUS
806           || player_status->bonus == ICE_BONUS)
807         {
808           safe_timer.start(TUX_SAFE_TIME);
809           player_status->bonus = GROWUP_BONUS;
810         }
811       else 
812         {
813           growing_timer.start(GROWING_TIME);
814           safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
815           bbox.set_height(31.8);
816           duck = false;
817           player_status->bonus = NO_BONUS;
818         }
819     }
820   else
821     {
822       physic.enable_gravity(true);
823       physic.set_acceleration(0, 0);
824       physic.set_velocity(0, 700);
825       player_status->lives -= 1;
826       player_status->bonus = NO_BONUS;
827       dying = true;
828       dying_timer.start(3.0);
829       flags |= FLAG_NO_COLLDET;
830     }
831 }
832
833 void
834 Player::move(const Vector& vector)
835 {
836   bbox.set_pos(vector);
837   if(is_big())
838     bbox.set_size(31.8, 63.8);
839   else
840     bbox.set_size(31.8, 31.8);
841   on_ground_flag = false;
842   duck = false;
843   last_ground_y = vector.y;
844
845   physic.reset();
846 }
847
848 void
849 Player::check_bounds(Camera* camera)
850 {
851   /* Keep tux in bounds: */
852   if (get_pos().x < 0)
853     { // Lock Tux to the size of the level, so that he doesn't fall of
854       // on the left side
855       bbox.set_pos(Vector(0, get_pos().y));
856     }
857
858   /* Keep in-bounds, vertically: */
859   if (get_pos().y > Sector::current()->solids->get_height() * 32)
860     {
861       kill(KILL);
862       return;
863     }
864
865   bool adjust = false;
866   // can happen if back scrolling is disabled
867   if(get_pos().x < camera->get_translation().x) {
868     bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
869     adjust = true;
870   }
871   if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
872   {
873     bbox.set_pos(Vector(
874           camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
875           get_pos().y));
876     adjust = true;
877   }
878
879   if(adjust) {
880     // FIXME
881 #if 0
882     // squished now?
883     if(collision_object_map(bbox)) {
884       kill(KILL);
885       return;
886     }
887 #endif
888   }
889 }
890
891 void
892 Player::bounce(BadGuy& )
893 {
894   //Make sure we stopped flapping
895   flapping = false;
896   falling_from_flap = false;
897   
898   if(controller->hold(Controller::JUMP))
899     physic.set_velocity_y(520);
900   else
901     physic.set_velocity_y(200);
902 }
903