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.
34 #include "resources.h"
35 #include "interactive_object.h"
36 #include "screen/screen.h"
38 // behavior definitions:
39 #define TILES_FOR_BUTTJUMP 3
40 // animation times (in ms):
41 #define SHOOTING_TIME 320
43 #define AUTOSCROLL_DEAD_INTERVAL 300
46 Surface* growingtux_left[GROWING_FRAMES];
47 Surface* growingtux_right[GROWING_FRAMES];
51 Sprite* smalltux_gameover;
52 Sprite* smalltux_star;
53 Sprite* largetux_star;
55 PlayerSprite smalltux;
56 PlayerSprite largetux;
62 PlayerKeymap::PlayerKeymap()
64 keymap.jump = SDLK_SPACE;
65 keymap.activate = SDLK_UP;
66 keymap.duck = SDLK_DOWN;
67 keymap.left = SDLK_LEFT;
68 keymap.right = SDLK_RIGHT;
69 keymap.fire = SDLK_LCTRL;
72 void player_input_init(player_input_type* pplayer_input)
74 pplayer_input->down = UP;
75 pplayer_input->fire = UP;
76 pplayer_input->left = UP;
77 pplayer_input->old_fire = UP;
78 pplayer_input->right = UP;
79 pplayer_input->up = UP;
80 pplayer_input->old_up = UP;
81 pplayer_input->activate = UP;
96 holding_something = false;
102 got_power = NONE_POWER;
106 previous_base = old_base = base;
114 fall_mode = ON_GROUND;
122 player_input_init(&input);
124 invincible_timer.init(true);
125 skidding_timer.init(true);
126 safe_timer.init(true);
127 frame_timer.init(true);
128 kick_timer.init(true);
129 shooting_timer.init(true);
130 growing_timer.init(true);
136 Player::key_event(SDLKey key, int state)
138 if(key == keymap.right)
143 else if(key == keymap.left)
148 else if(key == keymap.jump)
153 else if(key == keymap.duck)
158 else if(key == keymap.fire)
165 else if(key == keymap.activate)
167 input.activate = state;
170 /** check for interactive objects */
171 for(Sector::InteractiveObjects::iterator i
172 = Sector::current()->interactive_objects.begin();
173 i != Sector::current()->interactive_objects.end(); ++i) {
174 if(rectcollision(base, (*i)->get_area())) {
175 (*i)->interaction(INTERACTION_ACTIVATE);
187 Player::level_begin()
191 previous_base = old_base = base;
196 player_input_init(&input);
198 invincible_timer.init(true);
199 skidding_timer.init(true);
200 safe_timer.init(true);
201 frame_timer.init(true);
202 growing_timer.init(true);
208 Player::action(float elapsed_time)
210 bool jumped_in_solid = false;
212 if(dying && !dying_timer.check()) {
217 if (input.fire == UP)
218 holding_something = false;
221 previous_base = base;
223 /* --- HANDLE TUX! --- */
224 if(dying == DYING_NOT)
227 physic.apply(elapsed_time, base.x, base.y);
229 if(dying == DYING_NOT)
231 base_type target = base;
233 collision_swept_object_map(&old_base, &base);
235 if ((!invincible_timer.started() && !safe_timer.started())
236 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
237 || isspike(base.x, base.y + base.height)
238 || isspike(base.x + base.width, base.y + base.height)))
243 // Don't accelerate Tux if he is running against a wall
244 if (target.x != base.x)
246 physic.set_velocity_x(0);
249 // special exception for cases where we're stuck under tiles after
250 // being ducked. In this case we drift out
251 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
252 && collision_object_map(base))
254 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
255 previous_base = old_base = base;
261 physic.enable_gravity(true);
265 physic.set_velocity_y(0);
266 jumped_in_solid = true;
273 if (physic.get_velocity_y() < 0)
275 base.y = (int)(((int)base.y / 32) * 32);
276 physic.set_velocity_y(0);
279 physic.enable_gravity(false);
280 /* Reset score multiplier (for multi-hits): */
281 if (!invincible_timer.started())
282 player_status.score_multiplier = 1;
287 if (isbrick(base.x, base.y) ||
288 isfullbox(base.x, base.y))
290 Sector::current()->trygrabdistro(
291 Vector(base.x, base.y - 32), BOUNCE);
292 Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
294 Sector::current()->trybreakbrick(
295 Vector(base.x, base.y), size == SMALL);
297 bumpbrick(base.x, base.y);
298 Sector::current()->tryemptybox(Vector(base.x, base.y), RIGHT);
301 if (isbrick(base.x+ 31, base.y) ||
302 isfullbox(base.x+ 31, base.y))
304 Sector::current()->trygrabdistro(
305 Vector(base.x+ 31, base.y - 32), BOUNCE);
306 Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
309 Sector::current()->trybreakbrick(
310 Vector(base.x+ 31, base.y), size == SMALL);
312 bumpbrick(base.x+ 31, base.y);
313 Sector::current()->tryemptybox(Vector(base.x+ 31, base.y), LEFT);
325 /* Make sure jumping is off. */
331 /* ---- DONE HANDLING TUX! --- */
334 skidding_timer.check();
335 invincible_timer.check();
343 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
344 issolid(base.x + 1, base.y + base.height) ||
345 issolid(base.x + base.width - 1, base.y + base.height));
349 Player::under_solid()
351 return ( issolid(base.x + base.width / 2, base.y) ||
352 issolid(base.x + 1, base.y) ||
353 issolid(base.x + base.width - 1, base.y) );
357 Player::tiles_on_air(int tiles)
359 for(int t = 0; t != tiles; t++)
361 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
362 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
363 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
370 Player::handle_horizontal_input()
372 float vx = physic.get_velocity_x();
373 float vy = physic.get_velocity_y();
374 float ax = physic.get_acceleration_x();
375 float ay = physic.get_acceleration_y();
378 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
382 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
388 if (input.fire == UP) {
389 ax = dirsign * WALK_ACCELERATION_X;
391 if(vx >= MAX_WALK_XM && dirsign > 0) {
394 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
399 ax = dirsign * RUN_ACCELERATION_X;
401 if(vx >= MAX_RUN_XM && dirsign > 0) {
404 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
410 // we can reach WALK_SPEED without any acceleration
411 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
412 vx = dirsign * WALK_SPEED;
415 // changing directions?
416 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
417 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
418 skidding_timer.start(SKID_TIME);
419 sound_manager->play_sound(sounds[SND_SKID]);
426 // we get slower when not pressing any keys
428 if(fabs(vx) < WALK_SPEED) {
432 ax = WALK_ACCELERATION_X * 1.5;
434 ax = WALK_ACCELERATION_X * -1.5;
438 // if we're on ice slow down acceleration or deceleration
439 if (isice(base.x, base.y + base.height))
441 /* the acceleration/deceleration rate on ice is inversely proportional to
442 * the current velocity.
445 // increasing 1 will increase acceleration/deceleration rate
446 // decreasing 1 will decrease acceleration/deceleration rate
447 // must stay above zero, though
448 if (ax != 0) ax *= 1 / fabs(vx);
451 physic.set_velocity(vx, vy);
452 physic.set_acceleration(ax, ay);
456 Player::handle_vertical_input()
460 fall_mode = ON_GROUND;
461 last_ground_y = base.y;
463 if(base.y > last_ground_y)
465 else if(fall_mode == ON_GROUND)
470 if(input.up == DOWN && can_jump && on_ground())
472 if(duck) { // only jump a little bit when in duck mode {
473 physic.set_velocity_y(3);
475 // jump higher if we are running
476 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
477 physic.set_velocity_y(5.8);
479 physic.set_velocity_y(5.2);
486 sound_manager->play_sound(sounds[SND_JUMP]);
488 sound_manager->play_sound(sounds[SND_BIGJUMP]);
490 // Let go of jump key
491 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
494 physic.set_velocity_y(0);
497 /* In case the player has pressed Down while in a certain range of air,
498 enable butt jump action */
499 if (input.down == DOWN && !butt_jump)
500 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
503 /* When Down is not held anymore, disable butt jump */
504 if(butt_jump && input.down == UP)
508 if (butt_jump && on_ground() && size == BIG)
512 // Break bricks beneath Tux
513 if(Sector::current()->trybreakbrick(
514 Vector(base.x + 1, base.y + base.height), false)
515 || Sector::current()->trybreakbrick(
516 Vector(base.x + base.width - 1, base.y + base.height), false))
518 physic.set_velocity_y(2);
522 // Kill nearby badguys
523 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
524 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
525 i != gameobjects.end();
528 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
531 if (fabsf(base.x - badguy->base.x) < 300 &&
532 fabsf(base.y - badguy->base.y) < 300 &&
533 (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
534 issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
540 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
541 issolid(base.x + 1, base.y + base.height + 64) ||
542 issolid(base.x + base.width - 1, base.y + base.height + 64))
546 && input.old_up == UP)
551 if(on_ground()) /* Make sure jumping is off. */
554 input.old_up = input.up;
558 Player::handle_input()
560 /* Handle horizontal movement: */
561 handle_horizontal_input();
565 if (on_ground() && input.up == UP)
567 handle_vertical_input();
570 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
572 if(Sector::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
573 physic.get_velocity_x(), dir))
574 shooting_timer.start(SHOOTING_TIME);
575 input.old_fire = DOWN;
578 /* tux animations: */
579 if(!frame_timer.check())
581 frame_timer.start(25);
582 if (input.right == UP && input.left == UP)
589 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
590 (global_frame_counter % 4) == 0)
591 frame_main = (frame_main + 1) % 4;
601 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
606 // changing base size confuses collision otherwise
607 old_base = previous_base = base;
609 else if(input.down == UP && size == BIG && duck)
611 // try if we can really unduck
614 // when unducking in air we need some space to do so
615 if(on_ground() || !collision_object_map(base)) {
617 // changing base size confuses collision otherwise
618 old_base = previous_base = base;
620 // undo the ducking changes
628 Player::grow(bool animate)
638 growing_timer.start(GROWING_TIME);
640 old_base = previous_base = base;
644 Player::grabdistros()
649 Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
650 Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
652 Sector::current()->trygrabdistro(
653 Vector(base.x, base.y + base.height), NO_BOUNCE);
654 Sector::current()->trygrabdistro(
655 Vector(base.x+ 31, base.y + base.height), NO_BOUNCE);
659 Sector::current()->trygrabdistro(
660 Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
661 Sector::current()->trygrabdistro(
662 Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
667 /* Enough distros for a One-up? */
668 if (player_status.distros >= DISTROS_LIFEUP)
670 player_status.distros = player_status.distros - DISTROS_LIFEUP;
671 if(player_status.lives < MAX_LIVES)
672 ++player_status.lives;
673 /*We want to hear the sound even, if MAX_LIVES is reached*/
674 sound_manager->play_sound(sounds[SND_LIFEUP]);
679 Player::draw(DrawingContext& context)
681 PlayerSprite* sprite;
685 else if (got_power == FIRE_POWER)
687 else if (got_power == ICE_POWER)
692 int layer = LAYER_OBJECTS - 1;
693 Vector pos = Vector(base.x, base.y);
695 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
697 if (dying == DYING_SQUISHED)
699 smalltux_gameover->draw(context, pos, LAYER_OBJECTS+1);
703 if(growing_timer.check())
706 context.draw_surface(growingtux_right[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
708 context.draw_surface(growingtux_left[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
710 else if (duck && size != SMALL)
713 sprite->duck_right->draw(context, pos, layer);
715 sprite->duck_left->draw(context, pos, layer);
717 else if (skidding_timer.started())
720 sprite->skid_right->draw(context, pos, layer);
722 sprite->skid_left->draw(context, pos, layer);
724 else if (kick_timer.started())
727 sprite->kick_right->draw(context, pos, layer);
729 sprite->kick_left->draw(context, pos, layer);
731 else if (physic.get_velocity_y() != 0)
734 sprite->jump_right->draw(context, pos, layer);
736 sprite->jump_left->draw(context, pos, layer);
740 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
743 sprite->stand_right->draw(context, pos, layer);
745 sprite->stand_left->draw(context, pos, layer);
750 sprite->walk_right->draw(context, pos, layer);
752 sprite->walk_left->draw(context, pos, layer);
758 // Draw arm overlay graphics when Tux is holding something
759 if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check() && !duck)
762 sprite->grab_right->draw(context, pos, LAYER_OBJECTS + 1);
764 sprite->grab_left->draw(context, pos, LAYER_OBJECTS + 1);
767 // Draw blinking star overlay
768 if (invincible_timer.started() &&
769 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
771 if (size == SMALL || duck)
772 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
774 largetux_star->draw(context, pos, LAYER_OBJECTS + 2);
778 context.draw_filled_rect(Vector(base.x, base.y),
779 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
783 Player::collision(const MovingObject& other, int collision_type)
786 (void) collision_type;
787 // will be implemented later
791 Player::collision(void* p_c_object, int c_object)
793 BadGuy* pbad_c = NULL;
794 Trampoline* ptramp_c = NULL;
795 FlyingPlatform* pplatform_c = NULL;
800 pbad_c = (BadGuy*) p_c_object;
802 /* Hurt player if he touches a badguy */
803 if (!pbad_c->dying && !dying &&
804 !safe_timer.started() &&
805 pbad_c->mode != BadGuy::HELD)
807 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
808 && !holding_something)
810 holding_something = true;
811 pbad_c->mode = BadGuy::HELD;
814 else if (pbad_c->mode == BadGuy::FLAT)
816 // Don't get hurt if we're kicking a flat badguy!
818 else if (pbad_c->mode == BadGuy::KICK)
820 /* Hurt if you get hit by kicked laptop: */
821 if (!invincible_timer.started())
828 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
829 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
830 || pbad_c->kind == BAD_SPIKY))
834 if (!invincible_timer.started())
843 player_status.score_multiplier++;
848 ptramp_c = (Trampoline*) p_c_object;
850 // Pick up trampoline
851 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
853 holding_something = true;
854 ptramp_c->mode = Trampoline::M_HELD;
855 ptramp_c->base.y -= 8;
857 // Set down trampoline
858 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
860 holding_something = false;
861 ptramp_c->mode = Trampoline::M_NORMAL;
862 ptramp_c->base.y += 8;
863 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
866 // ptramp_c->base.x = base.x + base.width+1;
868 // ptramp_c->base.x = base.x - base.width-1;
871 // Don't let tux walk through trampoline
872 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
874 if (physic.get_velocity_x() > 0) // RIGHT
876 physic.set_velocity_x(0);
877 base.x = ptramp_c->base.x - base.width;
879 else if (physic.get_velocity_x() < 0) // LEFT
881 physic.set_velocity_x(0);
882 base.x = ptramp_c->base.x + ptramp_c->base.width;
887 case CO_FLYING_PLATFORM:
888 pplatform_c = (FlyingPlatform*) p_c_object;
890 base.y = pplatform_c->base.y - base.height;
891 physic.set_velocity_x(pplatform_c->get_vel_x());
893 physic.enable_gravity(false);
895 fall_mode = ON_GROUND;
907 Player::kill(HurtMode mode)
912 sound_manager->play_sound(sounds[SND_HURT]);
914 physic.set_velocity_x(0);
916 if (mode == SHRINK && size == BIG)
918 if (got_power != NONE_POWER)
920 got_power = NONE_POWER;
928 safe_timer.start(TUX_SAFE_TIME);
932 physic.enable_gravity(true);
933 physic.set_acceleration(0, 0);
934 physic.set_velocity(0, 7);
935 --player_status.lives;
936 dying = DYING_SQUISHED;
937 dying_timer.start(3000);
941 /* Remove Tux's power ups */
943 Player::remove_powerups()
945 got_power = NONE_POWER;
951 Player::move(const Vector& vector)
955 old_base = previous_base = base;
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
968 /* Keep in-bounds, vertically: */
969 if (base.y > Sector::current()->solids->get_height() * 32)
976 // can happen if back scrolling is disabled
977 if(base.x < camera->get_translation().x) {
978 base.x = camera->get_translation().x;
981 if(base.x >= camera->get_translation().x + screen->w - base.width) {
982 base.x = camera->get_translation().x + screen->w - base.width;
988 if(collision_object_map(base)) {