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