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.
35 #include "resources.h"
36 #include "interactive_object.h"
37 #include "screen/screen.h"
39 // behavior definitions:
40 #define TILES_FOR_BUTTJUMP 3
41 // animation times (in ms):
42 #define SHOOTING_TIME 320
43 #define STOMP_TIME 250
45 #define AUTOSCROLL_DEAD_INTERVAL 300
48 Surface* growingtux_left[GROWING_FRAMES];
49 Surface* growingtux_right[GROWING_FRAMES];
53 Sprite* smalltux_gameover;
54 Sprite* smalltux_star;
55 Sprite* largetux_star;
57 PlayerSprite smalltux;
58 PlayerSprite largetux;
64 PlayerKeymap::PlayerKeymap()
66 keymap.jump = SDLK_SPACE;
67 keymap.activate = SDLK_UP;
68 keymap.duck = SDLK_DOWN;
69 keymap.left = SDLK_LEFT;
70 keymap.right = SDLK_RIGHT;
71 keymap.fire = SDLK_LCTRL;
74 void player_input_init(player_input_type* pplayer_input)
76 pplayer_input->down = UP;
77 pplayer_input->fire = UP;
78 pplayer_input->left = UP;
79 pplayer_input->old_fire = UP;
80 pplayer_input->right = UP;
81 pplayer_input->up = UP;
82 pplayer_input->old_up = UP;
83 pplayer_input->activate = UP;
98 holding_something = false;
104 got_power = NONE_POWER;
108 previous_base = old_base = base;
116 fall_mode = ON_GROUND;
121 stomp_pos = Vector(0,0);
125 player_input_init(&input);
127 invincible_timer.init(true);
128 skidding_timer.init(true);
129 safe_timer.init(true);
130 frame_timer.init(true);
131 kick_timer.init(true);
132 shooting_timer.init(true);
133 growing_timer.init(true);
139 Player::key_event(SDLKey key, int state)
141 if(key == keymap.right)
146 else if(key == keymap.left)
151 else if(key == keymap.jump)
156 else if(key == keymap.duck)
161 else if(key == keymap.fire)
168 else if(key == keymap.activate)
170 input.activate = state;
173 /** check for interactive objects */
174 for(Sector::InteractiveObjects::iterator i
175 = Sector::current()->interactive_objects.begin();
176 i != Sector::current()->interactive_objects.end(); ++i) {
177 if(rectcollision(base, (*i)->get_area())) {
178 (*i)->interaction(INTERACTION_ACTIVATE);
190 Player::level_begin()
194 previous_base = old_base = base;
199 player_input_init(&input);
201 invincible_timer.init(true);
202 skidding_timer.init(true);
203 safe_timer.init(true);
204 frame_timer.init(true);
205 growing_timer.init(true);
211 Player::action(float elapsed_time)
213 bool jumped_in_solid = false;
215 if(dying && !dying_timer.check()) {
220 if (input.fire == UP)
221 holding_something = false;
224 previous_base = base;
226 /* --- HANDLE TUX! --- */
227 if(dying == DYING_NOT)
230 physic.apply(elapsed_time, base.x, base.y);
232 if(dying == DYING_NOT)
234 base_type target = base;
236 collision_swept_object_map(&old_base, &base);
238 if ((!invincible_timer.started() && !safe_timer.started())
239 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
240 || isspike(base.x, base.y + base.height)
241 || isspike(base.x + base.width, base.y + base.height)))
246 // Don't accelerate Tux if he is running against a wall
247 if (target.x != base.x)
249 physic.set_velocity_x(0);
252 // special exception for cases where we're stuck under tiles after
253 // being ducked. In this case we drift out
254 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
255 && collision_object_map(base))
257 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
258 previous_base = old_base = base;
264 physic.enable_gravity(true);
268 physic.set_velocity_y(0);
269 jumped_in_solid = true;
276 if (physic.get_velocity_y() < 0)
278 base.y = (int)(((int)base.y / 32) * 32);
279 physic.set_velocity_y(0);
282 physic.enable_gravity(false);
283 /* Reset score multiplier (for multi-hits): */
284 if (!invincible_timer.started())
285 player_status.score_multiplier = 1;
290 if (isbrick(base.x, base.y) ||
291 isfullbox(base.x, base.y))
293 Sector::current()->trygrabdistro(
294 Vector(base.x, base.y - 32), BOUNCE);
295 Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
297 Sector::current()->trybreakbrick(
298 Vector(base.x, base.y), size == SMALL);
300 bumpbrick(base.x, base.y);
301 Sector::current()->tryemptybox(Vector(base.x, base.y), RIGHT);
304 if (isbrick(base.x+ 31, base.y) ||
305 isfullbox(base.x+ 31, base.y))
307 Sector::current()->trygrabdistro(
308 Vector(base.x+ 31, base.y - 32), BOUNCE);
309 Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
312 Sector::current()->trybreakbrick(
313 Vector(base.x+ 31, base.y), size == SMALL);
315 bumpbrick(base.x+ 31, base.y);
316 Sector::current()->tryemptybox(Vector(base.x+ 31, base.y), LEFT);
328 /* Make sure jumping is off. */
334 /* ---- DONE HANDLING TUX! --- */
337 skidding_timer.check();
338 invincible_timer.check();
346 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
347 issolid(base.x + 1, base.y + base.height) ||
348 issolid(base.x + base.width - 1, base.y + base.height));
352 Player::under_solid()
354 return ( issolid(base.x + base.width / 2, base.y) ||
355 issolid(base.x + 1, base.y) ||
356 issolid(base.x + base.width - 1, base.y) );
360 Player::tiles_on_air(int tiles)
362 for(int t = 0; t != tiles; t++)
364 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
365 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
366 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
373 Player::handle_horizontal_input()
375 float vx = physic.get_velocity_x();
376 float vy = physic.get_velocity_y();
377 float ax = physic.get_acceleration_x();
378 float ay = physic.get_acceleration_y();
381 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
385 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
391 if (input.fire == UP) {
392 ax = dirsign * WALK_ACCELERATION_X;
394 if(vx >= MAX_WALK_XM && dirsign > 0) {
397 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
402 ax = dirsign * RUN_ACCELERATION_X;
404 if(vx >= MAX_RUN_XM && dirsign > 0) {
407 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
413 // we can reach WALK_SPEED without any acceleration
414 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
415 vx = dirsign * WALK_SPEED;
418 // changing directions?
419 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
420 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
421 skidding_timer.start(SKID_TIME);
422 sound_manager->play_sound(sounds[SND_SKID]);
429 // we get slower when not pressing any keys
431 if(fabs(vx) < WALK_SPEED) {
435 ax = WALK_ACCELERATION_X * 1.5;
437 ax = WALK_ACCELERATION_X * -1.5;
441 // if we're on ice slow down acceleration or deceleration
442 if (isice(base.x, base.y + base.height))
444 /* the acceleration/deceleration rate on ice is inversely proportional to
445 * the current velocity.
448 // increasing 1 will increase acceleration/deceleration rate
449 // decreasing 1 will decrease acceleration/deceleration rate
450 // must stay above zero, though
451 if (ax != 0) ax *= 1 / fabs(vx);
454 physic.set_velocity(vx, vy);
455 physic.set_acceleration(ax, ay);
459 Player::handle_vertical_input()
463 fall_mode = ON_GROUND;
464 last_ground_y = base.y;
466 if(base.y > last_ground_y)
468 else if(fall_mode == ON_GROUND)
473 if(input.up == DOWN && can_jump && on_ground())
475 if(duck) { // only jump a little bit when in duck mode {
476 physic.set_velocity_y(3);
478 // jump higher if we are running
479 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
480 physic.set_velocity_y(5.8);
482 physic.set_velocity_y(5.2);
489 sound_manager->play_sound(sounds[SND_JUMP]);
491 sound_manager->play_sound(sounds[SND_BIGJUMP]);
493 // Let go of jump key
494 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
497 physic.set_velocity_y(0);
500 /* In case the player has pressed Down while in a certain range of air,
501 enable butt jump action */
502 if (input.down == DOWN && !butt_jump && !duck)
503 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
506 /* When Down is not held anymore, disable butt jump */
507 if(butt_jump && input.down == UP)
511 if (butt_jump && on_ground() && size == BIG)
515 stomp_pos = Vector(base.x - 32, base.y);
517 stomp_pos = Vector(base.x - 32, base.y + 32);
518 stomp_timer.start(STOMP_TIME);
522 // Break bricks beneath Tux
523 if(Sector::current()->trybreakbrick(
524 Vector(base.x + 1, base.y + base.height), false)
525 || Sector::current()->trybreakbrick(
526 Vector(base.x + base.width - 1, base.y + base.height), false))
528 physic.set_velocity_y(2);
532 // Kill nearby badguys
533 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
534 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
535 i != gameobjects.end();
538 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
542 if (fabsf(base.x - badguy->base.x) < 150 &&
543 fabsf(base.y - badguy->base.y) < 60 &&
544 (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
545 issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
551 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
552 issolid(base.x + 1, base.y + base.height + 64) ||
553 issolid(base.x + base.width - 1, base.y + base.height + 64))
557 && input.old_up == UP)
562 if(on_ground()) /* Make sure jumping is off. */
565 input.old_up = input.up;
569 Player::handle_input()
571 /* Handle horizontal movement: */
572 handle_horizontal_input();
576 if (on_ground() && input.up == UP)
578 handle_vertical_input();
581 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
583 if(Sector::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
584 physic.get_velocity_x(), dir))
585 shooting_timer.start(SHOOTING_TIME);
586 input.old_fire = DOWN;
589 /* tux animations: */
590 if(!frame_timer.check())
592 frame_timer.start(25);
593 if (input.right == UP && input.left == UP)
600 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
601 (global_frame_counter % 4) == 0)
602 frame_main = (frame_main + 1) % 4;
612 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
617 // changing base size confuses collision otherwise
618 old_base = previous_base = base;
620 else if(input.down == UP && size == BIG && duck)
622 // try if we can really unduck
625 // when unducking in air we need some space to do so
626 if(on_ground() || !collision_object_map(base)) {
628 // changing base size confuses collision otherwise
629 old_base = previous_base = base;
631 // undo the ducking changes
639 Player::grow(bool animate)
649 growing_timer.start(GROWING_TIME);
651 old_base = previous_base = base;
655 Player::grabdistros()
660 Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
661 Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
662 Sector::current()->trygrabdistro(
663 Vector(base.x, base.y + base.height), NO_BOUNCE);
664 Sector::current()->trygrabdistro(
665 Vector(base.x+ 31, base.y + base.height), NO_BOUNCE);
669 Sector::current()->trygrabdistro(
670 Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
671 Sector::current()->trygrabdistro(
672 Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
677 /* Enough distros for a One-up? */
678 if (player_status.distros >= DISTROS_LIFEUP)
680 player_status.distros = player_status.distros - DISTROS_LIFEUP;
681 if(player_status.lives < MAX_LIVES)
682 ++player_status.lives;
683 /*We want to hear the sound even, if MAX_LIVES is reached*/
684 sound_manager->play_sound(sounds[SND_LIFEUP]);
689 Player::draw(DrawingContext& context)
691 PlayerSprite* sprite;
695 else if (got_power == FIRE_POWER)
697 else if (got_power == ICE_POWER)
702 int layer = LAYER_OBJECTS - 1;
703 Vector pos = Vector(base.x, base.y);
705 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
707 if (dying == DYING_SQUISHED)
709 smalltux_gameover->draw(context, pos, LAYER_FOREGROUNDTILES+1);
713 if(growing_timer.check())
716 context.draw_surface(growingtux_right[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
718 context.draw_surface(growingtux_left[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
720 else if (duck && size != SMALL)
723 sprite->duck_right->draw(context, pos, layer);
725 sprite->duck_left->draw(context, pos, layer);
727 else if (skidding_timer.started())
730 sprite->skid_right->draw(context, pos, layer);
732 sprite->skid_left->draw(context, pos, layer);
734 else if (kick_timer.started())
737 sprite->kick_right->draw(context, pos, layer);
739 sprite->kick_left->draw(context, pos, layer);
741 else if (physic.get_velocity_y() != 0)
744 sprite->jump_right->draw(context, pos, layer);
746 sprite->jump_left->draw(context, pos, layer);
750 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
753 sprite->stand_right->draw(context, pos, layer);
755 sprite->stand_left->draw(context, pos, layer);
760 sprite->walk_right->draw(context, pos, layer);
762 sprite->walk_left->draw(context, pos, layer);
768 // Draw arm overlay graphics when Tux is holding something
769 if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check() && !duck)
772 sprite->grab_right->draw(context, pos, LAYER_OBJECTS + 1);
774 sprite->grab_left->draw(context, pos, LAYER_OBJECTS + 1);
777 // Draw stomp clouds when doing a butt jump
778 if (stomp_timer.check())
779 sprite->stomp->draw(context, stomp_pos, LAYER_OBJECTS + 1);
781 // Draw blinking star overlay
782 if (invincible_timer.started() &&
783 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
785 if (size == SMALL || duck)
786 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
788 largetux_star->draw(context, pos, LAYER_OBJECTS + 2);
792 context.draw_filled_rect(Vector(base.x, base.y),
793 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
797 Player::collision(const MovingObject& other, int collision_type)
800 (void) collision_type;
801 // will be implemented later
805 Player::collision(void* p_c_object, int c_object)
807 BadGuy* pbad_c = NULL;
808 Trampoline* ptramp_c = NULL;
809 FlyingPlatform* pplatform_c = NULL;
814 pbad_c = (BadGuy*) p_c_object;
816 /* Hurt player if he touches a badguy */
817 if (!pbad_c->dying && !dying &&
818 !safe_timer.started() &&
819 pbad_c->mode != BadGuy::HELD)
821 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
822 && !holding_something)
824 holding_something = true;
825 pbad_c->mode = BadGuy::HELD;
828 else if (pbad_c->mode == BadGuy::FLAT)
830 // Don't get hurt if we're kicking a flat badguy!
832 else if (pbad_c->mode == BadGuy::KICK)
834 /* Hurt if you get hit by kicked laptop: */
835 if (!invincible_timer.started())
842 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
843 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
844 || pbad_c->kind == BAD_SPIKY))
848 if (!invincible_timer.started())
857 player_status.score_multiplier++;
862 ptramp_c = (Trampoline*) p_c_object;
864 // Pick up trampoline
865 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
867 holding_something = true;
868 ptramp_c->mode = Trampoline::M_HELD;
869 ptramp_c->base.y -= 8;
871 // Set down trampoline
872 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
874 holding_something = false;
875 ptramp_c->mode = Trampoline::M_NORMAL;
876 ptramp_c->base.y += 8;
877 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
880 // ptramp_c->base.x = base.x + base.width+1;
882 // ptramp_c->base.x = base.x - base.width-1;
885 // Don't let tux walk through trampoline
886 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
888 if (physic.get_velocity_x() > 0) // RIGHT
890 physic.set_velocity_x(0);
891 base.x = ptramp_c->base.x - base.width;
893 else if (physic.get_velocity_x() < 0) // LEFT
895 physic.set_velocity_x(0);
896 base.x = ptramp_c->base.x + ptramp_c->base.width;
901 case CO_FLYING_PLATFORM:
902 pplatform_c = (FlyingPlatform*) p_c_object;
904 base.y = pplatform_c->base.y - base.height;
905 physic.set_velocity_x(pplatform_c->get_vel_x());
907 physic.enable_gravity(false);
909 fall_mode = ON_GROUND;
921 Player::kill(HurtMode mode)
926 sound_manager->play_sound(sounds[SND_HURT]);
928 physic.set_velocity_x(0);
930 if (mode == SHRINK && size == BIG)
932 if (got_power != NONE_POWER)
934 got_power = NONE_POWER;
942 safe_timer.start(TUX_SAFE_TIME);
946 physic.enable_gravity(true);
947 physic.set_acceleration(0, 0);
948 physic.set_velocity(0, 7);
949 --player_status.lives;
950 dying = DYING_SQUISHED;
951 dying_timer.start(3000);
955 /* Remove Tux's power ups */
957 Player::remove_powerups()
959 got_power = NONE_POWER;
965 Player::move(const Vector& vector)
969 old_base = previous_base = base;
973 Player::check_bounds(Camera* camera)
975 /* Keep tux in bounds: */
977 { // Lock Tux to the size of the level, so that he doesn't fall of
982 /* Keep in-bounds, vertically: */
983 if (base.y > Sector::current()->solids->get_height() * 32)
990 // can happen if back scrolling is disabled
991 if(base.x < camera->get_translation().x) {
992 base.x = camera->get_translation().x;
995 if(base.x >= camera->get_translation().x + screen->w - base.width) {
996 base.x = camera->get_translation().x + screen->w - base.width;
1002 if(collision_object_map(base)) {
1010 Player::bounce(BadGuy* badguy)
1013 physic.set_velocity_y(5.2);
1015 physic.set_velocity_y(2);
1017 // Move the player a little bit above the badguy to avoid collision
1018 // between badguy and player directly after the bounce has happend
1019 base.y = badguy->base.y - base.height - 2;