d7e380f12d4756729b787c023339f40eae3a3061
[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::add_coins(int count)
565 {
566   player_status->add_coins(count);
567 }
568
569 void
570 Player::set_bonus(const std::string& bonustype)
571 {
572   if(bonustype == "grow")
573     set_bonus(GROWUP_BONUS);
574   else if(bonustype == "fireflower")
575     set_bonus(FIRE_BONUS);
576   else if(bonustype == "iceflower")
577     set_bonus(ICE_BONUS);
578   else if(bonustype == "none")
579     set_bonus(NO_BONUS);
580   
581   
582   std::ostringstream msg;
583   msg << "Unknown bonus type "  << bonustype;
584   throw std::runtime_error(msg.str());
585 }
586
587 void
588 Player::set_bonus(BonusType type, bool animate)
589 {
590   if(player_status->bonus >= type)
591     return;
592   
593   if(player_status->bonus == NO_BONUS) {
594     adjust_height = 62.8;
595     if(animate)
596       growing_timer.start(GROWING_TIME);
597   }
598   
599   player_status->bonus = type;
600 }
601
602 void
603 Player::set_visible(bool visible)
604 {
605   this->visible = visible;
606 }
607
608 bool
609 Player::get_visible()
610 {
611   return visible;
612 }
613
614 void
615 Player::kick()
616 {
617   kick_timer.start(KICK_TIME);
618 }
619
620 void
621 Player::draw(DrawingContext& context)
622 {
623   if(!visible)
624     return;
625
626   TuxBodyParts* tux_body;
627           
628   if (player_status->bonus == GROWUP_BONUS)
629     tux_body = big_tux;
630   else if (player_status->bonus == FIRE_BONUS)
631     tux_body = fire_tux;
632   else if (player_status->bonus == ICE_BONUS)
633     tux_body = ice_tux;
634   else
635     tux_body = small_tux;
636
637   int layer = LAYER_OBJECTS + 10;
638
639   /* Set Tux sprite action */
640   if (duck && is_big())
641     {
642     if(dir == LEFT)
643       tux_body->set_action("duck-left");
644     else // dir == RIGHT
645       tux_body->set_action("duck-right");
646     }
647   else if (skidding_timer.started() && !skidding_timer.check())
648     {
649     if(dir == LEFT)
650       tux_body->set_action("skid-left");
651     else // dir == RIGHT
652       tux_body->set_action("skid-right");
653     }
654   else if (kick_timer.started() && !kick_timer.check())
655     {
656     if(dir == LEFT)
657       tux_body->set_action("kick-left");
658     else // dir == RIGHT
659       tux_body->set_action("kick-right");
660     }
661   else if (butt_jump && is_big())
662     {
663     if(dir == LEFT)
664       tux_body->set_action("buttjump-left");
665     else // dir == RIGHT
666       tux_body->set_action("buttjump-right");
667     }
668   else if (!on_ground())
669     {
670     if(dir == LEFT)
671       tux_body->set_action("jump-left");
672     else // dir == RIGHT
673       tux_body->set_action("jump-right");
674     }
675   else
676     {
677     if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
678       {
679       if(dir == LEFT)
680         tux_body->set_action("stand-left");
681       else // dir == RIGHT
682         tux_body->set_action("stand-right");
683       }
684     else // moving
685       {
686       if(dir == LEFT)
687         tux_body->set_action("walk-left");
688       else // dir == RIGHT
689         tux_body->set_action("walk-right");
690       }
691     }
692
693   if(idle_timer.check())
694     {
695     if(is_big())
696       {
697       if(dir == LEFT)
698         tux_body->head->set_action("idle-left", 1);
699       else // dir == RIGHT
700         tux_body->head->set_action("idle-right", 1);
701       }
702
703     }
704
705   // Tux is holding something
706   if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
707       (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
708     {
709     if (duck)
710       {
711       if(dir == LEFT)
712         tux_body->arms->set_action("duck+grab-left");
713       else // dir == RIGHT
714         tux_body->arms->set_action("duck+grab-right");
715       }
716     else
717       {
718       if(dir == LEFT)
719         tux_body->arms->set_action("grab-left");
720       else // dir == RIGHT
721         tux_body->arms->set_action("grab-right");
722       }
723     }
724
725   /* Draw Tux */
726   if(dying) {
727     smalltux_gameover->draw(context, get_pos(), layer);
728   } else if(growing_timer.get_timeleft() > 0) {
729       if (dir == RIGHT) {
730         context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
731                  GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
732       } else {
733         context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
734                 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
735       }
736     }
737   else if (safe_timer.started() && size_t(game_time*40)%2)
738     ;  // don't draw Tux
739   else
740     tux_body->draw(context, get_pos(), layer);
741
742   // Draw blinking star overlay
743   if (invincible_timer.started() &&
744      (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
745       || size_t(game_time*20)%2)
746      && !dying)
747   {
748     if (!is_big() || duck)
749       smalltux_star->draw(context, get_pos(), layer + 5);
750     else
751       bigtux_star->draw(context, get_pos(), layer + 5);
752   } 
753 }
754
755 void
756 Player::collision_tile(uint32_t tile_attributes)
757 {
758   if(tile_attributes & Tile::HURTS)
759     kill(SHRINK);
760 }
761
762 HitResponse
763 Player::collision(GameObject& other, const CollisionHit& hit)
764 {
765   Bullet* bullet = dynamic_cast<Bullet*> (&other);
766   if(bullet) {
767     return FORCE_MOVE;
768   }
769
770   if(other.get_flags() & FLAG_PORTABLE) {
771     Portable* portable = dynamic_cast<Portable*> (&other);
772     if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
773         && fabsf(hit.normal.x) > .9) {
774       grabbed_object = portable;
775       return CONTINUE;
776     }
777   }
778  
779   if(other.get_flags() & FLAG_SOLID) {
780     /*
781     printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
782         &other,
783         hit.normal.x, hit.normal.y, hit.depth,
784         get_pos().x, get_pos().y,
785         movement.x, movement.y);
786     */
787     
788     if(hit.normal.y < 0) { // landed on floor?
789       if(physic.get_velocity_y() < 0)
790         physic.set_velocity_y(0);
791
792       on_ground_flag = true;
793
794       // remember normal of this tile
795       if (hit.normal.y > -0.9) {
796         floor_normal.x = hit.normal.x;
797         floor_normal.y = hit.normal.y;
798       } else {
799         // slowly adjust to unisolid tiles. 
800         // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
801         floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
802         floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
803       }
804
805       // hack platforms so that we stand normally on them when going down...
806       Platform* platform = dynamic_cast<Platform*> (&other);
807       if(platform != NULL) {
808         if(platform->get_speed().y > 0)
809           physic.set_velocity_y(-platform->get_speed().y);
810         //physic.set_velocity_x(platform->get_speed().x);
811       }
812     } else if(hit.normal.y > 0) { // bumped against the roof
813       physic.set_velocity_y(.1);
814
815       // hack platform so that we are not glued to it from below
816       Platform* platform = dynamic_cast<Platform*> (&other);
817       if(platform != NULL) {
818         physic.set_velocity_y(-platform->get_speed().y);
819       }      
820     }
821     
822     if(fabsf(hit.normal.x) > .9) { // hit on the side?
823       physic.set_velocity_x(0);
824     }
825
826     MovingObject* omov = dynamic_cast<MovingObject*> (&other);
827     if(omov != NULL) {
828       Vector mov = movement - omov->get_movement();
829       /*
830       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",
831           omov,
832           hit.normal.x, hit.normal.y,
833           hit.depth,
834           movement.x, movement.y,
835           dest.p1.x, dest.p1.y,
836           omov->get_movement().x, omov->get_movement().y);
837       */
838     }
839     
840     return CONTINUE;
841   }
842
843 #ifdef DEBUG
844   assert(dynamic_cast<MovingObject*> (&other) != NULL);
845 #endif
846   MovingObject* moving_object = static_cast<MovingObject*> (&other); 
847   if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
848     TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
849     if(trigger) {
850       if(controller->pressed(Controller::UP))
851         trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
852     }
853
854     return FORCE_MOVE;
855   }
856
857   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
858   if(badguy != NULL) {
859     if(safe_timer.started())
860       return FORCE_MOVE;
861
862     return CONTINUE;
863   }
864
865   return FORCE_MOVE;
866 }
867
868 void
869 Player::make_invincible()
870 {
871   sound_manager->play("sounds/invincible.wav");
872   invincible_timer.start(TUX_INVINCIBLE_TIME);
873   Sector::current()->play_music(HERRING_MUSIC);               
874 }
875
876 /* Kill Player! */
877 void
878 Player::kill(HurtMode mode)
879 {
880   if(dying || deactivated)
881     return;
882
883   if(mode != KILL && 
884           (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
885     return;                          
886   
887   sound_manager->play("sounds/hurt.wav");
888
889   physic.set_velocity_x(0);
890
891   if (mode == SHRINK && is_big())
892     {
893       if (player_status->bonus == FIRE_BONUS
894           || player_status->bonus == ICE_BONUS)
895         {
896           safe_timer.start(TUX_SAFE_TIME);
897           player_status->bonus = GROWUP_BONUS;
898         }
899       else 
900         {
901           //growing_timer.start(GROWING_TIME);
902           safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
903           adjust_height = 30.8;
904           duck = false;
905           player_status->bonus = NO_BONUS;
906         }
907     }
908   else
909     {
910       srand(time(0));
911       int i;
912       for (i = 0; (i < 5) && (i < player_status->coins); i++)
913       {
914         // the numbers: starting x, starting y, velocity y
915         Sector::current()->add_object(new FallingCoin(get_pos() + Vector(rand()%5, rand()%50 - 32), rand()%200 - 100));
916       }
917       physic.enable_gravity(true);
918       physic.set_acceleration(0, 0);
919       physic.set_velocity(0, 700);
920       player_status->coins -= 25;
921       player_status->bonus = NO_BONUS;
922       dying = true;
923       dying_timer.start(3.0);
924       set_group(COLGROUP_DISABLED);
925
926       DisplayEffect* effect = new DisplayEffect();
927       effect->fade_out(3.0);
928       Sector::current()->add_object(effect);
929       sound_manager->stop_music(3.0);
930     }
931 }
932
933 void
934 Player::move(const Vector& vector)
935 {
936   bbox.set_pos(vector);
937   if(is_big())
938     bbox.set_size(31.8, 63.8);
939   else
940     bbox.set_size(31.8, 31.8);
941   duck = false;
942   last_ground_y = vector.y;
943
944   physic.reset();
945 }
946
947 void
948 Player::check_bounds(Camera* camera)
949 {
950   /* Keep tux in bounds: */
951   if (get_pos().x < 0)
952     { // Lock Tux to the size of the level, so that he doesn't fall of
953       // on the left side
954       bbox.set_pos(Vector(0, get_pos().y));
955     }
956
957   /* Keep in-bounds, vertically: */
958   if (get_pos().y > Sector::current()->solids->get_height() * 32)
959     {
960       kill(KILL);
961       return;
962     }
963
964   bool adjust = false;
965   // can happen if back scrolling is disabled
966   if(get_pos().x < camera->get_translation().x) {
967     bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
968     adjust = true;
969   }
970   if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
971   {
972     bbox.set_pos(Vector(
973           camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
974           get_pos().y));
975     adjust = true;
976   }
977
978   if(adjust) {
979     // FIXME
980 #if 0
981     // squished now?
982     if(collision_object_map(bbox)) {
983       kill(KILL);
984       return;
985     }
986 #endif
987   }
988 }
989
990 void
991 Player::add_velocity(const Vector& velocity)
992 {
993   physic.set_velocity(physic.get_velocity() + velocity);
994 }
995
996 void
997 Player::bounce(BadGuy& )
998 {
999   if(controller->hold(Controller::JUMP))
1000     physic.set_velocity_y(520);
1001   else
1002     physic.set_velocity_y(300);
1003 }
1004
1005 //Scripting Functions Below
1006
1007 void
1008 Player::deactivate()
1009 {
1010   deactivated = true;
1011   physic.set_velocity_x(0);
1012   physic.set_velocity_y(0);
1013 }
1014
1015 void
1016 Player::activate()
1017 {
1018   deactivated = false;
1019 }
1020
1021 void Player::walk(float speed)
1022 {
1023   physic.set_velocity_x(speed);
1024 }
1025