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"
52 static const int TILES_FOR_BUTTJUMP = 3;
53 static const float SHOOTING_TIME = .150;
54 /// time before idle animation starts
55 static const float IDLE_TIME = 2.5;
57 static const float WALK_ACCELERATION_X = 300;
58 static const float RUN_ACCELERATION_X = 400;
59 static const float SKID_XM = 200;
60 static const float SKID_TIME = .3;
61 static const float MAX_WALK_XM = 230;
62 static const float MAX_RUN_XM = 320;
63 static const float WALK_SPEED = 100;
65 static const float KICK_TIME = .3;
66 static const float CHEER_TIME = 1;
68 static const float UNDUCK_HURT_TIME = 0.25; /**< if Tux cannot unduck for this long, he will get hurt */
71 Surface* growingtux_left[GROWING_FRAMES];
72 Surface* growingtux_right[GROWING_FRAMES];
74 Surface* tux_life = 0;
76 TuxBodyParts* small_tux = 0;
77 TuxBodyParts* big_tux = 0;
78 TuxBodyParts* fire_tux = 0;
79 TuxBodyParts* ice_tux = 0;
82 TuxBodyParts::set_action(std::string action, int loops)
85 head->set_action(action, loops);
87 body->set_action(action, loops);
89 arms->set_action(action, loops);
91 feet->set_action(action, loops);
95 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
98 head->draw(context, pos, layer-1);
100 body->draw(context, pos, layer-3);
102 arms->draw(context, pos, layer+10);
104 feet->draw(context, pos, layer-2);
107 Player::Player(PlayerStatus* _player_status)
108 : player_status(_player_status), grabbed_object(NULL), ghost_mode(false)
110 controller = main_controller;
111 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
112 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
113 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
115 sound_manager->preload("sounds/bigjump.wav");
116 sound_manager->preload("sounds/jump.wav");
117 sound_manager->preload("sounds/hurt.wav");
118 sound_manager->preload("sounds/skid.wav");
125 delete smalltux_gameover;
126 delete smalltux_star;
134 set_size(31.8, 62.8);
136 set_size(31.8, 30.8);
145 fall_mode = ON_GROUND;
150 backflipping = false;
151 backflip_direction = 0;
154 on_ground_flag = false;
155 grabbed_object = NULL;
157 floor_normal = Vector(0,-1);
163 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
165 Scripting::Player* interface = static_cast<Scripting::Player*> (this);
166 Scripting::expose_object(vm, table_idx, interface, "Tux", false);
170 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
172 Scripting::unexpose_object(vm, table_idx, "Tux");
176 Player::set_controller(Controller* controller)
178 this->controller = controller;
182 Player::adjust_height(float new_height)
185 bbox2.move(Vector(0, bbox.get_height() - new_height));
186 bbox2.set_height(new_height);
187 if (!Sector::current()->is_free_space(bbox2)) return false;
188 // adjust bbox accordingly
189 // note that we use members of moving_object for this, so we can run this during CD, too
191 set_size(bbox2.get_width(), bbox2.get_height());
196 Player::update(float elapsed_time)
198 if(dying && dying_timer.check()) {
203 if(!dying && !deactivated)
206 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
207 if (deactivated) apply_friction();
209 // extend/shrink tux collision rectangle so that we fall through/walk over 1
211 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
217 // on downward slopes, adjust vertical velocity to match slope angle
219 if (floor_normal.y != 0) {
220 if ((floor_normal.x * physic.get_velocity_x()) > 0) {
221 // we overdo it a little, just to be on the safe side
222 physic.set_velocity_y(-physic.get_velocity_x() * (floor_normal.x / floor_normal.y) * 2);
227 // handle backflipping
229 //prevent player from changing direction when backflipping
230 dir = (backflip_direction == 1) ? LEFT : RIGHT;
231 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
236 fall_mode = ON_GROUND;
237 last_ground_y = get_pos().y;
239 if(get_pos().y > last_ground_y)
241 else if(fall_mode == ON_GROUND)
245 // check if we landed
248 if (backflipping && (!backflip_timer.started())) {
249 backflipping = false;
250 backflip_direction = 0;
252 // if controls are currently deactivated, we take care of standing up ourselves
253 if (deactivated) do_standup();
259 if (butt_jump && on_ground() && is_big()) {
262 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
264 Sector::current()->add_smoke_cloud(
265 Vector(get_pos().x - 32, get_pos().y + 32));
269 // Break bricks beneath Tux
270 if(Sector::current()->trybreakbrick(
271 Vector(base.x + 1, base.y + base.height), false)
272 || Sector::current()->trybreakbrick(
273 Vector(base.x + base.width - 1, base.y + base.height), false)) {
274 physic.set_velocity_y(-2);
278 // Kill nearby badguys
279 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
280 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
281 i != gameobjects.end();
283 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
285 // don't kill when badguys are already dying or in a certain mode
286 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
287 badguy->mode != BadGuy::BOMB_EXPLODE) {
288 if (fabsf(base.x - badguy->base.x) < 96 &&
289 fabsf(base.y - badguy->base.y) < 64)
297 // calculate movement for this frame
298 movement = physic.get_movement(elapsed_time);
300 if(grabbed_object != NULL && !dying) {
301 Vector pos = get_pos() +
302 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
303 grabbed_object->grab(*this, pos, dir);
306 if(grabbed_object != NULL && dying){
307 grabbed_object->ungrab(*this, dir);
308 grabbed_object = NULL;
311 on_ground_flag = false;
317 return on_ground_flag;
323 if(player_status->bonus == NO_BONUS)
330 Player::apply_friction()
332 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
333 physic.set_velocity_x(0);
334 physic.set_acceleration_x(0);
335 } else if(physic.get_velocity_x() < 0) {
336 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
338 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
342 // if we're on ice slow down acceleration or deceleration
343 if (isice(base.x, base.y + base.height))
345 /* the acceleration/deceleration rate on ice is inversely proportional to
346 * the current velocity.
349 // increasing 1 will increase acceleration/deceleration rate
350 // decreasing 1 will decrease acceleration/deceleration rate
351 // must stay above zero, though
352 if (ax != 0) ax *= 1 / fabs(vx);
359 Player::handle_horizontal_input()
361 float vx = physic.get_velocity_x();
362 float vy = physic.get_velocity_y();
363 float ax = physic.get_acceleration_x();
364 float ay = physic.get_acceleration_y();
367 if(!duck || physic.get_velocity_y() != 0) {
368 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
372 } else if(!controller->hold(Controller::LEFT)
373 && controller->hold(Controller::RIGHT)) {
380 if (!controller->hold(Controller::ACTION)) {
381 ax = dirsign * WALK_ACCELERATION_X;
383 if(vx >= MAX_WALK_XM && dirsign > 0) {
386 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
391 ax = dirsign * RUN_ACCELERATION_X;
393 if(vx >= MAX_RUN_XM && dirsign > 0) {
396 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
402 // we can reach WALK_SPEED without any acceleration
403 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
404 vx = dirsign * WALK_SPEED;
407 // changing directions?
408 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
410 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
411 skidding_timer.start(SKID_TIME);
412 sound_manager->play("sounds/skid.wav");
413 // dust some particles
414 Sector::current()->add_object(
416 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
417 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
418 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
427 physic.set_velocity(vx, vy);
428 physic.set_acceleration(ax, ay);
430 // we get slower when not pressing any keys
448 if (!is_big()) return;
450 if (physic.get_velocity_y() != 0) return;
451 if (!on_ground()) return;
453 if (adjust_height(31.8)) {
455 unduck_hurt_timer.stop();
462 Player::do_standup() {
464 if (!is_big()) return;
465 if (backflipping) return;
467 if (adjust_height(63.8)) {
469 unduck_hurt_timer.stop();
471 // if timer is not already running, start it.
472 if (unduck_hurt_timer.get_period() == 0) {
473 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
475 else if (unduck_hurt_timer.check()) {
483 Player::do_backflip() {
485 if (!on_ground()) return;
487 backflip_direction = (dir == LEFT)?(+1):(-1);
490 backflip_timer.start(0.15);
494 Player::do_jump(float yspeed) {
495 if (!on_ground()) return;
497 physic.set_velocity_y(yspeed);
498 //bbox.move(Vector(0, -1));
500 on_ground_flag = false;
505 sound_manager->play("sounds/bigjump.wav");
507 sound_manager->play("sounds/jump.wav");
512 Player::handle_vertical_input()
516 if(controller->pressed(Controller::JUMP) && (can_jump)) {
518 // when running, only jump a little bit; else do a backflip
519 if (physic.get_velocity_x() != 0) do_jump(-300); else do_backflip();
521 // jump a bit higher if we are running; else do a normal jump
522 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
525 // Let go of jump key
526 else if(!controller->hold(Controller::JUMP)) {
527 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
529 physic.set_velocity_y(0);
533 /* In case the player has pressed Down while in a certain range of air,
534 enable butt jump action */
535 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
539 /* When Down is not held anymore, disable butt jump */
540 if(butt_jump && !controller->hold(Controller::DOWN)) butt_jump = false;
542 /** jumping is only allowed if we're about to touch ground soon and if the
543 * button has been up in between the last jump
547 if ( (issolid(get_pos().x + bbox.get_width() / 2,
548 get_pos().y + bbox.get_height() + 64) ||
549 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
550 issolid(get_pos().x + bbox.get_width() - 1,
551 get_pos().y + bbox.get_height() + 64))
554 && input.jump && !input.old_jump)
562 Player::handle_input()
565 handle_input_ghost();
569 if(!controller->hold(Controller::ACTION) && grabbed_object) {
570 // move the grabbed object a bit away from tux
571 Vector pos = get_pos() +
572 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
573 bbox.get_height()*0.66666 - 32);
574 Rect dest(pos, pos + Vector(32, 32));
575 if(Sector::current()->is_free_space(dest)) {
576 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
578 moving_object->set_pos(pos);
580 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
582 grabbed_object->ungrab(*this, dir);
583 grabbed_object = NULL;
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)) do_duck(); else do_standup();
612 Player::handle_input_ghost()
616 if (controller->hold(Controller::LEFT)) {
618 vx -= MAX_RUN_XM * 2;
620 if (controller->hold(Controller::RIGHT)) {
622 vx += MAX_RUN_XM * 2;
624 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
625 vy -= MAX_RUN_XM * 2;
627 if (controller->hold(Controller::DOWN)) {
628 vy += MAX_RUN_XM * 2;
630 if (controller->hold(Controller::ACTION)) {
631 set_ghost_mode(false);
633 physic.set_velocity(vx, vy);
634 physic.set_acceleration(0, 0);
638 Player::add_coins(int count)
640 player_status->add_coins(count);
644 Player::add_bonus(const std::string& bonustype)
646 if(bonustype == "grow") {
647 add_bonus(GROWUP_BONUS);
648 } else if(bonustype == "fireflower") {
649 add_bonus(FIRE_BONUS);
650 } else if(bonustype == "iceflower") {
651 add_bonus(ICE_BONUS);
652 } else if(bonustype == "none") {
655 std::ostringstream msg;
656 msg << "Unknown bonus type " << bonustype;
657 throw std::runtime_error(msg.str());
662 Player::add_bonus(BonusType type, bool animate)
664 // always ignore NO_BONUS
665 if (type == NO_BONUS) {
669 // ignore GROWUP_BONUS if we're already big
670 if (type == GROWUP_BONUS) {
671 if (player_status->bonus == GROWUP_BONUS) return;
672 if (player_status->bonus == FIRE_BONUS) return;
673 if (player_status->bonus == ICE_BONUS) return;
676 set_bonus(type, animate);
680 Player::set_bonus(BonusType type, bool animate)
682 if(player_status->bonus == NO_BONUS) {
683 if (!adjust_height(62.8)) return;
685 growing_timer.start(GROWING_TIME);
688 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
689 player_status->max_fire_bullets = 0;
690 player_status->max_ice_bullets = 0;
692 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
693 if (type == ICE_BONUS) player_status->max_ice_bullets++;
695 player_status->bonus = type;
699 Player::set_visible(bool visible)
701 this->visible = visible;
705 Player::get_visible()
713 kick_timer.start(KICK_TIME);
717 Player::draw(DrawingContext& context)
722 TuxBodyParts* tux_body;
724 if (player_status->bonus == GROWUP_BONUS)
726 else if (player_status->bonus == FIRE_BONUS)
728 else if (player_status->bonus == ICE_BONUS)
731 tux_body = small_tux;
733 int layer = LAYER_OBJECTS + 1;
735 /* Set Tux sprite action */
739 tux_body->set_action("backflip-left");
741 tux_body->set_action("backflip-right");
743 else if (duck && is_big())
746 tux_body->set_action("duck-left");
748 tux_body->set_action("duck-right");
750 else if (skidding_timer.started() && !skidding_timer.check())
753 tux_body->set_action("skid-left");
755 tux_body->set_action("skid-right");
757 else if (kick_timer.started() && !kick_timer.check())
760 tux_body->set_action("kick-left");
762 tux_body->set_action("kick-right");
764 else if (butt_jump && is_big())
767 tux_body->set_action("buttjump-left");
769 tux_body->set_action("buttjump-right");
771 else if (!on_ground())
774 tux_body->set_action("jump-left");
776 tux_body->set_action("jump-right");
780 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
783 tux_body->set_action("stand-left");
785 tux_body->set_action("stand-right");
790 tux_body->set_action("walk-left");
792 tux_body->set_action("walk-right");
796 if(idle_timer.check())
801 tux_body->head->set_action("idle-left", 1);
803 tux_body->head->set_action("idle-right", 1);
808 // Tux is holding something
809 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
810 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
815 tux_body->arms->set_action("duck+grab-left");
817 tux_body->arms->set_action("duck+grab-right");
822 tux_body->arms->set_action("grab-left");
824 tux_body->arms->set_action("grab-right");
830 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
832 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
834 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
835 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
837 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
838 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
841 else if (safe_timer.started() && size_t(game_time*40)%2)
844 tux_body->draw(context, get_pos(), layer);
846 // Draw blinking star overlay
847 if (invincible_timer.started() &&
848 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
849 || size_t(game_time*20)%2)
852 if (!is_big() || duck)
853 smalltux_star->draw(context, get_pos(), layer + 5);
855 bigtux_star->draw(context, get_pos(), layer + 5);
860 Player::collision_tile(uint32_t tile_attributes)
862 if(tile_attributes & Tile::HURTS)
867 Player::collision(GameObject& other, const CollisionHit& hit)
869 Bullet* bullet = dynamic_cast<Bullet*> (&other);
874 if(other.get_flags() & FLAG_PORTABLE) {
875 Portable* portable = dynamic_cast<Portable*> (&other);
876 assert(portable != NULL);
877 if(portable && grabbed_object == NULL
878 && controller->hold(Controller::ACTION)
879 && fabsf(hit.normal.x) > .9) {
880 grabbed_object = portable;
881 grabbed_object->grab(*this, get_pos(), dir);
886 if(other.get_flags() & FLAG_SOLID) {
888 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
890 hit.normal.x, hit.normal.y, hit.depth,
891 get_pos().x, get_pos().y,
892 movement.x, movement.y);
895 if(hit.normal.y < 0) { // landed on floor?
896 if(physic.get_velocity_y() > 0)
897 physic.set_velocity_y(0);
899 on_ground_flag = true;
901 // remember normal of this tile
902 if (hit.normal.y > -0.9) {
903 floor_normal.x = hit.normal.x;
904 floor_normal.y = hit.normal.y;
906 // slowly adjust to unisolid tiles.
907 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
908 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
909 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
912 // hack platforms so that we stand normally on them when going down...
913 Platform* platform = dynamic_cast<Platform*> (&other);
914 if(platform != NULL) {
915 if(platform->get_speed().y > 0)
916 physic.set_velocity_y(platform->get_speed().y);
917 //physic.set_velocity_x(platform->get_speed().x);
919 } else if(hit.normal.y > 0) { // bumped against the roof
920 physic.set_velocity_y(-.1);
922 // hack platform so that we are not glued to it from below
923 Platform* platform = dynamic_cast<Platform*> (&other);
924 if(platform != NULL) {
925 physic.set_velocity_y(platform->get_speed().y);
929 if(fabsf(hit.normal.x) > .9) { // hit on the side?
930 physic.set_velocity_x(0);
933 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
935 Vector mov = movement - omov->get_movement();
937 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",
939 hit.normal.x, hit.normal.y,
941 movement.x, movement.y,
942 dest.p1.x, dest.p1.y,
943 omov->get_movement().x, omov->get_movement().y);
951 assert(dynamic_cast<MovingObject*> (&other) != NULL);
953 MovingObject* moving_object = static_cast<MovingObject*> (&other);
954 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
955 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
957 if(controller->pressed(Controller::UP))
958 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
964 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
966 if(safe_timer.started() || invincible_timer.started())
976 Player::make_invincible()
978 sound_manager->play("sounds/invincible.wav");
979 invincible_timer.start(TUX_INVINCIBLE_TIME);
980 Sector::current()->play_music(HERRING_MUSIC);
985 Player::kill(bool completely)
987 if(dying || deactivated)
990 if(!completely && safe_timer.started() || invincible_timer.started())
993 sound_manager->play("sounds/hurt.wav");
995 physic.set_velocity_x(0);
997 if(!completely && is_big()) {
998 if(player_status->bonus == FIRE_BONUS
999 || player_status->bonus == ICE_BONUS) {
1000 safe_timer.start(TUX_SAFE_TIME);
1001 set_bonus(GROWUP_BONUS);
1003 //growing_timer.start(GROWING_TIME);
1004 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1005 adjust_height(30.8);
1007 set_bonus(NO_BONUS);
1010 for (int i = 0; (i < 5) && (i < player_status->coins); i++)
1012 // the numbers: starting x, starting y, velocity y
1013 Sector::current()->add_object(new FallingCoin(get_pos() +
1014 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1015 systemRandom.rand(-100,100)));
1017 physic.enable_gravity(true);
1018 physic.set_acceleration(0, 0);
1019 physic.set_velocity(0, -700);
1020 player_status->coins -= 25;
1021 set_bonus(NO_BONUS);
1023 dying_timer.start(3.0);
1024 set_group(COLGROUP_DISABLED);
1026 DisplayEffect* effect = new DisplayEffect();
1027 effect->fade_out(3.0);
1028 Sector::current()->add_object(effect);
1029 sound_manager->stop_music(3.0);
1034 Player::move(const Vector& vector)
1038 // TODO: do we need the following? Seems irrelevant to moving the player
1040 set_size(31.8, 63.8);
1042 set_size(31.8, 31.8);
1044 last_ground_y = vector.y;
1050 Player::check_bounds(Camera* camera)
1052 /* Keep tux in bounds: */
1053 if (get_pos().x < 0) {
1054 // Lock Tux to the size of the level, so that he doesn't fall of
1056 set_pos(Vector(0, get_pos().y));
1059 /* Keep in-bounds, vertically: */
1060 if (get_pos().y > Sector::current()->solids->get_height() * 32) {
1065 bool adjust = false;
1066 // can happen if back scrolling is disabled
1067 if(get_pos().x < camera->get_translation().x) {
1068 set_pos(Vector(camera->get_translation().x, get_pos().y));
1071 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1074 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1083 if(collision_object_map(bbox)) {
1092 Player::add_velocity(const Vector& velocity)
1094 physic.set_velocity(physic.get_velocity() + velocity);
1098 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1100 if (end_speed.x > 0) physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1101 if (end_speed.x < 0) physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1102 if (end_speed.y > 0) physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1103 if (end_speed.y < 0) physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1107 Player::bounce(BadGuy& )
1109 if(controller->hold(Controller::JUMP))
1110 physic.set_velocity_y(-520);
1112 physic.set_velocity_y(-300);
1115 //Scripting Functions Below
1118 Player::deactivate()
1120 if (deactivated) return;
1122 physic.set_velocity_x(0);
1123 physic.set_velocity_y(0);
1124 physic.set_acceleration_x(0);
1125 physic.set_acceleration_y(0);
1131 if (!deactivated) return;
1132 deactivated = false;
1135 void Player::walk(float speed)
1137 physic.set_velocity_x(speed);
1141 Player::set_ghost_mode(bool enable)
1143 if (ghost_mode == enable) return;
1146 set_group(COLGROUP_DISABLED);
1147 physic.enable_gravity(false);
1148 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1151 set_group(COLGROUP_MOVING);
1152 physic.enable_gravity(true);
1153 log_debug << "You feel solid again." << std::endl;