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