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