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;
83 TuxBodyParts::set_action(std::string action, int loops)
86 head->set_action(action, loops);
88 body->set_action(action, loops);
90 arms->set_action(action, loops);
92 feet->set_action(action, loops);
96 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
99 head->draw(context, pos, layer-1);
101 body->draw(context, pos, layer-3);
103 arms->draw(context, pos, layer+10);
105 feet->draw(context, pos, layer-2);
108 Player::Player(PlayerStatus* _player_status)
109 : player_status(_player_status), grabbed_object(NULL), ghost_mode(false)
111 controller = main_controller;
112 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
113 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
114 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
116 sound_manager->preload("sounds/bigjump.wav");
117 sound_manager->preload("sounds/jump.wav");
118 sound_manager->preload("sounds/hurt.wav");
119 sound_manager->preload("sounds/skid.wav");
120 sound_manager->preload("sounds/flip.wav");
121 sound_manager->preload("sounds/invincible.wav");
128 delete smalltux_gameover;
129 delete smalltux_star;
137 set_size(31.8, 62.8);
139 set_size(31.8, 30.8);
148 fall_mode = ON_GROUND;
153 backflipping = false;
154 backflip_direction = 0;
157 on_ground_flag = false;
158 grabbed_object = NULL;
160 floor_normal = Vector(0,-1);
166 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
168 Scripting::Player* interface = static_cast<Scripting::Player*> (this);
169 Scripting::expose_object(vm, table_idx, interface, "Tux", false);
173 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
175 Scripting::unexpose_object(vm, table_idx, "Tux");
179 Player::set_controller(Controller* controller)
181 this->controller = controller;
185 Player::adjust_height(float new_height)
188 bbox2.move(Vector(0, bbox.get_height() - new_height));
189 bbox2.set_height(new_height);
190 if (!Sector::current()->is_free_space(bbox2)) return false;
191 // adjust bbox accordingly
192 // note that we use members of moving_object for this, so we can run this during CD, too
194 set_size(bbox2.get_width(), bbox2.get_height());
199 Player::update(float elapsed_time)
201 if(dying && dying_timer.check()) {
206 if(!dying && !deactivated)
209 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
210 if (deactivated) apply_friction();
212 // extend/shrink tux collision rectangle so that we fall through/walk over 1
214 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
220 // on downward slopes, adjust vertical velocity to match slope angle
222 if (floor_normal.y != 0) {
223 if ((floor_normal.x * physic.get_velocity_x()) > 0) {
224 // we overdo it a little, just to be on the safe side
225 physic.set_velocity_y(-physic.get_velocity_x() * (floor_normal.x / floor_normal.y) * 2);
230 // handle backflipping
232 //prevent player from changing direction when backflipping
233 dir = (backflip_direction == 1) ? LEFT : RIGHT;
234 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
239 fall_mode = ON_GROUND;
240 last_ground_y = get_pos().y;
242 if(get_pos().y > last_ground_y)
244 else if(fall_mode == ON_GROUND)
248 // check if we landed
251 if (backflipping && (!backflip_timer.started())) {
252 backflipping = false;
253 backflip_direction = 0;
255 // if controls are currently deactivated, we take care of standing up ourselves
256 if (deactivated) do_standup();
262 if (butt_jump && on_ground() && is_big()) {
265 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
267 Sector::current()->add_smoke_cloud(
268 Vector(get_pos().x - 32, get_pos().y + 32));
272 // Break bricks beneath Tux
273 if(Sector::current()->trybreakbrick(
274 Vector(base.x + 1, base.y + base.height), false)
275 || Sector::current()->trybreakbrick(
276 Vector(base.x + base.width - 1, base.y + base.height), false)) {
277 physic.set_velocity_y(-2);
281 // Kill nearby badguys
282 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
283 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
284 i != gameobjects.end();
286 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
288 // don't kill when badguys are already dying or in a certain mode
289 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
290 badguy->mode != BadGuy::BOMB_EXPLODE) {
291 if (fabsf(base.x - badguy->base.x) < 96 &&
292 fabsf(base.y - badguy->base.y) < 64)
300 // calculate movement for this frame
301 movement = physic.get_movement(elapsed_time);
303 if(grabbed_object != NULL && !dying) {
304 Vector pos = get_pos() +
305 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
306 grabbed_object->grab(*this, pos, dir);
309 if(grabbed_object != NULL && dying){
310 grabbed_object->ungrab(*this, dir);
311 grabbed_object = NULL;
314 on_ground_flag = false;
316 // when invincible, spawn particles
317 if (invincible_timer.started() &&
318 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
319 || size_t(game_time*20)%2)
322 if (systemRandom.rand(0, 2) == 0) {
323 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
324 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
325 Vector ppos = Vector(px, py);
326 Vector pspeed = Vector(0, 0);
327 Vector paccel = Vector(0, 0);
328 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
337 return on_ground_flag;
343 if(player_status->bonus == NO_BONUS)
350 Player::apply_friction()
352 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
353 physic.set_velocity_x(0);
354 physic.set_acceleration_x(0);
355 } else if(physic.get_velocity_x() < 0) {
356 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
358 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
362 // if we're on ice slow down acceleration or deceleration
363 if (isice(base.x, base.y + base.height))
365 /* the acceleration/deceleration rate on ice is inversely proportional to
366 * the current velocity.
369 // increasing 1 will increase acceleration/deceleration rate
370 // decreasing 1 will decrease acceleration/deceleration rate
371 // must stay above zero, though
372 if (ax != 0) ax *= 1 / fabs(vx);
379 Player::handle_horizontal_input()
381 float vx = physic.get_velocity_x();
382 float vy = physic.get_velocity_y();
383 float ax = physic.get_acceleration_x();
384 float ay = physic.get_acceleration_y();
387 if(!duck || physic.get_velocity_y() != 0) {
388 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
392 } else if(!controller->hold(Controller::LEFT)
393 && controller->hold(Controller::RIGHT)) {
400 if (!controller->hold(Controller::ACTION)) {
401 ax = dirsign * WALK_ACCELERATION_X;
403 if(vx >= MAX_WALK_XM && dirsign > 0) {
406 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
411 ax = dirsign * RUN_ACCELERATION_X;
413 if(vx >= MAX_RUN_XM && dirsign > 0) {
416 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
422 // we can reach WALK_SPEED without any acceleration
423 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
424 vx = dirsign * WALK_SPEED;
427 // changing directions?
428 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
430 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
431 skidding_timer.start(SKID_TIME);
432 sound_manager->play("sounds/skid.wav");
433 // dust some particles
434 Sector::current()->add_object(
436 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
437 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
438 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
447 physic.set_velocity(vx, vy);
448 physic.set_acceleration(ax, ay);
450 // we get slower when not pressing any keys
468 if (!is_big()) return;
470 if (physic.get_velocity_y() != 0) return;
471 if (!on_ground()) return;
473 if (adjust_height(31.8)) {
475 unduck_hurt_timer.stop();
482 Player::do_standup() {
484 if (!is_big()) return;
485 if (backflipping) return;
487 if (adjust_height(63.8)) {
489 unduck_hurt_timer.stop();
491 // if timer is not already running, start it.
492 if (unduck_hurt_timer.get_period() == 0) {
493 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
495 else if (unduck_hurt_timer.check()) {
503 Player::do_backflip() {
505 if (!on_ground()) return;
507 // TODO: we don't have an animation for firetux backflipping, so let's revert to bigtux
508 set_bonus(GROWUP_BONUS, true);
510 backflip_direction = (dir == LEFT)?(+1):(-1);
513 sound_manager->play("sounds/flip.wav");
514 backflip_timer.start(0.15);
518 Player::do_jump(float yspeed) {
519 if (!on_ground()) return;
521 physic.set_velocity_y(yspeed);
522 //bbox.move(Vector(0, -1));
524 on_ground_flag = false;
529 sound_manager->play("sounds/bigjump.wav");
531 sound_manager->play("sounds/jump.wav");
536 Player::handle_vertical_input()
540 if(controller->pressed(Controller::JUMP) && (can_jump)) {
542 // when running, only jump a little bit; else do a backflip
543 if (physic.get_velocity_x() != 0) do_jump(-300); else do_backflip();
545 // jump a bit higher if we are running; else do a normal jump
546 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
549 // Let go of jump key
550 else if(!controller->hold(Controller::JUMP)) {
551 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
553 physic.set_velocity_y(0);
557 /* In case the player has pressed Down while in a certain range of air,
558 enable butt jump action */
559 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
563 /* When Down is not held anymore, disable butt jump */
564 if(butt_jump && !controller->hold(Controller::DOWN)) butt_jump = false;
566 /** jumping is only allowed if we're about to touch ground soon and if the
567 * button has been up in between the last jump
571 if ( (issolid(get_pos().x + bbox.get_width() / 2,
572 get_pos().y + bbox.get_height() + 64) ||
573 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
574 issolid(get_pos().x + bbox.get_width() - 1,
575 get_pos().y + bbox.get_height() + 64))
578 && input.jump && !input.old_jump)
586 Player::handle_input()
589 handle_input_ghost();
593 if(!controller->hold(Controller::ACTION) && grabbed_object) {
594 // move the grabbed object a bit away from tux
595 Vector pos = get_pos() +
596 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
597 bbox.get_height()*0.66666 - 32);
598 Rect dest(pos, pos + Vector(32, 32));
599 if(Sector::current()->is_free_space(dest)) {
600 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
602 moving_object->set_pos(pos);
604 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
606 grabbed_object->ungrab(*this, dir);
607 grabbed_object = NULL;
611 /* Handle horizontal movement: */
612 if (!backflipping) handle_horizontal_input();
615 if (on_ground() && !controller->hold(Controller::JUMP))
618 /* Handle vertical movement: */
619 handle_vertical_input();
622 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
623 if(Sector::current()->add_bullet(
624 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
625 : Vector(32, bbox.get_height()/2)),
626 physic.get_velocity_x(), dir))
627 shooting_timer.start(SHOOTING_TIME);
630 /* Duck or Standup! */
631 if (controller->hold(Controller::DOWN)) do_duck(); else do_standup();
636 Player::handle_input_ghost()
640 if (controller->hold(Controller::LEFT)) {
642 vx -= MAX_RUN_XM * 2;
644 if (controller->hold(Controller::RIGHT)) {
646 vx += MAX_RUN_XM * 2;
648 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
649 vy -= MAX_RUN_XM * 2;
651 if (controller->hold(Controller::DOWN)) {
652 vy += MAX_RUN_XM * 2;
654 if (controller->hold(Controller::ACTION)) {
655 set_ghost_mode(false);
657 physic.set_velocity(vx, vy);
658 physic.set_acceleration(0, 0);
662 Player::add_coins(int count)
664 player_status->add_coins(count);
668 Player::add_bonus(const std::string& bonustype)
670 if(bonustype == "grow") {
671 add_bonus(GROWUP_BONUS);
672 } else if(bonustype == "fireflower") {
673 add_bonus(FIRE_BONUS);
674 } else if(bonustype == "iceflower") {
675 add_bonus(ICE_BONUS);
676 } else if(bonustype == "none") {
679 std::ostringstream msg;
680 msg << "Unknown bonus type " << bonustype;
681 throw std::runtime_error(msg.str());
686 Player::add_bonus(BonusType type, bool animate)
688 // always ignore NO_BONUS
689 if (type == NO_BONUS) {
693 // ignore GROWUP_BONUS if we're already big
694 if (type == GROWUP_BONUS) {
695 if (player_status->bonus == GROWUP_BONUS) return;
696 if (player_status->bonus == FIRE_BONUS) return;
697 if (player_status->bonus == ICE_BONUS) return;
700 set_bonus(type, animate);
704 Player::set_bonus(BonusType type, bool animate)
706 if(player_status->bonus == NO_BONUS) {
707 if (!adjust_height(62.8)) return;
709 growing_timer.start(GROWING_TIME);
712 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
713 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
714 // visually lose helmet
715 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
716 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
717 Vector paccel = Vector(0, 1000);
718 std::string action = (dir==LEFT)?"left":"right";
719 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
721 player_status->max_fire_bullets = 0;
722 player_status->max_ice_bullets = 0;
724 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
725 if (type == ICE_BONUS) player_status->max_ice_bullets++;
727 player_status->bonus = type;
731 Player::set_visible(bool visible)
733 this->visible = visible;
735 set_group(COLGROUP_MOVING);
737 set_group(COLGROUP_DISABLED);
741 Player::get_visible()
749 kick_timer.start(KICK_TIME);
753 Player::draw(DrawingContext& context)
758 TuxBodyParts* tux_body;
760 if (player_status->bonus == GROWUP_BONUS)
762 else if (player_status->bonus == FIRE_BONUS)
764 else if (player_status->bonus == ICE_BONUS)
767 tux_body = small_tux;
769 int layer = LAYER_OBJECTS + 1;
771 /* Set Tux sprite action */
775 tux_body->set_action("backflip-left");
777 tux_body->set_action("backflip-right");
779 else if (duck && is_big())
782 tux_body->set_action("duck-left");
784 tux_body->set_action("duck-right");
786 else if (skidding_timer.started() && !skidding_timer.check())
789 tux_body->set_action("skid-left");
791 tux_body->set_action("skid-right");
793 else if (kick_timer.started() && !kick_timer.check())
796 tux_body->set_action("kick-left");
798 tux_body->set_action("kick-right");
800 else if (butt_jump && is_big())
803 tux_body->set_action("buttjump-left");
805 tux_body->set_action("buttjump-right");
807 else if (!on_ground())
810 tux_body->set_action("jump-left");
812 tux_body->set_action("jump-right");
816 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
819 tux_body->set_action("stand-left");
821 tux_body->set_action("stand-right");
826 tux_body->set_action("walk-left");
828 tux_body->set_action("walk-right");
832 if(idle_timer.check())
837 tux_body->head->set_action("idle-left", 1);
839 tux_body->head->set_action("idle-right", 1);
844 // Tux is holding something
845 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
846 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
851 tux_body->arms->set_action("duck+grab-left");
853 tux_body->arms->set_action("duck+grab-right");
858 tux_body->arms->set_action("grab-left");
860 tux_body->arms->set_action("grab-right");
866 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
868 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
870 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
871 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
873 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
874 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
877 else if (safe_timer.started() && size_t(game_time*40)%2)
880 tux_body->draw(context, get_pos(), layer);
885 Player::collision_tile(uint32_t tile_attributes)
887 if(tile_attributes & Tile::HURTS)
892 Player::collision(GameObject& other, const CollisionHit& hit)
894 Bullet* bullet = dynamic_cast<Bullet*> (&other);
899 if(other.get_flags() & FLAG_PORTABLE) {
900 Portable* portable = dynamic_cast<Portable*> (&other);
901 assert(portable != NULL);
902 if(portable && grabbed_object == NULL
903 && controller->hold(Controller::ACTION)
904 && fabsf(hit.normal.x) > .9) {
905 grabbed_object = portable;
906 grabbed_object->grab(*this, get_pos(), dir);
911 if(other.get_flags() & FLAG_SOLID) {
913 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
915 hit.normal.x, hit.normal.y, hit.depth,
916 get_pos().x, get_pos().y,
917 movement.x, movement.y);
920 if(hit.normal.y < 0) { // landed on floor?
921 if(physic.get_velocity_y() > 0)
922 physic.set_velocity_y(0);
924 on_ground_flag = true;
926 // remember normal of this tile
927 if (hit.normal.y > -0.9) {
928 floor_normal.x = hit.normal.x;
929 floor_normal.y = hit.normal.y;
931 // slowly adjust to unisolid tiles.
932 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
933 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
934 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
937 // hack platforms so that we stand normally on them when going down...
938 Platform* platform = dynamic_cast<Platform*> (&other);
939 if(platform != NULL) {
940 if(platform->get_speed().y > 0)
941 physic.set_velocity_y(platform->get_speed().y);
942 //physic.set_velocity_x(platform->get_speed().x);
944 } else if(hit.normal.y > 0) { // bumped against the roof
945 physic.set_velocity_y(-.1);
947 // hack platform so that we are not glued to it from below
948 Platform* platform = dynamic_cast<Platform*> (&other);
949 if(platform != NULL) {
950 physic.set_velocity_y(platform->get_speed().y);
954 if(fabsf(hit.normal.x) > .9) { // hit on the side?
955 physic.set_velocity_x(0);
958 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
960 Vector mov = movement - omov->get_movement();
962 printf("W %p - HITN: %3.1f %3.1f D:%3.1f TM: %3.1f %3.1f TD: %3.1f %3.1f PM: %3.2f %3.1f\n",
964 hit.normal.x, hit.normal.y,
966 movement.x, movement.y,
967 dest.p1.x, dest.p1.y,
968 omov->get_movement().x, omov->get_movement().y);
976 assert(dynamic_cast<MovingObject*> (&other) != NULL);
978 MovingObject* moving_object = static_cast<MovingObject*> (&other);
979 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
980 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
982 if(controller->pressed(Controller::UP))
983 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
989 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
991 if(safe_timer.started() || invincible_timer.started())
1001 Player::make_invincible()
1003 sound_manager->play("sounds/invincible.wav");
1004 invincible_timer.start(TUX_INVINCIBLE_TIME);
1005 Sector::current()->play_music(HERRING_MUSIC);
1010 Player::kill(bool completely)
1012 if(dying || deactivated)
1015 if(!completely && safe_timer.started() || invincible_timer.started())
1018 sound_manager->play("sounds/hurt.wav");
1020 physic.set_velocity_x(0);
1022 if(!completely && is_big()) {
1023 if(player_status->bonus == FIRE_BONUS
1024 || player_status->bonus == ICE_BONUS) {
1025 safe_timer.start(TUX_SAFE_TIME);
1026 set_bonus(GROWUP_BONUS, true);
1028 //growing_timer.start(GROWING_TIME);
1029 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1030 adjust_height(30.8);
1032 set_bonus(NO_BONUS, true);
1035 for (int i = 0; (i < 5) && (i < player_status->coins); i++)
1037 // the numbers: starting x, starting y, velocity y
1038 Sector::current()->add_object(new FallingCoin(get_pos() +
1039 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1040 systemRandom.rand(-100,100)));
1042 physic.enable_gravity(true);
1043 physic.set_acceleration(0, 0);
1044 physic.set_velocity(0, -700);
1045 player_status->coins -= 25;
1046 set_bonus(NO_BONUS, true);
1048 dying_timer.start(3.0);
1049 set_group(COLGROUP_DISABLED);
1051 DisplayEffect* effect = new DisplayEffect();
1052 effect->fade_out(3.0);
1053 Sector::current()->add_object(effect);
1054 sound_manager->stop_music(3.0);
1059 Player::move(const Vector& vector)
1063 // TODO: do we need the following? Seems irrelevant to moving the player
1065 set_size(31.8, 63.8);
1067 set_size(31.8, 31.8);
1069 last_ground_y = vector.y;
1075 Player::check_bounds(Camera* camera)
1077 /* Keep tux in bounds: */
1078 if (get_pos().x < 0) {
1079 // Lock Tux to the size of the level, so that he doesn't fall of
1081 set_pos(Vector(0, get_pos().y));
1084 /* Keep in-bounds, vertically: */
1085 if (get_pos().y > Sector::current()->solids->get_height() * 32) {
1090 bool adjust = false;
1091 // can happen if back scrolling is disabled
1092 if(get_pos().x < camera->get_translation().x) {
1093 set_pos(Vector(camera->get_translation().x, get_pos().y));
1096 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1099 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1108 if(collision_object_map(bbox)) {
1117 Player::add_velocity(const Vector& velocity)
1119 physic.set_velocity(physic.get_velocity() + velocity);
1123 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1125 if (end_speed.x > 0) physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1126 if (end_speed.x < 0) physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1127 if (end_speed.y > 0) physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1128 if (end_speed.y < 0) physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1132 Player::bounce(BadGuy& )
1134 if(controller->hold(Controller::JUMP))
1135 physic.set_velocity_y(-520);
1137 physic.set_velocity_y(-300);
1140 //Scripting Functions Below
1143 Player::deactivate()
1145 if (deactivated) return;
1147 physic.set_velocity_x(0);
1148 physic.set_velocity_y(0);
1149 physic.set_acceleration_x(0);
1150 physic.set_acceleration_y(0);
1156 if (!deactivated) return;
1157 deactivated = false;
1160 void Player::walk(float speed)
1162 physic.set_velocity_x(speed);
1166 Player::set_ghost_mode(bool enable)
1168 if (ghost_mode == enable) return;
1171 set_group(COLGROUP_DISABLED);
1172 physic.enable_gravity(false);
1173 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1176 set_group(COLGROUP_MOVING);
1177 physic.enable_gravity(true);
1178 log_debug << "You feel solid again." << std::endl;