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