fix mriceblock grabbing
[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)
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(!controller->hold(Controller::ACTION) && grabbed_object) {
471     // move the grabbed object a bit away from tux
472     Vector pos = get_pos() + 
473         Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
474                 bbox.get_height()*0.66666 - 32);
475     Rect dest(pos, pos + Vector(32, 32));
476     if(Sector::current()->is_free_space(dest)) {
477       MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
478       if(moving_object) {
479         moving_object->set_pos(pos);
480       } else {
481         log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
482       }
483       grabbed_object->ungrab(*this, dir);
484       grabbed_object = NULL;
485     }
486   }
487  
488   /* Handle horizontal movement: */
489   if (!backflipping) handle_horizontal_input();
490   else {
491     if (backflip_direction == 0) {
492       dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
493     }
494     else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping 
495     if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
496   }
497
498
499   /* Jump/jumping? */
500   if (on_ground() && !controller->hold(Controller::JUMP))
501     can_jump = true;
502   handle_vertical_input();
503
504   /* Shoot! */
505   if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
506     if(Sector::current()->add_bullet(
507          get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2) 
508                       : Vector(32, bbox.get_height()/2)),
509          physic.get_velocity_x(), dir))
510       shooting_timer.start(SHOOTING_TIME);
511   }
512   
513   /* Duck! */
514   if (controller->hold(Controller::DOWN) && is_big() && !duck 
515       && physic.get_velocity_y() == 0 && on_ground()) {
516     duck = true;
517     bbox.move(Vector(0, 32));
518     bbox.set_height(31.8);
519   } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
520     // if we have some velocity left then check if there is space for
521     // unducking
522     bbox.move(Vector(0, -32));
523     bbox.set_height(63.8);
524     if(Sector::current()->is_free_space(bbox) || (
525         physic.get_velocity_x() > -.01 && physic.get_velocity_x() < .01
526         && physic.get_velocity_y() > -.01 && physic.get_velocity_y() < .01))
527     {
528       duck = false;
529     } else {
530       // undo the ducking changes
531       bbox.move(Vector(0, 32));
532       bbox.set_height(31.8); 
533     }
534   }
535 }
536
537 void
538 Player::add_coins(int count)
539 {
540   player_status->add_coins(count);
541 }
542
543 void
544 Player::add_bonus(const std::string& bonustype)
545 {
546   if(bonustype == "grow") {
547     add_bonus(GROWUP_BONUS);
548   } else if(bonustype == "fireflower") {
549     add_bonus(FIRE_BONUS);
550   } else if(bonustype == "iceflower") {
551     add_bonus(ICE_BONUS);
552   } else if(bonustype == "none") {
553     add_bonus(NO_BONUS);
554   } else {
555     std::ostringstream msg;
556     msg << "Unknown bonus type "  << bonustype;
557     throw std::runtime_error(msg.str());
558   }
559 }
560
561 void
562 Player::add_bonus(BonusType type, bool animate)
563 {
564   // always ignore NO_BONUS
565   if (type == NO_BONUS) {
566     return;
567   }
568
569   // ignore GROWUP_BONUS if we're already big
570   if (type == GROWUP_BONUS) {
571     if (player_status->bonus == GROWUP_BONUS) return; 
572     if (player_status->bonus == FIRE_BONUS) return;
573     if (player_status->bonus == ICE_BONUS) return;
574   }
575
576   set_bonus(type, animate);
577 }
578
579 void
580 Player::set_bonus(BonusType type, bool animate)
581 {
582   if(player_status->bonus == NO_BONUS) {
583     adjust_height = 62.8;
584     if(animate)
585       growing_timer.start(GROWING_TIME);
586   }
587
588   if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
589     player_status->max_fire_bullets = 0;
590     player_status->max_ice_bullets = 0;
591   }
592   if (type == FIRE_BONUS) player_status->max_fire_bullets++;
593   if (type == ICE_BONUS) player_status->max_ice_bullets++;
594
595   player_status->bonus = type;
596 }
597
598 void
599 Player::set_visible(bool visible)
600 {
601   this->visible = visible;
602 }
603
604 bool
605 Player::get_visible()
606 {
607   return visible;
608 }
609
610 void
611 Player::kick()
612 {
613   kick_timer.start(KICK_TIME);
614 }
615
616 void
617 Player::draw(DrawingContext& context)
618 {
619   if(!visible)
620     return;
621
622   TuxBodyParts* tux_body;
623           
624   if (player_status->bonus == GROWUP_BONUS)
625     tux_body = big_tux;
626   else if (player_status->bonus == FIRE_BONUS)
627     tux_body = fire_tux;
628   else if (player_status->bonus == ICE_BONUS)
629     tux_body = ice_tux;
630   else
631     tux_body = small_tux;
632
633   int layer = LAYER_OBJECTS + 1;
634
635   /* Set Tux sprite action */
636   if (duck && is_big())
637     {
638     if(dir == LEFT)
639       tux_body->set_action("duck-left");
640     else // dir == RIGHT
641       tux_body->set_action("duck-right");
642     }
643   else if (skidding_timer.started() && !skidding_timer.check())
644     {
645     if(dir == LEFT)
646       tux_body->set_action("skid-left");
647     else // dir == RIGHT
648       tux_body->set_action("skid-right");
649     }
650   else if (kick_timer.started() && !kick_timer.check())
651     {
652     if(dir == LEFT)
653       tux_body->set_action("kick-left");
654     else // dir == RIGHT
655       tux_body->set_action("kick-right");
656     }
657   else if (butt_jump && is_big())
658     {
659     if(dir == LEFT)
660       tux_body->set_action("buttjump-left");
661     else // dir == RIGHT
662       tux_body->set_action("buttjump-right");
663     }
664   else if (!on_ground())
665     {
666     if(dir == LEFT)
667       tux_body->set_action("jump-left");
668     else // dir == RIGHT
669       tux_body->set_action("jump-right");
670     }
671   else
672     {
673     if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
674       {
675       if(dir == LEFT)
676         tux_body->set_action("stand-left");
677       else // dir == RIGHT
678         tux_body->set_action("stand-right");
679       }
680     else // moving
681       {
682       if(dir == LEFT)
683         tux_body->set_action("walk-left");
684       else // dir == RIGHT
685         tux_body->set_action("walk-right");
686       }
687     }
688
689   if(idle_timer.check())
690     {
691     if(is_big())
692       {
693       if(dir == LEFT)
694         tux_body->head->set_action("idle-left", 1);
695       else // dir == RIGHT
696         tux_body->head->set_action("idle-right", 1);
697       }
698
699     }
700
701   // Tux is holding something
702   if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
703       (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
704     {
705     if (duck)
706       {
707       if(dir == LEFT)
708         tux_body->arms->set_action("duck+grab-left");
709       else // dir == RIGHT
710         tux_body->arms->set_action("duck+grab-right");
711       }
712     else
713       {
714       if(dir == LEFT)
715         tux_body->arms->set_action("grab-left");
716       else // dir == RIGHT
717         tux_body->arms->set_action("grab-right");
718       }
719     }
720
721   /* Draw Tux */
722   if(dying) {
723     smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
724   } else if(growing_timer.get_timeleft() > 0) {
725       if (dir == RIGHT) {
726         context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
727                  GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
728       } else {
729         context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
730                 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
731       }
732     }
733   else if (safe_timer.started() && size_t(game_time*40)%2)
734     ;  // don't draw Tux
735   else
736     tux_body->draw(context, get_pos(), layer);
737
738   // Draw blinking star overlay
739   if (invincible_timer.started() &&
740      (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
741       || size_t(game_time*20)%2)
742      && !dying)
743   {
744     if (!is_big() || duck)
745       smalltux_star->draw(context, get_pos(), layer + 5);
746     else
747       bigtux_star->draw(context, get_pos(), layer + 5);
748   } 
749 }
750
751 void
752 Player::collision_tile(uint32_t tile_attributes)
753 {
754   if(tile_attributes & Tile::HURTS)
755     kill(false);
756 }
757
758 HitResponse
759 Player::collision(GameObject& other, const CollisionHit& hit)
760 {
761   Bullet* bullet = dynamic_cast<Bullet*> (&other);
762   if(bullet) {
763     return FORCE_MOVE;
764   }
765
766   if(other.get_flags() & FLAG_PORTABLE) {
767     Portable* portable = dynamic_cast<Portable*> (&other);
768     assert(portable != NULL);
769     if(portable && grabbed_object == NULL
770         && controller->hold(Controller::ACTION)
771         && fabsf(hit.normal.x) > .9) {
772       grabbed_object = portable;
773       grabbed_object->grab(*this, get_pos(), dir);
774       return CONTINUE;
775     }
776   }
777  
778   if(other.get_flags() & FLAG_SOLID) {
779     /*
780     printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
781         &other,
782         hit.normal.x, hit.normal.y, hit.depth,
783         get_pos().x, get_pos().y,
784         movement.x, movement.y);
785     */
786     
787     if(hit.normal.y < 0) { // landed on floor?
788       if(physic.get_velocity_y() < 0)
789         physic.set_velocity_y(0);
790
791       on_ground_flag = true;
792
793       // remember normal of this tile
794       if (hit.normal.y > -0.9) {
795         floor_normal.x = hit.normal.x;
796         floor_normal.y = hit.normal.y;
797       } else {
798         // slowly adjust to unisolid tiles. 
799         // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
800         floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
801         floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
802       }
803
804       // hack platforms so that we stand normally on them when going down...
805       Platform* platform = dynamic_cast<Platform*> (&other);
806       if(platform != NULL) {
807         if(platform->get_speed().y > 0)
808           physic.set_velocity_y(-platform->get_speed().y);
809         //physic.set_velocity_x(platform->get_speed().x);
810       }
811     } else if(hit.normal.y > 0) { // bumped against the roof
812       physic.set_velocity_y(.1);
813
814       // hack platform so that we are not glued to it from below
815       Platform* platform = dynamic_cast<Platform*> (&other);
816       if(platform != NULL) {
817         physic.set_velocity_y(-platform->get_speed().y);
818       }      
819     }
820     
821     if(fabsf(hit.normal.x) > .9) { // hit on the side?
822       physic.set_velocity_x(0);
823     }
824
825     MovingObject* omov = dynamic_cast<MovingObject*> (&other);
826     if(omov != NULL) {
827       Vector mov = movement - omov->get_movement();
828       /*
829       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",
830           omov,
831           hit.normal.x, hit.normal.y,
832           hit.depth,
833           movement.x, movement.y,
834           dest.p1.x, dest.p1.y,
835           omov->get_movement().x, omov->get_movement().y);
836       */
837     }
838     
839     return CONTINUE;
840   }
841
842 #ifdef DEBUG
843   assert(dynamic_cast<MovingObject*> (&other) != NULL);
844 #endif
845   MovingObject* moving_object = static_cast<MovingObject*> (&other); 
846   if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
847     TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
848     if(trigger) {
849       if(controller->pressed(Controller::UP))
850         trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
851     }
852
853     return FORCE_MOVE;
854   }
855
856   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
857   if(badguy != NULL) {
858     if(safe_timer.started())
859       return FORCE_MOVE;
860
861     return CONTINUE;
862   }
863
864   return FORCE_MOVE;
865 }
866
867 void
868 Player::make_invincible()
869 {
870   sound_manager->play("sounds/invincible.wav");
871   invincible_timer.start(TUX_INVINCIBLE_TIME);
872   Sector::current()->play_music(HERRING_MUSIC);               
873 }
874
875 /* Kill Player! */
876 void
877 Player::kill(bool completely)
878 {
879   if(dying || deactivated)
880     return;
881
882   if(!completely && 
883       (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
884     return;                          
885   
886   sound_manager->play("sounds/hurt.wav");
887
888   physic.set_velocity_x(0);
889
890   if(!completely && is_big()) {
891     if(player_status->bonus == FIRE_BONUS
892         || player_status->bonus == ICE_BONUS) {
893       safe_timer.start(TUX_SAFE_TIME);
894       set_bonus(GROWUP_BONUS);
895     } else {
896       //growing_timer.start(GROWING_TIME);
897       safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
898       adjust_height = 30.8;
899       duck = false;
900       set_bonus(NO_BONUS);
901     }
902   } else {
903     for (int i = 0; (i < 5) && (i < player_status->coins); i++)
904     {
905       // the numbers: starting x, starting y, velocity y
906       Sector::current()->add_object(new FallingCoin(get_pos() + 
907             Vector(systemRandom.rand(5), systemRandom.rand(-32,18)), 
908             systemRandom.rand(-100,100)));
909     }
910     physic.enable_gravity(true);
911     physic.set_acceleration(0, 0);
912     physic.set_velocity(0, 700);
913     player_status->coins -= 25;
914     set_bonus(NO_BONUS);
915     dying = true;
916     dying_timer.start(3.0);
917     set_group(COLGROUP_DISABLED);
918
919     DisplayEffect* effect = new DisplayEffect();
920     effect->fade_out(3.0);
921     Sector::current()->add_object(effect);
922     sound_manager->stop_music(3.0);
923   }
924 }
925
926 void
927 Player::move(const Vector& vector)
928 {
929   bbox.set_pos(vector);
930   if(is_big())
931     bbox.set_size(31.8, 63.8);
932   else
933     bbox.set_size(31.8, 31.8);
934   duck = false;
935   last_ground_y = vector.y;
936
937   physic.reset();
938 }
939
940 void
941 Player::check_bounds(Camera* camera)
942 {
943   /* Keep tux in bounds: */
944   if (get_pos().x < 0) {
945     // Lock Tux to the size of the level, so that he doesn't fall of
946     // on the left side
947     bbox.set_pos(Vector(0, get_pos().y));
948   }
949
950   /* Keep in-bounds, vertically: */
951   if (get_pos().y > Sector::current()->solids->get_height() * 32) {
952     kill(true);
953     return;
954   }
955
956   bool adjust = false;
957   // can happen if back scrolling is disabled
958   if(get_pos().x < camera->get_translation().x) {
959     bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
960     adjust = true;
961   }
962   if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
963   {
964     bbox.set_pos(Vector(
965           camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
966           get_pos().y));
967     adjust = true;
968   }
969
970   if(adjust) {
971     // FIXME
972 #if 0
973     // squished now?
974     if(collision_object_map(bbox)) {
975       kill(KILL);
976       return;
977     }
978 #endif
979   }
980 }
981
982 void
983 Player::add_velocity(const Vector& velocity)
984 {
985   physic.set_velocity(physic.get_velocity() + velocity);
986 }
987
988 void
989 Player::bounce(BadGuy& )
990 {
991   if(controller->hold(Controller::JUMP))
992     physic.set_velocity_y(520);
993   else
994     physic.set_velocity_y(300);
995 }
996
997 //Scripting Functions Below
998
999 void
1000 Player::deactivate()
1001 {
1002   deactivated = true;
1003   physic.set_velocity_x(0);
1004   physic.set_velocity_y(0);
1005   physic.set_acceleration_x(0);
1006   physic.set_acceleration_y(0);
1007 }
1008
1009 void
1010 Player::activate()
1011 {
1012   deactivated = false;
1013 }
1014
1015 void Player::walk(float speed)
1016 {
1017   physic.set_velocity_x(speed);
1018 }
1019