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");
119 airarrow.reset(new Surface("images/engine/hud/airarrow.png"));
121 sound_manager->preload("sounds/bigjump.wav");
122 sound_manager->preload("sounds/jump.wav");
123 sound_manager->preload("sounds/hurt.wav");
124 sound_manager->preload("sounds/skid.wav");
125 sound_manager->preload("sounds/flip.wav");
126 sound_manager->preload("sounds/invincible.wav");
127 sound_manager->preload("sounds/splash.ogg");
135 delete smalltux_gameover;
136 delete smalltux_star;
144 set_size(31.8, 62.8);
146 set_size(31.8, 30.8);
156 fall_mode = ON_GROUND;
161 backflipping = false;
162 backflip_direction = 0;
166 on_ground_flag = false;
167 grabbed_object = NULL;
173 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
178 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
182 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
187 Scripting::unexpose_object(vm, table_idx, name);
191 Player::set_controller(Controller* controller)
193 this->controller = controller;
197 Player::adjust_height(float new_height)
200 bbox2.move(Vector(0, bbox.get_height() - new_height));
201 bbox2.set_height(new_height);
203 if(new_height > bbox.get_height()) {
204 Rect additional_space = bbox2;
205 additional_space.set_height(new_height - bbox.get_height());
206 if(!Sector::current()->is_free_of_statics(additional_space, this))
210 // adjust bbox accordingly
211 // note that we use members of moving_object for this, so we can run this during CD, too
213 set_size(bbox2.get_width(), bbox2.get_height());
218 Player::update(float elapsed_time)
225 if(dying && dying_timer.check()) {
230 if(!dying && !deactivated)
233 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
237 // extend/shrink tux collision rectangle so that we fall through/walk over 1
239 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
245 // on downward slopes, adjust vertical velocity so tux walks smoothly down
247 if(floor_normal.y != 0) {
248 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
249 physic.set_velocity_y(250);
254 // handle backflipping
256 //prevent player from changing direction when backflipping
257 dir = (backflip_direction == 1) ? LEFT : RIGHT;
258 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
263 fall_mode = ON_GROUND;
264 last_ground_y = get_pos().y;
266 if(get_pos().y > last_ground_y)
268 else if(fall_mode == ON_GROUND)
272 // check if we landed
275 if (backflipping && (!backflip_timer.started())) {
276 backflipping = false;
277 backflip_direction = 0;
279 // if controls are currently deactivated, we take care of standing up ourselves
285 // calculate movement for this frame
286 movement = physic.get_movement(elapsed_time);
288 if(grabbed_object != NULL && !dying) {
289 Vector pos = get_pos() +
290 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
291 grabbed_object->grab(*this, pos, dir);
294 if(grabbed_object != NULL && dying){
295 grabbed_object->ungrab(*this, dir);
296 grabbed_object = NULL;
299 on_ground_flag = false;
301 // when invincible, spawn particles
302 if (invincible_timer.started() && !dying)
304 if (systemRandom.rand(0, 2) == 0) {
305 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
306 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
307 Vector ppos = Vector(px, py);
308 Vector pspeed = Vector(0, 0);
309 Vector paccel = Vector(0, 0);
310 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
311 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
312 // make every other a longer sparkle to make trail a bit fuzzy
313 if (size_t(game_time*20)%2) {
314 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
316 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
319 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
329 return on_ground_flag;
335 if(player_status->bonus == NO_BONUS)
342 Player::apply_friction()
344 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
345 physic.set_velocity_x(0);
346 physic.set_acceleration_x(0);
347 } else if(physic.get_velocity_x() < 0) {
348 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
349 } else if(physic.get_velocity_x() > 0) {
350 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
355 Player::handle_horizontal_input()
357 float vx = physic.get_velocity_x();
358 float vy = physic.get_velocity_y();
359 float ax = physic.get_acceleration_x();
360 float ay = physic.get_acceleration_y();
363 if(!duck || physic.get_velocity_y() != 0) {
364 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
368 } else if(!controller->hold(Controller::LEFT)
369 && controller->hold(Controller::RIGHT)) {
376 // only run if action key is pressed and we're not holding anything
377 if (!(controller->hold(Controller::ACTION) && (!grabbed_object))) {
378 ax = dirsign * WALK_ACCELERATION_X;
380 if(vx >= MAX_WALK_XM && dirsign > 0) {
383 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
388 ax = dirsign * RUN_ACCELERATION_X;
390 if(vx >= MAX_RUN_XM && dirsign > 0) {
393 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
399 // we can reach WALK_SPEED without any acceleration
400 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
401 vx = dirsign * WALK_SPEED;
404 // changing directions?
405 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
407 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
408 skidding_timer.start(SKID_TIME);
409 sound_manager->play("sounds/skid.wav");
410 // dust some particles
411 Sector::current()->add_object(
413 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
414 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
415 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
424 physic.set_velocity(vx, vy);
425 physic.set_acceleration(ax, ay);
427 // we get slower when not pressing any keys
449 if (physic.get_velocity_y() != 0)
454 if (adjust_height(31.8)) {
456 unduck_hurt_timer.stop();
463 Player::do_standup() {
471 if (adjust_height(63.8)) {
473 unduck_hurt_timer.stop();
475 // if timer is not already running, start it.
476 if (unduck_hurt_timer.get_period() == 0) {
477 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
479 else if (unduck_hurt_timer.check()) {
487 Player::do_backflip() {
493 // TODO: we don't have an animation for firetux backflipping, so let's revert to bigtux
494 set_bonus(GROWUP_BONUS, true);
496 backflip_direction = (dir == LEFT)?(+1):(-1);
499 sound_manager->play("sounds/flip.wav");
500 backflip_timer.start(0.15);
504 Player::do_jump(float yspeed) {
508 physic.set_velocity_y(yspeed);
509 //bbox.move(Vector(0, -1));
511 on_ground_flag = false;
516 sound_manager->play("sounds/bigjump.wav");
518 sound_manager->play("sounds/jump.wav");
523 Player::handle_vertical_input()
526 if(controller->pressed(Controller::JUMP) && (can_jump)) {
528 // when running, only jump a little bit; else do a backflip
529 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
531 // jump a bit higher if we are running; else do a normal jump
532 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
535 // Let go of jump key
536 else if(!controller->hold(Controller::JUMP)) {
537 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
539 physic.set_velocity_y(0);
543 /* In case the player has pressed Down while in a certain range of air,
544 enable butt jump action */
545 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
549 /* When Down is not held anymore, disable butt jump */
550 if(butt_jump && !controller->hold(Controller::DOWN))
554 physic.set_acceleration_y(0);
556 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
557 physic.set_acceleration_y(-2000);
558 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
563 Player::handle_input()
566 handle_input_ghost();
571 if( controller->released( Controller::PEEK_LEFT ) ) {
574 if( controller->released( Controller::PEEK_RIGHT ) ) {
577 if( controller->pressed( Controller::PEEK_LEFT ) ) {
580 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
584 /* Handle horizontal movement: */
585 if (!backflipping) handle_horizontal_input();
588 if (on_ground() && !controller->hold(Controller::JUMP))
591 /* Handle vertical movement: */
592 handle_vertical_input();
595 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
596 if(Sector::current()->add_bullet(
597 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
598 : Vector(32, bbox.get_height()/2)),
599 physic.get_velocity_x(), dir))
600 shooting_timer.start(SHOOTING_TIME);
603 /* Duck or Standup! */
604 if (controller->hold(Controller::DOWN)) {
613 if(!controller->hold(Controller::ACTION) && grabbed_object) {
614 // move the grabbed object a bit away from tux
615 Vector pos = get_pos() +
616 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
617 bbox.get_height()*0.66666 - 32);
618 Rect dest(pos, pos + Vector(32, 32));
619 if(Sector::current()->is_free_of_movingstatics(dest)) {
620 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
622 moving_object->set_pos(pos);
624 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
626 grabbed_object->ungrab(*this, dir);
627 grabbed_object = NULL;
635 if(controller->hold(Controller::ACTION) && !grabbed_object
637 Sector* sector = Sector::current();
640 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
642 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
645 for(Sector::Portables::iterator i = sector->portables.begin();
646 i != sector->portables.end(); ++i) {
647 Portable* portable = *i;
648 if(!portable->is_portable())
651 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
653 if(moving_object == NULL)
656 if(moving_object->get_bbox().contains(pos)) {
657 grabbed_object = portable;
658 grabbed_object->grab(*this, get_pos(), dir);
666 Player::handle_input_ghost()
670 if (controller->hold(Controller::LEFT)) {
672 vx -= MAX_RUN_XM * 2;
674 if (controller->hold(Controller::RIGHT)) {
676 vx += MAX_RUN_XM * 2;
678 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
679 vy -= MAX_RUN_XM * 2;
681 if (controller->hold(Controller::DOWN)) {
682 vy += MAX_RUN_XM * 2;
684 if (controller->hold(Controller::ACTION)) {
685 set_ghost_mode(false);
687 physic.set_velocity(vx, vy);
688 physic.set_acceleration(0, 0);
692 Player::add_coins(int count)
694 player_status->add_coins(count);
700 return player_status->coins;
704 Player::add_bonus(const std::string& bonustype)
706 BonusType type = NO_BONUS;
708 if(bonustype == "grow") {
710 } else if(bonustype == "fireflower") {
712 } else if(bonustype == "iceflower") {
714 } else if(bonustype == "none") {
717 std::ostringstream msg;
718 msg << "Unknown bonus type " << bonustype;
719 throw std::runtime_error(msg.str());
722 return add_bonus(type);
726 Player::add_bonus(BonusType type, bool animate)
728 // always ignore NO_BONUS
729 if (type == NO_BONUS) {
733 // ignore GROWUP_BONUS if we're already big
734 if (type == GROWUP_BONUS) {
735 if (player_status->bonus == GROWUP_BONUS)
737 if (player_status->bonus == FIRE_BONUS)
739 if (player_status->bonus == ICE_BONUS)
743 return set_bonus(type, animate);
747 Player::set_bonus(BonusType type, bool animate)
749 if(player_status->bonus == NO_BONUS) {
750 if (!adjust_height(62.8)) {
751 printf("can't adjust\n");
755 growing_timer.start(GROWING_TIME);
758 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
759 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
760 // visually lose helmet
761 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
762 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
763 Vector paccel = Vector(0, 1000);
764 std::string action = (dir==LEFT)?"left":"right";
765 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
767 if ((player_status->bonus == ICE_BONUS) && (animate)) {
769 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
770 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
771 Vector paccel = Vector(0, 1000);
772 std::string action = (dir==LEFT)?"left":"right";
773 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
775 player_status->max_fire_bullets = 0;
776 player_status->max_ice_bullets = 0;
778 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
779 if (type == ICE_BONUS) player_status->max_ice_bullets++;
781 player_status->bonus = type;
786 Player::set_visible(bool visible)
788 this->visible = visible;
790 set_group(COLGROUP_MOVING);
792 set_group(COLGROUP_DISABLED);
796 Player::get_visible()
804 kick_timer.start(KICK_TIME);
808 Player::draw(DrawingContext& context)
813 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
814 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
815 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
816 float py = Sector::current()->camera->get_translation().y;
817 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
818 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
821 TuxBodyParts* tux_body;
823 if (player_status->bonus == GROWUP_BONUS)
825 else if (player_status->bonus == FIRE_BONUS)
827 else if (player_status->bonus == ICE_BONUS)
830 tux_body = small_tux;
832 int layer = LAYER_OBJECTS + 1;
834 /* Set Tux sprite action */
838 tux_body->set_action("backflip-left");
840 tux_body->set_action("backflip-right");
842 else if (duck && is_big())
845 tux_body->set_action("duck-left");
847 tux_body->set_action("duck-right");
849 else if (skidding_timer.started() && !skidding_timer.check())
852 tux_body->set_action("skid-left");
854 tux_body->set_action("skid-right");
856 else if (kick_timer.started() && !kick_timer.check())
859 tux_body->set_action("kick-left");
861 tux_body->set_action("kick-right");
863 else if (butt_jump && is_big())
866 tux_body->set_action("buttjump-left");
868 tux_body->set_action("buttjump-right");
870 else if (!on_ground())
873 tux_body->set_action("jump-left");
875 tux_body->set_action("jump-right");
879 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
882 tux_body->set_action("stand-left");
884 tux_body->set_action("stand-right");
889 tux_body->set_action("walk-left");
891 tux_body->set_action("walk-right");
895 if(idle_timer.check())
900 tux_body->head->set_action("idle-left", 1);
902 tux_body->head->set_action("idle-right", 1);
907 // Tux is holding something
908 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
909 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
914 tux_body->arms->set_action("duck+grab-left");
916 tux_body->arms->set_action("duck+grab-right");
921 tux_body->arms->set_action("grab-left");
923 tux_body->arms->set_action("grab-right");
929 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
931 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
933 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
934 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
936 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
937 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
940 else if (safe_timer.started() && size_t(game_time*40)%2)
943 tux_body->draw(context, get_pos(), layer);
948 Player::collision_tile(uint32_t tile_attributes)
950 if(tile_attributes & Tile::HURTS)
954 if( tile_attributes & Tile::WATER ){
960 if( tile_attributes & Tile::WATER ){
963 sound_manager->play( "sounds/splash.ogg" );
969 Player::collision_solid(const CollisionHit& hit)
972 if(physic.get_velocity_y() > 0)
973 physic.set_velocity_y(0);
975 on_ground_flag = true;
976 floor_normal = hit.slope_normal;
978 if(physic.get_velocity_y() < 0)
979 physic.set_velocity_y(.2);
982 if(hit.left || hit.right) {
983 physic.set_velocity_x(0);
988 if(hit.left || hit.right) {
990 } else if(hit.top || hit.bottom) {
997 Player::collision(GameObject& other, const CollisionHit& hit)
999 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1004 if(hit.left || hit.right) {
1005 try_grab(); //grab objects right now, in update it will be too late
1008 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1010 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1011 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1012 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1014 if(controller->pressed(Controller::UP))
1015 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1021 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1022 if(badguy != NULL) {
1023 if(safe_timer.started() || invincible_timer.started())
1033 Player::make_invincible()
1035 sound_manager->play("sounds/invincible.wav");
1036 invincible_timer.start(TUX_INVINCIBLE_TIME);
1037 Sector::current()->play_music(HERRING_MUSIC);
1042 Player::kill(bool completely)
1044 if(dying || deactivated)
1047 if(!completely && (safe_timer.started() || invincible_timer.started()))
1050 sound_manager->play("sounds/hurt.wav");
1052 physic.set_velocity_x(0);
1054 if(!completely && (is_big() || growing_timer.started())) {
1055 if(player_status->bonus == FIRE_BONUS
1056 || player_status->bonus == ICE_BONUS) {
1057 safe_timer.start(TUX_SAFE_TIME);
1058 set_bonus(GROWUP_BONUS, true);
1059 } else if(player_status->bonus == GROWUP_BONUS) {
1060 //growing_timer.start(GROWING_TIME);
1061 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1062 adjust_height(30.8);
1064 set_bonus(NO_BONUS, true);
1065 } else if(player_status->bonus == NO_BONUS) {
1066 growing_timer.stop();
1067 safe_timer.start(TUX_SAFE_TIME);
1068 adjust_height(30.8);
1072 for (int i = 0; (i < 5) && (i < player_status->coins); i++)
1074 // the numbers: starting x, starting y, velocity y
1075 Sector::current()->add_object(new FallingCoin(get_pos() +
1076 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1077 systemRandom.rand(-100,100)));
1079 physic.enable_gravity(true);
1080 physic.set_acceleration(0, 0);
1081 physic.set_velocity(0, -700);
1082 player_status->coins -= 25;
1083 set_bonus(NO_BONUS, true);
1085 dying_timer.start(3.0);
1086 set_group(COLGROUP_DISABLED);
1088 DisplayEffect* effect = new DisplayEffect();
1089 effect->fade_out(3.0);
1090 Sector::current()->add_object(effect);
1091 sound_manager->stop_music(3.0);
1096 Player::move(const Vector& vector)
1100 // TODO: do we need the following? Seems irrelevant to moving the player
1102 set_size(31.8, 63.8);
1104 set_size(31.8, 31.8);
1106 last_ground_y = vector.y;
1112 Player::check_bounds(Camera* camera)
1114 /* Keep tux in bounds: */
1115 if (get_pos().x < 0) {
1116 // Lock Tux to the size of the level, so that he doesn't fall of
1118 set_pos(Vector(0, get_pos().y));
1121 /* fallen out of the level? */
1122 if (get_pos().y > Sector::current()->get_height()) {
1127 // can happen if back scrolling is disabled
1128 if(get_pos().x < camera->get_translation().x) {
1129 set_pos(Vector(camera->get_translation().x, get_pos().y));
1131 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1134 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1140 Player::add_velocity(const Vector& velocity)
1142 physic.set_velocity(physic.get_velocity() + velocity);
1146 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1148 if (end_speed.x > 0)
1149 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1150 if (end_speed.x < 0)
1151 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1152 if (end_speed.y > 0)
1153 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1154 if (end_speed.y < 0)
1155 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1159 Player::bounce(BadGuy& )
1161 if(controller->hold(Controller::JUMP))
1162 physic.set_velocity_y(-520);
1164 physic.set_velocity_y(-300);
1167 //Scripting Functions Below
1170 Player::deactivate()
1175 physic.set_velocity_x(0);
1176 physic.set_velocity_y(0);
1177 physic.set_acceleration_x(0);
1178 physic.set_acceleration_y(0);
1186 deactivated = false;
1189 void Player::walk(float speed)
1191 physic.set_velocity_x(speed);
1195 Player::set_ghost_mode(bool enable)
1197 if (ghost_mode == enable)
1202 set_group(COLGROUP_DISABLED);
1203 physic.enable_gravity(false);
1204 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1207 set_group(COLGROUP_MOVING);
1208 physic.enable_gravity(true);
1209 log_debug << "You feel solid again." << std::endl;