4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include "gettext.hpp"
27 #include "sprite/sprite_manager.hpp"
28 #include "audio/sound_manager.hpp"
31 #include "sprite/sprite.hpp"
33 #include "resources.hpp"
34 #include "statistics.hpp"
35 #include "game_session.hpp"
36 #include "object/tilemap.hpp"
37 #include "object/camera.hpp"
38 #include "object/particles.hpp"
39 #include "object/portable.hpp"
40 #include "object/bullet.hpp"
41 #include "trigger/trigger_base.hpp"
42 #include "control/joystickkeyboardcontroller.hpp"
43 #include "scripting/squirrel_util.hpp"
45 #include "platform.hpp"
46 #include "badguy/badguy.hpp"
47 #include "player_status.hpp"
49 #include "falling_coin.hpp"
50 #include "random_generator.hpp"
51 #include "object/sprite_particle.hpp"
52 #include "trigger/climbable.hpp"
57 static const int TILES_FOR_BUTTJUMP = 3;
58 static const float BUTTJUMP_MIN_VELOCITY_Y = 400.0f;
59 static const float SHOOTING_TIME = .150f;
61 /** number of idle stages, including standing */
62 static const unsigned int IDLE_STAGE_COUNT = 5;
64 * how long to play each idle animation in milliseconds
65 * '0' means the sprite action is played once before moving onto the next
68 static const int IDLE_TIME[] = { 5000, 0, 2500, 0, 2500 };
70 static const std::string IDLE_STAGES[] =
77 /** acceleration in horizontal direction when walking
78 * (all accelerations are in pixel/s^2) */
79 static const float WALK_ACCELERATION_X = 300;
80 /** acceleration in horizontal direction when running */
81 static const float RUN_ACCELERATION_X = 400;
82 /** acceleration when skidding */
83 static const float SKID_XM = 200;
84 /** time of skidding in seconds */
85 static const float SKID_TIME = .3f;
86 /** maximum walk velocity (pixel/s) */
87 static const float MAX_WALK_XM = 230;
88 /** maximum run velocity (pixel/s) */
89 static const float MAX_RUN_XM = 320;
90 /** maximum horizontal climb velocity */
91 static const float MAX_CLIMB_XM = 48;
92 /** maximum vertical climb velocity */
93 static const float MAX_CLIMB_YM = 128;
94 /** instant velocity when tux starts to walk */
95 static const float WALK_SPEED = 100;
97 /** time of the kick (kicking mriceblock) animation */
98 static const float KICK_TIME = .3f;
99 /** time of tux cheering (currently unused) */
100 static const float CHEER_TIME = 1.0f;
102 /** if Tux cannot unduck for this long, he will get hurt */
103 static const float UNDUCK_HURT_TIME = 0.25f;
104 /** gravity is higher after the jump key is released before
105 the apex of the jump is reached */
106 static const float JUMP_EARLY_APEX_FACTOR = 3.0;
108 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) */
110 bool no_water = true;
113 Player::Player(PlayerStatus* _player_status, const std::string& name)
114 : scripting_controller(0),
115 player_status(_player_status),
116 scripting_controller_old(0),
117 grabbed_object(NULL), ghost_mode(false), edit_mode(false), idle_stage(0),
121 controller = main_controller;
122 scripting_controller = new CodeController();
123 sprite = sprite_manager->create("images/creatures/tux/tux.sprite");
124 airarrow.reset(new Surface("images/engine/hud/airarrow.png"));
125 idle_timer.start(IDLE_TIME[0]/1000.0f);
127 sound_manager->preload("sounds/bigjump.wav");
128 sound_manager->preload("sounds/jump.wav");
129 sound_manager->preload("sounds/hurt.wav");
130 sound_manager->preload("sounds/skid.wav");
131 sound_manager->preload("sounds/flip.wav");
132 sound_manager->preload("sounds/invincible_start.ogg");
133 sound_manager->preload("sounds/splash.ogg");
140 if (climbing) stop_climbing(*climbing);
142 delete scripting_controller;
149 set_size(31.8f, 62.8f);
151 set_size(31.8f, 30.8f);
162 fall_mode = ON_GROUND;
164 jump_early_apex = false;
166 wants_buttjump = false;
167 does_buttjump = false;
170 backflipping = false;
171 backflip_direction = 0;
174 speedlimit = 0; //no special limit
176 on_ground_flag = false;
177 grabbed_object = NULL;
185 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
190 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
194 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
199 Scripting::unexpose_object(vm, table_idx, name);
203 Player::get_speedlimit()
209 Player::set_speedlimit(float newlimit)
215 Player::set_controller(Controller* controller)
217 this->controller = controller;
221 Player::use_scripting_controller(bool use_or_release)
223 if ((use_or_release == true) && (controller != scripting_controller)) {
224 scripting_controller_old = get_controller();
225 set_controller(scripting_controller);
227 if ((use_or_release == false) && (controller == scripting_controller)) {
228 set_controller(scripting_controller_old);
229 scripting_controller_old = 0;
234 Player::do_scripting_controller(std::string control, bool pressed)
236 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
237 if(control == std::string(Controller::controlNames[i])) {
238 scripting_controller->press(Controller::Control(i), pressed);
244 Player::adjust_height(float new_height)
247 bbox2.move(Vector(0, bbox.get_height() - new_height));
248 bbox2.set_height(new_height);
250 if(new_height > bbox.get_height()) {
251 Rect additional_space = bbox2;
252 additional_space.set_height(new_height - bbox.get_height());
253 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
257 // adjust bbox accordingly
258 // note that we use members of moving_object for this, so we can run this during CD, too
260 set_size(bbox2.get_width(), bbox2.get_height());
265 Player::trigger_sequence(std::string sequence_name)
267 if (climbing) stop_climbing(*climbing);
268 GameSession::current()->start_sequence(sequence_name);
272 Player::update(float elapsed_time)
279 if(dying && dying_timer.check()) {
284 if(!dying && !deactivated)
287 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
291 // extend/shrink tux collision rectangle so that we fall through/walk over 1
293 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
299 // on downward slopes, adjust vertical velocity so tux walks smoothly down
301 if(floor_normal.y != 0) {
302 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
303 physic.set_velocity_y(250);
308 // handle backflipping
310 //prevent player from changing direction when backflipping
311 dir = (backflip_direction == 1) ? LEFT : RIGHT;
312 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
317 fall_mode = ON_GROUND;
318 last_ground_y = get_pos().y;
320 if(get_pos().y > last_ground_y)
322 else if(fall_mode == ON_GROUND)
326 // check if we landed
329 if (backflipping && (!backflip_timer.started())) {
330 backflipping = false;
331 backflip_direction = 0;
333 // if controls are currently deactivated, we take care of standing up ourselves
339 // calculate movement for this frame
340 movement = physic.get_movement(elapsed_time);
342 if(grabbed_object != NULL && !dying) {
343 Vector pos = get_pos() +
344 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
345 grabbed_object->grab(*this, pos, dir);
348 if(grabbed_object != NULL && dying){
349 grabbed_object->ungrab(*this, dir);
350 grabbed_object = NULL;
353 on_ground_flag = false;
355 // when invincible, spawn particles
356 if (invincible_timer.started() && !dying)
358 if (systemRandom.rand(0, 2) == 0) {
359 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
360 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
361 Vector ppos = Vector(px, py);
362 Vector pspeed = Vector(0, 0);
363 Vector paccel = Vector(0, 0);
364 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
365 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
366 // make every other a longer sparkle to make trail a bit fuzzy
367 if (size_t(game_time*20)%2) {
368 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
370 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
373 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
379 if (sprite->animation_done()) growing = false;
387 return on_ground_flag;
393 if(player_status->bonus == NO_BONUS)
400 Player::apply_friction()
402 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
403 physic.set_velocity_x(0);
404 physic.set_acceleration_x(0);
405 } else if(physic.get_velocity_x() < 0) {
406 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
407 } else if(physic.get_velocity_x() > 0) {
408 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
413 Player::handle_horizontal_input()
415 float vx = physic.get_velocity_x();
416 float vy = physic.get_velocity_y();
417 float ax = physic.get_acceleration_x();
418 float ay = physic.get_acceleration_y();
421 if(!duck || physic.get_velocity_y() != 0) {
422 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
426 } else if(!controller->hold(Controller::LEFT)
427 && controller->hold(Controller::RIGHT)) {
434 // do not run if action key is pressed or we're holding something
435 // so tux can only walk while shooting
436 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
437 ax = dirsign * WALK_ACCELERATION_X;
439 if(vx >= MAX_WALK_XM && dirsign > 0) {
442 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
447 if( vx * dirsign < MAX_WALK_XM ) {
448 ax = dirsign * WALK_ACCELERATION_X;
450 ax = dirsign * RUN_ACCELERATION_X;
453 if(vx >= MAX_RUN_XM && dirsign > 0) {
456 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
462 // we can reach WALK_SPEED without any acceleration
463 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
464 vx = dirsign * WALK_SPEED;
468 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
469 vx = dirsign * speedlimit;
473 // changing directions?
474 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
476 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
477 skidding_timer.start(SKID_TIME);
478 sound_manager->play("sounds/skid.wav");
479 // dust some particles
480 Sector::current()->add_object(
482 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
483 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
484 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
493 physic.set_velocity(vx, vy);
494 physic.set_acceleration(ax, ay);
496 // we get slower when not pressing any keys
518 if (physic.get_velocity_y() != 0)
525 if (adjust_height(31.8f)) {
528 unduck_hurt_timer.stop();
535 Player::do_standup() {
543 if (adjust_height(63.8f)) {
545 unduck_hurt_timer.stop();
547 // if timer is not already running, start it.
548 if (unduck_hurt_timer.get_period() == 0) {
549 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
551 else if (unduck_hurt_timer.check()) {
559 Player::do_backflip() {
565 backflip_direction = (dir == LEFT)?(+1):(-1);
568 sound_manager->play("sounds/flip.wav");
569 backflip_timer.start(0.15f);
573 Player::do_jump(float yspeed) {
577 physic.set_velocity_y(yspeed);
578 //bbox.move(Vector(0, -1));
580 on_ground_flag = false;
585 sound_manager->play("sounds/bigjump.wav");
587 sound_manager->play("sounds/jump.wav");
592 Player::early_jump_apex() {
593 if(jump_early_apex) {
596 jump_early_apex = true;
597 physic.set_gravity(physic.get_gravity() * JUMP_EARLY_APEX_FACTOR);
601 Player::do_jump_apex() {
602 if(!jump_early_apex) {
605 jump_early_apex = false;
606 physic.set_gravity(physic.get_gravity() / JUMP_EARLY_APEX_FACTOR);
610 Player::handle_vertical_input()
613 if(controller->pressed(Controller::JUMP)) jump_button_timer.start(JUMP_GRACE_TIME);
614 if(controller->hold(Controller::JUMP) && jump_button_timer.started() && can_jump) {
615 jump_button_timer.stop();
617 // when running, only jump a little bit; else do a backflip
618 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
620 // jump a bit higher if we are running; else do a normal jump
621 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
624 // Let go of jump key
625 else if(!controller->hold(Controller::JUMP)) {
626 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
632 if(jump_early_apex && physic.get_velocity_y() >= 0) {
636 /* In case the player has pressed Down while in a certain range of air,
637 enable butt jump action */
638 if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
639 wants_buttjump = true;
640 if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
643 /* When Down is not held anymore, disable butt jump */
644 if(!controller->hold(Controller::DOWN)) {
645 wants_buttjump = false;
646 does_buttjump = false;
650 physic.set_acceleration_y(0);
653 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
654 physic.set_acceleration_y(-2000);
655 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
661 Player::handle_input()
664 handle_input_ghost();
668 handle_input_climbing();
673 if( controller->released( Controller::PEEK_LEFT ) ) {
676 if( controller->released( Controller::PEEK_RIGHT ) ) {
679 if( controller->released( Controller::PEEK_UP ) ) {
682 if( controller->released( Controller::PEEK_DOWN ) ) {
685 if( controller->pressed( Controller::PEEK_LEFT ) ) {
688 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
691 if(!backflipping && !jumping && on_ground()) {
692 if( controller->pressed( Controller::PEEK_UP ) ) {
694 } else if( controller->pressed( Controller::PEEK_DOWN ) ) {
699 /* Handle horizontal movement: */
700 if (!backflipping) handle_horizontal_input();
706 /* Handle vertical movement: */
707 handle_vertical_input();
710 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
711 if(Sector::current()->add_bullet(
712 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
713 : Vector(32, bbox.get_height()/2)),
714 physic.get_velocity_x(), dir))
715 shooting_timer.start(SHOOTING_TIME);
718 /* Duck or Standup! */
719 if (controller->hold(Controller::DOWN)) {
728 if(!controller->hold(Controller::ACTION) && grabbed_object) {
729 // move the grabbed object a bit away from tux
730 Vector pos = get_pos() +
731 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
732 bbox.get_height()*0.66666 - 32);
733 Rect dest(pos, pos + Vector(32, 32));
734 if(Sector::current()->is_free_of_movingstatics(dest)) {
735 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
737 moving_object->set_pos(pos);
739 log_debug << "Non MovingObject grabbed?!?" << std::endl;
741 if(controller->hold(Controller::UP)) {
742 grabbed_object->ungrab(*this, UP);
744 grabbed_object->ungrab(*this, dir);
746 grabbed_object = NULL;
754 if(controller->hold(Controller::ACTION) && !grabbed_object
756 Sector* sector = Sector::current();
759 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
761 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
764 for(Sector::Portables::iterator i = sector->portables.begin();
765 i != sector->portables.end(); ++i) {
766 Portable* portable = *i;
767 if(!portable->is_portable())
770 // make sure the Portable is a MovingObject
771 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
772 assert(moving_object);
773 if(moving_object == NULL)
776 // make sure the Portable isn't currently non-solid
777 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
779 // check if we are within reach
780 if(moving_object->get_bbox().contains(pos)) {
781 if (climbing) stop_climbing(*climbing);
782 grabbed_object = portable;
783 grabbed_object->grab(*this, get_pos(), dir);
791 Player::handle_input_ghost()
795 if (controller->hold(Controller::LEFT)) {
797 vx -= MAX_RUN_XM * 2;
799 if (controller->hold(Controller::RIGHT)) {
801 vx += MAX_RUN_XM * 2;
803 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
804 vy -= MAX_RUN_XM * 2;
806 if (controller->hold(Controller::DOWN)) {
807 vy += MAX_RUN_XM * 2;
809 if (controller->hold(Controller::ACTION)) {
810 set_ghost_mode(false);
812 physic.set_velocity(vx, vy);
813 physic.set_acceleration(0, 0);
817 Player::add_coins(int count)
819 player_status->add_coins(count);
825 return player_status->coins;
829 Player::add_bonus(const std::string& bonustype)
831 BonusType type = NO_BONUS;
833 if(bonustype == "grow") {
835 } else if(bonustype == "fireflower") {
837 } else if(bonustype == "iceflower") {
839 } else if(bonustype == "none") {
842 std::ostringstream msg;
843 msg << "Unknown bonus type " << bonustype;
844 throw std::runtime_error(msg.str());
847 return add_bonus(type);
851 Player::add_bonus(BonusType type, bool animate)
853 // always ignore NO_BONUS
854 if (type == NO_BONUS) {
858 // ignore GROWUP_BONUS if we're already big
859 if (type == GROWUP_BONUS) {
860 if (player_status->bonus == GROWUP_BONUS)
862 if (player_status->bonus == FIRE_BONUS)
864 if (player_status->bonus == ICE_BONUS)
868 return set_bonus(type, animate);
872 Player::set_bonus(BonusType type, bool animate)
874 if(player_status->bonus == NO_BONUS) {
875 if (!adjust_height(62.8f)) {
876 printf("can't adjust\n");
881 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
883 if (climbing) stop_climbing(*climbing);
886 if (type == NO_BONUS) {
887 if (does_buttjump) does_buttjump = false;
890 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
891 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
892 // visually lose helmet
893 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
894 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
895 Vector paccel = Vector(0, 1000);
896 std::string action = (dir==LEFT)?"left":"right";
897 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
898 if (climbing) stop_climbing(*climbing);
900 if ((player_status->bonus == ICE_BONUS) && (animate)) {
902 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
903 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
904 Vector paccel = Vector(0, 1000);
905 std::string action = (dir==LEFT)?"left":"right";
906 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
907 if (climbing) stop_climbing(*climbing);
909 player_status->max_fire_bullets = 0;
910 player_status->max_ice_bullets = 0;
912 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
913 if (type == ICE_BONUS) player_status->max_ice_bullets++;
915 player_status->bonus = type;
920 Player::set_visible(bool visible)
922 this->visible = visible;
924 set_group(COLGROUP_MOVING);
926 set_group(COLGROUP_DISABLED);
930 Player::get_visible()
938 kick_timer.start(KICK_TIME);
942 Player::draw(DrawingContext& context)
947 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
948 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
949 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
950 float py = Sector::current()->camera->get_translation().y;
951 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
952 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
955 std::string sa_prefix = "";
956 std::string sa_postfix = "";
958 if (player_status->bonus == GROWUP_BONUS)
960 else if (player_status->bonus == FIRE_BONUS)
962 else if (player_status->bonus == ICE_BONUS)
968 sa_postfix = "-left";
970 sa_postfix = "-right";
972 /* Set Tux sprite action */
974 sprite->set_action("gameover");
977 sprite->set_action_continued("grow"+sa_postfix);
978 // while growing, do not change action
979 // do_duck() will take care of cancelling growing manually
980 // update() will take care of cancelling when growing completed
983 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
985 else if (backflipping) {
986 sprite->set_action(sa_prefix+"-backflip"+sa_postfix);
988 else if (duck && is_big()) {
989 sprite->set_action(sa_prefix+"-duck"+sa_postfix);
991 else if (skidding_timer.started() && !skidding_timer.check()) {
992 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
994 else if (kick_timer.started() && !kick_timer.check()) {
995 sprite->set_action(sa_prefix+"-kick"+sa_postfix);
997 else if ((wants_buttjump || does_buttjump) && is_big()) {
998 sprite->set_action(sa_prefix+"-buttjump"+sa_postfix);
1000 else if (!on_ground()) {
1001 sprite->set_action(sa_prefix+"-jump"+sa_postfix);
1004 if (fabsf(physic.get_velocity_x()) < 1.0f) {
1005 // Determine which idle stage we're at
1006 if (sprite->get_action().find("-stand-") == std::string::npos && sprite->get_action().find("-idle-") == std::string::npos) {
1008 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1010 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1012 else if (idle_timer.check() || (IDLE_TIME[idle_stage] == 0 && sprite->animation_done())) {
1014 if (idle_stage >= IDLE_STAGE_COUNT)
1017 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1019 if (IDLE_TIME[idle_stage] == 0)
1020 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix, 1);
1022 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1025 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1029 sprite->set_action(sa_prefix+"-walk"+sa_postfix);
1035 // Tux is holding something
1036 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1037 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
1045 if (safe_timer.started() && size_t(game_time*40)%2)
1048 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1054 Player::collision_tile(uint32_t tile_attributes)
1056 if(tile_attributes & Tile::HURTS)
1061 if( tile_attributes & Tile::WATER ){
1067 if( tile_attributes & Tile::WATER ){
1070 sound_manager->play( "sounds/splash.ogg" );
1077 Player::collision_solid(const CollisionHit& hit)
1080 if(physic.get_velocity_y() > 0)
1081 physic.set_velocity_y(0);
1083 on_ground_flag = true;
1084 floor_normal = hit.slope_normal;
1087 if (does_buttjump) {
1088 does_buttjump = false;
1089 physic.set_velocity_y(-300);
1090 on_ground_flag = false;
1091 Sector::current()->add_object(new Particles(
1092 Vector(get_bbox().p2.x, get_bbox().p2.y),
1094 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1096 Sector::current()->add_object(new Particles(
1097 Vector(get_bbox().p1.x, get_bbox().p2.y),
1099 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1103 } else if(hit.top) {
1104 if(physic.get_velocity_y() < 0)
1105 physic.set_velocity_y(.2f);
1108 if(hit.left || hit.right) {
1109 physic.set_velocity_x(0);
1114 if(hit.left || hit.right) {
1116 } else if(hit.top || hit.bottom) {
1123 Player::collision(GameObject& other, const CollisionHit& hit)
1125 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1130 if(hit.left || hit.right) {
1131 try_grab(); //grab objects right now, in update it will be too late
1134 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1136 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1137 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1138 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1140 if(controller->pressed(Controller::UP))
1141 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1147 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1148 if(badguy != NULL) {
1149 if(safe_timer.started() || invincible_timer.started())
1159 Player::make_invincible()
1161 sound_manager->play("sounds/invincible_start.ogg");
1162 invincible_timer.start(TUX_INVINCIBLE_TIME);
1163 Sector::current()->play_music(HERRING_MUSIC);
1168 Player::kill(bool completely)
1170 if(dying || deactivated)
1173 if(!completely && (safe_timer.started() || invincible_timer.started()))
1178 sound_manager->play("sounds/hurt.wav");
1180 if (climbing) stop_climbing(*climbing);
1182 physic.set_velocity_x(0);
1184 if(!completely && is_big()) {
1185 if(player_status->bonus == FIRE_BONUS
1186 || player_status->bonus == ICE_BONUS) {
1187 safe_timer.start(TUX_SAFE_TIME);
1188 set_bonus(GROWUP_BONUS, true);
1189 } else if(player_status->bonus == GROWUP_BONUS) {
1190 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1191 adjust_height(30.8f);
1193 backflipping = false;
1194 set_bonus(NO_BONUS, true);
1195 } else if(player_status->bonus == NO_BONUS) {
1196 safe_timer.start(TUX_SAFE_TIME);
1197 adjust_height(30.8f);
1202 // do not die when in edit mode
1204 set_ghost_mode(true);
1208 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1210 for (int i = 0; i < 5; i++)
1212 // the numbers: starting x, starting y, velocity y
1213 Sector::current()->add_object(new FallingCoin(get_pos() +
1214 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1215 systemRandom.rand(-100,100)));
1217 player_status->coins -= std::max(player_status->coins/10, 25);
1221 GameSession::current()->set_reset_point("", Vector());
1223 physic.enable_gravity(true);
1224 physic.set_acceleration(0, 0);
1225 physic.set_velocity(0, -700);
1226 set_bonus(NO_BONUS, true);
1228 dying_timer.start(3.0);
1229 set_group(COLGROUP_DISABLED);
1231 DisplayEffect* effect = new DisplayEffect();
1232 effect->fade_out(3.0);
1233 Sector::current()->add_object(effect);
1234 sound_manager->stop_music(3.0);
1239 Player::move(const Vector& vector)
1243 // TODO: do we need the following? Seems irrelevant to moving the player
1245 set_size(31.8f, 63.8f);
1247 set_size(31.8f, 31.8f);
1249 last_ground_y = vector.y;
1250 if (climbing) stop_climbing(*climbing);
1256 Player::check_bounds(Camera* camera)
1258 /* Keep tux in bounds: */
1259 if (get_pos().x < 0) {
1260 // Lock Tux to the size of the level, so that he doesn't fall of
1262 set_pos(Vector(0, get_pos().y));
1265 /* fallen out of the level? */
1266 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1271 // can happen if back scrolling is disabled
1272 if(get_pos().x < camera->get_translation().x) {
1273 set_pos(Vector(camera->get_translation().x, get_pos().y));
1275 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1278 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1284 Player::add_velocity(const Vector& velocity)
1286 physic.set_velocity(physic.get_velocity() + velocity);
1290 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1292 if (end_speed.x > 0)
1293 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1294 if (end_speed.x < 0)
1295 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1296 if (end_speed.y > 0)
1297 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1298 if (end_speed.y < 0)
1299 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1303 Player::get_velocity()
1305 return physic.get_velocity();
1309 Player::bounce(BadGuy& )
1311 if(controller->hold(Controller::JUMP))
1312 physic.set_velocity_y(-520);
1314 physic.set_velocity_y(-300);
1317 //Scripting Functions Below
1320 Player::deactivate()
1325 physic.set_velocity_x(0);
1326 physic.set_velocity_y(0);
1327 physic.set_acceleration_x(0);
1328 physic.set_acceleration_y(0);
1329 if (climbing) stop_climbing(*climbing);
1337 deactivated = false;
1340 void Player::walk(float speed)
1342 physic.set_velocity_x(speed);
1346 Player::set_ghost_mode(bool enable)
1348 if (ghost_mode == enable)
1351 if (climbing) stop_climbing(*climbing);
1355 set_group(COLGROUP_DISABLED);
1356 physic.enable_gravity(false);
1357 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1360 set_group(COLGROUP_MOVING);
1361 physic.enable_gravity(true);
1362 log_debug << "You feel solid again." << std::endl;
1368 Player::set_edit_mode(bool enable)
1374 Player::start_climbing(Climbable& climbable)
1376 if (climbing == &climbable) return;
1378 climbing = &climbable;
1379 physic.enable_gravity(false);
1380 physic.set_velocity(0, 0);
1381 physic.set_acceleration(0, 0);
1385 Player::stop_climbing(Climbable& /*climbable*/)
1387 if (!climbing) return;
1391 if (grabbed_object) {
1392 grabbed_object->ungrab(*this, dir);
1393 grabbed_object = NULL;
1396 physic.enable_gravity(true);
1397 physic.set_velocity(0, 0);
1398 physic.set_acceleration(0, 0);
1400 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1401 on_ground_flag = true;
1402 // TODO: This won't help. Why?
1408 Player::handle_input_climbing()
1411 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1417 if (controller->hold(Controller::LEFT)) {
1421 if (controller->hold(Controller::RIGHT)) {
1425 if (controller->hold(Controller::UP)) {
1428 if (controller->hold(Controller::DOWN)) {
1431 if (controller->hold(Controller::JUMP)) {
1433 stop_climbing(*climbing);
1439 if (controller->hold(Controller::ACTION)) {
1440 stop_climbing(*climbing);
1443 physic.set_velocity(vx, vy);
1444 physic.set_acceleration(0, 0);