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