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