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"
56 static const int TILES_FOR_BUTTJUMP = 3;
57 static const float BUTTJUMP_MIN_VELOCITY_Y = 400.0f;
58 static const float SHOOTING_TIME = .150f;
59 /// time before idle animation starts
60 static const float IDLE_TIME = 2.5f;
62 /** acceleration in horizontal direction when walking
63 * (all acceleratiosn are in pixel/s^2) */
64 static const float WALK_ACCELERATION_X = 300;
65 /** acceleration in horizontal direction when running */
66 static const float RUN_ACCELERATION_X = 400;
67 /** acceleration when skidding */
68 static const float SKID_XM = 200;
69 /** time of skidding in seconds */
70 static const float SKID_TIME = .3f;
71 /** maximum walk velocity (pixel/s) */
72 static const float MAX_WALK_XM = 230;
73 /** maximum run velocity (pixel/s) */
74 static const float MAX_RUN_XM = 320;
75 /** maximum horizontal climb velocity */
76 static const float MAX_CLIMB_XM = 48;
77 /** maximum vertical climb velocity */
78 static const float MAX_CLIMB_YM = 128;
79 /** instant velocity when tux starts to walk */
80 static const float WALK_SPEED = 100;
82 /** time of the kick (kicking mriceblock) animation */
83 static const float KICK_TIME = .3f;
84 /** time of tux cheering (currently unused) */
85 static const float CHEER_TIME = 1.0f;
87 /** if Tux cannot unduck for this long, he will get hurt */
88 static const float UNDUCK_HURT_TIME = 0.25f;
89 /** gravity is higher after the jump key is released before
90 the apex of the jump is reached */
91 static const float JUMP_EARLY_APEX_FACTOR = 3.0;
97 Player::Player(PlayerStatus* _player_status, const std::string& name)
98 : scripting_controller(0),
99 player_status(_player_status),
100 scripting_controller_old(0),
101 grabbed_object(NULL), ghost_mode(false), edit_mode(false), climbing(0)
104 controller = main_controller;
105 scripting_controller = new CodeController();
106 sprite = sprite_manager->create("images/creatures/tux/tux.sprite");
107 airarrow.reset(new Surface("images/engine/hud/airarrow.png"));
109 sound_manager->preload("sounds/bigjump.wav");
110 sound_manager->preload("sounds/jump.wav");
111 sound_manager->preload("sounds/hurt.wav");
112 sound_manager->preload("sounds/skid.wav");
113 sound_manager->preload("sounds/flip.wav");
114 sound_manager->preload("sounds/invincible.wav");
115 sound_manager->preload("sounds/splash.ogg");
116 sound_manager->preload("sounds/shoot.wav");
123 if (climbing) stop_climbing(*climbing);
125 delete scripting_controller;
132 set_size(31.8f, 62.8f);
134 set_size(31.8f, 30.8f);
144 fall_mode = ON_GROUND;
146 jump_early_apex = false;
148 wants_buttjump = false;
149 does_buttjump = false;
152 backflipping = false;
153 backflip_direction = 0;
156 speedlimit = 0; //no special limit
158 on_ground_flag = false;
159 grabbed_object = NULL;
167 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
172 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
176 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
181 Scripting::unexpose_object(vm, table_idx, name);
185 Player::get_speedlimit()
191 Player::set_speedlimit(float newlimit)
197 Player::set_controller(Controller* controller)
199 this->controller = controller;
203 Player::use_scripting_controller(bool use_or_release)
205 if ((use_or_release == true) && (controller != scripting_controller)) {
206 scripting_controller_old = get_controller();
207 set_controller(scripting_controller);
209 if ((use_or_release == false) && (controller == scripting_controller)) {
210 set_controller(scripting_controller_old);
211 scripting_controller_old = 0;
216 Player::do_scripting_controller(std::string control, bool pressed)
218 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
219 if(control == std::string(Controller::controlNames[i])) {
220 scripting_controller->press(Controller::Control(i), pressed);
226 Player::adjust_height(float new_height)
229 bbox2.move(Vector(0, bbox.get_height() - new_height));
230 bbox2.set_height(new_height);
232 if(new_height > bbox.get_height()) {
233 Rect additional_space = bbox2;
234 additional_space.set_height(new_height - bbox.get_height());
235 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
239 // adjust bbox accordingly
240 // note that we use members of moving_object for this, so we can run this during CD, too
242 set_size(bbox2.get_width(), bbox2.get_height());
247 Player::trigger_sequence(std::string sequence_name)
249 if (climbing) stop_climbing(*climbing);
250 GameSession::current()->start_sequence(sequence_name);
254 Player::update(float elapsed_time)
261 if(dying && dying_timer.check()) {
266 if(!dying && !deactivated)
269 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
273 // extend/shrink tux collision rectangle so that we fall through/walk over 1
275 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
281 // on downward slopes, adjust vertical velocity so tux walks smoothly down
283 if(floor_normal.y != 0) {
284 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
285 physic.set_velocity_y(250);
290 // handle backflipping
292 //prevent player from changing direction when backflipping
293 dir = (backflip_direction == 1) ? LEFT : RIGHT;
294 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
299 fall_mode = ON_GROUND;
300 last_ground_y = get_pos().y;
302 if(get_pos().y > last_ground_y)
304 else if(fall_mode == ON_GROUND)
308 // check if we landed
311 if (backflipping && (!backflip_timer.started())) {
312 backflipping = false;
313 backflip_direction = 0;
315 // if controls are currently deactivated, we take care of standing up ourselves
321 // calculate movement for this frame
322 movement = physic.get_movement(elapsed_time);
324 if(grabbed_object != NULL && !dying) {
325 Vector pos = get_pos() +
326 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
327 grabbed_object->grab(*this, pos, dir);
330 if(grabbed_object != NULL && dying){
331 grabbed_object->ungrab(*this, dir);
332 grabbed_object = NULL;
335 on_ground_flag = false;
337 // when invincible, spawn particles
338 if (invincible_timer.started() && !dying)
340 if (systemRandom.rand(0, 2) == 0) {
341 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
342 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
343 Vector ppos = Vector(px, py);
344 Vector pspeed = Vector(0, 0);
345 Vector paccel = Vector(0, 0);
346 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
347 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
348 // make every other a longer sparkle to make trail a bit fuzzy
349 if (size_t(game_time*20)%2) {
350 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
352 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
355 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
361 if (sprite->animation_done()) growing = false;
369 return on_ground_flag;
375 if(player_status->bonus == NO_BONUS)
382 Player::apply_friction()
384 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
385 physic.set_velocity_x(0);
386 physic.set_acceleration_x(0);
387 } else if(physic.get_velocity_x() < 0) {
388 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
389 } else if(physic.get_velocity_x() > 0) {
390 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
395 Player::handle_horizontal_input()
397 float vx = physic.get_velocity_x();
398 float vy = physic.get_velocity_y();
399 float ax = physic.get_acceleration_x();
400 float ay = physic.get_acceleration_y();
403 if(!duck || physic.get_velocity_y() != 0) {
404 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
408 } else if(!controller->hold(Controller::LEFT)
409 && controller->hold(Controller::RIGHT)) {
416 // do not run if action key is pressed or we're holding something
417 // so tux can only walk while shooting
418 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
419 ax = dirsign * WALK_ACCELERATION_X;
421 if(vx >= MAX_WALK_XM && dirsign > 0) {
424 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
429 if( vx * dirsign < MAX_WALK_XM ) {
430 ax = dirsign * WALK_ACCELERATION_X;
432 ax = dirsign * RUN_ACCELERATION_X;
435 if(vx >= MAX_RUN_XM && dirsign > 0) {
438 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
444 // we can reach WALK_SPEED without any acceleration
445 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
446 vx = dirsign * WALK_SPEED;
450 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
451 vx = dirsign * speedlimit;
455 // changing directions?
456 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
458 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
459 skidding_timer.start(SKID_TIME);
460 sound_manager->play("sounds/skid.wav");
461 // dust some particles
462 Sector::current()->add_object(
464 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
465 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
466 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
475 physic.set_velocity(vx, vy);
476 physic.set_acceleration(ax, ay);
478 // we get slower when not pressing any keys
500 if (physic.get_velocity_y() != 0)
507 if (adjust_height(31.8f)) {
510 unduck_hurt_timer.stop();
517 Player::do_standup() {
525 if (adjust_height(63.8f)) {
527 unduck_hurt_timer.stop();
529 // if timer is not already running, start it.
530 if (unduck_hurt_timer.get_period() == 0) {
531 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
533 else if (unduck_hurt_timer.check()) {
541 Player::do_backflip() {
547 backflip_direction = (dir == LEFT)?(+1):(-1);
550 sound_manager->play("sounds/flip.wav");
551 backflip_timer.start(0.15f);
555 Player::do_jump(float yspeed) {
559 physic.set_velocity_y(yspeed);
560 //bbox.move(Vector(0, -1));
562 on_ground_flag = false;
567 sound_manager->play("sounds/bigjump.wav");
569 sound_manager->play("sounds/jump.wav");
574 Player::early_jump_apex() {
575 if(jump_early_apex) {
578 jump_early_apex = true;
579 physic.set_gravity(physic.get_gravity() * JUMP_EARLY_APEX_FACTOR);
583 Player::do_jump_apex() {
584 if(!jump_early_apex) {
587 jump_early_apex = false;
588 physic.set_gravity(physic.get_gravity() / JUMP_EARLY_APEX_FACTOR);
592 Player::handle_vertical_input()
595 if(controller->pressed(Controller::JUMP) && (can_jump)) {
597 // when running, only jump a little bit; else do a backflip
598 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
600 // jump a bit higher if we are running; else do a normal jump
601 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
604 // Let go of jump key
605 else if(!controller->hold(Controller::JUMP)) {
606 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
612 if(jump_early_apex && physic.get_velocity_y() >= 0) {
616 /* In case the player has pressed Down while in a certain range of air,
617 enable butt jump action */
618 if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
619 wants_buttjump = true;
620 if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
623 /* When Down is not held anymore, disable butt jump */
624 if(!controller->hold(Controller::DOWN)) {
625 wants_buttjump = false;
626 does_buttjump = false;
630 physic.set_acceleration_y(0);
633 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
634 physic.set_acceleration_y(-2000);
635 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
641 Player::handle_input()
644 handle_input_ghost();
648 handle_input_climbing();
653 if( controller->released( Controller::PEEK_LEFT ) ) {
656 if( controller->released( Controller::PEEK_RIGHT ) ) {
659 if( controller->released( Controller::UP ) ) {
662 if( controller->released( Controller::DOWN ) ) {
665 if( controller->pressed( Controller::PEEK_LEFT ) ) {
668 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
671 if( controller->pressed( Controller::UP ) ) {
674 if( controller->pressed( Controller::DOWN ) ) {
678 /* Handle horizontal movement: */
679 if (!backflipping) handle_horizontal_input();
682 if (on_ground() && !controller->hold(Controller::JUMP))
685 /* Handle vertical movement: */
686 handle_vertical_input();
689 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
690 if(Sector::current()->add_bullet(
691 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
692 : Vector(32, bbox.get_height()/2)),
693 physic.get_velocity_x(), dir))
694 shooting_timer.start(SHOOTING_TIME);
697 /* Duck or Standup! */
698 if (controller->hold(Controller::DOWN)) {
707 if(!controller->hold(Controller::ACTION) && grabbed_object) {
708 // move the grabbed object a bit away from tux
709 Vector pos = get_pos() +
710 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
711 bbox.get_height()*0.66666 - 32);
712 Rect dest(pos, pos + Vector(32, 32));
713 if(Sector::current()->is_free_of_movingstatics(dest)) {
714 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
716 moving_object->set_pos(pos);
718 log_debug << "Non MovingObject grabbed?!?" << std::endl;
720 if(controller->hold(Controller::UP)) {
721 grabbed_object->ungrab(*this, UP);
723 grabbed_object->ungrab(*this, dir);
725 grabbed_object = NULL;
733 if(controller->hold(Controller::ACTION) && !grabbed_object
735 Sector* sector = Sector::current();
738 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
740 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
743 for(Sector::Portables::iterator i = sector->portables.begin();
744 i != sector->portables.end(); ++i) {
745 Portable* portable = *i;
746 if(!portable->is_portable())
749 // make sure the Portable is a MovingObject
750 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
751 assert(moving_object);
752 if(moving_object == NULL)
755 // make sure the Portable isn't currently non-solid
756 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
758 // check if we are within reach
759 if(moving_object->get_bbox().contains(pos)) {
760 if (climbing) stop_climbing(*climbing);
761 grabbed_object = portable;
762 grabbed_object->grab(*this, get_pos(), dir);
770 Player::handle_input_ghost()
774 if (controller->hold(Controller::LEFT)) {
776 vx -= MAX_RUN_XM * 2;
778 if (controller->hold(Controller::RIGHT)) {
780 vx += MAX_RUN_XM * 2;
782 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
783 vy -= MAX_RUN_XM * 2;
785 if (controller->hold(Controller::DOWN)) {
786 vy += MAX_RUN_XM * 2;
788 if (controller->hold(Controller::ACTION)) {
789 set_ghost_mode(false);
791 physic.set_velocity(vx, vy);
792 physic.set_acceleration(0, 0);
796 Player::add_coins(int count)
798 player_status->add_coins(count);
804 return player_status->coins;
808 Player::add_bonus(const std::string& bonustype)
810 BonusType type = NO_BONUS;
812 if(bonustype == "grow") {
814 } else if(bonustype == "fireflower") {
816 } else if(bonustype == "iceflower") {
818 } else if(bonustype == "none") {
821 std::ostringstream msg;
822 msg << "Unknown bonus type " << bonustype;
823 throw std::runtime_error(msg.str());
826 return add_bonus(type);
830 Player::add_bonus(BonusType type, bool animate)
832 // always ignore NO_BONUS
833 if (type == NO_BONUS) {
837 // ignore GROWUP_BONUS if we're already big
838 if (type == GROWUP_BONUS) {
839 if (player_status->bonus == GROWUP_BONUS)
841 if (player_status->bonus == FIRE_BONUS)
843 if (player_status->bonus == ICE_BONUS)
847 return set_bonus(type, animate);
851 Player::set_bonus(BonusType type, bool animate)
853 if(player_status->bonus == NO_BONUS) {
854 if (!adjust_height(62.8f)) {
855 printf("can't adjust\n");
860 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
862 if (climbing) stop_climbing(*climbing);
865 if (type == NO_BONUS) {
866 if (does_buttjump) does_buttjump = false;
869 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
870 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
871 // visually lose helmet
872 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
873 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
874 Vector paccel = Vector(0, 1000);
875 std::string action = (dir==LEFT)?"left":"right";
876 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
877 if (climbing) stop_climbing(*climbing);
879 if ((player_status->bonus == ICE_BONUS) && (animate)) {
881 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
882 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
883 Vector paccel = Vector(0, 1000);
884 std::string action = (dir==LEFT)?"left":"right";
885 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
886 if (climbing) stop_climbing(*climbing);
888 player_status->max_fire_bullets = 0;
889 player_status->max_ice_bullets = 0;
891 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
892 if (type == ICE_BONUS) player_status->max_ice_bullets++;
894 player_status->bonus = type;
899 Player::set_visible(bool visible)
901 this->visible = visible;
903 set_group(COLGROUP_MOVING);
905 set_group(COLGROUP_DISABLED);
909 Player::get_visible()
917 kick_timer.start(KICK_TIME);
921 Player::draw(DrawingContext& context)
926 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
927 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
928 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
929 float py = Sector::current()->camera->get_translation().y;
930 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
931 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
934 std::string sa_prefix = "";
935 if (player_status->bonus == GROWUP_BONUS)
937 else if (player_status->bonus == FIRE_BONUS)
939 else if (player_status->bonus == ICE_BONUS)
944 /* Set Tux sprite action */
946 sprite->set_action_continued((dir == LEFT)?"grow-left":"grow-right");
947 // while growing, do not change action
948 // do_duck() will take care of cancelling growing manually
949 // update() will take care of cancelling when growing completed
952 sprite->set_action(sa_prefix+((dir == LEFT)?"-skid-left":"-skid-right"));
954 else if (backflipping) {
955 sprite->set_action(sa_prefix+((dir == LEFT)?"-backflip-left":"-backflip-right"));
957 else if (duck && is_big()) {
958 sprite->set_action(sa_prefix+((dir == LEFT)?"-duck-left":"-duck-right"));
960 else if (skidding_timer.started() && !skidding_timer.check()) {
961 sprite->set_action(sa_prefix+((dir == LEFT)?"-skid-left":"-skid-right"));
963 else if (kick_timer.started() && !kick_timer.check()) {
964 sprite->set_action(sa_prefix+((dir == LEFT)?"-kick-left":"-kick-right"));
966 else if ((wants_buttjump || does_buttjump) && is_big()) {
967 sprite->set_action(sa_prefix+((dir == LEFT)?"-buttjump-left":"-buttjump-right"));
969 else if (!on_ground()) {
970 sprite->set_action(sa_prefix+((dir == LEFT)?"-jump-left":"-jump-right"));
973 if (fabsf(physic.get_velocity_x()) < 1.0f) {
974 // if(idle_timer.check()) {
975 // sprite->set_action(sa_prefix+((dir == LEFT)?"-idle-left":"-idle-right"));
977 sprite->set_action(sa_prefix+((dir == LEFT)?"-stand-left":"-stand-right"));
981 sprite->set_action(sa_prefix+((dir == LEFT)?"-walk-left":"-walk-right"));
987 // Tux is holding something
988 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
989 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
997 sprite->set_action("gameover");
1001 if (safe_timer.started() && size_t(game_time*40)%2)
1004 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
1010 Player::collision_tile(uint32_t tile_attributes)
1012 if(tile_attributes & Tile::HURTS)
1017 if( tile_attributes & Tile::WATER ){
1023 if( tile_attributes & Tile::WATER ){
1026 sound_manager->play( "sounds/splash.ogg" );
1033 Player::collision_solid(const CollisionHit& hit)
1036 if(physic.get_velocity_y() > 0)
1037 physic.set_velocity_y(0);
1039 on_ground_flag = true;
1040 floor_normal = hit.slope_normal;
1043 if (does_buttjump) {
1044 does_buttjump = false;
1045 physic.set_velocity_y(-300);
1046 on_ground_flag = false;
1047 Sector::current()->add_object(new Particles(
1048 Vector(get_bbox().p2.x, get_bbox().p2.y),
1050 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1052 Sector::current()->add_object(new Particles(
1053 Vector(get_bbox().p1.x, get_bbox().p2.y),
1055 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1059 } else if(hit.top) {
1060 if(physic.get_velocity_y() < 0)
1061 physic.set_velocity_y(.2f);
1064 if(hit.left || hit.right) {
1065 physic.set_velocity_x(0);
1070 if(hit.left || hit.right) {
1072 } else if(hit.top || hit.bottom) {
1079 Player::collision(GameObject& other, const CollisionHit& hit)
1081 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1086 if(hit.left || hit.right) {
1087 try_grab(); //grab objects right now, in update it will be too late
1090 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1092 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1093 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1094 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1096 if(controller->pressed(Controller::UP))
1097 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1103 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1104 if(badguy != NULL) {
1105 if(safe_timer.started() || invincible_timer.started())
1115 Player::make_invincible()
1117 sound_manager->play("sounds/invincible.wav");
1118 invincible_timer.start(TUX_INVINCIBLE_TIME);
1119 Sector::current()->play_music(HERRING_MUSIC);
1124 Player::kill(bool completely)
1126 if(dying || deactivated)
1129 if(!completely && (safe_timer.started() || invincible_timer.started()))
1134 sound_manager->play("sounds/hurt.wav");
1136 if (climbing) stop_climbing(*climbing);
1138 physic.set_velocity_x(0);
1140 if(!completely && is_big()) {
1141 if(player_status->bonus == FIRE_BONUS
1142 || player_status->bonus == ICE_BONUS) {
1143 safe_timer.start(TUX_SAFE_TIME);
1144 set_bonus(GROWUP_BONUS, true);
1145 } else if(player_status->bonus == GROWUP_BONUS) {
1146 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1147 adjust_height(30.8f);
1149 backflipping = false;
1150 set_bonus(NO_BONUS, true);
1151 } else if(player_status->bonus == NO_BONUS) {
1152 safe_timer.start(TUX_SAFE_TIME);
1153 adjust_height(30.8f);
1158 // do not die when in edit mode
1160 set_ghost_mode(true);
1164 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1166 for (int i = 0; i < 5; i++)
1168 // the numbers: starting x, starting y, velocity y
1169 Sector::current()->add_object(new FallingCoin(get_pos() +
1170 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1171 systemRandom.rand(-100,100)));
1173 player_status->coins -= std::max(player_status->coins/10, 25);
1177 GameSession::current()->set_reset_point("", Vector());
1179 physic.enable_gravity(true);
1180 physic.set_acceleration(0, 0);
1181 physic.set_velocity(0, -700);
1182 set_bonus(NO_BONUS, true);
1184 dying_timer.start(3.0);
1185 set_group(COLGROUP_DISABLED);
1187 DisplayEffect* effect = new DisplayEffect();
1188 effect->fade_out(3.0);
1189 Sector::current()->add_object(effect);
1190 sound_manager->stop_music(3.0);
1195 Player::move(const Vector& vector)
1199 // TODO: do we need the following? Seems irrelevant to moving the player
1201 set_size(31.8f, 63.8f);
1203 set_size(31.8f, 31.8f);
1205 last_ground_y = vector.y;
1206 if (climbing) stop_climbing(*climbing);
1212 Player::check_bounds(Camera* camera)
1214 /* Keep tux in bounds: */
1215 if (get_pos().x < 0) {
1216 // Lock Tux to the size of the level, so that he doesn't fall of
1218 set_pos(Vector(0, get_pos().y));
1221 /* fallen out of the level? */
1222 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1227 // can happen if back scrolling is disabled
1228 if(get_pos().x < camera->get_translation().x) {
1229 set_pos(Vector(camera->get_translation().x, get_pos().y));
1231 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1234 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1240 Player::add_velocity(const Vector& velocity)
1242 physic.set_velocity(physic.get_velocity() + velocity);
1246 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1248 if (end_speed.x > 0)
1249 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1250 if (end_speed.x < 0)
1251 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1252 if (end_speed.y > 0)
1253 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1254 if (end_speed.y < 0)
1255 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1259 Player::get_velocity()
1261 return physic.get_velocity();
1265 Player::bounce(BadGuy& )
1267 if(controller->hold(Controller::JUMP))
1268 physic.set_velocity_y(-520);
1270 physic.set_velocity_y(-300);
1273 //Scripting Functions Below
1276 Player::deactivate()
1281 physic.set_velocity_x(0);
1282 physic.set_velocity_y(0);
1283 physic.set_acceleration_x(0);
1284 physic.set_acceleration_y(0);
1285 if (climbing) stop_climbing(*climbing);
1293 deactivated = false;
1296 void Player::walk(float speed)
1298 physic.set_velocity_x(speed);
1302 Player::set_ghost_mode(bool enable)
1304 if (ghost_mode == enable)
1307 if (climbing) stop_climbing(*climbing);
1311 set_group(COLGROUP_DISABLED);
1312 physic.enable_gravity(false);
1313 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1316 set_group(COLGROUP_MOVING);
1317 physic.enable_gravity(true);
1318 log_debug << "You feel solid again." << std::endl;
1324 Player::set_edit_mode(bool enable)
1330 Player::start_climbing(Climbable& climbable)
1332 if (climbing == &climbable) return;
1334 climbing = &climbable;
1335 physic.enable_gravity(false);
1336 physic.set_velocity(0, 0);
1337 physic.set_acceleration(0, 0);
1341 Player::stop_climbing(Climbable& /*climbable*/)
1343 if (!climbing) return;
1347 if (grabbed_object) {
1348 grabbed_object->ungrab(*this, dir);
1349 grabbed_object = NULL;
1352 physic.enable_gravity(true);
1353 physic.set_velocity(0, 0);
1354 physic.set_acceleration(0, 0);
1356 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1357 on_ground_flag = true;
1358 // TODO: This won't help. Why?
1364 Player::handle_input_climbing()
1367 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1373 if (controller->hold(Controller::LEFT)) {
1377 if (controller->hold(Controller::RIGHT)) {
1381 if (controller->hold(Controller::UP)) {
1384 if (controller->hold(Controller::DOWN)) {
1387 if (controller->hold(Controller::JUMP)) {
1389 stop_climbing(*climbing);
1395 if (controller->hold(Controller::ACTION)) {
1396 stop_climbing(*climbing);
1399 physic.set_velocity(vx, vy);
1400 physic.set_acceleration(0, 0);