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;
83 TuxBodyParts::set_action(std::string action, int loops)
86 head->set_action(action, loops);
88 body->set_action(action, loops);
90 arms->set_action(action, loops);
92 feet->set_action(action, loops);
96 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
99 head->draw(context, pos, layer-1);
101 body->draw(context, pos, layer-3);
103 arms->draw(context, pos, layer+10);
105 feet->draw(context, pos, layer-2);
108 Player::Player(PlayerStatus* _player_status)
109 : player_status(_player_status), grabbed_object(NULL), ghost_mode(false)
111 controller = main_controller;
112 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
113 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
114 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
116 sound_manager->preload("sounds/bigjump.wav");
117 sound_manager->preload("sounds/jump.wav");
118 sound_manager->preload("sounds/hurt.wav");
119 sound_manager->preload("sounds/skid.wav");
126 delete smalltux_gameover;
127 delete smalltux_star;
135 set_size(31.8, 62.8);
137 set_size(31.8, 30.8);
146 fall_mode = ON_GROUND;
151 backflipping = false;
152 backflip_direction = 0;
155 on_ground_flag = false;
156 grabbed_object = NULL;
158 floor_normal = Vector(0,-1);
164 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
166 Scripting::Player* interface = static_cast<Scripting::Player*> (this);
167 Scripting::expose_object(vm, table_idx, interface, "Tux", false);
171 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
173 Scripting::unexpose_object(vm, table_idx, "Tux");
177 Player::set_controller(Controller* controller)
179 this->controller = controller;
183 Player::adjust_height(float new_height)
186 bbox2.move(Vector(0, bbox.get_height() - new_height));
187 bbox2.set_height(new_height);
188 if (!Sector::current()->is_free_space(bbox2)) return false;
189 // adjust bbox accordingly
190 // note that we use members of moving_object for this, so we can run this during CD, too
192 set_size(bbox2.get_width(), bbox2.get_height());
197 Player::update(float elapsed_time)
199 if(dying && dying_timer.check()) {
204 if(!dying && !deactivated)
207 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
208 if (deactivated) apply_friction();
210 // extend/shrink tux collision rectangle so that we fall through/walk over 1
212 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
218 // on downward slopes, adjust vertical velocity to match slope angle
220 if (floor_normal.y != 0) {
221 if ((floor_normal.x * physic.get_velocity_x()) > 0) {
222 // we overdo it a little, just to be on the safe side
223 physic.set_velocity_y(-physic.get_velocity_x() * (floor_normal.x / floor_normal.y) * 2);
228 // handle backflipping
230 //prevent player from changing direction when backflipping
231 dir = (backflip_direction == 1) ? LEFT : RIGHT;
232 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
237 fall_mode = ON_GROUND;
238 last_ground_y = get_pos().y;
240 if(get_pos().y > last_ground_y)
242 else if(fall_mode == ON_GROUND)
246 // check if we landed
249 if (backflipping && (!backflip_timer.started())) {
250 backflipping = false;
251 backflip_direction = 0;
253 // if controls are currently deactivated, we take care of standing up ourselves
254 if (deactivated) do_standup();
260 if (butt_jump && on_ground() && is_big()) {
263 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
265 Sector::current()->add_smoke_cloud(
266 Vector(get_pos().x - 32, get_pos().y + 32));
270 // Break bricks beneath Tux
271 if(Sector::current()->trybreakbrick(
272 Vector(base.x + 1, base.y + base.height), false)
273 || Sector::current()->trybreakbrick(
274 Vector(base.x + base.width - 1, base.y + base.height), false)) {
275 physic.set_velocity_y(-2);
279 // Kill nearby badguys
280 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
281 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
282 i != gameobjects.end();
284 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
286 // don't kill when badguys are already dying or in a certain mode
287 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
288 badguy->mode != BadGuy::BOMB_EXPLODE) {
289 if (fabsf(base.x - badguy->base.x) < 96 &&
290 fabsf(base.y - badguy->base.y) < 64)
298 // calculate movement for this frame
299 movement = physic.get_movement(elapsed_time);
301 if(grabbed_object != NULL && !dying) {
302 Vector pos = get_pos() +
303 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
304 grabbed_object->grab(*this, pos, dir);
307 if(grabbed_object != NULL && dying){
308 grabbed_object->ungrab(*this, dir);
309 grabbed_object = NULL;
312 on_ground_flag = false;
318 return on_ground_flag;
324 if(player_status->bonus == NO_BONUS)
331 Player::apply_friction()
333 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
334 physic.set_velocity_x(0);
335 physic.set_acceleration_x(0);
336 } else if(physic.get_velocity_x() < 0) {
337 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
339 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
343 // if we're on ice slow down acceleration or deceleration
344 if (isice(base.x, base.y + base.height))
346 /* the acceleration/deceleration rate on ice is inversely proportional to
347 * the current velocity.
350 // increasing 1 will increase acceleration/deceleration rate
351 // decreasing 1 will decrease acceleration/deceleration rate
352 // must stay above zero, though
353 if (ax != 0) ax *= 1 / fabs(vx);
360 Player::handle_horizontal_input()
362 float vx = physic.get_velocity_x();
363 float vy = physic.get_velocity_y();
364 float ax = physic.get_acceleration_x();
365 float ay = physic.get_acceleration_y();
368 if(!duck || physic.get_velocity_y() != 0) {
369 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
373 } else if(!controller->hold(Controller::LEFT)
374 && controller->hold(Controller::RIGHT)) {
381 if (!controller->hold(Controller::ACTION)) {
382 ax = dirsign * WALK_ACCELERATION_X;
384 if(vx >= MAX_WALK_XM && dirsign > 0) {
387 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
392 ax = dirsign * RUN_ACCELERATION_X;
394 if(vx >= MAX_RUN_XM && dirsign > 0) {
397 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
403 // we can reach WALK_SPEED without any acceleration
404 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
405 vx = dirsign * WALK_SPEED;
408 // changing directions?
409 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
411 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
412 skidding_timer.start(SKID_TIME);
413 sound_manager->play("sounds/skid.wav");
414 // dust some particles
415 Sector::current()->add_object(
417 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
418 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
419 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
428 physic.set_velocity(vx, vy);
429 physic.set_acceleration(ax, ay);
431 // we get slower when not pressing any keys
449 if (!is_big()) return;
451 if (physic.get_velocity_y() != 0) return;
452 if (!on_ground()) return;
454 if (adjust_height(31.8)) {
456 unduck_hurt_timer.stop();
463 Player::do_standup() {
465 if (!is_big()) return;
466 if (backflipping) return;
468 if (adjust_height(63.8)) {
470 unduck_hurt_timer.stop();
472 // if timer is not already running, start it.
473 if (unduck_hurt_timer.get_period() == 0) {
474 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
476 else if (unduck_hurt_timer.check()) {
484 Player::do_backflip() {
486 if (!on_ground()) return;
488 // TODO: we don't have an animation for firetux backflipping, so let's revert to bigtux
489 set_bonus(GROWUP_BONUS, true);
491 backflip_direction = (dir == LEFT)?(+1):(-1);
494 backflip_timer.start(0.15);
498 Player::do_jump(float yspeed) {
499 if (!on_ground()) return;
501 physic.set_velocity_y(yspeed);
502 //bbox.move(Vector(0, -1));
504 on_ground_flag = false;
509 sound_manager->play("sounds/bigjump.wav");
511 sound_manager->play("sounds/jump.wav");
516 Player::handle_vertical_input()
520 if(controller->pressed(Controller::JUMP) && (can_jump)) {
522 // when running, only jump a little bit; else do a backflip
523 if (physic.get_velocity_x() != 0) do_jump(-300); else do_backflip();
525 // jump a bit higher if we are running; else do a normal jump
526 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
529 // Let go of jump key
530 else if(!controller->hold(Controller::JUMP)) {
531 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
533 physic.set_velocity_y(0);
537 /* In case the player has pressed Down while in a certain range of air,
538 enable butt jump action */
539 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
543 /* When Down is not held anymore, disable butt jump */
544 if(butt_jump && !controller->hold(Controller::DOWN)) butt_jump = false;
546 /** jumping is only allowed if we're about to touch ground soon and if the
547 * button has been up in between the last jump
551 if ( (issolid(get_pos().x + bbox.get_width() / 2,
552 get_pos().y + bbox.get_height() + 64) ||
553 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
554 issolid(get_pos().x + bbox.get_width() - 1,
555 get_pos().y + bbox.get_height() + 64))
558 && input.jump && !input.old_jump)
566 Player::handle_input()
569 handle_input_ghost();
573 if(!controller->hold(Controller::ACTION) && grabbed_object) {
574 // move the grabbed object a bit away from tux
575 Vector pos = get_pos() +
576 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
577 bbox.get_height()*0.66666 - 32);
578 Rect dest(pos, pos + Vector(32, 32));
579 if(Sector::current()->is_free_space(dest)) {
580 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
582 moving_object->set_pos(pos);
584 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
586 grabbed_object->ungrab(*this, dir);
587 grabbed_object = NULL;
591 /* Handle horizontal movement: */
592 if (!backflipping) handle_horizontal_input();
595 if (on_ground() && !controller->hold(Controller::JUMP))
598 /* Handle vertical movement: */
599 handle_vertical_input();
602 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
603 if(Sector::current()->add_bullet(
604 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
605 : Vector(32, bbox.get_height()/2)),
606 physic.get_velocity_x(), dir))
607 shooting_timer.start(SHOOTING_TIME);
610 /* Duck or Standup! */
611 if (controller->hold(Controller::DOWN)) do_duck(); else do_standup();
616 Player::handle_input_ghost()
620 if (controller->hold(Controller::LEFT)) {
622 vx -= MAX_RUN_XM * 2;
624 if (controller->hold(Controller::RIGHT)) {
626 vx += MAX_RUN_XM * 2;
628 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
629 vy -= MAX_RUN_XM * 2;
631 if (controller->hold(Controller::DOWN)) {
632 vy += MAX_RUN_XM * 2;
634 if (controller->hold(Controller::ACTION)) {
635 set_ghost_mode(false);
637 physic.set_velocity(vx, vy);
638 physic.set_acceleration(0, 0);
642 Player::add_coins(int count)
644 player_status->add_coins(count);
648 Player::add_bonus(const std::string& bonustype)
650 if(bonustype == "grow") {
651 add_bonus(GROWUP_BONUS);
652 } else if(bonustype == "fireflower") {
653 add_bonus(FIRE_BONUS);
654 } else if(bonustype == "iceflower") {
655 add_bonus(ICE_BONUS);
656 } else if(bonustype == "none") {
659 std::ostringstream msg;
660 msg << "Unknown bonus type " << bonustype;
661 throw std::runtime_error(msg.str());
666 Player::add_bonus(BonusType type, bool animate)
668 // always ignore NO_BONUS
669 if (type == NO_BONUS) {
673 // ignore GROWUP_BONUS if we're already big
674 if (type == GROWUP_BONUS) {
675 if (player_status->bonus == GROWUP_BONUS) return;
676 if (player_status->bonus == FIRE_BONUS) return;
677 if (player_status->bonus == ICE_BONUS) return;
680 set_bonus(type, animate);
684 Player::set_bonus(BonusType type, bool animate)
686 if(player_status->bonus == NO_BONUS) {
687 if (!adjust_height(62.8)) return;
689 growing_timer.start(GROWING_TIME);
692 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
693 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
694 // visually lose helmet
695 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
696 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
697 Vector paccel = Vector(0, 1000);
698 std::string action = (dir==LEFT)?"left":"right";
699 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
701 player_status->max_fire_bullets = 0;
702 player_status->max_ice_bullets = 0;
704 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
705 if (type == ICE_BONUS) player_status->max_ice_bullets++;
707 player_status->bonus = type;
711 Player::set_visible(bool visible)
713 this->visible = visible;
717 Player::get_visible()
725 kick_timer.start(KICK_TIME);
729 Player::draw(DrawingContext& context)
734 TuxBodyParts* tux_body;
736 if (player_status->bonus == GROWUP_BONUS)
738 else if (player_status->bonus == FIRE_BONUS)
740 else if (player_status->bonus == ICE_BONUS)
743 tux_body = small_tux;
745 int layer = LAYER_OBJECTS + 1;
747 /* Set Tux sprite action */
751 tux_body->set_action("backflip-left");
753 tux_body->set_action("backflip-right");
755 else if (duck && is_big())
758 tux_body->set_action("duck-left");
760 tux_body->set_action("duck-right");
762 else if (skidding_timer.started() && !skidding_timer.check())
765 tux_body->set_action("skid-left");
767 tux_body->set_action("skid-right");
769 else if (kick_timer.started() && !kick_timer.check())
772 tux_body->set_action("kick-left");
774 tux_body->set_action("kick-right");
776 else if (butt_jump && is_big())
779 tux_body->set_action("buttjump-left");
781 tux_body->set_action("buttjump-right");
783 else if (!on_ground())
786 tux_body->set_action("jump-left");
788 tux_body->set_action("jump-right");
792 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
795 tux_body->set_action("stand-left");
797 tux_body->set_action("stand-right");
802 tux_body->set_action("walk-left");
804 tux_body->set_action("walk-right");
808 if(idle_timer.check())
813 tux_body->head->set_action("idle-left", 1);
815 tux_body->head->set_action("idle-right", 1);
820 // Tux is holding something
821 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
822 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
827 tux_body->arms->set_action("duck+grab-left");
829 tux_body->arms->set_action("duck+grab-right");
834 tux_body->arms->set_action("grab-left");
836 tux_body->arms->set_action("grab-right");
842 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
844 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
846 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
847 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
849 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
850 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
853 else if (safe_timer.started() && size_t(game_time*40)%2)
856 tux_body->draw(context, get_pos(), layer);
858 // Draw blinking star overlay
859 if (invincible_timer.started() &&
860 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
861 || size_t(game_time*20)%2)
864 if (!is_big() || duck)
865 smalltux_star->draw(context, get_pos(), layer + 5);
867 bigtux_star->draw(context, get_pos(), layer + 5);
872 Player::collision_tile(uint32_t tile_attributes)
874 if(tile_attributes & Tile::HURTS)
879 Player::collision(GameObject& other, const CollisionHit& hit)
881 Bullet* bullet = dynamic_cast<Bullet*> (&other);
886 if(other.get_flags() & FLAG_PORTABLE) {
887 Portable* portable = dynamic_cast<Portable*> (&other);
888 assert(portable != NULL);
889 if(portable && grabbed_object == NULL
890 && controller->hold(Controller::ACTION)
891 && fabsf(hit.normal.x) > .9) {
892 grabbed_object = portable;
893 grabbed_object->grab(*this, get_pos(), dir);
898 if(other.get_flags() & FLAG_SOLID) {
900 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
902 hit.normal.x, hit.normal.y, hit.depth,
903 get_pos().x, get_pos().y,
904 movement.x, movement.y);
907 if(hit.normal.y < 0) { // landed on floor?
908 if(physic.get_velocity_y() > 0)
909 physic.set_velocity_y(0);
911 on_ground_flag = true;
913 // remember normal of this tile
914 if (hit.normal.y > -0.9) {
915 floor_normal.x = hit.normal.x;
916 floor_normal.y = hit.normal.y;
918 // slowly adjust to unisolid tiles.
919 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
920 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
921 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
924 // hack platforms so that we stand normally on them when going down...
925 Platform* platform = dynamic_cast<Platform*> (&other);
926 if(platform != NULL) {
927 if(platform->get_speed().y > 0)
928 physic.set_velocity_y(platform->get_speed().y);
929 //physic.set_velocity_x(platform->get_speed().x);
931 } else if(hit.normal.y > 0) { // bumped against the roof
932 physic.set_velocity_y(-.1);
934 // hack platform so that we are not glued to it from below
935 Platform* platform = dynamic_cast<Platform*> (&other);
936 if(platform != NULL) {
937 physic.set_velocity_y(platform->get_speed().y);
941 if(fabsf(hit.normal.x) > .9) { // hit on the side?
942 physic.set_velocity_x(0);
945 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
947 Vector mov = movement - omov->get_movement();
949 printf("W %p - HITN: %3.1f %3.1f D:%3.1f TM: %3.1f %3.1f TD: %3.1f %3.1f PM: %3.2f %3.1f\n",
951 hit.normal.x, hit.normal.y,
953 movement.x, movement.y,
954 dest.p1.x, dest.p1.y,
955 omov->get_movement().x, omov->get_movement().y);
963 assert(dynamic_cast<MovingObject*> (&other) != NULL);
965 MovingObject* moving_object = static_cast<MovingObject*> (&other);
966 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
967 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
969 if(controller->pressed(Controller::UP))
970 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
976 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
978 if(safe_timer.started() || invincible_timer.started())
988 Player::make_invincible()
990 sound_manager->play("sounds/invincible.wav");
991 invincible_timer.start(TUX_INVINCIBLE_TIME);
992 Sector::current()->play_music(HERRING_MUSIC);
997 Player::kill(bool completely)
999 if(dying || deactivated)
1002 if(!completely && safe_timer.started() || invincible_timer.started())
1005 sound_manager->play("sounds/hurt.wav");
1007 physic.set_velocity_x(0);
1009 if(!completely && is_big()) {
1010 if(player_status->bonus == FIRE_BONUS
1011 || player_status->bonus == ICE_BONUS) {
1012 safe_timer.start(TUX_SAFE_TIME);
1013 set_bonus(GROWUP_BONUS, true);
1015 //growing_timer.start(GROWING_TIME);
1016 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1017 adjust_height(30.8);
1019 set_bonus(NO_BONUS, true);
1022 for (int i = 0; (i < 5) && (i < player_status->coins); i++)
1024 // the numbers: starting x, starting y, velocity y
1025 Sector::current()->add_object(new FallingCoin(get_pos() +
1026 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1027 systemRandom.rand(-100,100)));
1029 physic.enable_gravity(true);
1030 physic.set_acceleration(0, 0);
1031 physic.set_velocity(0, -700);
1032 player_status->coins -= 25;
1033 set_bonus(NO_BONUS, true);
1035 dying_timer.start(3.0);
1036 set_group(COLGROUP_DISABLED);
1038 DisplayEffect* effect = new DisplayEffect();
1039 effect->fade_out(3.0);
1040 Sector::current()->add_object(effect);
1041 sound_manager->stop_music(3.0);
1046 Player::move(const Vector& vector)
1050 // TODO: do we need the following? Seems irrelevant to moving the player
1052 set_size(31.8, 63.8);
1054 set_size(31.8, 31.8);
1056 last_ground_y = vector.y;
1062 Player::check_bounds(Camera* camera)
1064 /* Keep tux in bounds: */
1065 if (get_pos().x < 0) {
1066 // Lock Tux to the size of the level, so that he doesn't fall of
1068 set_pos(Vector(0, get_pos().y));
1071 /* Keep in-bounds, vertically: */
1072 if (get_pos().y > Sector::current()->solids->get_height() * 32) {
1077 bool adjust = false;
1078 // can happen if back scrolling is disabled
1079 if(get_pos().x < camera->get_translation().x) {
1080 set_pos(Vector(camera->get_translation().x, get_pos().y));
1083 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1086 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1095 if(collision_object_map(bbox)) {
1104 Player::add_velocity(const Vector& velocity)
1106 physic.set_velocity(physic.get_velocity() + velocity);
1110 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1112 if (end_speed.x > 0) physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1113 if (end_speed.x < 0) physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1114 if (end_speed.y > 0) physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1115 if (end_speed.y < 0) physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1119 Player::bounce(BadGuy& )
1121 if(controller->hold(Controller::JUMP))
1122 physic.set_velocity_y(-520);
1124 physic.set_velocity_y(-300);
1127 //Scripting Functions Below
1130 Player::deactivate()
1132 if (deactivated) return;
1134 physic.set_velocity_x(0);
1135 physic.set_velocity_y(0);
1136 physic.set_acceleration_x(0);
1137 physic.set_acceleration_y(0);
1143 if (!deactivated) return;
1144 deactivated = false;
1147 void Player::walk(float speed)
1149 physic.set_velocity_x(speed);
1153 Player::set_ghost_mode(bool enable)
1155 if (ghost_mode == enable) return;
1158 set_group(COLGROUP_DISABLED);
1159 physic.enable_gravity(false);
1160 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1163 set_group(COLGROUP_MOVING);
1164 physic.enable_gravity(true);
1165 log_debug << "You feel solid again." << std::endl;