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/input_manager.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 float BUTTJUMP_MIN_VELOCITY_Y = 400.0f;
44 static const float SHOOTING_TIME = .150f;
45 static const float GLIDE_TIME_PER_FLOWER = 0.5f;
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 /** bonus run velocity addition (pixel/s) */
77 static const float BONUS_RUN_XM = 80;
78 /** maximum horizontal climb velocity */
79 static const float MAX_CLIMB_XM = 96;
80 /** maximum vertical climb velocity */
81 static const float MAX_CLIMB_YM = 128;
82 /** maximum vertical glide velocity */
83 static const float MAX_GLIDE_YM = 128;
84 /** instant velocity when tux starts to walk */
85 static const float WALK_SPEED = 100;
87 /** multiplied by WALK_ACCELERATION to give friction */
88 static const float NORMAL_FRICTION_MULTIPLIER = 1.5f;
89 /** multiplied by WALK_ACCELERATION to give friction */
90 static const float ICE_FRICTION_MULTIPLIER = 0.1f;
91 static const float ICE_ACCELERATION_MULTIPLIER = 0.25f;
93 /** time of the kick (kicking mriceblock) animation */
94 static const float KICK_TIME = .3f;
96 /** if Tux cannot unduck for this long, he will get hurt */
97 static const float UNDUCK_HURT_TIME = 0.25f;
98 /** gravity is higher after the jump key is released before
99 the apex of the jump is reached */
100 static const float JUMP_EARLY_APEX_FACTOR = 3.0;
102 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) */
104 /* Tux's collision rectangle */
105 static const float TUX_WIDTH = 31.8f;
106 static const float RUNNING_TUX_WIDTH = 34;
107 static const float SMALL_TUX_HEIGHT = 30.8f;
108 static const float BIG_TUX_HEIGHT = 62.8f;
109 static const float DUCKED_TUX_HEIGHT = 31.8f;
111 bool no_water = true;
114 Player::Player(PlayerStatus* _player_status, const std::string& name_) :
117 scripting_controller(),
118 player_status(_player_status),
124 backflip_direction(),
131 scripting_controller_old(0),
135 lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-tiny.sprite")),
158 grabbed_object(NULL),
170 controller = InputManager::current()->get_controller();
171 scripting_controller.reset(new CodeController());
172 // if/when we have complete penny gfx, we can
173 // load those instead of Tux's sprite in the
175 sprite = SpriteManager::current()->create("images/creatures/tux/tux.sprite");
176 airarrow = Surface::create("images/engine/hud/airarrow.png");
177 idle_timer.start(IDLE_TIME[0]/1000.0f);
179 SoundManager::current()->preload("sounds/bigjump.wav");
180 SoundManager::current()->preload("sounds/jump.wav");
181 SoundManager::current()->preload("sounds/hurt.wav");
182 SoundManager::current()->preload("sounds/kill.wav");
183 SoundManager::current()->preload("sounds/skid.wav");
184 SoundManager::current()->preload("sounds/flip.wav");
185 SoundManager::current()->preload("sounds/invincible_start.ogg");
186 SoundManager::current()->preload("sounds/splash.ogg");
193 if (climbing) stop_climbing(*climbing);
200 set_size(TUX_WIDTH, BIG_TUX_HEIGHT);
202 set_size(TUX_WIDTH, SMALL_TUX_HEIGHT);
214 fall_mode = ON_GROUND;
216 jump_early_apex = false;
218 wants_buttjump = false;
219 does_buttjump = false;
222 backflipping = false;
223 backflip_direction = 0;
224 sprite->set_angle(0.0f);
230 ice_this_frame = false;
231 speedlimit = 0; //no special limit
232 lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
234 on_ground_flag = false;
235 grabbed_object = NULL;
243 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
248 scripting::expose_object(vm, table_idx, dynamic_cast<scripting::Player *>(this), name, false);
252 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
257 scripting::unexpose_object(vm, table_idx, name);
261 Player::get_speedlimit()
267 Player::set_speedlimit(float newlimit)
273 Player::set_controller(Controller* controller_)
275 this->controller = controller_;
279 Player::set_winning()
281 if( ! is_winning() ){
283 invincible_timer.start(10000.0f);
288 Player::use_scripting_controller(bool use_or_release)
290 if ((use_or_release == true) && (controller != scripting_controller.get())) {
291 scripting_controller_old = get_controller();
292 set_controller(scripting_controller.get());
294 if ((use_or_release == false) && (controller == scripting_controller.get())) {
295 set_controller(scripting_controller_old);
296 scripting_controller_old = 0;
301 Player::do_scripting_controller(std::string control, bool pressed)
303 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
304 if(control == std::string(Controller::controlNames[i])) {
305 scripting_controller->press(Controller::Control(i), pressed);
311 Player::adjust_height(float new_height)
314 bbox2.move(Vector(0, bbox.get_height() - new_height));
315 bbox2.set_height(new_height);
318 if(new_height > bbox.get_height()) {
319 Rectf additional_space = bbox2;
320 additional_space.set_height(new_height - bbox.get_height());
321 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
325 // adjust bbox accordingly
326 // note that we use members of moving_object for this, so we can run this during CD, too
328 set_size(bbox2.get_width(), bbox2.get_height());
333 Player::trigger_sequence(std::string sequence_name)
335 if (climbing) stop_climbing(*climbing);
336 backflipping = false;
337 backflip_direction = 0;
338 sprite->set_angle(0.0f);
339 GameSession::current()->start_sequence(sequence_name);
343 Player::update(float elapsed_time)
350 if(dying && dying_timer.check()) {
351 set_bonus(NO_BONUS, true);
356 if(!dying && !deactivated)
360 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
365 // extend/shrink tux collision rectangle so that we fall through/walk over 1
367 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
368 set_width(RUNNING_TUX_WIDTH);
370 set_width(TUX_WIDTH);
373 // on downward slopes, adjust vertical velocity so tux walks smoothly down
374 if (on_ground() && !dying) {
375 if(floor_normal.y != 0) {
376 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
377 physic.set_velocity_y(250);
382 // handle backflipping
383 if (backflipping && !dying) {
384 //prevent player from changing direction when backflipping
385 dir = (backflip_direction == 1) ? LEFT : RIGHT;
386 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
387 //rotate sprite during flip
388 sprite->set_angle(sprite->get_angle() + (dir==LEFT?1:-1) * elapsed_time * (360.0f / 0.5f));
393 fall_mode = ON_GROUND;
394 last_ground_y = get_pos().y;
396 if(get_pos().y > last_ground_y)
398 else if(fall_mode == ON_GROUND)
402 // check if we landed
405 if (backflipping && (backflip_timer.get_timegone() > 0.15f)) {
406 backflipping = false;
407 backflip_direction = 0;
408 sprite->set_angle(0.0f);
410 // if controls are currently deactivated, we take care of standing up ourselves
414 if (player_status->bonus == AIR_BONUS)
415 glide_time = player_status->max_air_time * GLIDE_TIME_PER_FLOWER;
418 // calculate movement for this frame
419 movement = physic.get_movement(elapsed_time);
421 if(grabbed_object != NULL && !dying) {
422 position_grabbed_object();
425 if(grabbed_object != NULL && dying){
426 grabbed_object->ungrab(*this, dir);
427 grabbed_object = NULL;
430 if(!ice_this_frame && on_ground())
433 on_ground_flag = false;
434 ice_this_frame = false;
436 // when invincible, spawn particles
437 if (invincible_timer.started())
439 if (graphicsRandom.rand(0, 2) == 0) {
440 float px = graphicsRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
441 float py = graphicsRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
442 Vector ppos = Vector(px, py);
443 Vector pspeed = Vector(0, 0);
444 Vector paccel = Vector(0, 0);
445 Sector::current()->add_object(std::make_shared<SpriteParticle>(
446 "images/objects/particles/sparkle.sprite",
447 // draw bright sparkle when there is lots of time left,
448 // dark sparkle when invincibility is about to end
449 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) ?
450 // make every other a longer sparkle to make trail a bit fuzzy
451 (size_t(game_time*20)%2) ? "small" : "medium"
453 "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
458 if (sprite->animation_done()) growing = false;
461 // when climbing animate only while moving
463 if((physic.get_velocity_x()==0)&&(physic.get_velocity_y()==0))
464 sprite->stop_animation();
466 sprite->set_animation_loops(-1);
474 return on_ground_flag;
480 if(player_status->bonus == NO_BONUS)
487 Player::apply_friction()
489 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
490 physic.set_velocity_x(0);
491 physic.set_acceleration_x(0);
493 float friction = WALK_ACCELERATION_X * (on_ice ? ICE_FRICTION_MULTIPLIER : NORMAL_FRICTION_MULTIPLIER);
494 if(physic.get_velocity_x() < 0) {
495 physic.set_acceleration_x(friction);
496 } else if(physic.get_velocity_x() > 0) {
497 physic.set_acceleration_x(-friction);
498 } // no friction for physic.get_velocity_x() == 0
503 Player::handle_horizontal_input()
505 float vx = physic.get_velocity_x();
506 float vy = physic.get_velocity_y();
507 float ax = physic.get_acceleration_x();
508 float ay = physic.get_acceleration_y();
511 if(!duck || physic.get_velocity_y() != 0) {
512 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
516 } else if(!controller->hold(Controller::LEFT)
517 && controller->hold(Controller::RIGHT)) {
524 // do not run if we're holding something which slows us down
525 if ( grabbed_object && grabbed_object->is_hampering() ) {
526 ax = dirsign * WALK_ACCELERATION_X;
528 if(vx >= MAX_WALK_XM && dirsign > 0) {
531 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
536 if( vx * dirsign < MAX_WALK_XM ) {
537 ax = dirsign * WALK_ACCELERATION_X;
539 ax = dirsign * RUN_ACCELERATION_X;
542 if(vx >= MAX_RUN_XM + BONUS_RUN_XM *((player_status->bonus == AIR_BONUS) ? 1 : 0) && dirsign > 0) {
543 vx = MAX_RUN_XM + BONUS_RUN_XM *((player_status->bonus == AIR_BONUS) ? 1 : 0);
545 } else if(vx <= -MAX_RUN_XM - BONUS_RUN_XM *((player_status->bonus == AIR_BONUS) ? 1 : 0) && dirsign < 0) {
546 vx = -MAX_RUN_XM - BONUS_RUN_XM *((player_status->bonus == AIR_BONUS) ? 1 : 0);
551 // we can reach WALK_SPEED without any acceleration
552 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
553 vx = dirsign * WALK_SPEED;
557 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
558 vx = dirsign * speedlimit;
562 // changing directions?
563 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
565 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
566 skidding_timer.start(SKID_TIME);
567 SoundManager::current()->play("sounds/skid.wav");
568 // dust some particles
569 Sector::current()->add_object(
570 std::make_shared<Particles>(
571 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
572 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
573 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
583 ax *= ICE_ACCELERATION_MULTIPLIER;
586 physic.set_velocity(vx, vy);
587 physic.set_acceleration(ax, ay);
589 // we get slower when not pressing any keys
611 if (physic.get_velocity_y() != 0)
618 if (adjust_height(DUCKED_TUX_HEIGHT)) {
621 unduck_hurt_timer.stop();
628 Player::do_standup() {
636 if (adjust_height(BIG_TUX_HEIGHT)) {
638 unduck_hurt_timer.stop();
640 // if timer is not already running, start it.
641 if (unduck_hurt_timer.get_period() == 0) {
642 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
644 else if (unduck_hurt_timer.check()) {
652 Player::do_backflip() {
658 backflip_direction = (dir == LEFT)?(+1):(-1);
660 do_jump((player_status->bonus == AIR_BONUS) ? -720 : -580);
661 SoundManager::current()->play("sounds/flip.wav");
662 backflip_timer.start(TUX_BACKFLIP_TIME);
666 Player::do_jump(float yspeed) {
670 physic.set_velocity_y(yspeed);
671 //bbox.move(Vector(0, -1));
673 on_ground_flag = false;
678 SoundManager::current()->play("sounds/bigjump.wav");
680 SoundManager::current()->play("sounds/jump.wav");
685 Player::early_jump_apex()
687 if (!jump_early_apex)
689 jump_early_apex = true;
690 physic.set_gravity_modifier(JUMP_EARLY_APEX_FACTOR);
695 Player::do_jump_apex()
699 jump_early_apex = false;
700 physic.set_gravity_modifier(1.0f);
705 Player::handle_vertical_input()
708 if(controller->pressed(Controller::JUMP)) jump_button_timer.start(JUMP_GRACE_TIME);
709 if(controller->hold(Controller::JUMP) && jump_button_timer.started() && can_jump) {
710 jump_button_timer.stop();
712 // when running, only jump a little bit; else do a backflip
713 if ((physic.get_velocity_x() != 0) ||
714 (controller->hold(Controller::LEFT)) ||
715 (controller->hold(Controller::RIGHT)))
724 // airflower allows for higher jumps-
725 // jump a bit higher if we are running; else do a normal jump
726 if(player_status->bonus == AIR_BONUS)
727 do_jump((fabs(physic.get_velocity_x()) > MAX_WALK_XM) ? -620 : -580);
729 do_jump((fabs(physic.get_velocity_x()) > MAX_WALK_XM) ? -580 : -520);
731 // airflower glide only when holding jump key
732 } else if (controller->hold(Controller::JUMP) && player_status->bonus == AIR_BONUS && physic.get_velocity_y() > MAX_GLIDE_YM) {
733 if (glide_time > 0 && !ability_timer.started())
734 ability_timer.start(glide_time);
735 else if (ability_timer.started()) {
736 // glide stops after some duration or if buttjump is initiated
737 if ((ability_timer.get_timeleft() <= 0.05f) || controller->hold(Controller::DOWN)) {
739 ability_timer.stop();
741 physic.set_velocity_y(MAX_GLIDE_YM);
742 physic.set_acceleration_y(0);
748 // Let go of jump key
749 else if(!controller->hold(Controller::JUMP)) {
750 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
754 if (player_status->bonus == AIR_BONUS && ability_timer.started()){
755 glide_time = ability_timer.get_timeleft();
756 ability_timer.stop();
760 if(jump_early_apex && physic.get_velocity_y() >= 0) {
764 /* In case the player has pressed Down while in a certain range of air,
765 enable butt jump action */
766 if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
767 wants_buttjump = true;
768 if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
771 /* When Down is not held anymore, disable butt jump */
772 if(!controller->hold(Controller::DOWN)) {
773 wants_buttjump = false;
774 does_buttjump = false;
778 physic.set_acceleration_y(0);
781 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
782 physic.set_acceleration_y(-2000);
783 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
789 Player::handle_input()
792 handle_input_ghost();
796 handle_input_climbing();
801 if( controller->released( Controller::PEEK_LEFT ) || controller->released( Controller::PEEK_RIGHT ) ) {
804 if( controller->released( Controller::PEEK_UP ) || controller->released( Controller::PEEK_DOWN ) ) {
807 if( controller->pressed( Controller::PEEK_LEFT ) ) {
810 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
813 if(!backflipping && !jumping && on_ground()) {
814 if( controller->pressed( Controller::PEEK_UP ) ) {
816 } else if( controller->pressed( Controller::PEEK_DOWN ) ) {
821 /* Handle horizontal movement: */
822 if (!backflipping && !stone) handle_horizontal_input();
828 /* Handle vertical movement: */
829 if (!stone) handle_vertical_input();
832 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
833 if((player_status->bonus == FIRE_BONUS &&
834 Sector::current()->get_active_bullets() < player_status->max_fire_bullets) ||
835 (player_status->bonus == ICE_BONUS &&
836 Sector::current()->get_active_bullets() < player_status->max_ice_bullets))
838 Vector pos = get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2) : Vector(32, bbox.get_height()/2));
839 auto new_bullet = std::make_shared<Bullet>(pos, physic.get_velocity_x(), dir, player_status->bonus);
840 Sector::current()->add_object(new_bullet);
842 SoundManager::current()->play("sounds/shoot.wav");
843 shooting_timer.start(SHOOTING_TIME);
848 if (controller->pressed(Controller::DOWN) && player_status->bonus == EARTH_BONUS) {
849 if (controller->hold(Controller::ACTION)) {
850 jump_early_apex = false;
858 /* Revert from Stone */
859 if (stone && !controller->hold(Controller::ACTION))
862 /* Duck or Standup! */
863 if (controller->hold(Controller::DOWN) && !stone) {
872 if(!controller->hold(Controller::ACTION) && grabbed_object) {
873 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
875 // move the grabbed object a bit away from tux
876 Rectf grabbed_bbox = moving_object->get_bbox();
878 dest_.p2.y = bbox.get_top() + bbox.get_height()*0.66666;
879 dest_.p1.y = dest_.p2.y - grabbed_bbox.get_height();
881 dest_.p2.x = bbox.get_left() - 1;
882 dest_.p1.x = dest_.p2.x - grabbed_bbox.get_width();
884 dest_.p1.x = bbox.get_right() + 1;
885 dest_.p2.x = dest_.p1.x + grabbed_bbox.get_width();
887 if(Sector::current()->is_free_of_tiles(dest_, true)) {
888 moving_object->set_pos(dest_.p1);
889 if(controller->hold(Controller::UP)) {
890 grabbed_object->ungrab(*this, UP);
892 grabbed_object->ungrab(*this, dir);
894 grabbed_object = NULL;
897 log_debug << "Non MovingObject grabbed?!?" << std::endl;
901 /* stop backflipping at will */
902 if( backflipping && ( !controller->hold(Controller::JUMP) && !backflip_timer.started()) ){
903 backflipping = false;
904 backflip_direction = 0;
905 sprite->set_angle(0.0f);
910 Player::position_grabbed_object()
912 MovingObject* moving_object = dynamic_cast<MovingObject*>(grabbed_object);
913 assert(moving_object);
915 // Position where we will hold the lower-inner corner
916 Vector pos(get_bbox().get_left() + get_bbox().get_width()/2,
917 get_bbox().get_top() + get_bbox().get_height()*0.66666);
919 // Adjust to find the grabbed object's upper-left corner
921 pos.x -= moving_object->get_bbox().get_width();
922 pos.y -= moving_object->get_bbox().get_height();
924 grabbed_object->grab(*this, pos, dir);
930 if(controller->hold(Controller::ACTION) && !grabbed_object
932 Sector* sector = Sector::current();
935 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
937 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
940 for(Sector::Portables::iterator i = sector->portables.begin();
941 i != sector->portables.end(); ++i) {
942 Portable* portable = *i;
943 if(!portable->is_portable())
946 // make sure the Portable is a MovingObject
947 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
948 assert(moving_object);
949 if(moving_object == NULL)
952 // make sure the Portable isn't currently non-solid
953 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
955 // check if we are within reach
956 if(moving_object->get_bbox().contains(pos)) {
957 if (climbing) stop_climbing(*climbing);
958 grabbed_object = portable;
959 position_grabbed_object();
967 Player::handle_input_ghost()
971 if (controller->hold(Controller::LEFT)) {
973 vx -= MAX_RUN_XM * 2;
975 if (controller->hold(Controller::RIGHT)) {
977 vx += MAX_RUN_XM * 2;
979 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
980 vy -= MAX_RUN_XM * 2;
982 if (controller->hold(Controller::DOWN)) {
983 vy += MAX_RUN_XM * 2;
985 if (controller->hold(Controller::ACTION)) {
986 set_ghost_mode(false);
988 physic.set_velocity(vx, vy);
989 physic.set_acceleration(0, 0);
993 Player::add_coins(int count)
995 player_status->add_coins(count);
1001 return player_status->coins;
1005 Player::add_bonus(const std::string& bonustype)
1007 BonusType type = NO_BONUS;
1009 if(bonustype == "grow") {
1010 type = GROWUP_BONUS;
1011 } else if(bonustype == "fireflower") {
1013 } else if(bonustype == "iceflower") {
1015 } else if(bonustype == "airflower") {
1017 } else if(bonustype == "earthflower") {
1019 } else if(bonustype == "none") {
1022 std::ostringstream msg;
1023 msg << "Unknown bonus type " << bonustype;
1024 throw std::runtime_error(msg.str());
1027 return add_bonus(type);
1031 Player::add_bonus(BonusType type, bool animate)
1033 // always ignore NO_BONUS
1034 if (type == NO_BONUS) {
1038 // ignore GROWUP_BONUS if we're already big
1039 if (type == GROWUP_BONUS) {
1040 if (!player_status->bonus == NO_BONUS)
1044 return set_bonus(type, animate);
1048 Player::set_bonus(BonusType type, bool animate)
1050 if((player_status->bonus == NO_BONUS) && (type != NO_BONUS)) {
1051 if (!adjust_height(BIG_TUX_HEIGHT)) {
1052 log_debug << "Can't adjust Tux height" << std::endl;
1057 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
1059 if (climbing) stop_climbing(*climbing);
1062 if (type == NO_BONUS) {
1063 if (does_buttjump) does_buttjump = false;
1066 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
1067 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
1068 // visually lose helmet
1069 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
1070 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
1071 Vector paccel = Vector(0, 1000);
1072 std::string action = (dir==LEFT)?"left":"right";
1073 Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
1074 if (climbing) stop_climbing(*climbing);
1076 if ((player_status->bonus == ICE_BONUS) && (animate)) {
1077 // visually lose cap
1078 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
1079 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
1080 Vector paccel = Vector(0, 1000);
1081 std::string action = (dir==LEFT)?"left":"right";
1082 Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
1083 if (climbing) stop_climbing(*climbing);
1085 if ((player_status->bonus == AIR_BONUS) && (animate)) {
1086 // visually lose hat
1087 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
1088 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
1089 Vector paccel = Vector(0, 1000);
1090 std::string action = (dir==LEFT)?"left":"right";
1091 Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
1092 if (climbing) stop_climbing(*climbing);
1094 if ((player_status->bonus == EARTH_BONUS) && (animate)) {
1095 // visually lose hard-hat
1096 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
1097 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
1098 Vector paccel = Vector(0, 1000);
1099 std::string action = (dir==LEFT)?"left":"right";
1100 Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
1101 if (climbing) stop_climbing(*climbing);
1103 player_status->max_fire_bullets = 0;
1104 player_status->max_ice_bullets = 0;
1105 player_status->max_air_time = 0;
1106 player_status->max_earth_time = 0;
1108 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
1109 if (type == ICE_BONUS) player_status->max_ice_bullets++;
1110 if (type == AIR_BONUS) player_status->max_air_time++;
1111 if (type == EARTH_BONUS) player_status->max_earth_time++;
1113 player_status->bonus = type;
1118 Player::set_visible(bool visible_)
1120 this->visible = visible_;
1122 set_group(COLGROUP_MOVING);
1124 set_group(COLGROUP_DISABLED);
1128 Player::get_visible()
1136 kick_timer.start(KICK_TIME);
1140 Player::draw(DrawingContext& context)
1145 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
1146 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
1147 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
1148 float py = Sector::current()->camera->get_translation().y;
1149 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
1150 context.draw_surface(airarrow, Vector(px, py), LAYER_HUD - 1);
1153 std::string sa_prefix = "";
1154 std::string sa_postfix = "";
1156 if (player_status->bonus == GROWUP_BONUS)
1158 else if (player_status->bonus == FIRE_BONUS)
1160 else if (player_status->bonus == ICE_BONUS)
1162 else if (player_status->bonus == AIR_BONUS)
1164 else if (player_status->bonus == EARTH_BONUS)
1167 sa_prefix = "small";
1170 sa_postfix = "-left";
1172 sa_postfix = "-right";
1174 /* Set Tux sprite action */
1176 sprite->set_action("gameover");
1179 sprite->set_action_continued("grow"+sa_postfix);
1180 // while growing, do not change action
1181 // do_duck() will take care of cancelling growing manually
1182 // update() will take care of cancelling when growing completed
1184 else if (climbing) {
1185 sprite->set_action(sa_prefix+"-climbing"+sa_postfix);
1187 else if (backflipping) {
1188 sprite->set_action(sa_prefix+"-backflip"+sa_postfix);
1190 else if (duck && is_big()) {
1191 sprite->set_action(sa_prefix+"-duck"+sa_postfix);
1193 else if (skidding_timer.started() && !skidding_timer.check()) {
1194 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1196 else if (kick_timer.started() && !kick_timer.check()) {
1197 sprite->set_action(sa_prefix+"-kick"+sa_postfix);
1199 else if ((wants_buttjump || does_buttjump) && is_big()) {
1200 sprite->set_action(sa_prefix+"-buttjump"+sa_postfix);
1202 else if (!on_ground() || fall_mode != ON_GROUND) {
1203 if(physic.get_velocity_x() != 0 || fall_mode != ON_GROUND) {
1204 sprite->set_action(sa_prefix+"-jump"+sa_postfix);
1208 if (fabsf(physic.get_velocity_x()) < 1.0f) {
1209 // Determine which idle stage we're at
1210 if (sprite->get_action().find("-stand-") == std::string::npos && sprite->get_action().find("-idle-") == std::string::npos) {
1212 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1214 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1216 else if (idle_timer.check() || (IDLE_TIME[idle_stage] == 0 && sprite->animation_done())) {
1218 if (idle_stage >= IDLE_STAGE_COUNT)
1221 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1223 if (IDLE_TIME[idle_stage] == 0)
1224 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix, 1);
1226 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1229 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1233 sprite->set_action(sa_prefix+"-walk"+sa_postfix);
1238 // Tux is holding something
1239 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1240 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
1248 if (safe_timer.started() && size_t(game_time*40)%2)
1251 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1252 // draw light with earthflower bonus
1253 if (player_status->bonus == EARTH_BONUS){
1254 context.push_target();
1255 context.set_target(DrawingContext::LIGHTMAP);
1256 lightsprite->draw(context, get_pos() + Vector(dir==LEFT ? 0 : 32, 0), 0);
1257 context.pop_target();
1264 Player::collision_tile(uint32_t tile_attributes)
1266 if(tile_attributes & Tile::HURTS)
1271 if( tile_attributes & Tile::WATER ){
1277 if( tile_attributes & Tile::WATER ){
1280 SoundManager::current()->play( "sounds/splash.ogg" );
1285 if(tile_attributes & Tile::ICE) {
1286 ice_this_frame = true;
1292 Player::collision_solid(const CollisionHit& hit)
1295 if(physic.get_velocity_y() > 0)
1296 physic.set_velocity_y(0);
1298 on_ground_flag = true;
1299 floor_normal = hit.slope_normal;
1302 if (does_buttjump) {
1303 does_buttjump = false;
1304 physic.set_velocity_y(-300);
1305 on_ground_flag = false;
1306 Sector::current()->add_object(std::make_shared<Particles>(
1307 Vector(get_bbox().p2.x, get_bbox().p2.y),
1309 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1311 Sector::current()->add_object(std::make_shared<Particles>(
1312 Vector(get_bbox().p1.x, get_bbox().p2.y),
1314 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1316 Sector::current()->camera->shake(.1f, 0, 5);
1319 } else if(hit.top) {
1320 if(physic.get_velocity_y() < 0)
1321 physic.set_velocity_y(.2f);
1324 if(hit.left || hit.right) {
1325 physic.set_velocity_x(0);
1330 if(hit.left || hit.right) {
1332 } else if(hit.top || hit.bottom) {
1339 Player::collision(GameObject& other, const CollisionHit& hit)
1341 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1346 Player* player = dynamic_cast<Player*> (&other);
1351 if(hit.left || hit.right) {
1352 try_grab(); //grab objects right now, in update it will be too late
1354 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1355 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1356 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1357 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1359 if(controller->pressed(Controller::UP))
1360 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1366 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1367 if(badguy != NULL) {
1368 if(safe_timer.started() || invincible_timer.started())
1380 Player::make_invincible()
1382 SoundManager::current()->play("sounds/invincible_start.ogg");
1383 invincible_timer.start(TUX_INVINCIBLE_TIME);
1384 Sector::current()->play_music(HERRING_MUSIC);
1389 Player::kill(bool completely)
1391 if(dying || deactivated || is_winning() )
1394 if(!completely && (safe_timer.started() || invincible_timer.started() || stone))
1399 if (climbing) stop_climbing(*climbing);
1401 physic.set_velocity_x(0);
1403 sprite->set_angle(0.0f);
1405 if(!completely && is_big()) {
1406 SoundManager::current()->play("sounds/hurt.wav");
1408 if(player_status->bonus == FIRE_BONUS
1409 || player_status->bonus == ICE_BONUS
1410 || player_status->bonus == AIR_BONUS
1411 || player_status->bonus == EARTH_BONUS) {
1412 safe_timer.start(TUX_SAFE_TIME);
1413 set_bonus(GROWUP_BONUS, true);
1414 } else if(player_status->bonus == GROWUP_BONUS) {
1415 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1416 adjust_height(SMALL_TUX_HEIGHT);
1418 backflipping = false;
1419 sprite->set_angle(0.0f);
1420 set_bonus(NO_BONUS, true);
1421 } else if(player_status->bonus == NO_BONUS) {
1422 safe_timer.start(TUX_SAFE_TIME);
1423 adjust_height(SMALL_TUX_HEIGHT);
1427 SoundManager::current()->play("sounds/kill.wav");
1429 // do not die when in edit mode
1431 set_ghost_mode(true);
1435 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1437 for (int i = 0; i < 5; i++)
1439 // the numbers: starting x, starting y, velocity y
1440 Sector::current()->add_object(std::make_shared<FallingCoin>(get_pos() +
1441 Vector(graphicsRandom.rand(5), graphicsRandom.rand(-32,18)),
1442 graphicsRandom.rand(-100,100)));
1444 player_status->coins -= std::max(player_status->coins/10, 25);
1448 GameSession::current()->set_reset_point("", Vector());
1450 physic.enable_gravity(true);
1451 physic.set_gravity_modifier(1.0f); // Undo jump_early_apex
1453 invincible_timer.stop();
1454 physic.set_acceleration(0, 0);
1455 physic.set_velocity(0, -700);
1456 set_bonus(NO_BONUS, true);
1458 dying_timer.start(3.0);
1459 set_group(COLGROUP_DISABLED);
1461 // TODO: need nice way to handle players dying in co-op mode
1462 Sector::current()->effect->fade_out(3.0);
1463 SoundManager::current()->stop_music(3.0);
1468 Player::move(const Vector& vector)
1472 // Reset size to get correct hitbox if Tux was eg. ducked before moving
1474 set_size(TUX_WIDTH, BIG_TUX_HEIGHT);
1476 set_size(TUX_WIDTH, SMALL_TUX_HEIGHT);
1478 backflipping = false;
1479 sprite->set_angle(0.0f);
1480 last_ground_y = vector.y;
1481 if (climbing) stop_climbing(*climbing);
1487 Player::check_bounds()
1489 /* Keep tux in sector bounds: */
1490 if (get_pos().x < 0) {
1491 // Lock Tux to the size of the level, so that he doesn't fall off
1493 set_pos(Vector(0, get_pos().y));
1496 if (get_bbox().get_right() > Sector::current()->get_width()) {
1497 // Lock Tux to the size of the level, so that he doesn't fall off
1499 set_pos(Vector(Sector::current()->get_width() - get_bbox().get_width(), get_pos().y));
1502 /* fallen out of the level? */
1503 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1510 Player::add_velocity(const Vector& velocity)
1512 physic.set_velocity(physic.get_velocity() + velocity);
1516 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1518 if (end_speed.x > 0)
1519 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1520 if (end_speed.x < 0)
1521 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1522 if (end_speed.y > 0)
1523 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1524 if (end_speed.y < 0)
1525 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1529 Player::get_velocity()
1531 return physic.get_velocity();
1535 Player::bounce(BadGuy& )
1537 if(!(player_status->bonus == AIR_BONUS))
1538 physic.set_velocity_y(controller->hold(Controller::JUMP) ? -520 : -300);
1540 physic.set_velocity_y(controller->hold(Controller::JUMP) ? -580 : -340);
1541 glide_time = player_status->max_air_time * GLIDE_TIME_PER_FLOWER;
1545 //scripting Functions Below
1548 Player::deactivate()
1553 physic.set_velocity_x(0);
1554 physic.set_velocity_y(0);
1555 physic.set_acceleration_x(0);
1556 physic.set_acceleration_y(0);
1557 if (climbing) stop_climbing(*climbing);
1565 deactivated = false;
1568 void Player::walk(float speed)
1570 physic.set_velocity_x(speed);
1573 void Player::set_dir(bool right)
1575 dir = right ? RIGHT : LEFT;
1579 Player::set_ghost_mode(bool enable)
1581 if (ghost_mode == enable)
1584 if (climbing) stop_climbing(*climbing);
1588 set_group(COLGROUP_DISABLED);
1589 physic.enable_gravity(false);
1590 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1593 set_group(COLGROUP_MOVING);
1594 physic.enable_gravity(true);
1595 log_debug << "You feel solid again." << std::endl;
1600 Player::set_edit_mode(bool enable)
1606 Player::start_climbing(Climbable& climbable)
1608 if (climbing || !&climbable) return;
1610 climbing = &climbable;
1611 physic.enable_gravity(false);
1612 physic.set_velocity(0, 0);
1613 physic.set_acceleration(0, 0);
1615 backflipping = false;
1616 backflip_direction = 0;
1617 sprite->set_angle(0.0f);
1622 Player::stop_climbing(Climbable& /*climbable*/)
1624 if (!climbing) return;
1628 if (grabbed_object) {
1629 grabbed_object->ungrab(*this, dir);
1630 grabbed_object = NULL;
1633 physic.enable_gravity(true);
1634 physic.set_velocity(0, 0);
1635 physic.set_acceleration(0, 0);
1637 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1638 on_ground_flag = true;
1639 // TODO: This won't help. Why?
1645 Player::handle_input_climbing()
1648 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1654 if (controller->hold(Controller::LEFT)) {
1658 if (controller->hold(Controller::RIGHT)) {
1662 if (controller->hold(Controller::UP)) {
1665 if (controller->hold(Controller::DOWN)) {
1668 if (controller->hold(Controller::JUMP)) {
1670 stop_climbing(*climbing);
1676 if (controller->hold(Controller::ACTION)) {
1677 stop_climbing(*climbing);
1680 physic.set_velocity(vx, vy);
1681 physic.set_acceleration(0, 0);