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 velcoity (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;
94 Player::Player(PlayerStatus* _player_status, const std::string& name)
95 : scripting_controller(0),
96 player_status(_player_status),
97 scripting_controller_old(0),
98 grabbed_object(NULL), ghost_mode(false), edit_mode(false), climbing(0)
101 controller = main_controller;
102 scripting_controller = new CodeController();
103 sprite = sprite_manager->create("images/creatures/tux/tux.sprite");
104 airarrow.reset(new Surface("images/engine/hud/airarrow.png"));
106 sound_manager->preload("sounds/bigjump.wav");
107 sound_manager->preload("sounds/jump.wav");
108 sound_manager->preload("sounds/hurt.wav");
109 sound_manager->preload("sounds/skid.wav");
110 sound_manager->preload("sounds/flip.wav");
111 sound_manager->preload("sounds/invincible.wav");
112 sound_manager->preload("sounds/splash.ogg");
113 sound_manager->preload("sounds/shoot.wav");
120 if (climbing) stop_climbing(*climbing);
122 delete scripting_controller;
129 set_size(31.8f, 62.8f);
131 set_size(31.8f, 30.8f);
141 fall_mode = ON_GROUND;
144 wants_buttjump = false;
145 does_buttjump = false;
148 backflipping = false;
149 backflip_direction = 0;
152 speedlimit = 0; //no special limit
154 on_ground_flag = false;
155 grabbed_object = NULL;
163 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
168 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
172 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
177 Scripting::unexpose_object(vm, table_idx, name);
181 Player::get_speedlimit()
187 Player::set_speedlimit(float newlimit)
193 Player::set_controller(Controller* controller)
195 this->controller = controller;
199 Player::use_scripting_controller(bool use_or_release)
201 if ((use_or_release == true) && (controller != scripting_controller)) {
202 scripting_controller_old = get_controller();
203 set_controller(scripting_controller);
205 if ((use_or_release == false) && (controller == scripting_controller)) {
206 set_controller(scripting_controller_old);
207 scripting_controller_old = 0;
212 Player::do_scripting_controller(std::string control, bool pressed)
214 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
215 if(control == std::string(Controller::controlNames[i])) {
216 scripting_controller->press(Controller::Control(i), pressed);
222 Player::adjust_height(float new_height)
225 bbox2.move(Vector(0, bbox.get_height() - new_height));
226 bbox2.set_height(new_height);
228 if(new_height > bbox.get_height()) {
229 Rect additional_space = bbox2;
230 additional_space.set_height(new_height - bbox.get_height());
231 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
235 // adjust bbox accordingly
236 // note that we use members of moving_object for this, so we can run this during CD, too
238 set_size(bbox2.get_width(), bbox2.get_height());
243 Player::trigger_sequence(std::string sequence_name)
245 if (climbing) stop_climbing(*climbing);
246 GameSession::current()->start_sequence(sequence_name);
250 Player::update(float elapsed_time)
257 if(dying && dying_timer.check()) {
262 if(!dying && !deactivated)
265 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
269 // extend/shrink tux collision rectangle so that we fall through/walk over 1
271 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
277 // on downward slopes, adjust vertical velocity so tux walks smoothly down
279 if(floor_normal.y != 0) {
280 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
281 physic.set_velocity_y(250);
286 // handle backflipping
288 //prevent player from changing direction when backflipping
289 dir = (backflip_direction == 1) ? LEFT : RIGHT;
290 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
295 fall_mode = ON_GROUND;
296 last_ground_y = get_pos().y;
298 if(get_pos().y > last_ground_y)
300 else if(fall_mode == ON_GROUND)
304 // check if we landed
307 if (backflipping && (!backflip_timer.started())) {
308 backflipping = false;
309 backflip_direction = 0;
311 // if controls are currently deactivated, we take care of standing up ourselves
317 // calculate movement for this frame
318 movement = physic.get_movement(elapsed_time);
320 if(grabbed_object != NULL && !dying) {
321 Vector pos = get_pos() +
322 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
323 grabbed_object->grab(*this, pos, dir);
326 if(grabbed_object != NULL && dying){
327 grabbed_object->ungrab(*this, dir);
328 grabbed_object = NULL;
331 on_ground_flag = false;
333 // when invincible, spawn particles
334 if (invincible_timer.started() && !dying)
336 if (systemRandom.rand(0, 2) == 0) {
337 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
338 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
339 Vector ppos = Vector(px, py);
340 Vector pspeed = Vector(0, 0);
341 Vector paccel = Vector(0, 0);
342 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
343 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
344 // make every other a longer sparkle to make trail a bit fuzzy
345 if (size_t(game_time*20)%2) {
346 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
348 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
351 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
357 if (sprite->animation_done()) growing = false;
365 return on_ground_flag;
371 if(player_status->bonus == NO_BONUS)
378 Player::apply_friction()
380 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
381 physic.set_velocity_x(0);
382 physic.set_acceleration_x(0);
383 } else if(physic.get_velocity_x() < 0) {
384 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
385 } else if(physic.get_velocity_x() > 0) {
386 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
391 Player::handle_horizontal_input()
393 float vx = physic.get_velocity_x();
394 float vy = physic.get_velocity_y();
395 float ax = physic.get_acceleration_x();
396 float ay = physic.get_acceleration_y();
399 if(!duck || physic.get_velocity_y() != 0) {
400 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
404 } else if(!controller->hold(Controller::LEFT)
405 && controller->hold(Controller::RIGHT)) {
412 // do not run if action key is pressed or we're holding something
413 // so tux can only walk while shooting
414 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
415 ax = dirsign * WALK_ACCELERATION_X;
417 if(vx >= MAX_WALK_XM && dirsign > 0) {
420 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
425 if( vx * dirsign < MAX_WALK_XM ) {
426 ax = dirsign * WALK_ACCELERATION_X;
428 ax = dirsign * RUN_ACCELERATION_X;
431 if(vx >= MAX_RUN_XM && dirsign > 0) {
434 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
440 // we can reach WALK_SPEED without any acceleration
441 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
442 vx = dirsign * WALK_SPEED;
446 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
447 vx = dirsign * speedlimit;
451 // changing directions?
452 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
454 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
455 skidding_timer.start(SKID_TIME);
456 sound_manager->play("sounds/skid.wav");
457 // dust some particles
458 Sector::current()->add_object(
460 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
461 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
462 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
471 physic.set_velocity(vx, vy);
472 physic.set_acceleration(ax, ay);
474 // we get slower when not pressing any keys
496 if (physic.get_velocity_y() != 0)
503 if (adjust_height(31.8f)) {
506 unduck_hurt_timer.stop();
513 Player::do_standup() {
521 if (adjust_height(63.8f)) {
523 unduck_hurt_timer.stop();
525 // if timer is not already running, start it.
526 if (unduck_hurt_timer.get_period() == 0) {
527 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
529 else if (unduck_hurt_timer.check()) {
537 Player::do_backflip() {
543 backflip_direction = (dir == LEFT)?(+1):(-1);
546 sound_manager->play("sounds/flip.wav");
547 backflip_timer.start(0.15f);
551 Player::do_jump(float yspeed) {
555 physic.set_velocity_y(yspeed);
556 //bbox.move(Vector(0, -1));
558 on_ground_flag = false;
563 sound_manager->play("sounds/bigjump.wav");
565 sound_manager->play("sounds/jump.wav");
570 Player::handle_vertical_input()
573 if(controller->pressed(Controller::JUMP) && (can_jump)) {
575 // when running, only jump a little bit; else do a backflip
576 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
578 // jump a bit higher if we are running; else do a normal jump
579 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
582 // Let go of jump key
583 else if(!controller->hold(Controller::JUMP)) {
584 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
586 physic.set_velocity_y(0);
590 /* In case the player has pressed Down while in a certain range of air,
591 enable butt jump action */
592 if (controller->hold(Controller::DOWN) && !duck && is_big() && !on_ground()) {
593 wants_buttjump = true;
594 if (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y) does_buttjump = true;
597 /* When Down is not held anymore, disable butt jump */
598 if(!controller->hold(Controller::DOWN)) {
599 wants_buttjump = false;
600 does_buttjump = false;
604 physic.set_acceleration_y(0);
607 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
608 physic.set_acceleration_y(-2000);
609 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
615 Player::handle_input()
618 handle_input_ghost();
622 handle_input_climbing();
627 if( controller->released( Controller::PEEK_LEFT ) ) {
630 if( controller->released( Controller::PEEK_RIGHT ) ) {
633 if( controller->released( Controller::UP ) ) {
636 if( controller->released( Controller::DOWN ) ) {
639 if( controller->pressed( Controller::PEEK_LEFT ) ) {
642 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
645 if( controller->pressed( Controller::UP ) ) {
648 if( controller->pressed( Controller::DOWN ) ) {
652 /* Handle horizontal movement: */
653 if (!backflipping) handle_horizontal_input();
656 if (on_ground() && !controller->hold(Controller::JUMP))
659 /* Handle vertical movement: */
660 handle_vertical_input();
663 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
664 if(Sector::current()->add_bullet(
665 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
666 : Vector(32, bbox.get_height()/2)),
667 physic.get_velocity_x(), dir))
668 shooting_timer.start(SHOOTING_TIME);
671 /* Duck or Standup! */
672 if (controller->hold(Controller::DOWN)) {
681 if(!controller->hold(Controller::ACTION) && grabbed_object) {
682 // move the grabbed object a bit away from tux
683 Vector pos = get_pos() +
684 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
685 bbox.get_height()*0.66666 - 32);
686 Rect dest(pos, pos + Vector(32, 32));
687 if(Sector::current()->is_free_of_movingstatics(dest)) {
688 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
690 moving_object->set_pos(pos);
692 log_debug << "Non MovingObject grabbed?!?" << std::endl;
694 if(controller->hold(Controller::UP)) {
695 grabbed_object->ungrab(*this, UP);
697 grabbed_object->ungrab(*this, dir);
699 grabbed_object = NULL;
707 if(controller->hold(Controller::ACTION) && !grabbed_object
709 Sector* sector = Sector::current();
712 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
714 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
717 for(Sector::Portables::iterator i = sector->portables.begin();
718 i != sector->portables.end(); ++i) {
719 Portable* portable = *i;
720 if(!portable->is_portable())
723 // make sure the Portable is a MovingObject
724 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
725 assert(moving_object);
726 if(moving_object == NULL)
729 // make sure the Portable isn't currently non-solid
730 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
732 // check if we are within reach
733 if(moving_object->get_bbox().contains(pos)) {
734 if (climbing) stop_climbing(*climbing);
735 grabbed_object = portable;
736 grabbed_object->grab(*this, get_pos(), dir);
744 Player::handle_input_ghost()
748 if (controller->hold(Controller::LEFT)) {
750 vx -= MAX_RUN_XM * 2;
752 if (controller->hold(Controller::RIGHT)) {
754 vx += MAX_RUN_XM * 2;
756 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
757 vy -= MAX_RUN_XM * 2;
759 if (controller->hold(Controller::DOWN)) {
760 vy += MAX_RUN_XM * 2;
762 if (controller->hold(Controller::ACTION)) {
763 set_ghost_mode(false);
765 physic.set_velocity(vx, vy);
766 physic.set_acceleration(0, 0);
770 Player::add_coins(int count)
772 player_status->add_coins(count);
778 return player_status->coins;
782 Player::add_bonus(const std::string& bonustype)
784 BonusType type = NO_BONUS;
786 if(bonustype == "grow") {
788 } else if(bonustype == "fireflower") {
790 } else if(bonustype == "iceflower") {
792 } else if(bonustype == "none") {
795 std::ostringstream msg;
796 msg << "Unknown bonus type " << bonustype;
797 throw std::runtime_error(msg.str());
800 return add_bonus(type);
804 Player::add_bonus(BonusType type, bool animate)
806 // always ignore NO_BONUS
807 if (type == NO_BONUS) {
811 // ignore GROWUP_BONUS if we're already big
812 if (type == GROWUP_BONUS) {
813 if (player_status->bonus == GROWUP_BONUS)
815 if (player_status->bonus == FIRE_BONUS)
817 if (player_status->bonus == ICE_BONUS)
821 return set_bonus(type, animate);
825 Player::set_bonus(BonusType type, bool animate)
827 if(player_status->bonus == NO_BONUS) {
828 if (!adjust_height(62.8f)) {
829 printf("can't adjust\n");
834 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
836 if (climbing) stop_climbing(*climbing);
839 if (type == NO_BONUS) {
840 if (does_buttjump) does_buttjump = false;
843 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
844 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
845 // visually lose helmet
846 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
847 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
848 Vector paccel = Vector(0, 1000);
849 std::string action = (dir==LEFT)?"left":"right";
850 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
851 if (climbing) stop_climbing(*climbing);
853 if ((player_status->bonus == ICE_BONUS) && (animate)) {
855 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
856 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
857 Vector paccel = Vector(0, 1000);
858 std::string action = (dir==LEFT)?"left":"right";
859 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
860 if (climbing) stop_climbing(*climbing);
862 player_status->max_fire_bullets = 0;
863 player_status->max_ice_bullets = 0;
865 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
866 if (type == ICE_BONUS) player_status->max_ice_bullets++;
868 player_status->bonus = type;
873 Player::set_visible(bool visible)
875 this->visible = visible;
877 set_group(COLGROUP_MOVING);
879 set_group(COLGROUP_DISABLED);
883 Player::get_visible()
891 kick_timer.start(KICK_TIME);
895 Player::draw(DrawingContext& context)
900 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
901 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
902 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
903 float py = Sector::current()->camera->get_translation().y;
904 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
905 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
908 std::string sa_prefix = "";
909 if (player_status->bonus == GROWUP_BONUS)
911 else if (player_status->bonus == FIRE_BONUS)
913 else if (player_status->bonus == ICE_BONUS)
918 /* Set Tux sprite action */
920 sprite->set_action_continued((dir == LEFT)?"grow-left":"grow-right");
921 // while growing, do not change action
922 // do_duck() will take care of cancelling growing manually
923 // update() will take care of cancelling when growing completed
926 sprite->set_action(sa_prefix+((dir == LEFT)?"-skid-left":"-skid-right"));
928 else if (backflipping) {
929 sprite->set_action(sa_prefix+((dir == LEFT)?"-backflip-left":"-backflip-right"));
931 else if (duck && is_big()) {
932 sprite->set_action(sa_prefix+((dir == LEFT)?"-duck-left":"-duck-right"));
934 else if (skidding_timer.started() && !skidding_timer.check()) {
935 sprite->set_action(sa_prefix+((dir == LEFT)?"-skid-left":"-skid-right"));
937 else if (kick_timer.started() && !kick_timer.check()) {
938 sprite->set_action(sa_prefix+((dir == LEFT)?"-kick-left":"-kick-right"));
940 else if ((wants_buttjump || does_buttjump) && is_big()) {
941 sprite->set_action(sa_prefix+((dir == LEFT)?"-buttjump-left":"-buttjump-right"));
943 else if (!on_ground()) {
944 sprite->set_action(sa_prefix+((dir == LEFT)?"-jump-left":"-jump-right"));
947 if (fabsf(physic.get_velocity_x()) < 1.0f) {
948 // if(idle_timer.check()) {
949 // sprite->set_action(sa_prefix+((dir == LEFT)?"-idle-left":"-idle-right"));
951 sprite->set_action(sa_prefix+((dir == LEFT)?"-stand-left":"-stand-right"));
955 sprite->set_action(sa_prefix+((dir == LEFT)?"-walk-left":"-walk-right"));
961 // Tux is holding something
962 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
963 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
971 sprite->set_action("gameover");
975 if (safe_timer.started() && size_t(game_time*40)%2)
978 sprite->draw(context, get_pos(), LAYER_OBJECTS + 1);
984 Player::collision_tile(uint32_t tile_attributes)
986 if(tile_attributes & Tile::HURTS)
991 if( tile_attributes & Tile::WATER ){
997 if( tile_attributes & Tile::WATER ){
1000 sound_manager->play( "sounds/splash.ogg" );
1007 Player::collision_solid(const CollisionHit& hit)
1010 if(physic.get_velocity_y() > 0)
1011 physic.set_velocity_y(0);
1013 on_ground_flag = true;
1014 floor_normal = hit.slope_normal;
1017 if (does_buttjump) {
1018 does_buttjump = false;
1019 physic.set_velocity_y(-300);
1020 on_ground_flag = false;
1021 Sector::current()->add_object(new Particles(
1022 Vector(get_bbox().p2.x, get_bbox().p2.y),
1024 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1026 Sector::current()->add_object(new Particles(
1027 Vector(get_bbox().p1.x, get_bbox().p2.y),
1029 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
1033 } else if(hit.top) {
1034 if(physic.get_velocity_y() < 0)
1035 physic.set_velocity_y(.2f);
1038 if(hit.left || hit.right) {
1039 physic.set_velocity_x(0);
1044 if(hit.left || hit.right) {
1046 } else if(hit.top || hit.bottom) {
1053 Player::collision(GameObject& other, const CollisionHit& hit)
1055 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1060 if(hit.left || hit.right) {
1061 try_grab(); //grab objects right now, in update it will be too late
1064 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1066 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1067 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1068 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1070 if(controller->pressed(Controller::UP))
1071 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1077 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1078 if(badguy != NULL) {
1079 if(safe_timer.started() || invincible_timer.started())
1089 Player::make_invincible()
1091 sound_manager->play("sounds/invincible.wav");
1092 invincible_timer.start(TUX_INVINCIBLE_TIME);
1093 Sector::current()->play_music(HERRING_MUSIC);
1098 Player::kill(bool completely)
1100 if(dying || deactivated)
1103 if(!completely && (safe_timer.started() || invincible_timer.started()))
1108 sound_manager->play("sounds/hurt.wav");
1110 if (climbing) stop_climbing(*climbing);
1112 physic.set_velocity_x(0);
1114 if(!completely && is_big()) {
1115 if(player_status->bonus == FIRE_BONUS
1116 || player_status->bonus == ICE_BONUS) {
1117 safe_timer.start(TUX_SAFE_TIME);
1118 set_bonus(GROWUP_BONUS, true);
1119 } else if(player_status->bonus == GROWUP_BONUS) {
1120 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1121 adjust_height(30.8f);
1123 set_bonus(NO_BONUS, true);
1124 } else if(player_status->bonus == NO_BONUS) {
1125 safe_timer.start(TUX_SAFE_TIME);
1126 adjust_height(30.8f);
1131 // do not die when in edit mode
1133 set_ghost_mode(true);
1137 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1139 for (int i = 0; i < 5; i++)
1141 // the numbers: starting x, starting y, velocity y
1142 Sector::current()->add_object(new FallingCoin(get_pos() +
1143 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1144 systemRandom.rand(-100,100)));
1146 player_status->coins -= std::max(player_status->coins/10, 25);
1150 GameSession::current()->set_reset_point("", Vector());
1152 physic.enable_gravity(true);
1153 physic.set_acceleration(0, 0);
1154 physic.set_velocity(0, -700);
1155 set_bonus(NO_BONUS, true);
1157 dying_timer.start(3.0);
1158 set_group(COLGROUP_DISABLED);
1160 DisplayEffect* effect = new DisplayEffect();
1161 effect->fade_out(3.0);
1162 Sector::current()->add_object(effect);
1163 sound_manager->stop_music(3.0);
1168 Player::move(const Vector& vector)
1172 // TODO: do we need the following? Seems irrelevant to moving the player
1174 set_size(31.8f, 63.8f);
1176 set_size(31.8f, 31.8f);
1178 last_ground_y = vector.y;
1179 if (climbing) stop_climbing(*climbing);
1185 Player::check_bounds(Camera* camera)
1187 /* Keep tux in bounds: */
1188 if (get_pos().x < 0) {
1189 // Lock Tux to the size of the level, so that he doesn't fall of
1191 set_pos(Vector(0, get_pos().y));
1194 /* fallen out of the level? */
1195 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1200 // can happen if back scrolling is disabled
1201 if(get_pos().x < camera->get_translation().x) {
1202 set_pos(Vector(camera->get_translation().x, get_pos().y));
1204 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1207 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1213 Player::add_velocity(const Vector& velocity)
1215 physic.set_velocity(physic.get_velocity() + velocity);
1219 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1221 if (end_speed.x > 0)
1222 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1223 if (end_speed.x < 0)
1224 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1225 if (end_speed.y > 0)
1226 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1227 if (end_speed.y < 0)
1228 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1232 Player::get_velocity()
1234 return physic.get_velocity();
1238 Player::bounce(BadGuy& )
1240 if(controller->hold(Controller::JUMP))
1241 physic.set_velocity_y(-520);
1243 physic.set_velocity_y(-300);
1246 //Scripting Functions Below
1249 Player::deactivate()
1254 physic.set_velocity_x(0);
1255 physic.set_velocity_y(0);
1256 physic.set_acceleration_x(0);
1257 physic.set_acceleration_y(0);
1258 if (climbing) stop_climbing(*climbing);
1266 deactivated = false;
1269 void Player::walk(float speed)
1271 physic.set_velocity_x(speed);
1275 Player::set_ghost_mode(bool enable)
1277 if (ghost_mode == enable)
1280 if (climbing) stop_climbing(*climbing);
1284 set_group(COLGROUP_DISABLED);
1285 physic.enable_gravity(false);
1286 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1289 set_group(COLGROUP_MOVING);
1290 physic.enable_gravity(true);
1291 log_debug << "You feel solid again." << std::endl;
1297 Player::set_edit_mode(bool enable)
1303 Player::start_climbing(Climbable& climbable)
1305 if (climbing == &climbable) return;
1307 climbing = &climbable;
1308 physic.enable_gravity(false);
1309 physic.set_velocity(0, 0);
1310 physic.set_acceleration(0, 0);
1314 Player::stop_climbing(Climbable& /*climbable*/)
1316 if (!climbing) return;
1320 if (grabbed_object) {
1321 grabbed_object->ungrab(*this, dir);
1322 grabbed_object = NULL;
1325 physic.enable_gravity(true);
1326 physic.set_velocity(0, 0);
1327 physic.set_acceleration(0, 0);
1329 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1330 on_ground_flag = true;
1331 // TODO: This won't help. Why?
1337 Player::handle_input_climbing()
1340 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1346 if (controller->hold(Controller::LEFT)) {
1350 if (controller->hold(Controller::RIGHT)) {
1354 if (controller->hold(Controller::UP)) {
1357 if (controller->hold(Controller::DOWN)) {
1360 if (controller->hold(Controller::JUMP)) {
1362 stop_climbing(*climbing);
1368 if (controller->hold(Controller::ACTION)) {
1369 stop_climbing(*climbing);
1372 physic.set_velocity(vx, vy);
1373 physic.set_acceleration(0, 0);