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