Moved some sprites from LAYER_OBJECTS to LAYER_FLOATINGOBJECTS
[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 #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 "statistics.hpp"
35 #include "game_session.hpp"
36 #include "object/tilemap.hpp"
37 #include "object/camera.hpp"
38 #include "object/particles.hpp"
39 #include "object/portable.hpp"
40 #include "object/bullet.hpp"
41 #include "trigger/trigger_base.hpp"
42 #include "control/joystickkeyboardcontroller.hpp"
43 #include "scripting/squirrel_util.hpp"
44 #include "main.hpp"
45 #include "platform.hpp"
46 #include "badguy/badguy.hpp"
47 #include "player_status.hpp"
48 #include "log.hpp"
49 #include "falling_coin.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+10);
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, 62.8);
126   else
127     bbox.set_size(31.8, 30.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         log_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::add_coins(int count)
538 {
539   player_status->add_coins(count);
540 }
541
542 void
543 Player::add_bonus(const std::string& bonustype)
544 {
545   if(bonustype == "grow")
546     add_bonus(GROWUP_BONUS);
547   else if(bonustype == "fireflower")
548     add_bonus(FIRE_BONUS);
549   else if(bonustype == "iceflower")
550     add_bonus(ICE_BONUS);
551   else if(bonustype == "none")
552     add_bonus(NO_BONUS);
553   
554   
555   std::ostringstream msg;
556   msg << "Unknown bonus type "  << bonustype;
557   throw std::runtime_error(msg.str());
558 }
559
560 void
561 Player::add_bonus(BonusType type, bool animate)
562 {
563   // always ignore NO_BONUS
564   if (type == NO_BONUS) {
565     return;
566   }
567
568   // ignore GROWUP_BONUS if we're already big
569   if (type == GROWUP_BONUS) {
570     if (player_status->bonus == GROWUP_BONUS) return; 
571     if (player_status->bonus == FIRE_BONUS) return;
572     if (player_status->bonus == ICE_BONUS) return;
573   }
574
575   set_bonus(type, animate);
576 }
577
578 void
579 Player::set_bonus(BonusType type, bool animate)
580 {
581   if(player_status->bonus == NO_BONUS) {
582     adjust_height = 62.8;
583     if(animate)
584       growing_timer.start(GROWING_TIME);
585   }
586
587   if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
588     player_status->max_fire_bullets = 0;
589     player_status->max_ice_bullets = 0;
590   }
591   if (type == FIRE_BONUS) player_status->max_fire_bullets++;
592   if (type == ICE_BONUS) player_status->max_ice_bullets++;
593
594   player_status->bonus = type;
595 }
596
597 void
598 Player::set_visible(bool visible)
599 {
600   this->visible = visible;
601 }
602
603 bool
604 Player::get_visible()
605 {
606   return visible;
607 }
608
609 void
610 Player::kick()
611 {
612   kick_timer.start(KICK_TIME);
613 }
614
615 void
616 Player::draw(DrawingContext& context)
617 {
618   if(!visible)
619     return;
620
621   TuxBodyParts* tux_body;
622           
623   if (player_status->bonus == GROWUP_BONUS)
624     tux_body = big_tux;
625   else if (player_status->bonus == FIRE_BONUS)
626     tux_body = fire_tux;
627   else if (player_status->bonus == ICE_BONUS)
628     tux_body = ice_tux;
629   else
630     tux_body = small_tux;
631
632   int layer = LAYER_OBJECTS + 1;
633
634   /* Set Tux sprite action */
635   if (duck && is_big())
636     {
637     if(dir == LEFT)
638       tux_body->set_action("duck-left");
639     else // dir == RIGHT
640       tux_body->set_action("duck-right");
641     }
642   else if (skidding_timer.started() && !skidding_timer.check())
643     {
644     if(dir == LEFT)
645       tux_body->set_action("skid-left");
646     else // dir == RIGHT
647       tux_body->set_action("skid-right");
648     }
649   else if (kick_timer.started() && !kick_timer.check())
650     {
651     if(dir == LEFT)
652       tux_body->set_action("kick-left");
653     else // dir == RIGHT
654       tux_body->set_action("kick-right");
655     }
656   else if (butt_jump && is_big())
657     {
658     if(dir == LEFT)
659       tux_body->set_action("buttjump-left");
660     else // dir == RIGHT
661       tux_body->set_action("buttjump-right");
662     }
663   else if (!on_ground())
664     {
665     if(dir == LEFT)
666       tux_body->set_action("jump-left");
667     else // dir == RIGHT
668       tux_body->set_action("jump-right");
669     }
670   else
671     {
672     if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
673       {
674       if(dir == LEFT)
675         tux_body->set_action("stand-left");
676       else // dir == RIGHT
677         tux_body->set_action("stand-right");
678       }
679     else // moving
680       {
681       if(dir == LEFT)
682         tux_body->set_action("walk-left");
683       else // dir == RIGHT
684         tux_body->set_action("walk-right");
685       }
686     }
687
688   if(idle_timer.check())
689     {
690     if(is_big())
691       {
692       if(dir == LEFT)
693         tux_body->head->set_action("idle-left", 1);
694       else // dir == RIGHT
695         tux_body->head->set_action("idle-right", 1);
696       }
697
698     }
699
700   // Tux is holding something
701   if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
702       (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
703     {
704     if (duck)
705       {
706       if(dir == LEFT)
707         tux_body->arms->set_action("duck+grab-left");
708       else // dir == RIGHT
709         tux_body->arms->set_action("duck+grab-right");
710       }
711     else
712       {
713       if(dir == LEFT)
714         tux_body->arms->set_action("grab-left");
715       else // dir == RIGHT
716         tux_body->arms->set_action("grab-right");
717       }
718     }
719
720   /* Draw Tux */
721   if(dying) {
722     smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
723   } else if(growing_timer.get_timeleft() > 0) {
724       if (dir == RIGHT) {
725         context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
726                  GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
727       } else {
728         context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
729                 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
730       }
731     }
732   else if (safe_timer.started() && size_t(game_time*40)%2)
733     ;  // don't draw Tux
734   else
735     tux_body->draw(context, get_pos(), layer);
736
737   // Draw blinking star overlay
738   if (invincible_timer.started() &&
739      (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
740       || size_t(game_time*20)%2)
741      && !dying)
742   {
743     if (!is_big() || duck)
744       smalltux_star->draw(context, get_pos(), layer + 5);
745     else
746       bigtux_star->draw(context, get_pos(), layer + 5);
747   } 
748 }
749
750 void
751 Player::collision_tile(uint32_t tile_attributes)
752 {
753   if(tile_attributes & Tile::HURTS)
754     kill(SHRINK);
755 }
756
757 HitResponse
758 Player::collision(GameObject& other, const CollisionHit& hit)
759 {
760   Bullet* bullet = dynamic_cast<Bullet*> (&other);
761   if(bullet) {
762     return FORCE_MOVE;
763   }
764
765   if(other.get_flags() & FLAG_PORTABLE) {
766     Portable* portable = dynamic_cast<Portable*> (&other);
767     if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
768         && fabsf(hit.normal.x) > .9) {
769       grabbed_object = portable;
770       return CONTINUE;
771     }
772   }
773  
774   if(other.get_flags() & FLAG_SOLID) {
775     /*
776     printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
777         &other,
778         hit.normal.x, hit.normal.y, hit.depth,
779         get_pos().x, get_pos().y,
780         movement.x, movement.y);
781     */
782     
783     if(hit.normal.y < 0) { // landed on floor?
784       if(physic.get_velocity_y() < 0)
785         physic.set_velocity_y(0);
786
787       on_ground_flag = true;
788
789       // remember normal of this tile
790       if (hit.normal.y > -0.9) {
791         floor_normal.x = hit.normal.x;
792         floor_normal.y = hit.normal.y;
793       } else {
794         // slowly adjust to unisolid tiles. 
795         // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
796         floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
797         floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
798       }
799
800       // hack platforms so that we stand normally on them when going down...
801       Platform* platform = dynamic_cast<Platform*> (&other);
802       if(platform != NULL) {
803         if(platform->get_speed().y > 0)
804           physic.set_velocity_y(-platform->get_speed().y);
805         //physic.set_velocity_x(platform->get_speed().x);
806       }
807     } else if(hit.normal.y > 0) { // bumped against the roof
808       physic.set_velocity_y(.1);
809
810       // hack platform so that we are not glued to it from below
811       Platform* platform = dynamic_cast<Platform*> (&other);
812       if(platform != NULL) {
813         physic.set_velocity_y(-platform->get_speed().y);
814       }      
815     }
816     
817     if(fabsf(hit.normal.x) > .9) { // hit on the side?
818       physic.set_velocity_x(0);
819     }
820
821     MovingObject* omov = dynamic_cast<MovingObject*> (&other);
822     if(omov != NULL) {
823       Vector mov = movement - omov->get_movement();
824       /*
825       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",
826           omov,
827           hit.normal.x, hit.normal.y,
828           hit.depth,
829           movement.x, movement.y,
830           dest.p1.x, dest.p1.y,
831           omov->get_movement().x, omov->get_movement().y);
832       */
833     }
834     
835     return CONTINUE;
836   }
837
838 #ifdef DEBUG
839   assert(dynamic_cast<MovingObject*> (&other) != NULL);
840 #endif
841   MovingObject* moving_object = static_cast<MovingObject*> (&other); 
842   if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
843     TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
844     if(trigger) {
845       if(controller->pressed(Controller::UP))
846         trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
847     }
848
849     return FORCE_MOVE;
850   }
851
852   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
853   if(badguy != NULL) {
854     if(safe_timer.started())
855       return FORCE_MOVE;
856
857     return CONTINUE;
858   }
859
860   return FORCE_MOVE;
861 }
862
863 void
864 Player::make_invincible()
865 {
866   sound_manager->play("sounds/invincible.wav");
867   invincible_timer.start(TUX_INVINCIBLE_TIME);
868   Sector::current()->play_music(HERRING_MUSIC);               
869 }
870
871 /* Kill Player! */
872 void
873 Player::kill(HurtMode mode)
874 {
875   if(dying || deactivated)
876     return;
877
878   if(mode != KILL && 
879           (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
880     return;                          
881   
882   sound_manager->play("sounds/hurt.wav");
883
884   physic.set_velocity_x(0);
885
886   if (mode == SHRINK && is_big())
887     {
888       if (player_status->bonus == FIRE_BONUS
889           || player_status->bonus == ICE_BONUS)
890         {
891           safe_timer.start(TUX_SAFE_TIME);
892           set_bonus(GROWUP_BONUS);
893         }
894       else 
895         {
896           //growing_timer.start(GROWING_TIME);
897           safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
898           adjust_height = 30.8;
899           duck = false;
900           set_bonus(NO_BONUS);
901         }
902     }
903   else
904     {
905       srand(time(0));
906       int i;
907       for (i = 0; (i < 5) && (i < player_status->coins); i++)
908       {
909         // the numbers: starting x, starting y, velocity y
910         Sector::current()->add_object(new FallingCoin(get_pos() + Vector(rand()%5, rand()%50 - 32), rand()%200 - 100));
911       }
912       physic.enable_gravity(true);
913       physic.set_acceleration(0, 0);
914       physic.set_velocity(0, 700);
915       player_status->coins -= 25;
916       set_bonus(NO_BONUS);
917       dying = true;
918       dying_timer.start(3.0);
919       set_group(COLGROUP_DISABLED);
920
921       DisplayEffect* effect = new DisplayEffect();
922       effect->fade_out(3.0);
923       Sector::current()->add_object(effect);
924       sound_manager->stop_music(3.0);
925     }
926 }
927
928 void
929 Player::move(const Vector& vector)
930 {
931   bbox.set_pos(vector);
932   if(is_big())
933     bbox.set_size(31.8, 63.8);
934   else
935     bbox.set_size(31.8, 31.8);
936   duck = false;
937   last_ground_y = vector.y;
938
939   physic.reset();
940 }
941
942 void
943 Player::check_bounds(Camera* camera)
944 {
945   /* Keep tux in bounds: */
946   if (get_pos().x < 0)
947     { // Lock Tux to the size of the level, so that he doesn't fall of
948       // on the left side
949       bbox.set_pos(Vector(0, get_pos().y));
950     }
951
952   /* Keep in-bounds, vertically: */
953   if (get_pos().y > Sector::current()->solids->get_height() * 32)
954     {
955       kill(KILL);
956       return;
957     }
958
959   bool adjust = false;
960   // can happen if back scrolling is disabled
961   if(get_pos().x < camera->get_translation().x) {
962     bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
963     adjust = true;
964   }
965   if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
966   {
967     bbox.set_pos(Vector(
968           camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
969           get_pos().y));
970     adjust = true;
971   }
972
973   if(adjust) {
974     // FIXME
975 #if 0
976     // squished now?
977     if(collision_object_map(bbox)) {
978       kill(KILL);
979       return;
980     }
981 #endif
982   }
983 }
984
985 void
986 Player::add_velocity(const Vector& velocity)
987 {
988   physic.set_velocity(physic.get_velocity() + velocity);
989 }
990
991 void
992 Player::bounce(BadGuy& )
993 {
994   if(controller->hold(Controller::JUMP))
995     physic.set_velocity_y(520);
996   else
997     physic.set_velocity_y(300);
998 }
999
1000 //Scripting Functions Below
1001
1002 void
1003 Player::deactivate()
1004 {
1005   deactivated = true;
1006   physic.set_velocity_x(0);
1007   physic.set_velocity_y(0);
1008 }
1009
1010 void
1011 Player::activate()
1012 {
1013   deactivated = false;
1014 }
1015
1016 void Player::walk(float speed)
1017 {
1018   physic.set_velocity_x(speed);
1019 }
1020