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"
52 #include "trigger/climbable.hpp"
54 static const int TILES_FOR_BUTTJUMP = 3;
55 static const float SHOOTING_TIME = .150f;
56 /// time before idle animation starts
57 static const float IDLE_TIME = 2.5f;
59 static const float WALK_ACCELERATION_X = 300;
60 static const float RUN_ACCELERATION_X = 400;
61 static const float SKID_XM = 200;
62 static const float SKID_TIME = .3f;
63 static const float MAX_WALK_XM = 230;
64 static const float MAX_RUN_XM = 320;
65 static const float MAX_CLIMB_XM = 48;
66 static const float MAX_CLIMB_YM = 128;
67 static const float WALK_SPEED = 100;
69 static const float KICK_TIME = .3f;
70 static const float CHEER_TIME = 1.0f;
72 static const float UNDUCK_HURT_TIME = 0.25f; /**< if Tux cannot unduck for this long, he will get hurt */
75 Surface* growingtux_left[GROWING_FRAMES];
76 Surface* growingtux_right[GROWING_FRAMES];
78 Surface* tux_life = 0;
80 TuxBodyParts* small_tux = 0;
81 TuxBodyParts* big_tux = 0;
82 TuxBodyParts* fire_tux = 0;
83 TuxBodyParts* ice_tux = 0;
89 TuxBodyParts::set_action(std::string action, int loops)
92 head->set_action(action, loops);
94 body->set_action(action, loops);
96 arms->set_action(action, loops);
98 feet->set_action(action, loops);
102 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
105 head->draw(context, pos, layer-1);
107 body->draw(context, pos, layer-3);
109 arms->draw(context, pos, layer+10);
111 feet->draw(context, pos, layer-2);
114 Player::Player(PlayerStatus* _player_status, const std::string& name)
115 : player_status(_player_status), grabbed_object(NULL), ghost_mode(false), climbing(0)
118 controller = main_controller;
119 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
120 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
121 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
122 airarrow.reset(new Surface("images/engine/hud/airarrow.png"));
124 sound_manager->preload("sounds/bigjump.wav");
125 sound_manager->preload("sounds/jump.wav");
126 sound_manager->preload("sounds/hurt.wav");
127 sound_manager->preload("sounds/skid.wav");
128 sound_manager->preload("sounds/flip.wav");
129 sound_manager->preload("sounds/invincible.wav");
130 sound_manager->preload("sounds/splash.ogg");
137 if (climbing) stop_climbing(*climbing);
138 delete smalltux_gameover;
139 delete smalltux_star;
147 set_size(31.8f, 62.8f);
149 set_size(31.8f, 30.8f);
159 fall_mode = ON_GROUND;
164 backflipping = false;
165 backflip_direction = 0;
168 speedlimit = 0; //no special limit
170 on_ground_flag = false;
171 grabbed_object = NULL;
179 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
184 Scripting::expose_object(vm, table_idx, dynamic_cast<Scripting::Player *>(this), name, false);
188 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
193 Scripting::unexpose_object(vm, table_idx, name);
197 Player::get_speedlimit()
203 Player::set_speedlimit(float newlimit)
209 Player::set_controller(Controller* controller)
211 this->controller = controller;
215 Player::adjust_height(float new_height)
218 bbox2.move(Vector(0, bbox.get_height() - new_height));
219 bbox2.set_height(new_height);
221 if(new_height > bbox.get_height()) {
222 Rect additional_space = bbox2;
223 additional_space.set_height(new_height - bbox.get_height());
224 if(!Sector::current()->is_free_of_statics(additional_space, this, true))
228 // adjust bbox accordingly
229 // note that we use members of moving_object for this, so we can run this during CD, too
231 set_size(bbox2.get_width(), bbox2.get_height());
236 Player::trigger_sequence(std::string sequence_name)
238 if (climbing) stop_climbing(*climbing);
239 GameSession::current()->start_sequence(sequence_name);
243 Player::update(float elapsed_time)
250 if(dying && dying_timer.check()) {
255 if(!dying && !deactivated)
258 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
262 // extend/shrink tux collision rectangle so that we fall through/walk over 1
264 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
270 // on downward slopes, adjust vertical velocity so tux walks smoothly down
272 if(floor_normal.y != 0) {
273 if ((floor_normal.x * physic.get_velocity_x()) >= 0) {
274 physic.set_velocity_y(250);
279 // handle backflipping
281 //prevent player from changing direction when backflipping
282 dir = (backflip_direction == 1) ? LEFT : RIGHT;
283 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
288 fall_mode = ON_GROUND;
289 last_ground_y = get_pos().y;
291 if(get_pos().y > last_ground_y)
293 else if(fall_mode == ON_GROUND)
297 // check if we landed
300 if (backflipping && (!backflip_timer.started())) {
301 backflipping = false;
302 backflip_direction = 0;
304 // if controls are currently deactivated, we take care of standing up ourselves
310 // calculate movement for this frame
311 movement = physic.get_movement(elapsed_time);
313 if(grabbed_object != NULL && !dying) {
314 Vector pos = get_pos() +
315 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
316 grabbed_object->grab(*this, pos, dir);
319 if(grabbed_object != NULL && dying){
320 grabbed_object->ungrab(*this, dir);
321 grabbed_object = NULL;
324 on_ground_flag = false;
326 // when invincible, spawn particles
327 if (invincible_timer.started() && !dying)
329 if (systemRandom.rand(0, 2) == 0) {
330 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
331 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
332 Vector ppos = Vector(px, py);
333 Vector pspeed = Vector(0, 0);
334 Vector paccel = Vector(0, 0);
335 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
336 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
337 // make every other a longer sparkle to make trail a bit fuzzy
338 if (size_t(game_time*20)%2) {
339 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
341 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
344 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
354 return on_ground_flag;
360 if(player_status->bonus == NO_BONUS)
367 Player::apply_friction()
369 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
370 physic.set_velocity_x(0);
371 physic.set_acceleration_x(0);
372 } else if(physic.get_velocity_x() < 0) {
373 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
374 } else if(physic.get_velocity_x() > 0) {
375 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
380 Player::handle_horizontal_input()
382 float vx = physic.get_velocity_x();
383 float vy = physic.get_velocity_y();
384 float ax = physic.get_acceleration_x();
385 float ay = physic.get_acceleration_y();
388 if(!duck || physic.get_velocity_y() != 0) {
389 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
393 } else if(!controller->hold(Controller::LEFT)
394 && controller->hold(Controller::RIGHT)) {
401 // do not run if action key is pressed or we're holding something
402 // so tux can only walk while shooting
403 if ( controller->hold(Controller::ACTION) || grabbed_object ) {
404 ax = dirsign * WALK_ACCELERATION_X;
406 if(vx >= MAX_WALK_XM && dirsign > 0) {
409 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
414 if( vx * dirsign < MAX_WALK_XM ) {
415 ax = dirsign * WALK_ACCELERATION_X;
417 ax = dirsign * RUN_ACCELERATION_X;
420 if(vx >= MAX_RUN_XM && dirsign > 0) {
423 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
429 // we can reach WALK_SPEED without any acceleration
430 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
431 vx = dirsign * WALK_SPEED;
435 if( speedlimit > 0 && vx * dirsign >= speedlimit ) {
436 vx = dirsign * speedlimit;
440 // changing directions?
441 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
443 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
444 skidding_timer.start(SKID_TIME);
445 sound_manager->play("sounds/skid.wav");
446 // dust some particles
447 Sector::current()->add_object(
449 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
450 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
451 Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
460 physic.set_velocity(vx, vy);
461 physic.set_acceleration(ax, ay);
463 // we get slower when not pressing any keys
485 if (physic.get_velocity_y() != 0)
490 if (adjust_height(31.8f)) {
492 unduck_hurt_timer.stop();
499 Player::do_standup() {
507 if (adjust_height(63.8f)) {
509 unduck_hurt_timer.stop();
511 // if timer is not already running, start it.
512 if (unduck_hurt_timer.get_period() == 0) {
513 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
515 else if (unduck_hurt_timer.check()) {
523 Player::do_backflip() {
529 // TODO: we don't have an animation for firetux backflipping, so let's revert to bigtux
530 set_bonus(GROWUP_BONUS, true);
532 backflip_direction = (dir == LEFT)?(+1):(-1);
535 sound_manager->play("sounds/flip.wav");
536 backflip_timer.start(0.15f);
540 Player::do_jump(float yspeed) {
544 physic.set_velocity_y(yspeed);
545 //bbox.move(Vector(0, -1));
547 on_ground_flag = false;
552 sound_manager->play("sounds/bigjump.wav");
554 sound_manager->play("sounds/jump.wav");
559 Player::handle_vertical_input()
562 if(controller->pressed(Controller::JUMP) && (can_jump)) {
564 // when running, only jump a little bit; else do a backflip
565 if ((physic.get_velocity_x() != 0) || (controller->hold(Controller::LEFT)) || (controller->hold(Controller::RIGHT))) do_jump(-300); else do_backflip();
567 // jump a bit higher if we are running; else do a normal jump
568 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
571 // Let go of jump key
572 else if(!controller->hold(Controller::JUMP)) {
573 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
575 physic.set_velocity_y(0);
579 /* In case the player has pressed Down while in a certain range of air,
580 enable butt jump action */
581 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
585 /* When Down is not held anymore, disable butt jump */
586 if(butt_jump && !controller->hold(Controller::DOWN))
590 physic.set_acceleration_y(0);
592 if (controller->hold(Controller::UP) || controller->hold(Controller::JUMP))
593 physic.set_acceleration_y(-2000);
594 physic.set_velocity_y(physic.get_velocity_y() * 0.94);
599 Player::handle_input()
602 handle_input_ghost();
606 handle_input_climbing();
611 if( controller->released( Controller::PEEK_LEFT ) ) {
614 if( controller->released( Controller::PEEK_RIGHT ) ) {
617 if( controller->pressed( Controller::PEEK_LEFT ) ) {
620 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
624 /* Handle horizontal movement: */
625 if (!backflipping) handle_horizontal_input();
628 if (on_ground() && !controller->hold(Controller::JUMP))
631 /* Handle vertical movement: */
632 handle_vertical_input();
635 if (controller->pressed(Controller::ACTION) && (player_status->bonus == FIRE_BONUS || player_status->bonus == ICE_BONUS)) {
636 if(Sector::current()->add_bullet(
637 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
638 : Vector(32, bbox.get_height()/2)),
639 physic.get_velocity_x(), dir))
640 shooting_timer.start(SHOOTING_TIME);
643 /* Duck or Standup! */
644 if (controller->hold(Controller::DOWN)) {
653 if(!controller->hold(Controller::ACTION) && grabbed_object) {
654 // move the grabbed object a bit away from tux
655 Vector pos = get_pos() +
656 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
657 bbox.get_height()*0.66666 - 32);
658 Rect dest(pos, pos + Vector(32, 32));
659 if(Sector::current()->is_free_of_movingstatics(dest)) {
660 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
662 moving_object->set_pos(pos);
664 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
666 grabbed_object->ungrab(*this, dir);
667 grabbed_object = NULL;
675 if(controller->hold(Controller::ACTION) && !grabbed_object
677 Sector* sector = Sector::current();
680 pos = Vector(bbox.get_left() - 5, bbox.get_bottom() - 16);
682 pos = Vector(bbox.get_right() + 5, bbox.get_bottom() - 16);
685 for(Sector::Portables::iterator i = sector->portables.begin();
686 i != sector->portables.end(); ++i) {
687 Portable* portable = *i;
688 if(!portable->is_portable())
691 MovingObject* moving_object = dynamic_cast<MovingObject*> (portable);
693 if(moving_object == NULL)
696 if(moving_object->get_bbox().contains(pos)) {
697 if (climbing) stop_climbing(*climbing);
698 grabbed_object = portable;
699 grabbed_object->grab(*this, get_pos(), dir);
707 Player::handle_input_ghost()
711 if (controller->hold(Controller::LEFT)) {
713 vx -= MAX_RUN_XM * 2;
715 if (controller->hold(Controller::RIGHT)) {
717 vx += MAX_RUN_XM * 2;
719 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
720 vy -= MAX_RUN_XM * 2;
722 if (controller->hold(Controller::DOWN)) {
723 vy += MAX_RUN_XM * 2;
725 if (controller->hold(Controller::ACTION)) {
726 set_ghost_mode(false);
728 physic.set_velocity(vx, vy);
729 physic.set_acceleration(0, 0);
733 Player::add_coins(int count)
735 player_status->add_coins(count);
741 return player_status->coins;
745 Player::add_bonus(const std::string& bonustype)
747 BonusType type = NO_BONUS;
749 if(bonustype == "grow") {
751 } else if(bonustype == "fireflower") {
753 } else if(bonustype == "iceflower") {
755 } else if(bonustype == "none") {
758 std::ostringstream msg;
759 msg << "Unknown bonus type " << bonustype;
760 throw std::runtime_error(msg.str());
763 return add_bonus(type);
767 Player::add_bonus(BonusType type, bool animate)
769 // always ignore NO_BONUS
770 if (type == NO_BONUS) {
774 // ignore GROWUP_BONUS if we're already big
775 if (type == GROWUP_BONUS) {
776 if (player_status->bonus == GROWUP_BONUS)
778 if (player_status->bonus == FIRE_BONUS)
780 if (player_status->bonus == ICE_BONUS)
784 return set_bonus(type, animate);
788 Player::set_bonus(BonusType type, bool animate)
790 if(player_status->bonus == NO_BONUS) {
791 if (!adjust_height(62.8f)) {
792 printf("can't adjust\n");
796 growing_timer.start(GROWING_TIME);
797 if (climbing) stop_climbing(*climbing);
800 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
801 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
802 // visually lose helmet
803 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
804 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
805 Vector paccel = Vector(0, 1000);
806 std::string action = (dir==LEFT)?"left":"right";
807 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
808 if (climbing) stop_climbing(*climbing);
810 if ((player_status->bonus == ICE_BONUS) && (animate)) {
812 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
813 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
814 Vector paccel = Vector(0, 1000);
815 std::string action = (dir==LEFT)?"left":"right";
816 Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
817 if (climbing) stop_climbing(*climbing);
819 player_status->max_fire_bullets = 0;
820 player_status->max_ice_bullets = 0;
822 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
823 if (type == ICE_BONUS) player_status->max_ice_bullets++;
825 player_status->bonus = type;
830 Player::set_visible(bool visible)
832 this->visible = visible;
834 set_group(COLGROUP_MOVING);
836 set_group(COLGROUP_DISABLED);
840 Player::get_visible()
848 kick_timer.start(KICK_TIME);
852 Player::draw(DrawingContext& context)
857 // if Tux is above camera, draw little "air arrow" to show where he is x-wise
858 if (Sector::current() && Sector::current()->camera && (get_bbox().p2.y - 16 < Sector::current()->camera->get_translation().y)) {
859 float px = get_pos().x + (get_bbox().p2.x - get_bbox().p1.x - airarrow.get()->get_width()) / 2;
860 float py = Sector::current()->camera->get_translation().y;
861 py += std::min(((py - (get_bbox().p2.y + 16)) / 4), 16.0f);
862 context.draw_surface(airarrow.get(), Vector(px, py), LAYER_HUD - 1);
865 TuxBodyParts* tux_body;
867 if (player_status->bonus == GROWUP_BONUS)
869 else if (player_status->bonus == FIRE_BONUS)
871 else if (player_status->bonus == ICE_BONUS)
874 tux_body = small_tux;
876 int layer = LAYER_OBJECTS + 1;
878 /* Set Tux sprite action */
881 tux_body->set_action("skid-left");
883 else if (backflipping)
886 tux_body->set_action("backflip-left");
888 tux_body->set_action("backflip-right");
890 else if (duck && is_big())
893 tux_body->set_action("duck-left");
895 tux_body->set_action("duck-right");
897 else if (skidding_timer.started() && !skidding_timer.check())
900 tux_body->set_action("skid-left");
902 tux_body->set_action("skid-right");
904 else if (kick_timer.started() && !kick_timer.check())
907 tux_body->set_action("kick-left");
909 tux_body->set_action("kick-right");
911 else if (butt_jump && is_big())
914 tux_body->set_action("buttjump-left");
916 tux_body->set_action("buttjump-right");
918 else if (!on_ground())
921 tux_body->set_action("jump-left");
923 tux_body->set_action("jump-right");
927 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
930 tux_body->set_action("stand-left");
932 tux_body->set_action("stand-right");
937 tux_body->set_action("walk-left");
939 tux_body->set_action("walk-right");
943 if(idle_timer.check())
948 tux_body->head->set_action("idle-left", 1);
950 tux_body->head->set_action("idle-right", 1);
955 // Tux is holding something
956 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
957 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
962 tux_body->arms->set_action("duck+grab-left");
964 tux_body->arms->set_action("duck+grab-right");
969 tux_body->arms->set_action("grab-left");
971 tux_body->arms->set_action("grab-right");
977 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
979 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
981 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
982 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
984 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
985 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
988 else if (safe_timer.started() && size_t(game_time*40)%2)
991 tux_body->draw(context, get_pos(), layer);
996 Player::collision_tile(uint32_t tile_attributes)
998 if(tile_attributes & Tile::HURTS)
1002 if( tile_attributes & Tile::WATER ){
1008 if( tile_attributes & Tile::WATER ){
1011 sound_manager->play( "sounds/splash.ogg" );
1017 Player::collision_solid(const CollisionHit& hit)
1020 if(physic.get_velocity_y() > 0)
1021 physic.set_velocity_y(0);
1023 on_ground_flag = true;
1024 floor_normal = hit.slope_normal;
1025 } else if(hit.top) {
1026 if(physic.get_velocity_y() < 0)
1027 physic.set_velocity_y(.2f);
1030 if(hit.left || hit.right) {
1031 physic.set_velocity_x(0);
1036 if(hit.left || hit.right) {
1038 } else if(hit.top || hit.bottom) {
1045 Player::collision(GameObject& other, const CollisionHit& hit)
1047 Bullet* bullet = dynamic_cast<Bullet*> (&other);
1052 if(hit.left || hit.right) {
1053 try_grab(); //grab objects right now, in update it will be too late
1056 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1058 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1059 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1060 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1062 if(controller->pressed(Controller::UP))
1063 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1069 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1070 if(badguy != NULL) {
1071 if(safe_timer.started() || invincible_timer.started())
1081 Player::make_invincible()
1083 sound_manager->play("sounds/invincible.wav");
1084 invincible_timer.start(TUX_INVINCIBLE_TIME);
1085 Sector::current()->play_music(HERRING_MUSIC);
1090 Player::kill(bool completely)
1092 if(dying || deactivated)
1095 if(!completely && (safe_timer.started() || invincible_timer.started()))
1098 sound_manager->play("sounds/hurt.wav");
1099 if (climbing) stop_climbing(*climbing);
1101 physic.set_velocity_x(0);
1103 if(!completely && (is_big() || growing_timer.started())) {
1104 if(player_status->bonus == FIRE_BONUS
1105 || player_status->bonus == ICE_BONUS) {
1106 safe_timer.start(TUX_SAFE_TIME);
1107 set_bonus(GROWUP_BONUS, true);
1108 } else if(player_status->bonus == GROWUP_BONUS) {
1109 //growing_timer.start(GROWING_TIME);
1110 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1111 adjust_height(30.8f);
1113 set_bonus(NO_BONUS, true);
1114 } else if(player_status->bonus == NO_BONUS) {
1115 growing_timer.stop();
1116 safe_timer.start(TUX_SAFE_TIME);
1117 adjust_height(30.8f);
1121 if (player_status->coins >= 25 && !GameSession::current()->get_reset_point_sectorname().empty())
1123 for (int i = 0; i < 5; i++)
1125 // the numbers: starting x, starting y, velocity y
1126 Sector::current()->add_object(new FallingCoin(get_pos() +
1127 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1128 systemRandom.rand(-100,100)));
1130 player_status->coins -= 25;
1134 GameSession::current()->set_reset_point("", Vector());
1136 physic.enable_gravity(true);
1137 physic.set_acceleration(0, 0);
1138 physic.set_velocity(0, -700);
1139 set_bonus(NO_BONUS, true);
1141 dying_timer.start(3.0);
1142 set_group(COLGROUP_DISABLED);
1144 DisplayEffect* effect = new DisplayEffect();
1145 effect->fade_out(3.0);
1146 Sector::current()->add_object(effect);
1147 sound_manager->stop_music(3.0);
1152 Player::move(const Vector& vector)
1156 // TODO: do we need the following? Seems irrelevant to moving the player
1158 set_size(31.8f, 63.8f);
1160 set_size(31.8f, 31.8f);
1162 last_ground_y = vector.y;
1163 if (climbing) stop_climbing(*climbing);
1169 Player::check_bounds(Camera* camera)
1171 /* Keep tux in bounds: */
1172 if (get_pos().x < 0) {
1173 // Lock Tux to the size of the level, so that he doesn't fall of
1175 set_pos(Vector(0, get_pos().y));
1178 /* fallen out of the level? */
1179 if (get_pos().y > Sector::current()->get_height()) {
1184 // can happen if back scrolling is disabled
1185 if(get_pos().x < camera->get_translation().x) {
1186 set_pos(Vector(camera->get_translation().x, get_pos().y));
1188 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1191 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1197 Player::add_velocity(const Vector& velocity)
1199 physic.set_velocity(physic.get_velocity() + velocity);
1203 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1205 if (end_speed.x > 0)
1206 physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1207 if (end_speed.x < 0)
1208 physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1209 if (end_speed.y > 0)
1210 physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1211 if (end_speed.y < 0)
1212 physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1216 Player::bounce(BadGuy& )
1218 if(controller->hold(Controller::JUMP))
1219 physic.set_velocity_y(-520);
1221 physic.set_velocity_y(-300);
1224 //Scripting Functions Below
1227 Player::deactivate()
1232 physic.set_velocity_x(0);
1233 physic.set_velocity_y(0);
1234 physic.set_acceleration_x(0);
1235 physic.set_acceleration_y(0);
1236 if (climbing) stop_climbing(*climbing);
1244 deactivated = false;
1247 void Player::walk(float speed)
1249 physic.set_velocity_x(speed);
1253 Player::set_ghost_mode(bool enable)
1255 if (ghost_mode == enable)
1258 if (climbing) stop_climbing(*climbing);
1262 set_group(COLGROUP_DISABLED);
1263 physic.enable_gravity(false);
1264 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1267 set_group(COLGROUP_MOVING);
1268 physic.enable_gravity(true);
1269 log_debug << "You feel solid again." << std::endl;
1275 Player::start_climbing(Climbable& climbable)
1277 if (climbing == &climbable) return;
1279 climbing = &climbable;
1280 physic.enable_gravity(false);
1281 physic.set_velocity(0, 0);
1282 physic.set_acceleration(0, 0);
1286 Player::stop_climbing(Climbable& /*climbable*/)
1288 if (!climbing) return;
1292 if (grabbed_object) {
1293 grabbed_object->ungrab(*this, dir);
1294 grabbed_object = NULL;
1297 physic.enable_gravity(true);
1298 physic.set_velocity(0, 0);
1299 physic.set_acceleration(0, 0);
1301 if ((controller->hold(Controller::JUMP)) || (controller->hold(Controller::UP))) {
1302 on_ground_flag = true;
1303 // TODO: This won't help. Why?
1309 Player::handle_input_climbing()
1312 log_warning << "handle_input_climbing called with climbing set to 0. Input handling skipped" << std::endl;
1318 if (controller->hold(Controller::LEFT)) {
1322 if (controller->hold(Controller::RIGHT)) {
1326 if (controller->hold(Controller::UP)) {
1329 if (controller->hold(Controller::DOWN)) {
1332 if (controller->hold(Controller::JUMP)) {
1334 stop_climbing(*climbing);
1340 if (controller->hold(Controller::ACTION)) {
1341 stop_climbing(*climbing);
1344 physic.set_velocity(vx, vy);
1345 physic.set_acceleration(0, 0);