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
47 Sprite* smalltux_gameover;
48 Sprite* smalltux_star;
49 Sprite* largetux_star;
50 Sprite* growingtux_left;
51 Sprite* growingtux_right;
53 PlayerSprite smalltux;
54 PlayerSprite largetux;
60 PlayerKeymap::PlayerKeymap()
62 keymap.jump = SDLK_SPACE;
63 keymap.activate = SDLK_UP;
64 keymap.duck = SDLK_DOWN;
65 keymap.left = SDLK_LEFT;
66 keymap.right = SDLK_RIGHT;
67 keymap.fire = SDLK_LCTRL;
70 void player_input_init(player_input_type* pplayer_input)
72 pplayer_input->down = UP;
73 pplayer_input->fire = UP;
74 pplayer_input->left = UP;
75 pplayer_input->old_fire = UP;
76 pplayer_input->right = UP;
77 pplayer_input->up = UP;
78 pplayer_input->old_up = UP;
79 pplayer_input->activate = UP;
94 holding_something = false;
100 got_power = NONE_POWER;
104 previous_base = old_base = base;
112 fall_mode = ON_GROUND;
120 player_input_init(&input);
122 invincible_timer.init(true);
123 skidding_timer.init(true);
124 safe_timer.init(true);
125 frame_timer.init(true);
126 kick_timer.init(true);
127 shooting_timer.init(true);
128 growing_timer.init(true);
134 Player::key_event(SDLKey key, int state)
136 if(key == keymap.right)
141 else if(key == keymap.left)
146 else if(key == keymap.jump)
151 else if(key == keymap.duck)
156 else if(key == keymap.fire)
163 else if(key == keymap.activate)
165 input.activate = state;
168 /** check for interactive objects */
169 for(Sector::InteractiveObjects::iterator i
170 = Sector::current()->interactive_objects.begin();
171 i != Sector::current()->interactive_objects.end(); ++i) {
172 if(rectcollision(base, (*i)->get_area())) {
173 (*i)->interaction(INTERACTION_ACTIVATE);
185 Player::level_begin()
189 previous_base = old_base = base;
194 player_input_init(&input);
196 invincible_timer.init(true);
197 skidding_timer.init(true);
198 safe_timer.init(true);
199 frame_timer.init(true);
200 growing_timer.init(true);
206 Player::action(float elapsed_time)
208 bool jumped_in_solid = false;
210 if(dying && !dying_timer.check()) {
215 if (input.fire == UP)
216 holding_something = false;
219 previous_base = base;
221 /* --- HANDLE TUX! --- */
222 if(dying == DYING_NOT)
225 physic.apply(elapsed_time, base.x, base.y);
227 if(dying == DYING_NOT)
229 base_type target = base;
231 collision_swept_object_map(&old_base, &base);
233 if ((!invincible_timer.started() && !safe_timer.started())
234 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
235 || isspike(base.x, base.y + base.height)
236 || isspike(base.x + base.width, base.y + base.height)))
241 // Don't accelerate Tux if he is running against a wall
242 if (target.x != base.x)
244 physic.set_velocity_x(0);
247 // special exception for cases where we're stuck under tiles after
248 // being ducked. In this case we drift out
249 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
250 && collision_object_map(base))
252 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
253 previous_base = old_base = base;
259 physic.enable_gravity(true);
263 physic.set_velocity_y(0);
264 jumped_in_solid = true;
271 if (physic.get_velocity_y() < 0)
273 base.y = (int)(((int)base.y / 32) * 32);
274 physic.set_velocity_y(0);
277 physic.enable_gravity(false);
278 /* Reset score multiplier (for multi-hits): */
279 if (!invincible_timer.started())
280 player_status.score_multiplier = 1;
285 if (isbrick(base.x, base.y) ||
286 isfullbox(base.x, base.y))
288 Sector::current()->trygrabdistro(
289 Vector(base.x, base.y - 32), BOUNCE);
290 Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
292 Sector::current()->trybreakbrick(
293 Vector(base.x, base.y), size == SMALL);
295 bumpbrick(base.x, base.y);
296 Sector::current()->tryemptybox(Vector(base.x, base.y), RIGHT);
299 if (isbrick(base.x+ 31, base.y) ||
300 isfullbox(base.x+ 31, base.y))
302 Sector::current()->trygrabdistro(
303 Vector(base.x+ 31, base.y - 32), BOUNCE);
304 Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
307 Sector::current()->trybreakbrick(
308 Vector(base.x+ 31, base.y), size == SMALL);
310 bumpbrick(base.x+ 31, base.y);
311 Sector::current()->tryemptybox(Vector(base.x+ 31, base.y), LEFT);
323 /* Make sure jumping is off. */
329 /* ---- DONE HANDLING TUX! --- */
332 skidding_timer.check();
333 invincible_timer.check();
341 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
342 issolid(base.x + 1, base.y + base.height) ||
343 issolid(base.x + base.width - 1, base.y + base.height));
347 Player::under_solid()
349 return ( issolid(base.x + base.width / 2, base.y) ||
350 issolid(base.x + 1, base.y) ||
351 issolid(base.x + base.width - 1, base.y) );
355 Player::tiles_on_air(int tiles)
357 for(int t = 0; t != tiles; t++)
359 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
360 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
361 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
368 Player::handle_horizontal_input()
370 float vx = physic.get_velocity_x();
371 float vy = physic.get_velocity_y();
372 float ax = physic.get_acceleration_x();
373 float ay = physic.get_acceleration_y();
376 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
380 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
386 if (input.fire == UP) {
387 ax = dirsign * WALK_ACCELERATION_X;
389 if(vx >= MAX_WALK_XM && dirsign > 0) {
392 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
397 ax = dirsign * RUN_ACCELERATION_X;
399 if(vx >= MAX_RUN_XM && dirsign > 0) {
402 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
408 // we can reach WALK_SPEED without any acceleration
409 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
410 vx = dirsign * WALK_SPEED;
413 // changing directions?
414 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
415 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
416 skidding_timer.start(SKID_TIME);
417 sound_manager->play_sound(sounds[SND_SKID]);
424 // we get slower when not pressing any keys
426 if(fabs(vx) < WALK_SPEED) {
430 ax = WALK_ACCELERATION_X * 1.5;
432 ax = WALK_ACCELERATION_X * -1.5;
436 // if we're on ice slow down acceleration or deceleration
437 if (isice(base.x, base.y + base.height))
439 /* the acceleration/deceleration rate on ice is inversely proportional to
440 * the current velocity.
443 // increasing 1 will increase acceleration/deceleration rate
444 // decreasing 1 will decrease acceleration/deceleration rate
445 // must stay above zero, though
446 if (ax != 0) ax *= 1 / fabs(vx);
449 physic.set_velocity(vx, vy);
450 physic.set_acceleration(ax, ay);
454 Player::handle_vertical_input()
458 fall_mode = ON_GROUND;
459 last_ground_y = base.y;
461 if(base.y > last_ground_y)
463 else if(fall_mode == ON_GROUND)
468 if(input.up == DOWN && can_jump && on_ground())
470 if(duck) { // only jump a little bit when in duck mode {
471 physic.set_velocity_y(3);
473 // jump higher if we are running
474 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
475 physic.set_velocity_y(5.8);
477 physic.set_velocity_y(5.2);
484 sound_manager->play_sound(sounds[SND_JUMP]);
486 sound_manager->play_sound(sounds[SND_BIGJUMP]);
488 // Let go of jump key
489 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
492 physic.set_velocity_y(0);
495 /* In case the player has pressed Down while in a certain range of air,
496 enable butt jump action */
497 if (input.down == DOWN && !butt_jump)
498 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
501 /* When Down is not held anymore, disable butt jump */
502 if(butt_jump && input.down == UP)
506 if (butt_jump && on_ground() && size == BIG)
510 // Break bricks beneath Tux
511 if(Sector::current()->trybreakbrick(
512 Vector(base.x + 1, base.y + base.height), false)
513 || Sector::current()->trybreakbrick(
514 Vector(base.x + base.width - 1, base.y + base.height), false))
516 physic.set_velocity_y(2);
520 // Kill nearby badguys
521 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
522 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
523 i != gameobjects.end();
526 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
529 if (fabsf(base.x - badguy->base.x) < 300 &&
530 fabsf(base.y - badguy->base.y) < 300 &&
531 (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
532 issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
538 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
539 issolid(base.x + 1, base.y + base.height + 64) ||
540 issolid(base.x + base.width - 1, base.y + base.height + 64))
544 && input.old_up == UP)
549 if(on_ground()) /* Make sure jumping is off. */
552 input.old_up = input.up;
556 Player::handle_input()
558 /* Handle horizontal movement: */
559 handle_horizontal_input();
563 if (on_ground() && input.up == UP)
565 handle_vertical_input();
568 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
570 if(Sector::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
571 physic.get_velocity_x(), dir))
572 shooting_timer.start(SHOOTING_TIME);
573 input.old_fire = DOWN;
576 /* tux animations: */
577 if(!frame_timer.check())
579 frame_timer.start(25);
580 if (input.right == UP && input.left == UP)
587 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
588 (global_frame_counter % 4) == 0)
589 frame_main = (frame_main + 1) % 4;
599 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
604 // changing base size confuses collision otherwise
605 old_base = previous_base = base;
607 else if(input.down == UP && size == BIG && duck)
609 // try if we can really unduck
612 // when unducking in air we need some space to do so
613 if(on_ground() || !collision_object_map(base)) {
615 // changing base size confuses collision otherwise
616 old_base = previous_base = base;
618 // undo the ducking changes
626 Player::grow(bool animate)
636 growing_timer.start((int)((growingtux_left->get_frames() / growingtux_left->get_fps()) * 1000));
638 old_base = previous_base = base;
642 Player::grabdistros()
647 Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
648 Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
650 Sector::current()->trygrabdistro(
651 Vector(base.x, base.y + base.height), NO_BOUNCE);
652 Sector::current()->trygrabdistro(
653 Vector(base.x+ 31, base.y + base.height), NO_BOUNCE);
657 Sector::current()->trygrabdistro(
658 Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
659 Sector::current()->trygrabdistro(
660 Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
665 /* Enough distros for a One-up? */
666 if (player_status.distros >= DISTROS_LIFEUP)
668 player_status.distros = player_status.distros - DISTROS_LIFEUP;
669 if(player_status.lives < MAX_LIVES)
670 ++player_status.lives;
671 /*We want to hear the sound even, if MAX_LIVES is reached*/
672 sound_manager->play_sound(sounds[SND_LIFEUP]);
677 Player::draw(DrawingContext& context)
679 PlayerSprite* sprite;
683 else if (got_power == FIRE_POWER)
685 else if (got_power == ICE_POWER)
690 int layer = LAYER_OBJECTS - 1;
691 Vector pos = Vector(base.x, base.y);
693 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
695 if (dying == DYING_SQUISHED)
697 smalltux_gameover->draw(context, pos, LAYER_OBJECTS+1);
701 if(growing_timer.check())
704 growingtux_right->draw(context, pos, layer);
706 growingtux_left->draw(context, pos, layer);
708 else if (duck && size != SMALL)
711 sprite->duck_right->draw(context, pos, layer);
713 sprite->duck_left->draw(context, pos, layer);
715 else if (skidding_timer.started())
718 sprite->skid_right->draw(context, pos, layer);
720 sprite->skid_left->draw(context, pos, layer);
722 else if (kick_timer.started())
725 sprite->kick_right->draw(context, pos, layer);
727 sprite->kick_left->draw(context, pos, layer);
729 else if (physic.get_velocity_y() != 0)
732 sprite->jump_right->draw(context, pos, layer);
734 sprite->jump_left->draw(context, pos, layer);
738 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
741 sprite->stand_right->draw(context, pos, layer);
743 sprite->stand_left->draw(context, pos, layer);
748 sprite->walk_right->draw(context, pos, layer);
750 sprite->walk_left->draw(context, pos, layer);
756 // Draw arm overlay graphics when Tux is holding something
757 if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check() && !duck)
760 sprite->grab_right->draw(context, pos, LAYER_OBJECTS + 1);
762 sprite->grab_left->draw(context, pos, LAYER_OBJECTS + 1);
765 // Draw blinking star overlay
766 if (invincible_timer.started() &&
767 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
769 if (size == SMALL || duck)
770 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
772 largetux_star->draw(context, pos, LAYER_OBJECTS + 2);
776 context.draw_filled_rect(Vector(base.x, base.y),
777 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
781 Player::collision(const MovingObject& other, int collision_type)
784 (void) collision_type;
785 // will be implemented later
789 Player::collision(void* p_c_object, int c_object)
791 BadGuy* pbad_c = NULL;
792 Trampoline* ptramp_c = NULL;
793 FlyingPlatform* pplatform_c = NULL;
798 pbad_c = (BadGuy*) p_c_object;
800 /* Hurt player if he touches a badguy */
801 if (!pbad_c->dying && !dying &&
802 !safe_timer.started() &&
803 pbad_c->mode != BadGuy::HELD)
805 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
806 && !holding_something)
808 holding_something = true;
809 pbad_c->mode = BadGuy::HELD;
812 else if (pbad_c->mode == BadGuy::FLAT)
814 // Don't get hurt if we're kicking a flat badguy!
816 else if (pbad_c->mode == BadGuy::KICK)
818 /* Hurt if you get hit by kicked laptop: */
819 if (!invincible_timer.started())
826 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
827 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
828 || pbad_c->kind == BAD_SPIKY))
832 if (!invincible_timer.started())
841 player_status.score_multiplier++;
846 ptramp_c = (Trampoline*) p_c_object;
848 // Pick up trampoline
849 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
851 holding_something = true;
852 ptramp_c->mode = Trampoline::M_HELD;
853 ptramp_c->base.y -= 8;
855 // Set down trampoline
856 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
858 holding_something = false;
859 ptramp_c->mode = Trampoline::M_NORMAL;
860 ptramp_c->base.y += 8;
861 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
864 // ptramp_c->base.x = base.x + base.width+1;
866 // ptramp_c->base.x = base.x - base.width-1;
869 // Don't let tux walk through trampoline
870 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
872 if (physic.get_velocity_x() > 0) // RIGHT
874 physic.set_velocity_x(0);
875 base.x = ptramp_c->base.x - base.width;
877 else if (physic.get_velocity_x() < 0) // LEFT
879 physic.set_velocity_x(0);
880 base.x = ptramp_c->base.x + ptramp_c->base.width;
885 case CO_FLYING_PLATFORM:
886 pplatform_c = (FlyingPlatform*) p_c_object;
888 base.y = pplatform_c->base.y - base.height;
889 physic.set_velocity_x(pplatform_c->get_vel_x());
891 physic.enable_gravity(false);
893 fall_mode = ON_GROUND;
905 Player::kill(HurtMode mode)
910 sound_manager->play_sound(sounds[SND_HURT]);
912 physic.set_velocity_x(0);
914 if (mode == SHRINK && size == BIG)
916 if (got_power != NONE_POWER)
918 got_power = NONE_POWER;
926 safe_timer.start(TUX_SAFE_TIME);
930 physic.enable_gravity(true);
931 physic.set_acceleration(0, 0);
932 physic.set_velocity(0, 7);
933 --player_status.lives;
934 dying = DYING_SQUISHED;
935 dying_timer.start(3000);
939 /* Remove Tux's power ups */
941 Player::remove_powerups()
943 got_power = NONE_POWER;
949 Player::move(const Vector& vector)
953 old_base = previous_base = base;
957 Player::check_bounds(Camera* camera)
959 /* Keep tux in bounds: */
961 { // Lock Tux to the size of the level, so that he doesn't fall of
966 /* Keep in-bounds, vertically: */
967 if (base.y > Sector::current()->solids->get_height() * 32)
974 // can happen if back scrolling is disabled
975 if(base.x < camera->get_translation().x) {
976 base.x = camera->get_translation().x;
979 if(base.x >= camera->get_translation().x + screen->w - base.width) {
980 base.x = camera->get_translation().x + screen->w - base.width;
986 if(collision_object_map(base)) {