small fixes
[supertux.git] / src / object / player.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.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 "video/screen.hpp"
35 #include "statistics.hpp"
36 #include "game_session.hpp"
37 #include "object/tilemap.hpp"
38 #include "object/camera.hpp"
39 #include "object/gameobjs.hpp"
40 #include "object/portable.hpp"
41 #include "trigger/trigger_base.hpp"
42 #include "control/joystickkeyboardcontroller.hpp"
43 #include "main.hpp"
44
45 static const int TILES_FOR_BUTTJUMP = 3;
46 static const float SHOOTING_TIME = .150;
47 /// time before idle animation starts
48 static const float IDLE_TIME = 2.5;
49
50 static const float WALK_ACCELERATION_X = 300;
51 static const float RUN_ACCELERATION_X = 400;
52 static const float SKID_XM = 200;
53 static const float SKID_TIME = .3;
54 static const float MAX_WALK_XM = 230;
55 static const float MAX_RUN_XM = 320;
56 static const float WALK_SPEED = 100;
57
58 // growing animation
59 Surface* growingtux_left[GROWING_FRAMES];
60 Surface* growingtux_right[GROWING_FRAMES];
61
62 Surface* tux_life = 0;
63
64 TuxBodyParts* small_tux = 0;
65 TuxBodyParts* big_tux = 0;
66 TuxBodyParts* fire_tux = 0;
67 TuxBodyParts* ice_tux = 0;
68
69 void
70 TuxBodyParts::set_action(std::string action, int loops)
71 {
72   if(head != NULL)
73     head->set_action(action, loops);
74   if(body != NULL)
75     body->set_action(action, loops);
76   if(arms != NULL)
77     arms->set_action(action, loops);
78   if(feet != NULL)
79     feet->set_action(action, loops);
80 }
81
82 void
83 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
84 {
85   if(head != NULL)
86     head->draw(context, pos, layer-1);
87   if(body != NULL)
88     body->draw(context, pos, layer-3);
89   if(arms != NULL)
90     arms->draw(context, pos, layer);
91   if(feet != NULL)
92     feet->draw(context, pos, layer-2);
93 }
94
95 Player::Player(PlayerStatus* _player_status)
96   : player_status(_player_status), grabbed_object(0)
97 {
98   controller = main_controller;
99   smalltux_gameover = sprite_manager->create("smalltux-gameover");
100   smalltux_star = sprite_manager->create("smalltux-star");
101   bigtux_star = sprite_manager->create("bigtux-star");
102   init();
103 }
104
105 Player::~Player()
106 {
107   delete smalltux_gameover;
108   delete smalltux_star;
109   delete bigtux_star;
110 }
111
112 void
113 Player::init()
114 {
115   if(is_big())
116     bbox.set_size(31.8, 63.8);
117   else
118     bbox.set_size(31.8, 31.8);
119
120   dir = RIGHT;
121   old_dir = dir;
122   duck = false;
123   dead = false;
124
125   dying = false;
126   last_ground_y = 0;
127   fall_mode = ON_GROUND;
128   jumping = false;
129   flapping = false;
130   can_jump = true;
131   can_flap = false;
132   falling_from_flap = false;
133   enable_hover = false;
134   butt_jump = false;
135   deactivated = false;
136   
137   flapping_velocity = 0;
138
139   // temporary to help player's choosing a flapping
140   flapping_mode = NO_FLAP;
141
142   // Ricardo's flapping
143   flaps_nb = 0;
144
145   on_ground_flag = false;
146   grabbed_object = 0;
147
148   physic.reset();
149 }
150
151 void
152 Player::set_controller(Controller* controller)
153 {
154   this->controller = controller;
155 }
156
157 void
158 Player::update(float elapsed_time)
159 {
160   if(dying && dying_timer.check()) {
161     dead = true;
162     return;
163   }
164
165   if(!controller->hold(Controller::ACTION) && grabbed_object) {
166     grabbed_object = 0;
167     // move the grabbed object a bit away from tux
168     Vector pos = get_pos() + 
169         Vector(dir == LEFT ? -bbox.get_width() : bbox.get_width(),
170                 bbox.get_height()*0.66666 - 32);
171     MovingObject* object = dynamic_cast<MovingObject*> (grabbed_object);
172     if(object) {
173       object->set_pos(pos);
174     } else {
175 #ifdef DEBUG
176       std::cout << "Non MovingObjetc grabbed?!?\n";
177 #endif
178     }
179   }
180
181   if(!dying && !deactivated)
182     handle_input();
183
184   movement = physic.get_movement(elapsed_time);
185   on_ground_flag = false;
186
187 #if 0
188   // special exception for cases where we're stuck under tiles after
189   // being ducked. In this case we drift out
190   if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
191      && collision_object_map(base)) {
192     base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
193     previous_base = old_base = base;
194   }
195 #endif
196
197   if(grabbed_object != 0) {
198     Vector pos = get_pos() + 
199       Vector(dir == LEFT ? -16 : 16,
200              bbox.get_height()*0.66666 - 32);
201     grabbed_object->grab(*this, pos);
202   }
203 }
204
205 bool
206 Player::on_ground()
207 {
208   return on_ground_flag;
209 }
210
211 bool
212 Player::is_big()
213 {
214   if(player_status->bonus == NO_BONUS)
215     return false;
216
217   return true;
218 }
219
220 void
221 Player::handle_horizontal_input()
222 {
223   float vx = physic.get_velocity_x();
224   float vy = physic.get_velocity_y();
225   float ax = physic.get_acceleration_x();
226   float ay = physic.get_acceleration_y();
227
228   float dirsign = 0;
229   if(!duck || physic.get_velocity_y() != 0) {
230     if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
231       old_dir = dir;
232       dir = LEFT;
233       dirsign = -1;
234     } else if(!controller->hold(Controller::LEFT)
235               && controller->hold(Controller::RIGHT)) {
236       old_dir = dir;
237       dir = RIGHT;
238       dirsign = 1;
239     }
240   }
241
242   if (!controller->hold(Controller::ACTION)) {
243     ax = dirsign * WALK_ACCELERATION_X;
244     // limit speed
245     if(vx >= MAX_WALK_XM && dirsign > 0) {
246       vx = MAX_WALK_XM;
247       ax = 0;
248     } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
249       vx = -MAX_WALK_XM;
250       ax = 0;
251     }
252   } else {
253     ax = dirsign * RUN_ACCELERATION_X;
254     // limit speed
255     if(vx >= MAX_RUN_XM && dirsign > 0) {
256       vx = MAX_RUN_XM;
257       ax = 0;
258     } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
259       vx = -MAX_RUN_XM;
260       ax = 0;
261     }
262   }
263
264   // we can reach WALK_SPEED without any acceleration
265   if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
266     vx = dirsign * WALK_SPEED;
267   }
268
269   // changing directions?
270   if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
271     // let's skid!
272     if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
273       skidding_timer.start(SKID_TIME);
274       sound_manager->play("sounds/skid.wav");
275       // dust some particles
276       Sector::current()->add_object(
277         new Particles(
278           Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
279                  bbox.p2.y),
280           dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
281           Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
282           LAYER_OBJECTS+1));
283       
284       ax *= 2.5;
285     } else {
286       ax *= 2;
287     }
288   }
289
290   // we get slower when not pressing any keys
291   if(dirsign == 0) {
292     if(fabs(vx) < WALK_SPEED) {
293       vx = 0;
294       ax = 0;
295     } else if(vx < 0) {
296       ax = WALK_ACCELERATION_X * 1.5;
297     } else {
298       ax = WALK_ACCELERATION_X * -1.5;
299     }
300   }
301
302 #if 0
303   // if we're on ice slow down acceleration or deceleration
304   if (isice(base.x, base.y + base.height))
305   {
306     /* the acceleration/deceleration rate on ice is inversely proportional to
307      * the current velocity.
308      */
309
310     // increasing 1 will increase acceleration/deceleration rate
311     // decreasing 1 will decrease acceleration/deceleration rate
312     //  must stay above zero, though
313     if (ax != 0) ax *= 1 / fabs(vx);
314   }
315 #endif
316
317   // extend/shrink tux collision rectangle so that we fall through/walk over 1
318   // tile holes
319   if(fabsf(vx) > MAX_WALK_XM) {
320     bbox.set_width(33);
321   } else {
322     bbox.set_width(31.8);
323   }
324
325   physic.set_velocity(vx, vy);
326   physic.set_acceleration(ax, ay);
327 }
328
329 void
330 Player::handle_vertical_input()
331 {
332   // set fall mode...
333   if(on_ground()) {
334     fall_mode = ON_GROUND;
335     last_ground_y = get_pos().y;
336   } else {
337     if(get_pos().y > last_ground_y)
338       fall_mode = FALLING;
339     else if(fall_mode == ON_GROUND)
340       fall_mode = JUMPING;
341   }
342
343   if(on_ground()) { /* Make sure jumping is off. */
344     jumping = false;
345     flapping = false;
346     falling_from_flap = false;
347     if (flapping_timer.started()) {
348       flapping_timer.stop();
349     }
350
351     physic.set_acceleration_y(0); //for flapping
352   }
353
354   // Press jump key
355   if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
356     if (duck) // only jump a little bit when in duck mode
357       physic.set_velocity_y(300);
358     else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
359       physic.set_velocity_y(580);
360     else
361       physic.set_velocity_y(520);
362     
363     //bbox.move(Vector(0, -1));
364     jumping = true;
365     flapping = false;
366     can_jump = false;
367     can_flap = false;
368     flaps_nb = 0; // Ricardo's flapping
369     if (is_big())
370       sound_manager->play("sounds/bigjump.wav");
371     else
372       sound_manager->play("sounds/jump.wav");
373   } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
374     if (!flapping && !duck && !falling_from_flap && !on_ground()) {
375       can_flap = true;
376     }
377     if (jumping && physic.get_velocity_y() > 0) {
378       jumping = false;
379       physic.set_velocity_y(0);
380     }
381   }
382 #if CHOOSEFLAPSTYLE
383   // temporary to help players choosing a flapping
384   if(flapping_mode == RICARDO_FLAP) {
385     // Flapping, Ricardo's version
386     // similar to SM3 Fox
387     if(controller->pressed(Controller::JUMP) && can_flap && flaps_nb < 3) {
388       physic.set_velocity_y(350);
389       physic.set_velocity_x(physic.get_velocity_x() * 35);
390       flaps_nb++;
391     }
392   } else if(flapping_mode == MAREK_FLAP) {
393 #endif
394     // Flapping, Marek and Ondra's version
395     if (controller->hold(Controller::JUMP) && can_flap)
396     {
397       if (flapping_timer.check()) 
398       {
399         can_flap = false;
400         //flapping = false;
401         falling_from_flap = true;
402       }
403       else if (!flapping_timer.started())
404       {
405         flapping_timer.start(TUX_FLAPPING_TIME);
406         flapping_velocity = physic.get_velocity_x();
407       }
408       else
409       {
410         jumping = true;
411         flapping = true;
412         float cv = flapping_velocity * sqrt(
413           TUX_FLAPPING_TIME - flapping_timer.get_timegone() 
414           / TUX_FLAPPING_TIME);
415       
416         //Handle change of direction while flapping
417         if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
418           cv *= (-1);
419         }
420         else if (cv == 0) {
421           if (controller->hold(Controller::LEFT)) {
422                 cv = -TUX_FLAPPING_LEAST_X;
423           }
424           else if (controller->hold(Controller::RIGHT)) {
425             cv = TUX_FLAPPING_LEAST_X;
426           }
427         }
428         physic.set_velocity_x(cv);
429         physic.set_velocity_y(flapping_timer.get_timegone()
430             * TUX_FLAPPING_STRENGTH);
431         //std::cout << "Timegone: " << flapping_timer.get_timegone() << ", Y velocity: " << physic.get_velocity_y() << "\n";
432       }
433     }
434 #if CHOOSEFLAPSTYLE
435   } else if(flapping_mode == RYAN_FLAP) {
436     // Flapping, Ryan's version
437     if (controller->hold(Controller::JUMP) && can_flap)
438     {
439       if (!flapping_timer.started())
440       {
441         flapping_timer.start(TUX_FLAPPING_TIME);
442       }
443       if (flapping_timer.check()) 
444       {
445         can_flap = false;
446         falling_from_flap = true;
447       }
448       jumping = true;
449       flapping = true;
450       if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
451           && physic.get_velocity_y() < 0)
452       {
453         float gravity = Sector::current()->gravity;
454         (void)gravity;
455         float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
456         
457         // XXX: magic numbers. should be a percent of gravity
458         //      gravity is (by default) -0.1f
459         physic.set_acceleration_y(12 + 1*xr);
460         
461 #if 0
462         // To slow down x-vel when flapping (not working)
463         if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
464         {
465           if (physic.get_velocity_x() < 0)
466             physic.set_acceleration_x(1.0f);
467           else if (physic.get_velocity_x() > 0)
468             physic.set_acceleration_x(-1.0f);
469         }
470 #endif
471       }
472     } else {
473       physic.set_acceleration_y(0);
474     }
475   }
476 #endif
477
478   /* In case the player has pressed Down while in a certain range of air,
479      enable butt jump action */
480   if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
481     //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
482     butt_jump = true;
483   
484   /* When Down is not held anymore, disable butt jump */
485   if(butt_jump && !controller->hold(Controller::DOWN))
486     butt_jump = false;
487   
488 #if 0
489   // Do butt jump
490   if (butt_jump && on_ground() && is_big()) {
491     // Add a smoke cloud
492     if (duck) 
493       Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
494     else 
495       Sector::current()->add_smoke_cloud(
496         Vector(get_pos().x - 32, get_pos().y + 32));
497     
498     butt_jump = false;
499     
500     // Break bricks beneath Tux
501     if(Sector::current()->trybreakbrick(
502          Vector(base.x + 1, base.y + base.height), false)
503        || Sector::current()->trybreakbrick(
504          Vector(base.x + base.width - 1, base.y + base.height), false)) {
505       physic.set_velocity_y(2);
506       butt_jump = true;
507     }
508     
509     // Kill nearby badguys
510     std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
511     for (std::vector<GameObject*>::iterator i = gameobjects.begin();
512          i != gameobjects.end();
513          i++) {
514       BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
515       if(badguy) {
516         // don't kill when badguys are already dying or in a certain mode
517         if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
518            badguy->mode != BadGuy::BOMB_EXPLODE) {
519           if (fabsf(base.x - badguy->base.x) < 96 &&
520               fabsf(base.y - badguy->base.y) < 64)
521             badguy->kill_me(25);
522         }
523       }
524     }
525   }
526 #endif
527
528   /** jumping is only allowed if we're about to touch ground soon and if the
529    * button has been up in between the last jump
530    */
531   // FIXME
532 #if 0
533   if ( (issolid(get_pos().x + bbox.get_width() / 2,
534           get_pos().y + bbox.get_height() + 64) ||
535         issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
536         issolid(get_pos().x + bbox.get_width() - 1,
537           get_pos().y + bbox.get_height() + 64))
538        && jumping  == false
539        && can_jump == false
540        && input.jump && !input.old_jump)
541     {
542       can_jump = true;
543     }
544 #endif
545 }
546
547 void
548 Player::handle_input()
549 {
550   /* Handle horizontal movement: */
551   handle_horizontal_input();
552
553   /* Jump/jumping? */
554   if (on_ground() && !controller->hold(Controller::JUMP))
555     can_jump = true;
556   handle_vertical_input();
557
558   /* Shoot! */
559   if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
560     if(Sector::current()->add_bullet(
561          get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2) 
562                       : Vector(32, bbox.get_height()/2)),
563          physic.get_velocity_x(), dir))
564       shooting_timer.start(SHOOTING_TIME);
565   }
566   
567   /* Duck! */
568   if (controller->hold(Controller::DOWN) && is_big() && !duck 
569       && physic.get_velocity_y() == 0 && on_ground()) {
570     duck = true;
571     bbox.move(Vector(0, 32));
572     bbox.set_height(31.8);
573   } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
574     // try if we can really unduck
575     bbox.move(Vector(0, -32));
576     bbox.set_height(63.8);
577     duck = false;
578     // FIXME
579 #if 0
580     // when unducking in air we need some space to do so
581     if(on_ground() || !collision_object_map(bbox)) {
582       duck = false;
583     } else {
584       // undo the ducking changes
585       bbox.move(Vector(0, 32));
586       bbox.set_height(31.8);
587     }
588 #endif
589   }
590 }
591
592 void
593 Player::set_bonus(BonusType type, bool animate)
594 {
595   if(player_status->bonus >= type)
596     return;
597   
598   if(player_status->bonus == NO_BONUS) {
599     bbox.set_height(63.8);
600     bbox.move(Vector(0, -32));
601     if(animate)
602       growing_timer.start(GROWING_TIME);
603   }
604   
605   player_status->bonus = type;
606 }
607
608 void
609 Player::draw(DrawingContext& context)
610 {
611   TuxBodyParts* tux_body;
612           
613   if (player_status->bonus == GROWUP_BONUS)
614     tux_body = big_tux;
615   else if (player_status->bonus == FIRE_BONUS)
616     tux_body = fire_tux;
617   else if (player_status->bonus == ICE_BONUS)
618     tux_body = ice_tux;
619   else
620     tux_body = small_tux;
621
622   int layer = LAYER_OBJECTS + 10;
623
624   /* Set Tux sprite action */
625   if (duck && is_big())
626     {
627     if(dir == LEFT)
628       tux_body->set_action("duck-left");
629     else // dir == RIGHT
630       tux_body->set_action("duck-right");
631     }
632   else if (skidding_timer.started() && !skidding_timer.check())
633     {
634     if(dir == LEFT)
635       tux_body->set_action("skid-left");
636     else // dir == RIGHT
637       tux_body->set_action("skid-right");
638     }
639   else if (kick_timer.started() && !kick_timer.check())
640     {
641     if(dir == LEFT)
642       tux_body->set_action("kick-left");
643     else // dir == RIGHT
644       tux_body->set_action("kick-right");
645     }
646   else if (butt_jump && is_big())
647     {
648     if(dir == LEFT)
649       tux_body->set_action("buttjump-left");
650     else // dir == RIGHT
651       tux_body->set_action("buttjump-right");
652     }
653   else if (physic.get_velocity_y() != 0)
654     {
655     if(dir == LEFT)
656       tux_body->set_action("jump-left");
657     else // dir == RIGHT
658       tux_body->set_action("jump-right");
659     }
660   else
661     {
662     if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
663       {
664       if(dir == LEFT)
665         tux_body->set_action("stand-left");
666       else // dir == RIGHT
667         tux_body->set_action("stand-right");
668       }
669     else // moving
670       {
671       if(dir == LEFT)
672         tux_body->set_action("walk-left");
673       else // dir == RIGHT
674         tux_body->set_action("walk-right");
675       }
676     }
677
678   if(idle_timer.check())
679     {
680     if(is_big())
681       {
682       if(dir == LEFT)
683         tux_body->head->set_action("idle-left", 1);
684       else // dir == RIGHT
685         tux_body->head->set_action("idle-right", 1);
686       }
687
688     }
689
690   // Tux is holding something
691   if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
692       (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
693     {
694     if (duck)
695       {
696       if(dir == LEFT)
697         tux_body->arms->set_action("duck+grab-left");
698       else // dir == RIGHT
699         tux_body->arms->set_action("duck+grab-right");
700       }
701     else
702       {
703       if(dir == LEFT)
704         tux_body->arms->set_action("grab-left");
705       else // dir == RIGHT
706         tux_body->arms->set_action("grab-right");
707       }
708     }
709
710   /* Draw Tux */
711   if(dying) {
712     smalltux_gameover->draw(context, get_pos(), layer);
713   } else if(growing_timer.get_timeleft() > 0) {
714     if(!is_big())
715       {
716       if (dir == RIGHT)
717         context.draw_surface(growingtux_right[GROWING_FRAMES-1 - 
718                  int((growing_timer.get_timegone() *
719                  GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
720       else
721         context.draw_surface(growingtux_left[GROWING_FRAMES-1 - 
722                 int((growing_timer.get_timegone() *
723                 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
724       }
725     else
726       {
727       if (dir == RIGHT)
728         context.draw_surface(growingtux_right[
729             int((growing_timer.get_timegone() *
730                 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
731       else
732         context.draw_surface(growingtux_left[
733             int((growing_timer.get_timegone() *
734                              GROWING_FRAMES) / GROWING_TIME)],
735             get_pos(), layer);
736       }
737     }
738   else if (safe_timer.started() && size_t(game_time*40)%2)
739     ;  // don't draw Tux
740   else
741     tux_body->draw(context, get_pos(), layer);
742
743   // Draw blinking star overlay
744   if (invincible_timer.started() &&
745      (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
746       || size_t(game_time*20)%2)
747      && !dying)
748   {
749     if (!is_big() || duck)
750       smalltux_star->draw(context, get_pos(), layer + 5);
751     else
752       bigtux_star->draw(context, get_pos(), layer + 5);
753   } 
754 }
755
756 HitResponse
757 Player::collision(GameObject& other, const CollisionHit& hit)
758 {
759   Portable* portable = dynamic_cast<Portable*> (&other);
760   if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
761      && fabsf(hit.normal.x) > .9) {
762     grabbed_object = portable;
763     return CONTINUE;
764   }
765  
766   if(other.get_flags() & FLAG_SOLID) {
767     TileMap* tilemap = dynamic_cast<TileMap*> (&other);
768     if(tilemap) {
769       const TilemapCollisionHit* thit = 
770         static_cast<const TilemapCollisionHit*> (&hit);
771       if(thit->tileflags & Tile::SPIKE)
772         kill(SHRINK);
773
774       if(! (thit->tileflags & Tile::SOLID))
775         return FORCE_MOVE;
776     }
777     
778     if(hit.normal.y < 0) { // landed on floor?
779       if (physic.get_velocity_y() < 0)
780         physic.set_velocity_y(0);
781       on_ground_flag = true;
782     } else if(hit.normal.y > 0) { // bumped against the roof
783       physic.set_velocity_y(.1);
784     }
785     
786     if(fabsf(hit.normal.x) > .9) { // hit on the side?
787       physic.set_velocity_x(0);
788     }
789
790     return CONTINUE;
791   }
792
793   TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
794   if(trigger) {
795     if(controller->pressed(Controller::UP))
796       trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
797   }
798
799   return FORCE_MOVE;
800 }
801
802 void
803 Player::make_invincible()
804 {
805   sound_manager->play("sounds/invincible.wav");
806   invincible_timer.start(TUX_INVINCIBLE_TIME);
807   Sector::current()->play_music(HERRING_MUSIC);               
808 }
809
810 /* Kill Player! */
811 void
812 Player::kill(HurtMode mode)
813 {
814   if(dying || deactivated)
815     return;
816
817   if(mode != KILL && 
818           safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
819     return;                          
820   
821   sound_manager->play("sounds/hurt.wav");
822
823   physic.set_velocity_x(0);
824
825   if (mode == SHRINK && is_big())
826     {
827       if (player_status->bonus == FIRE_BONUS
828           || player_status->bonus == ICE_BONUS)
829         {
830           safe_timer.start(TUX_SAFE_TIME);
831           player_status->bonus = GROWUP_BONUS;
832         }
833       else 
834         {
835           growing_timer.start(GROWING_TIME);
836           safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
837           bbox.set_height(31.8);
838           duck = false;
839           player_status->bonus = NO_BONUS;
840         }
841     }
842   else
843     {
844       physic.enable_gravity(true);
845       physic.set_acceleration(0, 0);
846       physic.set_velocity(0, 700);
847       player_status->lives -= 1;
848       player_status->bonus = NO_BONUS;
849       dying = true;
850       dying_timer.start(3.0);
851       flags |= FLAG_NO_COLLDET;
852     }
853 }
854
855 void
856 Player::move(const Vector& vector)
857 {
858   bbox.set_pos(vector);
859   if(is_big())
860     bbox.set_size(31.8, 63.8);
861   else
862     bbox.set_size(31.8, 31.8);
863   on_ground_flag = false;
864   duck = false;
865   last_ground_y = vector.y;
866
867   physic.reset();
868 }
869
870 void
871 Player::check_bounds(Camera* camera)
872 {
873   /* Keep tux in bounds: */
874   if (get_pos().x < 0)
875     { // Lock Tux to the size of the level, so that he doesn't fall of
876       // on the left side
877       bbox.set_pos(Vector(0, get_pos().y));
878     }
879
880   /* Keep in-bounds, vertically: */
881   if (get_pos().y > Sector::current()->solids->get_height() * 32)
882     {
883       kill(KILL);
884       return;
885     }
886
887   bool adjust = false;
888   // can happen if back scrolling is disabled
889   if(get_pos().x < camera->get_translation().x) {
890     bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
891     adjust = true;
892   }
893   if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
894   {
895     bbox.set_pos(Vector(
896           camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
897           get_pos().y));
898     adjust = true;
899   }
900
901   if(adjust) {
902     // FIXME
903 #if 0
904     // squished now?
905     if(collision_object_map(bbox)) {
906       kill(KILL);
907       return;
908     }
909 #endif
910   }
911 }
912
913 void
914 Player::bounce(BadGuy& )
915 {
916   //Make sure we stopped flapping
917   flapping = false;
918   falling_from_flap = false;
919   
920   if(controller->hold(Controller::JUMP))
921     physic.set_velocity_y(520);
922   else
923     physic.set_velocity_y(300);
924 }
925
926 //Scripting Functions Below
927
928 void
929 Player::deactivate()
930 {
931   deactivated = true;
932   physic.set_velocity_x(0);
933   physic.set_velocity_y(0);
934 }
935
936 void
937 Player::activate()
938 {
939   deactivated = false;
940 }
941
942 void Player::walk(float speed)
943 {
944   physic.set_velocity_x(speed);
945 }