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 && !duck)
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);
532 if (fabsf(base.x - badguy->base.x) < 150 &&
533 fabsf(base.y - badguy->base.y) < 60 &&
534 (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
535 issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
541 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
542 issolid(base.x + 1, base.y + base.height + 64) ||
543 issolid(base.x + base.width - 1, base.y + base.height + 64))
547 && input.old_up == UP)
552 if(on_ground()) /* Make sure jumping is off. */
555 input.old_up = input.up;
559 Player::handle_input()
561 /* Handle horizontal movement: */
562 handle_horizontal_input();
566 if (on_ground() && input.up == UP)
568 handle_vertical_input();
571 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
573 if(Sector::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
574 physic.get_velocity_x(), dir))
575 shooting_timer.start(SHOOTING_TIME);
576 input.old_fire = DOWN;
579 /* tux animations: */
580 if(!frame_timer.check())
582 frame_timer.start(25);
583 if (input.right == UP && input.left == UP)
590 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
591 (global_frame_counter % 4) == 0)
592 frame_main = (frame_main + 1) % 4;
602 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
607 // changing base size confuses collision otherwise
608 old_base = previous_base = base;
610 else if(input.down == UP && size == BIG && duck)
612 // try if we can really unduck
615 // when unducking in air we need some space to do so
616 if(on_ground() || !collision_object_map(base)) {
618 // changing base size confuses collision otherwise
619 old_base = previous_base = base;
621 // undo the ducking changes
629 Player::grow(bool animate)
639 growing_timer.start(GROWING_TIME);
641 old_base = previous_base = base;
645 Player::grabdistros()
650 Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
651 Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
653 Sector::current()->trygrabdistro(
654 Vector(base.x, base.y + base.height), NO_BOUNCE);
655 Sector::current()->trygrabdistro(
656 Vector(base.x+ 31, base.y + base.height), NO_BOUNCE);
660 Sector::current()->trygrabdistro(
661 Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
662 Sector::current()->trygrabdistro(
663 Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
668 /* Enough distros for a One-up? */
669 if (player_status.distros >= DISTROS_LIFEUP)
671 player_status.distros = player_status.distros - DISTROS_LIFEUP;
672 if(player_status.lives < MAX_LIVES)
673 ++player_status.lives;
674 /*We want to hear the sound even, if MAX_LIVES is reached*/
675 sound_manager->play_sound(sounds[SND_LIFEUP]);
680 Player::draw(DrawingContext& context)
682 PlayerSprite* sprite;
686 else if (got_power == FIRE_POWER)
688 else if (got_power == ICE_POWER)
693 int layer = LAYER_OBJECTS - 1;
694 Vector pos = Vector(base.x, base.y);
696 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
698 if (dying == DYING_SQUISHED)
700 smalltux_gameover->draw(context, pos, LAYER_OBJECTS+1);
704 if(growing_timer.check())
707 context.draw_surface(growingtux_right[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
709 context.draw_surface(growingtux_left[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
711 else if (duck && size != SMALL)
714 sprite->duck_right->draw(context, pos, layer);
716 sprite->duck_left->draw(context, pos, layer);
718 else if (skidding_timer.started())
721 sprite->skid_right->draw(context, pos, layer);
723 sprite->skid_left->draw(context, pos, layer);
725 else if (kick_timer.started())
728 sprite->kick_right->draw(context, pos, layer);
730 sprite->kick_left->draw(context, pos, layer);
732 else if (physic.get_velocity_y() != 0)
735 sprite->jump_right->draw(context, pos, layer);
737 sprite->jump_left->draw(context, pos, layer);
741 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
744 sprite->stand_right->draw(context, pos, layer);
746 sprite->stand_left->draw(context, pos, layer);
751 sprite->walk_right->draw(context, pos, layer);
753 sprite->walk_left->draw(context, pos, layer);
759 // Draw arm overlay graphics when Tux is holding something
760 if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check() && !duck)
763 sprite->grab_right->draw(context, pos, LAYER_OBJECTS + 1);
765 sprite->grab_left->draw(context, pos, LAYER_OBJECTS + 1);
768 // Draw blinking star overlay
769 if (invincible_timer.started() &&
770 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
772 if (size == SMALL || duck)
773 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
775 largetux_star->draw(context, pos, LAYER_OBJECTS + 2);
779 context.draw_filled_rect(Vector(base.x, base.y),
780 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
784 Player::collision(const MovingObject& other, int collision_type)
787 (void) collision_type;
788 // will be implemented later
792 Player::collision(void* p_c_object, int c_object)
794 BadGuy* pbad_c = NULL;
795 Trampoline* ptramp_c = NULL;
796 FlyingPlatform* pplatform_c = NULL;
801 pbad_c = (BadGuy*) p_c_object;
803 /* Hurt player if he touches a badguy */
804 if (!pbad_c->dying && !dying &&
805 !safe_timer.started() &&
806 pbad_c->mode != BadGuy::HELD)
808 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
809 && !holding_something)
811 holding_something = true;
812 pbad_c->mode = BadGuy::HELD;
815 else if (pbad_c->mode == BadGuy::FLAT)
817 // Don't get hurt if we're kicking a flat badguy!
819 else if (pbad_c->mode == BadGuy::KICK)
821 /* Hurt if you get hit by kicked laptop: */
822 if (!invincible_timer.started())
829 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
830 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
831 || pbad_c->kind == BAD_SPIKY))
835 if (!invincible_timer.started())
844 player_status.score_multiplier++;
849 ptramp_c = (Trampoline*) p_c_object;
851 // Pick up trampoline
852 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
854 holding_something = true;
855 ptramp_c->mode = Trampoline::M_HELD;
856 ptramp_c->base.y -= 8;
858 // Set down trampoline
859 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
861 holding_something = false;
862 ptramp_c->mode = Trampoline::M_NORMAL;
863 ptramp_c->base.y += 8;
864 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
867 // ptramp_c->base.x = base.x + base.width+1;
869 // ptramp_c->base.x = base.x - base.width-1;
872 // Don't let tux walk through trampoline
873 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
875 if (physic.get_velocity_x() > 0) // RIGHT
877 physic.set_velocity_x(0);
878 base.x = ptramp_c->base.x - base.width;
880 else if (physic.get_velocity_x() < 0) // LEFT
882 physic.set_velocity_x(0);
883 base.x = ptramp_c->base.x + ptramp_c->base.width;
888 case CO_FLYING_PLATFORM:
889 pplatform_c = (FlyingPlatform*) p_c_object;
891 base.y = pplatform_c->base.y - base.height;
892 physic.set_velocity_x(pplatform_c->get_vel_x());
894 physic.enable_gravity(false);
896 fall_mode = ON_GROUND;
908 Player::kill(HurtMode mode)
913 sound_manager->play_sound(sounds[SND_HURT]);
915 physic.set_velocity_x(0);
917 if (mode == SHRINK && size == BIG)
919 if (got_power != NONE_POWER)
921 got_power = NONE_POWER;
929 safe_timer.start(TUX_SAFE_TIME);
933 physic.enable_gravity(true);
934 physic.set_acceleration(0, 0);
935 physic.set_velocity(0, 7);
936 --player_status.lives;
937 dying = DYING_SQUISHED;
938 dying_timer.start(3000);
942 /* Remove Tux's power ups */
944 Player::remove_powerups()
946 got_power = NONE_POWER;
952 Player::move(const Vector& vector)
956 old_base = previous_base = base;
960 Player::check_bounds(Camera* camera)
962 /* Keep tux in bounds: */
964 { // Lock Tux to the size of the level, so that he doesn't fall of
969 /* Keep in-bounds, vertically: */
970 if (base.y > Sector::current()->solids->get_height() * 32)
977 // can happen if back scrolling is disabled
978 if(base.x < camera->get_translation().x) {
979 base.x = camera->get_translation().x;
982 if(base.x >= camera->get_translation().x + screen->w - base.width) {
983 base.x = camera->get_translation().x + screen->w - base.width;
989 if(collision_object_map(base)) {