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