3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.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 "video/screen.hpp"
35 #include "statistics.hpp"
36 #include "game_session.hpp"
37 #include "object/tilemap.hpp"
38 #include "object/camera.hpp"
39 #include "object/particles.hpp"
40 #include "object/portable.hpp"
41 #include "object/bullet.hpp"
42 #include "trigger/trigger_base.hpp"
43 #include "control/joystickkeyboardcontroller.hpp"
45 #include "platform.hpp"
46 #include "badguy/badguy.hpp"
47 #include "player_status.hpp"
50 static const int TILES_FOR_BUTTJUMP = 3;
51 static const float SHOOTING_TIME = .150;
52 /// time before idle animation starts
53 static const float IDLE_TIME = 2.5;
55 static const float WALK_ACCELERATION_X = 300;
56 static const float RUN_ACCELERATION_X = 400;
57 static const float SKID_XM = 200;
58 static const float SKID_TIME = .3;
59 static const float MAX_WALK_XM = 230;
60 static const float MAX_RUN_XM = 320;
61 static const float WALK_SPEED = 100;
63 static const float KICK_TIME = .3;
66 Surface* growingtux_left[GROWING_FRAMES];
67 Surface* growingtux_right[GROWING_FRAMES];
69 Surface* tux_life = 0;
71 TuxBodyParts* small_tux = 0;
72 TuxBodyParts* big_tux = 0;
73 TuxBodyParts* fire_tux = 0;
74 TuxBodyParts* ice_tux = 0;
77 TuxBodyParts::set_action(std::string action, int loops)
80 head->set_action(action, loops);
82 body->set_action(action, loops);
84 arms->set_action(action, loops);
86 feet->set_action(action, loops);
90 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
93 head->draw(context, pos, layer-1);
95 body->draw(context, pos, layer-3);
97 arms->draw(context, pos, layer);
99 feet->draw(context, pos, layer-2);
102 Player::Player(PlayerStatus* _player_status)
103 : player_status(_player_status), grabbed_object(0)
105 controller = main_controller;
106 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
107 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
108 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
110 Console::registerCommand("set_bonus", this);
117 delete smalltux_gameover;
118 delete smalltux_star;
126 bbox.set_size(31.8, 63.8);
128 bbox.set_size(31.8, 31.8);
138 fall_mode = ON_GROUND;
143 backflipping = false;
144 backflip_direction = 0;
147 on_ground_flag = false;
150 floor_normal = Vector(0,-1);
156 Player::set_controller(Controller* controller)
158 this->controller = controller;
162 Player::update(float elapsed_time)
164 if(dying && dying_timer.check()) {
169 if(adjust_height != 0) {
170 bbox.move(Vector(0, bbox.get_height() - adjust_height));
171 bbox.set_height(adjust_height);
175 if(!controller->hold(Controller::ACTION) && grabbed_object) {
176 // move the grabbed object a bit away from tux
177 Vector pos = get_pos() +
178 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
179 bbox.get_height()*0.66666 - 32);
180 Rect dest(pos, pos + Vector(32, 32));
181 if(Sector::current()->is_free_space(dest)) {
182 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
184 moving_object->set_pos(pos);
186 msg_debug << "Non MovingObjetc grabbed?!?" << std::endl;
188 grabbed_object->ungrab(*this, dir);
193 if(!dying && !deactivated)
196 movement = physic.get_movement(elapsed_time);
199 // special exception for cases where we're stuck under tiles after
200 // being ducked. In this case we drift out
201 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
202 && collision_object_map(base)) {
203 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
204 previous_base = old_base = base;
208 if(grabbed_object != 0) {
209 Vector pos = get_pos() +
210 Vector(dir == LEFT ? -16 : 16,
211 bbox.get_height()*0.66666 - 32);
212 grabbed_object->grab(*this, pos, dir);
215 on_ground_flag = false;
221 return on_ground_flag;
227 if(player_status->bonus == NO_BONUS)
234 Player::handle_horizontal_input()
236 float vx = physic.get_velocity_x();
237 float vy = physic.get_velocity_y();
238 float ax = physic.get_acceleration_x();
239 float ay = physic.get_acceleration_y();
242 if(!duck || physic.get_velocity_y() != 0) {
243 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
247 } else if(!controller->hold(Controller::LEFT)
248 && controller->hold(Controller::RIGHT)) {
255 if (!controller->hold(Controller::ACTION)) {
256 ax = dirsign * WALK_ACCELERATION_X;
258 if(vx >= MAX_WALK_XM && dirsign > 0) {
261 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
266 ax = dirsign * RUN_ACCELERATION_X;
268 if(vx >= MAX_RUN_XM && dirsign > 0) {
271 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
277 // we can reach WALK_SPEED without any acceleration
278 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
279 vx = dirsign * WALK_SPEED;
282 // changing directions?
283 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
285 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
286 skidding_timer.start(SKID_TIME);
287 sound_manager->play("sounds/skid.wav");
288 // dust some particles
289 Sector::current()->add_object(
291 Vector(dir == RIGHT ? bbox.p2.x : bbox.p1.x, bbox.p2.y),
292 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
293 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
302 // we get slower when not pressing any keys
304 if(fabs(vx) < WALK_SPEED) {
308 ax = WALK_ACCELERATION_X * 1.5;
310 ax = WALK_ACCELERATION_X * -1.5;
315 // if we're on ice slow down acceleration or deceleration
316 if (isice(base.x, base.y + base.height))
318 /* the acceleration/deceleration rate on ice is inversely proportional to
319 * the current velocity.
322 // increasing 1 will increase acceleration/deceleration rate
323 // decreasing 1 will decrease acceleration/deceleration rate
324 // must stay above zero, though
325 if (ax != 0) ax *= 1 / fabs(vx);
329 // extend/shrink tux collision rectangle so that we fall through/walk over 1
331 if(fabsf(vx) > MAX_WALK_XM) {
334 bbox.set_width(31.8);
337 // on downward slopes, adjust vertical velocity to match slope angle
339 if (floor_normal.y != 0) {
340 if ((floor_normal.x * vx) > 0) {
341 // we overdo it a little, just to be on the safe side
342 vy = vx * (floor_normal.x / floor_normal.y) * 2;
347 physic.set_velocity(vx, vy);
348 physic.set_acceleration(ax, ay);
352 Player::handle_vertical_input()
356 fall_mode = ON_GROUND;
357 last_ground_y = get_pos().y;
359 if(get_pos().y > last_ground_y)
361 else if(fall_mode == ON_GROUND)
365 if(on_ground()) { /* Make sure jumping is off. */
368 backflipping = false;
369 backflip_direction = 0;
374 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
376 if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
377 physic.set_velocity_y(300);
378 else { //do a backflip
380 physic.set_velocity_y(580);
381 backflip_timer.start(0.15);
384 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
385 physic.set_velocity_y(580);
387 physic.set_velocity_y(520);
389 //bbox.move(Vector(0, -1));
393 sound_manager->play("sounds/bigjump.wav");
395 sound_manager->play("sounds/jump.wav");
396 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
397 if (!backflipping && jumping && physic.get_velocity_y() > 0) {
399 physic.set_velocity_y(0);
403 /* In case the player has pressed Down while in a certain range of air,
404 enable butt jump action */
405 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
406 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
409 /* When Down is not held anymore, disable butt jump */
410 if(butt_jump && !controller->hold(Controller::DOWN))
415 if (butt_jump && on_ground() && is_big()) {
418 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
420 Sector::current()->add_smoke_cloud(
421 Vector(get_pos().x - 32, get_pos().y + 32));
425 // Break bricks beneath Tux
426 if(Sector::current()->trybreakbrick(
427 Vector(base.x + 1, base.y + base.height), false)
428 || Sector::current()->trybreakbrick(
429 Vector(base.x + base.width - 1, base.y + base.height), false)) {
430 physic.set_velocity_y(2);
434 // Kill nearby badguys
435 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
436 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
437 i != gameobjects.end();
439 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
441 // don't kill when badguys are already dying or in a certain mode
442 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
443 badguy->mode != BadGuy::BOMB_EXPLODE) {
444 if (fabsf(base.x - badguy->base.x) < 96 &&
445 fabsf(base.y - badguy->base.y) < 64)
453 /** jumping is only allowed if we're about to touch ground soon and if the
454 * button has been up in between the last jump
458 if ( (issolid(get_pos().x + bbox.get_width() / 2,
459 get_pos().y + bbox.get_height() + 64) ||
460 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
461 issolid(get_pos().x + bbox.get_width() - 1,
462 get_pos().y + bbox.get_height() + 64))
465 && input.jump && !input.old_jump)
473 Player::handle_input()
475 /* Handle horizontal movement: */
476 if (!backflipping) handle_horizontal_input();
478 if (backflip_direction == 0) {
479 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
481 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
482 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
487 if (on_ground() && !controller->hold(Controller::JUMP))
489 handle_vertical_input();
492 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
493 if(Sector::current()->add_bullet(
494 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
495 : Vector(32, bbox.get_height()/2)),
496 physic.get_velocity_x(), dir))
497 shooting_timer.start(SHOOTING_TIME);
501 if (controller->hold(Controller::DOWN) && is_big() && !duck
502 && physic.get_velocity_y() == 0 && on_ground()) {
504 bbox.move(Vector(0, 32));
505 bbox.set_height(31.8);
506 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
507 // if we have some velocity left then check if there is space for
509 bbox.move(Vector(0, -32));
510 bbox.set_height(63.8);
511 if(Sector::current()->is_free_space(bbox) || (
512 physic.get_velocity_x() > -.01 && physic.get_velocity_x() < .01
513 && physic.get_velocity_y() > -.01 && physic.get_velocity_y() < .01))
517 // undo the ducking changes
518 bbox.move(Vector(0, 32));
519 bbox.set_height(31.8);
525 Player::set_bonus(BonusType type, bool animate)
527 if(player_status->bonus >= type)
530 if(player_status->bonus == NO_BONUS) {
531 adjust_height = 63.8;
533 growing_timer.start(GROWING_TIME);
536 player_status->bonus = type;
540 Player::set_visible(bool visible)
542 this->visible = visible;
546 Player::get_visible()
554 kick_timer.start(KICK_TIME);
558 Player::draw(DrawingContext& context)
563 TuxBodyParts* tux_body;
565 if (player_status->bonus == GROWUP_BONUS)
567 else if (player_status->bonus == FIRE_BONUS)
569 else if (player_status->bonus == ICE_BONUS)
572 tux_body = small_tux;
574 int layer = LAYER_OBJECTS + 10;
576 /* Set Tux sprite action */
577 if (duck && is_big())
580 tux_body->set_action("duck-left");
582 tux_body->set_action("duck-right");
584 else if (skidding_timer.started() && !skidding_timer.check())
587 tux_body->set_action("skid-left");
589 tux_body->set_action("skid-right");
591 else if (kick_timer.started() && !kick_timer.check())
594 tux_body->set_action("kick-left");
596 tux_body->set_action("kick-right");
598 else if (butt_jump && is_big())
601 tux_body->set_action("buttjump-left");
603 tux_body->set_action("buttjump-right");
605 else if (!on_ground())
608 tux_body->set_action("jump-left");
610 tux_body->set_action("jump-right");
614 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
617 tux_body->set_action("stand-left");
619 tux_body->set_action("stand-right");
624 tux_body->set_action("walk-left");
626 tux_body->set_action("walk-right");
630 if(idle_timer.check())
635 tux_body->head->set_action("idle-left", 1);
637 tux_body->head->set_action("idle-right", 1);
642 // Tux is holding something
643 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
644 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
649 tux_body->arms->set_action("duck+grab-left");
651 tux_body->arms->set_action("duck+grab-right");
656 tux_body->arms->set_action("grab-left");
658 tux_body->arms->set_action("grab-right");
664 smalltux_gameover->draw(context, get_pos(), layer);
665 } else if(growing_timer.get_timeleft() > 0) {
667 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
668 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
670 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
671 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
674 else if (safe_timer.started() && size_t(game_time*40)%2)
677 tux_body->draw(context, get_pos(), layer);
679 // Draw blinking star overlay
680 if (invincible_timer.started() &&
681 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
682 || size_t(game_time*20)%2)
685 if (!is_big() || duck)
686 smalltux_star->draw(context, get_pos(), layer + 5);
688 bigtux_star->draw(context, get_pos(), layer + 5);
693 Player::collision_tile(uint32_t tile_attributes)
695 if(tile_attributes & Tile::HURTS)
700 Player::collision(GameObject& other, const CollisionHit& hit)
702 Bullet* bullet = dynamic_cast<Bullet*> (&other);
707 if(other.get_flags() & FLAG_PORTABLE) {
708 Portable* portable = dynamic_cast<Portable*> (&other);
709 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
710 && fabsf(hit.normal.x) > .9) {
711 grabbed_object = portable;
716 if(other.get_flags() & FLAG_SOLID) {
718 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
720 hit.normal.x, hit.normal.y, hit.depth,
721 get_pos().x, get_pos().y,
722 movement.x, movement.y);
725 if(hit.normal.y < 0) { // landed on floor?
726 if(physic.get_velocity_y() < 0)
727 physic.set_velocity_y(0);
729 on_ground_flag = true;
731 // remember normal of this tile
732 if (hit.normal.y > -0.9) {
733 floor_normal.x = hit.normal.x;
734 floor_normal.y = hit.normal.y;
736 // slowly adjust to unisolid tiles.
737 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
738 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
739 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
742 // hack platforms so that we stand normally on them when going down...
743 Platform* platform = dynamic_cast<Platform*> (&other);
744 if(platform != NULL) {
745 if(platform->get_speed().y > 0)
746 physic.set_velocity_y(-platform->get_speed().y);
747 //physic.set_velocity_x(platform->get_speed().x);
749 } else if(hit.normal.y > 0) { // bumped against the roof
750 physic.set_velocity_y(.1);
752 // hack platform so that we are not glued to it from below
753 Platform* platform = dynamic_cast<Platform*> (&other);
754 if(platform != NULL) {
755 physic.set_velocity_y(-platform->get_speed().y);
759 if(fabsf(hit.normal.x) > .9) { // hit on the side?
760 physic.set_velocity_x(0);
763 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
765 Vector mov = movement - omov->get_movement();
767 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",
769 hit.normal.x, hit.normal.y,
771 movement.x, movement.y,
772 dest.p1.x, dest.p1.y,
773 omov->get_movement().x, omov->get_movement().y);
781 assert(dynamic_cast<MovingObject*> (&other) != NULL);
783 MovingObject* moving_object = static_cast<MovingObject*> (&other);
784 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
785 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
787 if(controller->pressed(Controller::UP))
788 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
794 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
796 if(safe_timer.started())
806 Player::make_invincible()
808 sound_manager->play("sounds/invincible.wav");
809 invincible_timer.start(TUX_INVINCIBLE_TIME);
810 Sector::current()->play_music(HERRING_MUSIC);
815 Player::kill(HurtMode mode)
817 if(dying || deactivated)
821 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
824 sound_manager->play("sounds/hurt.wav");
826 physic.set_velocity_x(0);
828 if (mode == SHRINK && is_big())
830 if (player_status->bonus == FIRE_BONUS
831 || player_status->bonus == ICE_BONUS)
833 safe_timer.start(TUX_SAFE_TIME);
834 player_status->bonus = GROWUP_BONUS;
838 //growing_timer.start(GROWING_TIME);
839 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
840 adjust_height = 31.8;
842 player_status->bonus = NO_BONUS;
847 physic.enable_gravity(true);
848 physic.set_acceleration(0, 0);
849 physic.set_velocity(0, 700);
850 player_status->coins -= 25;
851 player_status->bonus = NO_BONUS;
853 dying_timer.start(3.0);
854 set_group(COLGROUP_DISABLED);
856 DisplayEffect* effect = new DisplayEffect();
857 effect->fade_out(3.0);
858 Sector::current()->add_object(effect);
859 sound_manager->stop_music(3.0);
864 Player::move(const Vector& vector)
866 bbox.set_pos(vector);
868 bbox.set_size(31.8, 63.8);
870 bbox.set_size(31.8, 31.8);
872 last_ground_y = vector.y;
878 Player::check_bounds(Camera* camera)
880 /* Keep tux in bounds: */
882 { // Lock Tux to the size of the level, so that he doesn't fall of
884 bbox.set_pos(Vector(0, get_pos().y));
887 /* Keep in-bounds, vertically: */
888 if (get_pos().y > Sector::current()->solids->get_height() * 32)
895 // can happen if back scrolling is disabled
896 if(get_pos().x < camera->get_translation().x) {
897 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
900 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
903 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
912 if(collision_object_map(bbox)) {
921 Player::add_velocity(const Vector& velocity)
923 physic.set_velocity(physic.get_velocity() + velocity);
927 Player::bounce(BadGuy& )
929 if(controller->hold(Controller::JUMP))
930 physic.set_velocity_y(520);
932 physic.set_velocity_y(300);
935 //Scripting Functions Below
941 physic.set_velocity_x(0);
942 physic.set_velocity_y(0);
951 void Player::walk(float speed)
953 physic.set_velocity_x(speed);
957 Player::consoleCommand(std::string command, std::vector<std::string> arguments)
959 if (command == "set_bonus") {
960 if (arguments.size() == 1) {
961 if (arguments[0] == "egg") {
962 set_bonus(GROWUP_BONUS, false);
965 if (arguments[0] == "fire") {
966 set_bonus(FIRE_BONUS, false);
969 if (arguments[0] == "ice") {
970 set_bonus(ICE_BONUS, false);
974 msg_info << "Usage: give {\"egg\" | \"fire\" | \"ice\"}" << std::endl;