EnableKeyRepeat while console is open.
[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
52 static const int TILES_FOR_BUTTJUMP = 3;
53 static const float SHOOTING_TIME = .150;
54 /// time before idle animation starts
55 static const float IDLE_TIME = 2.5;
56
57 static const float WALK_ACCELERATION_X = 300;
58 static const float RUN_ACCELERATION_X = 400;
59 static const float SKID_XM = 200;
60 static const float SKID_TIME = .3;
61 static const float MAX_WALK_XM = 230;
62 static const float MAX_RUN_XM = 320;
63 static const float WALK_SPEED = 100;
64
65 static const float KICK_TIME = .3;
66
67 // growing animation
68 Surface* growingtux_left[GROWING_FRAMES];
69 Surface* growingtux_right[GROWING_FRAMES];
70
71 Surface* tux_life = 0;
72
73 TuxBodyParts* small_tux = 0;
74 TuxBodyParts* big_tux = 0;
75 TuxBodyParts* fire_tux = 0;
76 TuxBodyParts* ice_tux = 0;
77
78 void
79 TuxBodyParts::set_action(std::string action, int loops)
80 {
81   if(head != NULL)
82     head->set_action(action, loops);
83   if(body != NULL)
84     body->set_action(action, loops);
85   if(arms != NULL)
86     arms->set_action(action, loops);
87   if(feet != NULL)
88     feet->set_action(action, loops);
89 }
90
91 void
92 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
93 {
94   if(head != NULL)
95     head->draw(context, pos, layer-1);
96   if(body != NULL)
97     body->draw(context, pos, layer-3);
98   if(arms != NULL)
99     arms->draw(context, pos, layer+10);
100   if(feet != NULL)
101     feet->draw(context, pos, layer-2);
102 }
103
104 Player::Player(PlayerStatus* _player_status)
105   : player_status(_player_status), grabbed_object(NULL), ghost_mode(false)
106 {
107   controller = main_controller;
108   smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
109   smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
110   bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
111
112   sound_manager->preload("sounds/bigjump.wav");
113   sound_manager->preload("sounds/jump.wav");
114   sound_manager->preload("sounds/hurt.wav");
115   sound_manager->preload("sounds/skid.wav");
116
117   init();
118 }
119
120 Player::~Player()
121 {
122   delete smalltux_gameover;
123   delete smalltux_star;
124   delete bigtux_star;
125 }
126
127 void
128 Player::init()
129 {
130   if(is_big())
131     bbox.set_size(31.8, 62.8);
132   else
133     bbox.set_size(31.8, 30.8);
134   adjust_height = 0;
135
136   dir = RIGHT;
137   old_dir = dir;
138   duck = false;
139   dead = false;
140
141   dying = false;
142   last_ground_y = 0;
143   fall_mode = ON_GROUND;
144   jumping = false;
145   can_jump = true;
146   butt_jump = false;
147   deactivated = false;
148   backflipping = false;
149   backflip_direction = 0;
150   visible = true;
151   
152   on_ground_flag = false;
153   grabbed_object = NULL;
154
155   floor_normal = Vector(0,-1);
156
157   physic.reset();
158 }
159
160 void
161 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
162 {
163   Scripting::Player* interface = static_cast<Scripting::Player*> (this);
164   Scripting::expose_object(vm, table_idx, interface, "Tux", false);
165 }
166
167 void
168 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
169 {
170   Scripting::unexpose_object(vm, table_idx, "Tux");
171 }
172
173 void
174 Player::set_controller(Controller* controller)
175 {
176   this->controller = controller;
177 }
178
179 void
180 Player::update(float elapsed_time)
181 {
182   if(dying && dying_timer.check()) {
183     dead = true;
184     return;
185   }
186
187   if(adjust_height != 0) {
188     bbox.move(Vector(0, bbox.get_height() - adjust_height));
189     bbox.set_height(adjust_height);
190     adjust_height = 0;
191   }
192
193   if(!dying && !deactivated)
194     handle_input();
195
196   movement = physic.get_movement(elapsed_time);
197
198   if(grabbed_object != NULL && !dying) {
199     Vector pos = get_pos() + 
200       Vector(dir == LEFT ? -16 : 16,
201              bbox.get_height()*0.66666 - 32);
202     grabbed_object->grab(*this, pos, dir);
203   }
204   
205   if(grabbed_object != NULL && dying){
206     grabbed_object->ungrab(*this, dir);
207     grabbed_object = NULL;
208   }
209
210   on_ground_flag = false;
211 }
212
213 bool
214 Player::on_ground()
215 {
216   return on_ground_flag;
217 }
218
219 bool
220 Player::is_big()
221 {
222   if(player_status->bonus == NO_BONUS)
223     return false;
224
225   return true;
226 }
227
228 void
229 Player::handle_horizontal_input()
230 {
231   float vx = physic.get_velocity_x();
232   float vy = physic.get_velocity_y();
233   float ax = physic.get_acceleration_x();
234   float ay = physic.get_acceleration_y();
235
236   float dirsign = 0;
237   if(!duck || physic.get_velocity_y() != 0) {
238     if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
239       old_dir = dir;
240       dir = LEFT;
241       dirsign = -1;
242     } else if(!controller->hold(Controller::LEFT)
243               && controller->hold(Controller::RIGHT)) {
244       old_dir = dir;
245       dir = RIGHT;
246       dirsign = 1;
247     }
248   }
249
250   if (!controller->hold(Controller::ACTION)) {
251     ax = dirsign * WALK_ACCELERATION_X;
252     // limit speed
253     if(vx >= MAX_WALK_XM && dirsign > 0) {
254       vx = MAX_WALK_XM;
255       ax = 0;
256     } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
257       vx = -MAX_WALK_XM;
258       ax = 0;
259     }
260   } else {
261     ax = dirsign * RUN_ACCELERATION_X;
262     // limit speed
263     if(vx >= MAX_RUN_XM && dirsign > 0) {
264       vx = MAX_RUN_XM;
265       ax = 0;
266     } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
267       vx = -MAX_RUN_XM;
268       ax = 0;
269     }
270   }
271
272   // we can reach WALK_SPEED without any acceleration
273   if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
274     vx = dirsign * WALK_SPEED;
275   }
276
277   // changing directions?
278   if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
279     // let's skid!
280     if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
281       skidding_timer.start(SKID_TIME);
282       sound_manager->play("sounds/skid.wav");
283       // dust some particles
284       Sector::current()->add_object(
285         new Particles(
286           Vector(dir == RIGHT ? bbox.p2.x : bbox.p1.x, bbox.p2.y),
287           dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
288           Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
289           LAYER_OBJECTS+1));
290       
291       ax *= 2.5;
292     } else {
293       ax *= 2;
294     }
295   }
296
297   // we get slower when not pressing any keys
298   if(dirsign == 0) {
299     if(fabs(vx) < WALK_SPEED) {
300       vx = 0;
301       ax = 0;
302     } else if(vx < 0) {
303       ax = WALK_ACCELERATION_X * 1.5;
304     } else {
305       ax = WALK_ACCELERATION_X * -1.5;
306     }
307   }
308
309 #if 0
310   // if we're on ice slow down acceleration or deceleration
311   if (isice(base.x, base.y + base.height))
312   {
313     /* the acceleration/deceleration rate on ice is inversely proportional to
314      * the current velocity.
315      */
316
317     // increasing 1 will increase acceleration/deceleration rate
318     // decreasing 1 will decrease acceleration/deceleration rate
319     //  must stay above zero, though
320     if (ax != 0) ax *= 1 / fabs(vx);
321   }
322 #endif
323
324   // extend/shrink tux collision rectangle so that we fall through/walk over 1
325   // tile holes
326   if(fabsf(vx) > MAX_WALK_XM) {
327     bbox.set_width(34);
328   } else {
329     bbox.set_width(31.8);
330   }
331
332   // on downward slopes, adjust vertical velocity to match slope angle
333   if (on_ground()) {
334     if (floor_normal.y != 0) {
335       if ((floor_normal.x * vx) > 0) {
336         // we overdo it a little, just to be on the safe side
337         vy = vx * (floor_normal.x / floor_normal.y) * 2;
338       }
339     }
340   }
341
342   physic.set_velocity(vx, vy);
343   physic.set_acceleration(ax, ay);
344 }
345
346 void
347 Player::handle_vertical_input()
348 {
349   // set fall mode...
350   if(on_ground()) {
351     fall_mode = ON_GROUND;
352     last_ground_y = get_pos().y;
353   } else {
354     if(get_pos().y > last_ground_y)
355       fall_mode = FALLING;
356     else if(fall_mode == ON_GROUND)
357       fall_mode = JUMPING;
358   }
359
360   if(on_ground()) { /* Make sure jumping is off. */
361     jumping = false;
362     if (backflipping) {
363       backflipping = false;
364       backflip_direction = 0;
365     }
366   }
367
368   // Press jump key
369   if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
370     if (duck) { 
371       if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
372         physic.set_velocity_y(300);
373       else { //do a backflip
374         backflipping = true;
375         physic.set_velocity_y(580);
376         backflip_timer.start(0.15);
377       }
378     }
379     else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
380       physic.set_velocity_y(580);
381     else
382       physic.set_velocity_y(520);
383     
384     //bbox.move(Vector(0, -1));
385     jumping = true;
386     can_jump = false;
387     if (is_big())
388       sound_manager->play("sounds/bigjump.wav");
389     else
390       sound_manager->play("sounds/jump.wav");
391   } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
392     if (!backflipping && jumping && physic.get_velocity_y() > 0) {
393       jumping = false;
394       physic.set_velocity_y(0);
395     }
396   }
397
398   /* In case the player has pressed Down while in a certain range of air,
399      enable butt jump action */
400   if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
401     //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
402     butt_jump = true;
403   
404   /* When Down is not held anymore, disable butt jump */
405   if(butt_jump && !controller->hold(Controller::DOWN))
406     butt_jump = false;
407   
408 #if 0
409   // Do butt jump
410   if (butt_jump && on_ground() && is_big()) {
411     // Add a smoke cloud
412     if (duck) 
413       Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
414     else 
415       Sector::current()->add_smoke_cloud(
416         Vector(get_pos().x - 32, get_pos().y + 32));
417     
418     butt_jump = false;
419     
420     // Break bricks beneath Tux
421     if(Sector::current()->trybreakbrick(
422          Vector(base.x + 1, base.y + base.height), false)
423        || Sector::current()->trybreakbrick(
424          Vector(base.x + base.width - 1, base.y + base.height), false)) {
425       physic.set_velocity_y(2);
426       butt_jump = true;
427     }
428     
429     // Kill nearby badguys
430     std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
431     for (std::vector<GameObject*>::iterator i = gameobjects.begin();
432          i != gameobjects.end();
433          i++) {
434       BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
435       if(badguy) {
436         // don't kill when badguys are already dying or in a certain mode
437         if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
438            badguy->mode != BadGuy::BOMB_EXPLODE) {
439           if (fabsf(base.x - badguy->base.x) < 96 &&
440               fabsf(base.y - badguy->base.y) < 64)
441             badguy->kill_me(25);
442         }
443       }
444     }
445   }
446 #endif
447
448   /** jumping is only allowed if we're about to touch ground soon and if the
449    * button has been up in between the last jump
450    */
451   // FIXME
452 #if 0
453   if ( (issolid(get_pos().x + bbox.get_width() / 2,
454           get_pos().y + bbox.get_height() + 64) ||
455         issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
456         issolid(get_pos().x + bbox.get_width() - 1,
457           get_pos().y + bbox.get_height() + 64))
458        && jumping  == false
459        && can_jump == false
460        && input.jump && !input.old_jump)
461     {
462       can_jump = true;
463     }
464 #endif
465 }
466
467 void
468 Player::handle_input()
469 {
470   if (ghost_mode) {
471     handle_input_ghost();
472     return;
473   }
474
475   if(!controller->hold(Controller::ACTION) && grabbed_object) {
476     // move the grabbed object a bit away from tux
477     Vector pos = get_pos() + 
478         Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
479                 bbox.get_height()*0.66666 - 32);
480     Rect dest(pos, pos + Vector(32, 32));
481     if(Sector::current()->is_free_space(dest)) {
482       MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
483       if(moving_object) {
484         moving_object->set_pos(pos);
485       } else {
486         log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
487       }
488       grabbed_object->ungrab(*this, dir);
489       grabbed_object = NULL;
490     }
491   }
492  
493   /* Handle horizontal movement: */
494   if (!backflipping) handle_horizontal_input();
495   else {
496     if (backflip_direction == 0) {
497       dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
498     }
499     else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping 
500     if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
501   }
502
503
504   /* Jump/jumping? */
505   if (on_ground() && !controller->hold(Controller::JUMP))
506     can_jump = true;
507   handle_vertical_input();
508
509   /* Shoot! */
510   if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
511     if(Sector::current()->add_bullet(
512          get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2) 
513                       : Vector(32, bbox.get_height()/2)),
514          physic.get_velocity_x(), dir))
515       shooting_timer.start(SHOOTING_TIME);
516   }
517   
518   /* Duck! */
519   if (controller->hold(Controller::DOWN) && is_big() && !duck 
520       && physic.get_velocity_y() == 0 && on_ground()) {
521     duck = true;
522     bbox.move(Vector(0, 32));
523     bbox.set_height(31.8);
524   } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
525     // if we have some velocity left then check if there is space for
526     // unducking
527     bbox.move(Vector(0, -32));
528     bbox.set_height(63.8);
529     if(Sector::current()->is_free_space(bbox) || (
530         physic.get_velocity_x() > -.01 && physic.get_velocity_x() < .01
531         && physic.get_velocity_y() > -.01 && physic.get_velocity_y() < .01))
532     {
533       duck = false;
534     } else {
535       // undo the ducking changes
536       bbox.move(Vector(0, 32));
537       bbox.set_height(31.8); 
538     }
539   }
540 }
541
542 void
543 Player::handle_input_ghost()
544 {
545   float vx = 0;
546   float vy = 0;
547   if (controller->hold(Controller::LEFT)) vx -= MAX_RUN_XM;
548   if (controller->hold(Controller::RIGHT)) vx += MAX_RUN_XM;
549   if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) vy += MAX_RUN_XM;
550   if (controller->hold(Controller::DOWN)) vy -= MAX_RUN_XM;
551   if (controller->hold(Controller::ACTION)) set_ghost_mode(false);
552   physic.set_velocity(vx, vy);
553   physic.set_acceleration(0, 0);
554 }
555
556 void
557 Player::add_coins(int count)
558 {
559   player_status->add_coins(count);
560 }
561
562 void
563 Player::add_bonus(const std::string& bonustype)
564 {
565   if(bonustype == "grow") {
566     add_bonus(GROWUP_BONUS);
567   } else if(bonustype == "fireflower") {
568     add_bonus(FIRE_BONUS);
569   } else if(bonustype == "iceflower") {
570     add_bonus(ICE_BONUS);
571   } else if(bonustype == "none") {
572     add_bonus(NO_BONUS);
573   } else {
574     std::ostringstream msg;
575     msg << "Unknown bonus type "  << bonustype;
576     throw std::runtime_error(msg.str());
577   }
578 }
579
580 void
581 Player::add_bonus(BonusType type, bool animate)
582 {
583   // always ignore NO_BONUS
584   if (type == NO_BONUS) {
585     return;
586   }
587
588   // ignore GROWUP_BONUS if we're already big
589   if (type == GROWUP_BONUS) {
590     if (player_status->bonus == GROWUP_BONUS) return; 
591     if (player_status->bonus == FIRE_BONUS) return;
592     if (player_status->bonus == ICE_BONUS) return;
593   }
594
595   set_bonus(type, animate);
596 }
597
598 void
599 Player::set_bonus(BonusType type, bool animate)
600 {
601   if(player_status->bonus == NO_BONUS) {
602     adjust_height = 62.8;
603     if(animate)
604       growing_timer.start(GROWING_TIME);
605   }
606
607   if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
608     player_status->max_fire_bullets = 0;
609     player_status->max_ice_bullets = 0;
610   }
611   if (type == FIRE_BONUS) player_status->max_fire_bullets++;
612   if (type == ICE_BONUS) player_status->max_ice_bullets++;
613
614   player_status->bonus = type;
615 }
616
617 void
618 Player::set_visible(bool visible)
619 {
620   this->visible = visible;
621 }
622
623 bool
624 Player::get_visible()
625 {
626   return visible;
627 }
628
629 void
630 Player::kick()
631 {
632   kick_timer.start(KICK_TIME);
633 }
634
635 void
636 Player::draw(DrawingContext& context)
637 {
638   if(!visible)
639     return;
640
641   TuxBodyParts* tux_body;
642           
643   if (player_status->bonus == GROWUP_BONUS)
644     tux_body = big_tux;
645   else if (player_status->bonus == FIRE_BONUS)
646     tux_body = fire_tux;
647   else if (player_status->bonus == ICE_BONUS)
648     tux_body = ice_tux;
649   else
650     tux_body = small_tux;
651
652   int layer = LAYER_OBJECTS + 1;
653
654   /* Set Tux sprite action */
655   if (duck && is_big())
656     {
657     if(dir == LEFT)
658       tux_body->set_action("duck-left");
659     else // dir == RIGHT
660       tux_body->set_action("duck-right");
661     }
662   else if (skidding_timer.started() && !skidding_timer.check())
663     {
664     if(dir == LEFT)
665       tux_body->set_action("skid-left");
666     else // dir == RIGHT
667       tux_body->set_action("skid-right");
668     }
669   else if (kick_timer.started() && !kick_timer.check())
670     {
671     if(dir == LEFT)
672       tux_body->set_action("kick-left");
673     else // dir == RIGHT
674       tux_body->set_action("kick-right");
675     }
676   else if (butt_jump && is_big())
677     {
678     if(dir == LEFT)
679       tux_body->set_action("buttjump-left");
680     else // dir == RIGHT
681       tux_body->set_action("buttjump-right");
682     }
683   else if (!on_ground())
684     {
685     if(dir == LEFT)
686       tux_body->set_action("jump-left");
687     else // dir == RIGHT
688       tux_body->set_action("jump-right");
689     }
690   else
691     {
692     if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
693       {
694       if(dir == LEFT)
695         tux_body->set_action("stand-left");
696       else // dir == RIGHT
697         tux_body->set_action("stand-right");
698       }
699     else // moving
700       {
701       if(dir == LEFT)
702         tux_body->set_action("walk-left");
703       else // dir == RIGHT
704         tux_body->set_action("walk-right");
705       }
706     }
707
708   if(idle_timer.check())
709     {
710     if(is_big())
711       {
712       if(dir == LEFT)
713         tux_body->head->set_action("idle-left", 1);
714       else // dir == RIGHT
715         tux_body->head->set_action("idle-right", 1);
716       }
717
718     }
719
720   // Tux is holding something
721   if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
722       (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
723     {
724     if (duck)
725       {
726       if(dir == LEFT)
727         tux_body->arms->set_action("duck+grab-left");
728       else // dir == RIGHT
729         tux_body->arms->set_action("duck+grab-right");
730       }
731     else
732       {
733       if(dir == LEFT)
734         tux_body->arms->set_action("grab-left");
735       else // dir == RIGHT
736         tux_body->arms->set_action("grab-right");
737       }
738     }
739
740   /* Draw Tux */
741   if(dying) {
742     smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
743   } else if(growing_timer.get_timeleft() > 0) {
744       if (dir == RIGHT) {
745         context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
746                  GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
747       } else {
748         context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
749                 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
750       }
751     }
752   else if (safe_timer.started() && size_t(game_time*40)%2)
753     ;  // don't draw Tux
754   else
755     tux_body->draw(context, get_pos(), layer);
756
757   // Draw blinking star overlay
758   if (invincible_timer.started() &&
759      (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
760       || size_t(game_time*20)%2)
761      && !dying)
762   {
763     if (!is_big() || duck)
764       smalltux_star->draw(context, get_pos(), layer + 5);
765     else
766       bigtux_star->draw(context, get_pos(), layer + 5);
767   } 
768 }
769
770 void
771 Player::collision_tile(uint32_t tile_attributes)
772 {
773   if(tile_attributes & Tile::HURTS)
774     kill(false);
775 }
776
777 HitResponse
778 Player::collision(GameObject& other, const CollisionHit& hit)
779 {
780   Bullet* bullet = dynamic_cast<Bullet*> (&other);
781   if(bullet) {
782     return FORCE_MOVE;
783   }
784
785   if(other.get_flags() & FLAG_PORTABLE) {
786     Portable* portable = dynamic_cast<Portable*> (&other);
787     assert(portable != NULL);
788     if(portable && grabbed_object == NULL
789         && controller->hold(Controller::ACTION)
790         && fabsf(hit.normal.x) > .9) {
791       grabbed_object = portable;
792       grabbed_object->grab(*this, get_pos(), dir);
793       return CONTINUE;
794     }
795   }
796  
797   if(other.get_flags() & FLAG_SOLID) {
798     /*
799     printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
800         &other,
801         hit.normal.x, hit.normal.y, hit.depth,
802         get_pos().x, get_pos().y,
803         movement.x, movement.y);
804     */
805     
806     if(hit.normal.y < 0) { // landed on floor?
807       if(physic.get_velocity_y() < 0)
808         physic.set_velocity_y(0);
809
810       on_ground_flag = true;
811
812       // remember normal of this tile
813       if (hit.normal.y > -0.9) {
814         floor_normal.x = hit.normal.x;
815         floor_normal.y = hit.normal.y;
816       } else {
817         // slowly adjust to unisolid tiles. 
818         // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
819         floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
820         floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
821       }
822
823       // hack platforms so that we stand normally on them when going down...
824       Platform* platform = dynamic_cast<Platform*> (&other);
825       if(platform != NULL) {
826         if(platform->get_speed().y > 0)
827           physic.set_velocity_y(-platform->get_speed().y);
828         //physic.set_velocity_x(platform->get_speed().x);
829       }
830     } else if(hit.normal.y > 0) { // bumped against the roof
831       physic.set_velocity_y(.1);
832
833       // hack platform so that we are not glued to it from below
834       Platform* platform = dynamic_cast<Platform*> (&other);
835       if(platform != NULL) {
836         physic.set_velocity_y(-platform->get_speed().y);
837       }      
838     }
839     
840     if(fabsf(hit.normal.x) > .9) { // hit on the side?
841       physic.set_velocity_x(0);
842     }
843
844     MovingObject* omov = dynamic_cast<MovingObject*> (&other);
845     if(omov != NULL) {
846       Vector mov = movement - omov->get_movement();
847       /*
848       printf("W %p - HITN: %3.1f %3.1f D:%3.1f TM: %3.1f %3.1f TD: %3.1f %3.1f PM: %3.2f %3.1f\n",
849           omov,
850           hit.normal.x, hit.normal.y,
851           hit.depth,
852           movement.x, movement.y,
853           dest.p1.x, dest.p1.y,
854           omov->get_movement().x, omov->get_movement().y);
855       */
856     }
857     
858     return CONTINUE;
859   }
860
861 #ifdef DEBUG
862   assert(dynamic_cast<MovingObject*> (&other) != NULL);
863 #endif
864   MovingObject* moving_object = static_cast<MovingObject*> (&other); 
865   if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
866     TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
867     if(trigger) {
868       if(controller->pressed(Controller::UP))
869         trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
870     }
871
872     return FORCE_MOVE;
873   }
874
875   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
876   if(badguy != NULL) {
877     if(safe_timer.started() || invincible_timer.started())
878       return FORCE_MOVE;
879
880     return CONTINUE;
881   }
882
883   return FORCE_MOVE;
884 }
885
886 void
887 Player::make_invincible()
888 {
889   sound_manager->play("sounds/invincible.wav");
890   invincible_timer.start(TUX_INVINCIBLE_TIME);
891   Sector::current()->play_music(HERRING_MUSIC);               
892 }
893
894 /* Kill Player! */
895 void
896 Player::kill(bool completely)
897 {
898   if(dying || deactivated)
899     return;
900
901   if(!completely && safe_timer.started() || invincible_timer.started())
902     return;                          
903   
904   sound_manager->play("sounds/hurt.wav");
905
906   physic.set_velocity_x(0);
907
908   if(!completely && is_big()) {
909     if(player_status->bonus == FIRE_BONUS
910         || player_status->bonus == ICE_BONUS) {
911       safe_timer.start(TUX_SAFE_TIME);
912       set_bonus(GROWUP_BONUS);
913     } else {
914       //growing_timer.start(GROWING_TIME);
915       safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
916       adjust_height = 30.8;
917       duck = false;
918       set_bonus(NO_BONUS);
919     }
920   } else {
921     for (int i = 0; (i < 5) && (i < player_status->coins); i++)
922     {
923       // the numbers: starting x, starting y, velocity y
924       Sector::current()->add_object(new FallingCoin(get_pos() + 
925             Vector(systemRandom.rand(5), systemRandom.rand(-32,18)), 
926             systemRandom.rand(-100,100)));
927     }
928     physic.enable_gravity(true);
929     physic.set_acceleration(0, 0);
930     physic.set_velocity(0, 700);
931     player_status->coins -= 25;
932     set_bonus(NO_BONUS);
933     dying = true;
934     dying_timer.start(3.0);
935     set_group(COLGROUP_DISABLED);
936
937     DisplayEffect* effect = new DisplayEffect();
938     effect->fade_out(3.0);
939     Sector::current()->add_object(effect);
940     sound_manager->stop_music(3.0);
941   }
942 }
943
944 void
945 Player::move(const Vector& vector)
946 {
947   bbox.set_pos(vector);
948   if(is_big())
949     bbox.set_size(31.8, 63.8);
950   else
951     bbox.set_size(31.8, 31.8);
952   duck = false;
953   last_ground_y = vector.y;
954
955   physic.reset();
956 }
957
958 void
959 Player::check_bounds(Camera* camera)
960 {
961   /* Keep tux in bounds: */
962   if (get_pos().x < 0) {
963     // Lock Tux to the size of the level, so that he doesn't fall of
964     // on the left side
965     bbox.set_pos(Vector(0, get_pos().y));
966   }
967
968   /* Keep in-bounds, vertically: */
969   if (get_pos().y > Sector::current()->solids->get_height() * 32) {
970     kill(true);
971     return;
972   }
973
974   bool adjust = false;
975   // can happen if back scrolling is disabled
976   if(get_pos().x < camera->get_translation().x) {
977     bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
978     adjust = true;
979   }
980   if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
981   {
982     bbox.set_pos(Vector(
983           camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
984           get_pos().y));
985     adjust = true;
986   }
987
988   if(adjust) {
989     // FIXME
990 #if 0
991     // squished now?
992     if(collision_object_map(bbox)) {
993       kill(KILL);
994       return;
995     }
996 #endif
997   }
998 }
999
1000 void
1001 Player::add_velocity(const Vector& velocity)
1002 {
1003   physic.set_velocity(physic.get_velocity() + velocity);
1004 }
1005
1006 void
1007 Player::bounce(BadGuy& )
1008 {
1009   if(controller->hold(Controller::JUMP))
1010     physic.set_velocity_y(520);
1011   else
1012     physic.set_velocity_y(300);
1013 }
1014
1015 //Scripting Functions Below
1016
1017 void
1018 Player::deactivate()
1019 {
1020   deactivated = true;
1021   physic.set_velocity_x(0);
1022   physic.set_velocity_y(0);
1023   physic.set_acceleration_x(0);
1024   physic.set_acceleration_y(0);
1025 }
1026
1027 void
1028 Player::activate()
1029 {
1030   deactivated = false;
1031 }
1032
1033 void Player::walk(float speed)
1034 {
1035   physic.set_velocity_x(speed);
1036 }
1037
1038 void
1039 Player::set_ghost_mode(bool enable)
1040 {
1041   if (ghost_mode == enable) return;
1042   if (enable) {
1043     ghost_mode = true;
1044     set_group(COLGROUP_DISABLED);
1045     physic.enable_gravity(false);
1046     log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1047   } else {
1048     ghost_mode = false;
1049     set_group(COLGROUP_MOVING);
1050     physic.enable_gravity(true);
1051     log_debug << "You feel solid again." << std::endl;
1052   }
1053 }
1054