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");
114 delete smalltux_gameover;
115 delete smalltux_star;
123 bbox.set_size(31.8, 63.8);
125 bbox.set_size(31.8, 31.8);
135 fall_mode = ON_GROUND;
140 backflipping = false;
141 backflip_direction = 0;
144 on_ground_flag = false;
147 floor_normal = Vector(0,-1);
153 Player::set_controller(Controller* controller)
155 this->controller = controller;
159 Player::update(float elapsed_time)
161 if(dying && dying_timer.check()) {
166 if(adjust_height != 0) {
167 bbox.move(Vector(0, bbox.get_height() - adjust_height));
168 bbox.set_height(adjust_height);
172 if(!controller->hold(Controller::ACTION) && grabbed_object) {
173 // move the grabbed object a bit away from tux
174 Vector pos = get_pos() +
175 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
176 bbox.get_height()*0.66666 - 32);
177 Rect dest(pos, pos + Vector(32, 32));
178 if(Sector::current()->is_free_space(dest)) {
179 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
181 moving_object->set_pos(pos);
183 msg_debug("Non MovingObjetc grabbed?!?");
185 grabbed_object->ungrab(*this, dir);
190 if(!dying && !deactivated)
193 movement = physic.get_movement(elapsed_time);
196 // special exception for cases where we're stuck under tiles after
197 // being ducked. In this case we drift out
198 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
199 && collision_object_map(base)) {
200 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
201 previous_base = old_base = base;
205 if(grabbed_object != 0) {
206 Vector pos = get_pos() +
207 Vector(dir == LEFT ? -16 : 16,
208 bbox.get_height()*0.66666 - 32);
209 grabbed_object->grab(*this, pos, dir);
212 on_ground_flag = false;
218 return on_ground_flag;
224 if(player_status->bonus == NO_BONUS)
231 Player::handle_horizontal_input()
233 float vx = physic.get_velocity_x();
234 float vy = physic.get_velocity_y();
235 float ax = physic.get_acceleration_x();
236 float ay = physic.get_acceleration_y();
239 if(!duck || physic.get_velocity_y() != 0) {
240 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
244 } else if(!controller->hold(Controller::LEFT)
245 && controller->hold(Controller::RIGHT)) {
252 if (!controller->hold(Controller::ACTION)) {
253 ax = dirsign * WALK_ACCELERATION_X;
255 if(vx >= MAX_WALK_XM && dirsign > 0) {
258 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
263 ax = dirsign * RUN_ACCELERATION_X;
265 if(vx >= MAX_RUN_XM && dirsign > 0) {
268 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
274 // we can reach WALK_SPEED without any acceleration
275 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
276 vx = dirsign * WALK_SPEED;
279 // changing directions?
280 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
282 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
283 skidding_timer.start(SKID_TIME);
284 sound_manager->play("sounds/skid.wav");
285 // dust some particles
286 Sector::current()->add_object(
288 Vector(dir == RIGHT ? bbox.p2.x : bbox.p1.x, bbox.p2.y),
289 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
290 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
299 // we get slower when not pressing any keys
301 if(fabs(vx) < WALK_SPEED) {
305 ax = WALK_ACCELERATION_X * 1.5;
307 ax = WALK_ACCELERATION_X * -1.5;
312 // if we're on ice slow down acceleration or deceleration
313 if (isice(base.x, base.y + base.height))
315 /* the acceleration/deceleration rate on ice is inversely proportional to
316 * the current velocity.
319 // increasing 1 will increase acceleration/deceleration rate
320 // decreasing 1 will decrease acceleration/deceleration rate
321 // must stay above zero, though
322 if (ax != 0) ax *= 1 / fabs(vx);
326 // extend/shrink tux collision rectangle so that we fall through/walk over 1
328 if(fabsf(vx) > MAX_WALK_XM) {
331 bbox.set_width(31.8);
334 // on downward slopes, adjust vertical velocity to match slope angle
336 if (floor_normal.y != 0) {
337 if ((floor_normal.x * vx) > 0) {
338 // we overdo it a little, just to be on the safe side
339 vy = vx * (floor_normal.x / floor_normal.y) * 2;
344 physic.set_velocity(vx, vy);
345 physic.set_acceleration(ax, ay);
349 Player::handle_vertical_input()
353 fall_mode = ON_GROUND;
354 last_ground_y = get_pos().y;
356 if(get_pos().y > last_ground_y)
358 else if(fall_mode == ON_GROUND)
362 if(on_ground()) { /* Make sure jumping is off. */
365 backflipping = false;
366 backflip_direction = 0;
371 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
373 if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
374 physic.set_velocity_y(300);
375 else { //do a backflip
377 physic.set_velocity_y(580);
378 backflip_timer.start(0.15);
381 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
382 physic.set_velocity_y(580);
384 physic.set_velocity_y(520);
386 //bbox.move(Vector(0, -1));
390 sound_manager->play("sounds/bigjump.wav");
392 sound_manager->play("sounds/jump.wav");
393 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
394 if (!backflipping && jumping && physic.get_velocity_y() > 0) {
396 physic.set_velocity_y(0);
400 /* In case the player has pressed Down while in a certain range of air,
401 enable butt jump action */
402 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
403 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
406 /* When Down is not held anymore, disable butt jump */
407 if(butt_jump && !controller->hold(Controller::DOWN))
412 if (butt_jump && on_ground() && is_big()) {
415 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
417 Sector::current()->add_smoke_cloud(
418 Vector(get_pos().x - 32, get_pos().y + 32));
422 // Break bricks beneath Tux
423 if(Sector::current()->trybreakbrick(
424 Vector(base.x + 1, base.y + base.height), false)
425 || Sector::current()->trybreakbrick(
426 Vector(base.x + base.width - 1, base.y + base.height), false)) {
427 physic.set_velocity_y(2);
431 // Kill nearby badguys
432 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
433 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
434 i != gameobjects.end();
436 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
438 // don't kill when badguys are already dying or in a certain mode
439 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
440 badguy->mode != BadGuy::BOMB_EXPLODE) {
441 if (fabsf(base.x - badguy->base.x) < 96 &&
442 fabsf(base.y - badguy->base.y) < 64)
450 /** jumping is only allowed if we're about to touch ground soon and if the
451 * button has been up in between the last jump
455 if ( (issolid(get_pos().x + bbox.get_width() / 2,
456 get_pos().y + bbox.get_height() + 64) ||
457 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
458 issolid(get_pos().x + bbox.get_width() - 1,
459 get_pos().y + bbox.get_height() + 64))
462 && input.jump && !input.old_jump)
470 Player::handle_input()
472 /* Handle horizontal movement: */
473 if (!backflipping) handle_horizontal_input();
475 if (backflip_direction == 0) {
476 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
478 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
479 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
484 if (on_ground() && !controller->hold(Controller::JUMP))
486 handle_vertical_input();
489 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
490 if(Sector::current()->add_bullet(
491 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
492 : Vector(32, bbox.get_height()/2)),
493 physic.get_velocity_x(), dir))
494 shooting_timer.start(SHOOTING_TIME);
498 if (controller->hold(Controller::DOWN) && is_big() && !duck
499 && physic.get_velocity_y() == 0 && on_ground()) {
501 bbox.move(Vector(0, 32));
502 bbox.set_height(31.8);
503 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
504 // if we have some velocity left then check if there is space for
506 bbox.move(Vector(0, -32));
507 bbox.set_height(63.8);
508 if(Sector::current()->is_free_space(bbox) || (
509 physic.get_velocity_x() > -.01 && physic.get_velocity_x() < .01
510 && physic.get_velocity_y() > -.01 && physic.get_velocity_y() < .01))
514 // undo the ducking changes
515 bbox.move(Vector(0, 32));
516 bbox.set_height(31.8);
522 Player::set_bonus(BonusType type, bool animate)
524 if(player_status->bonus >= type)
527 if(player_status->bonus == NO_BONUS) {
528 adjust_height = 63.8;
530 growing_timer.start(GROWING_TIME);
533 player_status->bonus = type;
537 Player::set_visible(bool visible)
539 this->visible = visible;
543 Player::get_visible()
551 kick_timer.start(KICK_TIME);
555 Player::draw(DrawingContext& context)
560 TuxBodyParts* tux_body;
562 if (player_status->bonus == GROWUP_BONUS)
564 else if (player_status->bonus == FIRE_BONUS)
566 else if (player_status->bonus == ICE_BONUS)
569 tux_body = small_tux;
571 int layer = LAYER_OBJECTS + 10;
573 /* Set Tux sprite action */
574 if (duck && is_big())
577 tux_body->set_action("duck-left");
579 tux_body->set_action("duck-right");
581 else if (skidding_timer.started() && !skidding_timer.check())
584 tux_body->set_action("skid-left");
586 tux_body->set_action("skid-right");
588 else if (kick_timer.started() && !kick_timer.check())
591 tux_body->set_action("kick-left");
593 tux_body->set_action("kick-right");
595 else if (butt_jump && is_big())
598 tux_body->set_action("buttjump-left");
600 tux_body->set_action("buttjump-right");
602 else if (!on_ground())
605 tux_body->set_action("jump-left");
607 tux_body->set_action("jump-right");
611 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
614 tux_body->set_action("stand-left");
616 tux_body->set_action("stand-right");
621 tux_body->set_action("walk-left");
623 tux_body->set_action("walk-right");
627 if(idle_timer.check())
632 tux_body->head->set_action("idle-left", 1);
634 tux_body->head->set_action("idle-right", 1);
639 // Tux is holding something
640 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
641 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
646 tux_body->arms->set_action("duck+grab-left");
648 tux_body->arms->set_action("duck+grab-right");
653 tux_body->arms->set_action("grab-left");
655 tux_body->arms->set_action("grab-right");
661 smalltux_gameover->draw(context, get_pos(), layer);
662 } else if(growing_timer.get_timeleft() > 0) {
664 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
665 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
667 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
668 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
671 else if (safe_timer.started() && size_t(game_time*40)%2)
674 tux_body->draw(context, get_pos(), layer);
676 // Draw blinking star overlay
677 if (invincible_timer.started() &&
678 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
679 || size_t(game_time*20)%2)
682 if (!is_big() || duck)
683 smalltux_star->draw(context, get_pos(), layer + 5);
685 bigtux_star->draw(context, get_pos(), layer + 5);
690 Player::collision_tile(uint32_t tile_attributes)
692 if(tile_attributes & Tile::HURTS)
697 Player::collision(GameObject& other, const CollisionHit& hit)
699 Bullet* bullet = dynamic_cast<Bullet*> (&other);
704 if(other.get_flags() & FLAG_PORTABLE) {
705 Portable* portable = dynamic_cast<Portable*> (&other);
706 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
707 && fabsf(hit.normal.x) > .9) {
708 grabbed_object = portable;
713 if(other.get_flags() & FLAG_SOLID) {
715 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
717 hit.normal.x, hit.normal.y, hit.depth,
718 get_pos().x, get_pos().y,
719 movement.x, movement.y);
722 if(hit.normal.y < 0) { // landed on floor?
723 if(physic.get_velocity_y() < 0)
724 physic.set_velocity_y(0);
726 on_ground_flag = true;
728 // remember normal of this tile
729 if (hit.normal.y > -0.9) {
730 floor_normal.x = hit.normal.x;
731 floor_normal.y = hit.normal.y;
733 // slowly adjust to unisolid tiles.
734 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
735 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
736 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
739 // hack platforms so that we stand normally on them when going down...
740 Platform* platform = dynamic_cast<Platform*> (&other);
741 if(platform != NULL) {
742 if(platform->get_speed().y > 0)
743 physic.set_velocity_y(-platform->get_speed().y);
744 //physic.set_velocity_x(platform->get_speed().x);
746 } else if(hit.normal.y > 0) { // bumped against the roof
747 physic.set_velocity_y(.1);
749 // hack platform so that we are not glued to it from below
750 Platform* platform = dynamic_cast<Platform*> (&other);
751 if(platform != NULL) {
752 physic.set_velocity_y(-platform->get_speed().y);
756 if(fabsf(hit.normal.x) > .9) { // hit on the side?
757 physic.set_velocity_x(0);
760 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
762 Vector mov = movement - omov->get_movement();
764 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",
766 hit.normal.x, hit.normal.y,
768 movement.x, movement.y,
769 dest.p1.x, dest.p1.y,
770 omov->get_movement().x, omov->get_movement().y);
778 assert(dynamic_cast<MovingObject*> (&other) != NULL);
780 MovingObject* moving_object = static_cast<MovingObject*> (&other);
781 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
782 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
784 if(controller->pressed(Controller::UP))
785 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
791 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
793 if(safe_timer.started())
803 Player::make_invincible()
805 sound_manager->play("sounds/invincible.wav");
806 invincible_timer.start(TUX_INVINCIBLE_TIME);
807 Sector::current()->play_music(HERRING_MUSIC);
812 Player::kill(HurtMode mode)
814 if(dying || deactivated)
818 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
821 sound_manager->play("sounds/hurt.wav");
823 physic.set_velocity_x(0);
825 if (mode == SHRINK && is_big())
827 if (player_status->bonus == FIRE_BONUS
828 || player_status->bonus == ICE_BONUS)
830 safe_timer.start(TUX_SAFE_TIME);
831 player_status->bonus = GROWUP_BONUS;
835 //growing_timer.start(GROWING_TIME);
836 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
837 adjust_height = 31.8;
839 player_status->bonus = NO_BONUS;
844 physic.enable_gravity(true);
845 physic.set_acceleration(0, 0);
846 physic.set_velocity(0, 700);
847 player_status->coins -= 25;
848 player_status->bonus = NO_BONUS;
850 dying_timer.start(3.0);
851 set_group(COLGROUP_DISABLED);
853 DisplayEffect* effect = new DisplayEffect();
854 effect->fade_out(3.0);
855 Sector::current()->add_object(effect);
856 sound_manager->stop_music(3.0);
861 Player::move(const Vector& vector)
863 bbox.set_pos(vector);
865 bbox.set_size(31.8, 63.8);
867 bbox.set_size(31.8, 31.8);
869 last_ground_y = vector.y;
875 Player::check_bounds(Camera* camera)
877 /* Keep tux in bounds: */
879 { // Lock Tux to the size of the level, so that he doesn't fall of
881 bbox.set_pos(Vector(0, get_pos().y));
884 /* Keep in-bounds, vertically: */
885 if (get_pos().y > Sector::current()->solids->get_height() * 32)
892 // can happen if back scrolling is disabled
893 if(get_pos().x < camera->get_translation().x) {
894 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
897 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
900 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
909 if(collision_object_map(bbox)) {
918 Player::add_velocity(const Vector& velocity)
920 physic.set_velocity(physic.get_velocity() + velocity);
924 Player::bounce(BadGuy& )
926 if(controller->hold(Controller::JUMP))
927 physic.set_velocity_y(520);
929 physic.set_velocity_y(300);
932 //Scripting Functions Below
938 physic.set_velocity_x(0);
939 physic.set_velocity_y(0);
948 void Player::walk(float speed)
950 physic.set_velocity_x(speed);