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