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