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
236 // extend/shrink tux collision rectangle so that we fall through/walk over 1
238 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
244 // on downward slopes, adjust vertical velocity so tux walks smoothly down
246 if(floor_normal.y != 0) {
247 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
248 physic.set_velocity_y(250);
253 // handle backflipping
255 //prevent player from changing direction when backflipping
256 dir = (backflip_direction == 1) ? LEFT : RIGHT;
257 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
262 fall_mode = ON_GROUND;
263 last_ground_y = get_pos().y;
265 if(get_pos().y > last_ground_y)
267 else if(fall_mode == ON_GROUND)
271 // check if we landed
274 if (backflipping && (!backflip_timer.started())) {
275 backflipping = false;
276 backflip_direction = 0;
278 // if controls are currently deactivated, we take care of standing up ourselves
284 // calculate movement for this frame
285 movement = physic.get_movement(elapsed_time);
287 if(grabbed_object != NULL && !dying) {
288 Vector pos = get_pos() +
289 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
290 grabbed_object->grab(*this, pos, dir);
293 if(grabbed_object != NULL && dying){
294 grabbed_object->ungrab(*this, dir);
295 grabbed_object = NULL;
298 on_ground_flag = false;
300 // when invincible, spawn particles
301 if (invincible_timer.started() && !dying)
303 if (systemRandom.rand(0, 2) == 0) {
304 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
305 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
306 Vector ppos = Vector(px, py);
307 Vector pspeed = Vector(0, 0);
308 Vector paccel = Vector(0, 0);
309 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
310 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
311 // make every other a longer sparkle to make trail a bit fuzzy
312 if (size_t(game_time*20)%2) {
313 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
315 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
318 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
328 return on_ground_flag;
334 if(player_status->bonus == NO_BONUS)
341 Player::apply_friction()
343 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
344 physic.set_velocity_x(0);
345 physic.set_acceleration_x(0);
346 } else if(physic.get_velocity_x() < 0) {
347 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
349 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
354 Player::handle_horizontal_input()
356 float vx = physic.get_velocity_x();
357 float vy = physic.get_velocity_y();
358 float ax = physic.get_acceleration_x();
359 float ay = physic.get_acceleration_y();
362 if(!duck || physic.get_velocity_y() != 0) {
363 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
367 } else if(!controller->hold(Controller::LEFT)
368 && controller->hold(Controller::RIGHT)) {
375 // only run if action key is pressed and we're not holding anything
376 if (!(controller->hold(Controller::ACTION) && (!grabbed_object))) {
377 ax = dirsign * WALK_ACCELERATION_X;
379 if(vx >= MAX_WALK_XM && dirsign > 0) {
382 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
387 ax = dirsign * RUN_ACCELERATION_X;
389 if(vx >= MAX_RUN_XM && dirsign > 0) {
392 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
398 // we can reach WALK_SPEED without any acceleration
399 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
400 vx = dirsign * WALK_SPEED;
403 // changing directions?
404 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
406 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
407 skidding_timer.start(SKID_TIME);
408 sound_manager->play("sounds/skid.wav");
409 // dust some particles
410 Sector::current()->add_object(
412 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
413 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
414 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
423 physic.set_velocity(vx, vy);
424 physic.set_acceleration(ax, ay);
426 // we get slower when not pressing any keys
448 if (physic.get_velocity_y() != 0)
453 if (adjust_height(31.8)) {
455 unduck_hurt_timer.stop();
462 Player::do_standup() {
470 if (adjust_height(63.8)) {
472 unduck_hurt_timer.stop();
474 // if timer is not already running, start it.
475 if (unduck_hurt_timer.get_period() == 0) {
476 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
478 else if (unduck_hurt_timer.check()) {
486 Player::do_backflip() {
492 // TODO: we don't have an animation for firetux backflipping, so let's revert to bigtux
493 set_bonus(GROWUP_BONUS, true);
495 backflip_direction = (dir == LEFT)?(+1):(-1);
498 sound_manager->play("sounds/flip.wav");
499 backflip_timer.start(0.15);
503 Player::do_jump(float yspeed) {
507 physic.set_velocity_y(yspeed);
508 //bbox.move(Vector(0, -1));
510 on_ground_flag = false;
515 sound_manager->play("sounds/bigjump.wav");
517 sound_manager->play("sounds/jump.wav");
522 Player::handle_vertical_input()
525 if(controller->pressed(Controller::JUMP) && (can_jump)) {
527 // when running, only jump a little bit; else do a backflip
528 if (physic.get_velocity_x() != 0) do_jump(-300); else do_backflip();
530 // jump a bit higher if we are running; else do a normal jump
531 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
534 // Let go of jump key
535 else if(!controller->hold(Controller::JUMP)) {
536 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
538 physic.set_velocity_y(0);
542 /* In case the player has pressed Down while in a certain range of air,
543 enable butt jump action */
544 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
548 /* When Down is not held anymore, disable butt jump */
549 if(butt_jump && !controller->hold(Controller::DOWN))
553 physic.set_acceleration_y(0);
555 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
556 physic.set_acceleration_y(-2000);
557 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
562 Player::handle_input()
565 handle_input_ghost();
570 if( controller->released( Controller::PEEK_LEFT ) ) {
573 if( controller->released( Controller::PEEK_RIGHT ) ) {
576 if( controller->pressed( Controller::PEEK_LEFT ) ) {
579 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
583 /* Handle horizontal movement: */
584 if (!backflipping) handle_horizontal_input();
587 if (on_ground() && !controller->hold(Controller::JUMP))
590 /* Handle vertical movement: */
591 handle_vertical_input();
594 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
595 if(Sector::current()->add_bullet(
596 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
597 : Vector(32, bbox.get_height()/2)),
598 physic.get_velocity_x(), dir))
599 shooting_timer.start(SHOOTING_TIME);
602 /* Duck or Standup! */
603 if (controller->hold(Controller::DOWN)) {
612 if(!controller->hold(Controller::ACTION) && grabbed_object) {
613 // move the grabbed object a bit away from tux
614 Vector pos = get_pos() +
615 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
616 bbox.get_height()*0.66666 - 32);
617 Rect dest(pos, pos + Vector(32, 32));
618 if(Sector::current()->is_free_of_statics(dest)) {
619 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
621 moving_object->set_pos(pos);
623 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
625 grabbed_object->ungrab(*this, dir);
626 grabbed_object = NULL;
634 if(controller->hold(Controller::ACTION) && !grabbed_object
636 Sector* sector = Sector::current();
639 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
641 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
644 for(Sector::Portables::iterator i = sector->portables.begin();
645 i != sector->portables.end(); ++i) {
646 Portable* portable = *i;
647 if(!portable->is_portable())
650 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
652 if(moving_object == NULL)
655 if(moving_object->get_bbox().contains(pos)) {
656 grabbed_object = portable;
657 grabbed_object->grab(*this, get_pos(), dir);
665 Player::handle_input_ghost()
669 if (controller->hold(Controller::LEFT)) {
671 vx -= MAX_RUN_XM * 2;
673 if (controller->hold(Controller::RIGHT)) {
675 vx += MAX_RUN_XM * 2;
677 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
678 vy -= MAX_RUN_XM * 2;
680 if (controller->hold(Controller::DOWN)) {
681 vy += MAX_RUN_XM * 2;
683 if (controller->hold(Controller::ACTION)) {
684 set_ghost_mode(false);
686 physic.set_velocity(vx, vy);
687 physic.set_acceleration(0, 0);
691 Player::add_coins(int count)
693 player_status->add_coins(count);
697 Player::add_bonus(const std::string& bonustype)
699 BonusType type = NO_BONUS;
701 if(bonustype == "grow") {
703 } else if(bonustype == "fireflower") {
705 } else if(bonustype == "iceflower") {
707 } else if(bonustype == "none") {
710 std::ostringstream msg;
711 msg << "Unknown bonus type " << bonustype;
712 throw std::runtime_error(msg.str());
715 return add_bonus(type);
719 Player::add_bonus(BonusType type, bool animate)
721 // always ignore NO_BONUS
722 if (type == NO_BONUS) {
726 // ignore GROWUP_BONUS if we're already big
727 if (type == GROWUP_BONUS) {
728 if (player_status->bonus == GROWUP_BONUS)
730 if (player_status->bonus == FIRE_BONUS)
732 if (player_status->bonus == ICE_BONUS)
736 return set_bonus(type, animate);
740 Player::set_bonus(BonusType type, bool animate)
742 if(player_status->bonus == NO_BONUS) {
743 if (!adjust_height(62.8)) {
744 printf("can't adjust\n");
748 growing_timer.start(GROWING_TIME);
751 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
752 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
753 // visually lose helmet
754 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
755 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
756 Vector paccel = Vector(0, 1000);
757 std::string action = (dir==LEFT)?"left":"right";
758 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
760 if ((player_status->bonus == ICE_BONUS) && (animate)) {
762 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
763 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
764 Vector paccel = Vector(0, 1000);
765 std::string action = (dir==LEFT)?"left":"right";
766 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
768 player_status->max_fire_bullets = 0;
769 player_status->max_ice_bullets = 0;
771 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
772 if (type == ICE_BONUS) player_status->max_ice_bullets++;
774 player_status->bonus = type;
779 Player::set_visible(bool visible)
781 this->visible = visible;
783 set_group(COLGROUP_MOVING);
785 set_group(COLGROUP_DISABLED);
789 Player::get_visible()
797 kick_timer.start(KICK_TIME);
801 Player::draw(DrawingContext& context)
806 TuxBodyParts* tux_body;
808 if (player_status->bonus == GROWUP_BONUS)
810 else if (player_status->bonus == FIRE_BONUS)
812 else if (player_status->bonus == ICE_BONUS)
815 tux_body = small_tux;
817 int layer = LAYER_OBJECTS + 1;
819 /* Set Tux sprite action */
823 tux_body->set_action("backflip-left");
825 tux_body->set_action("backflip-right");
827 else if (duck && is_big())
830 tux_body->set_action("duck-left");
832 tux_body->set_action("duck-right");
834 else if (skidding_timer.started() && !skidding_timer.check())
837 tux_body->set_action("skid-left");
839 tux_body->set_action("skid-right");
841 else if (kick_timer.started() && !kick_timer.check())
844 tux_body->set_action("kick-left");
846 tux_body->set_action("kick-right");
848 else if (butt_jump && is_big())
851 tux_body->set_action("buttjump-left");
853 tux_body->set_action("buttjump-right");
855 else if (!on_ground())
858 tux_body->set_action("jump-left");
860 tux_body->set_action("jump-right");
864 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
867 tux_body->set_action("stand-left");
869 tux_body->set_action("stand-right");
874 tux_body->set_action("walk-left");
876 tux_body->set_action("walk-right");
880 if(idle_timer.check())
885 tux_body->head->set_action("idle-left", 1);
887 tux_body->head->set_action("idle-right", 1);
892 // Tux is holding something
893 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
894 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
899 tux_body->arms->set_action("duck+grab-left");
901 tux_body->arms->set_action("duck+grab-right");
906 tux_body->arms->set_action("grab-left");
908 tux_body->arms->set_action("grab-right");
914 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
916 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
918 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
919 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
921 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
922 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
925 else if (safe_timer.started() && size_t(game_time*40)%2)
928 tux_body->draw(context, get_pos(), layer);
933 Player::collision_tile(uint32_t tile_attributes)
935 if(tile_attributes & Tile::HURTS)
939 if( tile_attributes & Tile::WATER ){
945 if( tile_attributes & Tile::WATER ){
948 sound_manager->play( "sounds/splash.ogg" );
954 Player::collision_solid(const CollisionHit& hit)
957 if(physic.get_velocity_y() > 0)
958 physic.set_velocity_y(0);
960 on_ground_flag = true;
961 floor_normal = hit.slope_normal;
963 if(physic.get_velocity_y() < 0)
964 physic.set_velocity_y(.2);
967 if(hit.left || hit.right) {
968 physic.set_velocity_x(0);
973 if(hit.left || hit.right) {
975 } else if(hit.top || hit.bottom) {
982 Player::collision(GameObject& other, const CollisionHit& hit)
984 Bullet* bullet = dynamic_cast<Bullet*> (&other);
989 if(hit.left || hit.right) {
990 try_grab(); //grab objects right now, in update it will be too late
993 assert(dynamic_cast<MovingObject*> (&other) != NULL);
995 MovingObject* moving_object = static_cast<MovingObject*> (&other);
996 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
997 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
999 if(controller->pressed(Controller::UP))
1000 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1006 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1007 if(badguy != NULL) {
1008 if(safe_timer.started() || invincible_timer.started())
1018 Player::make_invincible()
1020 sound_manager->play("sounds/invincible.wav");
1021 invincible_timer.start(TUX_INVINCIBLE_TIME);
1022 Sector::current()->play_music(HERRING_MUSIC);
1027 Player::kill(bool completely)
1029 if(dying || deactivated)
1032 if(!completely && (safe_timer.started() || invincible_timer.started()))
1035 sound_manager->play("sounds/hurt.wav");
1037 physic.set_velocity_x(0);
1039 if(!completely && is_big()) {
1040 if(player_status->bonus == FIRE_BONUS
1041 || player_status->bonus == ICE_BONUS) {
1042 safe_timer.start(TUX_SAFE_TIME);
1043 set_bonus(GROWUP_BONUS, true);
1045 //growing_timer.start(GROWING_TIME);
1046 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1047 adjust_height(30.8);
1049 set_bonus(NO_BONUS, true);
1052 for (int i = 0; (i < 5) && (i < player_status->coins); i++)
1054 // the numbers: starting x, starting y, velocity y
1055 Sector::current()->add_object(new FallingCoin(get_pos() +
1056 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1057 systemRandom.rand(-100,100)));
1059 physic.enable_gravity(true);
1060 physic.set_acceleration(0, 0);
1061 physic.set_velocity(0, -700);
1062 player_status->coins -= 25;
1063 set_bonus(NO_BONUS, true);
1065 dying_timer.start(3.0);
1066 set_group(COLGROUP_DISABLED);
1068 DisplayEffect* effect = new DisplayEffect();
1069 effect->fade_out(3.0);
1070 Sector::current()->add_object(effect);
1071 sound_manager->stop_music(3.0);
1076 Player::move(const Vector& vector)
1080 // TODO: do we need the following? Seems irrelevant to moving the player
1082 set_size(31.8, 63.8);
1084 set_size(31.8, 31.8);
1086 last_ground_y = vector.y;
1092 Player::check_bounds(Camera* camera)
1094 /* Keep tux in bounds: */
1095 if (get_pos().x < 0) {
1096 // Lock Tux to the size of the level, so that he doesn't fall of
1098 set_pos(Vector(0, get_pos().y));
1101 /* fallen out of the level? */
1102 if (get_pos().y > Sector::current()->get_height()) {
1107 // can happen if back scrolling is disabled
1108 if(get_pos().x < camera->get_translation().x) {
1109 set_pos(Vector(camera->get_translation().x, get_pos().y));
1111 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1114 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1120 Player::add_velocity(const Vector& velocity)
1122 physic.set_velocity(physic.get_velocity() + velocity);
1126 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1128 if (end_speed.x > 0)
1129 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1130 if (end_speed.x < 0)
1131 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1132 if (end_speed.y > 0)
1133 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1134 if (end_speed.y < 0)
1135 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1139 Player::bounce(BadGuy& )
1141 if(controller->hold(Controller::JUMP))
1142 physic.set_velocity_y(-520);
1144 physic.set_velocity_y(-300);
1147 //Scripting Functions Below
1150 Player::deactivate()
1155 physic.set_velocity_x(0);
1156 physic.set_velocity_y(0);
1157 physic.set_acceleration_x(0);
1158 physic.set_acceleration_y(0);
1166 deactivated = false;
1169 void Player::walk(float speed)
1171 physic.set_velocity_x(speed);
1175 Player::set_ghost_mode(bool enable)
1177 if (ghost_mode == enable)
1182 set_group(COLGROUP_DISABLED);
1183 physic.enable_gravity(false);
1184 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1187 set_group(COLGROUP_MOVING);
1188 physic.enable_gravity(true);
1189 log_debug << "You feel solid again." << std::endl;