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 SHOOTING_TIME = .150f;
58 /// time before idle animation starts
59 static const float IDLE_TIME = 2.5f;
61 /** acceleration in horizontal direction when walking
62 * (all acceleratiosn are in pixel/s^2) */
63 static const float WALK_ACCELERATION_X = 300;
64 /** acceleration in horizontal direction when running */
65 static const float RUN_ACCELERATION_X = 400;
66 /** acceleration when skidding */
67 static const float SKID_XM = 200;
68 /** time of skidding in seconds */
69 static const float SKID_TIME = .3f;
70 /** maximum walk velocity (pixel/s) */
71 static const float MAX_WALK_XM = 230;
72 /** maximum run velcoity (pixel/s) */
73 static const float MAX_RUN_XM = 320;
74 /** maximum horizontal climb velocity */
75 static const float MAX_CLIMB_XM = 48;
76 /** maximum vertical climb velocity */
77 static const float MAX_CLIMB_YM = 128;
78 /** instant velocity when tux starts to walk */
79 static const float WALK_SPEED = 100;
81 /** time of the kick (kicking mriceblock) animation */
82 static const float KICK_TIME = .3f;
83 /** time of tux cheering (currently unused) */
84 static const float CHEER_TIME = 1.0f;
86 /** if Tux cannot unduck for this long, he will get hurt */
87 static const float UNDUCK_HURT_TIME = 0.25f;
90 Surface* growingtux_left[GROWING_FRAMES];
91 Surface* growingtux_right[GROWING_FRAMES];
93 Surface* tux_life = 0;
95 TuxBodyParts* small_tux = 0;
96 TuxBodyParts* big_tux = 0;
97 TuxBodyParts* fire_tux = 0;
98 TuxBodyParts* ice_tux = 0;
101 bool no_water = true;
104 TuxBodyParts::set_action(std::string action, int loops)
107 head->set_action(action, loops);
109 body->set_action(action, loops);
111 arms->set_action(action, loops);
113 feet->set_action(action, loops);
117 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer, Portable* grabbed_object)
120 head->draw(context, pos, layer-2);
122 body->draw(context, pos, layer-4);
124 arms->draw(context, pos, layer-1 + (grabbed_object?10:0));
126 feet->draw(context, pos, layer-3);
129 Player::Player(PlayerStatus* _player_status, const std::string& name)
130 : player_status(_player_status), grabbed_object(NULL), ghost_mode(false), climbing(0)
133 controller = main_controller;
134 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
135 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
136 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
137 airarrow.reset(new Surface("images/engine/hud/airarrow.png"));
139 sound_manager->preload("sounds/bigjump.wav");
140 sound_manager->preload("sounds/jump.wav");
141 sound_manager->preload("sounds/hurt.wav");
142 sound_manager->preload("sounds/skid.wav");
143 sound_manager->preload("sounds/flip.wav");
144 sound_manager->preload("sounds/invincible.wav");
145 sound_manager->preload("sounds/splash.ogg");
152 if (climbing) stop_climbing(*climbing);
153 delete smalltux_gameover;
154 delete smalltux_star;
162 set_size(31.8f, 62.8f);
164 set_size(31.8f, 30.8f);
174 fall_mode = ON_GROUND;
179 backflipping = false;
180 backflip_direction = 0;
183 speedlimit = 0; //no special limit
185 on_ground_flag = false;
186 grabbed_object = NULL;
194 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
199 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
203 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
208 Scripting::unexpose_object(vm, table_idx, name);
212 Player::get_speedlimit()
218 Player::set_speedlimit(float newlimit)
224 Player::set_controller(Controller* controller)
226 this->controller = controller;
230 Player::adjust_height(float new_height)
233 bbox2.move(Vector(0, bbox.get_height() - new_height));
234 bbox2.set_height(new_height);
236 if(new_height > bbox.get_height()) {
237 Rect additional_space = bbox2;
238 additional_space.set_height(new_height - bbox.get_height());
239 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
243 // adjust bbox accordingly
244 // note that we use members of moving_object for this, so we can run this during CD, too
246 set_size(bbox2.get_width(), bbox2.get_height());
251 Player::trigger_sequence(std::string sequence_name)
253 if (climbing) stop_climbing(*climbing);
254 GameSession::current()->start_sequence(sequence_name);
258 Player::update(float elapsed_time)
265 if(dying && dying_timer.check()) {
270 if(!dying && !deactivated)
273 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
277 // extend/shrink tux collision rectangle so that we fall through/walk over 1
279 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
285 // on downward slopes, adjust vertical velocity so tux walks smoothly down
287 if(floor_normal.y != 0) {
288 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
289 physic.set_velocity_y(250);
294 // handle backflipping
296 //prevent player from changing direction when backflipping
297 dir = (backflip_direction == 1) ? LEFT : RIGHT;
298 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
303 fall_mode = ON_GROUND;
304 last_ground_y = get_pos().y;
306 if(get_pos().y > last_ground_y)
308 else if(fall_mode == ON_GROUND)
312 // check if we landed
315 if (backflipping && (!backflip_timer.started())) {
316 backflipping = false;
317 backflip_direction = 0;
319 // if controls are currently deactivated, we take care of standing up ourselves
325 // calculate movement for this frame
326 movement = physic.get_movement(elapsed_time);
328 if(grabbed_object != NULL && !dying) {
329 Vector pos = get_pos() +
330 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
331 grabbed_object->grab(*this, pos, dir);
334 if(grabbed_object != NULL && dying){
335 grabbed_object->ungrab(*this, dir);
336 grabbed_object = NULL;
339 on_ground_flag = false;
341 // when invincible, spawn particles
342 if (invincible_timer.started() && !dying)
344 if (systemRandom.rand(0, 2) == 0) {
345 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
346 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
347 Vector ppos = Vector(px, py);
348 Vector pspeed = Vector(0, 0);
349 Vector paccel = Vector(0, 0);
350 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
351 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
352 // make every other a longer sparkle to make trail a bit fuzzy
353 if (size_t(game_time*20)%2) {
354 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
356 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
359 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
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)
505 if (adjust_height(31.8f)) {
507 unduck_hurt_timer.stop();
514 Player::do_standup() {
522 if (adjust_height(63.8f)) {
524 unduck_hurt_timer.stop();
526 // if timer is not already running, start it.
527 if (unduck_hurt_timer.get_period() == 0) {
528 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
530 else if (unduck_hurt_timer.check()) {
538 Player::do_backflip() {
544 // TODO: we don't have an animation for firetux backflipping, so let's revert to bigtux
545 set_bonus(GROWUP_BONUS, true);
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::handle_vertical_input()
577 if(controller->pressed(Controller::JUMP) && (can_jump)) {
579 // when running, only jump a little bit; else do a backflip
580 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
582 // jump a bit higher if we are running; else do a normal jump
583 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
586 // Let go of jump key
587 else if(!controller->hold(Controller::JUMP)) {
588 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
590 physic.set_velocity_y(0);
594 /* In case the player has pressed Down while in a certain range of air,
595 enable butt jump action */
596 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
600 /* When Down is not held anymore, disable butt jump */
601 if(butt_jump && !controller->hold(Controller::DOWN))
605 physic.set_acceleration_y(0);
608 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
609 physic.set_acceleration_y(-2000);
610 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
616 Player::handle_input()
619 handle_input_ghost();
623 handle_input_climbing();
628 if( controller->released( Controller::PEEK_LEFT ) ) {
631 if( controller->released( Controller::PEEK_RIGHT ) ) {
634 if( controller->released( Controller::UP ) ) {
637 if( controller->released( Controller::DOWN ) ) {
640 if( controller->pressed( Controller::PEEK_LEFT ) ) {
643 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
646 if( controller->pressed( Controller::UP ) ) {
649 if( controller->pressed( Controller::DOWN ) ) {
653 /* Handle horizontal movement: */
654 if (!backflipping) handle_horizontal_input();
657 if (on_ground() && !controller->hold(Controller::JUMP))
660 /* Handle vertical movement: */
661 handle_vertical_input();
664 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
665 if(Sector::current()->add_bullet(
666 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
667 : Vector(32, bbox.get_height()/2)),
668 physic.get_velocity_x(), dir))
669 shooting_timer.start(SHOOTING_TIME);
672 /* Duck or Standup! */
673 if (controller->hold(Controller::DOWN)) {
682 if(!controller->hold(Controller::ACTION) && grabbed_object) {
683 // move the grabbed object a bit away from tux
684 Vector pos = get_pos() +
685 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
686 bbox.get_height()*0.66666 - 32);
687 Rect dest(pos, pos + Vector(32, 32));
688 if(Sector::current()->is_free_of_movingstatics(dest)) {
689 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
691 moving_object->set_pos(pos);
693 log_debug << "Non MovingObject grabbed?!?" << std::endl;
695 if(controller->hold(Controller::UP)) {
696 grabbed_object->ungrab(*this, UP);
698 grabbed_object->ungrab(*this, dir);
700 grabbed_object = NULL;
708 if(controller->hold(Controller::ACTION) && !grabbed_object
710 Sector* sector = Sector::current();
713 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
715 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
718 for(Sector::Portables::iterator i = sector->portables.begin();
719 i != sector->portables.end(); ++i) {
720 Portable* portable = *i;
721 if(!portable->is_portable())
724 // make sure the Portable is a MovingObject
725 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
726 assert(moving_object);
727 if(moving_object == NULL)
730 // make sure the Portable isn't currently non-solid
731 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
733 // check if we are within reach
734 if(moving_object->get_bbox().contains(pos)) {
735 if (climbing) stop_climbing(*climbing);
736 grabbed_object = portable;
737 grabbed_object->grab(*this, get_pos(), dir);
745 Player::handle_input_ghost()
749 if (controller->hold(Controller::LEFT)) {
751 vx -= MAX_RUN_XM * 2;
753 if (controller->hold(Controller::RIGHT)) {
755 vx += MAX_RUN_XM * 2;
757 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
758 vy -= MAX_RUN_XM * 2;
760 if (controller->hold(Controller::DOWN)) {
761 vy += MAX_RUN_XM * 2;
763 if (controller->hold(Controller::ACTION)) {
764 set_ghost_mode(false);
766 physic.set_velocity(vx, vy);
767 physic.set_acceleration(0, 0);
771 Player::add_coins(int count)
773 player_status->add_coins(count);
779 return player_status->coins;
783 Player::add_bonus(const std::string& bonustype)
785 BonusType type = NO_BONUS;
787 if(bonustype == "grow") {
789 } else if(bonustype == "fireflower") {
791 } else if(bonustype == "iceflower") {
793 } else if(bonustype == "none") {
796 std::ostringstream msg;
797 msg << "Unknown bonus type " << bonustype;
798 throw std::runtime_error(msg.str());
801 return add_bonus(type);
805 Player::add_bonus(BonusType type, bool animate)
807 // always ignore NO_BONUS
808 if (type == NO_BONUS) {
812 // ignore GROWUP_BONUS if we're already big
813 if (type == GROWUP_BONUS) {
814 if (player_status->bonus == GROWUP_BONUS)
816 if (player_status->bonus == FIRE_BONUS)
818 if (player_status->bonus == ICE_BONUS)
822 return set_bonus(type, animate);
826 Player::set_bonus(BonusType type, bool animate)
828 if(player_status->bonus == NO_BONUS) {
829 if (!adjust_height(62.8f)) {
830 printf("can't adjust\n");
834 growing_timer.start(GROWING_TIME);
835 if (climbing) stop_climbing(*climbing);
838 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
839 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
840 // visually lose helmet
841 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
842 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
843 Vector paccel = Vector(0, 1000);
844 std::string action = (dir==LEFT)?"left":"right";
845 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
846 if (climbing) stop_climbing(*climbing);
848 if ((player_status->bonus == ICE_BONUS) && (animate)) {
850 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
851 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
852 Vector paccel = Vector(0, 1000);
853 std::string action = (dir==LEFT)?"left":"right";
854 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
855 if (climbing) stop_climbing(*climbing);
857 player_status->max_fire_bullets = 0;
858 player_status->max_ice_bullets = 0;
860 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
861 if (type == ICE_BONUS) player_status->max_ice_bullets++;
863 player_status->bonus = type;
868 Player::set_visible(bool visible)
870 this->visible = visible;
872 set_group(COLGROUP_MOVING);
874 set_group(COLGROUP_DISABLED);
878 Player::get_visible()
886 kick_timer.start(KICK_TIME);
890 Player::draw(DrawingContext& context)
895 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
896 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
897 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
898 float py = Sector::current()->camera->get_translation().y;
899 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
900 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
903 TuxBodyParts* tux_body;
905 if (player_status->bonus == GROWUP_BONUS)
907 else if (player_status->bonus == FIRE_BONUS)
909 else if (player_status->bonus == ICE_BONUS)
912 tux_body = small_tux;
914 int layer = LAYER_OBJECTS + 1;
916 /* Set Tux sprite action */
919 tux_body->set_action("skid-left");
921 else if (backflipping)
924 tux_body->set_action("backflip-left");
926 tux_body->set_action("backflip-right");
928 else if (duck && is_big())
931 tux_body->set_action("duck-left");
933 tux_body->set_action("duck-right");
935 else if (skidding_timer.started() && !skidding_timer.check())
938 tux_body->set_action("skid-left");
940 tux_body->set_action("skid-right");
942 else if (kick_timer.started() && !kick_timer.check())
945 tux_body->set_action("kick-left");
947 tux_body->set_action("kick-right");
949 else if (butt_jump && is_big())
952 tux_body->set_action("buttjump-left");
954 tux_body->set_action("buttjump-right");
956 else if (!on_ground())
959 tux_body->set_action("jump-left");
961 tux_body->set_action("jump-right");
965 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
968 tux_body->set_action("stand-left");
970 tux_body->set_action("stand-right");
975 tux_body->set_action("walk-left");
977 tux_body->set_action("walk-right");
981 if(idle_timer.check())
986 tux_body->head->set_action("idle-left", 1);
988 tux_body->head->set_action("idle-right", 1);
993 // Tux is holding something
994 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
995 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
1000 tux_body->arms->set_action("duck+grab-left");
1001 else // dir == RIGHT
1002 tux_body->arms->set_action("duck+grab-right");
1007 tux_body->arms->set_action("grab-left");
1008 else // dir == RIGHT
1009 tux_body->arms->set_action("grab-right");
1015 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
1017 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
1019 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
1020 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
1022 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
1023 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
1026 else if (safe_timer.started() && size_t(game_time*40)%2)
1029 tux_body->draw(context, get_pos(), layer, grabbed_object);
1034 Player::collision_tile(uint32_t tile_attributes)
1036 if(tile_attributes & Tile::HURTS)
1041 if( tile_attributes & Tile::WATER ){
1047 if( tile_attributes & Tile::WATER ){
1050 sound_manager->play( "sounds/splash.ogg" );
1057 Player::collision_solid(const CollisionHit& hit)
1060 if(physic.get_velocity_y() > 0)
1061 physic.set_velocity_y(0);
1063 on_ground_flag = true;
1064 floor_normal = hit.slope_normal;
1065 } else if(hit.top) {
1066 if(physic.get_velocity_y() < 0)
1067 physic.set_velocity_y(.2f);
1070 if(hit.left || hit.right) {
1071 physic.set_velocity_x(0);
1076 if(hit.left || hit.right) {
1078 } else if(hit.top || hit.bottom) {
1085 Player::collision(GameObject& other, const CollisionHit& hit)
1087 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1092 if(hit.left || hit.right) {
1093 try_grab(); //grab objects right now, in update it will be too late
1096 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1098 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1099 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1100 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1102 if(controller->pressed(Controller::UP))
1103 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1109 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1110 if(badguy != NULL) {
1111 if(safe_timer.started() || invincible_timer.started())
1121 Player::make_invincible()
1123 sound_manager->play("sounds/invincible.wav");
1124 invincible_timer.start(TUX_INVINCIBLE_TIME);
1125 Sector::current()->play_music(HERRING_MUSIC);
1130 Player::kill(bool completely)
1132 if(dying || deactivated)
1135 if(!completely && (safe_timer.started() || invincible_timer.started()))
1138 sound_manager->play("sounds/hurt.wav");
1139 if (climbing) stop_climbing(*climbing);
1141 physic.set_velocity_x(0);
1143 if(!completely && (is_big() || growing_timer.started())) {
1144 if(player_status->bonus == FIRE_BONUS
1145 || player_status->bonus == ICE_BONUS) {
1146 safe_timer.start(TUX_SAFE_TIME);
1147 set_bonus(GROWUP_BONUS, true);
1148 } else if(player_status->bonus == GROWUP_BONUS) {
1149 //growing_timer.start(GROWING_TIME);
1150 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1151 adjust_height(30.8f);
1153 set_bonus(NO_BONUS, true);
1154 } else if(player_status->bonus == NO_BONUS) {
1155 growing_timer.stop();
1156 safe_timer.start(TUX_SAFE_TIME);
1157 adjust_height(30.8f);
1161 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1163 for (int i = 0; i < 5; i++)
1165 // the numbers: starting x, starting y, velocity y
1166 Sector::current()->add_object(new FallingCoin(get_pos() +
1167 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1168 systemRandom.rand(-100,100)));
1170 player_status->coins -= std::max(player_status->coins/10, 25);
1174 GameSession::current()->set_reset_point("", Vector());
1176 physic.enable_gravity(true);
1177 physic.set_acceleration(0, 0);
1178 physic.set_velocity(0, -700);
1179 set_bonus(NO_BONUS, true);
1181 dying_timer.start(3.0);
1182 set_group(COLGROUP_DISABLED);
1184 DisplayEffect* effect = new DisplayEffect();
1185 effect->fade_out(3.0);
1186 Sector::current()->add_object(effect);
1187 sound_manager->stop_music(3.0);
1192 Player::move(const Vector& vector)
1196 // TODO: do we need the following? Seems irrelevant to moving the player
1198 set_size(31.8f, 63.8f);
1200 set_size(31.8f, 31.8f);
1202 last_ground_y = vector.y;
1203 if (climbing) stop_climbing(*climbing);
1209 Player::check_bounds(Camera* camera)
1211 /* Keep tux in bounds: */
1212 if (get_pos().x < 0) {
1213 // Lock Tux to the size of the level, so that he doesn't fall of
1215 set_pos(Vector(0, get_pos().y));
1218 /* fallen out of the level? */
1219 if (get_pos().y > Sector::current()->get_height()) {
1224 // can happen if back scrolling is disabled
1225 if(get_pos().x < camera->get_translation().x) {
1226 set_pos(Vector(camera->get_translation().x, get_pos().y));
1228 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1231 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1237 Player::add_velocity(const Vector& velocity)
1239 physic.set_velocity(physic.get_velocity() + velocity);
1243 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1245 if (end_speed.x > 0)
1246 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1247 if (end_speed.x < 0)
1248 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1249 if (end_speed.y > 0)
1250 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1251 if (end_speed.y < 0)
1252 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1256 Player::bounce(BadGuy& )
1258 if(controller->hold(Controller::JUMP))
1259 physic.set_velocity_y(-520);
1261 physic.set_velocity_y(-300);
1264 //Scripting Functions Below
1267 Player::deactivate()
1272 physic.set_velocity_x(0);
1273 physic.set_velocity_y(0);
1274 physic.set_acceleration_x(0);
1275 physic.set_acceleration_y(0);
1276 if (climbing) stop_climbing(*climbing);
1284 deactivated = false;
1287 void Player::walk(float speed)
1289 physic.set_velocity_x(speed);
1293 Player::set_ghost_mode(bool enable)
1295 if (ghost_mode == enable)
1298 if (climbing) stop_climbing(*climbing);
1302 set_group(COLGROUP_DISABLED);
1303 physic.enable_gravity(false);
1304 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1307 set_group(COLGROUP_MOVING);
1308 physic.enable_gravity(true);
1309 log_debug << "You feel solid again." << std::endl;
1315 Player::start_climbing(Climbable& climbable)
1317 if (climbing == &climbable) return;
1319 climbing = &climbable;
1320 physic.enable_gravity(false);
1321 physic.set_velocity(0, 0);
1322 physic.set_acceleration(0, 0);
1326 Player::stop_climbing(Climbable& /*climbable*/)
1328 if (!climbing) return;
1332 if (grabbed_object) {
1333 grabbed_object->ungrab(*this, dir);
1334 grabbed_object = NULL;
1337 physic.enable_gravity(true);
1338 physic.set_velocity(0, 0);
1339 physic.set_acceleration(0, 0);
1341 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1342 on_ground_flag = true;
1343 // TODO: This won't help. Why?
1349 Player::handle_input_climbing()
1352 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1358 if (controller->hold(Controller::LEFT)) {
1362 if (controller->hold(Controller::RIGHT)) {
1366 if (controller->hold(Controller::UP)) {
1369 if (controller->hold(Controller::DOWN)) {
1372 if (controller->hold(Controller::JUMP)) {
1374 stop_climbing(*climbing);
1380 if (controller->hold(Controller::ACTION)) {
1381 stop_climbing(*climbing);
1384 physic.set_velocity(vx, vy);
1385 physic.set_acceleration(0, 0);