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.
23 #include "audio/sound_manager.hpp"
24 #include "badguy/badguy.hpp"
25 #include "control/codecontroller.hpp"
26 #include "control/joystickkeyboardcontroller.hpp"
27 #include "display_effect.hpp"
29 #include "falling_coin.hpp"
30 #include "game_session.hpp"
31 #include "gettext.hpp"
33 #include "object/bullet.hpp"
34 #include "object/camera.hpp"
35 #include "object/portable.hpp"
36 #include "object/sprite_particle.hpp"
37 #include "object/tilemap.hpp"
38 #include "particles.hpp"
39 #include "player_status.hpp"
40 #include "random_generator.hpp"
42 #include "scripting/squirrel_util.hpp"
43 #include "sprite/sprite.hpp"
44 #include "sprite/sprite_manager.hpp"
46 #include "trigger/climbable.hpp"
56 static const int TILES_FOR_BUTTJUMP = 3;
57 static const float BUTTJUMP_MIN_VELOCITY_Y = 400.0f;
58 static const float SHOOTING_TIME = .150f;
60 /** number of idle stages, including standing */
61 static const unsigned int IDLE_STAGE_COUNT = 5;
63 * how long to play each idle animation in milliseconds
64 * '0' means the sprite action is played once before moving onto the next
67 static const int IDLE_TIME[] = { 5000, 0, 2500, 0, 2500 };
69 static const std::string IDLE_STAGES[] =
76 /** acceleration in horizontal direction when walking
77 * (all accelerations are in pixel/s^2) */
78 static const float WALK_ACCELERATION_X = 300;
79 /** acceleration in horizontal direction when running */
80 static const float RUN_ACCELERATION_X = 400;
81 /** acceleration when skidding */
82 static const float SKID_XM = 200;
83 /** time of skidding in seconds */
84 static const float SKID_TIME = .3f;
85 /** maximum walk velocity (pixel/s) */
86 static const float MAX_WALK_XM = 230;
87 /** maximum run velocity (pixel/s) */
88 static const float MAX_RUN_XM = 320;
89 /** maximum horizontal climb velocity */
90 static const float MAX_CLIMB_XM = 48;
91 /** maximum vertical climb velocity */
92 static const float MAX_CLIMB_YM = 128;
93 /** instant velocity when tux starts to walk */
94 static const float WALK_SPEED = 100;
96 /** multiplied by WALK_ACCELERATION to give friction */
97 static const float NORMAL_FRICTION_MULTIPLIER = 1.5f;
98 /** multiplied by WALK_ACCELERATION to give friction */
99 static const float ICE_FRICTION_MULTIPLIER = 0.1f;
100 static const float ICE_ACCELERATION_MULTIPLIER = 0.25f;
102 /** time of the kick (kicking mriceblock) animation */
103 static const float KICK_TIME = .3f;
104 /** time of tux cheering (currently unused) */
105 static const float CHEER_TIME = 1.0f;
107 /** if Tux cannot unduck for this long, he will get hurt */
108 static const float UNDUCK_HURT_TIME = 0.25f;
109 /** gravity is higher after the jump key is released before
110 the apex of the jump is reached */
111 static const float JUMP_EARLY_APEX_FACTOR = 3.0;
113 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) */
115 bool no_water = true;
118 Player::Player(PlayerStatus* _player_status, const std::string& name)
119 : scripting_controller(0),
120 player_status(_player_status),
121 scripting_controller_old(0),
122 grabbed_object(NULL), ghost_mode(false), edit_mode(false), idle_stage(0),
126 controller = main_controller;
127 scripting_controller = new CodeController();
128 sprite = sprite_manager->create("images/creatures/tux/tux.sprite");
129 airarrow.reset(new Surface("images/engine/hud/airarrow.png"));
130 idle_timer.start(IDLE_TIME[0]/1000.0f);
132 sound_manager->preload("sounds/bigjump.wav");
133 sound_manager->preload("sounds/jump.wav");
134 sound_manager->preload("sounds/hurt.wav");
135 sound_manager->preload("sounds/skid.wav");
136 sound_manager->preload("sounds/flip.wav");
137 sound_manager->preload("sounds/invincible_start.ogg");
138 sound_manager->preload("sounds/splash.ogg");
145 if (climbing) stop_climbing(*climbing);
147 delete scripting_controller;
154 set_size(31.8f, 62.8f);
156 set_size(31.8f, 30.8f);
167 fall_mode = ON_GROUND;
169 jump_early_apex = false;
171 wants_buttjump = false;
172 does_buttjump = false;
175 backflipping = false;
176 backflip_direction = 0;
180 ice_this_frame = false;
181 speedlimit = 0; //no special limit
183 on_ground_flag = false;
184 grabbed_object = NULL;
192 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
197 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
201 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
206 Scripting::unexpose_object(vm, table_idx, name);
210 Player::get_speedlimit()
216 Player::set_speedlimit(float newlimit)
222 Player::set_controller(Controller* controller)
224 this->controller = controller;
228 Player::use_scripting_controller(bool use_or_release)
230 if ((use_or_release == true) && (controller != scripting_controller)) {
231 scripting_controller_old = get_controller();
232 set_controller(scripting_controller);
234 if ((use_or_release == false) && (controller == scripting_controller)) {
235 set_controller(scripting_controller_old);
236 scripting_controller_old = 0;
241 Player::do_scripting_controller(std::string control, bool pressed)
243 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
244 if(control == std::string(Controller::controlNames[i])) {
245 scripting_controller->press(Controller::Control(i), pressed);
251 Player::adjust_height(float new_height)
254 bbox2.move(Vector(0, bbox.get_height() - new_height));
255 bbox2.set_height(new_height);
257 if(new_height > bbox.get_height()) {
258 Rect additional_space = bbox2;
259 additional_space.set_height(new_height - bbox.get_height());
260 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
264 // adjust bbox accordingly
265 // note that we use members of moving_object for this, so we can run this during CD, too
267 set_size(bbox2.get_width(), bbox2.get_height());
272 Player::trigger_sequence(std::string sequence_name)
274 if (climbing) stop_climbing(*climbing);
275 GameSession::current()->start_sequence(sequence_name);
279 Player::update(float elapsed_time)
286 if(dying && dying_timer.check()) {
291 if(!dying && !deactivated)
294 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
298 // extend/shrink tux collision rectangle so that we fall through/walk over 1
300 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
306 // on downward slopes, adjust vertical velocity so tux walks smoothly down
308 if(floor_normal.y != 0) {
309 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
310 physic.set_velocity_y(250);
315 // handle backflipping
317 //prevent player from changing direction when backflipping
318 dir = (backflip_direction == 1) ? LEFT : RIGHT;
319 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
324 fall_mode = ON_GROUND;
325 last_ground_y = get_pos().y;
327 if(get_pos().y > last_ground_y)
329 else if(fall_mode == ON_GROUND)
333 // check if we landed
336 if (backflipping && (!backflip_timer.started())) {
337 backflipping = false;
338 backflip_direction = 0;
340 // if controls are currently deactivated, we take care of standing up ourselves
346 // calculate movement for this frame
347 movement = physic.get_movement(elapsed_time);
349 if(grabbed_object != NULL && !dying) {
350 Vector pos = get_pos() +
351 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
352 grabbed_object->grab(*this, pos, dir);
355 if(grabbed_object != NULL && dying){
356 grabbed_object->ungrab(*this, dir);
357 grabbed_object = NULL;
360 if(!ice_this_frame && on_ground())
363 on_ground_flag = false;
364 ice_this_frame = false;
366 // when invincible, spawn particles
367 if (invincible_timer.started() && !dying)
369 if (systemRandom.rand(0, 2) == 0) {
370 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
371 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
372 Vector ppos = Vector(px, py);
373 Vector pspeed = Vector(0, 0);
374 Vector paccel = Vector(0, 0);
375 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite",
376 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
377 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) ?
378 // make every other a longer sparkle to make trail a bit fuzzy
379 (size_t(game_time*20)%2) ? "small" : "medium"
381 "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
386 if (sprite->animation_done()) growing = false;
394 return on_ground_flag;
400 if(player_status->bonus == NO_BONUS)
407 Player::apply_friction()
409 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
410 physic.set_velocity_x(0);
411 physic.set_acceleration_x(0);
413 float friction = WALK_ACCELERATION_X * (on_ice ? ICE_FRICTION_MULTIPLIER : NORMAL_FRICTION_MULTIPLIER);
414 if(physic.get_velocity_x() < 0) {
415 physic.set_acceleration_x(friction);
416 } else if(physic.get_velocity_x() > 0) {
417 physic.set_acceleration_x(-friction);
418 } // no friction for physic.get_velocity_x() == 0
423 Player::handle_horizontal_input()
425 float vx = physic.get_velocity_x();
426 float vy = physic.get_velocity_y();
427 float ax = physic.get_acceleration_x();
428 float ay = physic.get_acceleration_y();
431 if(!duck || physic.get_velocity_y() != 0) {
432 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
436 } else if(!controller->hold(Controller::LEFT)
437 && controller->hold(Controller::RIGHT)) {
444 // do not run if action key is pressed or we're holding something
445 // so tux can only walk while shooting
446 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
447 ax = dirsign * WALK_ACCELERATION_X;
449 if(vx >= MAX_WALK_XM && dirsign > 0) {
452 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
457 if( vx * dirsign < MAX_WALK_XM ) {
458 ax = dirsign * WALK_ACCELERATION_X;
460 ax = dirsign * RUN_ACCELERATION_X;
463 if(vx >= MAX_RUN_XM && dirsign > 0) {
466 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
472 // we can reach WALK_SPEED without any acceleration
473 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
474 vx = dirsign * WALK_SPEED;
478 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
479 vx = dirsign * speedlimit;
483 // changing directions?
484 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
486 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
487 skidding_timer.start(SKID_TIME);
488 sound_manager->play("sounds/skid.wav");
489 // dust some particles
490 Sector::current()->add_object(
492 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
493 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
494 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
504 ax *= ICE_ACCELERATION_MULTIPLIER;
507 physic.set_velocity(vx, vy);
508 physic.set_acceleration(ax, ay);
510 // we get slower when not pressing any keys
532 if (physic.get_velocity_y() != 0)
539 if (adjust_height(31.8f)) {
542 unduck_hurt_timer.stop();
549 Player::do_standup() {
557 if (adjust_height(63.8f)) {
559 unduck_hurt_timer.stop();
561 // if timer is not already running, start it.
562 if (unduck_hurt_timer.get_period() == 0) {
563 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
565 else if (unduck_hurt_timer.check()) {
573 Player::do_backflip() {
579 backflip_direction = (dir == LEFT)?(+1):(-1);
582 sound_manager->play("sounds/flip.wav");
583 backflip_timer.start(0.15f);
587 Player::do_jump(float yspeed) {
591 physic.set_velocity_y(yspeed);
592 //bbox.move(Vector(0, -1));
594 on_ground_flag = false;
599 sound_manager->play("sounds/bigjump.wav");
601 sound_manager->play("sounds/jump.wav");
606 Player::early_jump_apex() {
607 if(jump_early_apex) {
610 jump_early_apex = true;
611 physic.set_gravity(physic.get_gravity() * JUMP_EARLY_APEX_FACTOR);
615 Player::do_jump_apex() {
616 if(!jump_early_apex) {
619 jump_early_apex = false;
620 physic.set_gravity(physic.get_gravity() / JUMP_EARLY_APEX_FACTOR);
624 Player::handle_vertical_input()
627 if(controller->pressed(Controller::JUMP)) jump_button_timer.start(JUMP_GRACE_TIME);
628 if(controller->hold(Controller::JUMP) && jump_button_timer.started() && can_jump) {
629 jump_button_timer.stop();
631 // when running, only jump a little bit; else do a backflip
632 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
634 // jump a bit higher if we are running; else do a normal jump
635 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
638 // Let go of jump key
639 else if(!controller->hold(Controller::JUMP)) {
640 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
646 if(jump_early_apex && physic.get_velocity_y() >= 0) {
650 /* In case the player has pressed Down while in a certain range of air,
651 enable butt jump action */
652 if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
653 wants_buttjump = true;
654 if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
657 /* When Down is not held anymore, disable butt jump */
658 if(!controller->hold(Controller::DOWN)) {
659 wants_buttjump = false;
660 does_buttjump = false;
664 physic.set_acceleration_y(0);
667 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
668 physic.set_acceleration_y(-2000);
669 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
675 Player::handle_input()
678 handle_input_ghost();
682 handle_input_climbing();
687 if( controller->released( Controller::PEEK_LEFT ) || controller->released( Controller::PEEK_RIGHT ) ) {
690 if( controller->released( Controller::PEEK_UP ) || controller->released( Controller::PEEK_DOWN ) ) {
693 if( controller->pressed( Controller::PEEK_LEFT ) ) {
696 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
699 if(!backflipping && !jumping && on_ground()) {
700 if( controller->pressed( Controller::PEEK_UP ) ) {
702 } else if( controller->pressed( Controller::PEEK_DOWN ) ) {
707 /* Handle horizontal movement: */
708 if (!backflipping) handle_horizontal_input();
714 /* Handle vertical movement: */
715 handle_vertical_input();
718 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
719 if(Sector::current()->add_bullet(
720 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
721 : Vector(32, bbox.get_height()/2)),
722 physic.get_velocity_x(), dir))
723 shooting_timer.start(SHOOTING_TIME);
726 /* Duck or Standup! */
727 if (controller->hold(Controller::DOWN)) {
736 if(!controller->hold(Controller::ACTION) && grabbed_object) {
737 // move the grabbed object a bit away from tux
738 Vector pos = get_pos() +
739 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
740 bbox.get_height()*0.66666 - 32);
741 Rect dest(pos, pos + Vector(32, 32));
742 if(Sector::current()->is_free_of_movingstatics(dest)) {
743 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
745 moving_object->set_pos(pos);
747 log_debug << "Non MovingObject grabbed?!?" << std::endl;
749 if(controller->hold(Controller::UP)) {
750 grabbed_object->ungrab(*this, UP);
752 grabbed_object->ungrab(*this, dir);
754 grabbed_object = NULL;
762 if(controller->hold(Controller::ACTION) && !grabbed_object
764 Sector* sector = Sector::current();
767 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
769 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
772 for(Sector::Portables::iterator i = sector->portables.begin();
773 i != sector->portables.end(); ++i) {
774 Portable* portable = *i;
775 if(!portable->is_portable())
778 // make sure the Portable is a MovingObject
779 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
780 assert(moving_object);
781 if(moving_object == NULL)
784 // make sure the Portable isn't currently non-solid
785 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
787 // check if we are within reach
788 if(moving_object->get_bbox().contains(pos)) {
789 if (climbing) stop_climbing(*climbing);
790 grabbed_object = portable;
791 grabbed_object->grab(*this, get_pos(), dir);
799 Player::handle_input_ghost()
803 if (controller->hold(Controller::LEFT)) {
805 vx -= MAX_RUN_XM * 2;
807 if (controller->hold(Controller::RIGHT)) {
809 vx += MAX_RUN_XM * 2;
811 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
812 vy -= MAX_RUN_XM * 2;
814 if (controller->hold(Controller::DOWN)) {
815 vy += MAX_RUN_XM * 2;
817 if (controller->hold(Controller::ACTION)) {
818 set_ghost_mode(false);
820 physic.set_velocity(vx, vy);
821 physic.set_acceleration(0, 0);
825 Player::add_coins(int count)
827 player_status->add_coins(count);
833 return player_status->coins;
837 Player::add_bonus(const std::string& bonustype)
839 BonusType type = NO_BONUS;
841 if(bonustype == "grow") {
843 } else if(bonustype == "fireflower") {
845 } else if(bonustype == "iceflower") {
847 } else if(bonustype == "none") {
850 std::ostringstream msg;
851 msg << "Unknown bonus type " << bonustype;
852 throw std::runtime_error(msg.str());
855 return add_bonus(type);
859 Player::add_bonus(BonusType type, bool animate)
861 // always ignore NO_BONUS
862 if (type == NO_BONUS) {
866 // ignore GROWUP_BONUS if we're already big
867 if (type == GROWUP_BONUS) {
868 if (player_status->bonus == GROWUP_BONUS)
870 if (player_status->bonus == FIRE_BONUS)
872 if (player_status->bonus == ICE_BONUS)
876 return set_bonus(type, animate);
880 Player::set_bonus(BonusType type, bool animate)
882 if(player_status->bonus == NO_BONUS) {
883 if (!adjust_height(62.8f)) {
884 printf("can't adjust\n");
889 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
891 if (climbing) stop_climbing(*climbing);
894 if (type == NO_BONUS) {
895 if (does_buttjump) does_buttjump = false;
898 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
899 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
900 // visually lose helmet
901 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
902 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
903 Vector paccel = Vector(0, 1000);
904 std::string action = (dir==LEFT)?"left":"right";
905 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
906 if (climbing) stop_climbing(*climbing);
908 if ((player_status->bonus == ICE_BONUS) && (animate)) {
910 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
911 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
912 Vector paccel = Vector(0, 1000);
913 std::string action = (dir==LEFT)?"left":"right";
914 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
915 if (climbing) stop_climbing(*climbing);
917 player_status->max_fire_bullets = 0;
918 player_status->max_ice_bullets = 0;
920 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
921 if (type == ICE_BONUS) player_status->max_ice_bullets++;
923 player_status->bonus = type;
928 Player::set_visible(bool visible)
930 this->visible = visible;
932 set_group(COLGROUP_MOVING);
934 set_group(COLGROUP_DISABLED);
938 Player::get_visible()
946 kick_timer.start(KICK_TIME);
950 Player::draw(DrawingContext& context)
955 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
956 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
957 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
958 float py = Sector::current()->camera->get_translation().y;
959 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
960 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
963 std::string sa_prefix = "";
964 std::string sa_postfix = "";
966 if (player_status->bonus == GROWUP_BONUS)
968 else if (player_status->bonus == FIRE_BONUS)
970 else if (player_status->bonus == ICE_BONUS)
976 sa_postfix = "-left";
978 sa_postfix = "-right";
980 /* Set Tux sprite action */
982 sprite->set_action("gameover");
985 sprite->set_action_continued("grow"+sa_postfix);
986 // while growing, do not change action
987 // do_duck() will take care of cancelling growing manually
988 // update() will take care of cancelling when growing completed
991 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
993 else if (backflipping) {
994 sprite->set_action(sa_prefix+"-backflip"+sa_postfix);
996 else if (duck && is_big()) {
997 sprite->set_action(sa_prefix+"-duck"+sa_postfix);
999 else if (skidding_timer.started() && !skidding_timer.check()) {
1000 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1002 else if (kick_timer.started() && !kick_timer.check()) {
1003 sprite->set_action(sa_prefix+"-kick"+sa_postfix);
1005 else if ((wants_buttjump || does_buttjump) && is_big()) {
1006 sprite->set_action(sa_prefix+"-buttjump"+sa_postfix);
1008 else if (!on_ground()) {
1009 sprite->set_action(sa_prefix+"-jump"+sa_postfix);
1012 if (fabsf(physic.get_velocity_x()) < 1.0f) {
1013 // Determine which idle stage we're at
1014 if (sprite->get_action().find("-stand-") == std::string::npos && sprite->get_action().find("-idle-") == std::string::npos) {
1016 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1018 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1020 else if (idle_timer.check() || (IDLE_TIME[idle_stage] == 0 && sprite->animation_done())) {
1022 if (idle_stage >= IDLE_STAGE_COUNT)
1025 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1027 if (IDLE_TIME[idle_stage] == 0)
1028 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix, 1);
1030 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1033 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1037 sprite->set_action(sa_prefix+"-walk"+sa_postfix);
1043 // Tux is holding something
1044 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1045 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
1053 if (safe_timer.started() && size_t(game_time*40)%2)
1056 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1062 Player::collision_tile(uint32_t tile_attributes)
1064 if(tile_attributes & Tile::HURTS)
1069 if( tile_attributes & Tile::WATER ){
1075 if( tile_attributes & Tile::WATER ){
1078 sound_manager->play( "sounds/splash.ogg" );
1083 if(tile_attributes & Tile::ICE) {
1084 ice_this_frame = true;
1090 Player::collision_solid(const CollisionHit& hit)
1093 if(physic.get_velocity_y() > 0)
1094 physic.set_velocity_y(0);
1096 on_ground_flag = true;
1097 floor_normal = hit.slope_normal;
1100 if (does_buttjump) {
1101 does_buttjump = false;
1102 physic.set_velocity_y(-300);
1103 on_ground_flag = false;
1104 Sector::current()->add_object(new Particles(
1105 Vector(get_bbox().p2.x, get_bbox().p2.y),
1107 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1109 Sector::current()->add_object(new Particles(
1110 Vector(get_bbox().p1.x, get_bbox().p2.y),
1112 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1116 } else if(hit.top) {
1117 if(physic.get_velocity_y() < 0)
1118 physic.set_velocity_y(.2f);
1121 if(hit.left || hit.right) {
1122 physic.set_velocity_x(0);
1127 if(hit.left || hit.right) {
1129 } else if(hit.top || hit.bottom) {
1136 Player::collision(GameObject& other, const CollisionHit& hit)
1138 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1143 if(hit.left || hit.right) {
1144 try_grab(); //grab objects right now, in update it will be too late
1147 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1149 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1150 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1151 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1153 if(controller->pressed(Controller::UP))
1154 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1160 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1161 if(badguy != NULL) {
1162 if(safe_timer.started() || invincible_timer.started())
1172 Player::make_invincible()
1174 sound_manager->play("sounds/invincible_start.ogg");
1175 invincible_timer.start(TUX_INVINCIBLE_TIME);
1176 Sector::current()->play_music(HERRING_MUSIC);
1181 Player::kill(bool completely)
1183 if(dying || deactivated)
1186 if(!completely && (safe_timer.started() || invincible_timer.started()))
1191 sound_manager->play("sounds/hurt.wav");
1193 if (climbing) stop_climbing(*climbing);
1195 physic.set_velocity_x(0);
1197 if(!completely && is_big()) {
1198 if(player_status->bonus == FIRE_BONUS
1199 || player_status->bonus == ICE_BONUS) {
1200 safe_timer.start(TUX_SAFE_TIME);
1201 set_bonus(GROWUP_BONUS, true);
1202 } else if(player_status->bonus == GROWUP_BONUS) {
1203 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1204 adjust_height(30.8f);
1206 backflipping = false;
1207 set_bonus(NO_BONUS, true);
1208 } else if(player_status->bonus == NO_BONUS) {
1209 safe_timer.start(TUX_SAFE_TIME);
1210 adjust_height(30.8f);
1215 // do not die when in edit mode
1217 set_ghost_mode(true);
1221 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1223 for (int i = 0; i < 5; i++)
1225 // the numbers: starting x, starting y, velocity y
1226 Sector::current()->add_object(new FallingCoin(get_pos() +
1227 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1228 systemRandom.rand(-100,100)));
1230 player_status->coins -= std::max(player_status->coins/10, 25);
1234 GameSession::current()->set_reset_point("", Vector());
1236 physic.enable_gravity(true);
1237 physic.set_acceleration(0, 0);
1238 physic.set_velocity(0, -700);
1239 set_bonus(NO_BONUS, true);
1241 dying_timer.start(3.0);
1242 set_group(COLGROUP_DISABLED);
1244 Sector::current()->effect->fade_out(3.0);
1245 sound_manager->stop_music(3.0);
1250 Player::move(const Vector& vector)
1254 // TODO: do we need the following? Seems irrelevant to moving the player
1256 set_size(31.8f, 63.8f);
1258 set_size(31.8f, 31.8f);
1260 last_ground_y = vector.y;
1261 if (climbing) stop_climbing(*climbing);
1267 Player::check_bounds(Camera* camera)
1269 /* Keep tux in sector bounds: */
1270 if (get_pos().x < 0) {
1271 // Lock Tux to the size of the level, so that he doesn't fall off
1273 set_pos(Vector(0, get_pos().y));
1276 if (get_bbox().get_right() > Sector::current()->get_width()) {
1277 // Lock Tux to the size of the level, so that he doesn't fall off
1279 set_pos(Vector(Sector::current()->get_width() - get_bbox().get_width(), get_pos().y));
1282 /* fallen out of the level? */
1283 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1288 // can happen if back scrolling is disabled
1289 if(get_pos().x < camera->get_translation().x) {
1290 set_pos(Vector(camera->get_translation().x, get_pos().y));
1292 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1295 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1301 Player::add_velocity(const Vector& velocity)
1303 physic.set_velocity(physic.get_velocity() + velocity);
1307 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1309 if (end_speed.x > 0)
1310 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1311 if (end_speed.x < 0)
1312 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1313 if (end_speed.y > 0)
1314 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1315 if (end_speed.y < 0)
1316 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1320 Player::get_velocity()
1322 return physic.get_velocity();
1326 Player::bounce(BadGuy& )
1328 if(controller->hold(Controller::JUMP))
1329 physic.set_velocity_y(-520);
1331 physic.set_velocity_y(-300);
1334 //Scripting Functions Below
1337 Player::deactivate()
1342 physic.set_velocity_x(0);
1343 physic.set_velocity_y(0);
1344 physic.set_acceleration_x(0);
1345 physic.set_acceleration_y(0);
1346 if (climbing) stop_climbing(*climbing);
1354 deactivated = false;
1357 void Player::walk(float speed)
1359 physic.set_velocity_x(speed);
1363 Player::set_ghost_mode(bool enable)
1365 if (ghost_mode == enable)
1368 if (climbing) stop_climbing(*climbing);
1372 set_group(COLGROUP_DISABLED);
1373 physic.enable_gravity(false);
1374 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1377 set_group(COLGROUP_MOVING);
1378 physic.enable_gravity(true);
1379 log_debug << "You feel solid again." << std::endl;
1385 Player::set_edit_mode(bool enable)
1391 Player::start_climbing(Climbable& climbable)
1393 if (climbing == &climbable) return;
1395 climbing = &climbable;
1396 physic.enable_gravity(false);
1397 physic.set_velocity(0, 0);
1398 physic.set_acceleration(0, 0);
1402 Player::stop_climbing(Climbable& /*climbable*/)
1404 if (!climbing) return;
1408 if (grabbed_object) {
1409 grabbed_object->ungrab(*this, dir);
1410 grabbed_object = NULL;
1413 physic.enable_gravity(true);
1414 physic.set_velocity(0, 0);
1415 physic.set_acceleration(0, 0);
1417 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1418 on_ground_flag = true;
1419 // TODO: This won't help. Why?
1425 Player::handle_input_climbing()
1428 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1434 if (controller->hold(Controller::LEFT)) {
1438 if (controller->hold(Controller::RIGHT)) {
1442 if (controller->hold(Controller::UP)) {
1445 if (controller->hold(Controller::DOWN)) {
1448 if (controller->hold(Controller::JUMP)) {
1450 stop_climbing(*climbing);
1456 if (controller->hold(Controller::ACTION)) {
1457 stop_climbing(*climbing);
1460 physic.set_velocity(vx, vy);
1461 physic.set_acceleration(0, 0);