2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #include "object/player.hpp"
20 #include "audio/sound_manager.hpp"
21 #include "badguy/badguy.hpp"
22 #include "control/input_manager.hpp"
23 #include "math/random_generator.hpp"
24 #include "object/bullet.hpp"
25 #include "object/camera.hpp"
26 #include "object/display_effect.hpp"
27 #include "object/falling_coin.hpp"
28 #include "object/particles.hpp"
29 #include "object/portable.hpp"
30 #include "object/sprite_particle.hpp"
31 #include "scripting/squirrel_util.hpp"
32 #include "supertux/game_session.hpp"
33 #include "supertux/globals.hpp"
34 #include "supertux/sector.hpp"
35 #include "supertux/tile.hpp"
36 #include "trigger/climbable.hpp"
43 static const int TILES_FOR_BUTTJUMP = 3;
44 static const float BUTTJUMP_MIN_VELOCITY_Y = 400.0f;
45 static const float SHOOTING_TIME = .150f;
47 /** number of idle stages, including standing */
48 static const unsigned int IDLE_STAGE_COUNT = 5;
50 * how long to play each idle animation in milliseconds
51 * '0' means the sprite action is played once before moving onto the next
54 static const int IDLE_TIME[] = { 5000, 0, 2500, 0, 2500 };
56 static const std::string IDLE_STAGES[] =
63 /** acceleration in horizontal direction when walking
64 * (all accelerations are in pixel/s^2) */
65 static const float WALK_ACCELERATION_X = 300;
66 /** acceleration in horizontal direction when running */
67 static const float RUN_ACCELERATION_X = 400;
68 /** acceleration when skidding */
69 static const float SKID_XM = 200;
70 /** time of skidding in seconds */
71 static const float SKID_TIME = .3f;
72 /** maximum walk velocity (pixel/s) */
73 static const float MAX_WALK_XM = 230;
74 /** maximum run velocity (pixel/s) */
75 static const float MAX_RUN_XM = 320;
76 /** maximum horizontal climb velocity */
77 static const float MAX_CLIMB_XM = 96;
78 /** maximum vertical climb velocity */
79 static const float MAX_CLIMB_YM = 128;
80 /** instant velocity when tux starts to walk */
81 static const float WALK_SPEED = 100;
83 /** multiplied by WALK_ACCELERATION to give friction */
84 static const float NORMAL_FRICTION_MULTIPLIER = 1.5f;
85 /** multiplied by WALK_ACCELERATION to give friction */
86 static const float ICE_FRICTION_MULTIPLIER = 0.1f;
87 static const float ICE_ACCELERATION_MULTIPLIER = 0.25f;
89 /** time of the kick (kicking mriceblock) animation */
90 static const float KICK_TIME = .3f;
91 /** time of tux cheering (currently unused) */
92 static const float CHEER_TIME = 1.0f;
94 /** if Tux cannot unduck for this long, he will get hurt */
95 static const float UNDUCK_HURT_TIME = 0.25f;
96 /** gravity is higher after the jump key is released before
97 the apex of the jump is reached */
98 static const float JUMP_EARLY_APEX_FACTOR = 3.0;
100 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) */
102 /* Tux's collision rectangle */
103 static const float TUX_WIDTH = 31.8f;
104 static const float RUNNING_TUX_WIDTH = 34;
105 static const float SMALL_TUX_HEIGHT = 30.8f;
106 static const float BIG_TUX_HEIGHT = 62.8f;
107 static const float DUCKED_TUX_HEIGHT = 31.8f;
109 bool no_water = true;
112 Player::Player(PlayerStatus* _player_status, const std::string& name_) :
115 scripting_controller(),
116 player_status(_player_status),
122 backflip_direction(),
127 scripting_controller_old(0),
151 grabbed_object(NULL),
163 controller = InputManager::current()->get_controller();
164 scripting_controller.reset(new CodeController());
165 // if/when we have complete penny gfx, we can
166 // load those instead of Tux's sprite in the
168 sprite = SpriteManager::current()->create("images/creatures/tux/tux.sprite");
169 airarrow = Surface::create("images/engine/hud/airarrow.png");
170 idle_timer.start(IDLE_TIME[0]/1000.0f);
172 SoundManager::current()->preload("sounds/bigjump.wav");
173 SoundManager::current()->preload("sounds/jump.wav");
174 SoundManager::current()->preload("sounds/hurt.wav");
175 SoundManager::current()->preload("sounds/kill.wav");
176 SoundManager::current()->preload("sounds/skid.wav");
177 SoundManager::current()->preload("sounds/flip.wav");
178 SoundManager::current()->preload("sounds/invincible_start.ogg");
179 SoundManager::current()->preload("sounds/splash.ogg");
186 if (climbing) stop_climbing(*climbing);
193 set_size(TUX_WIDTH, BIG_TUX_HEIGHT);
195 set_size(TUX_WIDTH, SMALL_TUX_HEIGHT);
207 fall_mode = ON_GROUND;
209 jump_early_apex = false;
211 wants_buttjump = false;
212 does_buttjump = false;
215 backflipping = false;
216 backflip_direction = 0;
217 sprite->set_angle(0.0f);
221 ice_this_frame = false;
222 speedlimit = 0; //no special limit
224 on_ground_flag = false;
225 grabbed_object = NULL;
233 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
238 scripting::expose_object(vm, table_idx, dynamic_cast<scripting::Player *>(this), name, false);
242 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
247 scripting::unexpose_object(vm, table_idx, name);
251 Player::get_speedlimit()
257 Player::set_speedlimit(float newlimit)
263 Player::set_controller(Controller* controller_)
265 this->controller = controller_;
269 Player::set_winning()
271 if( ! is_winning() ){
273 invincible_timer.start(10000.0f);
278 Player::use_scripting_controller(bool use_or_release)
280 if ((use_or_release == true) && (controller != scripting_controller.get())) {
281 scripting_controller_old = get_controller();
282 set_controller(scripting_controller.get());
284 if ((use_or_release == false) && (controller == scripting_controller.get())) {
285 set_controller(scripting_controller_old);
286 scripting_controller_old = 0;
291 Player::do_scripting_controller(std::string control, bool pressed)
293 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
294 if(control == std::string(Controller::controlNames[i])) {
295 scripting_controller->press(Controller::Control(i), pressed);
301 Player::adjust_height(float new_height)
304 bbox2.move(Vector(0, bbox.get_height() - new_height));
305 bbox2.set_height(new_height);
308 if(new_height > bbox.get_height()) {
309 Rectf additional_space = bbox2;
310 additional_space.set_height(new_height - bbox.get_height());
311 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
315 // adjust bbox accordingly
316 // note that we use members of moving_object for this, so we can run this during CD, too
318 set_size(bbox2.get_width(), bbox2.get_height());
323 Player::trigger_sequence(std::string sequence_name)
325 if (climbing) stop_climbing(*climbing);
326 backflipping = false;
327 backflip_direction = 0;
328 sprite->set_angle(0.0f);
329 GameSession::current()->start_sequence(sequence_name);
333 Player::update(float elapsed_time)
340 if(dying && dying_timer.check()) {
341 set_bonus(NO_BONUS, true);
346 if(!dying && !deactivated)
350 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
355 // extend/shrink tux collision rectangle so that we fall through/walk over 1
357 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
358 set_width(RUNNING_TUX_WIDTH);
360 set_width(TUX_WIDTH);
363 // on downward slopes, adjust vertical velocity so tux walks smoothly down
364 if (on_ground() && !dying) {
365 if(floor_normal.y != 0) {
366 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
367 physic.set_velocity_y(250);
372 // handle backflipping
373 if (backflipping && !dying) {
374 //prevent player from changing direction when backflipping
375 dir = (backflip_direction == 1) ? LEFT : RIGHT;
376 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
377 //rotate sprite during flip
378 sprite->set_angle(sprite->get_angle() + (dir==LEFT?1:-1) * elapsed_time * (360.0f / 0.5f));
383 fall_mode = ON_GROUND;
384 last_ground_y = get_pos().y;
386 if(get_pos().y > last_ground_y)
388 else if(fall_mode == ON_GROUND)
392 // check if we landed
395 if (backflipping && (backflip_timer.get_timegone() > 0.15f)) {
396 backflipping = false;
397 backflip_direction = 0;
398 sprite->set_angle(0.0f);
400 // if controls are currently deactivated, we take care of standing up ourselves
406 // calculate movement for this frame
407 movement = physic.get_movement(elapsed_time);
409 if(grabbed_object != NULL && !dying) {
410 position_grabbed_object();
413 if(grabbed_object != NULL && dying){
414 grabbed_object->ungrab(*this, dir);
415 grabbed_object = NULL;
418 if(!ice_this_frame && on_ground())
421 on_ground_flag = false;
422 ice_this_frame = false;
424 // when invincible, spawn particles
425 if (invincible_timer.started())
427 if (graphicsRandom.rand(0, 2) == 0) {
428 float px = graphicsRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
429 float py = graphicsRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
430 Vector ppos = Vector(px, py);
431 Vector pspeed = Vector(0, 0);
432 Vector paccel = Vector(0, 0);
433 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite",
434 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
435 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) ?
436 // make every other a longer sparkle to make trail a bit fuzzy
437 (size_t(game_time*20)%2) ? "small" : "medium"
439 "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
444 if (sprite->animation_done()) growing = false;
447 // when climbing animate only while moving
449 if((physic.get_velocity_x()==0)&&(physic.get_velocity_y()==0))
450 sprite->stop_animation();
452 sprite->set_animation_loops(-1);
460 return on_ground_flag;
466 if(player_status->bonus == NO_BONUS)
473 Player::apply_friction()
475 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
476 physic.set_velocity_x(0);
477 physic.set_acceleration_x(0);
479 float friction = WALK_ACCELERATION_X * (on_ice ? ICE_FRICTION_MULTIPLIER : NORMAL_FRICTION_MULTIPLIER);
480 if(physic.get_velocity_x() < 0) {
481 physic.set_acceleration_x(friction);
482 } else if(physic.get_velocity_x() > 0) {
483 physic.set_acceleration_x(-friction);
484 } // no friction for physic.get_velocity_x() == 0
489 Player::handle_horizontal_input()
491 float vx = physic.get_velocity_x();
492 float vy = physic.get_velocity_y();
493 float ax = physic.get_acceleration_x();
494 float ay = physic.get_acceleration_y();
497 if(!duck || physic.get_velocity_y() != 0) {
498 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
502 } else if(!controller->hold(Controller::LEFT)
503 && controller->hold(Controller::RIGHT)) {
510 // do not run if we're holding something which slows us down
511 if ( grabbed_object && grabbed_object->is_hampering() ) {
512 ax = dirsign * WALK_ACCELERATION_X;
514 if(vx >= MAX_WALK_XM && dirsign > 0) {
517 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
522 if( vx * dirsign < MAX_WALK_XM ) {
523 ax = dirsign * WALK_ACCELERATION_X;
525 ax = dirsign * RUN_ACCELERATION_X;
528 if(vx >= MAX_RUN_XM && dirsign > 0) {
531 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
537 // we can reach WALK_SPEED without any acceleration
538 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
539 vx = dirsign * WALK_SPEED;
543 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
544 vx = dirsign * speedlimit;
548 // changing directions?
549 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
551 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
552 skidding_timer.start(SKID_TIME);
553 SoundManager::current()->play("sounds/skid.wav");
554 // dust some particles
555 Sector::current()->add_object(
557 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
558 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
559 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
569 ax *= ICE_ACCELERATION_MULTIPLIER;
572 physic.set_velocity(vx, vy);
573 physic.set_acceleration(ax, ay);
575 // we get slower when not pressing any keys
597 if (physic.get_velocity_y() != 0)
604 if (adjust_height(DUCKED_TUX_HEIGHT)) {
607 unduck_hurt_timer.stop();
614 Player::do_standup() {
622 if (adjust_height(BIG_TUX_HEIGHT)) {
624 unduck_hurt_timer.stop();
626 // if timer is not already running, start it.
627 if (unduck_hurt_timer.get_period() == 0) {
628 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
630 else if (unduck_hurt_timer.check()) {
638 Player::do_backflip() {
644 backflip_direction = (dir == LEFT)?(+1):(-1);
647 SoundManager::current()->play("sounds/flip.wav");
648 backflip_timer.start(TUX_BACKFLIP_TIME);
652 Player::do_jump(float yspeed) {
656 physic.set_velocity_y(yspeed);
657 //bbox.move(Vector(0, -1));
659 on_ground_flag = false;
664 SoundManager::current()->play("sounds/bigjump.wav");
666 SoundManager::current()->play("sounds/jump.wav");
671 Player::early_jump_apex()
673 if (!jump_early_apex)
675 jump_early_apex = true;
676 physic.set_gravity_modifier(JUMP_EARLY_APEX_FACTOR);
681 Player::do_jump_apex()
685 jump_early_apex = false;
686 physic.set_gravity_modifier(1.0f);
691 Player::handle_vertical_input()
694 if(controller->pressed(Controller::JUMP)) jump_button_timer.start(JUMP_GRACE_TIME);
695 if(controller->hold(Controller::JUMP) && jump_button_timer.started() && can_jump) {
696 jump_button_timer.stop();
698 // when running, only jump a little bit; else do a backflip
699 if ((physic.get_velocity_x() != 0) ||
700 (controller->hold(Controller::LEFT)) ||
701 (controller->hold(Controller::RIGHT)))
710 // jump a bit higher if we are running; else do a normal jump
711 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
714 // Let go of jump key
715 else if(!controller->hold(Controller::JUMP)) {
716 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
722 if(jump_early_apex && physic.get_velocity_y() >= 0) {
726 /* In case the player has pressed Down while in a certain range of air,
727 enable butt jump action */
728 if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
729 wants_buttjump = true;
730 if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
733 /* When Down is not held anymore, disable butt jump */
734 if(!controller->hold(Controller::DOWN)) {
735 wants_buttjump = false;
736 does_buttjump = false;
740 physic.set_acceleration_y(0);
743 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
744 physic.set_acceleration_y(-2000);
745 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
751 Player::handle_input()
754 handle_input_ghost();
758 handle_input_climbing();
763 if( controller->released( Controller::PEEK_LEFT ) || controller->released( Controller::PEEK_RIGHT ) ) {
766 if( controller->released( Controller::PEEK_UP ) || controller->released( Controller::PEEK_DOWN ) ) {
769 if( controller->pressed( Controller::PEEK_LEFT ) ) {
772 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
775 if(!backflipping && !jumping && on_ground()) {
776 if( controller->pressed( Controller::PEEK_UP ) ) {
778 } else if( controller->pressed( Controller::PEEK_DOWN ) ) {
783 /* Handle horizontal movement: */
784 if (!backflipping) handle_horizontal_input();
790 /* Handle vertical movement: */
791 handle_vertical_input();
794 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
795 if(Sector::current()->add_bullet(
796 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
797 : Vector(32, bbox.get_height()/2)),
799 physic.get_velocity_x(), dir))
800 shooting_timer.start(SHOOTING_TIME);
803 /* Duck or Standup! */
804 if (controller->hold(Controller::DOWN)) {
813 if(!controller->hold(Controller::ACTION) && grabbed_object) {
814 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
816 // move the grabbed object a bit away from tux
817 Rectf grabbed_bbox = moving_object->get_bbox();
819 dest_.p2.y = bbox.get_top() + bbox.get_height()*0.66666;
820 dest_.p1.y = dest_.p2.y - grabbed_bbox.get_height();
822 dest_.p2.x = bbox.get_left() - 1;
823 dest_.p1.x = dest_.p2.x - grabbed_bbox.get_width();
825 dest_.p1.x = bbox.get_right() + 1;
826 dest_.p2.x = dest_.p1.x + grabbed_bbox.get_width();
828 if(Sector::current()->is_free_of_tiles(dest_, true)) {
829 moving_object->set_pos(dest_.p1);
830 if(controller->hold(Controller::UP)) {
831 grabbed_object->ungrab(*this, UP);
833 grabbed_object->ungrab(*this, dir);
835 grabbed_object = NULL;
838 log_debug << "Non MovingObject grabbed?!?" << std::endl;
842 /* stop backflipping at will */
843 if( backflipping && ( !controller->hold(Controller::JUMP) && !backflip_timer.started()) ){
844 backflipping = false;
845 backflip_direction = 0;
846 sprite->set_angle(0.0f);
851 Player::position_grabbed_object()
853 MovingObject* moving_object = dynamic_cast<MovingObject*>(grabbed_object);
854 assert(moving_object);
856 // Position where we will hold the lower-inner corner
857 Vector pos(get_bbox().get_left() + get_bbox().get_width()/2,
858 get_bbox().get_top() + get_bbox().get_height()*0.66666);
860 // Adjust to find the grabbed object's upper-left corner
862 pos.x -= moving_object->get_bbox().get_width();
863 pos.y -= moving_object->get_bbox().get_height();
865 grabbed_object->grab(*this, pos, dir);
871 if(controller->hold(Controller::ACTION) && !grabbed_object
873 Sector* sector = Sector::current();
876 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
878 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
881 for(Sector::Portables::iterator i = sector->portables.begin();
882 i != sector->portables.end(); ++i) {
883 Portable* portable = *i;
884 if(!portable->is_portable())
887 // make sure the Portable is a MovingObject
888 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
889 assert(moving_object);
890 if(moving_object == NULL)
893 // make sure the Portable isn't currently non-solid
894 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
896 // check if we are within reach
897 if(moving_object->get_bbox().contains(pos)) {
898 if (climbing) stop_climbing(*climbing);
899 grabbed_object = portable;
900 position_grabbed_object();
908 Player::handle_input_ghost()
912 if (controller->hold(Controller::LEFT)) {
914 vx -= MAX_RUN_XM * 2;
916 if (controller->hold(Controller::RIGHT)) {
918 vx += MAX_RUN_XM * 2;
920 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
921 vy -= MAX_RUN_XM * 2;
923 if (controller->hold(Controller::DOWN)) {
924 vy += MAX_RUN_XM * 2;
926 if (controller->hold(Controller::ACTION)) {
927 set_ghost_mode(false);
929 physic.set_velocity(vx, vy);
930 physic.set_acceleration(0, 0);
934 Player::add_coins(int count)
936 player_status->add_coins(count);
942 return player_status->coins;
946 Player::add_bonus(const std::string& bonustype)
948 BonusType type = NO_BONUS;
950 if(bonustype == "grow") {
952 } else if(bonustype == "fireflower") {
954 } else if(bonustype == "iceflower") {
956 } else if(bonustype == "none") {
959 std::ostringstream msg;
960 msg << "Unknown bonus type " << bonustype;
961 throw std::runtime_error(msg.str());
964 return add_bonus(type);
968 Player::add_bonus(BonusType type, bool animate)
970 // always ignore NO_BONUS
971 if (type == NO_BONUS) {
975 // ignore GROWUP_BONUS if we're already big
976 if (type == GROWUP_BONUS) {
977 if (player_status->bonus == GROWUP_BONUS)
979 if (player_status->bonus == FIRE_BONUS)
981 if (player_status->bonus == ICE_BONUS)
985 return set_bonus(type, animate);
989 Player::set_bonus(BonusType type, bool animate)
991 if((player_status->bonus == NO_BONUS) && (type != NO_BONUS)) {
992 if (!adjust_height(BIG_TUX_HEIGHT)) {
993 log_debug << "Can't adjust Tux height" << std::endl;
998 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
1000 if (climbing) stop_climbing(*climbing);
1003 if (type == NO_BONUS) {
1004 if (does_buttjump) does_buttjump = false;
1007 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
1008 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
1009 // visually lose helmet
1010 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
1011 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
1012 Vector paccel = Vector(0, 1000);
1013 std::string action = (dir==LEFT)?"left":"right";
1014 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
1015 if (climbing) stop_climbing(*climbing);
1017 if ((player_status->bonus == ICE_BONUS) && (animate)) {
1018 // visually lose cap
1019 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
1020 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
1021 Vector paccel = Vector(0, 1000);
1022 std::string action = (dir==LEFT)?"left":"right";
1023 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
1024 if (climbing) stop_climbing(*climbing);
1026 player_status->max_fire_bullets = 0;
1027 player_status->max_ice_bullets = 0;
1029 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
1030 if (type == ICE_BONUS) player_status->max_ice_bullets++;
1032 player_status->bonus = type;
1037 Player::set_visible(bool visible_)
1039 this->visible = visible_;
1041 set_group(COLGROUP_MOVING);
1043 set_group(COLGROUP_DISABLED);
1047 Player::get_visible()
1055 kick_timer.start(KICK_TIME);
1059 Player::draw(DrawingContext& context)
1064 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
1065 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
1066 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
1067 float py = Sector::current()->camera->get_translation().y;
1068 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
1069 context.draw_surface(airarrow, Vector(px, py), LAYER_HUD - 1);
1072 std::string sa_prefix = "";
1073 std::string sa_postfix = "";
1075 if (player_status->bonus == GROWUP_BONUS)
1077 else if (player_status->bonus == FIRE_BONUS)
1079 else if (player_status->bonus == ICE_BONUS)
1082 sa_prefix = "small";
1085 sa_postfix = "-left";
1087 sa_postfix = "-right";
1089 /* Set Tux sprite action */
1091 sprite->set_action("gameover");
1094 sprite->set_action_continued("grow"+sa_postfix);
1095 // while growing, do not change action
1096 // do_duck() will take care of cancelling growing manually
1097 // update() will take care of cancelling when growing completed
1099 else if (climbing) {
1100 sprite->set_action(sa_prefix+"-climbing"+sa_postfix);
1102 else if (backflipping) {
1103 sprite->set_action(sa_prefix+"-backflip"+sa_postfix);
1105 else if (duck && is_big()) {
1106 sprite->set_action(sa_prefix+"-duck"+sa_postfix);
1108 else if (skidding_timer.started() && !skidding_timer.check()) {
1109 sprite->set_action(sa_prefix+"-skid"+sa_postfix);
1111 else if (kick_timer.started() && !kick_timer.check()) {
1112 sprite->set_action(sa_prefix+"-kick"+sa_postfix);
1114 else if ((wants_buttjump || does_buttjump) && is_big()) {
1115 sprite->set_action(sa_prefix+"-buttjump"+sa_postfix);
1117 else if (!on_ground() || fall_mode != ON_GROUND) {
1118 if(physic.get_velocity_x() != 0 || fall_mode != ON_GROUND) {
1119 sprite->set_action(sa_prefix+"-jump"+sa_postfix);
1123 if (fabsf(physic.get_velocity_x()) < 1.0f) {
1124 // Determine which idle stage we're at
1125 if (sprite->get_action().find("-stand-") == std::string::npos && sprite->get_action().find("-idle-") == std::string::npos) {
1127 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1129 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1131 else if (idle_timer.check() || (IDLE_TIME[idle_stage] == 0 && sprite->animation_done())) {
1133 if (idle_stage >= IDLE_STAGE_COUNT)
1136 idle_timer.start(IDLE_TIME[idle_stage]/1000.0f);
1138 if (IDLE_TIME[idle_stage] == 0)
1139 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix, 1);
1141 sprite->set_action(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1144 sprite->set_action_continued(sa_prefix+("-" + IDLE_STAGES[idle_stage])+sa_postfix);
1148 sprite->set_action(sa_prefix+"-walk"+sa_postfix);
1153 // Tux is holding something
1154 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1155 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
1163 if (safe_timer.started() && size_t(game_time*40)%2)
1166 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1172 Player::collision_tile(uint32_t tile_attributes)
1174 if(tile_attributes & Tile::HURTS)
1179 if( tile_attributes & Tile::WATER ){
1185 if( tile_attributes & Tile::WATER ){
1188 SoundManager::current()->play( "sounds/splash.ogg" );
1193 if(tile_attributes & Tile::ICE) {
1194 ice_this_frame = true;
1200 Player::collision_solid(const CollisionHit& hit)
1203 if(physic.get_velocity_y() > 0)
1204 physic.set_velocity_y(0);
1206 on_ground_flag = true;
1207 floor_normal = hit.slope_normal;
1210 if (does_buttjump) {
1211 does_buttjump = false;
1212 physic.set_velocity_y(-300);
1213 on_ground_flag = false;
1214 Sector::current()->add_object(new Particles(
1215 Vector(get_bbox().p2.x, get_bbox().p2.y),
1217 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1219 Sector::current()->add_object(new Particles(
1220 Vector(get_bbox().p1.x, get_bbox().p2.y),
1222 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1224 Sector::current()->camera->shake(.1f, 0, 5);
1227 } else if(hit.top) {
1228 if(physic.get_velocity_y() < 0)
1229 physic.set_velocity_y(.2f);
1232 if(hit.left || hit.right) {
1233 physic.set_velocity_x(0);
1238 if(hit.left || hit.right) {
1240 } else if(hit.top || hit.bottom) {
1247 Player::collision(GameObject& other, const CollisionHit& hit)
1249 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1254 Player* player = dynamic_cast<Player*> (&other);
1259 if(hit.left || hit.right) {
1260 try_grab(); //grab objects right now, in update it will be too late
1262 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1263 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1264 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1265 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1267 if(controller->pressed(Controller::UP))
1268 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1274 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1275 if(badguy != NULL) {
1276 if(safe_timer.started() || invincible_timer.started())
1286 Player::make_invincible()
1288 SoundManager::current()->play("sounds/invincible_start.ogg");
1289 invincible_timer.start(TUX_INVINCIBLE_TIME);
1290 Sector::current()->play_music(HERRING_MUSIC);
1295 Player::kill(bool completely)
1297 if(dying || deactivated || is_winning() )
1300 if(!completely && (safe_timer.started() || invincible_timer.started()))
1305 if (climbing) stop_climbing(*climbing);
1307 physic.set_velocity_x(0);
1309 if(!completely && is_big()) {
1310 SoundManager::current()->play("sounds/hurt.wav");
1312 if(player_status->bonus == FIRE_BONUS
1313 || player_status->bonus == ICE_BONUS) {
1314 safe_timer.start(TUX_SAFE_TIME);
1315 set_bonus(GROWUP_BONUS, true);
1316 } else if(player_status->bonus == GROWUP_BONUS) {
1317 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1318 adjust_height(SMALL_TUX_HEIGHT);
1320 backflipping = false;
1321 sprite->set_angle(0.0f);
1322 set_bonus(NO_BONUS, true);
1323 } else if(player_status->bonus == NO_BONUS) {
1324 safe_timer.start(TUX_SAFE_TIME);
1325 adjust_height(SMALL_TUX_HEIGHT);
1329 SoundManager::current()->play("sounds/kill.wav");
1331 // do not die when in edit mode
1333 set_ghost_mode(true);
1337 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1339 for (int i = 0; i < 5; i++)
1341 // the numbers: starting x, starting y, velocity y
1342 Sector::current()->add_object(new FallingCoin(get_pos() +
1343 Vector(graphicsRandom.rand(5), graphicsRandom.rand(-32,18)),
1344 graphicsRandom.rand(-100,100)));
1346 player_status->coins -= std::max(player_status->coins/10, 25);
1350 GameSession::current()->set_reset_point("", Vector());
1352 physic.enable_gravity(true);
1353 physic.set_gravity_modifier(1.0f); // Undo jump_early_apex
1355 invincible_timer.stop();
1356 physic.set_acceleration(0, 0);
1357 physic.set_velocity(0, -700);
1358 set_bonus(NO_BONUS, true);
1360 dying_timer.start(3.0);
1361 set_group(COLGROUP_DISABLED);
1363 // TODO: need nice way to handle players dying in co-op mode
1364 Sector::current()->effect->fade_out(3.0);
1365 SoundManager::current()->stop_music(3.0);
1370 Player::move(const Vector& vector)
1374 // Reset size to get correct hitbox if Tux was eg. ducked before moving
1376 set_size(TUX_WIDTH, BIG_TUX_HEIGHT);
1378 set_size(TUX_WIDTH, SMALL_TUX_HEIGHT);
1380 backflipping = false;
1381 sprite->set_angle(0.0f);
1382 last_ground_y = vector.y;
1383 if (climbing) stop_climbing(*climbing);
1389 Player::check_bounds()
1391 /* Keep tux in sector bounds: */
1392 if (get_pos().x < 0) {
1393 // Lock Tux to the size of the level, so that he doesn't fall off
1395 set_pos(Vector(0, get_pos().y));
1398 if (get_bbox().get_right() > Sector::current()->get_width()) {
1399 // Lock Tux to the size of the level, so that he doesn't fall off
1401 set_pos(Vector(Sector::current()->get_width() - get_bbox().get_width(), get_pos().y));
1404 /* fallen out of the level? */
1405 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1412 Player::add_velocity(const Vector& velocity)
1414 physic.set_velocity(physic.get_velocity() + velocity);
1418 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1420 if (end_speed.x > 0)
1421 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1422 if (end_speed.x < 0)
1423 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1424 if (end_speed.y > 0)
1425 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1426 if (end_speed.y < 0)
1427 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1431 Player::get_velocity()
1433 return physic.get_velocity();
1437 Player::bounce(BadGuy& )
1439 if(controller->hold(Controller::JUMP))
1440 physic.set_velocity_y(-520);
1442 physic.set_velocity_y(-300);
1445 //scripting Functions Below
1448 Player::deactivate()
1453 physic.set_velocity_x(0);
1454 physic.set_velocity_y(0);
1455 physic.set_acceleration_x(0);
1456 physic.set_acceleration_y(0);
1457 if (climbing) stop_climbing(*climbing);
1465 deactivated = false;
1468 void Player::walk(float speed)
1470 physic.set_velocity_x(speed);
1473 void Player::set_dir(bool right)
1475 dir = right ? RIGHT : LEFT;
1479 Player::set_ghost_mode(bool enable)
1481 if (ghost_mode == enable)
1484 if (climbing) stop_climbing(*climbing);
1488 set_group(COLGROUP_DISABLED);
1489 physic.enable_gravity(false);
1490 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1493 set_group(COLGROUP_MOVING);
1494 physic.enable_gravity(true);
1495 log_debug << "You feel solid again." << std::endl;
1500 Player::set_edit_mode(bool enable)
1506 Player::start_climbing(Climbable& climbable)
1508 if (climbing || !&climbable) return;
1510 climbing = &climbable;
1511 physic.enable_gravity(false);
1512 physic.set_velocity(0, 0);
1513 physic.set_acceleration(0, 0);
1515 backflipping = false;
1516 backflip_direction = 0;
1517 sprite->set_angle(0.0f);
1522 Player::stop_climbing(Climbable& /*climbable*/)
1524 if (!climbing) return;
1528 if (grabbed_object) {
1529 grabbed_object->ungrab(*this, dir);
1530 grabbed_object = NULL;
1533 physic.enable_gravity(true);
1534 physic.set_velocity(0, 0);
1535 physic.set_acceleration(0, 0);
1537 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1538 on_ground_flag = true;
1539 // TODO: This won't help. Why?
1545 Player::handle_input_climbing()
1548 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1554 if (controller->hold(Controller::LEFT)) {
1558 if (controller->hold(Controller::RIGHT)) {
1562 if (controller->hold(Controller::UP)) {
1565 if (controller->hold(Controller::DOWN)) {
1568 if (controller->hold(Controller::JUMP)) {
1570 stop_climbing(*climbing);
1576 if (controller->hold(Controller::ACTION)) {
1577 stop_climbing(*climbing);
1580 physic.set_velocity(vx, vy);
1581 physic.set_acceleration(0, 0);