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.
25 #include "app/globals.h"
30 #include "special/sprite.h"
35 #include "resources.h"
36 #include "interactive_object.h"
37 #include "video/screen.h"
39 // behavior definitions:
40 #define TILES_FOR_BUTTJUMP 3
41 // animation times (in ms):
42 #define SHOOTING_TIME 320
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;
65 PlayerKeymap::PlayerKeymap()
67 keymap.jump = SDLK_SPACE;
68 keymap.activate = SDLK_UP;
69 keymap.duck = SDLK_DOWN;
70 keymap.left = SDLK_LEFT;
71 keymap.right = SDLK_RIGHT;
72 keymap.fire = SDLK_LCTRL;
75 void player_input_init(player_input_type* pplayer_input)
77 pplayer_input->down = UP;
78 pplayer_input->fire = UP;
79 pplayer_input->left = UP;
80 pplayer_input->old_fire = UP;
81 pplayer_input->right = UP;
82 pplayer_input->up = UP;
83 pplayer_input->old_up = UP;
84 pplayer_input->activate = UP;
99 holding_something = false;
105 got_power = NONE_POWER;
109 previous_base = old_base = base;
117 fall_mode = ON_GROUND;
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, Sector::current()->gravity);
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 SoundManager::get()->play_sound(IDToSound(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 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
491 SoundManager::get()->play_sound(IDToSound(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 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y));
517 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y + 32));
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 SoundManager::get()->play_sound(IDToSound(SND_LIFEUP));
688 Player::draw(DrawingContext& context)
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() || growing_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())
717 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
718 ((growing_timer.get_gone() *
719 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
721 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
722 ((growing_timer.get_gone() *
723 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
728 context.draw_surface(growingtux_right[(growing_timer.get_gone() *
729 GROWING_FRAMES) / GROWING_TIME], pos, layer);
731 context.draw_surface(growingtux_left[(growing_timer.get_gone() *
732 GROWING_FRAMES) / GROWING_TIME], pos, layer);
735 else if (duck && size != SMALL)
738 sprite->set_action("duck-right");
740 sprite->set_action("duck-left");
742 else if (skidding_timer.started())
745 sprite->set_action("skid-right");
747 sprite->set_action("skid-left");
749 else if (kick_timer.started())
752 sprite->set_action("kick-right");
754 sprite->set_action("kick-left");
756 else if (physic.get_velocity_y() != 0)
759 sprite->set_action("jump-right");
761 sprite->set_action("jump-left");
765 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
768 sprite->set_action("stand-right");
770 sprite->set_action("stand-left");
775 sprite->set_action("walk-right");
777 sprite->set_action("walk-left");
783 if(dying != DYING_SQUISHED && !growing_timer.check())
784 sprite->draw(context, pos, layer);
786 // Draw arm overlay graphics when Tux is holding something
787 if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check() && !duck)
790 tux_arm->set_action("grab-right");
792 tux_arm->set_action("grab-left");
793 tux_arm->draw(context, pos, LAYER_OBJECTS + 1);
796 // Draw blinking star overlay
797 if (invincible_timer.started() &&
798 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
800 if (size == SMALL || duck)
801 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
803 largetux_star->draw(context, pos, LAYER_OBJECTS + 2);
807 context.draw_filled_rect(Vector(base.x, base.y),
808 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
812 Player::collision(const MovingObject& other, int collision_type)
815 (void) collision_type;
816 // will be implemented later
820 Player::collision(void* p_c_object, int c_object)
822 BadGuy* pbad_c = NULL;
823 Trampoline* ptramp_c = NULL;
824 FlyingPlatform* pplatform_c = NULL;
829 pbad_c = (BadGuy*) p_c_object;
831 /* Hurt player if he touches a badguy */
832 if (!pbad_c->dying && !dying &&
833 !safe_timer.started() &&
834 pbad_c->mode != BadGuy::HELD)
836 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
837 && !holding_something)
839 holding_something = true;
840 pbad_c->mode = BadGuy::HELD;
843 else if (pbad_c->mode == BadGuy::FLAT)
845 // Don't get hurt if we're kicking a flat badguy!
847 else if (pbad_c->mode == BadGuy::KICK)
849 /* Hurt if you get hit by kicked laptop: */
850 if (!invincible_timer.started())
857 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
858 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
859 || pbad_c->kind == BAD_SPIKY))
863 if (!invincible_timer.started())
872 player_status.score_multiplier++;
877 ptramp_c = (Trampoline*) p_c_object;
879 // Pick up trampoline
880 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
882 holding_something = true;
883 ptramp_c->mode = Trampoline::M_HELD;
884 ptramp_c->base.y -= 8;
886 // Set down trampoline
887 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
889 holding_something = false;
890 ptramp_c->mode = Trampoline::M_NORMAL;
891 ptramp_c->base.y += 8;
892 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
895 // ptramp_c->base.x = base.x + base.width+1;
897 // ptramp_c->base.x = base.x - base.width-1;
900 // Don't let tux walk through trampoline
901 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
903 if (physic.get_velocity_x() > 0) // RIGHT
905 physic.set_velocity_x(0);
906 base.x = ptramp_c->base.x - base.width;
908 else if (physic.get_velocity_x() < 0) // LEFT
910 physic.set_velocity_x(0);
911 base.x = ptramp_c->base.x + ptramp_c->base.width;
916 case CO_FLYING_PLATFORM:
917 pplatform_c = (FlyingPlatform*) p_c_object;
919 base.y = pplatform_c->base.y - base.height;
920 physic.set_velocity_x(pplatform_c->get_vel_x());
922 physic.enable_gravity(false);
924 fall_mode = ON_GROUND;
936 Player::kill(HurtMode mode)
941 SoundManager::get()->play_sound(IDToSound(SND_HURT));
943 physic.set_velocity_x(0);
945 if (mode == SHRINK && size == BIG)
947 if (got_power != NONE_POWER)
949 safe_timer.start(TUX_SAFE_TIME);
950 got_power = NONE_POWER;
954 growing_timer.start(GROWING_TIME);
955 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
963 physic.enable_gravity(true);
964 physic.set_acceleration(0, 0);
965 physic.set_velocity(0, 7);
966 --player_status.lives;
967 dying = DYING_SQUISHED;
968 dying_timer.start(3000);
972 /* Remove Tux's power ups */
974 Player::remove_powerups()
976 got_power = NONE_POWER;
982 Player::move(const Vector& vector)
986 old_base = previous_base = base;
990 Player::check_bounds(Camera* camera)
992 /* Keep tux in bounds: */
994 { // Lock Tux to the size of the level, so that he doesn't fall of
999 /* Keep in-bounds, vertically: */
1000 if (base.y > Sector::current()->solids->get_height() * 32)
1006 bool adjust = false;
1007 // can happen if back scrolling is disabled
1008 if(base.x < camera->get_translation().x) {
1009 base.x = camera->get_translation().x;
1012 if(base.x >= camera->get_translation().x + screen->w - base.width) {
1013 base.x = camera->get_translation().x + screen->w - base.width;
1019 if(collision_object_map(base)) {
1027 Player::bounce(BadGuy* badguy)
1030 physic.set_velocity_y(5.2);
1032 physic.set_velocity_y(2);
1034 // Move the player a little bit above the badguy to avoid collision
1035 // between badguy and player directly after the bounce has happend
1036 base.y = badguy->base.y - base.height - 2;