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 "app/globals.h"
27 #include "app/gettext.h"
32 #include "special/sprite.h"
37 #include "resources.h"
38 #include "video/screen.h"
39 #include "statistics.h"
41 #include "trigger/trigger_base.h"
43 // behavior definitions:
44 #define TILES_FOR_BUTTJUMP 3
45 // animation times (in ms):
46 #define SHOOTING_TIME .150
48 // time before idle animation starts
49 #define IDLE_TIME 2.500
52 Surface* growingtux_left[GROWING_FRAMES];
53 Surface* growingtux_right[GROWING_FRAMES];
55 Surface* tux_life = 0;
57 Sprite* smalltux_gameover = 0;
58 Sprite* smalltux_star = 0;
59 Sprite* bigtux_star = 0;
61 TuxBodyParts* small_tux = 0;
62 TuxBodyParts* big_tux = 0;
63 TuxBodyParts* fire_tux = 0;
64 TuxBodyParts* ice_tux = 0;
68 PlayerKeymap::PlayerKeymap()
71 keymap.down = SDLK_DOWN;
72 keymap.left = SDLK_LEFT;
73 keymap.right = SDLK_RIGHT;
75 keymap.power = SDLK_LCTRL;
76 keymap.jump = SDLK_SPACE;
79 void player_input_init(player_input_type* pplayer_input)
81 pplayer_input->up = UP;
82 pplayer_input->old_up = UP;
83 pplayer_input->down = UP;
84 pplayer_input->fire = UP;
85 pplayer_input->left = UP;
86 pplayer_input->old_fire = UP;
87 pplayer_input->right = UP;
88 pplayer_input->jump = UP;
89 pplayer_input->old_jump = UP;
90 pplayer_input->activate = UP;
94 TuxBodyParts::set_action(std::string action, int loops)
97 head->set_action(action, loops);
99 body->set_action(action, loops);
101 arms->set_action(action, loops);
103 feet->set_action(action, loops);
107 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer,
108 Uint32 drawing_effect)
111 head->draw(context, pos, layer-1, drawing_effect);
113 body->draw(context, pos, layer-3, drawing_effect);
115 arms->draw(context, pos, layer, drawing_effect);
117 feet->draw(context, pos, layer-2, drawing_effect);
132 holding_something = false;
134 bbox.set_size(31.8, 31.8);
137 got_power = NONE_POWER;
146 fall_mode = ON_GROUND;
151 falling_from_flap = false;
152 enable_hover = false;
155 flapping_velocity = 0;
157 // temporary to help player's choosing a flapping
158 flapping_mode = MAREK_FLAP;
160 // Ricardo's flapping
163 on_ground_flag = false;
165 player_input_init(&input);
171 Player::key_event(SDLKey key, int state)
173 idle_timer.start(IDLE_TIME, true);
175 if(key == keymap.right)
180 else if(key == keymap.left)
185 else if(key == keymap.up)
190 /* Up key also opens activates stuff */
191 input.activate = state;
194 else if(key == keymap.down)
199 else if(key == keymap.power)
207 else if(key == keymap.jump)
219 Player::level_begin()
221 move(Vector(100, 170));
226 player_input_init(&input);
228 on_ground_flag = false;
236 return player_status;
240 Player::action(float elapsed_time)
242 if(dying && dying_timer.check()) {
247 if (input.fire == UP)
248 holding_something = false;
250 if(dying == DYING_NOT)
253 movement = physic.get_movement(elapsed_time);
256 // special exception for cases where we're stuck under tiles after
257 // being ducked. In this case we drift out
258 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
259 && collision_object_map(base))
261 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
262 previous_base = old_base = base;
265 /* Reset score multiplier (for multi-hits): */
266 if (!invincible_timer.started())
268 if(player_status.score_multiplier > player_status.max_score_multiplier)
270 player_status.max_score_multiplier = player_status.score_multiplier;
274 sprintf(str, _("New max combo: %d"), player_status.max_score_multiplier-1);
275 Sector::current()->add_floating_text(base, str);
277 player_status.score_multiplier = 1;
284 on_ground_flag = false;
290 return on_ground_flag;
294 Player::handle_horizontal_input()
296 float vx = physic.get_velocity_x();
297 float vy = physic.get_velocity_y();
298 float ax = physic.get_acceleration_x();
299 float ay = physic.get_acceleration_y();
302 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
306 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
312 if (input.fire == UP) {
313 ax = dirsign * WALK_ACCELERATION_X;
315 if(vx >= MAX_WALK_XM && dirsign > 0) {
318 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
323 ax = dirsign * RUN_ACCELERATION_X;
325 if(vx >= MAX_RUN_XM && dirsign > 0) {
328 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
334 // we can reach WALK_SPEED without any acceleration
335 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
336 vx = dirsign * WALK_SPEED;
339 // changing directions?
340 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0)))
343 if(fabs(vx)>SKID_XM && !skidding_timer.started())
345 skidding_timer.start(SKID_TIME);
346 SoundManager::get()->play_sound(IDToSound(SND_SKID));
347 // dust some partcles
348 Sector::current()->add_object(
350 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
352 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
353 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
364 // we get slower when not pressing any keys
366 if(fabs(vx) < WALK_SPEED) {
370 ax = WALK_ACCELERATION_X * 1.5;
372 ax = WALK_ACCELERATION_X * -1.5;
377 // if we're on ice slow down acceleration or deceleration
378 if (isice(base.x, base.y + base.height))
380 /* the acceleration/deceleration rate on ice is inversely proportional to
381 * the current velocity.
384 // increasing 1 will increase acceleration/deceleration rate
385 // decreasing 1 will decrease acceleration/deceleration rate
386 // must stay above zero, though
387 if (ax != 0) ax *= 1 / fabs(vx);
391 // extend/shrink tux collision rectangle so that we fall through/walk over 1
393 if(vx > MAX_WALK_XM) {
396 bbox.set_width(31.8);
399 physic.set_velocity(vx, vy);
400 physic.set_acceleration(ax, ay);
404 Player::handle_vertical_input()
408 fall_mode = ON_GROUND;
409 last_ground_y = get_pos().y;
411 if(get_pos().y > last_ground_y)
413 else if(fall_mode == ON_GROUND)
417 if(on_ground()) { /* Make sure jumping is off. */
420 falling_from_flap = false;
421 if (flapping_timer.started()) {
422 flapping_timer.start(0);
425 physic.set_acceleration_y(0); //for flapping
429 if(input.jump == DOWN && can_jump && on_ground())
431 if(duck) { // only jump a little bit when in duck mode {
432 physic.set_velocity_y(300);
434 // jump higher if we are running
435 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
436 physic.set_velocity_y(580);
438 physic.set_velocity_y(520);
441 //bbox.move(Vector(0, -1));
446 flaps_nb = 0; // Ricardo's flapping
448 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
450 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
452 // Let go of jump key
453 else if(input.jump == UP)
455 if (!flapping && !duck && !falling_from_flap && !on_ground())
459 if (jumping && physic.get_velocity_y() > 0)
462 physic.set_velocity_y(0);
466 // temporary to help player's choosing a flapping
467 if(flapping_mode == RICARDO_FLAP)
469 // Flapping, Ricardo's version
470 // similar to SM3 Fox
471 if(input.jump == DOWN && input.old_jump == UP && can_flap &&
474 physic.set_velocity_y(350);
475 physic.set_velocity_x(physic.get_velocity_x() * 35);
479 else if(flapping_mode == MAREK_FLAP)
481 // Flapping, Marek's version
482 if (input.jump == DOWN && can_flap)
484 if (!flapping_timer.started())
486 flapping_timer.start(TUX_FLAPPING_TIME);
487 flapping_velocity = physic.get_velocity_x();
489 if (flapping_timer.check())
492 falling_from_flap = true;
496 if (!flapping_timer.check()) {
497 float cv = flapping_velocity * sqrt(
498 TUX_FLAPPING_TIME - flapping_timer.get_timegone()
499 / TUX_FLAPPING_TIME);
501 //Handle change of direction while flapping
502 if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
505 physic.set_velocity_x(cv);
506 physic.set_velocity_y(
507 flapping_timer.get_timegone()/.850);
511 else if(flapping_mode == RYAN_FLAP)
513 // Flapping, Ryan's version
514 if (input.jump == DOWN && can_flap)
516 if (!flapping_timer.started())
518 flapping_timer.start(TUX_FLAPPING_TIME);
520 if (flapping_timer.check())
523 falling_from_flap = true;
527 if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
528 && physic.get_velocity_y() < 0)
530 float gravity = Sector::current()->gravity;
532 float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
534 // XXX: magic numbers. should be a percent of gravity
535 // gravity is (by default) -0.1f
536 physic.set_acceleration_y(12 + 1*xr);
539 // To slow down x-vel when flapping (not working)
540 if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
542 if (physic.get_velocity_x() < 0)
543 physic.set_acceleration_x(1.0f);
544 else if (physic.get_velocity_x() > 0)
545 physic.set_acceleration_x(-1.0f);
552 physic.set_acceleration_y(0);
557 //(disabled by default, use cheat code "hover" to toggle on/off)
558 //TODO: needs some tweaking, especially when used together with double jump and jumping off badguys
559 if (enable_hover && input.jump == DOWN && !jumping && !butt_jump && physic.get_velocity_y() <= 0)
561 physic.set_velocity_y(-100);
565 /* In case the player has pressed Down while in a certain range of air,
566 enable butt jump action */
567 if (input.down == DOWN && !butt_jump && !duck)
568 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
572 /* When Down is not held anymore, disable butt jump */
573 if(butt_jump && input.down == UP)
577 if (butt_jump && on_ground() && size == BIG)
581 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
583 Sector::current()->add_smoke_cloud(
584 Vector(get_pos().x - 32, get_pos().y + 32));
589 // Break bricks beneath Tux
590 if(Sector::current()->trybreakbrick(
591 Vector(base.x + 1, base.y + base.height), false)
592 || Sector::current()->trybreakbrick(
593 Vector(base.x + base.width - 1, base.y + base.height), false))
595 physic.set_velocity_y(2);
601 // Kill nearby badguys
602 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
603 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
604 i != gameobjects.end();
607 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
610 // don't kill when badguys are already dying or in a certain mode
611 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
612 badguy->mode != BadGuy::BOMB_EXPLODE)
614 if (fabsf(base.x - badguy->base.x) < 96 &&
615 fabsf(base.y - badguy->base.y) < 64)
623 /** jumping is only allowed if we're about to touch ground soon and if the
624 * button has been up in between the last jump
628 if ( (issolid(get_pos().x + bbox.get_width() / 2,
629 get_pos().y + bbox.get_height() + 64) ||
630 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
631 issolid(get_pos().x + bbox.get_width() - 1,
632 get_pos().y + bbox.get_height() + 64))
635 && input.jump == DOWN
636 && input.old_jump == UP)
642 input.old_jump = input.jump;
646 Player::handle_input()
648 /* Handle horizontal movement: */
649 handle_horizontal_input();
652 if (on_ground() && input.jump == UP)
654 handle_vertical_input();
657 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER) {
658 if(Sector::current()->add_bullet(
659 get_pos() + Vector(0, bbox.get_height()/2),
660 physic.get_velocity_x(), dir))
661 shooting_timer.start(SHOOTING_TIME);
662 input.old_fire = DOWN;
666 if (input.down == DOWN && size == BIG && !duck
667 && physic.get_velocity_y() == 0 && on_ground())
670 bbox.move(Vector(0, 32));
671 bbox.set_height(31.8);
673 else if(input.down == UP && size == BIG && duck)
675 // try if we can really unduck
676 bbox.move(Vector(0, -32));
677 bbox.set_height(63.8);
681 // when unducking in air we need some space to do so
682 if(on_ground() || !collision_object_map(bbox)) {
685 // undo the ducking changes
686 bbox.move(Vector(0, 32));
687 bbox.set_height(31.8);
694 Player::grow(bool animate)
700 bbox.set_height(63.8);
701 bbox.move(Vector(0, -32));
704 growing_timer.start(GROWING_TIME);
708 Player::grabdistros()
713 Player::draw(DrawingContext& context)
715 TuxBodyParts* tux_body;
718 tux_body = small_tux;
719 else if (got_power == FIRE_POWER)
721 else if (got_power == ICE_POWER)
726 int layer = LAYER_OBJECTS + 10;
728 /* Set Tux sprite action */
729 if (duck && size == BIG)
732 tux_body->set_action("duck-left");
734 tux_body->set_action("duck-right");
736 else if (skidding_timer.started() && !skidding_timer.check())
739 tux_body->set_action("skid-left");
741 tux_body->set_action("skid-right");
743 else if (kick_timer.started() && !kick_timer.check())
746 tux_body->set_action("kick-left");
748 tux_body->set_action("kick-right");
750 else if (butt_jump && size == BIG)
753 tux_body->set_action("buttjump-left");
755 tux_body->set_action("buttjump-right");
757 else if (physic.get_velocity_y() != 0)
760 tux_body->set_action("jump-left");
762 tux_body->set_action("jump-right");
766 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
769 tux_body->set_action("stand-left");
771 tux_body->set_action("stand-right");
776 tux_body->set_action("walk-left");
778 tux_body->set_action("walk-right");
782 if(idle_timer.check())
787 tux_body->head->set_action("idle-left", 1);
789 tux_body->head->set_action("idle-right", 1);
794 // Tux is holding something
795 if ((holding_something && physic.get_velocity_y() == 0) ||
796 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
801 tux_body->arms->set_action("duck+grab-left");
803 tux_body->arms->set_action("duck+grab-right");
808 tux_body->arms->set_action("grab-left");
810 tux_body->arms->set_action("grab-right");
815 if (dying == DYING_SQUISHED) {
816 smalltux_gameover->draw(context, get_pos(), LAYER_FOREGROUNDTILES+1);
817 } else if(growing_timer.get_timeleft() > 0) {
821 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
822 int((growing_timer.get_timegone() *
823 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
825 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
826 int((growing_timer.get_timegone() *
827 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
832 context.draw_surface(growingtux_right[
833 int((growing_timer.get_timegone() *
834 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
836 context.draw_surface(growingtux_left[
837 int((growing_timer.get_timegone() *
838 GROWING_FRAMES) / GROWING_TIME)],
842 else if (safe_timer.started() && size_t(global_time*40)%2)
845 tux_body->draw(context, get_pos(), layer);
847 // Draw blinking star overlay
848 if (invincible_timer.started() &&
849 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
850 || size_t(global_time*20)%2)
853 if (size == SMALL || duck)
854 smalltux_star->draw(context, get_pos(), layer + 5);
856 bigtux_star->draw(context, get_pos(), layer + 5);
860 context.draw_filled_rect(get_pos(),
861 Vector(bbox.get_width(), bbox.get_height()),
862 Color(75,75,75, 150), LAYER_OBJECTS+20);
866 Player::collision(GameObject& other, const CollisionHit& hit)
868 if(other.get_flags() & FLAG_SOLID) {
869 if(hit.normal.y < 0) { // landed on floor?
870 if (physic.get_velocity_y() < 0)
871 physic.set_velocity_y(0);
872 on_ground_flag = true;
873 } else if(hit.normal.y > 0) { // bumped against the roof
874 physic.set_velocity_y(.1);
877 if(fabsf(hit.normal.x) > .9) { // hit on the side?
878 physic.set_velocity_x(0);
884 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
886 if(input.up == DOWN && input.old_up == UP)
887 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
894 Player::make_invincible()
896 SoundManager::get()->play_sound(IDToSound(SND_HERRING));
897 invincible_timer.start(TUX_INVINCIBLE_TIME);
898 Sector::current()->play_music(HERRING_MUSIC);
903 Player::kill(HurtMode mode)
908 if(safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
911 SoundManager::get()->play_sound(IDToSound(SND_HURT));
913 physic.set_velocity_x(0);
915 if (mode == SHRINK && size == BIG)
917 if (got_power != NONE_POWER)
919 safe_timer.start(TUX_SAFE_TIME);
920 got_power = NONE_POWER;
924 growing_timer.start(GROWING_TIME);
925 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
927 bbox.set_height(31.8);
933 physic.enable_gravity(true);
934 physic.set_acceleration(0, 0);
935 physic.set_velocity(0, 700);
936 --player_status.lives;
937 dying = DYING_SQUISHED;
938 dying_timer.start(3.0);
939 flags |= FLAG_NO_COLLDET;
943 /* Remove Tux's power ups */
945 Player::remove_powerups()
947 got_power = NONE_POWER;
949 bbox.set_height(31.8);
953 Player::move(const Vector& vector)
955 bbox.set_pos(vector);
959 Player::check_bounds(Camera* camera)
961 /* Keep tux in bounds: */
963 { // Lock Tux to the size of the level, so that he doesn't fall of
965 bbox.set_pos(Vector(0, get_pos().y));
968 /* Keep in-bounds, vertically: */
969 if (get_pos().y > Sector::current()->solids->get_height() * 32)
976 // can happen if back scrolling is disabled
977 if(get_pos().x < camera->get_translation().x) {
978 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
981 if(get_pos().x >= camera->get_translation().x + screen->w - bbox.get_width())
984 camera->get_translation().x + screen->w - bbox.get_width(),
993 if(collision_object_map(bbox)) {
1002 Player::bounce(BadGuy& )
1004 //Make sure we stopped flapping
1006 falling_from_flap = false;
1009 physic.set_velocity_y(520);
1011 physic.set_velocity_y(200);