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
42 #define STOMP_TIME 250
44 #define AUTOSCROLL_DEAD_INTERVAL 300
47 Surface* growingtux_left[GROWING_FRAMES];
48 Surface* growingtux_right[GROWING_FRAMES];
52 Sprite* smalltux_gameover;
53 Sprite* smalltux_star;
54 Sprite* largetux_star;
56 PlayerSprite smalltux;
57 PlayerSprite largetux;
63 PlayerKeymap::PlayerKeymap()
65 keymap.jump = SDLK_SPACE;
66 keymap.activate = SDLK_UP;
67 keymap.duck = SDLK_DOWN;
68 keymap.left = SDLK_LEFT;
69 keymap.right = SDLK_RIGHT;
70 keymap.fire = SDLK_LCTRL;
73 void player_input_init(player_input_type* pplayer_input)
75 pplayer_input->down = UP;
76 pplayer_input->fire = UP;
77 pplayer_input->left = UP;
78 pplayer_input->old_fire = UP;
79 pplayer_input->right = UP;
80 pplayer_input->up = UP;
81 pplayer_input->old_up = UP;
82 pplayer_input->activate = UP;
97 holding_something = false;
103 got_power = NONE_POWER;
107 previous_base = old_base = base;
115 fall_mode = ON_GROUND;
123 player_input_init(&input);
125 invincible_timer.init(true);
126 skidding_timer.init(true);
127 safe_timer.init(true);
128 frame_timer.init(true);
129 kick_timer.init(true);
130 shooting_timer.init(true);
131 growing_timer.init(true);
137 Player::key_event(SDLKey key, int state)
139 if(key == keymap.right)
144 else if(key == keymap.left)
149 else if(key == keymap.jump)
154 else if(key == keymap.duck)
159 else if(key == keymap.fire)
166 else if(key == keymap.activate)
168 input.activate = state;
171 /** check for interactive objects */
172 for(Sector::InteractiveObjects::iterator i
173 = Sector::current()->interactive_objects.begin();
174 i != Sector::current()->interactive_objects.end(); ++i) {
175 if(rectcollision(base, (*i)->get_area())) {
176 (*i)->interaction(INTERACTION_ACTIVATE);
188 Player::level_begin()
192 previous_base = old_base = base;
197 player_input_init(&input);
199 invincible_timer.init(true);
200 skidding_timer.init(true);
201 safe_timer.init(true);
202 frame_timer.init(true);
203 growing_timer.init(true);
209 Player::action(float elapsed_time)
211 bool jumped_in_solid = false;
213 if(dying && !dying_timer.check()) {
218 if (input.fire == UP)
219 holding_something = false;
222 previous_base = base;
224 /* --- HANDLE TUX! --- */
225 if(dying == DYING_NOT)
228 physic.apply(elapsed_time, base.x, base.y);
230 if(dying == DYING_NOT)
232 base_type target = base;
234 collision_swept_object_map(&old_base, &base);
236 if ((!invincible_timer.started() && !safe_timer.started())
237 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
238 || isspike(base.x, base.y + base.height)
239 || isspike(base.x + base.width, base.y + base.height)))
244 // Don't accelerate Tux if he is running against a wall
245 if (target.x != base.x)
247 physic.set_velocity_x(0);
250 // special exception for cases where we're stuck under tiles after
251 // being ducked. In this case we drift out
252 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
253 && collision_object_map(base))
255 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
256 previous_base = old_base = base;
262 physic.enable_gravity(true);
266 physic.set_velocity_y(0);
267 jumped_in_solid = true;
274 if (physic.get_velocity_y() < 0)
276 base.y = (int)(((int)base.y / 32) * 32);
277 physic.set_velocity_y(0);
280 physic.enable_gravity(false);
281 /* Reset score multiplier (for multi-hits): */
282 if (!invincible_timer.started())
283 player_status.score_multiplier = 1;
288 if (isbrick(base.x, base.y) ||
289 isfullbox(base.x, base.y))
291 Sector::current()->trygrabdistro(
292 Vector(base.x, base.y - 32), BOUNCE);
293 Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
295 Sector::current()->trybreakbrick(
296 Vector(base.x, base.y), size == SMALL);
298 bumpbrick(base.x, base.y);
299 Sector::current()->tryemptybox(Vector(base.x, base.y), RIGHT);
302 if (isbrick(base.x+ 31, base.y) ||
303 isfullbox(base.x+ 31, base.y))
305 Sector::current()->trygrabdistro(
306 Vector(base.x+ 31, base.y - 32), BOUNCE);
307 Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
310 Sector::current()->trybreakbrick(
311 Vector(base.x+ 31, base.y), size == SMALL);
313 bumpbrick(base.x+ 31, base.y);
314 Sector::current()->tryemptybox(Vector(base.x+ 31, base.y), LEFT);
326 /* Make sure jumping is off. */
332 /* ---- DONE HANDLING TUX! --- */
335 skidding_timer.check();
336 invincible_timer.check();
344 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
345 issolid(base.x + 1, base.y + base.height) ||
346 issolid(base.x + base.width - 1, base.y + base.height));
350 Player::under_solid()
352 return ( issolid(base.x + base.width / 2, base.y) ||
353 issolid(base.x + 1, base.y) ||
354 issolid(base.x + base.width - 1, base.y) );
358 Player::tiles_on_air(int tiles)
360 for(int t = 0; t != tiles; t++)
362 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
363 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
364 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
371 Player::handle_horizontal_input()
373 float vx = physic.get_velocity_x();
374 float vy = physic.get_velocity_y();
375 float ax = physic.get_acceleration_x();
376 float ay = physic.get_acceleration_y();
379 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
383 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
389 if (input.fire == UP) {
390 ax = dirsign * WALK_ACCELERATION_X;
392 if(vx >= MAX_WALK_XM && dirsign > 0) {
395 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
400 ax = dirsign * RUN_ACCELERATION_X;
402 if(vx >= MAX_RUN_XM && dirsign > 0) {
405 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
411 // we can reach WALK_SPEED without any acceleration
412 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
413 vx = dirsign * WALK_SPEED;
416 // changing directions?
417 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
418 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
419 skidding_timer.start(SKID_TIME);
420 sound_manager->play_sound(sounds[SND_SKID]);
427 // we get slower when not pressing any keys
429 if(fabs(vx) < WALK_SPEED) {
433 ax = WALK_ACCELERATION_X * 1.5;
435 ax = WALK_ACCELERATION_X * -1.5;
439 // if we're on ice slow down acceleration or deceleration
440 if (isice(base.x, base.y + base.height))
442 /* the acceleration/deceleration rate on ice is inversely proportional to
443 * the current velocity.
446 // increasing 1 will increase acceleration/deceleration rate
447 // decreasing 1 will decrease acceleration/deceleration rate
448 // must stay above zero, though
449 if (ax != 0) ax *= 1 / fabs(vx);
452 physic.set_velocity(vx, vy);
453 physic.set_acceleration(ax, ay);
457 Player::handle_vertical_input()
461 fall_mode = ON_GROUND;
462 last_ground_y = base.y;
464 if(base.y > last_ground_y)
466 else if(fall_mode == ON_GROUND)
471 if(input.up == DOWN && can_jump && on_ground())
473 if(duck) { // only jump a little bit when in duck mode {
474 physic.set_velocity_y(3);
476 // jump higher if we are running
477 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
478 physic.set_velocity_y(5.8);
480 physic.set_velocity_y(5.2);
487 sound_manager->play_sound(sounds[SND_JUMP]);
489 sound_manager->play_sound(sounds[SND_BIGJUMP]);
491 // Let go of jump key
492 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
495 physic.set_velocity_y(0);
498 /* In case the player has pressed Down while in a certain range of air,
499 enable butt jump action */
500 if (input.down == DOWN && !butt_jump && !duck)
501 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
504 /* When Down is not held anymore, disable butt jump */
505 if(butt_jump && input.down == UP)
509 if (butt_jump && on_ground() && size == BIG)
511 stomp_timer.start(STOMP_TIME);
514 // Break bricks beneath Tux
515 if(Sector::current()->trybreakbrick(
516 Vector(base.x + 1, base.y + base.height), false)
517 || Sector::current()->trybreakbrick(
518 Vector(base.x + base.width - 1, base.y + base.height), false))
520 physic.set_velocity_y(2);
524 // Kill nearby badguys
525 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
526 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
527 i != gameobjects.end();
530 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
534 if (fabsf(base.x - badguy->base.x) < 150 &&
535 fabsf(base.y - badguy->base.y) < 60 &&
536 (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
537 issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
543 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
544 issolid(base.x + 1, base.y + base.height + 64) ||
545 issolid(base.x + base.width - 1, base.y + base.height + 64))
549 && input.old_up == UP)
554 if(on_ground()) /* Make sure jumping is off. */
557 input.old_up = input.up;
561 Player::handle_input()
563 /* Handle horizontal movement: */
564 handle_horizontal_input();
568 if (on_ground() && input.up == UP)
570 handle_vertical_input();
573 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
575 if(Sector::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
576 physic.get_velocity_x(), dir))
577 shooting_timer.start(SHOOTING_TIME);
578 input.old_fire = DOWN;
581 /* tux animations: */
582 if(!frame_timer.check())
584 frame_timer.start(25);
585 if (input.right == UP && input.left == UP)
592 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
593 (global_frame_counter % 4) == 0)
594 frame_main = (frame_main + 1) % 4;
604 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
609 // changing base size confuses collision otherwise
610 old_base = previous_base = base;
612 else if(input.down == UP && size == BIG && duck)
614 // try if we can really unduck
617 // when unducking in air we need some space to do so
618 if(on_ground() || !collision_object_map(base)) {
620 // changing base size confuses collision otherwise
621 old_base = previous_base = base;
623 // undo the ducking changes
631 Player::grow(bool animate)
641 growing_timer.start(GROWING_TIME);
643 old_base = previous_base = base;
647 Player::grabdistros()
652 Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
653 Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
655 Sector::current()->trygrabdistro(
656 Vector(base.x, base.y + base.height), NO_BOUNCE);
657 Sector::current()->trygrabdistro(
658 Vector(base.x+ 31, base.y + base.height), NO_BOUNCE);
662 Sector::current()->trygrabdistro(
663 Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
664 Sector::current()->trygrabdistro(
665 Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
670 /* Enough distros for a One-up? */
671 if (player_status.distros >= DISTROS_LIFEUP)
673 player_status.distros = player_status.distros - DISTROS_LIFEUP;
674 if(player_status.lives < MAX_LIVES)
675 ++player_status.lives;
676 /*We want to hear the sound even, if MAX_LIVES is reached*/
677 sound_manager->play_sound(sounds[SND_LIFEUP]);
682 Player::draw(DrawingContext& context)
684 PlayerSprite* sprite;
688 else if (got_power == FIRE_POWER)
690 else if (got_power == ICE_POWER)
695 int layer = LAYER_OBJECTS - 1;
696 Vector pos = Vector(base.x, base.y);
698 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
700 if (dying == DYING_SQUISHED)
702 smalltux_gameover->draw(context, pos, LAYER_OBJECTS+1);
706 if(growing_timer.check())
709 context.draw_surface(growingtux_right[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
711 context.draw_surface(growingtux_left[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
713 else if (duck && size != SMALL)
716 sprite->duck_right->draw(context, pos, layer);
718 sprite->duck_left->draw(context, pos, layer);
720 else if (skidding_timer.started())
723 sprite->skid_right->draw(context, pos, layer);
725 sprite->skid_left->draw(context, pos, layer);
727 else if (kick_timer.started())
730 sprite->kick_right->draw(context, pos, layer);
732 sprite->kick_left->draw(context, pos, layer);
734 else if (physic.get_velocity_y() != 0)
737 sprite->jump_right->draw(context, pos, layer);
739 sprite->jump_left->draw(context, pos, layer);
743 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
746 sprite->stand_right->draw(context, pos, layer);
748 sprite->stand_left->draw(context, pos, layer);
753 sprite->walk_right->draw(context, pos, layer);
755 sprite->walk_left->draw(context, pos, layer);
761 // Draw arm overlay graphics when Tux is holding something
762 if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check() && !duck)
765 sprite->grab_right->draw(context, pos, LAYER_OBJECTS + 1);
767 sprite->grab_left->draw(context, pos, LAYER_OBJECTS + 1);
770 // Draw stomp clouds when doing a butt jump
771 if (stomp_timer.check())
773 sprite->stomp->draw(context, Vector(base.x - 32, base.y), LAYER_OBJECTS + 1);
775 sprite->stomp->draw(context, Vector(base.x - 32, base.y + 32), LAYER_OBJECTS + 1);
777 // Draw blinking star overlay
778 if (invincible_timer.started() &&
779 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
781 if (size == SMALL || duck)
782 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
784 largetux_star->draw(context, pos, LAYER_OBJECTS + 2);
788 context.draw_filled_rect(Vector(base.x, base.y),
789 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
793 Player::collision(const MovingObject& other, int collision_type)
796 (void) collision_type;
797 // will be implemented later
801 Player::collision(void* p_c_object, int c_object)
803 BadGuy* pbad_c = NULL;
804 Trampoline* ptramp_c = NULL;
805 FlyingPlatform* pplatform_c = NULL;
810 pbad_c = (BadGuy*) p_c_object;
812 /* Hurt player if he touches a badguy */
813 if (!pbad_c->dying && !dying &&
814 !safe_timer.started() &&
815 pbad_c->mode != BadGuy::HELD)
817 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
818 && !holding_something)
820 holding_something = true;
821 pbad_c->mode = BadGuy::HELD;
824 else if (pbad_c->mode == BadGuy::FLAT)
826 // Don't get hurt if we're kicking a flat badguy!
828 else if (pbad_c->mode == BadGuy::KICK)
830 /* Hurt if you get hit by kicked laptop: */
831 if (!invincible_timer.started())
838 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
839 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
840 || pbad_c->kind == BAD_SPIKY))
844 if (!invincible_timer.started())
853 player_status.score_multiplier++;
858 ptramp_c = (Trampoline*) p_c_object;
860 // Pick up trampoline
861 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
863 holding_something = true;
864 ptramp_c->mode = Trampoline::M_HELD;
865 ptramp_c->base.y -= 8;
867 // Set down trampoline
868 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
870 holding_something = false;
871 ptramp_c->mode = Trampoline::M_NORMAL;
872 ptramp_c->base.y += 8;
873 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
876 // ptramp_c->base.x = base.x + base.width+1;
878 // ptramp_c->base.x = base.x - base.width-1;
881 // Don't let tux walk through trampoline
882 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
884 if (physic.get_velocity_x() > 0) // RIGHT
886 physic.set_velocity_x(0);
887 base.x = ptramp_c->base.x - base.width;
889 else if (physic.get_velocity_x() < 0) // LEFT
891 physic.set_velocity_x(0);
892 base.x = ptramp_c->base.x + ptramp_c->base.width;
897 case CO_FLYING_PLATFORM:
898 pplatform_c = (FlyingPlatform*) p_c_object;
900 base.y = pplatform_c->base.y - base.height;
901 physic.set_velocity_x(pplatform_c->get_vel_x());
903 physic.enable_gravity(false);
905 fall_mode = ON_GROUND;
917 Player::kill(HurtMode mode)
922 sound_manager->play_sound(sounds[SND_HURT]);
924 physic.set_velocity_x(0);
926 if (mode == SHRINK && size == BIG)
928 if (got_power != NONE_POWER)
930 got_power = NONE_POWER;
938 safe_timer.start(TUX_SAFE_TIME);
942 physic.enable_gravity(true);
943 physic.set_acceleration(0, 0);
944 physic.set_velocity(0, 7);
945 --player_status.lives;
946 dying = DYING_SQUISHED;
947 dying_timer.start(3000);
951 /* Remove Tux's power ups */
953 Player::remove_powerups()
955 got_power = NONE_POWER;
961 Player::move(const Vector& vector)
965 old_base = previous_base = base;
969 Player::check_bounds(Camera* camera)
971 /* Keep tux in bounds: */
973 { // Lock Tux to the size of the level, so that he doesn't fall of
978 /* Keep in-bounds, vertically: */
979 if (base.y > Sector::current()->solids->get_height() * 32)
986 // can happen if back scrolling is disabled
987 if(base.x < camera->get_translation().x) {
988 base.x = camera->get_translation().x;
991 if(base.x >= camera->get_translation().x + screen->w - base.width) {
992 base.x = camera->get_translation().x + screen->w - base.width;
998 if(collision_object_map(base)) {