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