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