2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #include "object/player.hpp"
20 #include "audio/sound_manager.hpp"
21 #include "badguy/badguy.hpp"
22 #include "control/joystickkeyboardcontroller.hpp"
23 #include "math/random_generator.hpp"
24 #include "object/bullet.hpp"
25 #include "object/camera.hpp"
26 #include "object/display_effect.hpp"
27 #include "object/falling_coin.hpp"
28 #include "object/particles.hpp"
29 #include "object/portable.hpp"
30 #include "object/sprite_particle.hpp"
31 #include "scripting/squirrel_util.hpp"
32 #include "supertux/game_session.hpp"
33 #include "supertux/globals.hpp"
34 #include "supertux/sector.hpp"
35 #include "supertux/tile.hpp"
36 #include "trigger/climbable.hpp"
43 static const int TILES_FOR_BUTTJUMP = 3;
44 static const float BUTTJUMP_MIN_VELOCITY_Y = 400.0f;
45 static const float SHOOTING_TIME = .150f;
47 /** number of idle stages, including standing */
48 static const unsigned int IDLE_STAGE_COUNT = 5;
50 * how long to play each idle animation in milliseconds
51 * '0' means the sprite action is played once before moving onto the next
54 static const int IDLE_TIME[] = { 5000, 0, 2500, 0, 2500 };
56 static const std::string IDLE_STAGES[] =
63 /** acceleration in horizontal direction when walking
64 * (all accelerations are in pixel/s^2) */
65 static const float WALK_ACCELERATION_X = 300;
66 /** acceleration in horizontal direction when running */
67 static const float RUN_ACCELERATION_X = 400;
68 /** acceleration when skidding */
69 static const float SKID_XM = 200;
70 /** time of skidding in seconds */
71 static const float SKID_TIME = .3f;
72 /** maximum walk velocity (pixel/s) */
73 static const float MAX_WALK_XM = 230;
74 /** maximum run velocity (pixel/s) */
75 static const float MAX_RUN_XM = 320;
76 /** maximum horizontal climb velocity */
77 static const float MAX_CLIMB_XM = 96;
78 /** maximum vertical climb velocity */
79 static const float MAX_CLIMB_YM = 128;
80 /** instant velocity when tux starts to walk */
81 static const float WALK_SPEED = 100;
83 /** multiplied by WALK_ACCELERATION to give friction */
84 static const float NORMAL_FRICTION_MULTIPLIER = 1.5f;
85 /** multiplied by WALK_ACCELERATION to give friction */
86 static const float ICE_FRICTION_MULTIPLIER = 0.1f;
87 static const float ICE_ACCELERATION_MULTIPLIER = 0.25f;
89 /** time of the kick (kicking mriceblock) animation */
90 static const float KICK_TIME = .3f;
91 /** time of tux cheering (currently unused) */
92 static const float CHEER_TIME = 1.0f;
94 /** if Tux cannot unduck for this long, he will get hurt */
95 static const float UNDUCK_HURT_TIME = 0.25f;
96 /** gravity is higher after the jump key is released before
97 the apex of the jump is reached */
98 static const float JUMP_EARLY_APEX_FACTOR = 3.0;
100 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) */
102 /* Tux's collision rectangle */
103 static const float TUX_WIDTH = 31.8f;
104 static const float RUNNING_TUX_WIDTH = 34;
105 static const float SMALL_TUX_HEIGHT = 30.8f;
106 static const float BIG_TUX_HEIGHT = 62.8f;
107 static const float DUCKED_TUX_HEIGHT = 31.8f;
109 bool no_water = true;
112 Player::Player(PlayerStatus* _player_status, const std::string& name) :
115 scripting_controller(0),
116 player_status(_player_status),
121 backflip_direction(),
126 scripting_controller_old(0),
150 grabbed_object(NULL),
162 controller = g_jk_controller->get_main_controller();
163 scripting_controller.reset(new CodeController());
164 // if/when we have complete penny gfx, we can
165 // load those instead of Tux's sprite in the
167 sprite = sprite_manager->create("images/creatures/tux/tux.sprite");
168 airarrow = Surface::create("images/engine/hud/airarrow.png");
169 idle_timer.start(IDLE_TIME[0]/1000.0f);
171 sound_manager->preload("sounds/bigjump.wav");
172 sound_manager->preload("sounds/jump.wav");
173 sound_manager->preload("sounds/hurt.wav");
174 sound_manager->preload("sounds/kill.wav");
175 sound_manager->preload("sounds/skid.wav");
176 sound_manager->preload("sounds/flip.wav");
177 sound_manager->preload("sounds/invincible_start.ogg");
178 sound_manager->preload("sounds/splash.ogg");
185 if (climbing) stop_climbing(*climbing);
192 set_size(TUX_WIDTH, BIG_TUX_HEIGHT);
194 set_size(TUX_WIDTH, SMALL_TUX_HEIGHT);
206 fall_mode = ON_GROUND;
208 jump_early_apex = false;
210 wants_buttjump = false;
211 does_buttjump = false;
214 backflipping = false;
215 backflip_direction = 0;
219 ice_this_frame = false;
220 speedlimit = 0; //no special limit
222 on_ground_flag = false;
223 grabbed_object = NULL;
231 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
236 scripting::expose_object(vm, table_idx, dynamic_cast<scripting::Player *>(this), name, false);
240 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
245 scripting::unexpose_object(vm, table_idx, name);
249 Player::get_speedlimit()
255 Player::set_speedlimit(float newlimit)
261 Player::set_controller(Controller* controller)
263 this->controller = controller;
267 Player::set_winning()
269 if( ! is_winning() ){
271 invincible_timer.start(10000.0f);
276 Player::use_scripting_controller(bool use_or_release)
278 if ((use_or_release == true) && (controller != scripting_controller.get())) {
279 scripting_controller_old = get_controller();
280 set_controller(scripting_controller.get());
282 if ((use_or_release == false) && (controller == scripting_controller.get())) {
283 set_controller(scripting_controller_old);
284 scripting_controller_old = 0;
289 Player::do_scripting_controller(std::string control, bool pressed)
291 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
292 if(control == std::string(Controller::controlNames[i])) {
293 scripting_controller->press(Controller::Control(i), pressed);
299 Player::adjust_height(float new_height)
302 bbox2.move(Vector(0, bbox.get_height() - new_height));
303 bbox2.set_height(new_height);
306 if(new_height > bbox.get_height()) {
307 Rectf additional_space = bbox2;
308 additional_space.set_height(new_height - bbox.get_height());
309 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
313 // adjust bbox accordingly
314 // note that we use members of moving_object for this, so we can run this during CD, too
316 set_size(bbox2.get_width(), bbox2.get_height());
321 Player::trigger_sequence(std::string sequence_name)
323 if (climbing) stop_climbing(*climbing);
324 backflipping = false;
325 backflip_direction = 0;
326 GameSession::current()->start_sequence(sequence_name);
330 Player::update(float elapsed_time)
337 if(dying && dying_timer.check()) {
342 if(!dying && !deactivated)
345 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
349 // extend/shrink tux collision rectangle so that we fall through/walk over 1
351 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
352 set_width(RUNNING_TUX_WIDTH);
354 set_width(TUX_WIDTH);
357 // on downward slopes, adjust vertical velocity so tux walks smoothly down
359 if(floor_normal.y != 0) {
360 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
361 physic.set_velocity_y(250);
366 // handle backflipping
368 //prevent player from changing direction when backflipping
369 dir = (backflip_direction == 1) ? LEFT : RIGHT;
370 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
375 fall_mode = ON_GROUND;
376 last_ground_y = get_pos().y;
378 if(get_pos().y > last_ground_y)
380 else if(fall_mode == ON_GROUND)
384 // check if we landed
387 if (backflipping && (!backflip_timer.started())) {
388 backflipping = false;
389 backflip_direction = 0;
391 // if controls are currently deactivated, we take care of standing up ourselves
397 // calculate movement for this frame
398 movement = physic.get_movement(elapsed_time);
400 if(grabbed_object != NULL && !dying) {
401 position_grabbed_object();
404 if(grabbed_object != NULL && dying){
405 grabbed_object->ungrab(*this, dir);
406 grabbed_object = NULL;
409 if(!ice_this_frame && on_ground())
412 on_ground_flag = false;
413 ice_this_frame = false;
415 // when invincible, spawn particles
416 if (invincible_timer.started())
418 if (graphicsRandom.rand(0, 2) == 0) {
419 float px = graphicsRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
420 float py = graphicsRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
421 Vector ppos = Vector(px, py);
422 Vector pspeed = Vector(0, 0);
423 Vector paccel = Vector(0, 0);
424 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite",
425 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
426 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) ?
427 // make every other a longer sparkle to make trail a bit fuzzy
428 (size_t(game_time*20)%2) ? "small" : "medium"
430 "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
435 if (sprite->animation_done()) growing = false;
438 // when climbing animate only while moving
440 if((physic.get_velocity_x()==0)&&(physic.get_velocity_y()==0))
441 sprite->stop_animation();
443 sprite->set_animation_loops(-1);
451 return on_ground_flag;
457 if(player_status->bonus == NO_BONUS)
464 Player::apply_friction()
466 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
467 physic.set_velocity_x(0);
468 physic.set_acceleration_x(0);
470 float friction = WALK_ACCELERATION_X * (on_ice ? ICE_FRICTION_MULTIPLIER : NORMAL_FRICTION_MULTIPLIER);
471 if(physic.get_velocity_x() < 0) {
472 physic.set_acceleration_x(friction);
473 } else if(physic.get_velocity_x() > 0) {
474 physic.set_acceleration_x(-friction);
475 } // no friction for physic.get_velocity_x() == 0
480 Player::handle_horizontal_input()
482 float vx = physic.get_velocity_x();
483 float vy = physic.get_velocity_y();
484 float ax = physic.get_acceleration_x();
485 float ay = physic.get_acceleration_y();
488 if(!duck || physic.get_velocity_y() != 0) {
489 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
493 } else if(!controller->hold(Controller::LEFT)
494 && controller->hold(Controller::RIGHT)) {
501 // do not run if we're holding something which slows us down
502 if ( grabbed_object && grabbed_object->is_hampering() ) {
503 ax = dirsign * WALK_ACCELERATION_X;
505 if(vx >= MAX_WALK_XM && dirsign > 0) {
508 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
513 if( vx * dirsign < MAX_WALK_XM ) {
514 ax = dirsign * WALK_ACCELERATION_X;
516 ax = dirsign * RUN_ACCELERATION_X;
519 if(vx >= MAX_RUN_XM && dirsign > 0) {
522 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
528 // we can reach WALK_SPEED without any acceleration
529 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
530 vx = dirsign * WALK_SPEED;
534 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
535 vx = dirsign * speedlimit;
539 // changing directions?
540 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
542 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
543 skidding_timer.start(SKID_TIME);
544 sound_manager->play("sounds/skid.wav");
545 // dust some particles
546 Sector::current()->add_object(
548 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
549 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
550 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
560 ax *= ICE_ACCELERATION_MULTIPLIER;
563 physic.set_velocity(vx, vy);
564 physic.set_acceleration(ax, ay);
566 // we get slower when not pressing any keys
588 if (physic.get_velocity_y() != 0)
595 if (adjust_height(DUCKED_TUX_HEIGHT)) {
598 unduck_hurt_timer.stop();
605 Player::do_standup() {
613 if (adjust_height(BIG_TUX_HEIGHT)) {
615 unduck_hurt_timer.stop();
617 // if timer is not already running, start it.
618 if (unduck_hurt_timer.get_period() == 0) {
619 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
621 else if (unduck_hurt_timer.check()) {
629 Player::do_backflip() {
635 backflip_direction = (dir == LEFT)?(+1):(-1);
638 sound_manager->play("sounds/flip.wav");
639 backflip_timer.start(0.15f);
643 Player::do_jump(float yspeed) {
647 physic.set_velocity_y(yspeed);
648 //bbox.move(Vector(0, -1));
650 on_ground_flag = false;
655 sound_manager->play("sounds/bigjump.wav");
657 sound_manager->play("sounds/jump.wav");
662 Player::early_jump_apex()
664 if (!jump_early_apex)
666 jump_early_apex = true;
667 physic.set_gravity_modifier(JUMP_EARLY_APEX_FACTOR);
672 Player::do_jump_apex()
676 jump_early_apex = false;
677 physic.set_gravity_modifier(1.0f);
682 Player::handle_vertical_input()
685 if(controller->pressed(Controller::JUMP)) jump_button_timer.start(JUMP_GRACE_TIME);
686 if(controller->hold(Controller::JUMP) && jump_button_timer.started() && can_jump) {
687 jump_button_timer.stop();
689 // when running, only jump a little bit; else do a backflip
690 if ((physic.get_velocity_x() != 0) ||
691 (controller->hold(Controller::LEFT)) ||
692 (controller->hold(Controller::RIGHT)))
701 // jump a bit higher if we are running; else do a normal jump
702 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
705 // Let go of jump key
706 else if(!controller->hold(Controller::JUMP)) {
707 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
713 if(jump_early_apex && physic.get_velocity_y() >= 0) {
717 /* In case the player has pressed Down while in a certain range of air,
718 enable butt jump action */
719 if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
720 wants_buttjump = true;
721 if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
724 /* When Down is not held anymore, disable butt jump */
725 if(!controller->hold(Controller::DOWN)) {
726 wants_buttjump = false;
727 does_buttjump = false;
731 physic.set_acceleration_y(0);
734 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
735 physic.set_acceleration_y(-2000);
736 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
742 Player::handle_input()
745 handle_input_ghost();
749 handle_input_climbing();
754 if( controller->released( Controller::PEEK_LEFT ) || controller->released( Controller::PEEK_RIGHT ) ) {
757 if( controller->released( Controller::PEEK_UP ) || controller->released( Controller::PEEK_DOWN ) ) {
760 if( controller->pressed( Controller::PEEK_LEFT ) ) {
763 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
766 if(!backflipping && !jumping && on_ground()) {
767 if( controller->pressed( Controller::PEEK_UP ) ) {
769 } else if( controller->pressed( Controller::PEEK_DOWN ) ) {
774 /* Handle horizontal movement: */
775 if (!backflipping) handle_horizontal_input();
781 /* Handle vertical movement: */
782 handle_vertical_input();
785 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
786 if(Sector::current()->add_bullet(
787 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
788 : Vector(32, bbox.get_height()/2)),
790 physic.get_velocity_x(), dir))
791 shooting_timer.start(SHOOTING_TIME);
794 /* Duck or Standup! */
795 if (controller->hold(Controller::DOWN)) {
804 if(!controller->hold(Controller::ACTION) && grabbed_object) {
805 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
807 // move the grabbed object a bit away from tux
808 Rectf grabbed_bbox = moving_object->get_bbox();
810 dest.p2.y = bbox.get_top() + bbox.get_height()*0.66666;
811 dest.p1.y = dest.p2.y - grabbed_bbox.get_height();
813 dest.p2.x = bbox.get_left() - 1;
814 dest.p1.x = dest.p2.x - grabbed_bbox.get_width();
816 dest.p1.x = bbox.get_right() + 1;
817 dest.p2.x = dest.p1.x + grabbed_bbox.get_width();
819 if(Sector::current()->is_free_of_tiles(dest, true)) {
820 moving_object->set_pos(dest.p1);
821 if(controller->hold(Controller::UP)) {
822 grabbed_object->ungrab(*this, UP);
824 grabbed_object->ungrab(*this, dir);
826 grabbed_object = NULL;
829 log_debug << "Non MovingObject grabbed?!?" << std::endl;
835 Player::position_grabbed_object()
837 MovingObject* moving_object = dynamic_cast<MovingObject*>(grabbed_object);
838 assert(moving_object);
840 // Position where we will hold the lower-inner corner
841 Vector pos(get_bbox().get_left() + get_bbox().get_width()/2,
842 get_bbox().get_top() + get_bbox().get_height()*0.66666);
844 // Adjust to find the grabbed object's upper-left corner
846 pos.x -= moving_object->get_bbox().get_width();
847 pos.y -= moving_object->get_bbox().get_height();
849 grabbed_object->grab(*this, pos, dir);
855 if(controller->hold(Controller::ACTION) && !grabbed_object
857 Sector* sector = Sector::current();
860 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
862 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
865 for(Sector::Portables::iterator i = sector->portables.begin();
866 i != sector->portables.end(); ++i) {
867 Portable* portable = *i;
868 if(!portable->is_portable())
871 // make sure the Portable is a MovingObject
872 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
873 assert(moving_object);
874 if(moving_object == NULL)
877 // make sure the Portable isn't currently non-solid
878 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
880 // check if we are within reach
881 if(moving_object->get_bbox().contains(pos)) {
882 if (climbing) stop_climbing(*climbing);
883 grabbed_object = portable;
884 position_grabbed_object();
892 Player::handle_input_ghost()
896 if (controller->hold(Controller::LEFT)) {
898 vx -= MAX_RUN_XM * 2;
900 if (controller->hold(Controller::RIGHT)) {
902 vx += MAX_RUN_XM * 2;
904 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
905 vy -= MAX_RUN_XM * 2;
907 if (controller->hold(Controller::DOWN)) {
908 vy += MAX_RUN_XM * 2;
910 if (controller->hold(Controller::ACTION)) {
911 set_ghost_mode(false);
913 physic.set_velocity(vx, vy);
914 physic.set_acceleration(0, 0);
918 Player::add_coins(int count)
920 player_status->add_coins(count);
926 return player_status->coins;
930 Player::add_bonus(const std::string& bonustype)
932 BonusType type = NO_BONUS;
934 if(bonustype == "grow") {
936 } else if(bonustype == "fireflower") {
938 } else if(bonustype == "iceflower") {
940 } else if(bonustype == "none") {
943 std::ostringstream msg;
944 msg << "Unknown bonus type " << bonustype;
945 throw std::runtime_error(msg.str());
948 return add_bonus(type);
952 Player::add_bonus(BonusType type, bool animate)
954 // always ignore NO_BONUS
955 if (type == NO_BONUS) {
959 // ignore GROWUP_BONUS if we're already big
960 if (type == GROWUP_BONUS) {
961 if (player_status->bonus == GROWUP_BONUS)
963 if (player_status->bonus == FIRE_BONUS)
965 if (player_status->bonus == ICE_BONUS)
969 return set_bonus(type, animate);
973 Player::set_bonus(BonusType type, bool animate)
975 if((player_status->bonus == NO_BONUS) && (type != NO_BONUS)) {
976 if (!adjust_height(BIG_TUX_HEIGHT)) {
977 printf("can't adjust\n");
982 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
984 if (climbing) stop_climbing(*climbing);
987 if (type == NO_BONUS) {
988 if (does_buttjump) does_buttjump = false;
991 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
992 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
993 // visually lose helmet
994 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
995 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
996 Vector paccel = Vector(0, 1000);
997 std::string action = (dir==LEFT)?"left":"right";
998 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
999 if (climbing) stop_climbing(*climbing);
1001 if ((player_status->bonus == ICE_BONUS) && (animate)) {
1002 // visually lose cap
1003 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
1004 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
1005 Vector paccel = Vector(0, 1000);
1006 std::string action = (dir==LEFT)?"left":"right";
1007 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
1008 if (climbing) stop_climbing(*climbing);
1010 player_status->max_fire_bullets = 0;
1011 player_status->max_ice_bullets = 0;
1013 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
1014 if (type == ICE_BONUS) player_status->max_ice_bullets++;
1016 player_status->bonus = type;
1021 Player::set_visible(bool visible)
1023 this->visible = visible;
1025 set_group(COLGROUP_MOVING);
1027 set_group(COLGROUP_DISABLED);
1031 Player::get_visible()
1039 kick_timer.start(KICK_TIME);
1043 Player::draw(DrawingContext& context)
1048 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
1049 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
1050 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
1051 float py = Sector::current()->camera->get_translation().y;
1052 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
1053 context.draw_surface(airarrow, Vector(px, py), LAYER_HUD - 1);
1056 std::string sa_prefix = "";
1057 std::string sa_postfix = "";
1059 if (player_status->bonus == GROWUP_BONUS)
1061 else if (player_status->bonus == FIRE_BONUS)
1063 else if (player_status->bonus == ICE_BONUS)
1066 sa_prefix = "small";
1069 sa_postfix = "-left";
1071 sa_postfix = "-right";
1073 /* Set Tux sprite action */
1075 sprite->set_action("gameover");
1078 sprite->set_action_continued("grow"+sa_postfix);
1079 // while growing, do not change action
1080 // do_duck() will take care of cancelling growing manually
1081 // update() will take care of cancelling when growing completed
1083 else if (climbing) {
1084 sprite->set_action(sa_prefix+"-climbing"+sa_postfix);
1086 else if (backflipping) {
1087 sprite->set_action(sa_prefix+"-backflip"+sa_postfix);
1089 else if (duck && is_big()) {
1090 sprite->set_action(sa_prefix+"-duck"+sa_postfix);
1092 else if (skidding_timer.started() && !skidding_timer.check()) {
1093 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1095 else if (kick_timer.started() && !kick_timer.check()) {
1096 sprite->set_action(sa_prefix+"-kick"+sa_postfix);
1098 else if ((wants_buttjump || does_buttjump) && is_big()) {
1099 sprite->set_action(sa_prefix+"-buttjump"+sa_postfix);
1101 else if (!on_ground()) {
1102 sprite->set_action(sa_prefix+"-jump"+sa_postfix);
1105 if (fabsf(physic.get_velocity_x()) < 1.0f) {
1106 // Determine which idle stage we're at
1107 if (sprite->get_action().find("-stand-") == std::string::npos && sprite->get_action().find("-idle-") == std::string::npos) {
1109 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1111 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1113 else if (idle_timer.check() || (IDLE_TIME[idle_stage] == 0 && sprite->animation_done())) {
1115 if (idle_stage >= IDLE_STAGE_COUNT)
1118 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1120 if (IDLE_TIME[idle_stage] == 0)
1121 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix, 1);
1123 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1126 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1130 sprite->set_action(sa_prefix+"-walk"+sa_postfix);
1135 // Tux is holding something
1136 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1137 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
1145 if (safe_timer.started() && size_t(game_time*40)%2)
1148 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1154 Player::collision_tile(uint32_t tile_attributes)
1156 if(tile_attributes & Tile::HURTS)
1161 if( tile_attributes & Tile::WATER ){
1167 if( tile_attributes & Tile::WATER ){
1170 sound_manager->play( "sounds/splash.ogg" );
1175 if(tile_attributes & Tile::ICE) {
1176 ice_this_frame = true;
1182 Player::collision_solid(const CollisionHit& hit)
1185 if(physic.get_velocity_y() > 0)
1186 physic.set_velocity_y(0);
1188 on_ground_flag = true;
1189 floor_normal = hit.slope_normal;
1192 if (does_buttjump) {
1193 does_buttjump = false;
1194 physic.set_velocity_y(-300);
1195 on_ground_flag = false;
1196 Sector::current()->add_object(new Particles(
1197 Vector(get_bbox().p2.x, get_bbox().p2.y),
1199 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1201 Sector::current()->add_object(new Particles(
1202 Vector(get_bbox().p1.x, get_bbox().p2.y),
1204 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1206 Sector::current()->camera->shake(.1f, 0, 5);
1209 } else if(hit.top) {
1210 if(physic.get_velocity_y() < 0)
1211 physic.set_velocity_y(.2f);
1214 if(hit.left || hit.right) {
1215 physic.set_velocity_x(0);
1220 if(hit.left || hit.right) {
1222 } else if(hit.top || hit.bottom) {
1229 Player::collision(GameObject& other, const CollisionHit& hit)
1231 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1236 Player* player = dynamic_cast<Player*> (&other);
1241 if(hit.left || hit.right) {
1242 try_grab(); //grab objects right now, in update it will be too late
1244 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1245 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1246 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1247 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1249 if(controller->pressed(Controller::UP))
1250 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1256 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1257 if(badguy != NULL) {
1258 if(safe_timer.started() || invincible_timer.started())
1268 Player::make_invincible()
1270 sound_manager->play("sounds/invincible_start.ogg");
1271 invincible_timer.start(TUX_INVINCIBLE_TIME);
1272 Sector::current()->play_music(HERRING_MUSIC);
1277 Player::kill(bool completely)
1279 if(dying || deactivated || is_winning() )
1282 if(!completely && (safe_timer.started() || invincible_timer.started()))
1287 if (climbing) stop_climbing(*climbing);
1289 physic.set_velocity_x(0);
1291 if(!completely && is_big()) {
1292 sound_manager->play("sounds/hurt.wav");
1294 if(player_status->bonus == FIRE_BONUS
1295 || player_status->bonus == ICE_BONUS) {
1296 safe_timer.start(TUX_SAFE_TIME);
1297 set_bonus(GROWUP_BONUS, true);
1298 } else if(player_status->bonus == GROWUP_BONUS) {
1299 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1300 adjust_height(SMALL_TUX_HEIGHT);
1302 backflipping = false;
1303 set_bonus(NO_BONUS, true);
1304 } else if(player_status->bonus == NO_BONUS) {
1305 safe_timer.start(TUX_SAFE_TIME);
1306 adjust_height(SMALL_TUX_HEIGHT);
1310 sound_manager->play("sounds/kill.wav");
1312 // do not die when in edit mode
1314 set_ghost_mode(true);
1318 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1320 for (int i = 0; i < 5; i++)
1322 // the numbers: starting x, starting y, velocity y
1323 Sector::current()->add_object(new FallingCoin(get_pos() +
1324 Vector(graphicsRandom.rand(5), graphicsRandom.rand(-32,18)),
1325 graphicsRandom.rand(-100,100)));
1327 player_status->coins -= std::max(player_status->coins/10, 25);
1331 GameSession::current()->set_reset_point("", Vector());
1333 physic.enable_gravity(true);
1334 physic.set_gravity_modifier(1.0f); // Undo jump_early_apex
1336 invincible_timer.stop();
1337 physic.set_acceleration(0, 0);
1338 physic.set_velocity(0, -700);
1339 set_bonus(NO_BONUS, true);
1341 dying_timer.start(3.0);
1342 set_group(COLGROUP_DISABLED);
1344 // TODO: need nice way to handle players dying in co-op mode
1345 Sector::current()->effect->fade_out(3.0);
1346 sound_manager->stop_music(3.0);
1351 Player::move(const Vector& vector)
1355 // Reset size to get correct hitbox if Tux was eg. ducked before moving
1357 set_size(TUX_WIDTH, BIG_TUX_HEIGHT);
1359 set_size(TUX_WIDTH, SMALL_TUX_HEIGHT);
1361 backflipping = false;
1362 last_ground_y = vector.y;
1363 if (climbing) stop_climbing(*climbing);
1369 Player::check_bounds()
1371 /* Keep tux in sector bounds: */
1372 if (get_pos().x < 0) {
1373 // Lock Tux to the size of the level, so that he doesn't fall off
1375 set_pos(Vector(0, get_pos().y));
1378 if (get_bbox().get_right() > Sector::current()->get_width()) {
1379 // Lock Tux to the size of the level, so that he doesn't fall off
1381 set_pos(Vector(Sector::current()->get_width() - get_bbox().get_width(), get_pos().y));
1384 /* fallen out of the level? */
1385 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1392 Player::add_velocity(const Vector& velocity)
1394 physic.set_velocity(physic.get_velocity() + velocity);
1398 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1400 if (end_speed.x > 0)
1401 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1402 if (end_speed.x < 0)
1403 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1404 if (end_speed.y > 0)
1405 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1406 if (end_speed.y < 0)
1407 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1411 Player::get_velocity()
1413 return physic.get_velocity();
1417 Player::bounce(BadGuy& )
1419 if(controller->hold(Controller::JUMP))
1420 physic.set_velocity_y(-520);
1422 physic.set_velocity_y(-300);
1425 //scripting Functions Below
1428 Player::deactivate()
1433 physic.set_velocity_x(0);
1434 physic.set_velocity_y(0);
1435 physic.set_acceleration_x(0);
1436 physic.set_acceleration_y(0);
1437 if (climbing) stop_climbing(*climbing);
1445 deactivated = false;
1448 void Player::walk(float speed)
1450 physic.set_velocity_x(speed);
1454 Player::set_ghost_mode(bool enable)
1456 if (ghost_mode == enable)
1459 if (climbing) stop_climbing(*climbing);
1463 set_group(COLGROUP_DISABLED);
1464 physic.enable_gravity(false);
1465 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1468 set_group(COLGROUP_MOVING);
1469 physic.enable_gravity(true);
1470 log_debug << "You feel solid again." << std::endl;
1475 Player::set_edit_mode(bool enable)
1481 Player::start_climbing(Climbable& climbable)
1483 if (climbing || !&climbable) return;
1485 climbing = &climbable;
1486 physic.enable_gravity(false);
1487 physic.set_velocity(0, 0);
1488 physic.set_acceleration(0, 0);
1490 backflipping = false;
1491 backflip_direction = 0;
1496 Player::stop_climbing(Climbable& /*climbable*/)
1498 if (!climbing) return;
1502 if (grabbed_object) {
1503 grabbed_object->ungrab(*this, dir);
1504 grabbed_object = NULL;
1507 physic.enable_gravity(true);
1508 physic.set_velocity(0, 0);
1509 physic.set_acceleration(0, 0);
1511 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1512 on_ground_flag = true;
1513 // TODO: This won't help. Why?
1519 Player::handle_input_climbing()
1522 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1528 if (controller->hold(Controller::LEFT)) {
1532 if (controller->hold(Controller::RIGHT)) {
1536 if (controller->hold(Controller::UP)) {
1539 if (controller->hold(Controller::DOWN)) {
1542 if (controller->hold(Controller::JUMP)) {
1544 stop_climbing(*climbing);
1550 if (controller->hold(Controller::ACTION)) {
1551 stop_climbing(*climbing);
1554 physic.set_velocity(vx, vy);
1555 physic.set_acceleration(0, 0);