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::trigger_sequence(std::string sequence_name)
220 GameSession::current()->start_sequence(sequence_name);
224 Player::update(float elapsed_time)
231 if(dying && dying_timer.check()) {
236 if(!dying && !deactivated)
239 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
243 // extend/shrink tux collision rectangle so that we fall through/walk over 1
245 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
251 // on downward slopes, adjust vertical velocity so tux walks smoothly down
253 if(floor_normal.y != 0) {
254 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
255 physic.set_velocity_y(250);
260 // handle backflipping
262 //prevent player from changing direction when backflipping
263 dir = (backflip_direction == 1) ? LEFT : RIGHT;
264 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
269 fall_mode = ON_GROUND;
270 last_ground_y = get_pos().y;
272 if(get_pos().y > last_ground_y)
274 else if(fall_mode == ON_GROUND)
278 // check if we landed
281 if (backflipping && (!backflip_timer.started())) {
282 backflipping = false;
283 backflip_direction = 0;
285 // if controls are currently deactivated, we take care of standing up ourselves
291 // calculate movement for this frame
292 movement = physic.get_movement(elapsed_time);
294 if(grabbed_object != NULL && !dying) {
295 Vector pos = get_pos() +
296 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
297 grabbed_object->grab(*this, pos, dir);
300 if(grabbed_object != NULL && dying){
301 grabbed_object->ungrab(*this, dir);
302 grabbed_object = NULL;
305 on_ground_flag = false;
307 // when invincible, spawn particles
308 if (invincible_timer.started() && !dying)
310 if (systemRandom.rand(0, 2) == 0) {
311 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
312 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
313 Vector ppos = Vector(px, py);
314 Vector pspeed = Vector(0, 0);
315 Vector paccel = Vector(0, 0);
316 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
317 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
318 // make every other a longer sparkle to make trail a bit fuzzy
319 if (size_t(game_time*20)%2) {
320 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
322 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
325 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
335 return on_ground_flag;
341 if(player_status->bonus == NO_BONUS)
348 Player::apply_friction()
350 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
351 physic.set_velocity_x(0);
352 physic.set_acceleration_x(0);
353 } else if(physic.get_velocity_x() < 0) {
354 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
355 } else if(physic.get_velocity_x() > 0) {
356 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
361 Player::handle_horizontal_input()
363 float vx = physic.get_velocity_x();
364 float vy = physic.get_velocity_y();
365 float ax = physic.get_acceleration_x();
366 float ay = physic.get_acceleration_y();
369 if(!duck || physic.get_velocity_y() != 0) {
370 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
374 } else if(!controller->hold(Controller::LEFT)
375 && controller->hold(Controller::RIGHT)) {
382 // only run if action key is pressed and we're not holding anything
383 if (!(controller->hold(Controller::ACTION) && (!grabbed_object))) {
384 ax = dirsign * WALK_ACCELERATION_X;
386 if(vx >= MAX_WALK_XM && dirsign > 0) {
389 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
394 ax = dirsign * RUN_ACCELERATION_X;
396 if(vx >= MAX_RUN_XM && dirsign > 0) {
399 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
405 // we can reach WALK_SPEED without any acceleration
406 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
407 vx = dirsign * WALK_SPEED;
410 // changing directions?
411 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
413 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
414 skidding_timer.start(SKID_TIME);
415 sound_manager->play("sounds/skid.wav");
416 // dust some particles
417 Sector::current()->add_object(
419 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
420 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
421 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
430 physic.set_velocity(vx, vy);
431 physic.set_acceleration(ax, ay);
433 // we get slower when not pressing any keys
455 if (physic.get_velocity_y() != 0)
460 if (adjust_height(31.8)) {
462 unduck_hurt_timer.stop();
469 Player::do_standup() {
477 if (adjust_height(63.8)) {
479 unduck_hurt_timer.stop();
481 // if timer is not already running, start it.
482 if (unduck_hurt_timer.get_period() == 0) {
483 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
485 else if (unduck_hurt_timer.check()) {
493 Player::do_backflip() {
499 // TODO: we don't have an animation for firetux backflipping, so let's revert to bigtux
500 set_bonus(GROWUP_BONUS, true);
502 backflip_direction = (dir == LEFT)?(+1):(-1);
505 sound_manager->play("sounds/flip.wav");
506 backflip_timer.start(0.15);
510 Player::do_jump(float yspeed) {
514 physic.set_velocity_y(yspeed);
515 //bbox.move(Vector(0, -1));
517 on_ground_flag = false;
522 sound_manager->play("sounds/bigjump.wav");
524 sound_manager->play("sounds/jump.wav");
529 Player::handle_vertical_input()
532 if(controller->pressed(Controller::JUMP) && (can_jump)) {
534 // when running, only jump a little bit; else do a backflip
535 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
537 // jump a bit higher if we are running; else do a normal jump
538 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
541 // Let go of jump key
542 else if(!controller->hold(Controller::JUMP)) {
543 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
545 physic.set_velocity_y(0);
549 /* In case the player has pressed Down while in a certain range of air,
550 enable butt jump action */
551 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
555 /* When Down is not held anymore, disable butt jump */
556 if(butt_jump && !controller->hold(Controller::DOWN))
560 physic.set_acceleration_y(0);
562 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
563 physic.set_acceleration_y(-2000);
564 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
569 Player::handle_input()
572 handle_input_ghost();
577 if( controller->released( Controller::PEEK_LEFT ) ) {
580 if( controller->released( Controller::PEEK_RIGHT ) ) {
583 if( controller->pressed( Controller::PEEK_LEFT ) ) {
586 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
590 /* Handle horizontal movement: */
591 if (!backflipping) handle_horizontal_input();
594 if (on_ground() && !controller->hold(Controller::JUMP))
597 /* Handle vertical movement: */
598 handle_vertical_input();
601 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
602 if(Sector::current()->add_bullet(
603 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
604 : Vector(32, bbox.get_height()/2)),
605 physic.get_velocity_x(), dir))
606 shooting_timer.start(SHOOTING_TIME);
609 /* Duck or Standup! */
610 if (controller->hold(Controller::DOWN)) {
619 if(!controller->hold(Controller::ACTION) && grabbed_object) {
620 // move the grabbed object a bit away from tux
621 Vector pos = get_pos() +
622 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
623 bbox.get_height()*0.66666 - 32);
624 Rect dest(pos, pos + Vector(32, 32));
625 if(Sector::current()->is_free_of_movingstatics(dest)) {
626 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
628 moving_object->set_pos(pos);
630 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
632 grabbed_object->ungrab(*this, dir);
633 grabbed_object = NULL;
641 if(controller->hold(Controller::ACTION) && !grabbed_object
643 Sector* sector = Sector::current();
646 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
648 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
651 for(Sector::Portables::iterator i = sector->portables.begin();
652 i != sector->portables.end(); ++i) {
653 Portable* portable = *i;
654 if(!portable->is_portable())
657 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
659 if(moving_object == NULL)
662 if(moving_object->get_bbox().contains(pos)) {
663 grabbed_object = portable;
664 grabbed_object->grab(*this, get_pos(), dir);
672 Player::handle_input_ghost()
676 if (controller->hold(Controller::LEFT)) {
678 vx -= MAX_RUN_XM * 2;
680 if (controller->hold(Controller::RIGHT)) {
682 vx += MAX_RUN_XM * 2;
684 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
685 vy -= MAX_RUN_XM * 2;
687 if (controller->hold(Controller::DOWN)) {
688 vy += MAX_RUN_XM * 2;
690 if (controller->hold(Controller::ACTION)) {
691 set_ghost_mode(false);
693 physic.set_velocity(vx, vy);
694 physic.set_acceleration(0, 0);
698 Player::add_coins(int count)
700 player_status->add_coins(count);
706 return player_status->coins;
710 Player::add_bonus(const std::string& bonustype)
712 BonusType type = NO_BONUS;
714 if(bonustype == "grow") {
716 } else if(bonustype == "fireflower") {
718 } else if(bonustype == "iceflower") {
720 } else if(bonustype == "none") {
723 std::ostringstream msg;
724 msg << "Unknown bonus type " << bonustype;
725 throw std::runtime_error(msg.str());
728 return add_bonus(type);
732 Player::add_bonus(BonusType type, bool animate)
734 // always ignore NO_BONUS
735 if (type == NO_BONUS) {
739 // ignore GROWUP_BONUS if we're already big
740 if (type == GROWUP_BONUS) {
741 if (player_status->bonus == GROWUP_BONUS)
743 if (player_status->bonus == FIRE_BONUS)
745 if (player_status->bonus == ICE_BONUS)
749 return set_bonus(type, animate);
753 Player::set_bonus(BonusType type, bool animate)
755 if(player_status->bonus == NO_BONUS) {
756 if (!adjust_height(62.8)) {
757 printf("can't adjust\n");
761 growing_timer.start(GROWING_TIME);
764 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
765 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
766 // visually lose helmet
767 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
768 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
769 Vector paccel = Vector(0, 1000);
770 std::string action = (dir==LEFT)?"left":"right";
771 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
773 if ((player_status->bonus == ICE_BONUS) && (animate)) {
775 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
776 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
777 Vector paccel = Vector(0, 1000);
778 std::string action = (dir==LEFT)?"left":"right";
779 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
781 player_status->max_fire_bullets = 0;
782 player_status->max_ice_bullets = 0;
784 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
785 if (type == ICE_BONUS) player_status->max_ice_bullets++;
787 player_status->bonus = type;
792 Player::set_visible(bool visible)
794 this->visible = visible;
796 set_group(COLGROUP_MOVING);
798 set_group(COLGROUP_DISABLED);
802 Player::get_visible()
810 kick_timer.start(KICK_TIME);
814 Player::draw(DrawingContext& context)
819 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
820 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
821 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
822 float py = Sector::current()->camera->get_translation().y;
823 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
824 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
827 TuxBodyParts* tux_body;
829 if (player_status->bonus == GROWUP_BONUS)
831 else if (player_status->bonus == FIRE_BONUS)
833 else if (player_status->bonus == ICE_BONUS)
836 tux_body = small_tux;
838 int layer = LAYER_OBJECTS + 1;
840 /* Set Tux sprite action */
844 tux_body->set_action("backflip-left");
846 tux_body->set_action("backflip-right");
848 else if (duck && is_big())
851 tux_body->set_action("duck-left");
853 tux_body->set_action("duck-right");
855 else if (skidding_timer.started() && !skidding_timer.check())
858 tux_body->set_action("skid-left");
860 tux_body->set_action("skid-right");
862 else if (kick_timer.started() && !kick_timer.check())
865 tux_body->set_action("kick-left");
867 tux_body->set_action("kick-right");
869 else if (butt_jump && is_big())
872 tux_body->set_action("buttjump-left");
874 tux_body->set_action("buttjump-right");
876 else if (!on_ground())
879 tux_body->set_action("jump-left");
881 tux_body->set_action("jump-right");
885 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
888 tux_body->set_action("stand-left");
890 tux_body->set_action("stand-right");
895 tux_body->set_action("walk-left");
897 tux_body->set_action("walk-right");
901 if(idle_timer.check())
906 tux_body->head->set_action("idle-left", 1);
908 tux_body->head->set_action("idle-right", 1);
913 // Tux is holding something
914 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
915 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
920 tux_body->arms->set_action("duck+grab-left");
922 tux_body->arms->set_action("duck+grab-right");
927 tux_body->arms->set_action("grab-left");
929 tux_body->arms->set_action("grab-right");
935 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
937 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
939 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
940 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
942 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
943 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
946 else if (safe_timer.started() && size_t(game_time*40)%2)
949 tux_body->draw(context, get_pos(), layer);
954 Player::collision_tile(uint32_t tile_attributes)
956 if(tile_attributes & Tile::HURTS)
960 if( tile_attributes & Tile::WATER ){
966 if( tile_attributes & Tile::WATER ){
969 sound_manager->play( "sounds/splash.ogg" );
975 Player::collision_solid(const CollisionHit& hit)
978 if(physic.get_velocity_y() > 0)
979 physic.set_velocity_y(0);
981 on_ground_flag = true;
982 floor_normal = hit.slope_normal;
984 if(physic.get_velocity_y() < 0)
985 physic.set_velocity_y(.2);
988 if(hit.left || hit.right) {
989 physic.set_velocity_x(0);
994 if(hit.left || hit.right) {
996 } else if(hit.top || hit.bottom) {
1003 Player::collision(GameObject& other, const CollisionHit& hit)
1005 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1010 if(hit.left || hit.right) {
1011 try_grab(); //grab objects right now, in update it will be too late
1014 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1016 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1017 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1018 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1020 if(controller->pressed(Controller::UP))
1021 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1027 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1028 if(badguy != NULL) {
1029 if(safe_timer.started() || invincible_timer.started())
1039 Player::make_invincible()
1041 sound_manager->play("sounds/invincible.wav");
1042 invincible_timer.start(TUX_INVINCIBLE_TIME);
1043 Sector::current()->play_music(HERRING_MUSIC);
1048 Player::kill(bool completely)
1050 if(dying || deactivated)
1053 if(!completely && (safe_timer.started() || invincible_timer.started()))
1056 sound_manager->play("sounds/hurt.wav");
1058 physic.set_velocity_x(0);
1060 if(!completely && (is_big() || growing_timer.started())) {
1061 if(player_status->bonus == FIRE_BONUS
1062 || player_status->bonus == ICE_BONUS) {
1063 safe_timer.start(TUX_SAFE_TIME);
1064 set_bonus(GROWUP_BONUS, true);
1065 } else if(player_status->bonus == GROWUP_BONUS) {
1066 //growing_timer.start(GROWING_TIME);
1067 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1068 adjust_height(30.8);
1070 set_bonus(NO_BONUS, true);
1071 } else if(player_status->bonus == NO_BONUS) {
1072 growing_timer.stop();
1073 safe_timer.start(TUX_SAFE_TIME);
1074 adjust_height(30.8);
1078 for (int i = 0; (i < 5) && (i < player_status->coins); i++)
1080 // the numbers: starting x, starting y, velocity y
1081 Sector::current()->add_object(new FallingCoin(get_pos() +
1082 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1083 systemRandom.rand(-100,100)));
1085 physic.enable_gravity(true);
1086 physic.set_acceleration(0, 0);
1087 physic.set_velocity(0, -700);
1088 player_status->coins -= 25;
1089 set_bonus(NO_BONUS, true);
1091 dying_timer.start(3.0);
1092 set_group(COLGROUP_DISABLED);
1094 DisplayEffect* effect = new DisplayEffect();
1095 effect->fade_out(3.0);
1096 Sector::current()->add_object(effect);
1097 sound_manager->stop_music(3.0);
1102 Player::move(const Vector& vector)
1106 // TODO: do we need the following? Seems irrelevant to moving the player
1108 set_size(31.8, 63.8);
1110 set_size(31.8, 31.8);
1112 last_ground_y = vector.y;
1118 Player::check_bounds(Camera* camera)
1120 /* Keep tux in bounds: */
1121 if (get_pos().x < 0) {
1122 // Lock Tux to the size of the level, so that he doesn't fall of
1124 set_pos(Vector(0, get_pos().y));
1127 /* fallen out of the level? */
1128 if (get_pos().y > Sector::current()->get_height()) {
1133 // can happen if back scrolling is disabled
1134 if(get_pos().x < camera->get_translation().x) {
1135 set_pos(Vector(camera->get_translation().x, get_pos().y));
1137 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1140 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1146 Player::add_velocity(const Vector& velocity)
1148 physic.set_velocity(physic.get_velocity() + velocity);
1152 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1154 if (end_speed.x > 0)
1155 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1156 if (end_speed.x < 0)
1157 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1158 if (end_speed.y > 0)
1159 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1160 if (end_speed.y < 0)
1161 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1165 Player::bounce(BadGuy& )
1167 if(controller->hold(Controller::JUMP))
1168 physic.set_velocity_y(-520);
1170 physic.set_velocity_y(-300);
1173 //Scripting Functions Below
1176 Player::deactivate()
1181 physic.set_velocity_x(0);
1182 physic.set_velocity_y(0);
1183 physic.set_acceleration_x(0);
1184 physic.set_acceleration_y(0);
1192 deactivated = false;
1195 void Player::walk(float speed)
1197 physic.set_velocity_x(speed);
1201 Player::set_ghost_mode(bool enable)
1203 if (ghost_mode == enable)
1208 set_group(COLGROUP_DISABLED);
1209 physic.enable_gravity(false);
1210 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1213 set_group(COLGROUP_MOVING);
1214 physic.enable_gravity(true);
1215 log_debug << "You feel solid again." << std::endl;