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 bool no_water = true;
111 Player::Player(PlayerStatus* _player_status, const std::string& name)
112 : scripting_controller(0),
113 player_status(_player_status),
114 scripting_controller_old(0),
115 grabbed_object(NULL), ghost_mode(false), edit_mode(false), idle_stage(0),
119 controller = main_controller;
120 scripting_controller = new CodeController();
121 sprite = sprite_manager->create("images/creatures/tux/tux.sprite");
122 airarrow.reset(new Surface("images/engine/hud/airarrow.png"));
123 idle_timer.start(IDLE_TIME[0]/1000.0f);
125 sound_manager->preload("sounds/bigjump.wav");
126 sound_manager->preload("sounds/jump.wav");
127 sound_manager->preload("sounds/hurt.wav");
128 sound_manager->preload("sounds/skid.wav");
129 sound_manager->preload("sounds/flip.wav");
130 sound_manager->preload("sounds/invincible_start.ogg");
131 sound_manager->preload("sounds/splash.ogg");
138 if (climbing) stop_climbing(*climbing);
140 delete scripting_controller;
147 set_size(31.8f, 62.8f);
149 set_size(31.8f, 30.8f);
160 fall_mode = ON_GROUND;
162 jump_early_apex = false;
164 wants_buttjump = false;
165 does_buttjump = false;
168 backflipping = false;
169 backflip_direction = 0;
172 speedlimit = 0; //no special limit
174 on_ground_flag = false;
175 grabbed_object = NULL;
183 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
188 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
192 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
197 Scripting::unexpose_object(vm, table_idx, name);
201 Player::get_speedlimit()
207 Player::set_speedlimit(float newlimit)
213 Player::set_controller(Controller* controller)
215 this->controller = controller;
219 Player::use_scripting_controller(bool use_or_release)
221 if ((use_or_release == true) && (controller != scripting_controller)) {
222 scripting_controller_old = get_controller();
223 set_controller(scripting_controller);
225 if ((use_or_release == false) && (controller == scripting_controller)) {
226 set_controller(scripting_controller_old);
227 scripting_controller_old = 0;
232 Player::do_scripting_controller(std::string control, bool pressed)
234 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
235 if(control == std::string(Controller::controlNames[i])) {
236 scripting_controller->press(Controller::Control(i), pressed);
242 Player::adjust_height(float new_height)
245 bbox2.move(Vector(0, bbox.get_height() - new_height));
246 bbox2.set_height(new_height);
248 if(new_height > bbox.get_height()) {
249 Rect additional_space = bbox2;
250 additional_space.set_height(new_height - bbox.get_height());
251 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
255 // adjust bbox accordingly
256 // note that we use members of moving_object for this, so we can run this during CD, too
258 set_size(bbox2.get_width(), bbox2.get_height());
263 Player::trigger_sequence(std::string sequence_name)
265 if (climbing) stop_climbing(*climbing);
266 GameSession::current()->start_sequence(sequence_name);
270 Player::update(float elapsed_time)
277 if(dying && dying_timer.check()) {
282 if(!dying && !deactivated)
285 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
289 // extend/shrink tux collision rectangle so that we fall through/walk over 1
291 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
297 // on downward slopes, adjust vertical velocity so tux walks smoothly down
299 if(floor_normal.y != 0) {
300 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
301 physic.set_velocity_y(250);
306 // handle backflipping
308 //prevent player from changing direction when backflipping
309 dir = (backflip_direction == 1) ? LEFT : RIGHT;
310 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
315 fall_mode = ON_GROUND;
316 last_ground_y = get_pos().y;
318 if(get_pos().y > last_ground_y)
320 else if(fall_mode == ON_GROUND)
324 // check if we landed
327 if (backflipping && (!backflip_timer.started())) {
328 backflipping = false;
329 backflip_direction = 0;
331 // if controls are currently deactivated, we take care of standing up ourselves
337 // calculate movement for this frame
338 movement = physic.get_movement(elapsed_time);
340 if(grabbed_object != NULL && !dying) {
341 Vector pos = get_pos() +
342 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
343 grabbed_object->grab(*this, pos, dir);
346 if(grabbed_object != NULL && dying){
347 grabbed_object->ungrab(*this, dir);
348 grabbed_object = NULL;
351 on_ground_flag = false;
353 // when invincible, spawn particles
354 if (invincible_timer.started() && !dying)
356 if (systemRandom.rand(0, 2) == 0) {
357 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
358 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
359 Vector ppos = Vector(px, py);
360 Vector pspeed = Vector(0, 0);
361 Vector paccel = Vector(0, 0);
362 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
363 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
364 // make every other a longer sparkle to make trail a bit fuzzy
365 if (size_t(game_time*20)%2) {
366 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
368 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
371 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
377 if (sprite->animation_done()) growing = false;
385 return on_ground_flag;
391 if(player_status->bonus == NO_BONUS)
398 Player::apply_friction()
400 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
401 physic.set_velocity_x(0);
402 physic.set_acceleration_x(0);
403 } else if(physic.get_velocity_x() < 0) {
404 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
405 } else if(physic.get_velocity_x() > 0) {
406 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
411 Player::handle_horizontal_input()
413 float vx = physic.get_velocity_x();
414 float vy = physic.get_velocity_y();
415 float ax = physic.get_acceleration_x();
416 float ay = physic.get_acceleration_y();
419 if(!duck || physic.get_velocity_y() != 0) {
420 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
424 } else if(!controller->hold(Controller::LEFT)
425 && controller->hold(Controller::RIGHT)) {
432 // do not run if action key is pressed or we're holding something
433 // so tux can only walk while shooting
434 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
435 ax = dirsign * WALK_ACCELERATION_X;
437 if(vx >= MAX_WALK_XM && dirsign > 0) {
440 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
445 if( vx * dirsign < MAX_WALK_XM ) {
446 ax = dirsign * WALK_ACCELERATION_X;
448 ax = dirsign * RUN_ACCELERATION_X;
451 if(vx >= MAX_RUN_XM && dirsign > 0) {
454 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
460 // we can reach WALK_SPEED without any acceleration
461 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
462 vx = dirsign * WALK_SPEED;
466 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
467 vx = dirsign * speedlimit;
471 // changing directions?
472 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
474 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
475 skidding_timer.start(SKID_TIME);
476 sound_manager->play("sounds/skid.wav");
477 // dust some particles
478 Sector::current()->add_object(
480 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
481 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
482 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
491 physic.set_velocity(vx, vy);
492 physic.set_acceleration(ax, ay);
494 // we get slower when not pressing any keys
516 if (physic.get_velocity_y() != 0)
523 if (adjust_height(31.8f)) {
526 unduck_hurt_timer.stop();
533 Player::do_standup() {
541 if (adjust_height(63.8f)) {
543 unduck_hurt_timer.stop();
545 // if timer is not already running, start it.
546 if (unduck_hurt_timer.get_period() == 0) {
547 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
549 else if (unduck_hurt_timer.check()) {
557 Player::do_backflip() {
563 backflip_direction = (dir == LEFT)?(+1):(-1);
566 sound_manager->play("sounds/flip.wav");
567 backflip_timer.start(0.15f);
571 Player::do_jump(float yspeed) {
575 physic.set_velocity_y(yspeed);
576 //bbox.move(Vector(0, -1));
578 on_ground_flag = false;
583 sound_manager->play("sounds/bigjump.wav");
585 sound_manager->play("sounds/jump.wav");
590 Player::early_jump_apex() {
591 if(jump_early_apex) {
594 jump_early_apex = true;
595 physic.set_gravity(physic.get_gravity() * JUMP_EARLY_APEX_FACTOR);
599 Player::do_jump_apex() {
600 if(!jump_early_apex) {
603 jump_early_apex = false;
604 physic.set_gravity(physic.get_gravity() / JUMP_EARLY_APEX_FACTOR);
608 Player::handle_vertical_input()
611 if(controller->pressed(Controller::JUMP) && (can_jump)) {
613 // when running, only jump a little bit; else do a backflip
614 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
616 // jump a bit higher if we are running; else do a normal jump
617 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
620 // Let go of jump key
621 else if(!controller->hold(Controller::JUMP)) {
622 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
628 if(jump_early_apex && physic.get_velocity_y() >= 0) {
632 /* In case the player has pressed Down while in a certain range of air,
633 enable butt jump action */
634 if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
635 wants_buttjump = true;
636 if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
639 /* When Down is not held anymore, disable butt jump */
640 if(!controller->hold(Controller::DOWN)) {
641 wants_buttjump = false;
642 does_buttjump = false;
646 physic.set_acceleration_y(0);
649 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
650 physic.set_acceleration_y(-2000);
651 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
657 Player::handle_input()
660 handle_input_ghost();
664 handle_input_climbing();
669 if( controller->released( Controller::PEEK_LEFT ) ) {
672 if( controller->released( Controller::PEEK_RIGHT ) ) {
675 if( controller->released( Controller::PEEK_UP ) ) {
678 if( controller->released( Controller::PEEK_DOWN ) ) {
681 if( controller->pressed( Controller::PEEK_LEFT ) ) {
684 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
687 if(!backflipping && !jumping && on_ground()) {
688 if( controller->pressed( Controller::PEEK_UP ) ) {
690 } else if( controller->pressed( Controller::PEEK_DOWN ) ) {
695 /* Handle horizontal movement: */
696 if (!backflipping) handle_horizontal_input();
699 if (on_ground() && !controller->hold(Controller::JUMP))
702 /* Handle vertical movement: */
703 handle_vertical_input();
706 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
707 if(Sector::current()->add_bullet(
708 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
709 : Vector(32, bbox.get_height()/2)),
710 physic.get_velocity_x(), dir))
711 shooting_timer.start(SHOOTING_TIME);
714 /* Duck or Standup! */
715 if (controller->hold(Controller::DOWN)) {
724 if(!controller->hold(Controller::ACTION) && grabbed_object) {
725 // move the grabbed object a bit away from tux
726 Vector pos = get_pos() +
727 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
728 bbox.get_height()*0.66666 - 32);
729 Rect dest(pos, pos + Vector(32, 32));
730 if(Sector::current()->is_free_of_movingstatics(dest)) {
731 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
733 moving_object->set_pos(pos);
735 log_debug << "Non MovingObject grabbed?!?" << std::endl;
737 if(controller->hold(Controller::UP)) {
738 grabbed_object->ungrab(*this, UP);
740 grabbed_object->ungrab(*this, dir);
742 grabbed_object = NULL;
750 if(controller->hold(Controller::ACTION) && !grabbed_object
752 Sector* sector = Sector::current();
755 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
757 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
760 for(Sector::Portables::iterator i = sector->portables.begin();
761 i != sector->portables.end(); ++i) {
762 Portable* portable = *i;
763 if(!portable->is_portable())
766 // make sure the Portable is a MovingObject
767 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
768 assert(moving_object);
769 if(moving_object == NULL)
772 // make sure the Portable isn't currently non-solid
773 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
775 // check if we are within reach
776 if(moving_object->get_bbox().contains(pos)) {
777 if (climbing) stop_climbing(*climbing);
778 grabbed_object = portable;
779 grabbed_object->grab(*this, get_pos(), dir);
787 Player::handle_input_ghost()
791 if (controller->hold(Controller::LEFT)) {
793 vx -= MAX_RUN_XM * 2;
795 if (controller->hold(Controller::RIGHT)) {
797 vx += MAX_RUN_XM * 2;
799 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
800 vy -= MAX_RUN_XM * 2;
802 if (controller->hold(Controller::DOWN)) {
803 vy += MAX_RUN_XM * 2;
805 if (controller->hold(Controller::ACTION)) {
806 set_ghost_mode(false);
808 physic.set_velocity(vx, vy);
809 physic.set_acceleration(0, 0);
813 Player::add_coins(int count)
815 player_status->add_coins(count);
821 return player_status->coins;
825 Player::add_bonus(const std::string& bonustype)
827 BonusType type = NO_BONUS;
829 if(bonustype == "grow") {
831 } else if(bonustype == "fireflower") {
833 } else if(bonustype == "iceflower") {
835 } else if(bonustype == "none") {
838 std::ostringstream msg;
839 msg << "Unknown bonus type " << bonustype;
840 throw std::runtime_error(msg.str());
843 return add_bonus(type);
847 Player::add_bonus(BonusType type, bool animate)
849 // always ignore NO_BONUS
850 if (type == NO_BONUS) {
854 // ignore GROWUP_BONUS if we're already big
855 if (type == GROWUP_BONUS) {
856 if (player_status->bonus == GROWUP_BONUS)
858 if (player_status->bonus == FIRE_BONUS)
860 if (player_status->bonus == ICE_BONUS)
864 return set_bonus(type, animate);
868 Player::set_bonus(BonusType type, bool animate)
870 if(player_status->bonus == NO_BONUS) {
871 if (!adjust_height(62.8f)) {
872 printf("can't adjust\n");
877 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
879 if (climbing) stop_climbing(*climbing);
882 if (type == NO_BONUS) {
883 if (does_buttjump) does_buttjump = false;
886 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
887 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
888 // visually lose helmet
889 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
890 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
891 Vector paccel = Vector(0, 1000);
892 std::string action = (dir==LEFT)?"left":"right";
893 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
894 if (climbing) stop_climbing(*climbing);
896 if ((player_status->bonus == ICE_BONUS) && (animate)) {
898 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
899 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
900 Vector paccel = Vector(0, 1000);
901 std::string action = (dir==LEFT)?"left":"right";
902 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
903 if (climbing) stop_climbing(*climbing);
905 player_status->max_fire_bullets = 0;
906 player_status->max_ice_bullets = 0;
908 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
909 if (type == ICE_BONUS) player_status->max_ice_bullets++;
911 player_status->bonus = type;
916 Player::set_visible(bool visible)
918 this->visible = visible;
920 set_group(COLGROUP_MOVING);
922 set_group(COLGROUP_DISABLED);
926 Player::get_visible()
934 kick_timer.start(KICK_TIME);
938 Player::draw(DrawingContext& context)
943 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
944 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
945 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
946 float py = Sector::current()->camera->get_translation().y;
947 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
948 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
951 std::string sa_prefix = "";
952 std::string sa_postfix = "";
954 if (player_status->bonus == GROWUP_BONUS)
956 else if (player_status->bonus == FIRE_BONUS)
958 else if (player_status->bonus == ICE_BONUS)
964 sa_postfix = "-left";
966 sa_postfix = "-right";
968 /* Set Tux sprite action */
970 sprite->set_action("gameover");
973 sprite->set_action_continued("grow"+sa_postfix);
974 // while growing, do not change action
975 // do_duck() will take care of cancelling growing manually
976 // update() will take care of cancelling when growing completed
979 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
981 else if (backflipping) {
982 sprite->set_action(sa_prefix+"-backflip"+sa_postfix);
984 else if (duck && is_big()) {
985 sprite->set_action(sa_prefix+"-duck"+sa_postfix);
987 else if (skidding_timer.started() && !skidding_timer.check()) {
988 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
990 else if (kick_timer.started() && !kick_timer.check()) {
991 sprite->set_action(sa_prefix+"-kick"+sa_postfix);
993 else if ((wants_buttjump || does_buttjump) && is_big()) {
994 sprite->set_action(sa_prefix+"-buttjump"+sa_postfix);
996 else if (!on_ground()) {
997 sprite->set_action(sa_prefix+"-jump"+sa_postfix);
1000 if (fabsf(physic.get_velocity_x()) < 1.0f) {
1001 // Determine which idle stage we're at
1002 if (sprite->get_action().find("-stand-") == std::string::npos && sprite->get_action().find("-idle-") == std::string::npos) {
1004 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1006 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1008 else if (idle_timer.check() || (IDLE_TIME[idle_stage] == 0 && sprite->animation_done())) {
1010 if (idle_stage >= IDLE_STAGE_COUNT)
1013 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1015 if (IDLE_TIME[idle_stage] == 0)
1016 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix, 1);
1018 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1021 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1025 sprite->set_action(sa_prefix+"-walk"+sa_postfix);
1031 // Tux is holding something
1032 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1033 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
1041 if (safe_timer.started() && size_t(game_time*40)%2)
1044 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1050 Player::collision_tile(uint32_t tile_attributes)
1052 if(tile_attributes & Tile::HURTS)
1057 if( tile_attributes & Tile::WATER ){
1063 if( tile_attributes & Tile::WATER ){
1066 sound_manager->play( "sounds/splash.ogg" );
1073 Player::collision_solid(const CollisionHit& hit)
1076 if(physic.get_velocity_y() > 0)
1077 physic.set_velocity_y(0);
1079 on_ground_flag = true;
1080 floor_normal = hit.slope_normal;
1083 if (does_buttjump) {
1084 does_buttjump = false;
1085 physic.set_velocity_y(-300);
1086 on_ground_flag = false;
1087 Sector::current()->add_object(new Particles(
1088 Vector(get_bbox().p2.x, get_bbox().p2.y),
1090 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1092 Sector::current()->add_object(new Particles(
1093 Vector(get_bbox().p1.x, get_bbox().p2.y),
1095 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1099 } else if(hit.top) {
1100 if(physic.get_velocity_y() < 0)
1101 physic.set_velocity_y(.2f);
1104 if(hit.left || hit.right) {
1105 physic.set_velocity_x(0);
1110 if(hit.left || hit.right) {
1112 } else if(hit.top || hit.bottom) {
1119 Player::collision(GameObject& other, const CollisionHit& hit)
1121 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1126 if(hit.left || hit.right) {
1127 try_grab(); //grab objects right now, in update it will be too late
1130 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1132 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1133 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1134 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1136 if(controller->pressed(Controller::UP))
1137 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1143 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1144 if(badguy != NULL) {
1145 if(safe_timer.started() || invincible_timer.started())
1155 Player::make_invincible()
1157 sound_manager->play("sounds/invincible_start.ogg");
1158 invincible_timer.start(TUX_INVINCIBLE_TIME);
1159 Sector::current()->play_music(HERRING_MUSIC);
1164 Player::kill(bool completely)
1166 if(dying || deactivated)
1169 if(!completely && (safe_timer.started() || invincible_timer.started()))
1174 sound_manager->play("sounds/hurt.wav");
1176 if (climbing) stop_climbing(*climbing);
1178 physic.set_velocity_x(0);
1180 if(!completely && is_big()) {
1181 if(player_status->bonus == FIRE_BONUS
1182 || player_status->bonus == ICE_BONUS) {
1183 safe_timer.start(TUX_SAFE_TIME);
1184 set_bonus(GROWUP_BONUS, true);
1185 } else if(player_status->bonus == GROWUP_BONUS) {
1186 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1187 adjust_height(30.8f);
1189 backflipping = false;
1190 set_bonus(NO_BONUS, true);
1191 } else if(player_status->bonus == NO_BONUS) {
1192 safe_timer.start(TUX_SAFE_TIME);
1193 adjust_height(30.8f);
1198 // do not die when in edit mode
1200 set_ghost_mode(true);
1204 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1206 for (int i = 0; i < 5; i++)
1208 // the numbers: starting x, starting y, velocity y
1209 Sector::current()->add_object(new FallingCoin(get_pos() +
1210 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1211 systemRandom.rand(-100,100)));
1213 player_status->coins -= std::max(player_status->coins/10, 25);
1217 GameSession::current()->set_reset_point("", Vector());
1219 physic.enable_gravity(true);
1220 physic.set_acceleration(0, 0);
1221 physic.set_velocity(0, -700);
1222 set_bonus(NO_BONUS, true);
1224 dying_timer.start(3.0);
1225 set_group(COLGROUP_DISABLED);
1227 DisplayEffect* effect = new DisplayEffect();
1228 effect->fade_out(3.0);
1229 Sector::current()->add_object(effect);
1230 sound_manager->stop_music(3.0);
1235 Player::move(const Vector& vector)
1239 // TODO: do we need the following? Seems irrelevant to moving the player
1241 set_size(31.8f, 63.8f);
1243 set_size(31.8f, 31.8f);
1245 last_ground_y = vector.y;
1246 if (climbing) stop_climbing(*climbing);
1252 Player::check_bounds(Camera* camera)
1254 /* Keep tux in bounds: */
1255 if (get_pos().x < 0) {
1256 // Lock Tux to the size of the level, so that he doesn't fall of
1258 set_pos(Vector(0, get_pos().y));
1261 /* fallen out of the level? */
1262 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1267 // can happen if back scrolling is disabled
1268 if(get_pos().x < camera->get_translation().x) {
1269 set_pos(Vector(camera->get_translation().x, get_pos().y));
1271 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1274 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1280 Player::add_velocity(const Vector& velocity)
1282 physic.set_velocity(physic.get_velocity() + velocity);
1286 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1288 if (end_speed.x > 0)
1289 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1290 if (end_speed.x < 0)
1291 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1292 if (end_speed.y > 0)
1293 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1294 if (end_speed.y < 0)
1295 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1299 Player::get_velocity()
1301 return physic.get_velocity();
1305 Player::bounce(BadGuy& )
1307 if(controller->hold(Controller::JUMP))
1308 physic.set_velocity_y(-520);
1310 physic.set_velocity_y(-300);
1313 //Scripting Functions Below
1316 Player::deactivate()
1321 physic.set_velocity_x(0);
1322 physic.set_velocity_y(0);
1323 physic.set_acceleration_x(0);
1324 physic.set_acceleration_y(0);
1325 if (climbing) stop_climbing(*climbing);
1333 deactivated = false;
1336 void Player::walk(float speed)
1338 physic.set_velocity_x(speed);
1342 Player::set_ghost_mode(bool enable)
1344 if (ghost_mode == enable)
1347 if (climbing) stop_climbing(*climbing);
1351 set_group(COLGROUP_DISABLED);
1352 physic.enable_gravity(false);
1353 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1356 set_group(COLGROUP_MOVING);
1357 physic.enable_gravity(true);
1358 log_debug << "You feel solid again." << std::endl;
1364 Player::set_edit_mode(bool enable)
1370 Player::start_climbing(Climbable& climbable)
1372 if (climbing == &climbable) return;
1374 climbing = &climbable;
1375 physic.enable_gravity(false);
1376 physic.set_velocity(0, 0);
1377 physic.set_acceleration(0, 0);
1381 Player::stop_climbing(Climbable& /*climbable*/)
1383 if (!climbing) return;
1387 if (grabbed_object) {
1388 grabbed_object->ungrab(*this, dir);
1389 grabbed_object = NULL;
1392 physic.enable_gravity(true);
1393 physic.set_velocity(0, 0);
1394 physic.set_acceleration(0, 0);
1396 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1397 on_ground_flag = true;
1398 // TODO: This won't help. Why?
1404 Player::handle_input_climbing()
1407 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1413 if (controller->hold(Controller::LEFT)) {
1417 if (controller->hold(Controller::RIGHT)) {
1421 if (controller->hold(Controller::UP)) {
1424 if (controller->hold(Controller::DOWN)) {
1427 if (controller->hold(Controller::JUMP)) {
1429 stop_climbing(*climbing);
1435 if (controller->hold(Controller::ACTION)) {
1436 stop_climbing(*climbing);
1439 physic.set_velocity(vx, vy);
1440 physic.set_acceleration(0, 0);