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 : scripting_controller(0),
131 player_status(_player_status),
132 scripting_controller_old(0),
133 grabbed_object(NULL), ghost_mode(false), edit_mode(false), climbing(0)
136 controller = main_controller;
137 scripting_controller = new CodeController();
138 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
139 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
140 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
141 airarrow.reset(new Surface("images/engine/hud/airarrow.png"));
143 sound_manager->preload("sounds/bigjump.wav");
144 sound_manager->preload("sounds/jump.wav");
145 sound_manager->preload("sounds/hurt.wav");
146 sound_manager->preload("sounds/skid.wav");
147 sound_manager->preload("sounds/flip.wav");
148 sound_manager->preload("sounds/invincible.wav");
149 sound_manager->preload("sounds/splash.ogg");
156 if (climbing) stop_climbing(*climbing);
157 delete smalltux_gameover;
158 delete smalltux_star;
160 delete scripting_controller;
167 set_size(31.8f, 62.8f);
169 set_size(31.8f, 30.8f);
179 fall_mode = ON_GROUND;
184 backflipping = false;
185 backflip_direction = 0;
188 speedlimit = 0; //no special limit
190 on_ground_flag = false;
191 grabbed_object = NULL;
199 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
204 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
208 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
213 Scripting::unexpose_object(vm, table_idx, name);
217 Player::get_speedlimit()
223 Player::set_speedlimit(float newlimit)
229 Player::set_controller(Controller* controller)
231 this->controller = controller;
235 Player::use_scripting_controller(bool use_or_release)
237 if ((use_or_release == true) && (controller != scripting_controller)) {
238 scripting_controller_old = get_controller();
239 set_controller(scripting_controller);
241 if ((use_or_release == false) && (controller == scripting_controller)) {
242 set_controller(scripting_controller_old);
243 scripting_controller_old = 0;
248 Player::do_scripting_controller(std::string control, bool pressed)
250 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
251 if(control == std::string(Controller::controlNames[i])) {
252 scripting_controller->press(Controller::Control(i), pressed);
258 Player::adjust_height(float new_height)
261 bbox2.move(Vector(0, bbox.get_height() - new_height));
262 bbox2.set_height(new_height);
264 if(new_height > bbox.get_height()) {
265 Rect additional_space = bbox2;
266 additional_space.set_height(new_height - bbox.get_height());
267 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
271 // adjust bbox accordingly
272 // note that we use members of moving_object for this, so we can run this during CD, too
274 set_size(bbox2.get_width(), bbox2.get_height());
279 Player::trigger_sequence(std::string sequence_name)
281 if (climbing) stop_climbing(*climbing);
282 GameSession::current()->start_sequence(sequence_name);
286 Player::update(float elapsed_time)
293 if(dying && dying_timer.check()) {
298 if(!dying && !deactivated)
301 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
305 // extend/shrink tux collision rectangle so that we fall through/walk over 1
307 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
313 // on downward slopes, adjust vertical velocity so tux walks smoothly down
315 if(floor_normal.y != 0) {
316 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
317 physic.set_velocity_y(250);
322 // handle backflipping
324 //prevent player from changing direction when backflipping
325 dir = (backflip_direction == 1) ? LEFT : RIGHT;
326 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
331 fall_mode = ON_GROUND;
332 last_ground_y = get_pos().y;
334 if(get_pos().y > last_ground_y)
336 else if(fall_mode == ON_GROUND)
340 // check if we landed
343 if (backflipping && (!backflip_timer.started())) {
344 backflipping = false;
345 backflip_direction = 0;
347 // if controls are currently deactivated, we take care of standing up ourselves
353 // calculate movement for this frame
354 movement = physic.get_movement(elapsed_time);
356 if(grabbed_object != NULL && !dying) {
357 Vector pos = get_pos() +
358 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
359 grabbed_object->grab(*this, pos, dir);
362 if(grabbed_object != NULL && dying){
363 grabbed_object->ungrab(*this, dir);
364 grabbed_object = NULL;
367 on_ground_flag = false;
369 // when invincible, spawn particles
370 if (invincible_timer.started() && !dying)
372 if (systemRandom.rand(0, 2) == 0) {
373 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
374 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
375 Vector ppos = Vector(px, py);
376 Vector pspeed = Vector(0, 0);
377 Vector paccel = Vector(0, 0);
378 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
379 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
380 // make every other a longer sparkle to make trail a bit fuzzy
381 if (size_t(game_time*20)%2) {
382 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
384 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
387 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
397 return on_ground_flag;
403 if(player_status->bonus == NO_BONUS)
410 Player::apply_friction()
412 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
413 physic.set_velocity_x(0);
414 physic.set_acceleration_x(0);
415 } else if(physic.get_velocity_x() < 0) {
416 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
417 } else if(physic.get_velocity_x() > 0) {
418 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
423 Player::handle_horizontal_input()
425 float vx = physic.get_velocity_x();
426 float vy = physic.get_velocity_y();
427 float ax = physic.get_acceleration_x();
428 float ay = physic.get_acceleration_y();
431 if(!duck || physic.get_velocity_y() != 0) {
432 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
436 } else if(!controller->hold(Controller::LEFT)
437 && controller->hold(Controller::RIGHT)) {
444 // do not run if action key is pressed or we're holding something
445 // so tux can only walk while shooting
446 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
447 ax = dirsign * WALK_ACCELERATION_X;
449 if(vx >= MAX_WALK_XM && dirsign > 0) {
452 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
457 if( vx * dirsign < MAX_WALK_XM ) {
458 ax = dirsign * WALK_ACCELERATION_X;
460 ax = dirsign * RUN_ACCELERATION_X;
463 if(vx >= MAX_RUN_XM && dirsign > 0) {
466 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
472 // we can reach WALK_SPEED without any acceleration
473 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
474 vx = dirsign * WALK_SPEED;
478 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
479 vx = dirsign * speedlimit;
483 // changing directions?
484 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
486 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
487 skidding_timer.start(SKID_TIME);
488 sound_manager->play("sounds/skid.wav");
489 // dust some particles
490 Sector::current()->add_object(
492 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
493 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
494 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
503 physic.set_velocity(vx, vy);
504 physic.set_acceleration(ax, ay);
506 // we get slower when not pressing any keys
528 if (physic.get_velocity_y() != 0)
533 if (adjust_height(31.8f)) {
535 unduck_hurt_timer.stop();
542 Player::do_standup() {
550 if (adjust_height(63.8f)) {
552 unduck_hurt_timer.stop();
554 // if timer is not already running, start it.
555 if (unduck_hurt_timer.get_period() == 0) {
556 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
558 else if (unduck_hurt_timer.check()) {
566 Player::do_backflip() {
572 backflip_direction = (dir == LEFT)?(+1):(-1);
575 sound_manager->play("sounds/flip.wav");
576 backflip_timer.start(0.15f);
580 Player::do_jump(float yspeed) {
584 physic.set_velocity_y(yspeed);
585 //bbox.move(Vector(0, -1));
587 on_ground_flag = false;
592 sound_manager->play("sounds/bigjump.wav");
594 sound_manager->play("sounds/jump.wav");
599 Player::handle_vertical_input()
602 if(controller->pressed(Controller::JUMP) && (can_jump)) {
604 // when running, only jump a little bit; else do a backflip
605 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
607 // jump a bit higher if we are running; else do a normal jump
608 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
611 // Let go of jump key
612 else if(!controller->hold(Controller::JUMP)) {
613 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
615 physic.set_velocity_y(0);
619 /* In case the player has pressed Down while in a certain range of air,
620 enable butt jump action */
621 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
625 /* When Down is not held anymore, disable butt jump */
626 if(butt_jump && !controller->hold(Controller::DOWN))
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");
859 growing_timer.start(GROWING_TIME);
860 if (climbing) stop_climbing(*climbing);
863 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
864 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
865 // visually lose helmet
866 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
867 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
868 Vector paccel = Vector(0, 1000);
869 std::string action = (dir==LEFT)?"left":"right";
870 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
871 if (climbing) stop_climbing(*climbing);
873 if ((player_status->bonus == ICE_BONUS) && (animate)) {
875 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
876 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
877 Vector paccel = Vector(0, 1000);
878 std::string action = (dir==LEFT)?"left":"right";
879 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
880 if (climbing) stop_climbing(*climbing);
882 player_status->max_fire_bullets = 0;
883 player_status->max_ice_bullets = 0;
885 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
886 if (type == ICE_BONUS) player_status->max_ice_bullets++;
888 player_status->bonus = type;
893 Player::set_visible(bool visible)
895 this->visible = visible;
897 set_group(COLGROUP_MOVING);
899 set_group(COLGROUP_DISABLED);
903 Player::get_visible()
911 kick_timer.start(KICK_TIME);
915 Player::draw(DrawingContext& context)
920 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
921 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
922 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
923 float py = Sector::current()->camera->get_translation().y;
924 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
925 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
928 TuxBodyParts* tux_body;
930 if (player_status->bonus == GROWUP_BONUS)
932 else if (player_status->bonus == FIRE_BONUS)
934 else if (player_status->bonus == ICE_BONUS)
937 tux_body = small_tux;
939 int layer = LAYER_OBJECTS + 1;
941 /* Set Tux sprite action */
944 tux_body->set_action("skid-left");
946 else if (backflipping)
949 tux_body->set_action("backflip-left");
951 tux_body->set_action("backflip-right");
953 else if (duck && is_big())
956 tux_body->set_action("duck-left");
958 tux_body->set_action("duck-right");
960 else if (skidding_timer.started() && !skidding_timer.check())
963 tux_body->set_action("skid-left");
965 tux_body->set_action("skid-right");
967 else if (kick_timer.started() && !kick_timer.check())
970 tux_body->set_action("kick-left");
972 tux_body->set_action("kick-right");
974 else if (butt_jump && is_big())
977 tux_body->set_action("buttjump-left");
979 tux_body->set_action("buttjump-right");
981 else if (!on_ground())
984 tux_body->set_action("jump-left");
986 tux_body->set_action("jump-right");
990 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
993 tux_body->set_action("stand-left");
995 tux_body->set_action("stand-right");
1000 tux_body->set_action("walk-left");
1001 else // dir == RIGHT
1002 tux_body->set_action("walk-right");
1006 if(idle_timer.check())
1011 tux_body->head->set_action("idle-left", 1);
1012 else // dir == RIGHT
1013 tux_body->head->set_action("idle-right", 1);
1018 // Tux is holding something
1019 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1020 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
1025 tux_body->arms->set_action("duck+grab-left");
1026 else // dir == RIGHT
1027 tux_body->arms->set_action("duck+grab-right");
1032 tux_body->arms->set_action("grab-left");
1033 else // dir == RIGHT
1034 tux_body->arms->set_action("grab-right");
1040 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
1042 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
1044 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
1045 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
1047 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
1048 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
1051 else if (safe_timer.started() && size_t(game_time*40)%2)
1054 tux_body->draw(context, get_pos(), layer, grabbed_object);
1059 Player::collision_tile(uint32_t tile_attributes)
1061 if(tile_attributes & Tile::HURTS)
1066 if( tile_attributes & Tile::WATER ){
1072 if( tile_attributes & Tile::WATER ){
1075 sound_manager->play( "sounds/splash.ogg" );
1082 Player::collision_solid(const CollisionHit& hit)
1085 if(physic.get_velocity_y() > 0)
1086 physic.set_velocity_y(0);
1088 on_ground_flag = true;
1089 floor_normal = hit.slope_normal;
1090 } else if(hit.top) {
1091 if(physic.get_velocity_y() < 0)
1092 physic.set_velocity_y(.2f);
1095 if(hit.left || hit.right) {
1096 physic.set_velocity_x(0);
1101 if(hit.left || hit.right) {
1103 } else if(hit.top || hit.bottom) {
1110 Player::collision(GameObject& other, const CollisionHit& hit)
1112 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1117 if(hit.left || hit.right) {
1118 try_grab(); //grab objects right now, in update it will be too late
1121 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1123 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1124 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1125 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1127 if(controller->pressed(Controller::UP))
1128 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1134 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1135 if(badguy != NULL) {
1136 if(safe_timer.started() || invincible_timer.started())
1146 Player::make_invincible()
1148 sound_manager->play("sounds/invincible.wav");
1149 invincible_timer.start(TUX_INVINCIBLE_TIME);
1150 Sector::current()->play_music(HERRING_MUSIC);
1155 Player::kill(bool completely)
1157 if(dying || deactivated)
1160 if(!completely && (safe_timer.started() || invincible_timer.started()))
1163 sound_manager->play("sounds/hurt.wav");
1165 if (climbing) stop_climbing(*climbing);
1167 physic.set_velocity_x(0);
1169 if(!completely && (is_big() || growing_timer.started())) {
1170 if(player_status->bonus == FIRE_BONUS
1171 || player_status->bonus == ICE_BONUS) {
1172 safe_timer.start(TUX_SAFE_TIME);
1173 set_bonus(GROWUP_BONUS, true);
1174 } else if(player_status->bonus == GROWUP_BONUS) {
1175 //growing_timer.start(GROWING_TIME);
1176 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1177 adjust_height(30.8f);
1179 set_bonus(NO_BONUS, true);
1180 } else if(player_status->bonus == NO_BONUS) {
1181 growing_timer.stop();
1182 safe_timer.start(TUX_SAFE_TIME);
1183 adjust_height(30.8f);
1188 // do not die when in edit mode
1190 set_ghost_mode(true);
1194 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1196 for (int i = 0; i < 5; i++)
1198 // the numbers: starting x, starting y, velocity y
1199 Sector::current()->add_object(new FallingCoin(get_pos() +
1200 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1201 systemRandom.rand(-100,100)));
1203 player_status->coins -= std::max(player_status->coins/10, 25);
1207 GameSession::current()->set_reset_point("", Vector());
1209 physic.enable_gravity(true);
1210 physic.set_acceleration(0, 0);
1211 physic.set_velocity(0, -700);
1212 set_bonus(NO_BONUS, true);
1214 dying_timer.start(3.0);
1215 set_group(COLGROUP_DISABLED);
1217 DisplayEffect* effect = new DisplayEffect();
1218 effect->fade_out(3.0);
1219 Sector::current()->add_object(effect);
1220 sound_manager->stop_music(3.0);
1225 Player::move(const Vector& vector)
1229 // TODO: do we need the following? Seems irrelevant to moving the player
1231 set_size(31.8f, 63.8f);
1233 set_size(31.8f, 31.8f);
1235 last_ground_y = vector.y;
1236 if (climbing) stop_climbing(*climbing);
1242 Player::check_bounds(Camera* camera)
1244 /* Keep tux in bounds: */
1245 if (get_pos().x < 0) {
1246 // Lock Tux to the size of the level, so that he doesn't fall of
1248 set_pos(Vector(0, get_pos().y));
1251 /* fallen out of the level? */
1252 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1257 // can happen if back scrolling is disabled
1258 if(get_pos().x < camera->get_translation().x) {
1259 set_pos(Vector(camera->get_translation().x, get_pos().y));
1261 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1264 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1270 Player::add_velocity(const Vector& velocity)
1272 physic.set_velocity(physic.get_velocity() + velocity);
1276 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1278 if (end_speed.x > 0)
1279 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1280 if (end_speed.x < 0)
1281 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1282 if (end_speed.y > 0)
1283 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1284 if (end_speed.y < 0)
1285 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1289 Player::bounce(BadGuy& )
1291 if(controller->hold(Controller::JUMP))
1292 physic.set_velocity_y(-520);
1294 physic.set_velocity_y(-300);
1297 //Scripting Functions Below
1300 Player::deactivate()
1305 physic.set_velocity_x(0);
1306 physic.set_velocity_y(0);
1307 physic.set_acceleration_x(0);
1308 physic.set_acceleration_y(0);
1309 if (climbing) stop_climbing(*climbing);
1317 deactivated = false;
1320 void Player::walk(float speed)
1322 physic.set_velocity_x(speed);
1326 Player::set_ghost_mode(bool enable)
1328 if (ghost_mode == enable)
1331 if (climbing) stop_climbing(*climbing);
1335 set_group(COLGROUP_DISABLED);
1336 physic.enable_gravity(false);
1337 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1340 set_group(COLGROUP_MOVING);
1341 physic.enable_gravity(true);
1342 log_debug << "You feel solid again." << std::endl;
1348 Player::set_edit_mode(bool enable)
1354 Player::start_climbing(Climbable& climbable)
1356 if (climbing == &climbable) return;
1358 climbing = &climbable;
1359 physic.enable_gravity(false);
1360 physic.set_velocity(0, 0);
1361 physic.set_acceleration(0, 0);
1365 Player::stop_climbing(Climbable& /*climbable*/)
1367 if (!climbing) return;
1371 if (grabbed_object) {
1372 grabbed_object->ungrab(*this, dir);
1373 grabbed_object = NULL;
1376 physic.enable_gravity(true);
1377 physic.set_velocity(0, 0);
1378 physic.set_acceleration(0, 0);
1380 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1381 on_ground_flag = true;
1382 // TODO: This won't help. Why?
1388 Player::handle_input_climbing()
1391 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1397 if (controller->hold(Controller::LEFT)) {
1401 if (controller->hold(Controller::RIGHT)) {
1405 if (controller->hold(Controller::UP)) {
1408 if (controller->hold(Controller::DOWN)) {
1411 if (controller->hold(Controller::JUMP)) {
1413 stop_climbing(*climbing);
1419 if (controller->hold(Controller::ACTION)) {
1420 stop_climbing(*climbing);
1423 physic.set_velocity(vx, vy);
1424 physic.set_acceleration(0, 0);