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