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 = .150f;
55 /// time before idle animation starts
56 static const float IDLE_TIME = 2.5f;
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 = .3f;
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 = .3f;
67 static const float CHEER_TIME = 1.0f;
69 static const float UNDUCK_HURT_TIME = 0.25f; /**< 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.8f, 62.8f);
146 set_size(31.8f, 30.8f);
156 fall_mode = ON_GROUND;
161 backflipping = false;
162 backflip_direction = 0;
165 speedlimit = 0; //no special limit
167 on_ground_flag = false;
168 grabbed_object = NULL;
174 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
179 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
183 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
188 Scripting::unexpose_object(vm, table_idx, name);
192 Player::get_speedlimit()
198 Player::set_speedlimit(float newlimit)
204 Player::set_controller(Controller* controller)
206 this->controller = controller;
210 Player::adjust_height(float new_height)
213 bbox2.move(Vector(0, bbox.get_height() - new_height));
214 bbox2.set_height(new_height);
216 if(new_height > bbox.get_height()) {
217 Rect additional_space = bbox2;
218 additional_space.set_height(new_height - bbox.get_height());
219 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
223 // adjust bbox accordingly
224 // note that we use members of moving_object for this, so we can run this during CD, too
226 set_size(bbox2.get_width(), bbox2.get_height());
231 Player::trigger_sequence(std::string sequence_name)
233 GameSession::current()->start_sequence(sequence_name);
237 Player::update(float elapsed_time)
244 if(dying && dying_timer.check()) {
249 if(!dying && !deactivated)
252 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
256 // extend/shrink tux collision rectangle so that we fall through/walk over 1
258 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
264 // on downward slopes, adjust vertical velocity so tux walks smoothly down
266 if(floor_normal.y != 0) {
267 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
268 physic.set_velocity_y(250);
273 // handle backflipping
275 //prevent player from changing direction when backflipping
276 dir = (backflip_direction == 1) ? LEFT : RIGHT;
277 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
282 fall_mode = ON_GROUND;
283 last_ground_y = get_pos().y;
285 if(get_pos().y > last_ground_y)
287 else if(fall_mode == ON_GROUND)
291 // check if we landed
294 if (backflipping && (!backflip_timer.started())) {
295 backflipping = false;
296 backflip_direction = 0;
298 // if controls are currently deactivated, we take care of standing up ourselves
304 // calculate movement for this frame
305 movement = physic.get_movement(elapsed_time);
307 if(grabbed_object != NULL && !dying) {
308 Vector pos = get_pos() +
309 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
310 grabbed_object->grab(*this, pos, dir);
313 if(grabbed_object != NULL && dying){
314 grabbed_object->ungrab(*this, dir);
315 grabbed_object = NULL;
318 on_ground_flag = false;
320 // when invincible, spawn particles
321 if (invincible_timer.started() && !dying)
323 if (systemRandom.rand(0, 2) == 0) {
324 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
325 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
326 Vector ppos = Vector(px, py);
327 Vector pspeed = Vector(0, 0);
328 Vector paccel = Vector(0, 0);
329 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
330 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
331 // make every other a longer sparkle to make trail a bit fuzzy
332 if (size_t(game_time*20)%2) {
333 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
335 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
338 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
348 return on_ground_flag;
354 if(player_status->bonus == NO_BONUS)
361 Player::apply_friction()
363 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
364 physic.set_velocity_x(0);
365 physic.set_acceleration_x(0);
366 } else if(physic.get_velocity_x() < 0) {
367 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
368 } else if(physic.get_velocity_x() > 0) {
369 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
374 Player::handle_horizontal_input()
376 float vx = physic.get_velocity_x();
377 float vy = physic.get_velocity_y();
378 float ax = physic.get_acceleration_x();
379 float ay = physic.get_acceleration_y();
382 if(!duck || physic.get_velocity_y() != 0) {
383 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
387 } else if(!controller->hold(Controller::LEFT)
388 && controller->hold(Controller::RIGHT)) {
395 // do not run if action key is pressed or we're holding something
396 // so tux can only walk while shooting
397 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
398 ax = dirsign * WALK_ACCELERATION_X;
400 if(vx >= MAX_WALK_XM && dirsign > 0) {
403 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
408 if( vx * dirsign < MAX_WALK_XM ) {
409 ax = dirsign * WALK_ACCELERATION_X;
411 ax = dirsign * RUN_ACCELERATION_X;
414 if(vx >= MAX_RUN_XM && dirsign > 0) {
417 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
423 // we can reach WALK_SPEED without any acceleration
424 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
425 vx = dirsign * WALK_SPEED;
429 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
430 vx = dirsign * speedlimit;
434 // changing directions?
435 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
437 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
438 skidding_timer.start(SKID_TIME);
439 sound_manager->play("sounds/skid.wav");
440 // dust some particles
441 Sector::current()->add_object(
443 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
444 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
445 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
454 physic.set_velocity(vx, vy);
455 physic.set_acceleration(ax, ay);
457 // we get slower when not pressing any keys
479 if (physic.get_velocity_y() != 0)
484 if (adjust_height(31.8f)) {
486 unduck_hurt_timer.stop();
493 Player::do_standup() {
501 if (adjust_height(63.8f)) {
503 unduck_hurt_timer.stop();
505 // if timer is not already running, start it.
506 if (unduck_hurt_timer.get_period() == 0) {
507 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
509 else if (unduck_hurt_timer.check()) {
517 Player::do_backflip() {
523 // TODO: we don't have an animation for firetux backflipping, so let's revert to bigtux
524 set_bonus(GROWUP_BONUS, true);
526 backflip_direction = (dir == LEFT)?(+1):(-1);
529 sound_manager->play("sounds/flip.wav");
530 backflip_timer.start(0.15f);
534 Player::do_jump(float yspeed) {
538 physic.set_velocity_y(yspeed);
539 //bbox.move(Vector(0, -1));
541 on_ground_flag = false;
546 sound_manager->play("sounds/bigjump.wav");
548 sound_manager->play("sounds/jump.wav");
553 Player::handle_vertical_input()
556 if(controller->pressed(Controller::JUMP) && (can_jump)) {
558 // when running, only jump a little bit; else do a backflip
559 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
561 // jump a bit higher if we are running; else do a normal jump
562 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
565 // Let go of jump key
566 else if(!controller->hold(Controller::JUMP)) {
567 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
569 physic.set_velocity_y(0);
573 /* In case the player has pressed Down while in a certain range of air,
574 enable butt jump action */
575 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
579 /* When Down is not held anymore, disable butt jump */
580 if(butt_jump && !controller->hold(Controller::DOWN))
584 physic.set_acceleration_y(0);
586 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
587 physic.set_acceleration_y(-2000);
588 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
593 Player::handle_input()
596 handle_input_ghost();
601 if( controller->released( Controller::PEEK_LEFT ) ) {
604 if( controller->released( Controller::PEEK_RIGHT ) ) {
607 if( controller->pressed( Controller::PEEK_LEFT ) ) {
610 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
614 /* Handle horizontal movement: */
615 if (!backflipping) handle_horizontal_input();
618 if (on_ground() && !controller->hold(Controller::JUMP))
621 /* Handle vertical movement: */
622 handle_vertical_input();
625 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
626 if(Sector::current()->add_bullet(
627 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
628 : Vector(32, bbox.get_height()/2)),
629 physic.get_velocity_x(), dir))
630 shooting_timer.start(SHOOTING_TIME);
633 /* Duck or Standup! */
634 if (controller->hold(Controller::DOWN)) {
643 if(!controller->hold(Controller::ACTION) && grabbed_object) {
644 // move the grabbed object a bit away from tux
645 Vector pos = get_pos() +
646 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
647 bbox.get_height()*0.66666 - 32);
648 Rect dest(pos, pos + Vector(32, 32));
649 if(Sector::current()->is_free_of_movingstatics(dest)) {
650 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
652 moving_object->set_pos(pos);
654 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
656 grabbed_object->ungrab(*this, dir);
657 grabbed_object = NULL;
665 if(controller->hold(Controller::ACTION) && !grabbed_object
667 Sector* sector = Sector::current();
670 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
672 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
675 for(Sector::Portables::iterator i = sector->portables.begin();
676 i != sector->portables.end(); ++i) {
677 Portable* portable = *i;
678 if(!portable->is_portable())
681 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
683 if(moving_object == NULL)
686 if(moving_object->get_bbox().contains(pos)) {
687 grabbed_object = portable;
688 grabbed_object->grab(*this, get_pos(), dir);
696 Player::handle_input_ghost()
700 if (controller->hold(Controller::LEFT)) {
702 vx -= MAX_RUN_XM * 2;
704 if (controller->hold(Controller::RIGHT)) {
706 vx += MAX_RUN_XM * 2;
708 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
709 vy -= MAX_RUN_XM * 2;
711 if (controller->hold(Controller::DOWN)) {
712 vy += MAX_RUN_XM * 2;
714 if (controller->hold(Controller::ACTION)) {
715 set_ghost_mode(false);
717 physic.set_velocity(vx, vy);
718 physic.set_acceleration(0, 0);
722 Player::add_coins(int count)
724 player_status->add_coins(count);
730 return player_status->coins;
734 Player::add_bonus(const std::string& bonustype)
736 BonusType type = NO_BONUS;
738 if(bonustype == "grow") {
740 } else if(bonustype == "fireflower") {
742 } else if(bonustype == "iceflower") {
744 } else if(bonustype == "none") {
747 std::ostringstream msg;
748 msg << "Unknown bonus type " << bonustype;
749 throw std::runtime_error(msg.str());
752 return add_bonus(type);
756 Player::add_bonus(BonusType type, bool animate)
758 // always ignore NO_BONUS
759 if (type == NO_BONUS) {
763 // ignore GROWUP_BONUS if we're already big
764 if (type == GROWUP_BONUS) {
765 if (player_status->bonus == GROWUP_BONUS)
767 if (player_status->bonus == FIRE_BONUS)
769 if (player_status->bonus == ICE_BONUS)
773 return set_bonus(type, animate);
777 Player::set_bonus(BonusType type, bool animate)
779 if(player_status->bonus == NO_BONUS) {
780 if (!adjust_height(62.8f)) {
781 printf("can't adjust\n");
785 growing_timer.start(GROWING_TIME);
788 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
789 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
790 // visually lose helmet
791 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
792 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
793 Vector paccel = Vector(0, 1000);
794 std::string action = (dir==LEFT)?"left":"right";
795 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
797 if ((player_status->bonus == ICE_BONUS) && (animate)) {
799 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
800 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
801 Vector paccel = Vector(0, 1000);
802 std::string action = (dir==LEFT)?"left":"right";
803 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
805 player_status->max_fire_bullets = 0;
806 player_status->max_ice_bullets = 0;
808 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
809 if (type == ICE_BONUS) player_status->max_ice_bullets++;
811 player_status->bonus = type;
816 Player::set_visible(bool visible)
818 this->visible = visible;
820 set_group(COLGROUP_MOVING);
822 set_group(COLGROUP_DISABLED);
826 Player::get_visible()
834 kick_timer.start(KICK_TIME);
838 Player::draw(DrawingContext& context)
843 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
844 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
845 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
846 float py = Sector::current()->camera->get_translation().y;
847 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
848 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
851 TuxBodyParts* tux_body;
853 if (player_status->bonus == GROWUP_BONUS)
855 else if (player_status->bonus == FIRE_BONUS)
857 else if (player_status->bonus == ICE_BONUS)
860 tux_body = small_tux;
862 int layer = LAYER_OBJECTS + 1;
864 /* Set Tux sprite action */
868 tux_body->set_action("backflip-left");
870 tux_body->set_action("backflip-right");
872 else if (duck && is_big())
875 tux_body->set_action("duck-left");
877 tux_body->set_action("duck-right");
879 else if (skidding_timer.started() && !skidding_timer.check())
882 tux_body->set_action("skid-left");
884 tux_body->set_action("skid-right");
886 else if (kick_timer.started() && !kick_timer.check())
889 tux_body->set_action("kick-left");
891 tux_body->set_action("kick-right");
893 else if (butt_jump && is_big())
896 tux_body->set_action("buttjump-left");
898 tux_body->set_action("buttjump-right");
900 else if (!on_ground())
903 tux_body->set_action("jump-left");
905 tux_body->set_action("jump-right");
909 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
912 tux_body->set_action("stand-left");
914 tux_body->set_action("stand-right");
919 tux_body->set_action("walk-left");
921 tux_body->set_action("walk-right");
925 if(idle_timer.check())
930 tux_body->head->set_action("idle-left", 1);
932 tux_body->head->set_action("idle-right", 1);
937 // Tux is holding something
938 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
939 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
944 tux_body->arms->set_action("duck+grab-left");
946 tux_body->arms->set_action("duck+grab-right");
951 tux_body->arms->set_action("grab-left");
953 tux_body->arms->set_action("grab-right");
959 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
961 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
963 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
964 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
966 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
967 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
970 else if (safe_timer.started() && size_t(game_time*40)%2)
973 tux_body->draw(context, get_pos(), layer);
978 Player::collision_tile(uint32_t tile_attributes)
980 if(tile_attributes & Tile::HURTS)
984 if( tile_attributes & Tile::WATER ){
990 if( tile_attributes & Tile::WATER ){
993 sound_manager->play( "sounds/splash.ogg" );
999 Player::collision_solid(const CollisionHit& hit)
1002 if(physic.get_velocity_y() > 0)
1003 physic.set_velocity_y(0);
1005 on_ground_flag = true;
1006 floor_normal = hit.slope_normal;
1007 } else if(hit.top) {
1008 if(physic.get_velocity_y() < 0)
1009 physic.set_velocity_y(.2f);
1012 if(hit.left || hit.right) {
1013 physic.set_velocity_x(0);
1018 if(hit.left || hit.right) {
1020 } else if(hit.top || hit.bottom) {
1027 Player::collision(GameObject& other, const CollisionHit& hit)
1029 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1034 if(hit.left || hit.right) {
1035 try_grab(); //grab objects right now, in update it will be too late
1038 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1040 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1041 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1042 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1044 if(controller->pressed(Controller::UP))
1045 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1051 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1052 if(badguy != NULL) {
1053 if(safe_timer.started() || invincible_timer.started())
1063 Player::make_invincible()
1065 sound_manager->play("sounds/invincible.wav");
1066 invincible_timer.start(TUX_INVINCIBLE_TIME);
1067 Sector::current()->play_music(HERRING_MUSIC);
1072 Player::kill(bool completely)
1074 if(dying || deactivated)
1077 if(!completely && (safe_timer.started() || invincible_timer.started()))
1080 sound_manager->play("sounds/hurt.wav");
1082 physic.set_velocity_x(0);
1084 if(!completely && (is_big() || growing_timer.started())) {
1085 if(player_status->bonus == FIRE_BONUS
1086 || player_status->bonus == ICE_BONUS) {
1087 safe_timer.start(TUX_SAFE_TIME);
1088 set_bonus(GROWUP_BONUS, true);
1089 } else if(player_status->bonus == GROWUP_BONUS) {
1090 //growing_timer.start(GROWING_TIME);
1091 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1092 adjust_height(30.8f);
1094 set_bonus(NO_BONUS, true);
1095 } else if(player_status->bonus == NO_BONUS) {
1096 growing_timer.stop();
1097 safe_timer.start(TUX_SAFE_TIME);
1098 adjust_height(30.8f);
1102 for (int i = 0; (i < 5) && (i < player_status->coins); i++)
1104 // the numbers: starting x, starting y, velocity y
1105 Sector::current()->add_object(new FallingCoin(get_pos() +
1106 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1107 systemRandom.rand(-100,100)));
1109 physic.enable_gravity(true);
1110 physic.set_acceleration(0, 0);
1111 physic.set_velocity(0, -700);
1112 player_status->coins -= 25;
1113 set_bonus(NO_BONUS, true);
1115 dying_timer.start(3.0);
1116 set_group(COLGROUP_DISABLED);
1118 DisplayEffect* effect = new DisplayEffect();
1119 effect->fade_out(3.0);
1120 Sector::current()->add_object(effect);
1121 sound_manager->stop_music(3.0);
1126 Player::move(const Vector& vector)
1130 // TODO: do we need the following? Seems irrelevant to moving the player
1132 set_size(31.8f, 63.8f);
1134 set_size(31.8f, 31.8f);
1136 last_ground_y = vector.y;
1142 Player::check_bounds(Camera* camera)
1144 /* Keep tux in bounds: */
1145 if (get_pos().x < 0) {
1146 // Lock Tux to the size of the level, so that he doesn't fall of
1148 set_pos(Vector(0, get_pos().y));
1151 /* fallen out of the level? */
1152 if (get_pos().y > Sector::current()->get_height()) {
1157 // can happen if back scrolling is disabled
1158 if(get_pos().x < camera->get_translation().x) {
1159 set_pos(Vector(camera->get_translation().x, get_pos().y));
1161 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1164 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1170 Player::add_velocity(const Vector& velocity)
1172 physic.set_velocity(physic.get_velocity() + velocity);
1176 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1178 if (end_speed.x > 0)
1179 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1180 if (end_speed.x < 0)
1181 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1182 if (end_speed.y > 0)
1183 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1184 if (end_speed.y < 0)
1185 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1189 Player::bounce(BadGuy& )
1191 if(controller->hold(Controller::JUMP))
1192 physic.set_velocity_y(-520);
1194 physic.set_velocity_y(-300);
1197 //Scripting Functions Below
1200 Player::deactivate()
1205 physic.set_velocity_x(0);
1206 physic.set_velocity_y(0);
1207 physic.set_acceleration_x(0);
1208 physic.set_acceleration_y(0);
1216 deactivated = false;
1219 void Player::walk(float speed)
1221 physic.set_velocity_x(speed);
1225 Player::set_ghost_mode(bool enable)
1227 if (ghost_mode == enable)
1232 set_group(COLGROUP_DISABLED);
1233 physic.enable_gravity(false);
1234 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1237 set_group(COLGROUP_MOVING);
1238 physic.enable_gravity(true);
1239 log_debug << "You feel solid again." << std::endl;