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.
24 #include "app/globals.h"
29 #include "special/sprite.h"
34 #include "resources.h"
35 #include "interactive_object.h"
36 #include "video/screen.h"
37 #include "statistics.h"
40 // behavior definitions:
41 #define TILES_FOR_BUTTJUMP 3
42 // animation times (in ms):
43 #define SHOOTING_TIME 320
45 #define AUTOSCROLL_DEAD_INTERVAL 300
47 // time before idle animation starts
48 #define IDLE_TIME 2500
51 Surface* growingtux_left[GROWING_FRAMES];
52 Surface* growingtux_right[GROWING_FRAMES];
56 Sprite* smalltux_gameover;
57 Sprite* smalltux_star;
60 TuxBodyParts* small_tux;
61 TuxBodyParts* big_tux;
62 TuxBodyParts* fire_tux;
63 TuxBodyParts* ice_tux;
67 PlayerKeymap::PlayerKeymap()
69 keymap.jump = SDLK_SPACE;
70 keymap.activate = SDLK_UP;
71 keymap.duck = SDLK_DOWN;
72 keymap.left = SDLK_LEFT;
73 keymap.right = SDLK_RIGHT;
74 keymap.fire = SDLK_LCTRL;
77 void player_input_init(player_input_type* pplayer_input)
79 pplayer_input->down = UP;
80 pplayer_input->fire = UP;
81 pplayer_input->left = UP;
82 pplayer_input->old_fire = UP;
83 pplayer_input->right = UP;
84 pplayer_input->up = UP;
85 pplayer_input->old_up = UP;
86 pplayer_input->activate = UP;
90 TuxBodyParts::set_action(std::string action)
93 head->set_action(action);
95 body->set_action(action);
97 arms->set_action(action);
99 feet->set_action(action);
103 TuxBodyParts::one_time_animation()
106 head->start_animation(1);
108 body->start_animation(1);
110 arms->start_animation(1);
112 feet->start_animation(1);
116 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer,
117 Uint32 drawing_effect)
120 head->draw(context, pos, layer-1, drawing_effect);
122 body->draw(context, pos, layer-3, drawing_effect);
124 arms->draw(context, pos, layer, drawing_effect);
126 feet->draw(context, pos, layer-2, drawing_effect);
141 holding_something = false;
147 got_power = NONE_POWER;
151 previous_base = old_base = base;
159 fall_mode = ON_GROUND;
164 falling_from_flap = false;
165 enable_hover = false;
171 player_input_init(&input);
173 invincible_timer.init(true);
174 skidding_timer.init(true);
175 safe_timer.init(true);
176 frame_timer.init(true);
177 kick_timer.init(true);
178 shooting_timer.init(true);
179 growing_timer.init(true);
180 idle_timer.init(true);
181 flapping_timer.init(true);
187 Player::key_event(SDLKey key, int state)
189 idle_timer.start(IDLE_TIME);
191 if(key == keymap.right)
196 else if(key == keymap.left)
201 else if(key == keymap.jump)
206 else if(key == keymap.duck)
211 else if(key == keymap.fire)
218 else if(key == keymap.activate)
220 input.activate = state;
223 /** check for interactive objects */
224 for(Sector::InteractiveObjects::iterator i
225 = Sector::current()->interactive_objects.begin();
226 i != Sector::current()->interactive_objects.end(); ++i) {
227 if(rectcollision(base, (*i)->get_area())) {
228 (*i)->interaction(INTERACTION_ACTIVATE);
240 Player::level_begin()
244 previous_base = old_base = base;
249 player_input_init(&input);
251 invincible_timer.init(true);
252 skidding_timer.init(true);
253 safe_timer.init(true);
254 frame_timer.init(true);
255 growing_timer.init(true);
256 idle_timer.init(true);
262 Player::action(float elapsed_time)
264 bool jumped_in_solid = false;
266 if(dying && !dying_timer.check()) {
271 if (input.fire == UP)
272 holding_something = false;
275 previous_base = base;
277 /* --- HANDLE TUX! --- */
278 if(dying == DYING_NOT)
281 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
283 if(dying == DYING_NOT)
285 base_type target = base;
287 collision_swept_object_map(&old_base, &base);
289 if ((!invincible_timer.started() && !safe_timer.started())
290 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
291 || isspike(base.x, base.y + base.height)
292 || isspike(base.x + base.width, base.y + base.height)))
297 // Don't accelerate Tux if he is running against a wall
298 if (target.x != base.x)
300 physic.set_velocity_x(0);
303 // special exception for cases where we're stuck under tiles after
304 // being ducked. In this case we drift out
305 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
306 && collision_object_map(base))
308 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
309 previous_base = old_base = base;
315 physic.enable_gravity(true);
319 physic.set_velocity_y(0);
320 jumped_in_solid = true;
328 if (physic.get_velocity_y() < 0)
330 base.y = (int)(((int)base.y / 32) * 32);
331 physic.set_velocity_y(0);
334 physic.enable_gravity(false);
335 /* Reset score multiplier (for multi-hits): */
336 if (!invincible_timer.started())
337 player_status.score_multiplier = 1;
342 if (isbrick(base.x, base.y) ||
343 isfullbox(base.x, base.y))
345 Sector::current()->trygrabdistro(
346 Vector(base.x, base.y - 32), BOUNCE);
347 Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
349 Sector::current()->trybreakbrick(
350 Vector(base.x, base.y), size == SMALL);
352 bumpbrick(base.x, base.y);
353 Sector::current()->tryemptybox(Vector(base.x, base.y), RIGHT);
356 if (isbrick(base.x+ 31, base.y) ||
357 isfullbox(base.x+ 31, base.y))
359 Sector::current()->trygrabdistro(
360 Vector(base.x+ 31, base.y - 32), BOUNCE);
361 Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
364 Sector::current()->trybreakbrick(
365 Vector(base.x+ 31, base.y), size == SMALL);
367 bumpbrick(base.x+ 31, base.y);
368 Sector::current()->tryemptybox(Vector(base.x+ 31, base.y), LEFT);
380 /* Make sure jumping is off. */
387 /* ---- DONE HANDLING TUX! --- */
390 skidding_timer.check();
391 invincible_timer.check();
399 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
400 issolid(base.x + 1, base.y + base.height) ||
401 issolid(base.x + base.width - 1, base.y + base.height));
405 Player::under_solid()
407 return ( issolid(base.x + base.width / 2, base.y) ||
408 issolid(base.x + 1, base.y) ||
409 issolid(base.x + base.width - 1, base.y) );
413 Player::tiles_on_air(int tiles)
415 for(int t = 0; t != tiles; t++)
417 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
418 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
419 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
426 Player::handle_horizontal_input()
428 float vx = physic.get_velocity_x();
429 float vy = physic.get_velocity_y();
430 float ax = physic.get_acceleration_x();
431 float ay = physic.get_acceleration_y();
434 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
438 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
444 if (input.fire == UP) {
445 ax = dirsign * WALK_ACCELERATION_X;
447 if(vx >= MAX_WALK_XM && dirsign > 0) {
450 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
455 ax = dirsign * RUN_ACCELERATION_X;
457 if(vx >= MAX_RUN_XM && dirsign > 0) {
460 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
466 // we can reach WALK_SPEED without any acceleration
467 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
468 vx = dirsign * WALK_SPEED;
471 // changing directions?
472 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
473 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
474 skidding_timer.start(SKID_TIME);
475 SoundManager::get()->play_sound(IDToSound(SND_SKID));
482 // we get slower when not pressing any keys
484 if(fabs(vx) < WALK_SPEED) {
488 ax = WALK_ACCELERATION_X * 1.5;
490 ax = WALK_ACCELERATION_X * -1.5;
494 // if we're on ice slow down acceleration or deceleration
495 if (isice(base.x, base.y + base.height))
497 /* the acceleration/deceleration rate on ice is inversely proportional to
498 * the current velocity.
501 // increasing 1 will increase acceleration/deceleration rate
502 // decreasing 1 will decrease acceleration/deceleration rate
503 // must stay above zero, though
504 if (ax != 0) ax *= 1 / fabs(vx);
507 physic.set_velocity(vx, vy);
508 physic.set_acceleration(ax, ay);
512 Player::handle_vertical_input()
517 fall_mode = ON_GROUND;
518 last_ground_y = base.y;
520 if(base.y > last_ground_y)
522 else if(fall_mode == ON_GROUND)
527 if(input.up == DOWN && can_jump && on_ground())
529 if(duck) { // only jump a little bit when in duck mode {
530 physic.set_velocity_y(3);
532 // jump higher if we are running
533 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
534 physic.set_velocity_y(5.8);
536 physic.set_velocity_y(5.2);
545 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
547 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
549 // Let go of jump key
550 else if(input.up == UP)
552 if (!flapping && !duck && !falling_from_flap && !on_ground())
556 if (jumping && physic.get_velocity_y() > 0)
559 physic.set_velocity_y(0);
564 if (input.up == DOWN && can_flap)
566 if (!flapping_timer.started())
568 flapping_timer.start(TUX_FLAPPING_TIME);
570 if (!flapping_timer.check())
573 falling_from_flap = true;
577 if (flapping_timer.get_gone() <= TUX_FLAPPING_TIME)
579 physic.set_velocity_y((float)flapping_timer.get_gone()/450);
584 //(disabled by default, use cheat code "hover" to toggle on/off)
585 //TODO: needs some tweaking, especially when used together with double jump and jumping off badguys
586 if (enable_hover && input.up == DOWN && !jumping && !butt_jump && physic.get_velocity_y() <= 0)
588 physic.set_velocity_y(-1);
591 /* In case the player has pressed Down while in a certain range of air,
592 enable butt jump action */
593 if (input.down == DOWN && !butt_jump && !duck)
594 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
597 /* When Down is not held anymore, disable butt jump */
598 if(butt_jump && input.down == UP)
602 if (butt_jump && on_ground() && size == BIG)
606 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y));
608 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y + 32));
612 // Break bricks beneath Tux
613 if(Sector::current()->trybreakbrick(
614 Vector(base.x + 1, base.y + base.height), false)
615 || Sector::current()->trybreakbrick(
616 Vector(base.x + base.width - 1, base.y + base.height), false))
618 physic.set_velocity_y(2);
622 // Kill nearby badguys
623 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
624 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
625 i != gameobjects.end();
628 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
631 // don't kill when badguys are already dying or in a certain mode
632 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
633 badguy->mode != BadGuy::BOMB_EXPLODE)
635 if (fabsf(base.x - badguy->base.x) < 150 &&
636 fabsf(base.y - badguy->base.y) < 60 &&
637 (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
638 issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
645 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
646 issolid(base.x + 1, base.y + base.height + 64) ||
647 issolid(base.x + base.width - 1, base.y + base.height + 64))
651 && input.old_up == UP)
656 if(on_ground()) /* Make sure jumping is off. */
660 falling_from_flap = false;
661 if (flapping_timer.started()) {flapping_timer.stop();}
664 input.old_up = input.up;
668 Player::handle_input()
670 /* Handle horizontal movement: */
671 handle_horizontal_input();
675 if (on_ground() && input.up == UP)
677 handle_vertical_input();
680 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
682 if(Sector::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
683 physic.get_velocity_x(), dir))
684 shooting_timer.start(SHOOTING_TIME);
685 input.old_fire = DOWN;
688 /* tux animations: */
689 if(!frame_timer.check())
691 frame_timer.start(25);
692 if (input.right == UP && input.left == UP)
699 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
700 (global_frame_counter % 4) == 0)
701 frame_main = (frame_main + 1) % 4;
711 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
716 // changing base size confuses collision otherwise
717 old_base = previous_base = base;
719 else if(input.down == UP && size == BIG && duck)
721 // try if we can really unduck
724 // when unducking in air we need some space to do so
725 if(on_ground() || !collision_object_map(base)) {
727 // changing base size confuses collision otherwise
728 old_base = previous_base = base;
730 // undo the ducking changes
738 Player::grow(bool animate)
748 growing_timer.start(GROWING_TIME);
750 old_base = previous_base = base;
754 Player::grabdistros()
759 Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
760 Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
761 Sector::current()->trygrabdistro(
762 Vector(base.x, base.y + base.height), NO_BOUNCE);
763 Sector::current()->trygrabdistro(
764 Vector(base.x+ 31, base.y + base.height), NO_BOUNCE);
768 Sector::current()->trygrabdistro(
769 Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
770 Sector::current()->trygrabdistro(
771 Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
776 /* Enough distros for a One-up? */
777 if (player_status.distros >= DISTROS_LIFEUP)
779 player_status.distros = player_status.distros - DISTROS_LIFEUP;
780 if(player_status.lives < MAX_LIVES)
781 ++player_status.lives;
782 /*We want to hear the sound even, if MAX_LIVES is reached*/
783 SoundManager::get()->play_sound(IDToSound(SND_LIFEUP));
788 Player::draw(DrawingContext& context)
790 TuxBodyParts* tux_body;
793 tux_body = small_tux;
794 else if (got_power == FIRE_POWER)
796 else if (got_power == ICE_POWER)
801 int layer = LAYER_OBJECTS - 1;
802 Vector pos = Vector(base.x, base.y);
804 /* Set Tux sprite action */
805 if (duck && size == BIG)
808 tux_body->set_action("duck-left");
810 tux_body->set_action("duck-right");
812 else if (skidding_timer.started())
815 tux_body->set_action("skid-left");
817 tux_body->set_action("skid-right");
819 else if (kick_timer.started())
822 tux_body->set_action("kick-left");
824 tux_body->set_action("kick-right");
829 tux_body->set_action("buttjump-left");
831 tux_body->set_action("buttjump-right");
833 else if (physic.get_velocity_y() != 0)
836 tux_body->set_action("jump-left");
838 tux_body->set_action("jump-right");
842 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
845 tux_body->set_action("stand-left");
847 tux_body->set_action("stand-right");
852 tux_body->set_action("walk-left");
854 tux_body->set_action("walk-right");
858 if(idle_timer.get_left() < 0)
863 tux_body->head->set_action("idle-left");
865 tux_body->head->set_action("idle-right");
867 tux_body->head->start_animation(1);
870 idle_timer.start(IDLE_TIME);
873 // Tux is holding something
874 if ((holding_something && physic.get_velocity_y() == 0) ||
875 shooting_timer.check())
880 tux_body->arms->set_action("duck+grab-left");
882 tux_body->arms->set_action("duck+grab-right");
887 tux_body->arms->set_action("grab-left");
889 tux_body->arms->set_action("grab-right");
894 if (dying == DYING_SQUISHED)
896 smalltux_gameover->draw(context, pos, LAYER_FOREGROUNDTILES+1);
898 else if(growing_timer.check())
903 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
904 ((growing_timer.get_gone() *
905 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
907 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
908 ((growing_timer.get_gone() *
909 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
914 context.draw_surface(growingtux_right[(growing_timer.get_gone() *
915 GROWING_FRAMES) / GROWING_TIME], pos, layer);
917 context.draw_surface(growingtux_left[(growing_timer.get_gone() *
918 GROWING_FRAMES) / GROWING_TIME], pos, layer);
921 else if (safe_timer.started() && global_frame_counter%2)
924 tux_body->draw(context, pos, layer, dir == LEFT ? HORIZONTAL_FLIP : NONE_EFFECT);
926 // Draw blinking star overlay
927 if (invincible_timer.started() &&
928 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3)
931 if (size == SMALL || duck)
932 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
934 bigtux_star->draw(context, pos, LAYER_OBJECTS + 2);
938 context.draw_filled_rect(Vector(base.x, base.y),
939 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
943 Player::collision(const MovingObject& other, int collision_type)
946 (void) collision_type;
947 // will be implemented later
951 Player::collision(void* p_c_object, int c_object)
953 BadGuy* pbad_c = NULL;
954 Trampoline* ptramp_c = NULL;
955 FlyingPlatform* pplatform_c = NULL;
960 pbad_c = (BadGuy*) p_c_object;
962 /* Hurt player if he touches a badguy */
963 if (!pbad_c->dying && !dying &&
964 !safe_timer.started() &&
965 pbad_c->mode != BadGuy::HELD)
967 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
968 && !holding_something)
970 holding_something = true;
971 pbad_c->mode = BadGuy::HELD;
974 else if (pbad_c->mode == BadGuy::FLAT)
976 // Don't get hurt if we're kicking a flat badguy!
978 else if (pbad_c->mode == BadGuy::KICK)
980 /* Hurt if you get hit by kicked laptop: */
981 if (!invincible_timer.started())
988 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
989 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
990 || pbad_c->kind == BAD_SPIKY))
994 if (!invincible_timer.started())
1000 pbad_c->kill_me(25);
1003 player_status.score_multiplier++;
1008 ptramp_c = (Trampoline*) p_c_object;
1010 // Pick up trampoline
1011 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
1013 holding_something = true;
1014 ptramp_c->mode = Trampoline::M_HELD;
1015 ptramp_c->base.y -= 8;
1017 // Set down trampoline
1018 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
1020 holding_something = false;
1021 ptramp_c->mode = Trampoline::M_NORMAL;
1022 ptramp_c->base.y += 8;
1023 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
1026 // ptramp_c->base.x = base.x + base.width+1;
1028 // ptramp_c->base.x = base.x - base.width-1;
1031 // Don't let tux walk through trampoline
1032 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
1034 if (physic.get_velocity_x() > 0) // RIGHT
1036 physic.set_velocity_x(0);
1037 base.x = ptramp_c->base.x - base.width;
1039 else if (physic.get_velocity_x() < 0) // LEFT
1041 physic.set_velocity_x(0);
1042 base.x = ptramp_c->base.x + ptramp_c->base.width;
1047 case CO_FLYING_PLATFORM:
1048 pplatform_c = (FlyingPlatform*) p_c_object;
1050 base.y = pplatform_c->base.y - base.height;
1051 physic.set_velocity_x(pplatform_c->get_vel_x());
1053 physic.enable_gravity(false);
1055 fall_mode = ON_GROUND;
1067 Player::kill(HurtMode mode)
1072 SoundManager::get()->play_sound(IDToSound(SND_HURT));
1074 physic.set_velocity_x(0);
1076 if (mode == SHRINK && size == BIG)
1078 if (got_power != NONE_POWER)
1080 safe_timer.start(TUX_SAFE_TIME);
1081 got_power = NONE_POWER;
1085 growing_timer.start(GROWING_TIME);
1086 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
1094 physic.enable_gravity(true);
1095 physic.set_acceleration(0, 0);
1096 physic.set_velocity(0, 7);
1097 --player_status.lives;
1098 dying = DYING_SQUISHED;
1099 dying_timer.start(3000);
1103 /* Remove Tux's power ups */
1105 Player::remove_powerups()
1107 got_power = NONE_POWER;
1113 Player::move(const Vector& vector)
1117 old_base = previous_base = base;
1121 Player::check_bounds(Camera* camera)
1123 /* Keep tux in bounds: */
1125 { // Lock Tux to the size of the level, so that he doesn't fall of
1130 /* Keep in-bounds, vertically: */
1131 if (base.y > Sector::current()->solids->get_height() * 32)
1137 bool adjust = false;
1138 // can happen if back scrolling is disabled
1139 if(base.x < camera->get_translation().x) {
1140 base.x = camera->get_translation().x;
1143 if(base.x >= camera->get_translation().x + screen->w - base.width) {
1144 base.x = camera->get_translation().x + screen->w - base.width;
1150 if(collision_object_map(base)) {
1158 Player::bounce(BadGuy* badguy)
1160 //Make sure we stopped flapping
1162 falling_from_flap = false;
1165 physic.set_velocity_y(5.2);
1167 physic.set_velocity_y(2);
1169 // Move the player a little bit above the badguy to avoid collision
1170 // between badguy and player directly after the bounce has happend
1171 base.y = badguy->base.y - base.height - 2;