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.
25 #include "app/globals.h"
30 #include "special/sprite.h"
35 #include "resources.h"
36 #include "interactive_object.h"
37 #include "video/screen.h"
38 #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;
161 double_jumping = false;
163 can_double_jump = false;
164 enable_hover = false;
170 player_input_init(&input);
172 invincible_timer.init(true);
173 skidding_timer.init(true);
174 safe_timer.init(true);
175 frame_timer.init(true);
176 kick_timer.init(true);
177 shooting_timer.init(true);
178 growing_timer.init(true);
179 idle_timer.init(true);
185 Player::key_event(SDLKey key, int state)
187 idle_timer.start(IDLE_TIME);
189 if(key == keymap.right)
194 else if(key == keymap.left)
199 else if(key == keymap.jump)
204 else if(key == keymap.duck)
209 else if(key == keymap.fire)
216 else if(key == keymap.activate)
218 input.activate = state;
221 /** check for interactive objects */
222 for(Sector::InteractiveObjects::iterator i
223 = Sector::current()->interactive_objects.begin();
224 i != Sector::current()->interactive_objects.end(); ++i) {
225 if(rectcollision(base, (*i)->get_area())) {
226 (*i)->interaction(INTERACTION_ACTIVATE);
238 Player::level_begin()
242 previous_base = old_base = base;
247 player_input_init(&input);
249 invincible_timer.init(true);
250 skidding_timer.init(true);
251 safe_timer.init(true);
252 frame_timer.init(true);
253 growing_timer.init(true);
254 idle_timer.init(true);
260 Player::action(float elapsed_time)
262 bool jumped_in_solid = false;
264 if(dying && !dying_timer.check()) {
269 if (input.fire == UP)
270 holding_something = false;
273 previous_base = base;
275 /* --- HANDLE TUX! --- */
276 if(dying == DYING_NOT)
279 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
281 if(dying == DYING_NOT)
283 base_type target = base;
285 collision_swept_object_map(&old_base, &base);
287 if ((!invincible_timer.started() && !safe_timer.started())
288 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
289 || isspike(base.x, base.y + base.height)
290 || isspike(base.x + base.width, base.y + base.height)))
295 // Don't accelerate Tux if he is running against a wall
296 if (target.x != base.x)
298 physic.set_velocity_x(0);
301 // special exception for cases where we're stuck under tiles after
302 // being ducked. In this case we drift out
303 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
304 && collision_object_map(base))
306 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
307 previous_base = old_base = base;
313 physic.enable_gravity(true);
317 physic.set_velocity_y(0);
318 jumped_in_solid = true;
325 if (physic.get_velocity_y() < 0)
327 base.y = (int)(((int)base.y / 32) * 32);
328 physic.set_velocity_y(0);
331 physic.enable_gravity(false);
332 /* Reset score multiplier (for multi-hits): */
333 if (!invincible_timer.started())
334 player_status.score_multiplier = 1;
339 if (isbrick(base.x, base.y) ||
340 isfullbox(base.x, base.y))
342 Sector::current()->trygrabdistro(
343 Vector(base.x, base.y - 32), BOUNCE);
344 Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
346 Sector::current()->trybreakbrick(
347 Vector(base.x, base.y), size == SMALL);
349 bumpbrick(base.x, base.y);
350 Sector::current()->tryemptybox(Vector(base.x, base.y), RIGHT);
353 if (isbrick(base.x+ 31, base.y) ||
354 isfullbox(base.x+ 31, base.y))
356 Sector::current()->trygrabdistro(
357 Vector(base.x+ 31, base.y - 32), BOUNCE);
358 Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
361 Sector::current()->trybreakbrick(
362 Vector(base.x+ 31, base.y), size == SMALL);
364 bumpbrick(base.x+ 31, base.y);
365 Sector::current()->tryemptybox(Vector(base.x+ 31, base.y), LEFT);
377 /* Make sure jumping is off. */
383 /* ---- DONE HANDLING TUX! --- */
386 skidding_timer.check();
387 invincible_timer.check();
395 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
396 issolid(base.x + 1, base.y + base.height) ||
397 issolid(base.x + base.width - 1, base.y + base.height));
401 Player::under_solid()
403 return ( issolid(base.x + base.width / 2, base.y) ||
404 issolid(base.x + 1, base.y) ||
405 issolid(base.x + base.width - 1, base.y) );
409 Player::tiles_on_air(int tiles)
411 for(int t = 0; t != tiles; t++)
413 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
414 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
415 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
422 Player::handle_horizontal_input()
424 float vx = physic.get_velocity_x();
425 float vy = physic.get_velocity_y();
426 float ax = physic.get_acceleration_x();
427 float ay = physic.get_acceleration_y();
430 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
434 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
440 if (input.fire == UP) {
441 ax = dirsign * WALK_ACCELERATION_X;
443 if(vx >= MAX_WALK_XM && dirsign > 0) {
446 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
451 ax = dirsign * RUN_ACCELERATION_X;
453 if(vx >= MAX_RUN_XM && dirsign > 0) {
456 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
462 // we can reach WALK_SPEED without any acceleration
463 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
464 vx = dirsign * WALK_SPEED;
467 // changing directions?
468 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
469 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
470 skidding_timer.start(SKID_TIME);
471 SoundManager::get()->play_sound(IDToSound(SND_SKID));
478 // we get slower when not pressing any keys
480 if(fabs(vx) < WALK_SPEED) {
484 ax = WALK_ACCELERATION_X * 1.5;
486 ax = WALK_ACCELERATION_X * -1.5;
490 // if we're on ice slow down acceleration or deceleration
491 if (isice(base.x, base.y + base.height))
493 /* the acceleration/deceleration rate on ice is inversely proportional to
494 * the current velocity.
497 // increasing 1 will increase acceleration/deceleration rate
498 // decreasing 1 will decrease acceleration/deceleration rate
499 // must stay above zero, though
500 if (ax != 0) ax *= 1 / fabs(vx);
503 physic.set_velocity(vx, vy);
504 physic.set_acceleration(ax, ay);
508 Player::handle_vertical_input()
512 fall_mode = ON_GROUND;
513 last_ground_y = base.y;
515 if(base.y > last_ground_y)
517 else if(fall_mode == ON_GROUND)
522 if(input.up == DOWN && can_jump && on_ground())
524 global_stats.add_points(JUMPS_STAT, 1);
526 if(duck) { // only jump a little bit when in duck mode {
527 physic.set_velocity_y(3);
529 // jump higher if we are running
530 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
531 physic.set_velocity_y(5.8);
533 physic.set_velocity_y(5.2);
538 double_jumping = false;
540 can_double_jump = false;
542 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
544 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
546 // Let go of jump key
547 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
549 if (!double_jumping && !duck) {can_double_jump = true;}
551 physic.set_velocity_y(0);
555 if (input.up == DOWN && can_double_jump)
557 can_double_jump = false;
559 double_jumping = true;
560 physic.set_velocity_y(5.2);
564 //(disabled by default, use cheat code "hover" to toggle on/off)
565 //TODO: needs some tweaking, especially when used together with double jump and jumping off badguys
566 if (enable_hover && input.up == DOWN && !jumping && !butt_jump && physic.get_velocity_y() <= 0)
568 physic.set_velocity_y(-1);
571 /* In case the player has pressed Down while in a certain range of air,
572 enable butt jump action */
573 if (input.down == DOWN && !butt_jump && !duck)
574 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
577 /* When Down is not held anymore, disable butt jump */
578 if(butt_jump && input.down == UP)
582 if (butt_jump && on_ground() && size == BIG)
586 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y));
588 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y + 32));
592 // Break bricks beneath Tux
593 if(Sector::current()->trybreakbrick(
594 Vector(base.x + 1, base.y + base.height), false)
595 || Sector::current()->trybreakbrick(
596 Vector(base.x + base.width - 1, base.y + base.height), false))
598 physic.set_velocity_y(2);
602 // Kill nearby badguys
603 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
604 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
605 i != gameobjects.end();
608 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
612 if (fabsf(base.x - badguy->base.x) < 150 &&
613 fabsf(base.y - badguy->base.y) < 60 &&
614 (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
615 issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
621 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
622 issolid(base.x + 1, base.y + base.height + 64) ||
623 issolid(base.x + base.width - 1, base.y + base.height + 64))
627 && input.old_up == UP)
632 if(on_ground()) /* Make sure jumping is off. */
635 input.old_up = input.up;
639 Player::handle_input()
641 /* Handle horizontal movement: */
642 handle_horizontal_input();
646 if (on_ground() && input.up == UP)
648 handle_vertical_input();
651 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
653 if(Sector::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
654 physic.get_velocity_x(), dir))
655 shooting_timer.start(SHOOTING_TIME);
656 input.old_fire = DOWN;
659 /* tux animations: */
660 if(!frame_timer.check())
662 frame_timer.start(25);
663 if (input.right == UP && input.left == UP)
670 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
671 (global_frame_counter % 4) == 0)
672 frame_main = (frame_main + 1) % 4;
682 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
687 // changing base size confuses collision otherwise
688 old_base = previous_base = base;
690 else if(input.down == UP && size == BIG && duck)
692 // try if we can really unduck
695 // when unducking in air we need some space to do so
696 if(on_ground() || !collision_object_map(base)) {
698 // changing base size confuses collision otherwise
699 old_base = previous_base = base;
701 // undo the ducking changes
709 Player::grow(bool animate)
719 growing_timer.start(GROWING_TIME);
721 old_base = previous_base = base;
725 Player::grabdistros()
730 Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
731 Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
732 Sector::current()->trygrabdistro(
733 Vector(base.x, base.y + base.height), NO_BOUNCE);
734 Sector::current()->trygrabdistro(
735 Vector(base.x+ 31, base.y + base.height), NO_BOUNCE);
739 Sector::current()->trygrabdistro(
740 Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
741 Sector::current()->trygrabdistro(
742 Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
747 /* Enough distros for a One-up? */
748 if (player_status.distros >= DISTROS_LIFEUP)
750 player_status.distros = player_status.distros - DISTROS_LIFEUP;
751 if(player_status.lives < MAX_LIVES)
752 ++player_status.lives;
753 /*We want to hear the sound even, if MAX_LIVES is reached*/
754 SoundManager::get()->play_sound(IDToSound(SND_LIFEUP));
759 Player::draw(DrawingContext& context)
761 TuxBodyParts* tux_body;
764 tux_body = small_tux;
765 else if (got_power == FIRE_POWER)
767 else if (got_power == ICE_POWER)
772 int layer = LAYER_OBJECTS - 1;
773 Vector pos = Vector(base.x, base.y);
775 /* Set Tux sprite action */
776 if (duck && size == BIG)
779 tux_body->set_action("duck-left");
781 tux_body->set_action("duck-right");
783 else if (skidding_timer.started())
786 tux_body->set_action("skid-left");
788 tux_body->set_action("skid-right");
790 else if (kick_timer.started())
793 tux_body->set_action("kick-left");
795 tux_body->set_action("kick-right");
800 tux_body->set_action("buttjump-left");
802 tux_body->set_action("buttjump-right");
804 else if (physic.get_velocity_y() != 0)
807 tux_body->set_action("jump-left");
809 tux_body->set_action("jump-right");
813 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
816 tux_body->set_action("stand-left");
818 tux_body->set_action("stand-right");
823 tux_body->set_action("walk-left");
825 tux_body->set_action("walk-right");
829 if(idle_timer.get_left() < 0)
834 tux_body->head->set_action("idle-left");
836 tux_body->head->set_action("idle-right");
838 tux_body->head->start_animation(1);
841 idle_timer.start(IDLE_TIME);
844 // Tux is holding something
845 if ((holding_something && physic.get_velocity_y() == 0) ||
846 shooting_timer.check())
851 tux_body->arms->set_action("duck+grab-left");
853 tux_body->arms->set_action("duck+grab-right");
858 tux_body->arms->set_action("grab-left");
860 tux_body->arms->set_action("grab-right");
865 if (dying == DYING_SQUISHED)
867 smalltux_gameover->draw(context, pos, LAYER_FOREGROUNDTILES+1);
869 else if(growing_timer.check())
874 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
875 ((growing_timer.get_gone() *
876 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
878 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
879 ((growing_timer.get_gone() *
880 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
885 context.draw_surface(growingtux_right[(growing_timer.get_gone() *
886 GROWING_FRAMES) / GROWING_TIME], pos, layer);
888 context.draw_surface(growingtux_left[(growing_timer.get_gone() *
889 GROWING_FRAMES) / GROWING_TIME], pos, layer);
892 else if (safe_timer.started() && global_frame_counter%2)
895 tux_body->draw(context, pos, layer, dir == LEFT ? HORIZONTAL_FLIP : NONE_EFFECT);
897 // Draw blinking star overlay
898 if (invincible_timer.started() &&
899 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3)
902 if (size == SMALL || duck)
903 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
905 bigtux_star->draw(context, pos, LAYER_OBJECTS + 2);
909 context.draw_filled_rect(Vector(base.x, base.y),
910 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
914 Player::collision(const MovingObject& other, int collision_type)
917 (void) collision_type;
918 // will be implemented later
922 Player::collision(void* p_c_object, int c_object)
924 BadGuy* pbad_c = NULL;
925 Trampoline* ptramp_c = NULL;
926 FlyingPlatform* pplatform_c = NULL;
931 pbad_c = (BadGuy*) p_c_object;
933 /* Hurt player if he touches a badguy */
934 if (!pbad_c->dying && !dying &&
935 !safe_timer.started() &&
936 pbad_c->mode != BadGuy::HELD)
938 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
939 && !holding_something)
941 holding_something = true;
942 pbad_c->mode = BadGuy::HELD;
945 else if (pbad_c->mode == BadGuy::FLAT)
947 // Don't get hurt if we're kicking a flat badguy!
949 else if (pbad_c->mode == BadGuy::KICK)
951 /* Hurt if you get hit by kicked laptop: */
952 if (!invincible_timer.started())
959 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
960 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
961 || pbad_c->kind == BAD_SPIKY))
965 if (!invincible_timer.started())
974 player_status.score_multiplier++;
979 ptramp_c = (Trampoline*) p_c_object;
981 // Pick up trampoline
982 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
984 holding_something = true;
985 ptramp_c->mode = Trampoline::M_HELD;
986 ptramp_c->base.y -= 8;
988 // Set down trampoline
989 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
991 holding_something = false;
992 ptramp_c->mode = Trampoline::M_NORMAL;
993 ptramp_c->base.y += 8;
994 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
997 // ptramp_c->base.x = base.x + base.width+1;
999 // ptramp_c->base.x = base.x - base.width-1;
1002 // Don't let tux walk through trampoline
1003 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
1005 if (physic.get_velocity_x() > 0) // RIGHT
1007 physic.set_velocity_x(0);
1008 base.x = ptramp_c->base.x - base.width;
1010 else if (physic.get_velocity_x() < 0) // LEFT
1012 physic.set_velocity_x(0);
1013 base.x = ptramp_c->base.x + ptramp_c->base.width;
1018 case CO_FLYING_PLATFORM:
1019 pplatform_c = (FlyingPlatform*) p_c_object;
1021 base.y = pplatform_c->base.y - base.height;
1022 physic.set_velocity_x(pplatform_c->get_vel_x());
1024 physic.enable_gravity(false);
1026 fall_mode = ON_GROUND;
1038 Player::kill(HurtMode mode)
1043 SoundManager::get()->play_sound(IDToSound(SND_HURT));
1045 physic.set_velocity_x(0);
1047 if (mode == SHRINK && size == BIG)
1049 if (got_power != NONE_POWER)
1051 safe_timer.start(TUX_SAFE_TIME);
1052 got_power = NONE_POWER;
1056 growing_timer.start(GROWING_TIME);
1057 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
1065 physic.enable_gravity(true);
1066 physic.set_acceleration(0, 0);
1067 physic.set_velocity(0, 7);
1068 --player_status.lives;
1069 dying = DYING_SQUISHED;
1070 dying_timer.start(3000);
1074 /* Remove Tux's power ups */
1076 Player::remove_powerups()
1078 got_power = NONE_POWER;
1084 Player::move(const Vector& vector)
1088 old_base = previous_base = base;
1092 Player::check_bounds(Camera* camera)
1094 /* Keep tux in bounds: */
1096 { // Lock Tux to the size of the level, so that he doesn't fall of
1101 /* Keep in-bounds, vertically: */
1102 if (base.y > Sector::current()->solids->get_height() * 32)
1108 bool adjust = false;
1109 // can happen if back scrolling is disabled
1110 if(base.x < camera->get_translation().x) {
1111 base.x = camera->get_translation().x;
1114 if(base.x >= camera->get_translation().x + screen->w - base.width) {
1115 base.x = camera->get_translation().x + screen->w - base.width;
1121 if(collision_object_map(base)) {
1129 Player::bounce(BadGuy* badguy)
1132 physic.set_velocity_y(5.2);
1134 physic.set_velocity_y(2);
1136 // Move the player a little bit above the badguy to avoid collision
1137 // between badguy and player directly after the bounce has happend
1138 base.y = badguy->base.y - base.height - 2;