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)
112 : player_status(_player_status), grabbed_object(NULL), ghost_mode(false)
114 controller = main_controller;
115 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
116 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
117 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
119 sound_manager->preload("sounds/bigjump.wav");
120 sound_manager->preload("sounds/jump.wav");
121 sound_manager->preload("sounds/hurt.wav");
122 sound_manager->preload("sounds/skid.wav");
123 sound_manager->preload("sounds/flip.wav");
124 sound_manager->preload("sounds/invincible.wav");
125 sound_manager->preload("sounds/splash.ogg");
133 delete smalltux_gameover;
134 delete smalltux_star;
142 set_size(31.8, 62.8);
144 set_size(31.8, 30.8);
154 fall_mode = ON_GROUND;
159 backflipping = false;
160 backflip_direction = 0;
164 on_ground_flag = false;
165 grabbed_object = NULL;
171 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
173 Scripting::Player* interface = static_cast<Scripting::Player*> (this);
174 Scripting::expose_object(vm, table_idx, interface, "Tux", false);
178 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
180 Scripting::unexpose_object(vm, table_idx, "Tux");
184 Player::set_controller(Controller* controller)
186 this->controller = controller;
190 Player::adjust_height(float new_height)
193 bbox2.move(Vector(0, bbox.get_height() - new_height));
194 bbox2.set_height(new_height);
195 if (!Sector::current()->is_free_space(bbox2))
198 // adjust bbox accordingly
199 // note that we use members of moving_object for this, so we can run this during CD, too
201 set_size(bbox2.get_width(), bbox2.get_height());
206 Player::update(float elapsed_time)
213 if(dying && dying_timer.check()) {
218 if(!dying && !deactivated)
221 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
222 if (deactivated) apply_friction();
224 // extend/shrink tux collision rectangle so that we fall through/walk over 1
226 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
232 // on downward slopes, adjust vertical velocity so tux walks smoothly down
234 if(floor_normal.y != 0) {
235 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
236 physic.set_velocity_y(250);
241 // handle backflipping
243 //prevent player from changing direction when backflipping
244 dir = (backflip_direction == 1) ? LEFT : RIGHT;
245 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
250 fall_mode = ON_GROUND;
251 last_ground_y = get_pos().y;
253 if(get_pos().y > last_ground_y)
255 else if(fall_mode == ON_GROUND)
259 // check if we landed
262 if (backflipping && (!backflip_timer.started())) {
263 backflipping = false;
264 backflip_direction = 0;
266 // if controls are currently deactivated, we take care of standing up ourselves
267 if (deactivated) do_standup();
273 if (butt_jump && on_ground() && is_big()) {
276 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
278 Sector::current()->add_smoke_cloud(
279 Vector(get_pos().x - 32, get_pos().y + 32));
283 // Break bricks beneath Tux
284 if(Sector::current()->trybreakbrick(
285 Vector(base.x + 1, base.y + base.height), false)
286 || Sector::current()->trybreakbrick(
287 Vector(base.x + base.width - 1, base.y + base.height), false)) {
288 physic.set_velocity_y(-2);
292 // Kill nearby badguys
293 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
294 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
295 i != gameobjects.end();
297 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
299 // don't kill when badguys are already dying or in a certain mode
300 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
301 badguy->mode != BadGuy::BOMB_EXPLODE) {
302 if (fabsf(base.x - badguy->base.x) < 96 &&
303 fabsf(base.y - badguy->base.y) < 64)
311 // calculate movement for this frame
312 movement = physic.get_movement(elapsed_time);
314 if(grabbed_object != NULL && !dying) {
315 Vector pos = get_pos() +
316 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
317 grabbed_object->grab(*this, pos, dir);
320 if(grabbed_object != NULL && dying){
321 grabbed_object->ungrab(*this, dir);
322 grabbed_object = NULL;
325 on_ground_flag = false;
327 // when invincible, spawn particles
328 if (invincible_timer.started() && !dying)
330 if (systemRandom.rand(0, 2) == 0) {
331 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
332 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
333 Vector ppos = Vector(px, py);
334 Vector pspeed = Vector(0, 0);
335 Vector paccel = Vector(0, 0);
336 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
337 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
338 // make every other a longer sparkle to make trail a bit fuzzy
339 if (size_t(game_time*20)%2) {
340 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
342 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
345 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
355 return on_ground_flag;
361 if(player_status->bonus == NO_BONUS)
368 Player::apply_friction()
370 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
371 physic.set_velocity_x(0);
372 physic.set_acceleration_x(0);
373 } else if(physic.get_velocity_x() < 0) {
374 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
376 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
380 // if we're on ice slow down acceleration or deceleration
381 if (isice(base.x, base.y + base.height))
383 /* the acceleration/deceleration rate on ice is inversely proportional to
384 * the current velocity.
387 // increasing 1 will increase acceleration/deceleration rate
388 // decreasing 1 will decrease acceleration/deceleration rate
389 // must stay above zero, though
390 if (ax != 0) ax *= 1 / fabs(vx);
397 Player::handle_horizontal_input()
399 float vx = physic.get_velocity_x();
400 float vy = physic.get_velocity_y();
401 float ax = physic.get_acceleration_x();
402 float ay = physic.get_acceleration_y();
405 if(!duck || physic.get_velocity_y() != 0) {
406 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
410 } else if(!controller->hold(Controller::LEFT)
411 && controller->hold(Controller::RIGHT)) {
418 // only run if action key is pressed and we're not holding anything
419 if (!(controller->hold(Controller::ACTION) && (!grabbed_object))) {
420 ax = dirsign * WALK_ACCELERATION_X;
422 if(vx >= MAX_WALK_XM && dirsign > 0) {
425 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
430 ax = dirsign * RUN_ACCELERATION_X;
432 if(vx >= MAX_RUN_XM && dirsign > 0) {
435 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
441 // we can reach WALK_SPEED without any acceleration
442 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
443 vx = dirsign * WALK_SPEED;
446 // changing directions?
447 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
449 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
450 skidding_timer.start(SKID_TIME);
451 sound_manager->play("sounds/skid.wav");
452 // dust some particles
453 Sector::current()->add_object(
455 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
456 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
457 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
466 physic.set_velocity(vx, vy);
467 physic.set_acceleration(ax, ay);
469 // we get slower when not pressing any keys
487 if (!is_big()) return;
489 if (physic.get_velocity_y() != 0) return;
490 if (!on_ground()) return;
492 if (adjust_height(31.8)) {
494 unduck_hurt_timer.stop();
501 Player::do_standup() {
503 if (!is_big()) return;
504 if (backflipping) return;
506 if (adjust_height(63.8)) {
508 unduck_hurt_timer.stop();
510 // if timer is not already running, start it.
511 if (unduck_hurt_timer.get_period() == 0) {
512 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
514 else if (unduck_hurt_timer.check()) {
522 Player::do_backflip() {
524 if (!on_ground()) return;
526 // TODO: we don't have an animation for firetux backflipping, so let's revert to bigtux
527 set_bonus(GROWUP_BONUS, true);
529 backflip_direction = (dir == LEFT)?(+1):(-1);
532 sound_manager->play("sounds/flip.wav");
533 backflip_timer.start(0.15);
537 Player::do_jump(float yspeed) {
538 if (!on_ground()) return;
540 physic.set_velocity_y(yspeed);
541 //bbox.move(Vector(0, -1));
543 on_ground_flag = false;
548 sound_manager->play("sounds/bigjump.wav");
550 sound_manager->play("sounds/jump.wav");
555 Player::handle_vertical_input()
559 if(controller->pressed(Controller::JUMP) && (can_jump)) {
561 // when running, only jump a little bit; else do a backflip
562 if (physic.get_velocity_x() != 0) do_jump(-300); else do_backflip();
564 // jump a bit higher if we are running; else do a normal jump
565 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
568 // Let go of jump key
569 else if(!controller->hold(Controller::JUMP)) {
570 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
572 physic.set_velocity_y(0);
576 /* In case the player has pressed Down while in a certain range of air,
577 enable butt jump action */
578 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
582 /* When Down is not held anymore, disable butt jump */
583 if(butt_jump && !controller->hold(Controller::DOWN))
588 Player::handle_input()
591 handle_input_ghost();
595 if(!controller->hold(Controller::ACTION) && grabbed_object) {
596 // move the grabbed object a bit away from tux
597 Vector pos = get_pos() +
598 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
599 bbox.get_height()*0.66666 - 32);
600 Rect dest(pos, pos + Vector(32, 32));
601 if(Sector::current()->is_free_space(dest)) {
602 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
604 moving_object->set_pos(pos);
606 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
608 grabbed_object->ungrab(*this, dir);
609 grabbed_object = NULL;
614 if( controller->released( Controller::PEEK_LEFT ) ) {
617 if( controller->released( Controller::PEEK_RIGHT ) ) {
620 if( controller->pressed( Controller::PEEK_LEFT ) ) {
623 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
627 /* Handle horizontal movement: */
628 if (!backflipping) handle_horizontal_input();
631 if (on_ground() && !controller->hold(Controller::JUMP))
634 /* Handle vertical movement: */
635 handle_vertical_input();
638 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
639 if(Sector::current()->add_bullet(
640 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
641 : Vector(32, bbox.get_height()/2)),
642 physic.get_velocity_x(), dir))
643 shooting_timer.start(SHOOTING_TIME);
646 /* Duck or Standup! */
647 if (controller->hold(Controller::DOWN)) do_duck(); else do_standup();
652 Player::handle_input_ghost()
656 if (controller->hold(Controller::LEFT)) {
658 vx -= MAX_RUN_XM * 2;
660 if (controller->hold(Controller::RIGHT)) {
662 vx += MAX_RUN_XM * 2;
664 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
665 vy -= MAX_RUN_XM * 2;
667 if (controller->hold(Controller::DOWN)) {
668 vy += MAX_RUN_XM * 2;
670 if (controller->hold(Controller::ACTION)) {
671 set_ghost_mode(false);
673 physic.set_velocity(vx, vy);
674 physic.set_acceleration(0, 0);
678 Player::add_coins(int count)
680 player_status->add_coins(count);
684 Player::add_bonus(const std::string& bonustype)
686 BonusType type = NO_BONUS;
688 if(bonustype == "grow") {
690 } else if(bonustype == "fireflower") {
692 } else if(bonustype == "iceflower") {
694 } else if(bonustype == "none") {
697 std::ostringstream msg;
698 msg << "Unknown bonus type " << bonustype;
699 throw std::runtime_error(msg.str());
702 return add_bonus(type);
706 Player::add_bonus(BonusType type, bool animate)
708 // always ignore NO_BONUS
709 if (type == NO_BONUS) {
713 // ignore GROWUP_BONUS if we're already big
714 if (type == GROWUP_BONUS) {
715 if (player_status->bonus == GROWUP_BONUS)
717 if (player_status->bonus == FIRE_BONUS)
719 if (player_status->bonus == ICE_BONUS)
723 return set_bonus(type, animate);
727 Player::set_bonus(BonusType type, bool animate)
729 if(player_status->bonus == NO_BONUS) {
730 if (!adjust_height(62.8)) {
731 printf("can't adjust\n");
735 growing_timer.start(GROWING_TIME);
738 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
739 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
740 // visually lose helmet
741 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
742 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
743 Vector paccel = Vector(0, 1000);
744 std::string action = (dir==LEFT)?"left":"right";
745 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
747 player_status->max_fire_bullets = 0;
748 player_status->max_ice_bullets = 0;
750 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
751 if (type == ICE_BONUS) player_status->max_ice_bullets++;
753 player_status->bonus = type;
758 Player::set_visible(bool visible)
760 this->visible = visible;
762 set_group(COLGROUP_MOVING);
764 set_group(COLGROUP_DISABLED);
768 Player::get_visible()
776 kick_timer.start(KICK_TIME);
780 Player::draw(DrawingContext& context)
785 TuxBodyParts* tux_body;
787 if (player_status->bonus == GROWUP_BONUS)
789 else if (player_status->bonus == FIRE_BONUS)
791 else if (player_status->bonus == ICE_BONUS)
794 tux_body = small_tux;
796 int layer = LAYER_OBJECTS + 1;
798 /* Set Tux sprite action */
802 tux_body->set_action("backflip-left");
804 tux_body->set_action("backflip-right");
806 else if (duck && is_big())
809 tux_body->set_action("duck-left");
811 tux_body->set_action("duck-right");
813 else if (skidding_timer.started() && !skidding_timer.check())
816 tux_body->set_action("skid-left");
818 tux_body->set_action("skid-right");
820 else if (kick_timer.started() && !kick_timer.check())
823 tux_body->set_action("kick-left");
825 tux_body->set_action("kick-right");
827 else if (butt_jump && is_big())
830 tux_body->set_action("buttjump-left");
832 tux_body->set_action("buttjump-right");
834 else if (!on_ground())
837 tux_body->set_action("jump-left");
839 tux_body->set_action("jump-right");
843 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
846 tux_body->set_action("stand-left");
848 tux_body->set_action("stand-right");
853 tux_body->set_action("walk-left");
855 tux_body->set_action("walk-right");
859 if(idle_timer.check())
864 tux_body->head->set_action("idle-left", 1);
866 tux_body->head->set_action("idle-right", 1);
871 // Tux is holding something
872 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
873 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
878 tux_body->arms->set_action("duck+grab-left");
880 tux_body->arms->set_action("duck+grab-right");
885 tux_body->arms->set_action("grab-left");
887 tux_body->arms->set_action("grab-right");
893 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
895 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
897 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
898 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
900 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
901 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
904 else if (safe_timer.started() && size_t(game_time*40)%2)
907 tux_body->draw(context, get_pos(), layer);
912 Player::collision_tile(uint32_t tile_attributes)
914 if(tile_attributes & Tile::HURTS)
918 if( tile_attributes & Tile::WATER ){
924 if( tile_attributes & Tile::WATER ){
927 sound_manager->play( "sounds/splash.ogg" );
933 Player::collision_solid(const CollisionHit& hit)
936 if(physic.get_velocity_y() > 0)
937 physic.set_velocity_y(0);
939 on_ground_flag = true;
940 floor_normal = hit.slope_normal;
942 if(physic.get_velocity_y() < 0)
943 physic.set_velocity_y(.2);
946 if(hit.left || hit.right) {
947 physic.set_velocity_x(0);
952 if(hit.left || hit.right) {
954 } else if(hit.top || hit.bottom) {
961 Player::collision(GameObject& other, const CollisionHit& hit)
963 Bullet* bullet = dynamic_cast<Bullet*> (&other);
968 // if we hit something from the side that is portable, the ACTION button is pressed and we are not already holding anything: grab it
969 if ((hit.left || hit.right) && (other.get_flags() & FLAG_PORTABLE) && controller->hold(Controller::ACTION) && (!grabbed_object)) {
970 Portable* portable = dynamic_cast<Portable*> (&other);
971 assert(portable != NULL);
973 grabbed_object = portable;
974 grabbed_object->grab(*this, get_pos(), dir);
980 assert(dynamic_cast<MovingObject*> (&other) != NULL);
982 MovingObject* moving_object = static_cast<MovingObject*> (&other);
983 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
984 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
986 if(controller->pressed(Controller::UP))
987 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
993 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
995 if(safe_timer.started() || invincible_timer.started())
1005 Player::make_invincible()
1007 sound_manager->play("sounds/invincible.wav");
1008 invincible_timer.start(TUX_INVINCIBLE_TIME);
1009 Sector::current()->play_music(HERRING_MUSIC);
1014 Player::kill(bool completely)
1016 if(dying || deactivated)
1019 if(!completely && (safe_timer.started() || invincible_timer.started()))
1022 sound_manager->play("sounds/hurt.wav");
1024 physic.set_velocity_x(0);
1026 if(!completely && is_big()) {
1027 if(player_status->bonus == FIRE_BONUS
1028 || player_status->bonus == ICE_BONUS) {
1029 safe_timer.start(TUX_SAFE_TIME);
1030 set_bonus(GROWUP_BONUS, true);
1032 //growing_timer.start(GROWING_TIME);
1033 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1034 adjust_height(30.8);
1036 set_bonus(NO_BONUS, true);
1039 for (int i = 0; (i < 5) && (i < player_status->coins); i++)
1041 // the numbers: starting x, starting y, velocity y
1042 Sector::current()->add_object(new FallingCoin(get_pos() +
1043 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1044 systemRandom.rand(-100,100)));
1046 physic.enable_gravity(true);
1047 physic.set_acceleration(0, 0);
1048 physic.set_velocity(0, -700);
1049 player_status->coins -= 25;
1050 set_bonus(NO_BONUS, true);
1052 dying_timer.start(3.0);
1053 set_group(COLGROUP_DISABLED);
1055 DisplayEffect* effect = new DisplayEffect();
1056 effect->fade_out(3.0);
1057 Sector::current()->add_object(effect);
1058 sound_manager->stop_music(3.0);
1063 Player::move(const Vector& vector)
1067 // TODO: do we need the following? Seems irrelevant to moving the player
1069 set_size(31.8, 63.8);
1071 set_size(31.8, 31.8);
1073 last_ground_y = vector.y;
1079 Player::check_bounds(Camera* camera)
1081 /* Keep tux in bounds: */
1082 if (get_pos().x < 0) {
1083 // Lock Tux to the size of the level, so that he doesn't fall of
1085 set_pos(Vector(0, get_pos().y));
1088 /* Keep in-bounds, vertically: */
1089 if (get_pos().y > Sector::current()->get_height() * 32) {
1094 bool adjust = false;
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));
1100 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1103 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1112 if(collision_object_map(bbox)) {
1121 Player::add_velocity(const Vector& velocity)
1123 physic.set_velocity(physic.get_velocity() + velocity);
1127 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1129 if (end_speed.x > 0) physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1130 if (end_speed.x < 0) physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1131 if (end_speed.y > 0) physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1132 if (end_speed.y < 0) physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1136 Player::bounce(BadGuy& )
1138 if(controller->hold(Controller::JUMP))
1139 physic.set_velocity_y(-520);
1141 physic.set_velocity_y(-300);
1144 //Scripting Functions Below
1147 Player::deactivate()
1149 if (deactivated) return;
1151 physic.set_velocity_x(0);
1152 physic.set_velocity_y(0);
1153 physic.set_acceleration_x(0);
1154 physic.set_acceleration_y(0);
1160 if (!deactivated) return;
1161 deactivated = false;
1164 void Player::walk(float speed)
1166 physic.set_velocity_x(speed);
1170 Player::set_ghost_mode(bool enable)
1172 if (ghost_mode == enable) return;
1175 set_group(COLGROUP_DISABLED);
1176 physic.enable_gravity(false);
1177 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1180 set_group(COLGROUP_MOVING);
1181 physic.enable_gravity(true);
1182 log_debug << "You feel solid again." << std::endl;