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