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