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