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