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;
46 static const float STONE_TIME_PER_FLOWER = 2.0f;
48 /** number of idle stages, including standing */
49 static const unsigned int IDLE_STAGE_COUNT = 5;
51 * how long to play each idle animation in milliseconds
52 * '0' means the sprite action is played once before moving onto the next
55 static const int IDLE_TIME[] = { 5000, 0, 2500, 0, 2500 };
57 static const std::string IDLE_STAGES[] =
64 /** acceleration in horizontal direction when walking
65 * (all accelerations are in pixel/s^2) */
66 static const float WALK_ACCELERATION_X = 300;
67 /** acceleration in horizontal direction when running */
68 static const float RUN_ACCELERATION_X = 400;
69 /** acceleration when skidding */
70 static const float SKID_XM = 200;
71 /** time of skidding in seconds */
72 static const float SKID_TIME = .3f;
73 /** maximum walk velocity (pixel/s) */
74 static const float MAX_WALK_XM = 230;
75 /** maximum run velocity (pixel/s) */
76 static const float MAX_RUN_XM = 320;
77 /** bonus run velocity addition (pixel/s) */
78 static const float BONUS_RUN_XM = 80;
79 /** maximum horizontal climb velocity */
80 static const float MAX_CLIMB_XM = 96;
81 /** maximum vertical climb velocity */
82 static const float MAX_CLIMB_YM = 128;
83 /** maximum vertical glide velocity */
84 static const float MAX_GLIDE_YM = 128;
85 /** instant velocity when tux starts to walk */
86 static const float WALK_SPEED = 100;
88 /** multiplied by WALK_ACCELERATION to give friction */
89 static const float NORMAL_FRICTION_MULTIPLIER = 1.5f;
90 /** multiplied by WALK_ACCELERATION to give friction */
91 static const float ICE_FRICTION_MULTIPLIER = 0.1f;
92 static const float ICE_ACCELERATION_MULTIPLIER = 0.25f;
94 /** time of the kick (kicking mriceblock) animation */
95 static const float KICK_TIME = .3f;
97 /** if Tux cannot unduck for this long, he will get hurt */
98 static const float UNDUCK_HURT_TIME = 0.25f;
99 /** gravity is higher after the jump key is released before
100 the apex of the jump is reached */
101 static const float JUMP_EARLY_APEX_FACTOR = 3.0;
103 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) */
105 /* Tux's collision rectangle */
106 static const float TUX_WIDTH = 31.8f;
107 static const float RUNNING_TUX_WIDTH = 34;
108 static const float SMALL_TUX_HEIGHT = 30.8f;
109 static const float BIG_TUX_HEIGHT = 62.8f;
110 static const float DUCKED_TUX_HEIGHT = 31.8f;
112 bool no_water = true;
115 Player::Player(PlayerStatus* _player_status, const std::string& name_) :
118 scripting_controller(),
119 player_status(_player_status),
125 backflip_direction(),
132 scripting_controller_old(0),
136 lightsprite(SpriteManager::current()->create("images/creatures/tux/light.sprite")),
137 powersprite(SpriteManager::current()->create("images/creatures/tux/powerups.sprite")),
160 grabbed_object(NULL),
172 controller = InputManager::current()->get_controller();
173 scripting_controller.reset(new CodeController());
174 // if/when we have complete penny gfx, we can
175 // load those instead of Tux's sprite in the
177 sprite = SpriteManager::current()->create("images/creatures/tux/tux.sprite");
178 airarrow = Surface::create("images/engine/hud/airarrow.png");
179 idle_timer.start(IDLE_TIME[0]/1000.0f);
181 SoundManager::current()->preload("sounds/bigjump.wav");
182 SoundManager::current()->preload("sounds/jump.wav");
183 SoundManager::current()->preload("sounds/hurt.wav");
184 SoundManager::current()->preload("sounds/kill.wav");
185 SoundManager::current()->preload("sounds/skid.wav");
186 SoundManager::current()->preload("sounds/flip.wav");
187 SoundManager::current()->preload("sounds/invincible_start.ogg");
188 SoundManager::current()->preload("sounds/splash.ogg");
195 if (climbing) stop_climbing(*climbing);
202 set_size(TUX_WIDTH, BIG_TUX_HEIGHT);
204 set_size(TUX_WIDTH, SMALL_TUX_HEIGHT);
216 fall_mode = ON_GROUND;
218 jump_early_apex = false;
220 wants_buttjump = false;
221 does_buttjump = false;
224 backflipping = false;
225 backflip_direction = 0;
226 sprite->set_angle(0.0f);
227 powersprite->set_angle(0.0f);
228 lightsprite->set_angle(0.0f);
234 ice_this_frame = false;
235 speedlimit = 0; //no special limit
236 lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
238 on_ground_flag = false;
239 grabbed_object = NULL;
247 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
252 scripting::expose_object(vm, table_idx, dynamic_cast<scripting::Player *>(this), name, false);
256 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
261 scripting::unexpose_object(vm, table_idx, name);
265 Player::get_speedlimit()
271 Player::set_speedlimit(float newlimit)
277 Player::set_controller(Controller* controller_)
279 this->controller = controller_;
283 Player::set_winning()
285 if( ! is_winning() ){
287 invincible_timer.start(10000.0f);
292 Player::use_scripting_controller(bool use_or_release)
294 if ((use_or_release == true) && (controller != scripting_controller.get())) {
295 scripting_controller_old = get_controller();
296 set_controller(scripting_controller.get());
298 if ((use_or_release == false) && (controller == scripting_controller.get())) {
299 set_controller(scripting_controller_old);
300 scripting_controller_old = 0;
305 Player::do_scripting_controller(std::string control, bool pressed)
307 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
308 if(control == std::string(Controller::controlNames[i])) {
309 scripting_controller->press(Controller::Control(i), pressed);
315 Player::adjust_height(float new_height)
318 bbox2.move(Vector(0, bbox.get_height() - new_height));
319 bbox2.set_height(new_height);
322 if(new_height > bbox.get_height()) {
323 Rectf additional_space = bbox2;
324 additional_space.set_height(new_height - bbox.get_height());
325 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
329 // adjust bbox accordingly
330 // note that we use members of moving_object for this, so we can run this during CD, too
332 set_size(bbox2.get_width(), bbox2.get_height());
337 Player::trigger_sequence(std::string sequence_name)
339 if (climbing) stop_climbing(*climbing);
340 backflipping = false;
341 backflip_direction = 0;
342 sprite->set_angle(0.0f);
343 powersprite->set_angle(0.0f);
344 lightsprite->set_angle(0.0f);
345 GameSession::current()->start_sequence(sequence_name);
349 Player::update(float elapsed_time)
356 if(dying && dying_timer.check()) {
357 set_bonus(NO_BONUS, true);
362 if(!dying && !deactivated)
366 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
371 // extend/shrink tux collision rectangle so that we fall through/walk over 1
373 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
374 set_width(RUNNING_TUX_WIDTH);
376 set_width(TUX_WIDTH);
379 // on downward slopes, adjust vertical velocity so tux walks smoothly down
380 if (on_ground() && !dying) {
381 if(floor_normal.y != 0) {
382 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
383 physic.set_velocity_y(250);
388 // handle backflipping
389 if (backflipping && !dying) {
390 //prevent player from changing direction when backflipping
391 dir = (backflip_direction == 1) ? LEFT : RIGHT;
392 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
393 //rotate sprite during flip
394 sprite->set_angle(sprite->get_angle() + (dir==LEFT?1:-1) * elapsed_time * (360.0f / 0.5f));
395 if (player_status->bonus == EARTH_BONUS || player_status->bonus == AIR_BONUS) {
396 powersprite->set_angle(sprite->get_angle());
397 if (player_status->bonus == EARTH_BONUS)
398 lightsprite->set_angle(sprite->get_angle());
404 fall_mode = ON_GROUND;
405 last_ground_y = get_pos().y;
407 if(get_pos().y > last_ground_y)
409 else if(fall_mode == ON_GROUND)
413 // check if we landed
416 if (backflipping && (backflip_timer.get_timegone() > 0.15f)) {
417 backflipping = false;
418 backflip_direction = 0;
420 sprite->set_angle(0.0f);
421 powersprite->set_angle(0.0f);
422 lightsprite->set_angle(0.0f);
425 // if controls are currently deactivated, we take care of standing up ourselves
429 if (player_status->bonus == AIR_BONUS)
430 ability_time = player_status->max_air_time * GLIDE_TIME_PER_FLOWER;
433 // calculate movement for this frame
434 movement = physic.get_movement(elapsed_time);
436 if(grabbed_object != NULL && !dying) {
437 position_grabbed_object();
440 if(grabbed_object != NULL && dying){
441 grabbed_object->ungrab(*this, dir);
442 grabbed_object = NULL;
445 if(!ice_this_frame && on_ground())
448 on_ground_flag = false;
449 ice_this_frame = false;
451 // when invincible, spawn particles
452 if (invincible_timer.started())
454 if (graphicsRandom.rand(0, 2) == 0) {
455 float px = graphicsRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
456 float py = graphicsRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
457 Vector ppos = Vector(px, py);
458 Vector pspeed = Vector(0, 0);
459 Vector paccel = Vector(0, 0);
460 Sector::current()->add_object(std::make_shared<SpriteParticle>(
461 "images/objects/particles/sparkle.sprite",
462 // draw bright sparkle when there is lots of time left,
463 // dark sparkle when invincibility is about to end
464 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) ?
465 // make every other a longer sparkle to make trail a bit fuzzy
466 (size_t(game_time*20)%2) ? "small" : "medium"
468 "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
473 if (sprite->animation_done()) growing = false;
476 // when climbing animate only while moving
478 if((physic.get_velocity_x()==0)&&(physic.get_velocity_y()==0))
479 sprite->stop_animation();
481 sprite->set_animation_loops(-1);
489 return on_ground_flag;
495 if(player_status->bonus == NO_BONUS)
502 Player::apply_friction()
504 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
505 physic.set_velocity_x(0);
506 physic.set_acceleration_x(0);
508 float friction = WALK_ACCELERATION_X * (on_ice ? ICE_FRICTION_MULTIPLIER : NORMAL_FRICTION_MULTIPLIER);
509 if(physic.get_velocity_x() < 0) {
510 physic.set_acceleration_x(friction);
511 } else if(physic.get_velocity_x() > 0) {
512 physic.set_acceleration_x(-friction);
513 } // no friction for physic.get_velocity_x() == 0
518 Player::handle_horizontal_input()
520 float vx = physic.get_velocity_x();
521 float vy = physic.get_velocity_y();
522 float ax = physic.get_acceleration_x();
523 float ay = physic.get_acceleration_y();
526 if(!duck || physic.get_velocity_y() != 0) {
527 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
531 } else if(!controller->hold(Controller::LEFT)
532 && controller->hold(Controller::RIGHT)) {
539 // do not run if we're holding something which slows us down
540 if ( grabbed_object && grabbed_object->is_hampering() ) {
541 ax = dirsign * WALK_ACCELERATION_X;
543 if(vx >= MAX_WALK_XM && dirsign > 0) {
546 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
551 if( vx * dirsign < MAX_WALK_XM ) {
552 ax = dirsign * WALK_ACCELERATION_X;
554 ax = dirsign * RUN_ACCELERATION_X;
557 if(vx >= MAX_RUN_XM + BONUS_RUN_XM *((player_status->bonus == AIR_BONUS) ? 1 : 0) && dirsign > 0) {
558 vx = MAX_RUN_XM + BONUS_RUN_XM *((player_status->bonus == AIR_BONUS) ? 1 : 0);
560 } else if(vx <= -MAX_RUN_XM - BONUS_RUN_XM *((player_status->bonus == AIR_BONUS) ? 1 : 0) && dirsign < 0) {
561 vx = -MAX_RUN_XM - BONUS_RUN_XM *((player_status->bonus == AIR_BONUS) ? 1 : 0);
566 // we can reach WALK_SPEED without any acceleration
567 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
568 vx = dirsign * WALK_SPEED;
572 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
573 vx = dirsign * speedlimit;
577 // changing directions?
578 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
580 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
581 skidding_timer.start(SKID_TIME);
582 SoundManager::current()->play("sounds/skid.wav");
583 // dust some particles
584 Sector::current()->add_object(
585 std::make_shared<Particles>(
586 Vector(dir == LEFT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
587 dir == LEFT ? 50 : -70, dir == LEFT ? 70 : -50, 260, 280,
588 Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f, LAYER_OBJECTS+1));
597 ax *= ICE_ACCELERATION_MULTIPLIER;
600 physic.set_velocity(vx, vy);
601 physic.set_acceleration(ax, ay);
603 // we get slower when not pressing any keys
625 if (physic.get_velocity_y() != 0)
632 if (adjust_height(DUCKED_TUX_HEIGHT)) {
635 unduck_hurt_timer.stop();
642 Player::do_standup() {
652 if (adjust_height(BIG_TUX_HEIGHT)) {
654 unduck_hurt_timer.stop();
656 // if timer is not already running, start it.
657 if (unduck_hurt_timer.get_period() == 0) {
658 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
660 else if (unduck_hurt_timer.check()) {
668 Player::do_backflip() {
674 backflip_direction = (dir == LEFT)?(+1):(-1);
676 do_jump((player_status->bonus == AIR_BONUS) ? -720 : -580);
677 SoundManager::current()->play("sounds/flip.wav");
678 backflip_timer.start(TUX_BACKFLIP_TIME);
682 Player::do_jump(float yspeed) {
686 physic.set_velocity_y(yspeed);
687 //bbox.move(Vector(0, -1));
689 on_ground_flag = false;
694 SoundManager::current()->play("sounds/bigjump.wav");
696 SoundManager::current()->play("sounds/jump.wav");
701 Player::early_jump_apex()
703 if (!jump_early_apex)
705 jump_early_apex = true;
706 physic.set_gravity_modifier(JUMP_EARLY_APEX_FACTOR);
711 Player::do_jump_apex()
715 jump_early_apex = false;
716 physic.set_gravity_modifier(1.0f);
721 Player::handle_vertical_input()
724 if(controller->pressed(Controller::JUMP)) jump_button_timer.start(JUMP_GRACE_TIME);
725 if(controller->hold(Controller::JUMP) && jump_button_timer.started() && can_jump) {
726 jump_button_timer.stop();
728 // when running, only jump a little bit; else do a backflip
729 if ((physic.get_velocity_x() != 0) ||
730 (controller->hold(Controller::LEFT)) ||
731 (controller->hold(Controller::RIGHT)))
740 // airflower allows for higher jumps-
741 // jump a bit higher if we are running; else do a normal jump
742 if(player_status->bonus == AIR_BONUS)
743 do_jump((fabs(physic.get_velocity_x()) > MAX_WALK_XM) ? -620 : -580);
745 do_jump((fabs(physic.get_velocity_x()) > MAX_WALK_XM) ? -580 : -520);
747 // airflower glide only when holding jump key
748 } else if (controller->hold(Controller::JUMP) && player_status->bonus == AIR_BONUS && physic.get_velocity_y() > MAX_GLIDE_YM) {
749 if (ability_time > 0 && !ability_timer.started())
750 ability_timer.start(ability_time);
751 else if (ability_timer.started()) {
752 // glide stops after some duration or if buttjump is initiated
753 if ((ability_timer.get_timeleft() <= 0.05f) || controller->hold(Controller::DOWN)) {
755 ability_timer.stop();
757 physic.set_velocity_y(MAX_GLIDE_YM);
758 physic.set_acceleration_y(0);
764 // Let go of jump key
765 else if(!controller->hold(Controller::JUMP)) {
766 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
770 if (player_status->bonus == AIR_BONUS && ability_timer.started()){
771 ability_time = ability_timer.get_timeleft();
772 ability_timer.stop();
776 if(jump_early_apex && physic.get_velocity_y() >= 0) {
780 /* In case the player has pressed Down while in a certain range of air,
781 enable butt jump action */
782 if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
783 wants_buttjump = true;
784 if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
787 /* When Down is not held anymore, disable butt jump */
788 if(!controller->hold(Controller::DOWN)) {
789 wants_buttjump = false;
790 does_buttjump = false;
794 physic.set_acceleration_y(0);
797 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
798 physic.set_acceleration_y(-2000);
799 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
805 Player::handle_input()
808 handle_input_ghost();
812 handle_input_climbing();
817 if( controller->released( Controller::PEEK_LEFT ) || controller->released( Controller::PEEK_RIGHT ) ) {
820 if( controller->released( Controller::PEEK_UP ) || controller->released( Controller::PEEK_DOWN ) ) {
823 if( controller->pressed( Controller::PEEK_LEFT ) ) {
826 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
829 if(!backflipping && !jumping && on_ground()) {
830 if( controller->pressed( Controller::PEEK_UP ) ) {
832 } else if( controller->pressed( Controller::PEEK_DOWN ) ) {
837 /* Handle horizontal movement: */
838 if (!backflipping && !stone) handle_horizontal_input();
844 /* Handle vertical movement: */
845 if (!stone) handle_vertical_input();
848 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
849 if((player_status->bonus == FIRE_BONUS &&
850 Sector::current()->get_active_bullets() < player_status->max_fire_bullets) ||
851 (player_status->bonus == ICE_BONUS &&
852 Sector::current()->get_active_bullets() < player_status->max_ice_bullets))
854 Vector pos = get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2) : Vector(32, bbox.get_height()/2));
855 auto new_bullet = std::make_shared<Bullet>(pos, physic.get_velocity_x(), dir, player_status->bonus);
856 Sector::current()->add_object(new_bullet);
858 SoundManager::current()->play("sounds/shoot.wav");
859 shooting_timer.start(SHOOTING_TIME);
864 if (controller->pressed(Controller::DOWN) && player_status->bonus == EARTH_BONUS && !cooldown_timer.started()) {
865 if (controller->hold(Controller::ACTION) && !ability_timer.started()) {
866 ability_timer.start(player_status->max_earth_time * STONE_TIME_PER_FLOWER);
867 powersprite->stop_animation();
869 physic.set_gravity_modifier(1.0f); // Undo jump_early_apex
876 /* Revert from Stone */
877 if (stone && (!controller->hold(Controller::ACTION) || ability_timer.get_timeleft() <= 0.5f)) {
878 cooldown_timer.start(ability_timer.get_timegone()/2.0f); //The longer stone form is used, the longer until it can be used again
879 ability_timer.stop();
880 sprite->set_angle(0.0f);
881 powersprite->set_angle(0.0f);
882 lightsprite->set_angle(0.0f);
884 for (int i = 0; i < 8; i++)
886 Vector ppos = Vector(get_bbox().get_left() + 8 + 16*((int)i/4), get_bbox().get_top() + 16*(i%4));
887 float grey = graphicsRandom.randf(.4f, .8f);
888 Color pcolor = Color(grey, grey, grey);
889 Sector::current()->add_object(std::make_shared<Particles>(ppos, -60, 240, 42, 81, Vector(0, 500),
890 8, pcolor, 4 + graphicsRandom.randf(-0.4, 0.4),
891 0.8 + graphicsRandom.randf(0, 0.4), LAYER_OBJECTS+2));
895 /* Duck or Standup! */
896 if (controller->hold(Controller::DOWN) && !stone) {
905 if(!controller->hold(Controller::ACTION) && grabbed_object) {
906 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
908 // move the grabbed object a bit away from tux
909 Rectf grabbed_bbox = moving_object->get_bbox();
911 dest_.p2.y = bbox.get_top() + bbox.get_height()*0.66666;
912 dest_.p1.y = dest_.p2.y - grabbed_bbox.get_height();
914 dest_.p2.x = bbox.get_left() - 1;
915 dest_.p1.x = dest_.p2.x - grabbed_bbox.get_width();
917 dest_.p1.x = bbox.get_right() + 1;
918 dest_.p2.x = dest_.p1.x + grabbed_bbox.get_width();
920 if(Sector::current()->is_free_of_tiles(dest_, true)) {
921 moving_object->set_pos(dest_.p1);
922 if(controller->hold(Controller::UP)) {
923 grabbed_object->ungrab(*this, UP);
925 grabbed_object->ungrab(*this, dir);
927 grabbed_object = NULL;
930 log_debug << "Non MovingObject grabbed?!?" << std::endl;
934 /* stop backflipping at will */
935 if( backflipping && ( !controller->hold(Controller::JUMP) && !backflip_timer.started()) ){
936 backflipping = false;
937 backflip_direction = 0;
938 sprite->set_angle(0.0f);
939 powersprite->set_angle(0.0f);
940 lightsprite->set_angle(0.0f);
945 Player::position_grabbed_object()
947 MovingObject* moving_object = dynamic_cast<MovingObject*>(grabbed_object);
948 assert(moving_object);
950 // Position where we will hold the lower-inner corner
951 Vector pos(get_bbox().get_left() + get_bbox().get_width()/2,
952 get_bbox().get_top() + get_bbox().get_height()*0.66666);
954 // Adjust to find the grabbed object's upper-left corner
956 pos.x -= moving_object->get_bbox().get_width();
957 pos.y -= moving_object->get_bbox().get_height();
959 grabbed_object->grab(*this, pos, dir);
965 if(controller->hold(Controller::ACTION) && !grabbed_object
967 Sector* sector = Sector::current();
970 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
972 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
975 for(Sector::Portables::iterator i = sector->portables.begin();
976 i != sector->portables.end(); ++i) {
977 Portable* portable = *i;
978 if(!portable->is_portable())
981 // make sure the Portable is a MovingObject
982 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
983 assert(moving_object);
984 if(moving_object == NULL)
987 // make sure the Portable isn't currently non-solid
988 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
990 // check if we are within reach
991 if(moving_object->get_bbox().contains(pos)) {
992 if (climbing) stop_climbing(*climbing);
993 grabbed_object = portable;
994 position_grabbed_object();
1002 Player::handle_input_ghost()
1006 if (controller->hold(Controller::LEFT)) {
1008 vx -= MAX_RUN_XM * 2;
1010 if (controller->hold(Controller::RIGHT)) {
1012 vx += MAX_RUN_XM * 2;
1014 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
1015 vy -= MAX_RUN_XM * 2;
1017 if (controller->hold(Controller::DOWN)) {
1018 vy += MAX_RUN_XM * 2;
1020 if (controller->hold(Controller::ACTION)) {
1021 set_ghost_mode(false);
1023 physic.set_velocity(vx, vy);
1024 physic.set_acceleration(0, 0);
1028 Player::add_coins(int count)
1030 player_status->add_coins(count);
1036 return player_status->coins;
1040 Player::add_bonus(const std::string& bonustype)
1042 BonusType type = NO_BONUS;
1044 if(bonustype == "grow") {
1045 type = GROWUP_BONUS;
1046 } else if(bonustype == "fireflower") {
1048 } else if(bonustype == "iceflower") {
1050 } else if(bonustype == "airflower") {
1052 } else if(bonustype == "earthflower") {
1054 } else if(bonustype == "none") {
1057 std::ostringstream msg;
1058 msg << "Unknown bonus type " << bonustype;
1059 throw std::runtime_error(msg.str());
1062 return add_bonus(type);
1066 Player::add_bonus(BonusType type, bool animate)
1068 // always ignore NO_BONUS
1069 if (type == NO_BONUS) {
1073 // ignore GROWUP_BONUS if we're already big
1074 if (type == GROWUP_BONUS) {
1075 if (player_status->bonus != NO_BONUS)
1079 return set_bonus(type, animate);
1083 Player::set_bonus(BonusType type, bool animate)
1085 if((player_status->bonus == NO_BONUS) && (type != NO_BONUS)) {
1086 if (!adjust_height(BIG_TUX_HEIGHT)) {
1087 log_debug << "Can't adjust Tux height" << std::endl;
1092 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
1094 if (climbing) stop_climbing(*climbing);
1097 if (type == NO_BONUS) {
1098 if (does_buttjump) does_buttjump = false;
1101 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
1102 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
1103 // visually lose helmet
1104 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
1105 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
1106 Vector paccel = Vector(0, 1000);
1107 std::string action = (dir==LEFT)?"left":"right";
1108 Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
1109 if (climbing) stop_climbing(*climbing);
1111 if ((player_status->bonus == ICE_BONUS) && (animate)) {
1112 // visually lose cap
1113 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
1114 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
1115 Vector paccel = Vector(0, 1000);
1116 std::string action = (dir==LEFT)?"left":"right";
1117 Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
1118 if (climbing) stop_climbing(*climbing);
1120 if ((player_status->bonus == AIR_BONUS) && (animate)) {
1121 // visually lose hat
1122 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
1123 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
1124 Vector paccel = Vector(0, 1000);
1125 std::string action = (dir==LEFT)?"left":"right";
1126 Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/airtux-hat.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
1127 if (climbing) stop_climbing(*climbing);
1129 if ((player_status->bonus == EARTH_BONUS) && (animate)) {
1130 // visually lose hard-hat
1131 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
1132 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
1133 Vector paccel = Vector(0, 1000);
1134 std::string action = (dir==LEFT)?"left":"right";
1135 Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/earthtux-hardhat.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
1136 if (climbing) stop_climbing(*climbing);
1138 player_status->max_fire_bullets = 0;
1139 player_status->max_ice_bullets = 0;
1140 player_status->max_air_time = 0;
1141 player_status->max_earth_time = 0;
1143 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
1144 if (type == ICE_BONUS) player_status->max_ice_bullets++;
1145 if (type == AIR_BONUS) player_status->max_air_time++;
1146 if (type == EARTH_BONUS) player_status->max_earth_time++;
1148 player_status->bonus = type;
1153 Player::set_visible(bool visible_)
1155 this->visible = visible_;
1157 set_group(COLGROUP_MOVING);
1159 set_group(COLGROUP_DISABLED);
1163 Player::get_visible()
1171 kick_timer.start(KICK_TIME);
1175 Player::draw(DrawingContext& context)
1180 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
1181 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
1182 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
1183 float py = Sector::current()->camera->get_translation().y;
1184 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
1185 context.draw_surface(airarrow, Vector(px, py), LAYER_HUD - 1);
1188 std::string sa_prefix = "";
1189 std::string sa_postfix = "";
1191 if (player_status->bonus == GROWUP_BONUS)
1193 else if (player_status->bonus == FIRE_BONUS)
1195 else if (player_status->bonus == ICE_BONUS)
1197 else if (player_status->bonus == AIR_BONUS)
1199 else if (player_status->bonus == EARTH_BONUS)
1200 sa_prefix = "earth";
1202 sa_prefix = "small";
1205 sa_postfix = "-left";
1207 sa_postfix = "-right";
1209 /* Set Tux sprite action */
1211 sprite->set_action("gameover");
1214 sprite->set_action_continued("grow"+sa_postfix);
1215 // while growing, do not change action
1216 // do_duck() will take care of cancelling growing manually
1217 // update() will take care of cancelling when growing completed
1220 sprite->set_action(sprite->get_action()+"-stone");
1222 else if (climbing) {
1223 sprite->set_action(sa_prefix+"-climbing"+sa_postfix);
1225 else if (backflipping) {
1226 sprite->set_action(sa_prefix+"-backflip"+sa_postfix);
1228 else if (duck && is_big()) {
1229 sprite->set_action(sa_prefix+"-duck"+sa_postfix);
1231 else if (skidding_timer.started() && !skidding_timer.check()) {
1232 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1234 else if (kick_timer.started() && !kick_timer.check()) {
1235 sprite->set_action(sa_prefix+"-kick"+sa_postfix);
1237 else if ((wants_buttjump || does_buttjump) && is_big()) {
1238 sprite->set_action(sa_prefix+"-buttjump"+sa_postfix);
1240 else if (!on_ground() || fall_mode != ON_GROUND) {
1241 if(physic.get_velocity_x() != 0 || fall_mode != ON_GROUND) {
1242 sprite->set_action(sa_prefix+"-jump"+sa_postfix);
1246 if (fabsf(physic.get_velocity_x()) < 1.0f) {
1247 // Determine which idle stage we're at
1248 if (sprite->get_action().find("-stand-") == std::string::npos && sprite->get_action().find("-idle-") == std::string::npos) {
1250 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1252 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1254 else if (idle_timer.check() || (IDLE_TIME[idle_stage] == 0 && sprite->animation_done())) {
1256 if (idle_stage >= IDLE_STAGE_COUNT)
1259 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1261 if (IDLE_TIME[idle_stage] == 0)
1262 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix, 1);
1264 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1267 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1271 sprite->set_action(sa_prefix+"-walk"+sa_postfix);
1275 /* Set Tux powerup sprite action */
1276 if (player_status->bonus == EARTH_BONUS) {
1277 powersprite->set_action(sprite->get_action());
1278 lightsprite->set_action(sprite->get_action());
1279 } else if (player_status->bonus == AIR_BONUS)
1280 powersprite->set_action(sprite->get_action());
1283 // Tux is holding something
1284 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1285 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
1293 if (safe_timer.started() && size_t(game_time*40)%2)
1295 else if (player_status->bonus == EARTH_BONUS){ // draw special effects with earthflower bonus
1296 // shake at end of maximum stone duration
1297 Vector shake_delta = (stone && ability_timer.get_timeleft() < 1.0f) ? Vector(graphicsRandom.rand(-3,3), 0) : Vector(0,0);
1298 sprite->draw(context, get_pos() + shake_delta, LAYER_OBJECTS + 1);
1300 powersprite->draw(context, get_pos() + shake_delta, LAYER_OBJECTS + 1);
1302 context.push_target();
1303 context.set_target(DrawingContext::LIGHTMAP);
1304 lightsprite->draw(context, get_pos(), 0);
1305 context.pop_target();
1306 // give an indicator that stone form cannot be used for a while
1307 if (cooldown_timer.started() && graphicsRandom.rand(0, 4) == 0) {
1308 float px = graphicsRandom.randf(bbox.p1.x, bbox.p2.x);
1309 float py = bbox.p2.y+8;
1310 Vector ppos = Vector(px, py);
1311 Sector::current()->add_object(std::make_shared<SpriteParticle>(
1312 "images/objects/particles/sparkle.sprite", "dark",
1313 ppos, ANCHOR_MIDDLE, Vector(0, 0), Vector(0, 0), LAYER_OBJECTS+1+5));
1317 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1318 if (player_status->bonus == AIR_BONUS)
1319 powersprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1325 Player::collision_tile(uint32_t tile_attributes)
1327 if(tile_attributes & Tile::HURTS)
1332 if( tile_attributes & Tile::WATER ){
1338 if( tile_attributes & Tile::WATER ){
1341 SoundManager::current()->play( "sounds/splash.ogg" );
1346 if(tile_attributes & Tile::ICE) {
1347 ice_this_frame = true;
1353 Player::collision_solid(const CollisionHit& hit)
1356 if(physic.get_velocity_y() > 0)
1357 physic.set_velocity_y(0);
1359 on_ground_flag = true;
1360 floor_normal = hit.slope_normal;
1363 if (does_buttjump) {
1364 does_buttjump = false;
1365 physic.set_velocity_y(-300);
1366 on_ground_flag = false;
1367 Sector::current()->add_object(std::make_shared<Particles>(
1368 Vector(get_bbox().p2.x, get_bbox().p2.y),
1369 50, 70, 260, 280, Vector(0, 300), 3,
1370 Color(.4f, .4f, .4f), 3, .8f, LAYER_OBJECTS+1));
1371 Sector::current()->add_object(std::make_shared<Particles>(
1372 Vector(get_bbox().p1.x, get_bbox().p2.y),
1373 -70, -50, 260, 280, Vector(0, 300), 3,
1374 Color(.4f, .4f, .4f), 3, .8f, LAYER_OBJECTS+1));
1375 Sector::current()->camera->shake(.1f, 0, 5);
1378 } else if(hit.top) {
1379 if(physic.get_velocity_y() < 0)
1380 physic.set_velocity_y(.2f);
1383 if(hit.left || hit.right) {
1384 physic.set_velocity_x(0);
1389 if(hit.left || hit.right) {
1391 } else if(hit.top || hit.bottom) {
1398 Player::collision(GameObject& other, const CollisionHit& hit)
1400 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1405 Player* player = dynamic_cast<Player*> (&other);
1410 if(hit.left || hit.right) {
1411 try_grab(); //grab objects right now, in update it will be too late
1413 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1414 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1415 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1416 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1418 if(controller->pressed(Controller::UP))
1419 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1425 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1426 if(badguy != NULL) {
1427 if(safe_timer.started() || invincible_timer.started())
1439 Player::make_invincible()
1441 SoundManager::current()->play("sounds/invincible_start.ogg");
1442 invincible_timer.start(TUX_INVINCIBLE_TIME);
1443 Sector::current()->play_music(HERRING_MUSIC);
1448 Player::kill(bool completely)
1450 if(dying || deactivated || is_winning() )
1453 if(!completely && (safe_timer.started() || invincible_timer.started() || stone))
1458 if (climbing) stop_climbing(*climbing);
1460 physic.set_velocity_x(0);
1462 sprite->set_angle(0.0f);
1463 powersprite->set_angle(0.0f);
1464 lightsprite->set_angle(0.0f);
1466 if(!completely && is_big()) {
1467 SoundManager::current()->play("sounds/hurt.wav");
1469 if(player_status->bonus == FIRE_BONUS
1470 || player_status->bonus == ICE_BONUS
1471 || player_status->bonus == AIR_BONUS
1472 || player_status->bonus == EARTH_BONUS) {
1473 safe_timer.start(TUX_SAFE_TIME);
1474 set_bonus(GROWUP_BONUS, true);
1475 } else if(player_status->bonus == GROWUP_BONUS) {
1476 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1477 adjust_height(SMALL_TUX_HEIGHT);
1479 backflipping = false;
1480 sprite->set_angle(0.0f);
1481 powersprite->set_angle(0.0f);
1482 lightsprite->set_angle(0.0f);
1483 set_bonus(NO_BONUS, true);
1484 } else if(player_status->bonus == NO_BONUS) {
1485 safe_timer.start(TUX_SAFE_TIME);
1486 adjust_height(SMALL_TUX_HEIGHT);
1490 SoundManager::current()->play("sounds/kill.wav");
1492 // do not die when in edit mode
1494 set_ghost_mode(true);
1498 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1500 for (int i = 0; i < 5; i++)
1502 // the numbers: starting x, starting y, velocity y
1503 Sector::current()->add_object(std::make_shared<FallingCoin>(get_pos() +
1504 Vector(graphicsRandom.rand(5), graphicsRandom.rand(-32,18)),
1505 graphicsRandom.rand(-100,100)));
1507 player_status->coins -= std::max(player_status->coins/10, 25);
1511 GameSession::current()->set_reset_point("", Vector());
1513 physic.enable_gravity(true);
1514 physic.set_gravity_modifier(1.0f); // Undo jump_early_apex
1516 invincible_timer.stop();
1517 physic.set_acceleration(0, 0);
1518 physic.set_velocity(0, -700);
1519 set_bonus(NO_BONUS, true);
1521 dying_timer.start(3.0);
1522 set_group(COLGROUP_DISABLED);
1524 // TODO: need nice way to handle players dying in co-op mode
1525 Sector::current()->effect->fade_out(3.0);
1526 SoundManager::current()->stop_music(3.0);
1531 Player::move(const Vector& vector)
1535 // Reset size to get correct hitbox if Tux was eg. ducked before moving
1537 set_size(TUX_WIDTH, BIG_TUX_HEIGHT);
1539 set_size(TUX_WIDTH, SMALL_TUX_HEIGHT);
1541 backflipping = false;
1542 sprite->set_angle(0.0f);
1543 powersprite->set_angle(0.0f);
1544 lightsprite->set_angle(0.0f);
1545 last_ground_y = vector.y;
1546 if (climbing) stop_climbing(*climbing);
1552 Player::check_bounds()
1554 /* Keep tux in sector bounds: */
1555 if (get_pos().x < 0) {
1556 // Lock Tux to the size of the level, so that he doesn't fall off
1558 set_pos(Vector(0, get_pos().y));
1561 if (get_bbox().get_right() > Sector::current()->get_width()) {
1562 // Lock Tux to the size of the level, so that he doesn't fall off
1564 set_pos(Vector(Sector::current()->get_width() - get_bbox().get_width(), get_pos().y));
1567 /* fallen out of the level? */
1568 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1575 Player::add_velocity(const Vector& velocity)
1577 physic.set_velocity(physic.get_velocity() + velocity);
1581 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1583 if (end_speed.x > 0)
1584 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1585 if (end_speed.x < 0)
1586 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1587 if (end_speed.y > 0)
1588 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1589 if (end_speed.y < 0)
1590 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1594 Player::get_velocity()
1596 return physic.get_velocity();
1600 Player::bounce(BadGuy& )
1602 if(!(player_status->bonus == AIR_BONUS))
1603 physic.set_velocity_y(controller->hold(Controller::JUMP) ? -520 : -300);
1605 physic.set_velocity_y(controller->hold(Controller::JUMP) ? -580 : -340);
1606 ability_time = player_status->max_air_time * GLIDE_TIME_PER_FLOWER;
1610 //scripting Functions Below
1613 Player::deactivate()
1618 physic.set_velocity_x(0);
1619 physic.set_velocity_y(0);
1620 physic.set_acceleration_x(0);
1621 physic.set_acceleration_y(0);
1622 if (climbing) stop_climbing(*climbing);
1630 deactivated = false;
1633 void Player::walk(float speed)
1635 physic.set_velocity_x(speed);
1638 void Player::set_dir(bool right)
1640 dir = right ? RIGHT : LEFT;
1644 Player::set_ghost_mode(bool enable)
1646 if (ghost_mode == enable)
1649 if (climbing) stop_climbing(*climbing);
1653 set_group(COLGROUP_DISABLED);
1654 physic.enable_gravity(false);
1655 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1658 set_group(COLGROUP_MOVING);
1659 physic.enable_gravity(true);
1660 log_debug << "You feel solid again." << std::endl;
1665 Player::set_edit_mode(bool enable)
1671 Player::start_climbing(Climbable& climbable)
1673 if (climbing || !&climbable) return;
1675 climbing = &climbable;
1676 physic.enable_gravity(false);
1677 physic.set_velocity(0, 0);
1678 physic.set_acceleration(0, 0);
1680 backflipping = false;
1681 backflip_direction = 0;
1682 sprite->set_angle(0.0f);
1683 powersprite->set_angle(0.0f);
1684 lightsprite->set_angle(0.0f);
1689 Player::stop_climbing(Climbable& /*climbable*/)
1691 if (!climbing) return;
1695 if (grabbed_object) {
1696 grabbed_object->ungrab(*this, dir);
1697 grabbed_object = NULL;
1700 physic.enable_gravity(true);
1701 physic.set_velocity(0, 0);
1702 physic.set_acceleration(0, 0);
1704 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1705 on_ground_flag = true;
1706 // TODO: This won't help. Why?
1712 Player::handle_input_climbing()
1715 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1721 if (controller->hold(Controller::LEFT)) {
1725 if (controller->hold(Controller::RIGHT)) {
1729 if (controller->hold(Controller::UP)) {
1732 if (controller->hold(Controller::DOWN)) {
1735 if (controller->hold(Controller::JUMP)) {
1737 stop_climbing(*climbing);
1743 if (controller->hold(Controller::ACTION)) {
1744 stop_climbing(*climbing);
1747 physic.set_velocity(vx, vy);
1748 physic.set_acceleration(0, 0);