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