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"
53 static const int TILES_FOR_BUTTJUMP = 3;
54 static const float SHOOTING_TIME = .150;
55 /// time before idle animation starts
56 static const float IDLE_TIME = 2.5;
58 static const float WALK_ACCELERATION_X = 300;
59 static const float RUN_ACCELERATION_X = 400;
60 static const float SKID_XM = 200;
61 static const float SKID_TIME = .3;
62 static const float MAX_WALK_XM = 230;
63 static const float MAX_RUN_XM = 320;
64 static const float WALK_SPEED = 100;
66 static const float KICK_TIME = .3;
67 static const float CHEER_TIME = 1;
69 static const float UNDUCK_HURT_TIME = 0.25; /**< if Tux cannot unduck for this long, he will get hurt */
72 Surface* growingtux_left[GROWING_FRAMES];
73 Surface* growingtux_right[GROWING_FRAMES];
75 Surface* tux_life = 0;
77 TuxBodyParts* small_tux = 0;
78 TuxBodyParts* big_tux = 0;
79 TuxBodyParts* fire_tux = 0;
80 TuxBodyParts* ice_tux = 0;
86 TuxBodyParts::set_action(std::string action, int loops)
89 head->set_action(action, loops);
91 body->set_action(action, loops);
93 arms->set_action(action, loops);
95 feet->set_action(action, loops);
99 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
102 head->draw(context, pos, layer-1);
104 body->draw(context, pos, layer-3);
106 arms->draw(context, pos, layer+10);
108 feet->draw(context, pos, layer-2);
111 Player::Player(PlayerStatus* _player_status, const std::string& name)
112 : player_status(_player_status), grabbed_object(NULL), ghost_mode(false)
115 controller = main_controller;
116 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
117 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
118 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
120 sound_manager->preload("sounds/bigjump.wav");
121 sound_manager->preload("sounds/jump.wav");
122 sound_manager->preload("sounds/hurt.wav");
123 sound_manager->preload("sounds/skid.wav");
124 sound_manager->preload("sounds/flip.wav");
125 sound_manager->preload("sounds/invincible.wav");
126 sound_manager->preload("sounds/splash.ogg");
134 delete smalltux_gameover;
135 delete smalltux_star;
143 set_size(31.8, 62.8);
145 set_size(31.8, 30.8);
155 fall_mode = ON_GROUND;
160 backflipping = false;
161 backflip_direction = 0;
165 on_ground_flag = false;
166 grabbed_object = NULL;
172 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
177 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
181 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
186 Scripting::unexpose_object(vm, table_idx, name);
190 Player::set_controller(Controller* controller)
192 this->controller = controller;
196 Player::adjust_height(float new_height)
199 bbox2.move(Vector(0, bbox.get_height() - new_height));
200 bbox2.set_height(new_height);
202 if(new_height > bbox.get_height()) {
203 Rect additional_space = bbox2;
204 additional_space.set_height(new_height - bbox.get_height());
205 if(!Sector::current()->is_free_of_statics(additional_space, this))
209 // adjust bbox accordingly
210 // note that we use members of moving_object for this, so we can run this during CD, too
212 set_size(bbox2.get_width(), bbox2.get_height());
217 Player::update(float elapsed_time)
224 if(dying && dying_timer.check()) {
229 if(!dying && !deactivated)
232 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
238 // extend/shrink tux collision rectangle so that we fall through/walk over 1
240 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
246 // on downward slopes, adjust vertical velocity so tux walks smoothly down
248 if(floor_normal.y != 0) {
249 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
250 physic.set_velocity_y(250);
255 // handle backflipping
257 //prevent player from changing direction when backflipping
258 dir = (backflip_direction == 1) ? LEFT : RIGHT;
259 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
264 fall_mode = ON_GROUND;
265 last_ground_y = get_pos().y;
267 if(get_pos().y > last_ground_y)
269 else if(fall_mode == ON_GROUND)
273 // check if we landed
276 if (backflipping && (!backflip_timer.started())) {
277 backflipping = false;
278 backflip_direction = 0;
280 // if controls are currently deactivated, we take care of standing up ourselves
286 // calculate movement for this frame
287 movement = physic.get_movement(elapsed_time);
289 if(grabbed_object != NULL && !dying) {
290 Vector pos = get_pos() +
291 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
292 grabbed_object->grab(*this, pos, dir);
295 if(grabbed_object != NULL && dying){
296 grabbed_object->ungrab(*this, dir);
297 grabbed_object = NULL;
300 on_ground_flag = false;
302 // when invincible, spawn particles
303 if (invincible_timer.started() && !dying)
305 if (systemRandom.rand(0, 2) == 0) {
306 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
307 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
308 Vector ppos = Vector(px, py);
309 Vector pspeed = Vector(0, 0);
310 Vector paccel = Vector(0, 0);
311 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
312 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
313 // make every other a longer sparkle to make trail a bit fuzzy
314 if (size_t(game_time*20)%2) {
315 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
317 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
320 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
330 return on_ground_flag;
336 if(player_status->bonus == NO_BONUS)
343 Player::apply_friction()
345 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
346 physic.set_velocity_x(0);
347 physic.set_acceleration_x(0);
348 } else if(physic.get_velocity_x() < 0) {
349 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
351 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
356 Player::handle_horizontal_input()
358 float vx = physic.get_velocity_x();
359 float vy = physic.get_velocity_y();
360 float ax = physic.get_acceleration_x();
361 float ay = physic.get_acceleration_y();
364 if(!duck || physic.get_velocity_y() != 0) {
365 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
369 } else if(!controller->hold(Controller::LEFT)
370 && controller->hold(Controller::RIGHT)) {
377 // only run if action key is pressed and we're not holding anything
378 if (!(controller->hold(Controller::ACTION) && (!grabbed_object))) {
379 ax = dirsign * WALK_ACCELERATION_X;
381 if(vx >= MAX_WALK_XM && dirsign > 0) {
384 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
389 ax = dirsign * RUN_ACCELERATION_X;
391 if(vx >= MAX_RUN_XM && dirsign > 0) {
394 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
400 // we can reach WALK_SPEED without any acceleration
401 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
402 vx = dirsign * WALK_SPEED;
405 // changing directions?
406 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
408 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
409 skidding_timer.start(SKID_TIME);
410 sound_manager->play("sounds/skid.wav");
411 // dust some particles
412 Sector::current()->add_object(
414 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
415 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
416 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
425 physic.set_velocity(vx, vy);
426 physic.set_acceleration(ax, ay);
428 // we get slower when not pressing any keys
450 if (physic.get_velocity_y() != 0)
455 if (adjust_height(31.8)) {
457 unduck_hurt_timer.stop();
464 Player::do_standup() {
472 if (adjust_height(63.8)) {
474 unduck_hurt_timer.stop();
476 // if timer is not already running, start it.
477 if (unduck_hurt_timer.get_period() == 0) {
478 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
480 else if (unduck_hurt_timer.check()) {
488 Player::do_backflip() {
494 // TODO: we don't have an animation for firetux backflipping, so let's revert to bigtux
495 set_bonus(GROWUP_BONUS, true);
497 backflip_direction = (dir == LEFT)?(+1):(-1);
500 sound_manager->play("sounds/flip.wav");
501 backflip_timer.start(0.15);
505 Player::do_jump(float yspeed) {
509 physic.set_velocity_y(yspeed);
510 //bbox.move(Vector(0, -1));
512 on_ground_flag = false;
517 sound_manager->play("sounds/bigjump.wav");
519 sound_manager->play("sounds/jump.wav");
524 Player::handle_vertical_input()
527 if(controller->pressed(Controller::JUMP) && (can_jump)) {
529 // when running, only jump a little bit; else do a backflip
530 if (physic.get_velocity_x() != 0) do_jump(-300); else do_backflip();
532 // jump a bit higher if we are running; else do a normal jump
533 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
536 // Let go of jump key
537 else if(!controller->hold(Controller::JUMP)) {
538 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
540 physic.set_velocity_y(0);
544 /* In case the player has pressed Down while in a certain range of air,
545 enable butt jump action */
546 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
550 /* When Down is not held anymore, disable butt jump */
551 if(butt_jump && !controller->hold(Controller::DOWN))
555 physic.set_acceleration_y(0);
557 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
558 physic.set_acceleration_y(-2000);
559 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
564 Player::handle_input()
566 Sector* sector = Sector::current();
569 handle_input_ghost();
574 if( controller->released( Controller::PEEK_LEFT ) ) {
577 if( controller->released( Controller::PEEK_RIGHT ) ) {
580 if( controller->pressed( Controller::PEEK_LEFT ) ) {
583 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
587 /* Handle horizontal movement: */
588 if (!backflipping) handle_horizontal_input();
591 if (on_ground() && !controller->hold(Controller::JUMP))
594 /* Handle vertical movement: */
595 handle_vertical_input();
598 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
599 if(Sector::current()->add_bullet(
600 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
601 : Vector(32, bbox.get_height()/2)),
602 physic.get_velocity_x(), dir))
603 shooting_timer.start(SHOOTING_TIME);
606 /* Duck or Standup! */
607 if (controller->hold(Controller::DOWN)) {
614 if(controller->hold(Controller::ACTION) && !grabbed_object
615 && try_grab && !duck) {
617 if(grab_dir == LEFT) {
618 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
620 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
623 for(Sector::Portables::iterator i = sector->portables.begin();
624 i != sector->portables.end(); ++i) {
625 Portable* portable = *i;
626 if(!portable->is_portable())
629 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
631 if(moving_object == NULL)
634 if(moving_object->get_bbox().contains(pos)) {
635 grabbed_object = portable;
636 grabbed_object->grab(*this, get_pos(), dir);
642 if(!controller->hold(Controller::ACTION) && grabbed_object) {
643 // move the grabbed object a bit away from tux
644 Vector pos = get_pos() +
645 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
646 bbox.get_height()*0.66666 - 32);
647 Rect dest(pos, pos + Vector(32, 32));
648 if(Sector::current()->is_free_of_statics(dest)) {
649 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
651 moving_object->set_pos(pos);
653 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
655 grabbed_object->ungrab(*this, dir);
656 grabbed_object = NULL;
662 Player::handle_input_ghost()
666 if (controller->hold(Controller::LEFT)) {
668 vx -= MAX_RUN_XM * 2;
670 if (controller->hold(Controller::RIGHT)) {
672 vx += MAX_RUN_XM * 2;
674 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
675 vy -= MAX_RUN_XM * 2;
677 if (controller->hold(Controller::DOWN)) {
678 vy += MAX_RUN_XM * 2;
680 if (controller->hold(Controller::ACTION)) {
681 set_ghost_mode(false);
683 physic.set_velocity(vx, vy);
684 physic.set_acceleration(0, 0);
688 Player::add_coins(int count)
690 player_status->add_coins(count);
694 Player::add_bonus(const std::string& bonustype)
696 BonusType type = NO_BONUS;
698 if(bonustype == "grow") {
700 } else if(bonustype == "fireflower") {
702 } else if(bonustype == "iceflower") {
704 } else if(bonustype == "none") {
707 std::ostringstream msg;
708 msg << "Unknown bonus type " << bonustype;
709 throw std::runtime_error(msg.str());
712 return add_bonus(type);
716 Player::add_bonus(BonusType type, bool animate)
718 // always ignore NO_BONUS
719 if (type == NO_BONUS) {
723 // ignore GROWUP_BONUS if we're already big
724 if (type == GROWUP_BONUS) {
725 if (player_status->bonus == GROWUP_BONUS)
727 if (player_status->bonus == FIRE_BONUS)
729 if (player_status->bonus == ICE_BONUS)
733 return set_bonus(type, animate);
737 Player::set_bonus(BonusType type, bool animate)
739 if(player_status->bonus == NO_BONUS) {
740 if (!adjust_height(62.8)) {
741 printf("can't adjust\n");
745 growing_timer.start(GROWING_TIME);
748 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
749 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
750 // visually lose helmet
751 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
752 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
753 Vector paccel = Vector(0, 1000);
754 std::string action = (dir==LEFT)?"left":"right";
755 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
757 player_status->max_fire_bullets = 0;
758 player_status->max_ice_bullets = 0;
760 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
761 if (type == ICE_BONUS) player_status->max_ice_bullets++;
763 player_status->bonus = type;
768 Player::set_visible(bool visible)
770 this->visible = visible;
772 set_group(COLGROUP_MOVING);
774 set_group(COLGROUP_DISABLED);
778 Player::get_visible()
786 kick_timer.start(KICK_TIME);
790 Player::draw(DrawingContext& context)
795 TuxBodyParts* tux_body;
797 if (player_status->bonus == GROWUP_BONUS)
799 else if (player_status->bonus == FIRE_BONUS)
801 else if (player_status->bonus == ICE_BONUS)
804 tux_body = small_tux;
806 int layer = LAYER_OBJECTS + 1;
808 /* Set Tux sprite action */
812 tux_body->set_action("backflip-left");
814 tux_body->set_action("backflip-right");
816 else if (duck && is_big())
819 tux_body->set_action("duck-left");
821 tux_body->set_action("duck-right");
823 else if (skidding_timer.started() && !skidding_timer.check())
826 tux_body->set_action("skid-left");
828 tux_body->set_action("skid-right");
830 else if (kick_timer.started() && !kick_timer.check())
833 tux_body->set_action("kick-left");
835 tux_body->set_action("kick-right");
837 else if (butt_jump && is_big())
840 tux_body->set_action("buttjump-left");
842 tux_body->set_action("buttjump-right");
844 else if (!on_ground())
847 tux_body->set_action("jump-left");
849 tux_body->set_action("jump-right");
853 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
856 tux_body->set_action("stand-left");
858 tux_body->set_action("stand-right");
863 tux_body->set_action("walk-left");
865 tux_body->set_action("walk-right");
869 if(idle_timer.check())
874 tux_body->head->set_action("idle-left", 1);
876 tux_body->head->set_action("idle-right", 1);
881 // Tux is holding something
882 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
883 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
888 tux_body->arms->set_action("duck+grab-left");
890 tux_body->arms->set_action("duck+grab-right");
895 tux_body->arms->set_action("grab-left");
897 tux_body->arms->set_action("grab-right");
903 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
905 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
907 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
908 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
910 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
911 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
914 else if (safe_timer.started() && size_t(game_time*40)%2)
917 tux_body->draw(context, get_pos(), layer);
922 Player::collision_tile(uint32_t tile_attributes)
924 if(tile_attributes & Tile::HURTS)
928 if( tile_attributes & Tile::WATER ){
934 if( tile_attributes & Tile::WATER ){
937 sound_manager->play( "sounds/splash.ogg" );
943 Player::collision_solid(const CollisionHit& hit)
946 if(physic.get_velocity_y() > 0)
947 physic.set_velocity_y(0);
949 on_ground_flag = true;
950 floor_normal = hit.slope_normal;
952 if(physic.get_velocity_y() < 0)
953 physic.set_velocity_y(.2);
956 if(hit.left || hit.right) {
957 physic.set_velocity_x(0);
959 grab_dir = hit.left ? LEFT : RIGHT;
964 if(hit.left || hit.right) {
966 } else if(hit.top || hit.bottom) {
973 Player::collision(GameObject& other, const CollisionHit& )
975 Bullet* bullet = dynamic_cast<Bullet*> (&other);
981 assert(dynamic_cast<MovingObject*> (&other) != NULL);
983 MovingObject* moving_object = static_cast<MovingObject*> (&other);
984 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
985 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
987 if(controller->pressed(Controller::UP))
988 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
994 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
996 if(safe_timer.started() || invincible_timer.started())
1006 Player::make_invincible()
1008 sound_manager->play("sounds/invincible.wav");
1009 invincible_timer.start(TUX_INVINCIBLE_TIME);
1010 Sector::current()->play_music(HERRING_MUSIC);
1015 Player::kill(bool completely)
1017 if(dying || deactivated)
1020 if(!completely && (safe_timer.started() || invincible_timer.started()))
1023 sound_manager->play("sounds/hurt.wav");
1025 physic.set_velocity_x(0);
1027 if(!completely && is_big()) {
1028 if(player_status->bonus == FIRE_BONUS
1029 || player_status->bonus == ICE_BONUS) {
1030 safe_timer.start(TUX_SAFE_TIME);
1031 set_bonus(GROWUP_BONUS, true);
1033 //growing_timer.start(GROWING_TIME);
1034 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1035 adjust_height(30.8);
1037 set_bonus(NO_BONUS, true);
1040 for (int i = 0; (i < 5) && (i < player_status->coins); i++)
1042 // the numbers: starting x, starting y, velocity y
1043 Sector::current()->add_object(new FallingCoin(get_pos() +
1044 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1045 systemRandom.rand(-100,100)));
1047 physic.enable_gravity(true);
1048 physic.set_acceleration(0, 0);
1049 physic.set_velocity(0, -700);
1050 player_status->coins -= 25;
1051 set_bonus(NO_BONUS, true);
1053 dying_timer.start(3.0);
1054 set_group(COLGROUP_DISABLED);
1056 DisplayEffect* effect = new DisplayEffect();
1057 effect->fade_out(3.0);
1058 Sector::current()->add_object(effect);
1059 sound_manager->stop_music(3.0);
1064 Player::move(const Vector& vector)
1068 // TODO: do we need the following? Seems irrelevant to moving the player
1070 set_size(31.8, 63.8);
1072 set_size(31.8, 31.8);
1074 last_ground_y = vector.y;
1080 Player::check_bounds(Camera* camera)
1082 /* Keep tux in bounds: */
1083 if (get_pos().x < 0) {
1084 // Lock Tux to the size of the level, so that he doesn't fall of
1086 set_pos(Vector(0, get_pos().y));
1089 /* fallen out of the level? */
1090 if (get_pos().y > Sector::current()->get_height()) {
1095 // can happen if back scrolling is disabled
1096 if(get_pos().x < camera->get_translation().x) {
1097 set_pos(Vector(camera->get_translation().x, get_pos().y));
1099 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1102 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1108 Player::add_velocity(const Vector& velocity)
1110 physic.set_velocity(physic.get_velocity() + velocity);
1114 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1116 if (end_speed.x > 0)
1117 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1118 if (end_speed.x < 0)
1119 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1120 if (end_speed.y > 0)
1121 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1122 if (end_speed.y < 0)
1123 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1127 Player::bounce(BadGuy& )
1129 if(controller->hold(Controller::JUMP))
1130 physic.set_velocity_y(-520);
1132 physic.set_velocity_y(-300);
1135 //Scripting Functions Below
1138 Player::deactivate()
1143 physic.set_velocity_x(0);
1144 physic.set_velocity_y(0);
1145 physic.set_acceleration_x(0);
1146 physic.set_acceleration_y(0);
1154 deactivated = false;
1157 void Player::walk(float speed)
1159 physic.set_velocity_x(speed);
1163 Player::set_ghost_mode(bool enable)
1165 if (ghost_mode == enable)
1170 set_group(COLGROUP_DISABLED);
1171 physic.enable_gravity(false);
1172 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1175 set_group(COLGROUP_MOVING);
1176 physic.enable_gravity(true);
1177 log_debug << "You feel solid again." << std::endl;