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 if (player_status->bonus == GROWUP_BONUS)
954 else if (player_status->bonus == FIRE_BONUS)
956 else if (player_status->bonus == ICE_BONUS)
961 /* Set Tux sprite action */
963 sprite->set_action("gameover");
966 sprite->set_action_continued((dir == LEFT)?"grow-left":"grow-right");
967 // while growing, do not change action
968 // do_duck() will take care of cancelling growing manually
969 // update() will take care of cancelling when growing completed
972 sprite->set_action(sa_prefix+((dir == LEFT)?"-skid-left":"-skid-right"));
974 else if (backflipping) {
975 sprite->set_action(sa_prefix+((dir == LEFT)?"-backflip-left":"-backflip-right"));
977 else if (duck && is_big()) {
978 sprite->set_action(sa_prefix+((dir == LEFT)?"-duck-left":"-duck-right"));
980 else if (skidding_timer.started() && !skidding_timer.check()) {
981 sprite->set_action(sa_prefix+((dir == LEFT)?"-skid-left":"-skid-right"));
983 else if (kick_timer.started() && !kick_timer.check()) {
984 sprite->set_action(sa_prefix+((dir == LEFT)?"-kick-left":"-kick-right"));
986 else if ((wants_buttjump || does_buttjump) && is_big()) {
987 sprite->set_action(sa_prefix+((dir == LEFT)?"-buttjump-left":"-buttjump-right"));
989 else if (!on_ground()) {
990 sprite->set_action(sa_prefix+((dir == LEFT)?"-jump-left":"-jump-right"));
993 if (fabsf(physic.get_velocity_x()) < 1.0f) {
994 // Determine which idle stage we're at
995 if (sprite->get_action().find("-stand-") == std::string::npos && sprite->get_action().find("-idle-") == std::string::npos) {
997 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
999 sprite->set_action_continued(sa_prefix+((dir == LEFT)?"-" + IDLE_STAGES[idle_stage] + "-left":"-" + IDLE_STAGES[idle_stage] + "-right"));
1001 else if (idle_timer.check() || (IDLE_TIME[idle_stage] == 0 && sprite->animation_done())) {
1003 if (idle_stage >= IDLE_STAGE_COUNT)
1006 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1008 if (IDLE_TIME[idle_stage] == 0)
1009 sprite->set_action(sa_prefix+((dir == LEFT)?"-" + IDLE_STAGES[idle_stage] + "-left":"-" + IDLE_STAGES[idle_stage] + "-right"), 1);
1011 sprite->set_action(sa_prefix+((dir == LEFT)?"-" + IDLE_STAGES[idle_stage] + "-left":"-" + IDLE_STAGES[idle_stage] + "-right"));
1014 sprite->set_action_continued(sa_prefix+((dir == LEFT)?"-" + IDLE_STAGES[idle_stage] + "-left":"-" + IDLE_STAGES[idle_stage] + "-right"));
1018 sprite->set_action(sa_prefix+((dir == LEFT)?"-walk-left":"-walk-right"));
1024 // Tux is holding something
1025 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1026 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
1034 if (safe_timer.started() && size_t(game_time*40)%2)
1037 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1043 Player::collision_tile(uint32_t tile_attributes)
1045 if(tile_attributes & Tile::HURTS)
1050 if( tile_attributes & Tile::WATER ){
1056 if( tile_attributes & Tile::WATER ){
1059 sound_manager->play( "sounds/splash.ogg" );
1066 Player::collision_solid(const CollisionHit& hit)
1069 if(physic.get_velocity_y() > 0)
1070 physic.set_velocity_y(0);
1072 on_ground_flag = true;
1073 floor_normal = hit.slope_normal;
1076 if (does_buttjump) {
1077 does_buttjump = false;
1078 physic.set_velocity_y(-300);
1079 on_ground_flag = false;
1080 Sector::current()->add_object(new Particles(
1081 Vector(get_bbox().p2.x, get_bbox().p2.y),
1083 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1085 Sector::current()->add_object(new Particles(
1086 Vector(get_bbox().p1.x, get_bbox().p2.y),
1088 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1092 } else if(hit.top) {
1093 if(physic.get_velocity_y() < 0)
1094 physic.set_velocity_y(.2f);
1097 if(hit.left || hit.right) {
1098 physic.set_velocity_x(0);
1103 if(hit.left || hit.right) {
1105 } else if(hit.top || hit.bottom) {
1112 Player::collision(GameObject& other, const CollisionHit& hit)
1114 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1119 if(hit.left || hit.right) {
1120 try_grab(); //grab objects right now, in update it will be too late
1123 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1125 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1126 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1127 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1129 if(controller->pressed(Controller::UP))
1130 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1136 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1137 if(badguy != NULL) {
1138 if(safe_timer.started() || invincible_timer.started())
1148 Player::make_invincible()
1150 sound_manager->play("sounds/invincible_start.ogg");
1151 invincible_timer.start(TUX_INVINCIBLE_TIME);
1152 Sector::current()->play_music(HERRING_MUSIC);
1157 Player::kill(bool completely)
1159 if(dying || deactivated)
1162 if(!completely && (safe_timer.started() || invincible_timer.started()))
1167 sound_manager->play("sounds/hurt.wav");
1169 if (climbing) stop_climbing(*climbing);
1171 physic.set_velocity_x(0);
1173 if(!completely && is_big()) {
1174 if(player_status->bonus == FIRE_BONUS
1175 || player_status->bonus == ICE_BONUS) {
1176 safe_timer.start(TUX_SAFE_TIME);
1177 set_bonus(GROWUP_BONUS, true);
1178 } else if(player_status->bonus == GROWUP_BONUS) {
1179 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1180 adjust_height(30.8f);
1182 backflipping = false;
1183 set_bonus(NO_BONUS, true);
1184 } else if(player_status->bonus == NO_BONUS) {
1185 safe_timer.start(TUX_SAFE_TIME);
1186 adjust_height(30.8f);
1191 // do not die when in edit mode
1193 set_ghost_mode(true);
1197 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1199 for (int i = 0; i < 5; i++)
1201 // the numbers: starting x, starting y, velocity y
1202 Sector::current()->add_object(new FallingCoin(get_pos() +
1203 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1204 systemRandom.rand(-100,100)));
1206 player_status->coins -= std::max(player_status->coins/10, 25);
1210 GameSession::current()->set_reset_point("", Vector());
1212 physic.enable_gravity(true);
1213 physic.set_acceleration(0, 0);
1214 physic.set_velocity(0, -700);
1215 set_bonus(NO_BONUS, true);
1217 dying_timer.start(3.0);
1218 set_group(COLGROUP_DISABLED);
1220 DisplayEffect* effect = new DisplayEffect();
1221 effect->fade_out(3.0);
1222 Sector::current()->add_object(effect);
1223 sound_manager->stop_music(3.0);
1228 Player::move(const Vector& vector)
1232 // TODO: do we need the following? Seems irrelevant to moving the player
1234 set_size(31.8f, 63.8f);
1236 set_size(31.8f, 31.8f);
1238 last_ground_y = vector.y;
1239 if (climbing) stop_climbing(*climbing);
1245 Player::check_bounds(Camera* camera)
1247 /* Keep tux in bounds: */
1248 if (get_pos().x < 0) {
1249 // Lock Tux to the size of the level, so that he doesn't fall of
1251 set_pos(Vector(0, get_pos().y));
1254 /* fallen out of the level? */
1255 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1260 // can happen if back scrolling is disabled
1261 if(get_pos().x < camera->get_translation().x) {
1262 set_pos(Vector(camera->get_translation().x, get_pos().y));
1264 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1267 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1273 Player::add_velocity(const Vector& velocity)
1275 physic.set_velocity(physic.get_velocity() + velocity);
1279 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1281 if (end_speed.x > 0)
1282 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1283 if (end_speed.x < 0)
1284 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1285 if (end_speed.y > 0)
1286 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1287 if (end_speed.y < 0)
1288 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1292 Player::get_velocity()
1294 return physic.get_velocity();
1298 Player::bounce(BadGuy& )
1300 if(controller->hold(Controller::JUMP))
1301 physic.set_velocity_y(-520);
1303 physic.set_velocity_y(-300);
1306 //Scripting Functions Below
1309 Player::deactivate()
1314 physic.set_velocity_x(0);
1315 physic.set_velocity_y(0);
1316 physic.set_acceleration_x(0);
1317 physic.set_acceleration_y(0);
1318 if (climbing) stop_climbing(*climbing);
1326 deactivated = false;
1329 void Player::walk(float speed)
1331 physic.set_velocity_x(speed);
1335 Player::set_ghost_mode(bool enable)
1337 if (ghost_mode == enable)
1340 if (climbing) stop_climbing(*climbing);
1344 set_group(COLGROUP_DISABLED);
1345 physic.enable_gravity(false);
1346 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1349 set_group(COLGROUP_MOVING);
1350 physic.enable_gravity(true);
1351 log_debug << "You feel solid again." << std::endl;
1357 Player::set_edit_mode(bool enable)
1363 Player::start_climbing(Climbable& climbable)
1365 if (climbing == &climbable) return;
1367 climbing = &climbable;
1368 physic.enable_gravity(false);
1369 physic.set_velocity(0, 0);
1370 physic.set_acceleration(0, 0);
1374 Player::stop_climbing(Climbable& /*climbable*/)
1376 if (!climbing) return;
1380 if (grabbed_object) {
1381 grabbed_object->ungrab(*this, dir);
1382 grabbed_object = NULL;
1385 physic.enable_gravity(true);
1386 physic.set_velocity(0, 0);
1387 physic.set_acceleration(0, 0);
1389 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1390 on_ground_flag = true;
1391 // TODO: This won't help. Why?
1397 Player::handle_input_climbing()
1400 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1406 if (controller->hold(Controller::LEFT)) {
1410 if (controller->hold(Controller::RIGHT)) {
1414 if (controller->hold(Controller::UP)) {
1417 if (controller->hold(Controller::DOWN)) {
1420 if (controller->hold(Controller::JUMP)) {
1422 stop_climbing(*climbing);
1428 if (controller->hold(Controller::ACTION)) {
1429 stop_climbing(*climbing);
1432 physic.set_velocity(vx, vy);
1433 physic.set_acceleration(0, 0);