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 = 700.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 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
105 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
106 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.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 smalltux_gameover;
126 delete smalltux_star;
128 delete scripting_controller;
135 set_size(31.8f, 62.8f);
137 set_size(31.8f, 30.8f);
147 fall_mode = ON_GROUND;
153 backflipping = false;
154 backflip_direction = 0;
157 speedlimit = 0; //no special limit
159 on_ground_flag = false;
160 grabbed_object = NULL;
168 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
173 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
177 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
182 Scripting::unexpose_object(vm, table_idx, name);
186 Player::get_speedlimit()
192 Player::set_speedlimit(float newlimit)
198 Player::set_controller(Controller* controller)
200 this->controller = controller;
204 Player::use_scripting_controller(bool use_or_release)
206 if ((use_or_release == true) && (controller != scripting_controller)) {
207 scripting_controller_old = get_controller();
208 set_controller(scripting_controller);
210 if ((use_or_release == false) && (controller == scripting_controller)) {
211 set_controller(scripting_controller_old);
212 scripting_controller_old = 0;
217 Player::do_scripting_controller(std::string control, bool pressed)
219 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
220 if(control == std::string(Controller::controlNames[i])) {
221 scripting_controller->press(Controller::Control(i), pressed);
227 Player::adjust_height(float new_height)
230 bbox2.move(Vector(0, bbox.get_height() - new_height));
231 bbox2.set_height(new_height);
233 if(new_height > bbox.get_height()) {
234 Rect additional_space = bbox2;
235 additional_space.set_height(new_height - bbox.get_height());
236 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
240 // adjust bbox accordingly
241 // note that we use members of moving_object for this, so we can run this during CD, too
243 set_size(bbox2.get_width(), bbox2.get_height());
248 Player::trigger_sequence(std::string sequence_name)
250 if (climbing) stop_climbing(*climbing);
251 GameSession::current()->start_sequence(sequence_name);
255 Player::update(float elapsed_time)
262 if(dying && dying_timer.check()) {
267 if(!dying && !deactivated)
270 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
274 // extend/shrink tux collision rectangle so that we fall through/walk over 1
276 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
282 // on downward slopes, adjust vertical velocity so tux walks smoothly down
284 if(floor_normal.y != 0) {
285 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
286 physic.set_velocity_y(250);
291 // handle backflipping
293 //prevent player from changing direction when backflipping
294 dir = (backflip_direction == 1) ? LEFT : RIGHT;
295 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
300 fall_mode = ON_GROUND;
301 last_ground_y = get_pos().y;
303 if(get_pos().y > last_ground_y)
305 else if(fall_mode == ON_GROUND)
309 // check if we landed
312 if (backflipping && (!backflip_timer.started())) {
313 backflipping = false;
314 backflip_direction = 0;
316 // if controls are currently deactivated, we take care of standing up ourselves
322 // calculate movement for this frame
323 movement = physic.get_movement(elapsed_time);
325 if(grabbed_object != NULL && !dying) {
326 Vector pos = get_pos() +
327 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
328 grabbed_object->grab(*this, pos, dir);
331 if(grabbed_object != NULL && dying){
332 grabbed_object->ungrab(*this, dir);
333 grabbed_object = NULL;
336 on_ground_flag = false;
338 // when invincible, spawn particles
339 if (invincible_timer.started() && !dying)
341 if (systemRandom.rand(0, 2) == 0) {
342 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
343 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
344 Vector ppos = Vector(px, py);
345 Vector pspeed = Vector(0, 0);
346 Vector paccel = Vector(0, 0);
347 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
348 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
349 // make every other a longer sparkle to make trail a bit fuzzy
350 if (size_t(game_time*20)%2) {
351 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
353 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
356 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
362 if (sprite->animation_done()) growing = false;
370 return on_ground_flag;
376 if(player_status->bonus == NO_BONUS)
383 Player::apply_friction()
385 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
386 physic.set_velocity_x(0);
387 physic.set_acceleration_x(0);
388 } else if(physic.get_velocity_x() < 0) {
389 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
390 } else if(physic.get_velocity_x() > 0) {
391 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
396 Player::handle_horizontal_input()
398 float vx = physic.get_velocity_x();
399 float vy = physic.get_velocity_y();
400 float ax = physic.get_acceleration_x();
401 float ay = physic.get_acceleration_y();
404 if(!duck || physic.get_velocity_y() != 0) {
405 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
409 } else if(!controller->hold(Controller::LEFT)
410 && controller->hold(Controller::RIGHT)) {
417 // do not run if action key is pressed or we're holding something
418 // so tux can only walk while shooting
419 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
420 ax = dirsign * WALK_ACCELERATION_X;
422 if(vx >= MAX_WALK_XM && dirsign > 0) {
425 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
430 if( vx * dirsign < MAX_WALK_XM ) {
431 ax = dirsign * WALK_ACCELERATION_X;
433 ax = dirsign * RUN_ACCELERATION_X;
436 if(vx >= MAX_RUN_XM && dirsign > 0) {
439 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
445 // we can reach WALK_SPEED without any acceleration
446 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
447 vx = dirsign * WALK_SPEED;
451 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
452 vx = dirsign * speedlimit;
456 // changing directions?
457 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
459 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
460 skidding_timer.start(SKID_TIME);
461 sound_manager->play("sounds/skid.wav");
462 // dust some particles
463 Sector::current()->add_object(
465 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
466 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
467 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
476 physic.set_velocity(vx, vy);
477 physic.set_acceleration(ax, ay);
479 // we get slower when not pressing any keys
501 if (physic.get_velocity_y() != 0)
508 if (adjust_height(31.8f)) {
511 unduck_hurt_timer.stop();
518 Player::do_standup() {
526 if (adjust_height(63.8f)) {
528 unduck_hurt_timer.stop();
530 // if timer is not already running, start it.
531 if (unduck_hurt_timer.get_period() == 0) {
532 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
534 else if (unduck_hurt_timer.check()) {
542 Player::do_backflip() {
548 backflip_direction = (dir == LEFT)?(+1):(-1);
551 sound_manager->play("sounds/flip.wav");
552 backflip_timer.start(0.15f);
556 Player::do_jump(float yspeed) {
560 physic.set_velocity_y(yspeed);
561 //bbox.move(Vector(0, -1));
563 on_ground_flag = false;
568 sound_manager->play("sounds/bigjump.wav");
570 sound_manager->play("sounds/jump.wav");
575 Player::handle_vertical_input()
578 if(controller->pressed(Controller::JUMP) && (can_jump)) {
580 // when running, only jump a little bit; else do a backflip
581 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
583 // jump a bit higher if we are running; else do a normal jump
584 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
587 // Let go of jump key
588 else if(!controller->hold(Controller::JUMP)) {
589 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
591 physic.set_velocity_y(0);
595 /* In case the player has pressed Down while in a certain range of air,
596 enable butt jump action */
597 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && !on_ground() && (physic.get_velocity_y() >= BUTTJUMP_MIN_VELOCITY_Y)) {
601 /* When Down is not held anymore, disable butt jump */
602 if(butt_jump && !controller->hold(Controller::DOWN))
606 physic.set_acceleration_y(0);
609 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
610 physic.set_acceleration_y(-2000);
611 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
617 Player::handle_input()
620 handle_input_ghost();
624 handle_input_climbing();
629 if( controller->released( Controller::PEEK_LEFT ) ) {
632 if( controller->released( Controller::PEEK_RIGHT ) ) {
635 if( controller->released( Controller::UP ) ) {
638 if( controller->released( Controller::DOWN ) ) {
641 if( controller->pressed( Controller::PEEK_LEFT ) ) {
644 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
647 if( controller->pressed( Controller::UP ) ) {
650 if( controller->pressed( Controller::DOWN ) ) {
654 /* Handle horizontal movement: */
655 if (!backflipping) handle_horizontal_input();
658 if (on_ground() && !controller->hold(Controller::JUMP))
661 /* Handle vertical movement: */
662 handle_vertical_input();
665 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
666 if(Sector::current()->add_bullet(
667 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
668 : Vector(32, bbox.get_height()/2)),
669 physic.get_velocity_x(), dir))
670 shooting_timer.start(SHOOTING_TIME);
673 /* Duck or Standup! */
674 if (controller->hold(Controller::DOWN)) {
683 if(!controller->hold(Controller::ACTION) && grabbed_object) {
684 // move the grabbed object a bit away from tux
685 Vector pos = get_pos() +
686 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
687 bbox.get_height()*0.66666 - 32);
688 Rect dest(pos, pos + Vector(32, 32));
689 if(Sector::current()->is_free_of_movingstatics(dest)) {
690 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
692 moving_object->set_pos(pos);
694 log_debug << "Non MovingObject grabbed?!?" << std::endl;
696 if(controller->hold(Controller::UP)) {
697 grabbed_object->ungrab(*this, UP);
699 grabbed_object->ungrab(*this, dir);
701 grabbed_object = NULL;
709 if(controller->hold(Controller::ACTION) && !grabbed_object
711 Sector* sector = Sector::current();
714 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
716 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
719 for(Sector::Portables::iterator i = sector->portables.begin();
720 i != sector->portables.end(); ++i) {
721 Portable* portable = *i;
722 if(!portable->is_portable())
725 // make sure the Portable is a MovingObject
726 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
727 assert(moving_object);
728 if(moving_object == NULL)
731 // make sure the Portable isn't currently non-solid
732 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
734 // check if we are within reach
735 if(moving_object->get_bbox().contains(pos)) {
736 if (climbing) stop_climbing(*climbing);
737 grabbed_object = portable;
738 grabbed_object->grab(*this, get_pos(), dir);
746 Player::handle_input_ghost()
750 if (controller->hold(Controller::LEFT)) {
752 vx -= MAX_RUN_XM * 2;
754 if (controller->hold(Controller::RIGHT)) {
756 vx += MAX_RUN_XM * 2;
758 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
759 vy -= MAX_RUN_XM * 2;
761 if (controller->hold(Controller::DOWN)) {
762 vy += MAX_RUN_XM * 2;
764 if (controller->hold(Controller::ACTION)) {
765 set_ghost_mode(false);
767 physic.set_velocity(vx, vy);
768 physic.set_acceleration(0, 0);
772 Player::add_coins(int count)
774 player_status->add_coins(count);
780 return player_status->coins;
784 Player::add_bonus(const std::string& bonustype)
786 BonusType type = NO_BONUS;
788 if(bonustype == "grow") {
790 } else if(bonustype == "fireflower") {
792 } else if(bonustype == "iceflower") {
794 } else if(bonustype == "none") {
797 std::ostringstream msg;
798 msg << "Unknown bonus type " << bonustype;
799 throw std::runtime_error(msg.str());
802 return add_bonus(type);
806 Player::add_bonus(BonusType type, bool animate)
808 // always ignore NO_BONUS
809 if (type == NO_BONUS) {
813 // ignore GROWUP_BONUS if we're already big
814 if (type == GROWUP_BONUS) {
815 if (player_status->bonus == GROWUP_BONUS)
817 if (player_status->bonus == FIRE_BONUS)
819 if (player_status->bonus == ICE_BONUS)
823 return set_bonus(type, animate);
827 Player::set_bonus(BonusType type, bool animate)
829 if(player_status->bonus == NO_BONUS) {
830 if (!adjust_height(62.8f)) {
831 printf("can't adjust\n");
836 sprite->set_action((dir == LEFT)?"grow-left":"grow-right", 1);
838 if (climbing) stop_climbing(*climbing);
841 if (type == NO_BONUS) {
842 if (butt_jump) butt_jump = false;
845 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
846 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
847 // visually lose helmet
848 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
849 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
850 Vector paccel = Vector(0, 1000);
851 std::string action = (dir==LEFT)?"left":"right";
852 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
853 if (climbing) stop_climbing(*climbing);
855 if ((player_status->bonus == ICE_BONUS) && (animate)) {
857 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
858 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
859 Vector paccel = Vector(0, 1000);
860 std::string action = (dir==LEFT)?"left":"right";
861 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
862 if (climbing) stop_climbing(*climbing);
864 player_status->max_fire_bullets = 0;
865 player_status->max_ice_bullets = 0;
867 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
868 if (type == ICE_BONUS) player_status->max_ice_bullets++;
870 player_status->bonus = type;
875 Player::set_visible(bool visible)
877 this->visible = visible;
879 set_group(COLGROUP_MOVING);
881 set_group(COLGROUP_DISABLED);
885 Player::get_visible()
893 kick_timer.start(KICK_TIME);
897 Player::draw(DrawingContext& context)
902 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
903 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
904 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
905 float py = Sector::current()->camera->get_translation().y;
906 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
907 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
910 std::string sa_prefix = "";
911 if (player_status->bonus == GROWUP_BONUS)
913 else if (player_status->bonus == FIRE_BONUS)
915 else if (player_status->bonus == ICE_BONUS)
920 /* Set Tux sprite action */
922 // while growing, do not change action
923 // do_duck() will take care of cancelling growing manually
924 // update() will take care of cancelling when growing completed
927 sprite->set_action(sa_prefix+((dir == LEFT)?"-skid-left":"-skid-right"));
929 else if (backflipping) {
930 sprite->set_action(sa_prefix+((dir == LEFT)?"-backflip-left":"-backflip-right"));
932 else if (duck && is_big()) {
933 sprite->set_action(sa_prefix+((dir == LEFT)?"-duck-left":"-duck-right"));
935 else if (skidding_timer.started() && !skidding_timer.check()) {
936 sprite->set_action(sa_prefix+((dir == LEFT)?"-skid-left":"-skid-right"));
938 else if (kick_timer.started() && !kick_timer.check()) {
939 sprite->set_action(sa_prefix+((dir == LEFT)?"-kick-left":"-kick-right"));
941 else if (butt_jump && is_big()) {
942 sprite->set_action(sa_prefix+((dir == LEFT)?"-buttjump-left":"-buttjump-right"));
944 else if (!on_ground()) {
945 sprite->set_action(sa_prefix+((dir == LEFT)?"-jump-left":"-jump-right"));
948 if (fabsf(physic.get_velocity_x()) < 1.0f) {
949 // if(idle_timer.check()) {
950 // sprite->set_action(sa_prefix+((dir == LEFT)?"-idle-left":"-idle-right"));
952 sprite->set_action(sa_prefix+((dir == LEFT)?"-stand-left":"-stand-right"));
956 sprite->set_action(sa_prefix+((dir == LEFT)?"-walk-left":"-walk-right"));
962 // Tux is holding something
963 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
964 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check())) {
973 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
975 else 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;
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()))
1106 sound_manager->play("sounds/hurt.wav");
1108 if (climbing) stop_climbing(*climbing);
1110 physic.set_velocity_x(0);
1112 if(!completely && is_big()) {
1113 if(player_status->bonus == FIRE_BONUS
1114 || player_status->bonus == ICE_BONUS) {
1115 safe_timer.start(TUX_SAFE_TIME);
1116 set_bonus(GROWUP_BONUS, true);
1117 } else if(player_status->bonus == GROWUP_BONUS) {
1118 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1119 adjust_height(30.8f);
1121 set_bonus(NO_BONUS, true);
1122 } else if(player_status->bonus == NO_BONUS) {
1123 safe_timer.start(TUX_SAFE_TIME);
1124 adjust_height(30.8f);
1129 // do not die when in edit mode
1131 set_ghost_mode(true);
1135 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1137 for (int i = 0; i < 5; i++)
1139 // the numbers: starting x, starting y, velocity y
1140 Sector::current()->add_object(new FallingCoin(get_pos() +
1141 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1142 systemRandom.rand(-100,100)));
1144 player_status->coins -= std::max(player_status->coins/10, 25);
1148 GameSession::current()->set_reset_point("", Vector());
1150 physic.enable_gravity(true);
1151 physic.set_acceleration(0, 0);
1152 physic.set_velocity(0, -700);
1153 set_bonus(NO_BONUS, true);
1155 dying_timer.start(3.0);
1156 set_group(COLGROUP_DISABLED);
1158 DisplayEffect* effect = new DisplayEffect();
1159 effect->fade_out(3.0);
1160 Sector::current()->add_object(effect);
1161 sound_manager->stop_music(3.0);
1166 Player::move(const Vector& vector)
1170 // TODO: do we need the following? Seems irrelevant to moving the player
1172 set_size(31.8f, 63.8f);
1174 set_size(31.8f, 31.8f);
1176 last_ground_y = vector.y;
1177 if (climbing) stop_climbing(*climbing);
1183 Player::check_bounds(Camera* camera)
1185 /* Keep tux in bounds: */
1186 if (get_pos().x < 0) {
1187 // Lock Tux to the size of the level, so that he doesn't fall of
1189 set_pos(Vector(0, get_pos().y));
1192 /* fallen out of the level? */
1193 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1198 // can happen if back scrolling is disabled
1199 if(get_pos().x < camera->get_translation().x) {
1200 set_pos(Vector(camera->get_translation().x, get_pos().y));
1202 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1205 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1211 Player::add_velocity(const Vector& velocity)
1213 physic.set_velocity(physic.get_velocity() + velocity);
1217 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1219 if (end_speed.x > 0)
1220 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1221 if (end_speed.x < 0)
1222 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1223 if (end_speed.y > 0)
1224 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1225 if (end_speed.y < 0)
1226 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1230 Player::bounce(BadGuy& )
1232 if(controller->hold(Controller::JUMP))
1233 physic.set_velocity_y(-520);
1235 physic.set_velocity_y(-300);
1238 //Scripting Functions Below
1241 Player::deactivate()
1246 physic.set_velocity_x(0);
1247 physic.set_velocity_y(0);
1248 physic.set_acceleration_x(0);
1249 physic.set_acceleration_y(0);
1250 if (climbing) stop_climbing(*climbing);
1258 deactivated = false;
1261 void Player::walk(float speed)
1263 physic.set_velocity_x(speed);
1267 Player::set_ghost_mode(bool enable)
1269 if (ghost_mode == enable)
1272 if (climbing) stop_climbing(*climbing);
1276 set_group(COLGROUP_DISABLED);
1277 physic.enable_gravity(false);
1278 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1281 set_group(COLGROUP_MOVING);
1282 physic.enable_gravity(true);
1283 log_debug << "You feel solid again." << std::endl;
1289 Player::set_edit_mode(bool enable)
1295 Player::start_climbing(Climbable& climbable)
1297 if (climbing == &climbable) return;
1299 climbing = &climbable;
1300 physic.enable_gravity(false);
1301 physic.set_velocity(0, 0);
1302 physic.set_acceleration(0, 0);
1306 Player::stop_climbing(Climbable& /*climbable*/)
1308 if (!climbing) return;
1312 if (grabbed_object) {
1313 grabbed_object->ungrab(*this, dir);
1314 grabbed_object = NULL;
1317 physic.enable_gravity(true);
1318 physic.set_velocity(0, 0);
1319 physic.set_acceleration(0, 0);
1321 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1322 on_ground_flag = true;
1323 // TODO: This won't help. Why?
1329 Player::handle_input_climbing()
1332 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1338 if (controller->hold(Controller::LEFT)) {
1342 if (controller->hold(Controller::RIGHT)) {
1346 if (controller->hold(Controller::UP)) {
1349 if (controller->hold(Controller::DOWN)) {
1352 if (controller->hold(Controller::JUMP)) {
1354 stop_climbing(*climbing);
1360 if (controller->hold(Controller::ACTION)) {
1361 stop_climbing(*climbing);
1364 physic.set_velocity(vx, vy);
1365 physic.set_acceleration(0, 0);