4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
26 #include "gettext.hpp"
27 #include "sprite/sprite_manager.hpp"
28 #include "audio/sound_manager.hpp"
31 #include "sprite/sprite.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"
45 #include "platform.hpp"
46 #include "badguy/badguy.hpp"
47 #include "player_status.hpp"
49 #include "falling_coin.hpp"
50 #include "random_generator.hpp"
51 #include "object/sprite_particle.hpp"
52 #include "trigger/climbable.hpp"
54 static const int TILES_FOR_BUTTJUMP = 3;
55 static const float SHOOTING_TIME = .150f;
56 /// time before idle animation starts
57 static const float IDLE_TIME = 2.5f;
59 /** acceleration in horizontal direction when walking
60 * (all acceleratiosn are in pixel/s^2) */
61 static const float WALK_ACCELERATION_X = 300;
62 /** acceleration in horizontal direction when running */
63 static const float RUN_ACCELERATION_X = 400;
64 /** acceleration when skidding */
65 static const float SKID_XM = 200;
66 /** time of skidding in seconds */
67 static const float SKID_TIME = .3f;
68 /** maximum walk velocity (pixel/s) */
69 static const float MAX_WALK_XM = 230;
70 /** maximum run velcoity (pixel/s) */
71 static const float MAX_RUN_XM = 320;
72 /** maximum horizontal climb velocity */
73 static const float MAX_CLIMB_XM = 48;
74 /** maximum vertical climb velocity */
75 static const float MAX_CLIMB_YM = 128;
76 /** instant velocity when tux starts to walk */
77 static const float WALK_SPEED = 100;
79 /** time of the kick (kicking mriceblock) animation */
80 static const float KICK_TIME = .3f;
81 /** time of tux cheering (currently unused) */
82 static const float CHEER_TIME = 1.0f;
84 /** if Tux cannot unduck for this long, he will get hurt */
85 static const float UNDUCK_HURT_TIME = 0.25f;
88 Surface* growingtux_left[GROWING_FRAMES];
89 Surface* growingtux_right[GROWING_FRAMES];
91 Surface* tux_life = 0;
93 TuxBodyParts* small_tux = 0;
94 TuxBodyParts* big_tux = 0;
95 TuxBodyParts* fire_tux = 0;
96 TuxBodyParts* ice_tux = 0;
102 TuxBodyParts::set_action(std::string action, int loops)
105 head->set_action(action, loops);
107 body->set_action(action, loops);
109 arms->set_action(action, loops);
111 feet->set_action(action, loops);
115 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
118 head->draw(context, pos, layer-1);
120 body->draw(context, pos, layer-3);
122 arms->draw(context, pos, layer+10);
124 feet->draw(context, pos, layer-2);
127 Player::Player(PlayerStatus* _player_status, const std::string& name)
128 : player_status(_player_status), grabbed_object(NULL), ghost_mode(false), climbing(0)
131 controller = main_controller;
132 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
133 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
134 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
135 airarrow.reset(new Surface("images/engine/hud/airarrow.png"));
137 sound_manager->preload("sounds/bigjump.wav");
138 sound_manager->preload("sounds/jump.wav");
139 sound_manager->preload("sounds/hurt.wav");
140 sound_manager->preload("sounds/skid.wav");
141 sound_manager->preload("sounds/flip.wav");
142 sound_manager->preload("sounds/invincible.wav");
143 sound_manager->preload("sounds/splash.ogg");
150 if (climbing) stop_climbing(*climbing);
151 delete smalltux_gameover;
152 delete smalltux_star;
160 set_size(31.8f, 62.8f);
162 set_size(31.8f, 30.8f);
172 fall_mode = ON_GROUND;
177 backflipping = false;
178 backflip_direction = 0;
181 speedlimit = 0; //no special limit
183 on_ground_flag = false;
184 grabbed_object = NULL;
192 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
197 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
201 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
206 Scripting::unexpose_object(vm, table_idx, name);
210 Player::get_speedlimit()
216 Player::set_speedlimit(float newlimit)
222 Player::set_controller(Controller* controller)
224 this->controller = controller;
228 Player::adjust_height(float new_height)
231 bbox2.move(Vector(0, bbox.get_height() - new_height));
232 bbox2.set_height(new_height);
234 if(new_height > bbox.get_height()) {
235 Rect additional_space = bbox2;
236 additional_space.set_height(new_height - bbox.get_height());
237 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
241 // adjust bbox accordingly
242 // note that we use members of moving_object for this, so we can run this during CD, too
244 set_size(bbox2.get_width(), bbox2.get_height());
249 Player::trigger_sequence(std::string sequence_name)
251 if (climbing) stop_climbing(*climbing);
252 GameSession::current()->start_sequence(sequence_name);
256 Player::update(float elapsed_time)
263 if(dying && dying_timer.check()) {
268 if(!dying && !deactivated)
271 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
275 // extend/shrink tux collision rectangle so that we fall through/walk over 1
277 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
283 // on downward slopes, adjust vertical velocity so tux walks smoothly down
285 if(floor_normal.y != 0) {
286 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
287 physic.set_velocity_y(250);
292 // handle backflipping
294 //prevent player from changing direction when backflipping
295 dir = (backflip_direction == 1) ? LEFT : RIGHT;
296 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
301 fall_mode = ON_GROUND;
302 last_ground_y = get_pos().y;
304 if(get_pos().y > last_ground_y)
306 else if(fall_mode == ON_GROUND)
310 // check if we landed
313 if (backflipping && (!backflip_timer.started())) {
314 backflipping = false;
315 backflip_direction = 0;
317 // if controls are currently deactivated, we take care of standing up ourselves
323 // calculate movement for this frame
324 movement = physic.get_movement(elapsed_time);
326 if(grabbed_object != NULL && !dying) {
327 Vector pos = get_pos() +
328 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
329 grabbed_object->grab(*this, pos, dir);
332 if(grabbed_object != NULL && dying){
333 grabbed_object->ungrab(*this, dir);
334 grabbed_object = NULL;
337 on_ground_flag = false;
339 // when invincible, spawn particles
340 if (invincible_timer.started() && !dying)
342 if (systemRandom.rand(0, 2) == 0) {
343 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
344 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
345 Vector ppos = Vector(px, py);
346 Vector pspeed = Vector(0, 0);
347 Vector paccel = Vector(0, 0);
348 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
349 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
350 // make every other a longer sparkle to make trail a bit fuzzy
351 if (size_t(game_time*20)%2) {
352 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
354 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
357 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
367 return on_ground_flag;
373 if(player_status->bonus == NO_BONUS)
380 Player::apply_friction()
382 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
383 physic.set_velocity_x(0);
384 physic.set_acceleration_x(0);
385 } else if(physic.get_velocity_x() < 0) {
386 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
387 } else if(physic.get_velocity_x() > 0) {
388 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
393 Player::handle_horizontal_input()
395 float vx = physic.get_velocity_x();
396 float vy = physic.get_velocity_y();
397 float ax = physic.get_acceleration_x();
398 float ay = physic.get_acceleration_y();
401 if(!duck || physic.get_velocity_y() != 0) {
402 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
406 } else if(!controller->hold(Controller::LEFT)
407 && controller->hold(Controller::RIGHT)) {
414 // do not run if action key is pressed or we're holding something
415 // so tux can only walk while shooting
416 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
417 ax = dirsign * WALK_ACCELERATION_X;
419 if(vx >= MAX_WALK_XM && dirsign > 0) {
422 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
427 if( vx * dirsign < MAX_WALK_XM ) {
428 ax = dirsign * WALK_ACCELERATION_X;
430 ax = dirsign * RUN_ACCELERATION_X;
433 if(vx >= MAX_RUN_XM && dirsign > 0) {
436 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
442 // we can reach WALK_SPEED without any acceleration
443 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
444 vx = dirsign * WALK_SPEED;
448 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
449 vx = dirsign * speedlimit;
453 // changing directions?
454 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
456 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
457 skidding_timer.start(SKID_TIME);
458 sound_manager->play("sounds/skid.wav");
459 // dust some particles
460 Sector::current()->add_object(
462 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
463 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
464 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
473 physic.set_velocity(vx, vy);
474 physic.set_acceleration(ax, ay);
476 // we get slower when not pressing any keys
498 if (physic.get_velocity_y() != 0)
503 if (adjust_height(31.8f)) {
505 unduck_hurt_timer.stop();
512 Player::do_standup() {
520 if (adjust_height(63.8f)) {
522 unduck_hurt_timer.stop();
524 // if timer is not already running, start it.
525 if (unduck_hurt_timer.get_period() == 0) {
526 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
528 else if (unduck_hurt_timer.check()) {
536 Player::do_backflip() {
542 // TODO: we don't have an animation for firetux backflipping, so let's revert to bigtux
543 set_bonus(GROWUP_BONUS, true);
545 backflip_direction = (dir == LEFT)?(+1):(-1);
548 sound_manager->play("sounds/flip.wav");
549 backflip_timer.start(0.15f);
553 Player::do_jump(float yspeed) {
557 physic.set_velocity_y(yspeed);
558 //bbox.move(Vector(0, -1));
560 on_ground_flag = false;
565 sound_manager->play("sounds/bigjump.wav");
567 sound_manager->play("sounds/jump.wav");
572 Player::handle_vertical_input()
575 if(controller->pressed(Controller::JUMP) && (can_jump)) {
577 // when running, only jump a little bit; else do a backflip
578 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
580 // jump a bit higher if we are running; else do a normal jump
581 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
584 // Let go of jump key
585 else if(!controller->hold(Controller::JUMP)) {
586 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
588 physic.set_velocity_y(0);
592 /* In case the player has pressed Down while in a certain range of air,
593 enable butt jump action */
594 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
598 /* When Down is not held anymore, disable butt jump */
599 if(butt_jump && !controller->hold(Controller::DOWN))
603 physic.set_acceleration_y(0);
605 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
606 physic.set_acceleration_y(-2000);
607 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
612 Player::handle_input()
615 handle_input_ghost();
619 handle_input_climbing();
624 if( controller->released( Controller::PEEK_LEFT ) ) {
627 if( controller->released( Controller::PEEK_RIGHT ) ) {
630 if( controller->pressed( Controller::PEEK_LEFT ) ) {
633 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
637 /* Handle horizontal movement: */
638 if (!backflipping) handle_horizontal_input();
641 if (on_ground() && !controller->hold(Controller::JUMP))
644 /* Handle vertical movement: */
645 handle_vertical_input();
648 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
649 if(Sector::current()->add_bullet(
650 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
651 : Vector(32, bbox.get_height()/2)),
652 physic.get_velocity_x(), dir))
653 shooting_timer.start(SHOOTING_TIME);
656 /* Duck or Standup! */
657 if (controller->hold(Controller::DOWN)) {
666 if(!controller->hold(Controller::ACTION) && grabbed_object) {
667 // move the grabbed object a bit away from tux
668 Vector pos = get_pos() +
669 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
670 bbox.get_height()*0.66666 - 32);
671 Rect dest(pos, pos + Vector(32, 32));
672 if(Sector::current()->is_free_of_movingstatics(dest)) {
673 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
675 moving_object->set_pos(pos);
677 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
679 grabbed_object->ungrab(*this, dir);
680 grabbed_object = NULL;
688 if(controller->hold(Controller::ACTION) && !grabbed_object
690 Sector* sector = Sector::current();
693 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
695 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
698 for(Sector::Portables::iterator i = sector->portables.begin();
699 i != sector->portables.end(); ++i) {
700 Portable* portable = *i;
701 if(!portable->is_portable())
704 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
706 if(moving_object == NULL)
709 if(moving_object->get_bbox().contains(pos)) {
710 if (climbing) stop_climbing(*climbing);
711 grabbed_object = portable;
712 grabbed_object->grab(*this, get_pos(), dir);
720 Player::handle_input_ghost()
724 if (controller->hold(Controller::LEFT)) {
726 vx -= MAX_RUN_XM * 2;
728 if (controller->hold(Controller::RIGHT)) {
730 vx += MAX_RUN_XM * 2;
732 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
733 vy -= MAX_RUN_XM * 2;
735 if (controller->hold(Controller::DOWN)) {
736 vy += MAX_RUN_XM * 2;
738 if (controller->hold(Controller::ACTION)) {
739 set_ghost_mode(false);
741 physic.set_velocity(vx, vy);
742 physic.set_acceleration(0, 0);
746 Player::add_coins(int count)
748 player_status->add_coins(count);
754 return player_status->coins;
758 Player::add_bonus(const std::string& bonustype)
760 BonusType type = NO_BONUS;
762 if(bonustype == "grow") {
764 } else if(bonustype == "fireflower") {
766 } else if(bonustype == "iceflower") {
768 } else if(bonustype == "none") {
771 std::ostringstream msg;
772 msg << "Unknown bonus type " << bonustype;
773 throw std::runtime_error(msg.str());
776 return add_bonus(type);
780 Player::add_bonus(BonusType type, bool animate)
782 // always ignore NO_BONUS
783 if (type == NO_BONUS) {
787 // ignore GROWUP_BONUS if we're already big
788 if (type == GROWUP_BONUS) {
789 if (player_status->bonus == GROWUP_BONUS)
791 if (player_status->bonus == FIRE_BONUS)
793 if (player_status->bonus == ICE_BONUS)
797 return set_bonus(type, animate);
801 Player::set_bonus(BonusType type, bool animate)
803 if(player_status->bonus == NO_BONUS) {
804 if (!adjust_height(62.8f)) {
805 printf("can't adjust\n");
809 growing_timer.start(GROWING_TIME);
810 if (climbing) stop_climbing(*climbing);
813 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
814 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
815 // visually lose helmet
816 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
817 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
818 Vector paccel = Vector(0, 1000);
819 std::string action = (dir==LEFT)?"left":"right";
820 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
821 if (climbing) stop_climbing(*climbing);
823 if ((player_status->bonus == ICE_BONUS) && (animate)) {
825 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
826 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
827 Vector paccel = Vector(0, 1000);
828 std::string action = (dir==LEFT)?"left":"right";
829 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
830 if (climbing) stop_climbing(*climbing);
832 player_status->max_fire_bullets = 0;
833 player_status->max_ice_bullets = 0;
835 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
836 if (type == ICE_BONUS) player_status->max_ice_bullets++;
838 player_status->bonus = type;
843 Player::set_visible(bool visible)
845 this->visible = visible;
847 set_group(COLGROUP_MOVING);
849 set_group(COLGROUP_DISABLED);
853 Player::get_visible()
861 kick_timer.start(KICK_TIME);
865 Player::draw(DrawingContext& context)
870 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
871 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
872 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
873 float py = Sector::current()->camera->get_translation().y;
874 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
875 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
878 TuxBodyParts* tux_body;
880 if (player_status->bonus == GROWUP_BONUS)
882 else if (player_status->bonus == FIRE_BONUS)
884 else if (player_status->bonus == ICE_BONUS)
887 tux_body = small_tux;
889 int layer = LAYER_OBJECTS + 1;
891 /* Set Tux sprite action */
894 tux_body->set_action("skid-left");
896 else if (backflipping)
899 tux_body->set_action("backflip-left");
901 tux_body->set_action("backflip-right");
903 else if (duck && is_big())
906 tux_body->set_action("duck-left");
908 tux_body->set_action("duck-right");
910 else if (skidding_timer.started() && !skidding_timer.check())
913 tux_body->set_action("skid-left");
915 tux_body->set_action("skid-right");
917 else if (kick_timer.started() && !kick_timer.check())
920 tux_body->set_action("kick-left");
922 tux_body->set_action("kick-right");
924 else if (butt_jump && is_big())
927 tux_body->set_action("buttjump-left");
929 tux_body->set_action("buttjump-right");
931 else if (!on_ground())
934 tux_body->set_action("jump-left");
936 tux_body->set_action("jump-right");
940 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
943 tux_body->set_action("stand-left");
945 tux_body->set_action("stand-right");
950 tux_body->set_action("walk-left");
952 tux_body->set_action("walk-right");
956 if(idle_timer.check())
961 tux_body->head->set_action("idle-left", 1);
963 tux_body->head->set_action("idle-right", 1);
968 // Tux is holding something
969 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
970 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
975 tux_body->arms->set_action("duck+grab-left");
977 tux_body->arms->set_action("duck+grab-right");
982 tux_body->arms->set_action("grab-left");
984 tux_body->arms->set_action("grab-right");
990 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
992 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
994 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
995 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
997 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
998 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
1001 else if (safe_timer.started() && size_t(game_time*40)%2)
1004 tux_body->draw(context, get_pos(), layer);
1009 Player::collision_tile(uint32_t tile_attributes)
1011 if(tile_attributes & Tile::HURTS)
1015 if( tile_attributes & Tile::WATER ){
1021 if( tile_attributes & Tile::WATER ){
1024 sound_manager->play( "sounds/splash.ogg" );
1030 Player::collision_solid(const CollisionHit& hit)
1033 if(physic.get_velocity_y() > 0)
1034 physic.set_velocity_y(0);
1036 on_ground_flag = true;
1037 floor_normal = hit.slope_normal;
1038 } else if(hit.top) {
1039 if(physic.get_velocity_y() < 0)
1040 physic.set_velocity_y(.2f);
1043 if(hit.left || hit.right) {
1044 physic.set_velocity_x(0);
1049 if(hit.left || hit.right) {
1051 } else if(hit.top || hit.bottom) {
1058 Player::collision(GameObject& other, const CollisionHit& hit)
1060 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1065 if(hit.left || hit.right) {
1066 try_grab(); //grab objects right now, in update it will be too late
1069 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1071 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1072 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1073 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1075 if(controller->pressed(Controller::UP))
1076 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1082 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1083 if(badguy != NULL) {
1084 if(safe_timer.started() || invincible_timer.started())
1094 Player::make_invincible()
1096 sound_manager->play("sounds/invincible.wav");
1097 invincible_timer.start(TUX_INVINCIBLE_TIME);
1098 Sector::current()->play_music(HERRING_MUSIC);
1103 Player::kill(bool completely)
1105 if(dying || deactivated)
1108 if(!completely && (safe_timer.started() || invincible_timer.started()))
1111 sound_manager->play("sounds/hurt.wav");
1112 if (climbing) stop_climbing(*climbing);
1114 physic.set_velocity_x(0);
1116 if(!completely && (is_big() || growing_timer.started())) {
1117 if(player_status->bonus == FIRE_BONUS
1118 || player_status->bonus == ICE_BONUS) {
1119 safe_timer.start(TUX_SAFE_TIME);
1120 set_bonus(GROWUP_BONUS, true);
1121 } else if(player_status->bonus == GROWUP_BONUS) {
1122 //growing_timer.start(GROWING_TIME);
1123 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1124 adjust_height(30.8f);
1126 set_bonus(NO_BONUS, true);
1127 } else if(player_status->bonus == NO_BONUS) {
1128 growing_timer.stop();
1129 safe_timer.start(TUX_SAFE_TIME);
1130 adjust_height(30.8f);
1134 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1136 for (int i = 0; i < 5; i++)
1138 // the numbers: starting x, starting y, velocity y
1139 Sector::current()->add_object(new FallingCoin(get_pos() +
1140 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1141 systemRandom.rand(-100,100)));
1143 player_status->coins -= std::max(player_status->coins/10, 25);
1147 GameSession::current()->set_reset_point("", Vector());
1149 physic.enable_gravity(true);
1150 physic.set_acceleration(0, 0);
1151 physic.set_velocity(0, -700);
1152 set_bonus(NO_BONUS, true);
1154 dying_timer.start(3.0);
1155 set_group(COLGROUP_DISABLED);
1157 DisplayEffect* effect = new DisplayEffect();
1158 effect->fade_out(3.0);
1159 Sector::current()->add_object(effect);
1160 sound_manager->stop_music(3.0);
1165 Player::move(const Vector& vector)
1169 // TODO: do we need the following? Seems irrelevant to moving the player
1171 set_size(31.8f, 63.8f);
1173 set_size(31.8f, 31.8f);
1175 last_ground_y = vector.y;
1176 if (climbing) stop_climbing(*climbing);
1182 Player::check_bounds(Camera* camera)
1184 /* Keep tux in bounds: */
1185 if (get_pos().x < 0) {
1186 // Lock Tux to the size of the level, so that he doesn't fall of
1188 set_pos(Vector(0, get_pos().y));
1191 /* fallen out of the level? */
1192 if (get_pos().y > Sector::current()->get_height()) {
1197 // can happen if back scrolling is disabled
1198 if(get_pos().x < camera->get_translation().x) {
1199 set_pos(Vector(camera->get_translation().x, get_pos().y));
1201 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1204 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1210 Player::add_velocity(const Vector& velocity)
1212 physic.set_velocity(physic.get_velocity() + velocity);
1216 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1218 if (end_speed.x > 0)
1219 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1220 if (end_speed.x < 0)
1221 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1222 if (end_speed.y > 0)
1223 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1224 if (end_speed.y < 0)
1225 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1229 Player::bounce(BadGuy& )
1231 if(controller->hold(Controller::JUMP))
1232 physic.set_velocity_y(-520);
1234 physic.set_velocity_y(-300);
1237 //Scripting Functions Below
1240 Player::deactivate()
1245 physic.set_velocity_x(0);
1246 physic.set_velocity_y(0);
1247 physic.set_acceleration_x(0);
1248 physic.set_acceleration_y(0);
1249 if (climbing) stop_climbing(*climbing);
1257 deactivated = false;
1260 void Player::walk(float speed)
1262 physic.set_velocity_x(speed);
1266 Player::set_ghost_mode(bool enable)
1268 if (ghost_mode == enable)
1271 if (climbing) stop_climbing(*climbing);
1275 set_group(COLGROUP_DISABLED);
1276 physic.enable_gravity(false);
1277 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1280 set_group(COLGROUP_MOVING);
1281 physic.enable_gravity(true);
1282 log_debug << "You feel solid again." << std::endl;
1288 Player::start_climbing(Climbable& climbable)
1290 if (climbing == &climbable) return;
1292 climbing = &climbable;
1293 physic.enable_gravity(false);
1294 physic.set_velocity(0, 0);
1295 physic.set_acceleration(0, 0);
1299 Player::stop_climbing(Climbable& /*climbable*/)
1301 if (!climbing) return;
1305 if (grabbed_object) {
1306 grabbed_object->ungrab(*this, dir);
1307 grabbed_object = NULL;
1310 physic.enable_gravity(true);
1311 physic.set_velocity(0, 0);
1312 physic.set_acceleration(0, 0);
1314 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1315 on_ground_flag = true;
1316 // TODO: This won't help. Why?
1322 Player::handle_input_climbing()
1325 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1331 if (controller->hold(Controller::LEFT)) {
1335 if (controller->hold(Controller::RIGHT)) {
1339 if (controller->hold(Controller::UP)) {
1342 if (controller->hold(Controller::DOWN)) {
1345 if (controller->hold(Controller::JUMP)) {
1347 stop_climbing(*climbing);
1353 if (controller->hold(Controller::ACTION)) {
1354 stop_climbing(*climbing);
1357 physic.set_velocity(vx, vy);
1358 physic.set_acceleration(0, 0);