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 /** multiplied by WALK_ACCELERATION to give friction */
98 static const float NORMAL_FRICTION_MULTIPLIER = 1.5f;
99 /** multiplied by WALK_ACCELERATION to give friction */
100 static const float ICE_FRICTION_MULTIPLIER = 0.1f;
101 static const float ICE_ACCELERATION_MULTIPLIER = 0.25f;
103 /** time of the kick (kicking mriceblock) animation */
104 static const float KICK_TIME = .3f;
105 /** time of tux cheering (currently unused) */
106 static const float CHEER_TIME = 1.0f;
108 /** if Tux cannot unduck for this long, he will get hurt */
109 static const float UNDUCK_HURT_TIME = 0.25f;
110 /** gravity is higher after the jump key is released before
111 the apex of the jump is reached */
112 static const float JUMP_EARLY_APEX_FACTOR = 3.0;
114 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) */
116 bool no_water = true;
119 Player::Player(PlayerStatus* _player_status, const std::string& name)
120 : scripting_controller(0),
121 player_status(_player_status),
122 scripting_controller_old(0),
123 grabbed_object(NULL), ghost_mode(false), edit_mode(false), idle_stage(0),
127 controller = main_controller;
128 scripting_controller = new CodeController();
129 sprite = sprite_manager->create("images/creatures/tux/tux.sprite");
130 airarrow.reset(new Surface("images/engine/hud/airarrow.png"));
131 idle_timer.start(IDLE_TIME[0]/1000.0f);
133 sound_manager->preload("sounds/bigjump.wav");
134 sound_manager->preload("sounds/jump.wav");
135 sound_manager->preload("sounds/hurt.wav");
136 sound_manager->preload("sounds/skid.wav");
137 sound_manager->preload("sounds/flip.wav");
138 sound_manager->preload("sounds/invincible_start.ogg");
139 sound_manager->preload("sounds/splash.ogg");
146 if (climbing) stop_climbing(*climbing);
148 delete scripting_controller;
155 set_size(31.8f, 62.8f);
157 set_size(31.8f, 30.8f);
168 fall_mode = ON_GROUND;
170 jump_early_apex = false;
172 wants_buttjump = false;
173 does_buttjump = false;
176 backflipping = false;
177 backflip_direction = 0;
181 ice_this_frame = false;
182 speedlimit = 0; //no special limit
184 on_ground_flag = false;
185 grabbed_object = NULL;
193 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
198 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
202 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
207 Scripting::unexpose_object(vm, table_idx, name);
211 Player::get_speedlimit()
217 Player::set_speedlimit(float newlimit)
223 Player::set_controller(Controller* controller)
225 this->controller = controller;
229 Player::use_scripting_controller(bool use_or_release)
231 if ((use_or_release == true) && (controller != scripting_controller)) {
232 scripting_controller_old = get_controller();
233 set_controller(scripting_controller);
235 if ((use_or_release == false) && (controller == scripting_controller)) {
236 set_controller(scripting_controller_old);
237 scripting_controller_old = 0;
242 Player::do_scripting_controller(std::string control, bool pressed)
244 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
245 if(control == std::string(Controller::controlNames[i])) {
246 scripting_controller->press(Controller::Control(i), pressed);
252 Player::adjust_height(float new_height)
255 bbox2.move(Vector(0, bbox.get_height() - new_height));
256 bbox2.set_height(new_height);
258 if(new_height > bbox.get_height()) {
259 Rect additional_space = bbox2;
260 additional_space.set_height(new_height - bbox.get_height());
261 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
265 // adjust bbox accordingly
266 // note that we use members of moving_object for this, so we can run this during CD, too
268 set_size(bbox2.get_width(), bbox2.get_height());
273 Player::trigger_sequence(std::string sequence_name)
275 if (climbing) stop_climbing(*climbing);
276 GameSession::current()->start_sequence(sequence_name);
280 Player::update(float elapsed_time)
287 if(dying && dying_timer.check()) {
292 if(!dying && !deactivated)
295 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
299 // extend/shrink tux collision rectangle so that we fall through/walk over 1
301 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
307 // on downward slopes, adjust vertical velocity so tux walks smoothly down
309 if(floor_normal.y != 0) {
310 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
311 physic.set_velocity_y(250);
316 // handle backflipping
318 //prevent player from changing direction when backflipping
319 dir = (backflip_direction == 1) ? LEFT : RIGHT;
320 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
325 fall_mode = ON_GROUND;
326 last_ground_y = get_pos().y;
328 if(get_pos().y > last_ground_y)
330 else if(fall_mode == ON_GROUND)
334 // check if we landed
337 if (backflipping && (!backflip_timer.started())) {
338 backflipping = false;
339 backflip_direction = 0;
341 // if controls are currently deactivated, we take care of standing up ourselves
347 // calculate movement for this frame
348 movement = physic.get_movement(elapsed_time);
350 if(grabbed_object != NULL && !dying) {
351 Vector pos = get_pos() +
352 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
353 grabbed_object->grab(*this, pos, dir);
356 if(grabbed_object != NULL && dying){
357 grabbed_object->ungrab(*this, dir);
358 grabbed_object = NULL;
361 if(!ice_this_frame && on_ground())
364 on_ground_flag = false;
365 ice_this_frame = false;
367 // when invincible, spawn particles
368 if (invincible_timer.started() && !dying)
370 if (systemRandom.rand(0, 2) == 0) {
371 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
372 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
373 Vector ppos = Vector(px, py);
374 Vector pspeed = Vector(0, 0);
375 Vector paccel = Vector(0, 0);
376 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
377 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
378 // make every other a longer sparkle to make trail a bit fuzzy
379 if (size_t(game_time*20)%2) {
380 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
382 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
385 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
391 if (sprite->animation_done()) growing = false;
399 return on_ground_flag;
405 if(player_status->bonus == NO_BONUS)
412 Player::apply_friction()
414 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
415 physic.set_velocity_x(0);
416 physic.set_acceleration_x(0);
418 float friction = on_ice ? (WALK_ACCELERATION_X * ICE_FRICTION_MULTIPLIER) :
419 (WALK_ACCELERATION_X * NORMAL_FRICTION_MULTIPLIER);
420 if(physic.get_velocity_x() < 0) {
421 physic.set_acceleration_x(friction);
422 } else /*if(physic.get_velocity_x() > 0)*/ {
423 physic.set_acceleration_x(-friction);
429 Player::handle_horizontal_input()
431 float vx = physic.get_velocity_x();
432 float vy = physic.get_velocity_y();
433 float ax = physic.get_acceleration_x();
434 float ay = physic.get_acceleration_y();
437 if(!duck || physic.get_velocity_y() != 0) {
438 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
442 } else if(!controller->hold(Controller::LEFT)
443 && controller->hold(Controller::RIGHT)) {
450 // do not run if action key is pressed or we're holding something
451 // so tux can only walk while shooting
452 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
453 ax = dirsign * WALK_ACCELERATION_X;
455 if(vx >= MAX_WALK_XM && dirsign > 0) {
458 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
463 if( vx * dirsign < MAX_WALK_XM ) {
464 ax = dirsign * WALK_ACCELERATION_X;
466 ax = dirsign * RUN_ACCELERATION_X;
469 if(vx >= MAX_RUN_XM && dirsign > 0) {
472 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
478 // we can reach WALK_SPEED without any acceleration
479 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
480 vx = dirsign * WALK_SPEED;
484 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
485 vx = dirsign * speedlimit;
489 // changing directions?
490 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
492 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
493 skidding_timer.start(SKID_TIME);
494 sound_manager->play("sounds/skid.wav");
495 // dust some particles
496 Sector::current()->add_object(
498 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
499 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
500 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
510 ax *= ICE_ACCELERATION_MULTIPLIER;
513 physic.set_velocity(vx, vy);
514 physic.set_acceleration(ax, ay);
516 // we get slower when not pressing any keys
538 if (physic.get_velocity_y() != 0)
545 if (adjust_height(31.8f)) {
548 unduck_hurt_timer.stop();
555 Player::do_standup() {
563 if (adjust_height(63.8f)) {
565 unduck_hurt_timer.stop();
567 // if timer is not already running, start it.
568 if (unduck_hurt_timer.get_period() == 0) {
569 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
571 else if (unduck_hurt_timer.check()) {
579 Player::do_backflip() {
585 backflip_direction = (dir == LEFT)?(+1):(-1);
588 sound_manager->play("sounds/flip.wav");
589 backflip_timer.start(0.15f);
593 Player::do_jump(float yspeed) {
597 physic.set_velocity_y(yspeed);
598 //bbox.move(Vector(0, -1));
600 on_ground_flag = false;
605 sound_manager->play("sounds/bigjump.wav");
607 sound_manager->play("sounds/jump.wav");
612 Player::early_jump_apex() {
613 if(jump_early_apex) {
616 jump_early_apex = true;
617 physic.set_gravity(physic.get_gravity() * JUMP_EARLY_APEX_FACTOR);
621 Player::do_jump_apex() {
622 if(!jump_early_apex) {
625 jump_early_apex = false;
626 physic.set_gravity(physic.get_gravity() / JUMP_EARLY_APEX_FACTOR);
630 Player::handle_vertical_input()
633 if(controller->pressed(Controller::JUMP)) jump_button_timer.start(JUMP_GRACE_TIME);
634 if(controller->hold(Controller::JUMP) && jump_button_timer.started() && can_jump) {
635 jump_button_timer.stop();
637 // when running, only jump a little bit; else do a backflip
638 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
640 // jump a bit higher if we are running; else do a normal jump
641 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
644 // Let go of jump key
645 else if(!controller->hold(Controller::JUMP)) {
646 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
652 if(jump_early_apex && physic.get_velocity_y() >= 0) {
656 /* In case the player has pressed Down while in a certain range of air,
657 enable butt jump action */
658 if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
659 wants_buttjump = true;
660 if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
663 /* When Down is not held anymore, disable butt jump */
664 if(!controller->hold(Controller::DOWN)) {
665 wants_buttjump = false;
666 does_buttjump = false;
670 physic.set_acceleration_y(0);
673 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
674 physic.set_acceleration_y(-2000);
675 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
681 Player::handle_input()
684 handle_input_ghost();
688 handle_input_climbing();
693 if( controller->released( Controller::PEEK_LEFT ) ) {
696 if( controller->released( Controller::PEEK_RIGHT ) ) {
699 if( controller->released( Controller::PEEK_UP ) ) {
702 if( controller->released( Controller::PEEK_DOWN ) ) {
705 if( controller->pressed( Controller::PEEK_LEFT ) ) {
708 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
711 if(!backflipping && !jumping && on_ground()) {
712 if( controller->pressed( Controller::PEEK_UP ) ) {
714 } else if( controller->pressed( Controller::PEEK_DOWN ) ) {
719 /* Handle horizontal movement: */
720 if (!backflipping) handle_horizontal_input();
726 /* Handle vertical movement: */
727 handle_vertical_input();
730 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
731 if(Sector::current()->add_bullet(
732 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
733 : Vector(32, bbox.get_height()/2)),
734 physic.get_velocity_x(), dir))
735 shooting_timer.start(SHOOTING_TIME);
738 /* Duck or Standup! */
739 if (controller->hold(Controller::DOWN)) {
748 if(!controller->hold(Controller::ACTION) && grabbed_object) {
749 // move the grabbed object a bit away from tux
750 Vector pos = get_pos() +
751 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
752 bbox.get_height()*0.66666 - 32);
753 Rect dest(pos, pos + Vector(32, 32));
754 if(Sector::current()->is_free_of_movingstatics(dest)) {
755 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
757 moving_object->set_pos(pos);
759 log_debug << "Non MovingObject grabbed?!?" << std::endl;
761 if(controller->hold(Controller::UP)) {
762 grabbed_object->ungrab(*this, UP);
764 grabbed_object->ungrab(*this, dir);
766 grabbed_object = NULL;
774 if(controller->hold(Controller::ACTION) && !grabbed_object
776 Sector* sector = Sector::current();
779 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
781 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
784 for(Sector::Portables::iterator i = sector->portables.begin();
785 i != sector->portables.end(); ++i) {
786 Portable* portable = *i;
787 if(!portable->is_portable())
790 // make sure the Portable is a MovingObject
791 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
792 assert(moving_object);
793 if(moving_object == NULL)
796 // make sure the Portable isn't currently non-solid
797 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
799 // check if we are within reach
800 if(moving_object->get_bbox().contains(pos)) {
801 if (climbing) stop_climbing(*climbing);
802 grabbed_object = portable;
803 grabbed_object->grab(*this, get_pos(), dir);
811 Player::handle_input_ghost()
815 if (controller->hold(Controller::LEFT)) {
817 vx -= MAX_RUN_XM * 2;
819 if (controller->hold(Controller::RIGHT)) {
821 vx += MAX_RUN_XM * 2;
823 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
824 vy -= MAX_RUN_XM * 2;
826 if (controller->hold(Controller::DOWN)) {
827 vy += MAX_RUN_XM * 2;
829 if (controller->hold(Controller::ACTION)) {
830 set_ghost_mode(false);
832 physic.set_velocity(vx, vy);
833 physic.set_acceleration(0, 0);
837 Player::add_coins(int count)
839 player_status->add_coins(count);
845 return player_status->coins;
849 Player::add_bonus(const std::string& bonustype)
851 BonusType type = NO_BONUS;
853 if(bonustype == "grow") {
855 } else if(bonustype == "fireflower") {
857 } else if(bonustype == "iceflower") {
859 } else if(bonustype == "none") {
862 std::ostringstream msg;
863 msg << "Unknown bonus type " << bonustype;
864 throw std::runtime_error(msg.str());
867 return add_bonus(type);
871 Player::add_bonus(BonusType type, bool animate)
873 // always ignore NO_BONUS
874 if (type == NO_BONUS) {
878 // ignore GROWUP_BONUS if we're already big
879 if (type == GROWUP_BONUS) {
880 if (player_status->bonus == GROWUP_BONUS)
882 if (player_status->bonus == FIRE_BONUS)
884 if (player_status->bonus == ICE_BONUS)
888 return set_bonus(type, animate);
892 Player::set_bonus(BonusType type, bool animate)
894 if(player_status->bonus == NO_BONUS) {
895 if (!adjust_height(62.8f)) {
896 printf("can't adjust\n");
901 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
903 if (climbing) stop_climbing(*climbing);
906 if (type == NO_BONUS) {
907 if (does_buttjump) does_buttjump = false;
910 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
911 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
912 // visually lose helmet
913 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
914 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
915 Vector paccel = Vector(0, 1000);
916 std::string action = (dir==LEFT)?"left":"right";
917 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
918 if (climbing) stop_climbing(*climbing);
920 if ((player_status->bonus == ICE_BONUS) && (animate)) {
922 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
923 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
924 Vector paccel = Vector(0, 1000);
925 std::string action = (dir==LEFT)?"left":"right";
926 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
927 if (climbing) stop_climbing(*climbing);
929 player_status->max_fire_bullets = 0;
930 player_status->max_ice_bullets = 0;
932 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
933 if (type == ICE_BONUS) player_status->max_ice_bullets++;
935 player_status->bonus = type;
940 Player::set_visible(bool visible)
942 this->visible = visible;
944 set_group(COLGROUP_MOVING);
946 set_group(COLGROUP_DISABLED);
950 Player::get_visible()
958 kick_timer.start(KICK_TIME);
962 Player::draw(DrawingContext& context)
967 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
968 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
969 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
970 float py = Sector::current()->camera->get_translation().y;
971 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
972 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
975 std::string sa_prefix = "";
976 std::string sa_postfix = "";
978 if (player_status->bonus == GROWUP_BONUS)
980 else if (player_status->bonus == FIRE_BONUS)
982 else if (player_status->bonus == ICE_BONUS)
988 sa_postfix = "-left";
990 sa_postfix = "-right";
992 /* Set Tux sprite action */
994 sprite->set_action("gameover");
997 sprite->set_action_continued("grow"+sa_postfix);
998 // while growing, do not change action
999 // do_duck() will take care of cancelling growing manually
1000 // update() will take care of cancelling when growing completed
1002 else if (climbing) {
1003 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1005 else if (backflipping) {
1006 sprite->set_action(sa_prefix+"-backflip"+sa_postfix);
1008 else if (duck && is_big()) {
1009 sprite->set_action(sa_prefix+"-duck"+sa_postfix);
1011 else if (skidding_timer.started() && !skidding_timer.check()) {
1012 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1014 else if (kick_timer.started() && !kick_timer.check()) {
1015 sprite->set_action(sa_prefix+"-kick"+sa_postfix);
1017 else if ((wants_buttjump || does_buttjump) && is_big()) {
1018 sprite->set_action(sa_prefix+"-buttjump"+sa_postfix);
1020 else if (!on_ground()) {
1021 sprite->set_action(sa_prefix+"-jump"+sa_postfix);
1024 if (fabsf(physic.get_velocity_x()) < 1.0f) {
1025 // Determine which idle stage we're at
1026 if (sprite->get_action().find("-stand-") == std::string::npos && sprite->get_action().find("-idle-") == std::string::npos) {
1028 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1030 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1032 else if (idle_timer.check() || (IDLE_TIME[idle_stage] == 0 && sprite->animation_done())) {
1034 if (idle_stage >= IDLE_STAGE_COUNT)
1037 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1039 if (IDLE_TIME[idle_stage] == 0)
1040 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix, 1);
1042 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1045 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1049 sprite->set_action(sa_prefix+"-walk"+sa_postfix);
1055 // Tux is holding something
1056 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1057 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
1065 if (safe_timer.started() && size_t(game_time*40)%2)
1068 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1074 Player::collision_tile(uint32_t tile_attributes)
1076 if(tile_attributes & Tile::HURTS)
1081 if( tile_attributes & Tile::WATER ){
1087 if( tile_attributes & Tile::WATER ){
1090 sound_manager->play( "sounds/splash.ogg" );
1095 if(tile_attributes & (Tile::ICE | Tile::SOLID)) {
1096 ice_this_frame = true;
1102 Player::collision_solid(const CollisionHit& hit)
1105 if(physic.get_velocity_y() > 0)
1106 physic.set_velocity_y(0);
1108 on_ground_flag = true;
1109 floor_normal = hit.slope_normal;
1112 if (does_buttjump) {
1113 does_buttjump = false;
1114 physic.set_velocity_y(-300);
1115 on_ground_flag = false;
1116 Sector::current()->add_object(new Particles(
1117 Vector(get_bbox().p2.x, get_bbox().p2.y),
1119 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1121 Sector::current()->add_object(new Particles(
1122 Vector(get_bbox().p1.x, get_bbox().p2.y),
1124 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1128 } else if(hit.top) {
1129 if(physic.get_velocity_y() < 0)
1130 physic.set_velocity_y(.2f);
1133 if(hit.left || hit.right) {
1134 physic.set_velocity_x(0);
1139 if(hit.left || hit.right) {
1141 } else if(hit.top || hit.bottom) {
1148 Player::collision(GameObject& other, const CollisionHit& hit)
1150 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1155 if(hit.left || hit.right) {
1156 try_grab(); //grab objects right now, in update it will be too late
1159 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1161 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1162 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1163 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1165 if(controller->pressed(Controller::UP))
1166 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1172 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1173 if(badguy != NULL) {
1174 if(safe_timer.started() || invincible_timer.started())
1184 Player::make_invincible()
1186 sound_manager->play("sounds/invincible_start.ogg");
1187 invincible_timer.start(TUX_INVINCIBLE_TIME);
1188 Sector::current()->play_music(HERRING_MUSIC);
1193 Player::kill(bool completely)
1195 if(dying || deactivated)
1198 if(!completely && (safe_timer.started() || invincible_timer.started()))
1203 sound_manager->play("sounds/hurt.wav");
1205 if (climbing) stop_climbing(*climbing);
1207 physic.set_velocity_x(0);
1209 if(!completely && is_big()) {
1210 if(player_status->bonus == FIRE_BONUS
1211 || player_status->bonus == ICE_BONUS) {
1212 safe_timer.start(TUX_SAFE_TIME);
1213 set_bonus(GROWUP_BONUS, true);
1214 } else if(player_status->bonus == GROWUP_BONUS) {
1215 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1216 adjust_height(30.8f);
1218 backflipping = false;
1219 set_bonus(NO_BONUS, true);
1220 } else if(player_status->bonus == NO_BONUS) {
1221 safe_timer.start(TUX_SAFE_TIME);
1222 adjust_height(30.8f);
1227 // do not die when in edit mode
1229 set_ghost_mode(true);
1233 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1235 for (int i = 0; i < 5; i++)
1237 // the numbers: starting x, starting y, velocity y
1238 Sector::current()->add_object(new FallingCoin(get_pos() +
1239 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1240 systemRandom.rand(-100,100)));
1242 player_status->coins -= std::max(player_status->coins/10, 25);
1246 GameSession::current()->set_reset_point("", Vector());
1248 physic.enable_gravity(true);
1249 physic.set_acceleration(0, 0);
1250 physic.set_velocity(0, -700);
1251 set_bonus(NO_BONUS, true);
1253 dying_timer.start(3.0);
1254 set_group(COLGROUP_DISABLED);
1256 DisplayEffect* effect = new DisplayEffect();
1257 effect->fade_out(3.0);
1258 Sector::current()->add_object(effect);
1259 sound_manager->stop_music(3.0);
1264 Player::move(const Vector& vector)
1268 // TODO: do we need the following? Seems irrelevant to moving the player
1270 set_size(31.8f, 63.8f);
1272 set_size(31.8f, 31.8f);
1274 last_ground_y = vector.y;
1275 if (climbing) stop_climbing(*climbing);
1281 Player::check_bounds(Camera* camera)
1283 /* Keep tux in bounds: */
1284 if (get_pos().x < 0) {
1285 // Lock Tux to the size of the level, so that he doesn't fall of
1287 set_pos(Vector(0, get_pos().y));
1290 /* fallen out of the level? */
1291 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1296 // can happen if back scrolling is disabled
1297 if(get_pos().x < camera->get_translation().x) {
1298 set_pos(Vector(camera->get_translation().x, get_pos().y));
1300 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1303 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1309 Player::add_velocity(const Vector& velocity)
1311 physic.set_velocity(physic.get_velocity() + velocity);
1315 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1317 if (end_speed.x > 0)
1318 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1319 if (end_speed.x < 0)
1320 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1321 if (end_speed.y > 0)
1322 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1323 if (end_speed.y < 0)
1324 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1328 Player::get_velocity()
1330 return physic.get_velocity();
1334 Player::bounce(BadGuy& )
1336 if(controller->hold(Controller::JUMP))
1337 physic.set_velocity_y(-520);
1339 physic.set_velocity_y(-300);
1342 //Scripting Functions Below
1345 Player::deactivate()
1350 physic.set_velocity_x(0);
1351 physic.set_velocity_y(0);
1352 physic.set_acceleration_x(0);
1353 physic.set_acceleration_y(0);
1354 if (climbing) stop_climbing(*climbing);
1362 deactivated = false;
1365 void Player::walk(float speed)
1367 physic.set_velocity_x(speed);
1371 Player::set_ghost_mode(bool enable)
1373 if (ghost_mode == enable)
1376 if (climbing) stop_climbing(*climbing);
1380 set_group(COLGROUP_DISABLED);
1381 physic.enable_gravity(false);
1382 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1385 set_group(COLGROUP_MOVING);
1386 physic.enable_gravity(true);
1387 log_debug << "You feel solid again." << std::endl;
1393 Player::set_edit_mode(bool enable)
1399 Player::start_climbing(Climbable& climbable)
1401 if (climbing == &climbable) return;
1403 climbing = &climbable;
1404 physic.enable_gravity(false);
1405 physic.set_velocity(0, 0);
1406 physic.set_acceleration(0, 0);
1410 Player::stop_climbing(Climbable& /*climbable*/)
1412 if (!climbing) return;
1416 if (grabbed_object) {
1417 grabbed_object->ungrab(*this, dir);
1418 grabbed_object = NULL;
1421 physic.enable_gravity(true);
1422 physic.set_velocity(0, 0);
1423 physic.set_acceleration(0, 0);
1425 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1426 on_ground_flag = true;
1427 // TODO: This won't help. Why?
1433 Player::handle_input_climbing()
1436 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1442 if (controller->hold(Controller::LEFT)) {
1446 if (controller->hold(Controller::RIGHT)) {
1450 if (controller->hold(Controller::UP)) {
1453 if (controller->hold(Controller::DOWN)) {
1456 if (controller->hold(Controller::JUMP)) {
1458 stop_climbing(*climbing);
1464 if (controller->hold(Controller::ACTION)) {
1465 stop_climbing(*climbing);
1468 physic.set_velocity(vx, vy);
1469 physic.set_acceleration(0, 0);