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;
120 stomp_pos = Vector(0,0);
124 player_input_init(&input);
126 invincible_timer.init(true);
127 skidding_timer.init(true);
128 safe_timer.init(true);
129 frame_timer.init(true);
130 kick_timer.init(true);
131 shooting_timer.init(true);
132 growing_timer.init(true);
138 Player::key_event(SDLKey key, int state)
140 if(key == keymap.right)
145 else if(key == keymap.left)
150 else if(key == keymap.jump)
155 else if(key == keymap.duck)
160 else if(key == keymap.fire)
167 else if(key == keymap.activate)
169 input.activate = state;
172 /** check for interactive objects */
173 for(Sector::InteractiveObjects::iterator i
174 = Sector::current()->interactive_objects.begin();
175 i != Sector::current()->interactive_objects.end(); ++i) {
176 if(rectcollision(base, (*i)->get_area())) {
177 (*i)->interaction(INTERACTION_ACTIVATE);
189 Player::level_begin()
193 previous_base = old_base = base;
198 player_input_init(&input);
200 invincible_timer.init(true);
201 skidding_timer.init(true);
202 safe_timer.init(true);
203 frame_timer.init(true);
204 growing_timer.init(true);
210 Player::action(float elapsed_time)
212 bool jumped_in_solid = false;
214 if(dying && !dying_timer.check()) {
219 if (input.fire == UP)
220 holding_something = false;
223 previous_base = base;
225 /* --- HANDLE TUX! --- */
226 if(dying == DYING_NOT)
229 physic.apply(elapsed_time, base.x, base.y);
231 if(dying == DYING_NOT)
233 base_type target = base;
235 collision_swept_object_map(&old_base, &base);
237 if ((!invincible_timer.started() && !safe_timer.started())
238 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
239 || isspike(base.x, base.y + base.height)
240 || isspike(base.x + base.width, base.y + base.height)))
245 // Don't accelerate Tux if he is running against a wall
246 if (target.x != base.x)
248 physic.set_velocity_x(0);
251 // special exception for cases where we're stuck under tiles after
252 // being ducked. In this case we drift out
253 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
254 && collision_object_map(base))
256 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
257 previous_base = old_base = base;
263 physic.enable_gravity(true);
267 physic.set_velocity_y(0);
268 jumped_in_solid = true;
275 if (physic.get_velocity_y() < 0)
277 base.y = (int)(((int)base.y / 32) * 32);
278 physic.set_velocity_y(0);
281 physic.enable_gravity(false);
282 /* Reset score multiplier (for multi-hits): */
283 if (!invincible_timer.started())
284 player_status.score_multiplier = 1;
289 if (isbrick(base.x, base.y) ||
290 isfullbox(base.x, base.y))
292 Sector::current()->trygrabdistro(
293 Vector(base.x, base.y - 32), BOUNCE);
294 Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
296 Sector::current()->trybreakbrick(
297 Vector(base.x, base.y), size == SMALL);
299 bumpbrick(base.x, base.y);
300 Sector::current()->tryemptybox(Vector(base.x, base.y), RIGHT);
303 if (isbrick(base.x+ 31, base.y) ||
304 isfullbox(base.x+ 31, base.y))
306 Sector::current()->trygrabdistro(
307 Vector(base.x+ 31, base.y - 32), BOUNCE);
308 Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
311 Sector::current()->trybreakbrick(
312 Vector(base.x+ 31, base.y), size == SMALL);
314 bumpbrick(base.x+ 31, base.y);
315 Sector::current()->tryemptybox(Vector(base.x+ 31, base.y), LEFT);
327 /* Make sure jumping is off. */
333 /* ---- DONE HANDLING TUX! --- */
336 skidding_timer.check();
337 invincible_timer.check();
345 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
346 issolid(base.x + 1, base.y + base.height) ||
347 issolid(base.x + base.width - 1, base.y + base.height));
351 Player::under_solid()
353 return ( issolid(base.x + base.width / 2, base.y) ||
354 issolid(base.x + 1, base.y) ||
355 issolid(base.x + base.width - 1, base.y) );
359 Player::tiles_on_air(int tiles)
361 for(int t = 0; t != tiles; t++)
363 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
364 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
365 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
372 Player::handle_horizontal_input()
374 float vx = physic.get_velocity_x();
375 float vy = physic.get_velocity_y();
376 float ax = physic.get_acceleration_x();
377 float ay = physic.get_acceleration_y();
380 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
384 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
390 if (input.fire == UP) {
391 ax = dirsign * WALK_ACCELERATION_X;
393 if(vx >= MAX_WALK_XM && dirsign > 0) {
396 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
401 ax = dirsign * RUN_ACCELERATION_X;
403 if(vx >= MAX_RUN_XM && dirsign > 0) {
406 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
412 // we can reach WALK_SPEED without any acceleration
413 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
414 vx = dirsign * WALK_SPEED;
417 // changing directions?
418 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
419 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
420 skidding_timer.start(SKID_TIME);
421 sound_manager->play_sound(sounds[SND_SKID]);
428 // we get slower when not pressing any keys
430 if(fabs(vx) < WALK_SPEED) {
434 ax = WALK_ACCELERATION_X * 1.5;
436 ax = WALK_ACCELERATION_X * -1.5;
440 // if we're on ice slow down acceleration or deceleration
441 if (isice(base.x, base.y + base.height))
443 /* the acceleration/deceleration rate on ice is inversely proportional to
444 * the current velocity.
447 // increasing 1 will increase acceleration/deceleration rate
448 // decreasing 1 will decrease acceleration/deceleration rate
449 // must stay above zero, though
450 if (ax != 0) ax *= 1 / fabs(vx);
453 physic.set_velocity(vx, vy);
454 physic.set_acceleration(ax, ay);
458 Player::handle_vertical_input()
462 fall_mode = ON_GROUND;
463 last_ground_y = base.y;
465 if(base.y > last_ground_y)
467 else if(fall_mode == ON_GROUND)
472 if(input.up == DOWN && can_jump && on_ground())
474 if(duck) { // only jump a little bit when in duck mode {
475 physic.set_velocity_y(3);
477 // jump higher if we are running
478 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
479 physic.set_velocity_y(5.8);
481 physic.set_velocity_y(5.2);
488 sound_manager->play_sound(sounds[SND_JUMP]);
490 sound_manager->play_sound(sounds[SND_BIGJUMP]);
492 // Let go of jump key
493 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
496 physic.set_velocity_y(0);
499 /* In case the player has pressed Down while in a certain range of air,
500 enable butt jump action */
501 if (input.down == DOWN && !butt_jump && !duck)
502 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
505 /* When Down is not held anymore, disable butt jump */
506 if(butt_jump && input.down == UP)
510 if (butt_jump && on_ground() && size == BIG)
514 stomp_pos = Vector(base.x - 32, base.y);
516 stomp_pos = Vector(base.x - 32, base.y + 32);
517 stomp_timer.start(STOMP_TIME);
521 // Break bricks beneath Tux
522 if(Sector::current()->trybreakbrick(
523 Vector(base.x + 1, base.y + base.height), false)
524 || Sector::current()->trybreakbrick(
525 Vector(base.x + base.width - 1, base.y + base.height), false))
527 physic.set_velocity_y(2);
531 // Kill nearby badguys
532 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
533 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
534 i != gameobjects.end();
537 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
541 if (fabsf(base.x - badguy->base.x) < 150 &&
542 fabsf(base.y - badguy->base.y) < 60 &&
543 (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
544 issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
550 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
551 issolid(base.x + 1, base.y + base.height + 64) ||
552 issolid(base.x + base.width - 1, base.y + base.height + 64))
556 && input.old_up == UP)
561 if(on_ground()) /* Make sure jumping is off. */
564 input.old_up = input.up;
568 Player::handle_input()
570 /* Handle horizontal movement: */
571 handle_horizontal_input();
575 if (on_ground() && input.up == UP)
577 handle_vertical_input();
580 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
582 if(Sector::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
583 physic.get_velocity_x(), dir))
584 shooting_timer.start(SHOOTING_TIME);
585 input.old_fire = DOWN;
588 /* tux animations: */
589 if(!frame_timer.check())
591 frame_timer.start(25);
592 if (input.right == UP && input.left == UP)
599 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
600 (global_frame_counter % 4) == 0)
601 frame_main = (frame_main + 1) % 4;
611 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
616 // changing base size confuses collision otherwise
617 old_base = previous_base = base;
619 else if(input.down == UP && size == BIG && duck)
621 // try if we can really unduck
624 // when unducking in air we need some space to do so
625 if(on_ground() || !collision_object_map(base)) {
627 // changing base size confuses collision otherwise
628 old_base = previous_base = base;
630 // undo the ducking changes
638 Player::grow(bool animate)
648 growing_timer.start(GROWING_TIME);
650 old_base = previous_base = base;
654 Player::grabdistros()
659 Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
660 Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
661 Sector::current()->trygrabdistro(
662 Vector(base.x, base.y + base.height), NO_BOUNCE);
663 Sector::current()->trygrabdistro(
664 Vector(base.x+ 31, base.y + base.height), NO_BOUNCE);
668 Sector::current()->trygrabdistro(
669 Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
670 Sector::current()->trygrabdistro(
671 Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
676 /* Enough distros for a One-up? */
677 if (player_status.distros >= DISTROS_LIFEUP)
679 player_status.distros = player_status.distros - DISTROS_LIFEUP;
680 if(player_status.lives < MAX_LIVES)
681 ++player_status.lives;
682 /*We want to hear the sound even, if MAX_LIVES is reached*/
683 sound_manager->play_sound(sounds[SND_LIFEUP]);
688 Player::draw(DrawingContext& context)
690 PlayerSprite* sprite;
694 else if (got_power == FIRE_POWER)
696 else if (got_power == ICE_POWER)
701 int layer = LAYER_OBJECTS - 1;
702 Vector pos = Vector(base.x, base.y);
704 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
706 if (dying == DYING_SQUISHED)
708 smalltux_gameover->draw(context, pos, LAYER_FOREGROUNDTILES+1);
712 if(growing_timer.check())
715 context.draw_surface(growingtux_right[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
717 context.draw_surface(growingtux_left[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
719 else if (duck && size != SMALL)
722 sprite->duck_right->draw(context, pos, layer);
724 sprite->duck_left->draw(context, pos, layer);
726 else if (skidding_timer.started())
729 sprite->skid_right->draw(context, pos, layer);
731 sprite->skid_left->draw(context, pos, layer);
733 else if (kick_timer.started())
736 sprite->kick_right->draw(context, pos, layer);
738 sprite->kick_left->draw(context, pos, layer);
740 else if (physic.get_velocity_y() != 0)
743 sprite->jump_right->draw(context, pos, layer);
745 sprite->jump_left->draw(context, pos, layer);
749 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
752 sprite->stand_right->draw(context, pos, layer);
754 sprite->stand_left->draw(context, pos, layer);
759 sprite->walk_right->draw(context, pos, layer);
761 sprite->walk_left->draw(context, pos, layer);
767 // Draw arm overlay graphics when Tux is holding something
768 if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check() && !duck)
771 sprite->grab_right->draw(context, pos, LAYER_OBJECTS + 1);
773 sprite->grab_left->draw(context, pos, LAYER_OBJECTS + 1);
776 // Draw stomp clouds when doing a butt jump
777 if (stomp_timer.check())
778 sprite->stomp->draw(context, stomp_pos, LAYER_OBJECTS + 1);
780 // Draw blinking star overlay
781 if (invincible_timer.started() &&
782 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
784 if (size == SMALL || duck)
785 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
787 largetux_star->draw(context, pos, LAYER_OBJECTS + 2);
791 context.draw_filled_rect(Vector(base.x, base.y),
792 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
796 Player::collision(const MovingObject& other, int collision_type)
799 (void) collision_type;
800 // will be implemented later
804 Player::collision(void* p_c_object, int c_object)
806 BadGuy* pbad_c = NULL;
807 Trampoline* ptramp_c = NULL;
808 FlyingPlatform* pplatform_c = NULL;
813 pbad_c = (BadGuy*) p_c_object;
815 /* Hurt player if he touches a badguy */
816 if (!pbad_c->dying && !dying &&
817 !safe_timer.started() &&
818 pbad_c->mode != BadGuy::HELD)
820 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
821 && !holding_something)
823 holding_something = true;
824 pbad_c->mode = BadGuy::HELD;
827 else if (pbad_c->mode == BadGuy::FLAT)
829 // Don't get hurt if we're kicking a flat badguy!
831 else if (pbad_c->mode == BadGuy::KICK)
833 /* Hurt if you get hit by kicked laptop: */
834 if (!invincible_timer.started())
841 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
842 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
843 || pbad_c->kind == BAD_SPIKY))
847 if (!invincible_timer.started())
856 player_status.score_multiplier++;
861 ptramp_c = (Trampoline*) p_c_object;
863 // Pick up trampoline
864 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
866 holding_something = true;
867 ptramp_c->mode = Trampoline::M_HELD;
868 ptramp_c->base.y -= 8;
870 // Set down trampoline
871 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
873 holding_something = false;
874 ptramp_c->mode = Trampoline::M_NORMAL;
875 ptramp_c->base.y += 8;
876 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
879 // ptramp_c->base.x = base.x + base.width+1;
881 // ptramp_c->base.x = base.x - base.width-1;
884 // Don't let tux walk through trampoline
885 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
887 if (physic.get_velocity_x() > 0) // RIGHT
889 physic.set_velocity_x(0);
890 base.x = ptramp_c->base.x - base.width;
892 else if (physic.get_velocity_x() < 0) // LEFT
894 physic.set_velocity_x(0);
895 base.x = ptramp_c->base.x + ptramp_c->base.width;
900 case CO_FLYING_PLATFORM:
901 pplatform_c = (FlyingPlatform*) p_c_object;
903 base.y = pplatform_c->base.y - base.height;
904 physic.set_velocity_x(pplatform_c->get_vel_x());
906 physic.enable_gravity(false);
908 fall_mode = ON_GROUND;
920 Player::kill(HurtMode mode)
925 sound_manager->play_sound(sounds[SND_HURT]);
927 physic.set_velocity_x(0);
929 if (mode == SHRINK && size == BIG)
931 if (got_power != NONE_POWER)
933 got_power = NONE_POWER;
941 safe_timer.start(TUX_SAFE_TIME);
945 physic.enable_gravity(true);
946 physic.set_acceleration(0, 0);
947 physic.set_velocity(0, 7);
948 --player_status.lives;
949 dying = DYING_SQUISHED;
950 dying_timer.start(3000);
954 /* Remove Tux's power ups */
956 Player::remove_powerups()
958 got_power = NONE_POWER;
964 Player::move(const Vector& vector)
968 old_base = previous_base = base;
972 Player::check_bounds(Camera* camera)
974 /* Keep tux in bounds: */
976 { // Lock Tux to the size of the level, so that he doesn't fall of
981 /* Keep in-bounds, vertically: */
982 if (base.y > Sector::current()->solids->get_height() * 32)
989 // can happen if back scrolling is disabled
990 if(base.x < camera->get_translation().x) {
991 base.x = camera->get_translation().x;
994 if(base.x >= camera->get_translation().x + screen->w - base.width) {
995 base.x = camera->get_translation().x + screen->w - base.width;
1001 if(collision_object_map(base)) {