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