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;
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);
180 flapping_timer.init(true);
186 Player::key_event(SDLKey key, int state)
188 idle_timer.start(IDLE_TIME);
190 if(key == keymap.right)
195 else if(key == keymap.left)
200 else if(key == keymap.jump)
205 else if(key == keymap.duck)
210 else if(key == keymap.fire)
217 else if(key == keymap.activate)
219 input.activate = state;
222 /** check for interactive objects */
223 for(Sector::InteractiveObjects::iterator i
224 = Sector::current()->interactive_objects.begin();
225 i != Sector::current()->interactive_objects.end(); ++i) {
226 if(rectcollision(base, (*i)->get_area())) {
227 (*i)->interaction(INTERACTION_ACTIVATE);
239 Player::level_begin()
243 previous_base = old_base = base;
248 player_input_init(&input);
250 invincible_timer.init(true);
251 skidding_timer.init(true);
252 safe_timer.init(true);
253 frame_timer.init(true);
254 growing_timer.init(true);
255 idle_timer.init(true);
261 Player::action(float elapsed_time)
263 bool jumped_in_solid = false;
265 if(dying && !dying_timer.check()) {
270 if (input.fire == UP)
271 holding_something = false;
274 previous_base = base;
276 /* --- HANDLE TUX! --- */
277 if(dying == DYING_NOT)
280 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
282 if(dying == DYING_NOT)
284 base_type target = base;
286 collision_swept_object_map(&old_base, &base);
288 if ((!invincible_timer.started() && !safe_timer.started())
289 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
290 || isspike(base.x, base.y + base.height)
291 || isspike(base.x + base.width, base.y + base.height)))
296 // Don't accelerate Tux if he is running against a wall
297 if (target.x != base.x)
299 physic.set_velocity_x(0);
302 // special exception for cases where we're stuck under tiles after
303 // being ducked. In this case we drift out
304 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
305 && collision_object_map(base))
307 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
308 previous_base = old_base = base;
314 physic.enable_gravity(true);
318 physic.set_velocity_y(0);
319 jumped_in_solid = true;
326 if (physic.get_velocity_y() < 0)
328 base.y = (int)(((int)base.y / 32) * 32);
329 physic.set_velocity_y(0);
332 physic.enable_gravity(false);
333 /* Reset score multiplier (for multi-hits): */
334 if (!invincible_timer.started())
335 player_status.score_multiplier = 1;
340 if (isbrick(base.x, base.y) ||
341 isfullbox(base.x, base.y))
343 Sector::current()->trygrabdistro(
344 Vector(base.x, base.y - 32), BOUNCE);
345 Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
347 Sector::current()->trybreakbrick(
348 Vector(base.x, base.y), size == SMALL);
350 bumpbrick(base.x, base.y);
351 Sector::current()->tryemptybox(Vector(base.x, base.y), RIGHT);
354 if (isbrick(base.x+ 31, base.y) ||
355 isfullbox(base.x+ 31, base.y))
357 Sector::current()->trygrabdistro(
358 Vector(base.x+ 31, base.y - 32), BOUNCE);
359 Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
362 Sector::current()->trybreakbrick(
363 Vector(base.x+ 31, base.y), size == SMALL);
365 bumpbrick(base.x+ 31, base.y);
366 Sector::current()->tryemptybox(Vector(base.x+ 31, base.y), LEFT);
378 /* Make sure jumping is off. */
384 /* ---- DONE HANDLING TUX! --- */
387 skidding_timer.check();
388 invincible_timer.check();
396 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
397 issolid(base.x + 1, base.y + base.height) ||
398 issolid(base.x + base.width - 1, base.y + base.height));
402 Player::under_solid()
404 return ( issolid(base.x + base.width / 2, base.y) ||
405 issolid(base.x + 1, base.y) ||
406 issolid(base.x + base.width - 1, base.y) );
410 Player::tiles_on_air(int tiles)
412 for(int t = 0; t != tiles; t++)
414 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
415 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
416 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
423 Player::handle_horizontal_input()
425 float vx = physic.get_velocity_x();
426 float vy = physic.get_velocity_y();
427 float ax = physic.get_acceleration_x();
428 float ay = physic.get_acceleration_y();
431 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
435 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
441 if (input.fire == UP) {
442 ax = dirsign * WALK_ACCELERATION_X;
444 if(vx >= MAX_WALK_XM && dirsign > 0) {
447 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
452 ax = dirsign * RUN_ACCELERATION_X;
454 if(vx >= MAX_RUN_XM && dirsign > 0) {
457 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
463 // we can reach WALK_SPEED without any acceleration
464 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
465 vx = dirsign * WALK_SPEED;
468 // changing directions?
469 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
470 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
471 skidding_timer.start(SKID_TIME);
472 SoundManager::get()->play_sound(IDToSound(SND_SKID));
479 // we get slower when not pressing any keys
481 if(fabs(vx) < WALK_SPEED) {
485 ax = WALK_ACCELERATION_X * 1.5;
487 ax = WALK_ACCELERATION_X * -1.5;
491 // if we're on ice slow down acceleration or deceleration
492 if (isice(base.x, base.y + base.height))
494 /* the acceleration/deceleration rate on ice is inversely proportional to
495 * the current velocity.
498 // increasing 1 will increase acceleration/deceleration rate
499 // decreasing 1 will decrease acceleration/deceleration rate
500 // must stay above zero, though
501 if (ax != 0) ax *= 1 / fabs(vx);
504 physic.set_velocity(vx, vy);
505 physic.set_acceleration(ax, ay);
509 Player::handle_vertical_input()
513 fall_mode = ON_GROUND;
514 last_ground_y = base.y;
516 if(base.y > last_ground_y)
518 else if(fall_mode == ON_GROUND)
523 if(input.up == DOWN && can_jump && on_ground())
525 if(duck) { // only jump a little bit when in duck mode {
526 physic.set_velocity_y(3);
528 // jump higher if we are running
529 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
530 physic.set_velocity_y(5.8);
532 physic.set_velocity_y(5.2);
541 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
543 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
545 // Let go of jump key
546 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
548 if (!flapping && !duck)
553 physic.set_velocity_y(0);
557 if (input.up == DOWN && can_flap)
559 if (!flapping_timer.started()) {flapping_timer.start(TUX_FLAPPING_TIME);}
560 if (!flapping_timer.check()) {can_flap = false;}
563 if (flapping_timer.get_gone() <= TUX_FLAPPING_TIME)
565 physic.set_velocity_y((float)flapping_timer.get_gone()/450);
570 //(disabled by default, use cheat code "hover" to toggle on/off)
571 //TODO: needs some tweaking, especially when used together with double jump and jumping off badguys
572 if (enable_hover && input.up == DOWN && !jumping && !butt_jump && physic.get_velocity_y() <= 0)
574 physic.set_velocity_y(-1);
577 /* In case the player has pressed Down while in a certain range of air,
578 enable butt jump action */
579 if (input.down == DOWN && !butt_jump && !duck)
580 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
583 /* When Down is not held anymore, disable butt jump */
584 if(butt_jump && input.down == UP)
588 if (butt_jump && on_ground() && size == BIG)
592 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y));
594 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y + 32));
598 // Break bricks beneath Tux
599 if(Sector::current()->trybreakbrick(
600 Vector(base.x + 1, base.y + base.height), false)
601 || Sector::current()->trybreakbrick(
602 Vector(base.x + base.width - 1, base.y + base.height), false))
604 physic.set_velocity_y(2);
608 // Kill nearby badguys
609 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
610 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
611 i != gameobjects.end();
614 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
618 if (fabsf(base.x - badguy->base.x) < 150 &&
619 fabsf(base.y - badguy->base.y) < 60 &&
620 (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
621 issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
627 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
628 issolid(base.x + 1, base.y + base.height + 64) ||
629 issolid(base.x + base.width - 1, base.y + base.height + 64))
633 && input.old_up == UP)
638 if(on_ground()) /* Make sure jumping is off. */
641 input.old_up = input.up;
645 Player::handle_input()
647 /* Handle horizontal movement: */
648 handle_horizontal_input();
652 if (on_ground() && input.up == UP)
654 handle_vertical_input();
657 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
659 if(Sector::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
660 physic.get_velocity_x(), dir))
661 shooting_timer.start(SHOOTING_TIME);
662 input.old_fire = DOWN;
665 /* tux animations: */
666 if(!frame_timer.check())
668 frame_timer.start(25);
669 if (input.right == UP && input.left == UP)
676 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
677 (global_frame_counter % 4) == 0)
678 frame_main = (frame_main + 1) % 4;
688 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
693 // changing base size confuses collision otherwise
694 old_base = previous_base = base;
696 else if(input.down == UP && size == BIG && duck)
698 // try if we can really unduck
701 // when unducking in air we need some space to do so
702 if(on_ground() || !collision_object_map(base)) {
704 // changing base size confuses collision otherwise
705 old_base = previous_base = base;
707 // undo the ducking changes
715 Player::grow(bool animate)
725 growing_timer.start(GROWING_TIME);
727 old_base = previous_base = base;
731 Player::grabdistros()
736 Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
737 Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
738 Sector::current()->trygrabdistro(
739 Vector(base.x, base.y + base.height), NO_BOUNCE);
740 Sector::current()->trygrabdistro(
741 Vector(base.x+ 31, base.y + base.height), NO_BOUNCE);
745 Sector::current()->trygrabdistro(
746 Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
747 Sector::current()->trygrabdistro(
748 Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
753 /* Enough distros for a One-up? */
754 if (player_status.distros >= DISTROS_LIFEUP)
756 player_status.distros = player_status.distros - DISTROS_LIFEUP;
757 if(player_status.lives < MAX_LIVES)
758 ++player_status.lives;
759 /*We want to hear the sound even, if MAX_LIVES is reached*/
760 SoundManager::get()->play_sound(IDToSound(SND_LIFEUP));
765 Player::draw(DrawingContext& context)
767 TuxBodyParts* tux_body;
770 tux_body = small_tux;
771 else if (got_power == FIRE_POWER)
773 else if (got_power == ICE_POWER)
778 int layer = LAYER_OBJECTS - 1;
779 Vector pos = Vector(base.x, base.y);
781 /* Set Tux sprite action */
782 if (duck && size == BIG)
785 tux_body->set_action("duck-left");
787 tux_body->set_action("duck-right");
789 else if (skidding_timer.started())
792 tux_body->set_action("skid-left");
794 tux_body->set_action("skid-right");
796 else if (kick_timer.started())
799 tux_body->set_action("kick-left");
801 tux_body->set_action("kick-right");
806 tux_body->set_action("buttjump-left");
808 tux_body->set_action("buttjump-right");
810 else if (physic.get_velocity_y() != 0)
813 tux_body->set_action("jump-left");
815 tux_body->set_action("jump-right");
819 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
822 tux_body->set_action("stand-left");
824 tux_body->set_action("stand-right");
829 tux_body->set_action("walk-left");
831 tux_body->set_action("walk-right");
835 if(idle_timer.get_left() < 0)
840 tux_body->head->set_action("idle-left");
842 tux_body->head->set_action("idle-right");
844 tux_body->head->start_animation(1);
847 idle_timer.start(IDLE_TIME);
850 // Tux is holding something
851 if ((holding_something && physic.get_velocity_y() == 0) ||
852 shooting_timer.check())
857 tux_body->arms->set_action("duck+grab-left");
859 tux_body->arms->set_action("duck+grab-right");
864 tux_body->arms->set_action("grab-left");
866 tux_body->arms->set_action("grab-right");
871 if (dying == DYING_SQUISHED)
873 smalltux_gameover->draw(context, pos, LAYER_FOREGROUNDTILES+1);
875 else if(growing_timer.check())
880 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
881 ((growing_timer.get_gone() *
882 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
884 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
885 ((growing_timer.get_gone() *
886 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
891 context.draw_surface(growingtux_right[(growing_timer.get_gone() *
892 GROWING_FRAMES) / GROWING_TIME], pos, layer);
894 context.draw_surface(growingtux_left[(growing_timer.get_gone() *
895 GROWING_FRAMES) / GROWING_TIME], pos, layer);
898 else if (safe_timer.started() && global_frame_counter%2)
901 tux_body->draw(context, pos, layer, dir == LEFT ? HORIZONTAL_FLIP : NONE_EFFECT);
903 // Draw blinking star overlay
904 if (invincible_timer.started() &&
905 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3)
908 if (size == SMALL || duck)
909 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
911 bigtux_star->draw(context, pos, LAYER_OBJECTS + 2);
915 context.draw_filled_rect(Vector(base.x, base.y),
916 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
920 Player::collision(const MovingObject& other, int collision_type)
923 (void) collision_type;
924 // will be implemented later
928 Player::collision(void* p_c_object, int c_object)
930 BadGuy* pbad_c = NULL;
931 Trampoline* ptramp_c = NULL;
932 FlyingPlatform* pplatform_c = NULL;
937 pbad_c = (BadGuy*) p_c_object;
939 /* Hurt player if he touches a badguy */
940 if (!pbad_c->dying && !dying &&
941 !safe_timer.started() &&
942 pbad_c->mode != BadGuy::HELD)
944 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
945 && !holding_something)
947 holding_something = true;
948 pbad_c->mode = BadGuy::HELD;
951 else if (pbad_c->mode == BadGuy::FLAT)
953 // Don't get hurt if we're kicking a flat badguy!
955 else if (pbad_c->mode == BadGuy::KICK)
957 /* Hurt if you get hit by kicked laptop: */
958 if (!invincible_timer.started())
965 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
966 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
967 || pbad_c->kind == BAD_SPIKY))
971 if (!invincible_timer.started())
980 player_status.score_multiplier++;
985 ptramp_c = (Trampoline*) p_c_object;
987 // Pick up trampoline
988 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
990 holding_something = true;
991 ptramp_c->mode = Trampoline::M_HELD;
992 ptramp_c->base.y -= 8;
994 // Set down trampoline
995 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
997 holding_something = false;
998 ptramp_c->mode = Trampoline::M_NORMAL;
999 ptramp_c->base.y += 8;
1000 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
1003 // ptramp_c->base.x = base.x + base.width+1;
1005 // ptramp_c->base.x = base.x - base.width-1;
1008 // Don't let tux walk through trampoline
1009 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
1011 if (physic.get_velocity_x() > 0) // RIGHT
1013 physic.set_velocity_x(0);
1014 base.x = ptramp_c->base.x - base.width;
1016 else if (physic.get_velocity_x() < 0) // LEFT
1018 physic.set_velocity_x(0);
1019 base.x = ptramp_c->base.x + ptramp_c->base.width;
1024 case CO_FLYING_PLATFORM:
1025 pplatform_c = (FlyingPlatform*) p_c_object;
1027 base.y = pplatform_c->base.y - base.height;
1028 physic.set_velocity_x(pplatform_c->get_vel_x());
1030 physic.enable_gravity(false);
1032 fall_mode = ON_GROUND;
1044 Player::kill(HurtMode mode)
1049 SoundManager::get()->play_sound(IDToSound(SND_HURT));
1051 physic.set_velocity_x(0);
1053 if (mode == SHRINK && size == BIG)
1055 if (got_power != NONE_POWER)
1057 safe_timer.start(TUX_SAFE_TIME);
1058 got_power = NONE_POWER;
1062 growing_timer.start(GROWING_TIME);
1063 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
1071 physic.enable_gravity(true);
1072 physic.set_acceleration(0, 0);
1073 physic.set_velocity(0, 7);
1074 --player_status.lives;
1075 dying = DYING_SQUISHED;
1076 dying_timer.start(3000);
1080 /* Remove Tux's power ups */
1082 Player::remove_powerups()
1084 got_power = NONE_POWER;
1090 Player::move(const Vector& vector)
1094 old_base = previous_base = base;
1098 Player::check_bounds(Camera* camera)
1100 /* Keep tux in bounds: */
1102 { // Lock Tux to the size of the level, so that he doesn't fall of
1107 /* Keep in-bounds, vertically: */
1108 if (base.y > Sector::current()->solids->get_height() * 32)
1114 bool adjust = false;
1115 // can happen if back scrolling is disabled
1116 if(base.x < camera->get_translation().x) {
1117 base.x = camera->get_translation().x;
1120 if(base.x >= camera->get_translation().x + screen->w - base.width) {
1121 base.x = camera->get_translation().x + screen->w - base.width;
1127 if(collision_object_map(base)) {
1135 Player::bounce(BadGuy* badguy)
1138 physic.set_velocity_y(5.2);
1140 physic.set_velocity_y(2);
1142 // Move the player a little bit above the badguy to avoid collision
1143 // between badguy and player directly after the bounce has happend
1144 base.y = badguy->base.y - base.height - 2;