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");
150 sound_manager->preload("sounds/shoot.wav");
157 if (climbing) stop_climbing(*climbing);
158 delete smalltux_gameover;
159 delete smalltux_star;
161 delete scripting_controller;
168 set_size(31.8f, 62.8f);
170 set_size(31.8f, 30.8f);
180 fall_mode = ON_GROUND;
185 backflipping = false;
186 backflip_direction = 0;
189 speedlimit = 0; //no special limit
191 on_ground_flag = false;
192 grabbed_object = NULL;
200 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
205 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
209 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
214 Scripting::unexpose_object(vm, table_idx, name);
218 Player::get_speedlimit()
224 Player::set_speedlimit(float newlimit)
230 Player::set_controller(Controller* controller)
232 this->controller = controller;
236 Player::use_scripting_controller(bool use_or_release)
238 if ((use_or_release == true) && (controller != scripting_controller)) {
239 scripting_controller_old = get_controller();
240 set_controller(scripting_controller);
242 if ((use_or_release == false) && (controller == scripting_controller)) {
243 set_controller(scripting_controller_old);
244 scripting_controller_old = 0;
249 Player::do_scripting_controller(std::string control, bool pressed)
251 for(int i = 0; Controller::controlNames[i] != 0; ++i) {
252 if(control == std::string(Controller::controlNames[i])) {
253 scripting_controller->press(Controller::Control(i), pressed);
259 Player::adjust_height(float new_height)
262 bbox2.move(Vector(0, bbox.get_height() - new_height));
263 bbox2.set_height(new_height);
265 if(new_height > bbox.get_height()) {
266 Rect additional_space = bbox2;
267 additional_space.set_height(new_height - bbox.get_height());
268 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
272 // adjust bbox accordingly
273 // note that we use members of moving_object for this, so we can run this during CD, too
275 set_size(bbox2.get_width(), bbox2.get_height());
280 Player::trigger_sequence(std::string sequence_name)
282 if (climbing) stop_climbing(*climbing);
283 GameSession::current()->start_sequence(sequence_name);
287 Player::update(float elapsed_time)
294 if(dying && dying_timer.check()) {
299 if(!dying && !deactivated)
302 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
306 // extend/shrink tux collision rectangle so that we fall through/walk over 1
308 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
314 // on downward slopes, adjust vertical velocity so tux walks smoothly down
316 if(floor_normal.y != 0) {
317 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
318 physic.set_velocity_y(250);
323 // handle backflipping
325 //prevent player from changing direction when backflipping
326 dir = (backflip_direction == 1) ? LEFT : RIGHT;
327 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
332 fall_mode = ON_GROUND;
333 last_ground_y = get_pos().y;
335 if(get_pos().y > last_ground_y)
337 else if(fall_mode == ON_GROUND)
341 // check if we landed
344 if (backflipping && (!backflip_timer.started())) {
345 backflipping = false;
346 backflip_direction = 0;
348 // if controls are currently deactivated, we take care of standing up ourselves
354 // calculate movement for this frame
355 movement = physic.get_movement(elapsed_time);
357 if(grabbed_object != NULL && !dying) {
358 Vector pos = get_pos() +
359 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
360 grabbed_object->grab(*this, pos, dir);
363 if(grabbed_object != NULL && dying){
364 grabbed_object->ungrab(*this, dir);
365 grabbed_object = NULL;
368 on_ground_flag = false;
370 // when invincible, spawn particles
371 if (invincible_timer.started() && !dying)
373 if (systemRandom.rand(0, 2) == 0) {
374 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
375 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
376 Vector ppos = Vector(px, py);
377 Vector pspeed = Vector(0, 0);
378 Vector paccel = Vector(0, 0);
379 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
380 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
381 // make every other a longer sparkle to make trail a bit fuzzy
382 if (size_t(game_time*20)%2) {
383 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
385 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
388 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
398 return on_ground_flag;
404 if(player_status->bonus == NO_BONUS)
411 Player::apply_friction()
413 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
414 physic.set_velocity_x(0);
415 physic.set_acceleration_x(0);
416 } else if(physic.get_velocity_x() < 0) {
417 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
418 } else if(physic.get_velocity_x() > 0) {
419 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
424 Player::handle_horizontal_input()
426 float vx = physic.get_velocity_x();
427 float vy = physic.get_velocity_y();
428 float ax = physic.get_acceleration_x();
429 float ay = physic.get_acceleration_y();
432 if(!duck || physic.get_velocity_y() != 0) {
433 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
437 } else if(!controller->hold(Controller::LEFT)
438 && controller->hold(Controller::RIGHT)) {
445 // do not run if action key is pressed or we're holding something
446 // so tux can only walk while shooting
447 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
448 ax = dirsign * WALK_ACCELERATION_X;
450 if(vx >= MAX_WALK_XM && dirsign > 0) {
453 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
458 if( vx * dirsign < MAX_WALK_XM ) {
459 ax = dirsign * WALK_ACCELERATION_X;
461 ax = dirsign * RUN_ACCELERATION_X;
464 if(vx >= MAX_RUN_XM && dirsign > 0) {
467 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
473 // we can reach WALK_SPEED without any acceleration
474 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
475 vx = dirsign * WALK_SPEED;
479 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
480 vx = dirsign * speedlimit;
484 // changing directions?
485 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
487 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
488 skidding_timer.start(SKID_TIME);
489 sound_manager->play("sounds/skid.wav");
490 // dust some particles
491 Sector::current()->add_object(
493 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
494 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
495 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
504 physic.set_velocity(vx, vy);
505 physic.set_acceleration(ax, ay);
507 // we get slower when not pressing any keys
529 if (physic.get_velocity_y() != 0)
534 if (adjust_height(31.8f)) {
536 unduck_hurt_timer.stop();
543 Player::do_standup() {
551 if (adjust_height(63.8f)) {
553 unduck_hurt_timer.stop();
555 // if timer is not already running, start it.
556 if (unduck_hurt_timer.get_period() == 0) {
557 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
559 else if (unduck_hurt_timer.check()) {
567 Player::do_backflip() {
573 backflip_direction = (dir == LEFT)?(+1):(-1);
576 sound_manager->play("sounds/flip.wav");
577 backflip_timer.start(0.15f);
581 Player::do_jump(float yspeed) {
585 physic.set_velocity_y(yspeed);
586 //bbox.move(Vector(0, -1));
588 on_ground_flag = false;
593 sound_manager->play("sounds/bigjump.wav");
595 sound_manager->play("sounds/jump.wav");
600 Player::handle_vertical_input()
603 if(controller->pressed(Controller::JUMP) && (can_jump)) {
605 // when running, only jump a little bit; else do a backflip
606 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
608 // jump a bit higher if we are running; else do a normal jump
609 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
612 // Let go of jump key
613 else if(!controller->hold(Controller::JUMP)) {
614 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
616 physic.set_velocity_y(0);
620 /* In case the player has pressed Down while in a certain range of air,
621 enable butt jump action */
622 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
626 /* When Down is not held anymore, disable butt jump */
627 if(butt_jump && !controller->hold(Controller::DOWN))
631 physic.set_acceleration_y(0);
634 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
635 physic.set_acceleration_y(-2000);
636 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
642 Player::handle_input()
645 handle_input_ghost();
649 handle_input_climbing();
654 if( controller->released( Controller::PEEK_LEFT ) ) {
657 if( controller->released( Controller::PEEK_RIGHT ) ) {
660 if( controller->released( Controller::UP ) ) {
663 if( controller->released( Controller::DOWN ) ) {
666 if( controller->pressed( Controller::PEEK_LEFT ) ) {
669 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
672 if( controller->pressed( Controller::UP ) ) {
675 if( controller->pressed( Controller::DOWN ) ) {
679 /* Handle horizontal movement: */
680 if (!backflipping) handle_horizontal_input();
683 if (on_ground() && !controller->hold(Controller::JUMP))
686 /* Handle vertical movement: */
687 handle_vertical_input();
690 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
691 if(Sector::current()->add_bullet(
692 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
693 : Vector(32, bbox.get_height()/2)),
694 physic.get_velocity_x(), dir))
695 shooting_timer.start(SHOOTING_TIME);
698 /* Duck or Standup! */
699 if (controller->hold(Controller::DOWN)) {
708 if(!controller->hold(Controller::ACTION) && grabbed_object) {
709 // move the grabbed object a bit away from tux
710 Vector pos = get_pos() +
711 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
712 bbox.get_height()*0.66666 - 32);
713 Rect dest(pos, pos + Vector(32, 32));
714 if(Sector::current()->is_free_of_movingstatics(dest)) {
715 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
717 moving_object->set_pos(pos);
719 log_debug << "Non MovingObject grabbed?!?" << std::endl;
721 if(controller->hold(Controller::UP)) {
722 grabbed_object->ungrab(*this, UP);
724 grabbed_object->ungrab(*this, dir);
726 grabbed_object = NULL;
734 if(controller->hold(Controller::ACTION) && !grabbed_object
736 Sector* sector = Sector::current();
739 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
741 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
744 for(Sector::Portables::iterator i = sector->portables.begin();
745 i != sector->portables.end(); ++i) {
746 Portable* portable = *i;
747 if(!portable->is_portable())
750 // make sure the Portable is a MovingObject
751 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
752 assert(moving_object);
753 if(moving_object == NULL)
756 // make sure the Portable isn't currently non-solid
757 if(moving_object->get_group() == COLGROUP_DISABLED) continue;
759 // check if we are within reach
760 if(moving_object->get_bbox().contains(pos)) {
761 if (climbing) stop_climbing(*climbing);
762 grabbed_object = portable;
763 grabbed_object->grab(*this, get_pos(), dir);
771 Player::handle_input_ghost()
775 if (controller->hold(Controller::LEFT)) {
777 vx -= MAX_RUN_XM * 2;
779 if (controller->hold(Controller::RIGHT)) {
781 vx += MAX_RUN_XM * 2;
783 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
784 vy -= MAX_RUN_XM * 2;
786 if (controller->hold(Controller::DOWN)) {
787 vy += MAX_RUN_XM * 2;
789 if (controller->hold(Controller::ACTION)) {
790 set_ghost_mode(false);
792 physic.set_velocity(vx, vy);
793 physic.set_acceleration(0, 0);
797 Player::add_coins(int count)
799 player_status->add_coins(count);
805 return player_status->coins;
809 Player::add_bonus(const std::string& bonustype)
811 BonusType type = NO_BONUS;
813 if(bonustype == "grow") {
815 } else if(bonustype == "fireflower") {
817 } else if(bonustype == "iceflower") {
819 } else if(bonustype == "none") {
822 std::ostringstream msg;
823 msg << "Unknown bonus type " << bonustype;
824 throw std::runtime_error(msg.str());
827 return add_bonus(type);
831 Player::add_bonus(BonusType type, bool animate)
833 // always ignore NO_BONUS
834 if (type == NO_BONUS) {
838 // ignore GROWUP_BONUS if we're already big
839 if (type == GROWUP_BONUS) {
840 if (player_status->bonus == GROWUP_BONUS)
842 if (player_status->bonus == FIRE_BONUS)
844 if (player_status->bonus == ICE_BONUS)
848 return set_bonus(type, animate);
852 Player::set_bonus(BonusType type, bool animate)
854 if(player_status->bonus == NO_BONUS) {
855 if (!adjust_height(62.8f)) {
856 printf("can't adjust\n");
860 growing_timer.start(GROWING_TIME);
861 if (climbing) stop_climbing(*climbing);
864 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
865 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
866 // visually lose helmet
867 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
868 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
869 Vector paccel = Vector(0, 1000);
870 std::string action = (dir==LEFT)?"left":"right";
871 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
872 if (climbing) stop_climbing(*climbing);
874 if ((player_status->bonus == ICE_BONUS) && (animate)) {
876 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
877 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
878 Vector paccel = Vector(0, 1000);
879 std::string action = (dir==LEFT)?"left":"right";
880 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
881 if (climbing) stop_climbing(*climbing);
883 player_status->max_fire_bullets = 0;
884 player_status->max_ice_bullets = 0;
886 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
887 if (type == ICE_BONUS) player_status->max_ice_bullets++;
889 player_status->bonus = type;
894 Player::set_visible(bool visible)
896 this->visible = visible;
898 set_group(COLGROUP_MOVING);
900 set_group(COLGROUP_DISABLED);
904 Player::get_visible()
912 kick_timer.start(KICK_TIME);
916 Player::draw(DrawingContext& context)
921 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
922 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
923 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
924 float py = Sector::current()->camera->get_translation().y;
925 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
926 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
929 TuxBodyParts* tux_body;
931 if (player_status->bonus == GROWUP_BONUS)
933 else if (player_status->bonus == FIRE_BONUS)
935 else if (player_status->bonus == ICE_BONUS)
938 tux_body = small_tux;
940 int layer = LAYER_OBJECTS + 1;
942 /* Set Tux sprite action */
945 tux_body->set_action("skid-left");
947 else if (backflipping)
950 tux_body->set_action("backflip-left");
952 tux_body->set_action("backflip-right");
954 else if (duck && is_big())
957 tux_body->set_action("duck-left");
959 tux_body->set_action("duck-right");
961 else if (skidding_timer.started() && !skidding_timer.check())
964 tux_body->set_action("skid-left");
966 tux_body->set_action("skid-right");
968 else if (kick_timer.started() && !kick_timer.check())
971 tux_body->set_action("kick-left");
973 tux_body->set_action("kick-right");
975 else if (butt_jump && is_big())
978 tux_body->set_action("buttjump-left");
980 tux_body->set_action("buttjump-right");
982 else if (!on_ground())
985 tux_body->set_action("jump-left");
987 tux_body->set_action("jump-right");
991 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
994 tux_body->set_action("stand-left");
996 tux_body->set_action("stand-right");
1001 tux_body->set_action("walk-left");
1002 else // dir == RIGHT
1003 tux_body->set_action("walk-right");
1007 if(idle_timer.check())
1012 tux_body->head->set_action("idle-left", 1);
1013 else // dir == RIGHT
1014 tux_body->head->set_action("idle-right", 1);
1019 // Tux is holding something
1020 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
1021 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
1026 tux_body->arms->set_action("duck+grab-left");
1027 else // dir == RIGHT
1028 tux_body->arms->set_action("duck+grab-right");
1033 tux_body->arms->set_action("grab-left");
1034 else // dir == RIGHT
1035 tux_body->arms->set_action("grab-right");
1041 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
1043 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
1045 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
1046 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
1048 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
1049 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
1052 else if (safe_timer.started() && size_t(game_time*40)%2)
1055 tux_body->draw(context, get_pos(), layer, grabbed_object);
1060 Player::collision_tile(uint32_t tile_attributes)
1062 if(tile_attributes & Tile::HURTS)
1067 if( tile_attributes & Tile::WATER ){
1073 if( tile_attributes & Tile::WATER ){
1076 sound_manager->play( "sounds/splash.ogg" );
1083 Player::collision_solid(const CollisionHit& hit)
1086 if(physic.get_velocity_y() > 0)
1087 physic.set_velocity_y(0);
1089 on_ground_flag = true;
1090 floor_normal = hit.slope_normal;
1091 } else if(hit.top) {
1092 if(physic.get_velocity_y() < 0)
1093 physic.set_velocity_y(.2f);
1096 if(hit.left || hit.right) {
1097 physic.set_velocity_x(0);
1102 if(hit.left || hit.right) {
1104 } else if(hit.top || hit.bottom) {
1111 Player::collision(GameObject& other, const CollisionHit& hit)
1113 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1118 if(hit.left || hit.right) {
1119 try_grab(); //grab objects right now, in update it will be too late
1122 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1124 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1125 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1126 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1128 if(controller->pressed(Controller::UP))
1129 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1135 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1136 if(badguy != NULL) {
1137 if(safe_timer.started() || invincible_timer.started())
1147 Player::make_invincible()
1149 sound_manager->play("sounds/invincible.wav");
1150 invincible_timer.start(TUX_INVINCIBLE_TIME);
1151 Sector::current()->play_music(HERRING_MUSIC);
1156 Player::kill(bool completely)
1158 if(dying || deactivated)
1161 if(!completely && (safe_timer.started() || invincible_timer.started()))
1164 sound_manager->play("sounds/hurt.wav");
1166 if (climbing) stop_climbing(*climbing);
1168 physic.set_velocity_x(0);
1170 if(!completely && (is_big() || growing_timer.started())) {
1171 if(player_status->bonus == FIRE_BONUS
1172 || player_status->bonus == ICE_BONUS) {
1173 safe_timer.start(TUX_SAFE_TIME);
1174 set_bonus(GROWUP_BONUS, true);
1175 } else if(player_status->bonus == GROWUP_BONUS) {
1176 //growing_timer.start(GROWING_TIME);
1177 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1178 adjust_height(30.8f);
1180 set_bonus(NO_BONUS, true);
1181 } else if(player_status->bonus == NO_BONUS) {
1182 growing_timer.stop();
1183 safe_timer.start(TUX_SAFE_TIME);
1184 adjust_height(30.8f);
1189 // do not die when in edit mode
1191 set_ghost_mode(true);
1195 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1197 for (int i = 0; i < 5; i++)
1199 // the numbers: starting x, starting y, velocity y
1200 Sector::current()->add_object(new FallingCoin(get_pos() +
1201 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1202 systemRandom.rand(-100,100)));
1204 player_status->coins -= std::max(player_status->coins/10, 25);
1208 GameSession::current()->set_reset_point("", Vector());
1210 physic.enable_gravity(true);
1211 physic.set_acceleration(0, 0);
1212 physic.set_velocity(0, -700);
1213 set_bonus(NO_BONUS, true);
1215 dying_timer.start(3.0);
1216 set_group(COLGROUP_DISABLED);
1218 DisplayEffect* effect = new DisplayEffect();
1219 effect->fade_out(3.0);
1220 Sector::current()->add_object(effect);
1221 sound_manager->stop_music(3.0);
1226 Player::move(const Vector& vector)
1230 // TODO: do we need the following? Seems irrelevant to moving the player
1232 set_size(31.8f, 63.8f);
1234 set_size(31.8f, 31.8f);
1236 last_ground_y = vector.y;
1237 if (climbing) stop_climbing(*climbing);
1243 Player::check_bounds(Camera* camera)
1245 /* Keep tux in bounds: */
1246 if (get_pos().x < 0) {
1247 // Lock Tux to the size of the level, so that he doesn't fall of
1249 set_pos(Vector(0, get_pos().y));
1252 /* fallen out of the level? */
1253 if ((get_pos().y > Sector::current()->get_height()) && (!ghost_mode)) {
1258 // can happen if back scrolling is disabled
1259 if(get_pos().x < camera->get_translation().x) {
1260 set_pos(Vector(camera->get_translation().x, get_pos().y));
1262 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1265 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1271 Player::add_velocity(const Vector& velocity)
1273 physic.set_velocity(physic.get_velocity() + velocity);
1277 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1279 if (end_speed.x > 0)
1280 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1281 if (end_speed.x < 0)
1282 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1283 if (end_speed.y > 0)
1284 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1285 if (end_speed.y < 0)
1286 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1290 Player::bounce(BadGuy& )
1292 if(controller->hold(Controller::JUMP))
1293 physic.set_velocity_y(-520);
1295 physic.set_velocity_y(-300);
1298 //Scripting Functions Below
1301 Player::deactivate()
1306 physic.set_velocity_x(0);
1307 physic.set_velocity_y(0);
1308 physic.set_acceleration_x(0);
1309 physic.set_acceleration_y(0);
1310 if (climbing) stop_climbing(*climbing);
1318 deactivated = false;
1321 void Player::walk(float speed)
1323 physic.set_velocity_x(speed);
1327 Player::set_ghost_mode(bool enable)
1329 if (ghost_mode == enable)
1332 if (climbing) stop_climbing(*climbing);
1336 set_group(COLGROUP_DISABLED);
1337 physic.enable_gravity(false);
1338 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1341 set_group(COLGROUP_MOVING);
1342 physic.enable_gravity(true);
1343 log_debug << "You feel solid again." << std::endl;
1349 Player::set_edit_mode(bool enable)
1355 Player::start_climbing(Climbable& climbable)
1357 if (climbing == &climbable) return;
1359 climbing = &climbable;
1360 physic.enable_gravity(false);
1361 physic.set_velocity(0, 0);
1362 physic.set_acceleration(0, 0);
1366 Player::stop_climbing(Climbable& /*climbable*/)
1368 if (!climbing) return;
1372 if (grabbed_object) {
1373 grabbed_object->ungrab(*this, dir);
1374 grabbed_object = NULL;
1377 physic.enable_gravity(true);
1378 physic.set_velocity(0, 0);
1379 physic.set_acceleration(0, 0);
1381 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1382 on_ground_flag = true;
1383 // TODO: This won't help. Why?
1389 Player::handle_input_climbing()
1392 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1398 if (controller->hold(Controller::LEFT)) {
1402 if (controller->hold(Controller::RIGHT)) {
1406 if (controller->hold(Controller::UP)) {
1409 if (controller->hold(Controller::DOWN)) {
1412 if (controller->hold(Controller::JUMP)) {
1414 stop_climbing(*climbing);
1420 if (controller->hold(Controller::ACTION)) {
1421 stop_climbing(*climbing);
1424 physic.set_velocity(vx, vy);
1425 physic.set_acceleration(0, 0);