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"
57 static const int TILES_FOR_BUTTJUMP = 3;
58 static const float BUTTJUMP_MIN_VELOCITY_Y = 400.0f;
59 static const float SHOOTING_TIME = .150f;
61 /** number of idle stages, including standing */
62 static const unsigned int IDLE_STAGE_COUNT = 5;
64 * how long to play each idle animation in milliseconds
65 * '0' means the sprite action is played once before moving onto the next
68 static const int IDLE_TIME[] = { 5000, 0, 2500, 0, 2500 };
70 static const std::string IDLE_STAGES[] =
77 /** acceleration in horizontal direction when walking
78 * (all accelerations are in pixel/s^2) */
79 static const float WALK_ACCELERATION_X = 300;
80 /** acceleration in horizontal direction when running */
81 static const float RUN_ACCELERATION_X = 400;
82 /** acceleration when skidding */
83 static const float SKID_XM = 200;
84 /** time of skidding in seconds */
85 static const float SKID_TIME = .3f;
86 /** maximum walk velocity (pixel/s) */
87 static const float MAX_WALK_XM = 230;
88 /** maximum run velocity (pixel/s) */
89 static const float MAX_RUN_XM = 320;
90 /** maximum horizontal climb velocity */
91 static const float MAX_CLIMB_XM = 48;
92 /** maximum vertical climb velocity */
93 static const float MAX_CLIMB_YM = 128;
94 /** instant velocity when tux starts to walk */
95 static const float WALK_SPEED = 100;
97 /** multiplied by WALK_ACCELERATION to give friction */
98 static const float NORMAL_FRICTION_MULTIPLIER = 1.5f;
99 /** multiplied by WALK_ACCELERATION to give friction */
100 static const float ICE_FRICTION_MULTIPLIER = 0.1f;
101 static const float ICE_ACCELERATION_MULTIPLIER = 0.25f;
103 /** time of the kick (kicking mriceblock) animation */
104 static const float KICK_TIME = .3f;
105 /** time of tux cheering (currently unused) */
106 static const float CHEER_TIME = 1.0f;
108 /** if Tux cannot unduck for this long, he will get hurt */
109 static const float UNDUCK_HURT_TIME = 0.25f;
110 /** gravity is higher after the jump key is released before
111 the apex of the jump is reached */
112 static const float JUMP_EARLY_APEX_FACTOR = 3.0;
114 static const float JUMP_GRACE_TIME = 0.25f; /**< time before hitting the ground that the jump button may be pressed (and still trigger a jump) */
116 bool no_water = true;
119 Player::Player(PlayerStatus* _player_status, const std::string& name)
120 : scripting_controller(0),
121 player_status(_player_status),
122 scripting_controller_old(0),
123 grabbed_object(NULL), ghost_mode(false), edit_mode(false), idle_stage(0),
127 controller = main_controller;
128 scripting_controller = new CodeController();
129 sprite = sprite_manager->create("images/creatures/tux/tux.sprite");
130 airarrow.reset(new Surface("images/engine/hud/airarrow.png"));
131 idle_timer.start(IDLE_TIME[0]/1000.0f);
133 sound_manager->preload("sounds/bigjump.wav");
134 sound_manager->preload("sounds/jump.wav");
135 sound_manager->preload("sounds/hurt.wav");
136 sound_manager->preload("sounds/skid.wav");
137 sound_manager->preload("sounds/flip.wav");
138 sound_manager->preload("sounds/invincible_start.ogg");
139 sound_manager->preload("sounds/splash.ogg");
146 if (climbing) stop_climbing(*climbing);
148 delete scripting_controller;
155 set_size(31.8f, 62.8f);
157 set_size(31.8f, 30.8f);
168 fall_mode = ON_GROUND;
170 jump_early_apex = false;
172 wants_buttjump = false;
173 does_buttjump = false;
176 backflipping = false;
177 backflip_direction = 0;
181 ice_this_frame = false;
182 speedlimit = 0; //no special limit
184 on_ground_flag = false;
185 grabbed_object = NULL;
193 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
198 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
202 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
207 Scripting::unexpose_object(vm, table_idx, name);
211 Player::get_speedlimit()
217 Player::set_speedlimit(float newlimit)
223 Player::set_controller(Controller* controller)
225 this->controller = controller;
229 Player::use_scripting_controller(bool use_or_release)
231 if ((use_or_release == true) && (controller != scripting_controller)) {
232 scripting_controller_old = get_controller();
233 set_controller(scripting_controller);
235 if ((use_or_release == false) && (controller == scripting_controller)) {
236 set_controller(scripting_controller_old);
237 scripting_controller_old = 0;
242 Player::do_scripting_controller(std::string control, bool pressed)
244 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
245 if(control == std::string(Controller::controlNames[i])) {
246 scripting_controller->press(Controller::Control(i), pressed);
252 Player::adjust_height(float new_height)
255 bbox2.move(Vector(0, bbox.get_height() - new_height));
256 bbox2.set_height(new_height);
258 if(new_height > bbox.get_height()) {
259 Rect additional_space = bbox2;
260 additional_space.set_height(new_height - bbox.get_height());
261 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
265 // adjust bbox accordingly
266 // note that we use members of moving_object for this, so we can run this during CD, too
268 set_size(bbox2.get_width(), bbox2.get_height());
273 Player::trigger_sequence(std::string sequence_name)
275 if (climbing) stop_climbing(*climbing);
276 GameSession::current()->start_sequence(sequence_name);
280 Player::update(float elapsed_time)
287 if(dying && dying_timer.check()) {
292 if(!dying && !deactivated)
295 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
299 // extend/shrink tux collision rectangle so that we fall through/walk over 1
301 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
307 // on downward slopes, adjust vertical velocity so tux walks smoothly down
309 if(floor_normal.y != 0) {
310 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
311 physic.set_velocity_y(250);
316 // handle backflipping
318 //prevent player from changing direction when backflipping
319 dir = (backflip_direction == 1) ? LEFT : RIGHT;
320 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
325 fall_mode = ON_GROUND;
326 last_ground_y = get_pos().y;
328 if(get_pos().y > last_ground_y)
330 else if(fall_mode == ON_GROUND)
334 // check if we landed
337 if (backflipping && (!backflip_timer.started())) {
338 backflipping = false;
339 backflip_direction = 0;
341 // if controls are currently deactivated, we take care of standing up ourselves
347 // calculate movement for this frame
348 movement = physic.get_movement(elapsed_time);
350 if(grabbed_object != NULL && !dying) {
351 Vector pos = get_pos() +
352 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
353 grabbed_object->grab(*this, pos, dir);
356 if(grabbed_object != NULL && dying){
357 grabbed_object->ungrab(*this, dir);
358 grabbed_object = NULL;
361 if(!ice_this_frame && on_ground())
364 on_ground_flag = false;
365 ice_this_frame = false;
367 // when invincible, spawn particles
368 if (invincible_timer.started() && !dying)
370 if (systemRandom.rand(0, 2) == 0) {
371 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
372 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
373 Vector ppos = Vector(px, py);
374 Vector pspeed = Vector(0, 0);
375 Vector paccel = Vector(0, 0);
376 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
377 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
378 // make every other a longer sparkle to make trail a bit fuzzy
379 if (size_t(game_time*20)%2) {
380 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
382 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
385 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
391 if (sprite->animation_done()) growing = false;
399 return on_ground_flag;
405 if(player_status->bonus == NO_BONUS)
412 Player::apply_friction()
414 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
415 physic.set_velocity_x(0);
416 physic.set_acceleration_x(0);
418 float friction = WALK_ACCELERATION_X * (on_ice ? ICE_FRICTION_MULTIPLIER : NORMAL_FRICTION_MULTIPLIER);
419 if(physic.get_velocity_x() < 0) {
420 physic.set_acceleration_x(friction);
421 } else /*if(physic.get_velocity_x() > 0)*/ {
422 physic.set_acceleration_x(-friction);
428 Player::handle_horizontal_input()
430 float vx = physic.get_velocity_x();
431 float vy = physic.get_velocity_y();
432 float ax = physic.get_acceleration_x();
433 float ay = physic.get_acceleration_y();
436 if(!duck || physic.get_velocity_y() != 0) {
437 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
441 } else if(!controller->hold(Controller::LEFT)
442 && controller->hold(Controller::RIGHT)) {
449 // do not run if action key is pressed or we're holding something
450 // so tux can only walk while shooting
451 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
452 ax = dirsign * WALK_ACCELERATION_X;
454 if(vx >= MAX_WALK_XM && dirsign > 0) {
457 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
462 if( vx * dirsign < MAX_WALK_XM ) {
463 ax = dirsign * WALK_ACCELERATION_X;
465 ax = dirsign * RUN_ACCELERATION_X;
468 if(vx >= MAX_RUN_XM && dirsign > 0) {
471 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
477 // we can reach WALK_SPEED without any acceleration
478 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
479 vx = dirsign * WALK_SPEED;
483 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
484 vx = dirsign * speedlimit;
488 // changing directions?
489 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
491 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
492 skidding_timer.start(SKID_TIME);
493 sound_manager->play("sounds/skid.wav");
494 // dust some particles
495 Sector::current()->add_object(
497 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
498 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
499 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
509 ax *= ICE_ACCELERATION_MULTIPLIER;
512 physic.set_velocity(vx, vy);
513 physic.set_acceleration(ax, ay);
515 // we get slower when not pressing any keys
537 if (physic.get_velocity_y() != 0)
544 if (adjust_height(31.8f)) {
547 unduck_hurt_timer.stop();
554 Player::do_standup() {
562 if (adjust_height(63.8f)) {
564 unduck_hurt_timer.stop();
566 // if timer is not already running, start it.
567 if (unduck_hurt_timer.get_period() == 0) {
568 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
570 else if (unduck_hurt_timer.check()) {
578 Player::do_backflip() {
584 backflip_direction = (dir == LEFT)?(+1):(-1);
587 sound_manager->play("sounds/flip.wav");
588 backflip_timer.start(0.15f);
592 Player::do_jump(float yspeed) {
596 physic.set_velocity_y(yspeed);
597 //bbox.move(Vector(0, -1));
599 on_ground_flag = false;
604 sound_manager->play("sounds/bigjump.wav");
606 sound_manager->play("sounds/jump.wav");
611 Player::early_jump_apex() {
612 if(jump_early_apex) {
615 jump_early_apex = true;
616 physic.set_gravity(physic.get_gravity() * JUMP_EARLY_APEX_FACTOR);
620 Player::do_jump_apex() {
621 if(!jump_early_apex) {
624 jump_early_apex = false;
625 physic.set_gravity(physic.get_gravity() / JUMP_EARLY_APEX_FACTOR);
629 Player::handle_vertical_input()
632 if(controller->pressed(Controller::JUMP)) jump_button_timer.start(JUMP_GRACE_TIME);
633 if(controller->hold(Controller::JUMP) && jump_button_timer.started() && can_jump) {
634 jump_button_timer.stop();
636 // when running, only jump a little bit; else do a backflip
637 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
639 // jump a bit higher if we are running; else do a normal jump
640 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
643 // Let go of jump key
644 else if(!controller->hold(Controller::JUMP)) {
645 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
651 if(jump_early_apex && physic.get_velocity_y() >= 0) {
655 /* In case the player has pressed Down while in a certain range of air,
656 enable butt jump action */
657 if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
658 wants_buttjump = true;
659 if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
662 /* When Down is not held anymore, disable butt jump */
663 if(!controller->hold(Controller::DOWN)) {
664 wants_buttjump = false;
665 does_buttjump = false;
669 physic.set_acceleration_y(0);
672 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
673 physic.set_acceleration_y(-2000);
674 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
680 Player::handle_input()
683 handle_input_ghost();
687 handle_input_climbing();
692 if( controller->released( Controller::PEEK_LEFT ) ) {
695 if( controller->released( Controller::PEEK_RIGHT ) ) {
698 if( controller->released( Controller::PEEK_UP ) ) {
701 if( controller->released( Controller::PEEK_DOWN ) ) {
704 if( controller->pressed( Controller::PEEK_LEFT ) ) {
707 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
710 if(!backflipping && !jumping && on_ground()) {
711 if( controller->pressed( Controller::PEEK_UP ) ) {
713 } else if( controller->pressed( Controller::PEEK_DOWN ) ) {
718 /* Handle horizontal movement: */
719 if (!backflipping) handle_horizontal_input();
725 /* Handle vertical movement: */
726 handle_vertical_input();
729 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
730 if(Sector::current()->add_bullet(
731 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
732 : Vector(32, bbox.get_height()/2)),
733 physic.get_velocity_x(), dir))
734 shooting_timer.start(SHOOTING_TIME);
737 /* Duck or Standup! */
738 if (controller->hold(Controller::DOWN)) {
747 if(!controller->hold(Controller::ACTION) && grabbed_object) {
748 // move the grabbed object a bit away from tux
749 Vector pos = get_pos() +
750 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
751 bbox.get_height()*0.66666 - 32);
752 Rect dest(pos, pos + Vector(32, 32));
753 if(Sector::current()->is_free_of_movingstatics(dest)) {
754 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
756 moving_object->set_pos(pos);
758 log_debug << "Non MovingObject grabbed?!?" << std::endl;
760 if(controller->hold(Controller::UP)) {
761 grabbed_object->ungrab(*this, UP);
763 grabbed_object->ungrab(*this, dir);
765 grabbed_object = NULL;
773 if(controller->hold(Controller::ACTION) && !grabbed_object
775 Sector* sector = Sector::current();
778 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
780 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
783 for(Sector::Portables::iterator i = sector->portables.begin();
784 i != sector->portables.end(); ++i) {
785 Portable* portable = *i;
786 if(!portable->is_portable())
789 // make sure the Portable is a MovingObject
790 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
791 assert(moving_object);
792 if(moving_object == NULL)
795 // make sure the Portable isn't currently non-solid
796 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
798 // check if we are within reach
799 if(moving_object->get_bbox().contains(pos)) {
800 if (climbing) stop_climbing(*climbing);
801 grabbed_object = portable;
802 grabbed_object->grab(*this, get_pos(), dir);
810 Player::handle_input_ghost()
814 if (controller->hold(Controller::LEFT)) {
816 vx -= MAX_RUN_XM * 2;
818 if (controller->hold(Controller::RIGHT)) {
820 vx += MAX_RUN_XM * 2;
822 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
823 vy -= MAX_RUN_XM * 2;
825 if (controller->hold(Controller::DOWN)) {
826 vy += MAX_RUN_XM * 2;
828 if (controller->hold(Controller::ACTION)) {
829 set_ghost_mode(false);
831 physic.set_velocity(vx, vy);
832 physic.set_acceleration(0, 0);
836 Player::add_coins(int count)
838 player_status->add_coins(count);
844 return player_status->coins;
848 Player::add_bonus(const std::string& bonustype)
850 BonusType type = NO_BONUS;
852 if(bonustype == "grow") {
854 } else if(bonustype == "fireflower") {
856 } else if(bonustype == "iceflower") {
858 } else if(bonustype == "none") {
861 std::ostringstream msg;
862 msg << "Unknown bonus type " << bonustype;
863 throw std::runtime_error(msg.str());
866 return add_bonus(type);
870 Player::add_bonus(BonusType type, bool animate)
872 // always ignore NO_BONUS
873 if (type == NO_BONUS) {
877 // ignore GROWUP_BONUS if we're already big
878 if (type == GROWUP_BONUS) {
879 if (player_status->bonus == GROWUP_BONUS)
881 if (player_status->bonus == FIRE_BONUS)
883 if (player_status->bonus == ICE_BONUS)
887 return set_bonus(type, animate);
891 Player::set_bonus(BonusType type, bool animate)
893 if(player_status->bonus == NO_BONUS) {
894 if (!adjust_height(62.8f)) {
895 printf("can't adjust\n");
900 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
902 if (climbing) stop_climbing(*climbing);
905 if (type == NO_BONUS) {
906 if (does_buttjump) does_buttjump = false;
909 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
910 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
911 // visually lose helmet
912 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
913 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
914 Vector paccel = Vector(0, 1000);
915 std::string action = (dir==LEFT)?"left":"right";
916 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
917 if (climbing) stop_climbing(*climbing);
919 if ((player_status->bonus == ICE_BONUS) && (animate)) {
921 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
922 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
923 Vector paccel = Vector(0, 1000);
924 std::string action = (dir==LEFT)?"left":"right";
925 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
926 if (climbing) stop_climbing(*climbing);
928 player_status->max_fire_bullets = 0;
929 player_status->max_ice_bullets = 0;
931 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
932 if (type == ICE_BONUS) player_status->max_ice_bullets++;
934 player_status->bonus = type;
939 Player::set_visible(bool visible)
941 this->visible = visible;
943 set_group(COLGROUP_MOVING);
945 set_group(COLGROUP_DISABLED);
949 Player::get_visible()
957 kick_timer.start(KICK_TIME);
961 Player::draw(DrawingContext& context)
966 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
967 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
968 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
969 float py = Sector::current()->camera->get_translation().y;
970 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
971 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
974 std::string sa_prefix = "";
975 std::string sa_postfix = "";
977 if (player_status->bonus == GROWUP_BONUS)
979 else if (player_status->bonus == FIRE_BONUS)
981 else if (player_status->bonus == ICE_BONUS)
987 sa_postfix = "-left";
989 sa_postfix = "-right";
991 /* Set Tux sprite action */
993 sprite->set_action("gameover");
996 sprite->set_action_continued("grow"+sa_postfix);
997 // while growing, do not change action
998 // do_duck() will take care of cancelling growing manually
999 // update() will take care of cancelling when growing completed
1001 else if (climbing) {
1002 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1004 else if (backflipping) {
1005 sprite->set_action(sa_prefix+"-backflip"+sa_postfix);
1007 else if (duck && is_big()) {
1008 sprite->set_action(sa_prefix+"-duck"+sa_postfix);
1010 else if (skidding_timer.started() && !skidding_timer.check()) {
1011 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1013 else if (kick_timer.started() && !kick_timer.check()) {
1014 sprite->set_action(sa_prefix+"-kick"+sa_postfix);
1016 else if ((wants_buttjump || does_buttjump) && is_big()) {
1017 sprite->set_action(sa_prefix+"-buttjump"+sa_postfix);
1019 else if (!on_ground()) {
1020 sprite->set_action(sa_prefix+"-jump"+sa_postfix);
1023 if (fabsf(physic.get_velocity_x()) < 1.0f) {
1024 // Determine which idle stage we're at
1025 if (sprite->get_action().find("-stand-") == std::string::npos && sprite->get_action().find("-idle-") == std::string::npos) {
1027 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1029 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1031 else if (idle_timer.check() || (IDLE_TIME[idle_stage] == 0 && sprite->animation_done())) {
1033 if (idle_stage >= IDLE_STAGE_COUNT)
1036 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1038 if (IDLE_TIME[idle_stage] == 0)
1039 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix, 1);
1041 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1044 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1048 sprite->set_action(sa_prefix+"-walk"+sa_postfix);
1054 // Tux is holding something
1055 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1056 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
1064 if (safe_timer.started() && size_t(game_time*40)%2)
1067 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1073 Player::collision_tile(uint32_t tile_attributes)
1075 if(tile_attributes & Tile::HURTS)
1080 if( tile_attributes & Tile::WATER ){
1086 if( tile_attributes & Tile::WATER ){
1089 sound_manager->play( "sounds/splash.ogg" );
1094 if(tile_attributes & (Tile::ICE | Tile::SOLID)) {
1095 ice_this_frame = true;
1101 Player::collision_solid(const CollisionHit& hit)
1104 if(physic.get_velocity_y() > 0)
1105 physic.set_velocity_y(0);
1107 on_ground_flag = true;
1108 floor_normal = hit.slope_normal;
1111 if (does_buttjump) {
1112 does_buttjump = false;
1113 physic.set_velocity_y(-300);
1114 on_ground_flag = false;
1115 Sector::current()->add_object(new Particles(
1116 Vector(get_bbox().p2.x, get_bbox().p2.y),
1118 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1120 Sector::current()->add_object(new Particles(
1121 Vector(get_bbox().p1.x, get_bbox().p2.y),
1123 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1127 } else if(hit.top) {
1128 if(physic.get_velocity_y() < 0)
1129 physic.set_velocity_y(.2f);
1132 if(hit.left || hit.right) {
1133 physic.set_velocity_x(0);
1138 if(hit.left || hit.right) {
1140 } else if(hit.top || hit.bottom) {
1147 Player::collision(GameObject& other, const CollisionHit& hit)
1149 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1154 if(hit.left || hit.right) {
1155 try_grab(); //grab objects right now, in update it will be too late
1158 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1160 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1161 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1162 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1164 if(controller->pressed(Controller::UP))
1165 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1171 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1172 if(badguy != NULL) {
1173 if(safe_timer.started() || invincible_timer.started())
1183 Player::make_invincible()
1185 sound_manager->play("sounds/invincible_start.ogg");
1186 invincible_timer.start(TUX_INVINCIBLE_TIME);
1187 Sector::current()->play_music(HERRING_MUSIC);
1192 Player::kill(bool completely)
1194 if(dying || deactivated)
1197 if(!completely && (safe_timer.started() || invincible_timer.started()))
1202 sound_manager->play("sounds/hurt.wav");
1204 if (climbing) stop_climbing(*climbing);
1206 physic.set_velocity_x(0);
1208 if(!completely && is_big()) {
1209 if(player_status->bonus == FIRE_BONUS
1210 || player_status->bonus == ICE_BONUS) {
1211 safe_timer.start(TUX_SAFE_TIME);
1212 set_bonus(GROWUP_BONUS, true);
1213 } else if(player_status->bonus == GROWUP_BONUS) {
1214 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1215 adjust_height(30.8f);
1217 backflipping = false;
1218 set_bonus(NO_BONUS, true);
1219 } else if(player_status->bonus == NO_BONUS) {
1220 safe_timer.start(TUX_SAFE_TIME);
1221 adjust_height(30.8f);
1226 // do not die when in edit mode
1228 set_ghost_mode(true);
1232 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1234 for (int i = 0; i < 5; i++)
1236 // the numbers: starting x, starting y, velocity y
1237 Sector::current()->add_object(new FallingCoin(get_pos() +
1238 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1239 systemRandom.rand(-100,100)));
1241 player_status->coins -= std::max(player_status->coins/10, 25);
1245 GameSession::current()->set_reset_point("", Vector());
1247 physic.enable_gravity(true);
1248 physic.set_acceleration(0, 0);
1249 physic.set_velocity(0, -700);
1250 set_bonus(NO_BONUS, true);
1252 dying_timer.start(3.0);
1253 set_group(COLGROUP_DISABLED);
1255 DisplayEffect* effect = new DisplayEffect();
1256 effect->fade_out(3.0);
1257 Sector::current()->add_object(effect);
1258 sound_manager->stop_music(3.0);
1263 Player::move(const Vector& vector)
1267 // TODO: do we need the following? Seems irrelevant to moving the player
1269 set_size(31.8f, 63.8f);
1271 set_size(31.8f, 31.8f);
1273 last_ground_y = vector.y;
1274 if (climbing) stop_climbing(*climbing);
1280 Player::check_bounds(Camera* camera)
1282 /* Keep tux in bounds: */
1283 if (get_pos().x < 0) {
1284 // Lock Tux to the size of the level, so that he doesn't fall of
1286 set_pos(Vector(0, get_pos().y));
1289 /* fallen out of the level? */
1290 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1295 // can happen if back scrolling is disabled
1296 if(get_pos().x < camera->get_translation().x) {
1297 set_pos(Vector(camera->get_translation().x, get_pos().y));
1299 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1302 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1308 Player::add_velocity(const Vector& velocity)
1310 physic.set_velocity(physic.get_velocity() + velocity);
1314 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1316 if (end_speed.x > 0)
1317 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1318 if (end_speed.x < 0)
1319 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1320 if (end_speed.y > 0)
1321 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1322 if (end_speed.y < 0)
1323 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1327 Player::get_velocity()
1329 return physic.get_velocity();
1333 Player::bounce(BadGuy& )
1335 if(controller->hold(Controller::JUMP))
1336 physic.set_velocity_y(-520);
1338 physic.set_velocity_y(-300);
1341 //Scripting Functions Below
1344 Player::deactivate()
1349 physic.set_velocity_x(0);
1350 physic.set_velocity_y(0);
1351 physic.set_acceleration_x(0);
1352 physic.set_acceleration_y(0);
1353 if (climbing) stop_climbing(*climbing);
1361 deactivated = false;
1364 void Player::walk(float speed)
1366 physic.set_velocity_x(speed);
1370 Player::set_ghost_mode(bool enable)
1372 if (ghost_mode == enable)
1375 if (climbing) stop_climbing(*climbing);
1379 set_group(COLGROUP_DISABLED);
1380 physic.enable_gravity(false);
1381 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1384 set_group(COLGROUP_MOVING);
1385 physic.enable_gravity(true);
1386 log_debug << "You feel solid again." << std::endl;
1392 Player::set_edit_mode(bool enable)
1398 Player::start_climbing(Climbable& climbable)
1400 if (climbing == &climbable) return;
1402 climbing = &climbable;
1403 physic.enable_gravity(false);
1404 physic.set_velocity(0, 0);
1405 physic.set_acceleration(0, 0);
1409 Player::stop_climbing(Climbable& /*climbable*/)
1411 if (!climbing) return;
1415 if (grabbed_object) {
1416 grabbed_object->ungrab(*this, dir);
1417 grabbed_object = NULL;
1420 physic.enable_gravity(true);
1421 physic.set_velocity(0, 0);
1422 physic.set_acceleration(0, 0);
1424 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1425 on_ground_flag = true;
1426 // TODO: This won't help. Why?
1432 Player::handle_input_climbing()
1435 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1441 if (controller->hold(Controller::LEFT)) {
1445 if (controller->hold(Controller::RIGHT)) {
1449 if (controller->hold(Controller::UP)) {
1452 if (controller->hold(Controller::DOWN)) {
1455 if (controller->hold(Controller::JUMP)) {
1457 stop_climbing(*climbing);
1463 if (controller->hold(Controller::ACTION)) {
1464 stop_climbing(*climbing);
1467 physic.set_velocity(vx, vy);
1468 physic.set_acceleration(0, 0);