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 = 48;
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);
205 fall_mode = ON_GROUND;
207 jump_early_apex = false;
209 wants_buttjump = false;
210 does_buttjump = false;
213 backflipping = false;
214 backflip_direction = 0;
218 ice_this_frame = false;
219 speedlimit = 0; //no special limit
221 on_ground_flag = false;
222 grabbed_object = NULL;
230 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
235 scripting::expose_object(vm, table_idx, dynamic_cast<scripting::Player *>(this), name, false);
239 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
244 scripting::unexpose_object(vm, table_idx, name);
248 Player::get_speedlimit()
254 Player::set_speedlimit(float newlimit)
260 Player::set_controller(Controller* controller)
262 this->controller = controller;
266 Player::use_scripting_controller(bool use_or_release)
268 if ((use_or_release == true) && (controller != scripting_controller.get())) {
269 scripting_controller_old = get_controller();
270 set_controller(scripting_controller.get());
272 if ((use_or_release == false) && (controller == scripting_controller.get())) {
273 set_controller(scripting_controller_old);
274 scripting_controller_old = 0;
279 Player::do_scripting_controller(std::string control, bool pressed)
281 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
282 if(control == std::string(Controller::controlNames[i])) {
283 scripting_controller->press(Controller::Control(i), pressed);
289 Player::adjust_height(float new_height)
292 bbox2.move(Vector(0, bbox.get_height() - new_height));
293 bbox2.set_height(new_height);
296 if(new_height > bbox.get_height()) {
297 Rectf additional_space = bbox2;
298 additional_space.set_height(new_height - bbox.get_height());
299 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
303 // adjust bbox accordingly
304 // note that we use members of moving_object for this, so we can run this during CD, too
306 set_size(bbox2.get_width(), bbox2.get_height());
311 Player::trigger_sequence(std::string sequence_name)
313 if (climbing) stop_climbing(*climbing);
314 backflipping = false;
315 backflip_direction = 0;
316 GameSession::current()->start_sequence(sequence_name);
320 Player::update(float elapsed_time)
327 if(dying && dying_timer.check()) {
332 if(!dying && !deactivated)
335 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
339 // extend/shrink tux collision rectangle so that we fall through/walk over 1
341 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
342 set_width(RUNNING_TUX_WIDTH);
344 set_width(TUX_WIDTH);
347 // on downward slopes, adjust vertical velocity so tux walks smoothly down
349 if(floor_normal.y != 0) {
350 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
351 physic.set_velocity_y(250);
356 // handle backflipping
358 //prevent player from changing direction when backflipping
359 dir = (backflip_direction == 1) ? LEFT : RIGHT;
360 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
365 fall_mode = ON_GROUND;
366 last_ground_y = get_pos().y;
368 if(get_pos().y > last_ground_y)
370 else if(fall_mode == ON_GROUND)
374 // check if we landed
377 if (backflipping && (!backflip_timer.started())) {
378 backflipping = false;
379 backflip_direction = 0;
381 // if controls are currently deactivated, we take care of standing up ourselves
387 // calculate movement for this frame
388 movement = physic.get_movement(elapsed_time);
390 if(grabbed_object != NULL && !dying) {
391 position_grabbed_object();
394 if(grabbed_object != NULL && dying){
395 grabbed_object->ungrab(*this, dir);
396 grabbed_object = NULL;
399 if(!ice_this_frame && on_ground())
402 on_ground_flag = false;
403 ice_this_frame = false;
405 // when invincible, spawn particles
406 if (invincible_timer.started())
408 if (graphicsRandom.rand(0, 2) == 0) {
409 float px = graphicsRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
410 float py = graphicsRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
411 Vector ppos = Vector(px, py);
412 Vector pspeed = Vector(0, 0);
413 Vector paccel = Vector(0, 0);
414 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite",
415 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
416 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) ?
417 // make every other a longer sparkle to make trail a bit fuzzy
418 (size_t(game_time*20)%2) ? "small" : "medium"
420 "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
425 if (sprite->animation_done()) growing = false;
433 return on_ground_flag;
439 if(player_status->bonus == NO_BONUS)
446 Player::apply_friction()
448 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
449 physic.set_velocity_x(0);
450 physic.set_acceleration_x(0);
452 float friction = WALK_ACCELERATION_X * (on_ice ? ICE_FRICTION_MULTIPLIER : NORMAL_FRICTION_MULTIPLIER);
453 if(physic.get_velocity_x() < 0) {
454 physic.set_acceleration_x(friction);
455 } else if(physic.get_velocity_x() > 0) {
456 physic.set_acceleration_x(-friction);
457 } // no friction for physic.get_velocity_x() == 0
462 Player::handle_horizontal_input()
464 float vx = physic.get_velocity_x();
465 float vy = physic.get_velocity_y();
466 float ax = physic.get_acceleration_x();
467 float ay = physic.get_acceleration_y();
470 if(!duck || physic.get_velocity_y() != 0) {
471 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
475 } else if(!controller->hold(Controller::LEFT)
476 && controller->hold(Controller::RIGHT)) {
483 // do not run if we're holding something
484 if ( false /* grabbed_extra_heavy_object */) {
485 ax = dirsign * WALK_ACCELERATION_X;
487 if(vx >= MAX_WALK_XM && dirsign > 0) {
490 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
495 if( vx * dirsign < MAX_WALK_XM ) {
496 ax = dirsign * WALK_ACCELERATION_X;
498 ax = dirsign * RUN_ACCELERATION_X;
501 if(vx >= MAX_RUN_XM && dirsign > 0) {
504 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
510 // we can reach WALK_SPEED without any acceleration
511 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
512 vx = dirsign * WALK_SPEED;
516 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
517 vx = dirsign * speedlimit;
521 // changing directions?
522 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
524 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
525 skidding_timer.start(SKID_TIME);
526 sound_manager->play("sounds/skid.wav");
527 // dust some particles
528 Sector::current()->add_object(
530 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
531 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
532 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
542 ax *= ICE_ACCELERATION_MULTIPLIER;
545 physic.set_velocity(vx, vy);
546 physic.set_acceleration(ax, ay);
548 // we get slower when not pressing any keys
570 if (physic.get_velocity_y() != 0)
577 if (adjust_height(DUCKED_TUX_HEIGHT)) {
580 unduck_hurt_timer.stop();
587 Player::do_standup() {
595 if (adjust_height(BIG_TUX_HEIGHT)) {
597 unduck_hurt_timer.stop();
599 // if timer is not already running, start it.
600 if (unduck_hurt_timer.get_period() == 0) {
601 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
603 else if (unduck_hurt_timer.check()) {
611 Player::do_backflip() {
617 backflip_direction = (dir == LEFT)?(+1):(-1);
620 sound_manager->play("sounds/flip.wav");
621 backflip_timer.start(0.15f);
625 Player::do_jump(float yspeed) {
629 physic.set_velocity_y(yspeed);
630 //bbox.move(Vector(0, -1));
632 on_ground_flag = false;
637 sound_manager->play("sounds/bigjump.wav");
639 sound_manager->play("sounds/jump.wav");
644 Player::early_jump_apex()
646 if (!jump_early_apex)
648 jump_early_apex = true;
649 physic.set_gravity_modifier(JUMP_EARLY_APEX_FACTOR);
654 Player::do_jump_apex()
658 jump_early_apex = false;
659 physic.set_gravity_modifier(1.0f);
664 Player::handle_vertical_input()
667 if(controller->pressed(Controller::JUMP)) jump_button_timer.start(JUMP_GRACE_TIME);
668 if(controller->hold(Controller::JUMP) && jump_button_timer.started() && can_jump) {
669 jump_button_timer.stop();
671 // when running, only jump a little bit; else do a backflip
672 if ((physic.get_velocity_x() != 0) ||
673 (controller->hold(Controller::LEFT)) ||
674 (controller->hold(Controller::RIGHT)))
683 // jump a bit higher if we are running; else do a normal jump
684 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
687 // Let go of jump key
688 else if(!controller->hold(Controller::JUMP)) {
689 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
695 if(jump_early_apex && physic.get_velocity_y() >= 0) {
699 /* In case the player has pressed Down while in a certain range of air,
700 enable butt jump action */
701 if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
702 wants_buttjump = true;
703 if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
706 /* When Down is not held anymore, disable butt jump */
707 if(!controller->hold(Controller::DOWN)) {
708 wants_buttjump = false;
709 does_buttjump = false;
713 physic.set_acceleration_y(0);
716 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
717 physic.set_acceleration_y(-2000);
718 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
724 Player::handle_input()
727 handle_input_ghost();
731 handle_input_climbing();
736 if( controller->released( Controller::PEEK_LEFT ) || controller->released( Controller::PEEK_RIGHT ) ) {
739 if( controller->released( Controller::PEEK_UP ) || controller->released( Controller::PEEK_DOWN ) ) {
742 if( controller->pressed( Controller::PEEK_LEFT ) ) {
745 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
748 if(!backflipping && !jumping && on_ground()) {
749 if( controller->pressed( Controller::PEEK_UP ) ) {
751 } else if( controller->pressed( Controller::PEEK_DOWN ) ) {
756 /* Handle horizontal movement: */
757 if (!backflipping) handle_horizontal_input();
763 /* Handle vertical movement: */
764 handle_vertical_input();
767 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
768 if(Sector::current()->add_bullet(
769 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
770 : Vector(32, bbox.get_height()/2)),
772 physic.get_velocity_x(), dir))
773 shooting_timer.start(SHOOTING_TIME);
776 /* Duck or Standup! */
777 if (controller->hold(Controller::DOWN)) {
786 if(!controller->hold(Controller::ACTION) && grabbed_object) {
787 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
789 // move the grabbed object a bit away from tux
790 Rectf grabbed_bbox = moving_object->get_bbox();
792 dest.p2.y = bbox.get_top() + bbox.get_height()*0.66666;
793 dest.p1.y = dest.p2.y - grabbed_bbox.get_height();
795 dest.p2.x = bbox.get_left() - 1;
796 dest.p1.x = dest.p2.x - grabbed_bbox.get_width();
798 dest.p1.x = bbox.get_right() + 1;
799 dest.p2.x = dest.p1.x + grabbed_bbox.get_width();
801 if(Sector::current()->is_free_of_movingstatics(dest)) {
802 moving_object->set_pos(dest.p1);
803 if(controller->hold(Controller::UP)) {
804 grabbed_object->ungrab(*this, UP);
806 grabbed_object->ungrab(*this, dir);
808 grabbed_object = NULL;
811 log_debug << "Non MovingObject grabbed?!?" << std::endl;
817 Player::position_grabbed_object()
819 MovingObject* moving_object = dynamic_cast<MovingObject*>(grabbed_object);
820 assert(moving_object);
822 // Position where we will hold the lower-inner corner
823 Vector pos(get_bbox().get_left() + get_bbox().get_width()/2,
824 get_bbox().get_top() + get_bbox().get_height()*0.66666);
826 // Adjust to find the grabbed object's upper-left corner
828 pos.x -= moving_object->get_bbox().get_width();
829 pos.y -= moving_object->get_bbox().get_height();
831 grabbed_object->grab(*this, pos, dir);
837 if(controller->hold(Controller::ACTION) && !grabbed_object
839 Sector* sector = Sector::current();
842 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
844 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
847 for(Sector::Portables::iterator i = sector->portables.begin();
848 i != sector->portables.end(); ++i) {
849 Portable* portable = *i;
850 if(!portable->is_portable())
853 // make sure the Portable is a MovingObject
854 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
855 assert(moving_object);
856 if(moving_object == NULL)
859 // make sure the Portable isn't currently non-solid
860 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
862 // check if we are within reach
863 if(moving_object->get_bbox().contains(pos)) {
864 if (climbing) stop_climbing(*climbing);
865 grabbed_object = portable;
866 position_grabbed_object();
874 Player::handle_input_ghost()
878 if (controller->hold(Controller::LEFT)) {
880 vx -= MAX_RUN_XM * 2;
882 if (controller->hold(Controller::RIGHT)) {
884 vx += MAX_RUN_XM * 2;
886 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
887 vy -= MAX_RUN_XM * 2;
889 if (controller->hold(Controller::DOWN)) {
890 vy += MAX_RUN_XM * 2;
892 if (controller->hold(Controller::ACTION)) {
893 set_ghost_mode(false);
895 physic.set_velocity(vx, vy);
896 physic.set_acceleration(0, 0);
900 Player::add_coins(int count)
902 player_status->add_coins(count);
908 return player_status->coins;
912 Player::add_bonus(const std::string& bonustype)
914 BonusType type = NO_BONUS;
916 if(bonustype == "grow") {
918 } else if(bonustype == "fireflower") {
920 } else if(bonustype == "iceflower") {
922 } else if(bonustype == "none") {
925 std::ostringstream msg;
926 msg << "Unknown bonus type " << bonustype;
927 throw std::runtime_error(msg.str());
930 return add_bonus(type);
934 Player::add_bonus(BonusType type, bool animate)
936 // always ignore NO_BONUS
937 if (type == NO_BONUS) {
941 // ignore GROWUP_BONUS if we're already big
942 if (type == GROWUP_BONUS) {
943 if (player_status->bonus == GROWUP_BONUS)
945 if (player_status->bonus == FIRE_BONUS)
947 if (player_status->bonus == ICE_BONUS)
951 return set_bonus(type, animate);
955 Player::set_bonus(BonusType type, bool animate)
957 if((player_status->bonus == NO_BONUS) && (type != NO_BONUS)) {
958 if (!adjust_height(BIG_TUX_HEIGHT)) {
959 printf("can't adjust\n");
964 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
966 if (climbing) stop_climbing(*climbing);
969 if (type == NO_BONUS) {
970 if (does_buttjump) does_buttjump = false;
973 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
974 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
975 // visually lose helmet
976 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
977 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
978 Vector paccel = Vector(0, 1000);
979 std::string action = (dir==LEFT)?"left":"right";
980 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
981 if (climbing) stop_climbing(*climbing);
983 if ((player_status->bonus == ICE_BONUS) && (animate)) {
985 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
986 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
987 Vector paccel = Vector(0, 1000);
988 std::string action = (dir==LEFT)?"left":"right";
989 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
990 if (climbing) stop_climbing(*climbing);
992 player_status->max_fire_bullets = 0;
993 player_status->max_ice_bullets = 0;
995 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
996 if (type == ICE_BONUS) player_status->max_ice_bullets++;
998 player_status->bonus = type;
1003 Player::set_visible(bool visible)
1005 this->visible = visible;
1007 set_group(COLGROUP_MOVING);
1009 set_group(COLGROUP_DISABLED);
1013 Player::get_visible()
1021 kick_timer.start(KICK_TIME);
1025 Player::draw(DrawingContext& context)
1030 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
1031 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
1032 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
1033 float py = Sector::current()->camera->get_translation().y;
1034 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
1035 context.draw_surface(airarrow, Vector(px, py), LAYER_HUD - 1);
1038 std::string sa_prefix = "";
1039 std::string sa_postfix = "";
1041 if (player_status->bonus == GROWUP_BONUS)
1043 else if (player_status->bonus == FIRE_BONUS)
1045 else if (player_status->bonus == ICE_BONUS)
1048 sa_prefix = "small";
1051 sa_postfix = "-left";
1053 sa_postfix = "-right";
1055 /* Set Tux sprite action */
1057 sprite->set_action("gameover");
1060 sprite->set_action_continued("grow"+sa_postfix);
1061 // while growing, do not change action
1062 // do_duck() will take care of cancelling growing manually
1063 // update() will take care of cancelling when growing completed
1065 else if (climbing) {
1066 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1068 else if (backflipping) {
1069 sprite->set_action(sa_prefix+"-backflip"+sa_postfix);
1071 else if (duck && is_big()) {
1072 sprite->set_action(sa_prefix+"-duck"+sa_postfix);
1074 else if (skidding_timer.started() && !skidding_timer.check()) {
1075 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1077 else if (kick_timer.started() && !kick_timer.check()) {
1078 sprite->set_action(sa_prefix+"-kick"+sa_postfix);
1080 else if ((wants_buttjump || does_buttjump) && is_big()) {
1081 sprite->set_action(sa_prefix+"-buttjump"+sa_postfix);
1083 else if (!on_ground()) {
1084 sprite->set_action(sa_prefix+"-jump"+sa_postfix);
1087 if (fabsf(physic.get_velocity_x()) < 1.0f) {
1088 // Determine which idle stage we're at
1089 if (sprite->get_action().find("-stand-") == std::string::npos && sprite->get_action().find("-idle-") == std::string::npos) {
1091 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1093 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1095 else if (idle_timer.check() || (IDLE_TIME[idle_stage] == 0 && sprite->animation_done())) {
1097 if (idle_stage >= IDLE_STAGE_COUNT)
1100 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1102 if (IDLE_TIME[idle_stage] == 0)
1103 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix, 1);
1105 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1108 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1112 sprite->set_action(sa_prefix+"-walk"+sa_postfix);
1117 // Tux is holding something
1118 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1119 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
1127 if (safe_timer.started() && size_t(game_time*40)%2)
1130 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1136 Player::collision_tile(uint32_t tile_attributes)
1138 if(tile_attributes & Tile::HURTS)
1143 if( tile_attributes & Tile::WATER ){
1149 if( tile_attributes & Tile::WATER ){
1152 sound_manager->play( "sounds/splash.ogg" );
1157 if(tile_attributes & Tile::ICE) {
1158 ice_this_frame = true;
1164 Player::collision_solid(const CollisionHit& hit)
1167 if(physic.get_velocity_y() > 0)
1168 physic.set_velocity_y(0);
1170 on_ground_flag = true;
1171 floor_normal = hit.slope_normal;
1174 if (does_buttjump) {
1175 does_buttjump = false;
1176 physic.set_velocity_y(-300);
1177 on_ground_flag = false;
1178 Sector::current()->add_object(new Particles(
1179 Vector(get_bbox().p2.x, get_bbox().p2.y),
1181 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1183 Sector::current()->add_object(new Particles(
1184 Vector(get_bbox().p1.x, get_bbox().p2.y),
1186 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1188 Sector::current()->camera->shake(.1f, 0, 5);
1191 } else if(hit.top) {
1192 if(physic.get_velocity_y() < 0)
1193 physic.set_velocity_y(.2f);
1196 if(hit.left || hit.right) {
1197 physic.set_velocity_x(0);
1202 if(hit.left || hit.right) {
1204 } else if(hit.top || hit.bottom) {
1211 Player::collision(GameObject& other, const CollisionHit& hit)
1213 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1218 Player* player = dynamic_cast<Player*> (&other);
1223 if(hit.left || hit.right) {
1224 try_grab(); //grab objects right now, in update it will be too late
1226 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1227 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1228 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1229 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1231 if(controller->pressed(Controller::UP))
1232 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1238 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1239 if(badguy != NULL) {
1240 if(safe_timer.started() || invincible_timer.started())
1250 Player::make_invincible()
1252 sound_manager->play("sounds/invincible_start.ogg");
1253 invincible_timer.start(TUX_INVINCIBLE_TIME);
1254 Sector::current()->play_music(HERRING_MUSIC);
1259 Player::kill(bool completely)
1261 if(dying || deactivated)
1264 if(!completely && (safe_timer.started() || invincible_timer.started()))
1269 if (climbing) stop_climbing(*climbing);
1271 physic.set_velocity_x(0);
1273 if(!completely && is_big()) {
1274 sound_manager->play("sounds/hurt.wav");
1276 if(player_status->bonus == FIRE_BONUS
1277 || player_status->bonus == ICE_BONUS) {
1278 safe_timer.start(TUX_SAFE_TIME);
1279 set_bonus(GROWUP_BONUS, true);
1280 } else if(player_status->bonus == GROWUP_BONUS) {
1281 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1282 adjust_height(SMALL_TUX_HEIGHT);
1284 backflipping = false;
1285 set_bonus(NO_BONUS, true);
1286 } else if(player_status->bonus == NO_BONUS) {
1287 safe_timer.start(TUX_SAFE_TIME);
1288 adjust_height(SMALL_TUX_HEIGHT);
1292 sound_manager->play("sounds/kill.wav");
1294 // do not die when in edit mode
1296 set_ghost_mode(true);
1300 physic.enable_gravity(true);
1301 physic.set_gravity_modifier(1.0f); // Undo jump_early_apex
1303 invincible_timer.stop();
1304 physic.set_acceleration(0, 0);
1305 physic.set_velocity(0, -700);
1306 set_bonus(NO_BONUS, true);
1308 dying_timer.start(3.0);
1309 set_group(COLGROUP_DISABLED);
1311 // TODO: need nice way to handle players dying in co-op mode
1312 Sector::current()->effect->fade_out(3.0);
1313 sound_manager->stop_music(3.0);
1318 Player::move(const Vector& vector)
1322 // Reset size to get correct hitbox if Tux was eg. ducked before moving
1324 set_size(TUX_WIDTH, BIG_TUX_HEIGHT);
1326 set_size(TUX_WIDTH, SMALL_TUX_HEIGHT);
1328 backflipping = false;
1329 last_ground_y = vector.y;
1330 if (climbing) stop_climbing(*climbing);
1336 Player::check_bounds()
1338 /* Keep tux in sector bounds: */
1339 if (get_pos().x < 0) {
1340 // Lock Tux to the size of the level, so that he doesn't fall off
1342 set_pos(Vector(0, get_pos().y));
1345 if (get_bbox().get_right() > Sector::current()->get_width()) {
1346 // Lock Tux to the size of the level, so that he doesn't fall off
1348 set_pos(Vector(Sector::current()->get_width() - get_bbox().get_width(), get_pos().y));
1351 /* fallen out of the level? */
1352 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1359 Player::add_velocity(const Vector& velocity)
1361 physic.set_velocity(physic.get_velocity() + velocity);
1365 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1367 if (end_speed.x > 0)
1368 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1369 if (end_speed.x < 0)
1370 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1371 if (end_speed.y > 0)
1372 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1373 if (end_speed.y < 0)
1374 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1378 Player::get_velocity()
1380 return physic.get_velocity();
1384 Player::bounce(BadGuy& )
1386 if(controller->hold(Controller::JUMP))
1387 physic.set_velocity_y(-520);
1389 physic.set_velocity_y(-300);
1392 //scripting Functions Below
1395 Player::deactivate()
1400 physic.set_velocity_x(0);
1401 physic.set_velocity_y(0);
1402 physic.set_acceleration_x(0);
1403 physic.set_acceleration_y(0);
1404 if (climbing) stop_climbing(*climbing);
1412 deactivated = false;
1415 void Player::walk(float speed)
1417 physic.set_velocity_x(speed);
1421 Player::set_ghost_mode(bool enable)
1423 if (ghost_mode == enable)
1426 if (climbing) stop_climbing(*climbing);
1430 set_group(COLGROUP_DISABLED);
1431 physic.enable_gravity(false);
1432 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1435 set_group(COLGROUP_MOVING);
1436 physic.enable_gravity(true);
1437 log_debug << "You feel solid again." << std::endl;
1442 Player::set_edit_mode(bool enable)
1448 Player::start_climbing(Climbable& climbable)
1450 if (climbing == &climbable) return;
1452 climbing = &climbable;
1453 physic.enable_gravity(false);
1454 physic.set_velocity(0, 0);
1455 physic.set_acceleration(0, 0);
1459 Player::stop_climbing(Climbable& /*climbable*/)
1461 if (!climbing) return;
1465 if (grabbed_object) {
1466 grabbed_object->ungrab(*this, dir);
1467 grabbed_object = NULL;
1470 physic.enable_gravity(true);
1471 physic.set_velocity(0, 0);
1472 physic.set_acceleration(0, 0);
1474 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1475 on_ground_flag = true;
1476 // TODO: This won't help. Why?
1482 Player::handle_input_climbing()
1485 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1491 if (controller->hold(Controller::LEFT)) {
1495 if (controller->hold(Controller::RIGHT)) {
1499 if (controller->hold(Controller::UP)) {
1502 if (controller->hold(Controller::DOWN)) {
1505 if (controller->hold(Controller::JUMP)) {
1507 stop_climbing(*climbing);
1513 if (controller->hold(Controller::ACTION)) {
1514 stop_climbing(*climbing);
1517 physic.set_velocity(vx, vy);
1518 physic.set_acceleration(0, 0);