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 bool no_water = true;
105 Player::Player(PlayerStatus* _player_status, const std::string& name) :
108 scripting_controller(0),
109 player_status(_player_status),
114 backflip_direction(),
119 scripting_controller_old(0),
143 grabbed_object(NULL),
155 controller = g_main_controller;
156 scripting_controller.reset(new CodeController());
157 sprite = sprite_manager->create("images/creatures/tux/tux.sprite");
158 airarrow = Surface::create("images/engine/hud/airarrow.png");
159 idle_timer.start(IDLE_TIME[0]/1000.0f);
161 sound_manager->preload("sounds/bigjump.wav");
162 sound_manager->preload("sounds/jump.wav");
163 sound_manager->preload("sounds/hurt.wav");
164 sound_manager->preload("sounds/kill.wav");
165 sound_manager->preload("sounds/skid.wav");
166 sound_manager->preload("sounds/flip.wav");
167 sound_manager->preload("sounds/invincible_start.ogg");
168 sound_manager->preload("sounds/splash.ogg");
175 if (climbing) stop_climbing(*climbing);
182 set_size(31.8f, 62.8f);
184 set_size(31.8f, 30.8f);
195 fall_mode = ON_GROUND;
197 jump_early_apex = false;
199 wants_buttjump = false;
200 does_buttjump = false;
203 backflipping = false;
204 backflip_direction = 0;
208 ice_this_frame = false;
209 speedlimit = 0; //no special limit
211 on_ground_flag = false;
212 grabbed_object = NULL;
220 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
225 scripting::expose_object(vm, table_idx, dynamic_cast<scripting::Player *>(this), name, false);
229 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
234 scripting::unexpose_object(vm, table_idx, name);
238 Player::get_speedlimit()
244 Player::set_speedlimit(float newlimit)
250 Player::set_controller(Controller* controller)
252 this->controller = controller;
256 Player::use_scripting_controller(bool use_or_release)
258 if ((use_or_release == true) && (controller != scripting_controller.get())) {
259 scripting_controller_old = get_controller();
260 set_controller(scripting_controller.get());
262 if ((use_or_release == false) && (controller == scripting_controller.get())) {
263 set_controller(scripting_controller_old);
264 scripting_controller_old = 0;
269 Player::do_scripting_controller(std::string control, bool pressed)
271 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
272 if(control == std::string(Controller::controlNames[i])) {
273 scripting_controller->press(Controller::Control(i), pressed);
279 Player::adjust_height(float new_height)
282 bbox2.move(Vector(0, bbox.get_height() - new_height));
283 bbox2.set_height(new_height);
285 if(new_height > bbox.get_height()) {
286 Rectf additional_space = bbox2;
287 additional_space.set_height(new_height - bbox.get_height());
288 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
292 // adjust bbox accordingly
293 // note that we use members of moving_object for this, so we can run this during CD, too
295 set_size(bbox2.get_width(), bbox2.get_height());
300 Player::trigger_sequence(std::string sequence_name)
302 if (climbing) stop_climbing(*climbing);
303 GameSession::current()->start_sequence(sequence_name);
307 Player::update(float elapsed_time)
314 if(dying && dying_timer.check()) {
319 if(!dying && !deactivated)
322 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
326 // extend/shrink tux collision rectangle so that we fall through/walk over 1
328 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
334 // on downward slopes, adjust vertical velocity so tux walks smoothly down
336 if(floor_normal.y != 0) {
337 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
338 physic.set_velocity_y(250);
343 // handle backflipping
345 //prevent player from changing direction when backflipping
346 dir = (backflip_direction == 1) ? LEFT : RIGHT;
347 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
352 fall_mode = ON_GROUND;
353 last_ground_y = get_pos().y;
355 if(get_pos().y > last_ground_y)
357 else if(fall_mode == ON_GROUND)
361 // check if we landed
364 if (backflipping && (!backflip_timer.started())) {
365 backflipping = false;
366 backflip_direction = 0;
368 // if controls are currently deactivated, we take care of standing up ourselves
374 // calculate movement for this frame
375 movement = physic.get_movement(elapsed_time);
377 if(grabbed_object != NULL && !dying) {
378 Vector pos = get_pos() +
379 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
380 grabbed_object->grab(*this, pos, dir);
383 if(grabbed_object != NULL && dying){
384 grabbed_object->ungrab(*this, dir);
385 grabbed_object = NULL;
388 if(!ice_this_frame && on_ground())
391 on_ground_flag = false;
392 ice_this_frame = false;
394 // when invincible, spawn particles
395 if (invincible_timer.started() && !dying)
397 if (systemRandom.rand(0, 2) == 0) {
398 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
399 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
400 Vector ppos = Vector(px, py);
401 Vector pspeed = Vector(0, 0);
402 Vector paccel = Vector(0, 0);
403 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite",
404 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
405 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) ?
406 // make every other a longer sparkle to make trail a bit fuzzy
407 (size_t(game_time*20)%2) ? "small" : "medium"
409 "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
414 if (sprite->animation_done()) growing = false;
422 return on_ground_flag;
428 if(player_status->bonus == NO_BONUS)
435 Player::apply_friction()
437 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
438 physic.set_velocity_x(0);
439 physic.set_acceleration_x(0);
441 float friction = WALK_ACCELERATION_X * (on_ice ? ICE_FRICTION_MULTIPLIER : NORMAL_FRICTION_MULTIPLIER);
442 if(physic.get_velocity_x() < 0) {
443 physic.set_acceleration_x(friction);
444 } else if(physic.get_velocity_x() > 0) {
445 physic.set_acceleration_x(-friction);
446 } // no friction for physic.get_velocity_x() == 0
451 Player::handle_horizontal_input()
453 float vx = physic.get_velocity_x();
454 float vy = physic.get_velocity_y();
455 float ax = physic.get_acceleration_x();
456 float ay = physic.get_acceleration_y();
459 if(!duck || physic.get_velocity_y() != 0) {
460 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
464 } else if(!controller->hold(Controller::LEFT)
465 && controller->hold(Controller::RIGHT)) {
472 // do not run if action key is pressed or we're holding something
473 // so tux can only walk while shooting
474 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
475 ax = dirsign * WALK_ACCELERATION_X;
477 if(vx >= MAX_WALK_XM && dirsign > 0) {
480 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
485 if( vx * dirsign < MAX_WALK_XM ) {
486 ax = dirsign * WALK_ACCELERATION_X;
488 ax = dirsign * RUN_ACCELERATION_X;
491 if(vx >= MAX_RUN_XM && dirsign > 0) {
494 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
500 // we can reach WALK_SPEED without any acceleration
501 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
502 vx = dirsign * WALK_SPEED;
506 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
507 vx = dirsign * speedlimit;
511 // changing directions?
512 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
514 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
515 skidding_timer.start(SKID_TIME);
516 sound_manager->play("sounds/skid.wav");
517 // dust some particles
518 Sector::current()->add_object(
520 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
521 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
522 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
532 ax *= ICE_ACCELERATION_MULTIPLIER;
535 physic.set_velocity(vx, vy);
536 physic.set_acceleration(ax, ay);
538 // we get slower when not pressing any keys
560 if (physic.get_velocity_y() != 0)
567 if (adjust_height(31.8f)) {
570 unduck_hurt_timer.stop();
577 Player::do_standup() {
585 if (adjust_height(63.8f)) {
587 unduck_hurt_timer.stop();
589 // if timer is not already running, start it.
590 if (unduck_hurt_timer.get_period() == 0) {
591 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
593 else if (unduck_hurt_timer.check()) {
601 Player::do_backflip() {
607 backflip_direction = (dir == LEFT)?(+1):(-1);
610 sound_manager->play("sounds/flip.wav");
611 backflip_timer.start(0.15f);
615 Player::do_jump(float yspeed) {
619 physic.set_velocity_y(yspeed);
620 //bbox.move(Vector(0, -1));
622 on_ground_flag = false;
627 sound_manager->play("sounds/bigjump.wav");
629 sound_manager->play("sounds/jump.wav");
634 Player::early_jump_apex()
636 if (!jump_early_apex)
638 jump_early_apex = true;
639 physic.set_gravity_modifier(JUMP_EARLY_APEX_FACTOR);
644 Player::do_jump_apex()
648 jump_early_apex = false;
649 physic.set_gravity_modifier(1.0f);
654 Player::handle_vertical_input()
657 if(controller->pressed(Controller::JUMP)) jump_button_timer.start(JUMP_GRACE_TIME);
658 if(controller->hold(Controller::JUMP) && jump_button_timer.started() && can_jump) {
659 jump_button_timer.stop();
661 // when running, only jump a little bit; else do a backflip
662 if ((physic.get_velocity_x() != 0) ||
663 (controller->hold(Controller::LEFT)) ||
664 (controller->hold(Controller::RIGHT)))
673 // jump a bit higher if we are running; else do a normal jump
674 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
677 // Let go of jump key
678 else if(!controller->hold(Controller::JUMP)) {
679 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
685 if(jump_early_apex && physic.get_velocity_y() >= 0) {
689 /* In case the player has pressed Down while in a certain range of air,
690 enable butt jump action */
691 if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
692 wants_buttjump = true;
693 if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
696 /* When Down is not held anymore, disable butt jump */
697 if(!controller->hold(Controller::DOWN)) {
698 wants_buttjump = false;
699 does_buttjump = false;
703 physic.set_acceleration_y(0);
706 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
707 physic.set_acceleration_y(-2000);
708 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
714 Player::handle_input()
717 handle_input_ghost();
721 handle_input_climbing();
726 if( controller->released( Controller::PEEK_LEFT ) || controller->released( Controller::PEEK_RIGHT ) ) {
729 if( controller->released( Controller::PEEK_UP ) || controller->released( Controller::PEEK_DOWN ) ) {
732 if( controller->pressed( Controller::PEEK_LEFT ) ) {
735 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
738 if(!backflipping && !jumping && on_ground()) {
739 if( controller->pressed( Controller::PEEK_UP ) ) {
741 } else if( controller->pressed( Controller::PEEK_DOWN ) ) {
746 /* Handle horizontal movement: */
747 if (!backflipping) handle_horizontal_input();
753 /* Handle vertical movement: */
754 handle_vertical_input();
757 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
758 if(Sector::current()->add_bullet(
759 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
760 : Vector(32, bbox.get_height()/2)),
762 physic.get_velocity_x(), dir))
763 shooting_timer.start(SHOOTING_TIME);
766 /* Duck or Standup! */
767 if (controller->hold(Controller::DOWN)) {
776 if(!controller->hold(Controller::ACTION) && grabbed_object) {
777 // move the grabbed object a bit away from tux
778 Vector pos = get_pos() +
779 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
780 bbox.get_height()*0.66666 - 32);
781 Rectf dest(pos, pos + Vector(32, 32));
782 if(Sector::current()->is_free_of_movingstatics(dest)) {
783 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
785 moving_object->set_pos(pos);
787 log_debug << "Non MovingObject grabbed?!?" << std::endl;
789 if(controller->hold(Controller::UP)) {
790 grabbed_object->ungrab(*this, UP);
792 grabbed_object->ungrab(*this, dir);
794 grabbed_object = NULL;
802 if(controller->hold(Controller::ACTION) && !grabbed_object
804 Sector* sector = Sector::current();
807 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
809 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
812 for(Sector::Portables::iterator i = sector->portables.begin();
813 i != sector->portables.end(); ++i) {
814 Portable* portable = *i;
815 if(!portable->is_portable())
818 // make sure the Portable is a MovingObject
819 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
820 assert(moving_object);
821 if(moving_object == NULL)
824 // make sure the Portable isn't currently non-solid
825 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
827 // check if we are within reach
828 if(moving_object->get_bbox().contains(pos)) {
829 if (climbing) stop_climbing(*climbing);
830 grabbed_object = portable;
831 grabbed_object->grab(*this, get_pos(), dir);
839 Player::handle_input_ghost()
843 if (controller->hold(Controller::LEFT)) {
845 vx -= MAX_RUN_XM * 2;
847 if (controller->hold(Controller::RIGHT)) {
849 vx += MAX_RUN_XM * 2;
851 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
852 vy -= MAX_RUN_XM * 2;
854 if (controller->hold(Controller::DOWN)) {
855 vy += MAX_RUN_XM * 2;
857 if (controller->hold(Controller::ACTION)) {
858 set_ghost_mode(false);
860 physic.set_velocity(vx, vy);
861 physic.set_acceleration(0, 0);
865 Player::add_coins(int count)
867 player_status->add_coins(count);
873 return player_status->coins;
877 Player::add_bonus(const std::string& bonustype)
879 BonusType type = NO_BONUS;
881 if(bonustype == "grow") {
883 } else if(bonustype == "fireflower") {
885 } else if(bonustype == "iceflower") {
887 } else if(bonustype == "none") {
890 std::ostringstream msg;
891 msg << "Unknown bonus type " << bonustype;
892 throw std::runtime_error(msg.str());
895 return add_bonus(type);
899 Player::add_bonus(BonusType type, bool animate)
901 // always ignore NO_BONUS
902 if (type == NO_BONUS) {
906 // ignore GROWUP_BONUS if we're already big
907 if (type == GROWUP_BONUS) {
908 if (player_status->bonus == GROWUP_BONUS)
910 if (player_status->bonus == FIRE_BONUS)
912 if (player_status->bonus == ICE_BONUS)
916 return set_bonus(type, animate);
920 Player::set_bonus(BonusType type, bool animate)
922 if(player_status->bonus == NO_BONUS) {
923 if (!adjust_height(62.8f)) {
924 printf("can't adjust\n");
929 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
931 if (climbing) stop_climbing(*climbing);
934 if (type == NO_BONUS) {
935 if (does_buttjump) does_buttjump = false;
938 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
939 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
940 // visually lose helmet
941 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
942 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
943 Vector paccel = Vector(0, 1000);
944 std::string action = (dir==LEFT)?"left":"right";
945 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
946 if (climbing) stop_climbing(*climbing);
948 if ((player_status->bonus == ICE_BONUS) && (animate)) {
950 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
951 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
952 Vector paccel = Vector(0, 1000);
953 std::string action = (dir==LEFT)?"left":"right";
954 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
955 if (climbing) stop_climbing(*climbing);
957 player_status->max_fire_bullets = 0;
958 player_status->max_ice_bullets = 0;
960 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
961 if (type == ICE_BONUS) player_status->max_ice_bullets++;
963 player_status->bonus = type;
968 Player::set_visible(bool visible)
970 this->visible = visible;
972 set_group(COLGROUP_MOVING);
974 set_group(COLGROUP_DISABLED);
978 Player::get_visible()
986 kick_timer.start(KICK_TIME);
990 Player::draw(DrawingContext& context)
995 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
996 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
997 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
998 float py = Sector::current()->camera->get_translation().y;
999 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
1000 context.draw_surface(airarrow, Vector(px, py), LAYER_HUD - 1);
1003 std::string sa_prefix = "";
1004 std::string sa_postfix = "";
1006 if (player_status->bonus == GROWUP_BONUS)
1008 else if (player_status->bonus == FIRE_BONUS)
1010 else if (player_status->bonus == ICE_BONUS)
1013 sa_prefix = "small";
1016 sa_postfix = "-left";
1018 sa_postfix = "-right";
1020 /* Set Tux sprite action */
1022 sprite->set_action("gameover");
1025 sprite->set_action_continued("grow"+sa_postfix);
1026 // while growing, do not change action
1027 // do_duck() will take care of cancelling growing manually
1028 // update() will take care of cancelling when growing completed
1030 else if (climbing) {
1031 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1033 else if (backflipping) {
1034 sprite->set_action(sa_prefix+"-backflip"+sa_postfix);
1036 else if (duck && is_big()) {
1037 sprite->set_action(sa_prefix+"-duck"+sa_postfix);
1039 else if (skidding_timer.started() && !skidding_timer.check()) {
1040 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1042 else if (kick_timer.started() && !kick_timer.check()) {
1043 sprite->set_action(sa_prefix+"-kick"+sa_postfix);
1045 else if ((wants_buttjump || does_buttjump) && is_big()) {
1046 sprite->set_action(sa_prefix+"-buttjump"+sa_postfix);
1048 else if (!on_ground()) {
1049 sprite->set_action(sa_prefix+"-jump"+sa_postfix);
1052 if (fabsf(physic.get_velocity_x()) < 1.0f) {
1053 // Determine which idle stage we're at
1054 if (sprite->get_action().find("-stand-") == std::string::npos && sprite->get_action().find("-idle-") == std::string::npos) {
1056 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1058 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1060 else if (idle_timer.check() || (IDLE_TIME[idle_stage] == 0 && sprite->animation_done())) {
1062 if (idle_stage >= IDLE_STAGE_COUNT)
1065 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1067 if (IDLE_TIME[idle_stage] == 0)
1068 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix, 1);
1070 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1073 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1077 sprite->set_action(sa_prefix+"-walk"+sa_postfix);
1082 // Tux is holding something
1083 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1084 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
1092 if (safe_timer.started() && size_t(game_time*40)%2)
1095 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1101 Player::collision_tile(uint32_t tile_attributes)
1103 if(tile_attributes & Tile::HURTS)
1108 if( tile_attributes & Tile::WATER ){
1114 if( tile_attributes & Tile::WATER ){
1117 sound_manager->play( "sounds/splash.ogg" );
1122 if(tile_attributes & Tile::ICE) {
1123 ice_this_frame = true;
1129 Player::collision_solid(const CollisionHit& hit)
1132 if(physic.get_velocity_y() > 0)
1133 physic.set_velocity_y(0);
1135 on_ground_flag = true;
1136 floor_normal = hit.slope_normal;
1139 if (does_buttjump) {
1140 does_buttjump = false;
1141 physic.set_velocity_y(-300);
1142 on_ground_flag = false;
1143 Sector::current()->add_object(new Particles(
1144 Vector(get_bbox().p2.x, get_bbox().p2.y),
1146 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1148 Sector::current()->add_object(new Particles(
1149 Vector(get_bbox().p1.x, get_bbox().p2.y),
1151 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1155 } else if(hit.top) {
1156 if(physic.get_velocity_y() < 0)
1157 physic.set_velocity_y(.2f);
1160 if(hit.left || hit.right) {
1161 physic.set_velocity_x(0);
1166 if(hit.left || hit.right) {
1168 } else if(hit.top || hit.bottom) {
1175 Player::collision(GameObject& other, const CollisionHit& hit)
1177 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1182 if(hit.left || hit.right) {
1183 try_grab(); //grab objects right now, in update it will be too late
1185 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1186 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1187 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1188 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1190 if(controller->pressed(Controller::UP))
1191 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1197 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1198 if(badguy != NULL) {
1199 if(safe_timer.started() || invincible_timer.started())
1209 Player::make_invincible()
1211 sound_manager->play("sounds/invincible_start.ogg");
1212 invincible_timer.start(TUX_INVINCIBLE_TIME);
1213 Sector::current()->play_music(HERRING_MUSIC);
1218 Player::kill(bool completely)
1220 if(dying || deactivated)
1223 if(!completely && (safe_timer.started() || invincible_timer.started()))
1228 if (climbing) stop_climbing(*climbing);
1230 physic.set_velocity_x(0);
1232 if(!completely && is_big()) {
1233 sound_manager->play("sounds/hurt.wav");
1235 if(player_status->bonus == FIRE_BONUS
1236 || player_status->bonus == ICE_BONUS) {
1237 safe_timer.start(TUX_SAFE_TIME);
1238 set_bonus(GROWUP_BONUS, true);
1239 } else if(player_status->bonus == GROWUP_BONUS) {
1240 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1241 adjust_height(30.8f);
1243 backflipping = false;
1244 set_bonus(NO_BONUS, true);
1245 } else if(player_status->bonus == NO_BONUS) {
1246 safe_timer.start(TUX_SAFE_TIME);
1247 adjust_height(30.8f);
1251 sound_manager->play("sounds/kill.wav");
1253 // do not die when in edit mode
1255 set_ghost_mode(true);
1259 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1261 for (int i = 0; i < 5; i++)
1263 // the numbers: starting x, starting y, velocity y
1264 Sector::current()->add_object(new FallingCoin(get_pos() +
1265 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1266 systemRandom.rand(-100,100)));
1268 player_status->coins -= std::max(player_status->coins/10, 25);
1272 GameSession::current()->set_reset_point("", Vector());
1274 physic.enable_gravity(true);
1275 physic.set_acceleration(0, 0);
1276 physic.set_velocity(0, -700);
1277 set_bonus(NO_BONUS, true);
1279 dying_timer.start(3.0);
1280 set_group(COLGROUP_DISABLED);
1282 Sector::current()->effect->fade_out(3.0);
1283 sound_manager->stop_music(3.0);
1288 Player::move(const Vector& vector)
1292 // TODO: do we need the following? Seems irrelevant to moving the player
1294 set_size(31.8f, 63.8f);
1296 set_size(31.8f, 31.8f);
1298 last_ground_y = vector.y;
1299 if (climbing) stop_climbing(*climbing);
1305 Player::check_bounds(Camera* camera)
1307 /* Keep tux in sector bounds: */
1308 if (get_pos().x < 0) {
1309 // Lock Tux to the size of the level, so that he doesn't fall off
1311 set_pos(Vector(0, get_pos().y));
1314 if (get_bbox().get_right() > Sector::current()->get_width()) {
1315 // Lock Tux to the size of the level, so that he doesn't fall off
1317 set_pos(Vector(Sector::current()->get_width() - get_bbox().get_width(), get_pos().y));
1320 /* fallen out of the level? */
1321 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1326 // can happen if back scrolling is disabled
1327 if(get_pos().x < camera->get_translation().x) {
1328 set_pos(Vector(camera->get_translation().x, get_pos().y));
1330 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1333 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1339 Player::add_velocity(const Vector& velocity)
1341 physic.set_velocity(physic.get_velocity() + velocity);
1345 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1347 if (end_speed.x > 0)
1348 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1349 if (end_speed.x < 0)
1350 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1351 if (end_speed.y > 0)
1352 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1353 if (end_speed.y < 0)
1354 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1358 Player::get_velocity()
1360 return physic.get_velocity();
1364 Player::bounce(BadGuy& )
1366 if(controller->hold(Controller::JUMP))
1367 physic.set_velocity_y(-520);
1369 physic.set_velocity_y(-300);
1372 //scripting Functions Below
1375 Player::deactivate()
1380 physic.set_velocity_x(0);
1381 physic.set_velocity_y(0);
1382 physic.set_acceleration_x(0);
1383 physic.set_acceleration_y(0);
1384 if (climbing) stop_climbing(*climbing);
1392 deactivated = false;
1395 void Player::walk(float speed)
1397 physic.set_velocity_x(speed);
1401 Player::set_ghost_mode(bool enable)
1403 if (ghost_mode == enable)
1406 if (climbing) stop_climbing(*climbing);
1410 set_group(COLGROUP_DISABLED);
1411 physic.enable_gravity(false);
1412 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1415 set_group(COLGROUP_MOVING);
1416 physic.enable_gravity(true);
1417 log_debug << "You feel solid again." << std::endl;
1422 Player::set_edit_mode(bool enable)
1428 Player::start_climbing(Climbable& climbable)
1430 if (climbing == &climbable) return;
1432 climbing = &climbable;
1433 physic.enable_gravity(false);
1434 physic.set_velocity(0, 0);
1435 physic.set_acceleration(0, 0);
1439 Player::stop_climbing(Climbable& /*climbable*/)
1441 if (!climbing) return;
1445 if (grabbed_object) {
1446 grabbed_object->ungrab(*this, dir);
1447 grabbed_object = NULL;
1450 physic.enable_gravity(true);
1451 physic.set_velocity(0, 0);
1452 physic.set_acceleration(0, 0);
1454 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1455 on_ground_flag = true;
1456 // TODO: This won't help. Why?
1462 Player::handle_input_climbing()
1465 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1471 if (controller->hold(Controller::LEFT)) {
1475 if (controller->hold(Controller::RIGHT)) {
1479 if (controller->hold(Controller::UP)) {
1482 if (controller->hold(Controller::DOWN)) {
1485 if (controller->hold(Controller::JUMP)) {
1487 stop_climbing(*climbing);
1493 if (controller->hold(Controller::ACTION)) {
1494 stop_climbing(*climbing);
1497 physic.set_velocity(vx, vy);
1498 physic.set_acceleration(0, 0);