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