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
46 // time before idle animation starts
47 #define IDLE_TIME 2500
50 Surface* growingtux_left[GROWING_FRAMES];
51 Surface* growingtux_right[GROWING_FRAMES];
55 Sprite* smalltux_gameover;
56 Sprite* smalltux_star;
59 TuxBodyParts* small_tux;
60 TuxBodyParts* big_tux;
61 TuxBodyParts* fire_tux;
62 TuxBodyParts* ice_tux;
66 PlayerKeymap::PlayerKeymap()
68 keymap.jump = SDLK_SPACE;
69 keymap.activate = SDLK_UP;
70 keymap.duck = SDLK_DOWN;
71 keymap.left = SDLK_LEFT;
72 keymap.right = SDLK_RIGHT;
73 keymap.fire = SDLK_LCTRL;
76 void player_input_init(player_input_type* pplayer_input)
78 pplayer_input->down = UP;
79 pplayer_input->fire = UP;
80 pplayer_input->left = UP;
81 pplayer_input->old_fire = UP;
82 pplayer_input->right = UP;
83 pplayer_input->up = UP;
84 pplayer_input->old_up = UP;
85 pplayer_input->activate = UP;
89 TuxBodyParts::set_action(std::string action)
92 head->set_action(action);
94 body->set_action(action);
96 arms->set_action(action);
98 feet->set_action(action);
102 TuxBodyParts::one_time_animation()
105 head->start_animation(1);
107 body->start_animation(1);
109 arms->start_animation(1);
111 feet->start_animation(1);
115 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer,
116 Uint32 drawing_effect)
119 head->draw(context, pos, layer-1, drawing_effect);
121 body->draw(context, pos, layer-3, drawing_effect);
123 arms->draw(context, pos, layer, drawing_effect);
125 feet->draw(context, pos, layer-2, drawing_effect);
140 holding_something = false;
146 got_power = NONE_POWER;
150 previous_base = old_base = base;
158 fall_mode = ON_GROUND;
166 player_input_init(&input);
168 invincible_timer.init(true);
169 skidding_timer.init(true);
170 safe_timer.init(true);
171 frame_timer.init(true);
172 kick_timer.init(true);
173 shooting_timer.init(true);
174 growing_timer.init(true);
175 idle_timer.init(true);
181 Player::key_event(SDLKey key, int state)
183 idle_timer.start(IDLE_TIME);
185 if(key == keymap.right)
190 else if(key == keymap.left)
195 else if(key == keymap.jump)
200 else if(key == keymap.duck)
205 else if(key == keymap.fire)
212 else if(key == keymap.activate)
214 input.activate = state;
217 /** check for interactive objects */
218 for(Sector::InteractiveObjects::iterator i
219 = Sector::current()->interactive_objects.begin();
220 i != Sector::current()->interactive_objects.end(); ++i) {
221 if(rectcollision(base, (*i)->get_area())) {
222 (*i)->interaction(INTERACTION_ACTIVATE);
234 Player::level_begin()
238 previous_base = old_base = base;
243 player_input_init(&input);
245 invincible_timer.init(true);
246 skidding_timer.init(true);
247 safe_timer.init(true);
248 frame_timer.init(true);
249 growing_timer.init(true);
250 idle_timer.init(true);
256 Player::action(float elapsed_time)
258 bool jumped_in_solid = false;
260 if(dying && !dying_timer.check()) {
265 if (input.fire == UP)
266 holding_something = false;
269 previous_base = base;
271 /* --- HANDLE TUX! --- */
272 if(dying == DYING_NOT)
275 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
277 if(dying == DYING_NOT)
279 base_type target = base;
281 collision_swept_object_map(&old_base, &base);
283 if ((!invincible_timer.started() && !safe_timer.started())
284 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
285 || isspike(base.x, base.y + base.height)
286 || isspike(base.x + base.width, base.y + base.height)))
291 // Don't accelerate Tux if he is running against a wall
292 if (target.x != base.x)
294 physic.set_velocity_x(0);
297 // special exception for cases where we're stuck under tiles after
298 // being ducked. In this case we drift out
299 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
300 && collision_object_map(base))
302 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
303 previous_base = old_base = base;
309 physic.enable_gravity(true);
313 physic.set_velocity_y(0);
314 jumped_in_solid = true;
321 if (physic.get_velocity_y() < 0)
323 base.y = (int)(((int)base.y / 32) * 32);
324 physic.set_velocity_y(0);
327 physic.enable_gravity(false);
328 /* Reset score multiplier (for multi-hits): */
329 if (!invincible_timer.started())
330 player_status.score_multiplier = 1;
335 if (isbrick(base.x, base.y) ||
336 isfullbox(base.x, base.y))
338 Sector::current()->trygrabdistro(
339 Vector(base.x, base.y - 32), BOUNCE);
340 Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
342 Sector::current()->trybreakbrick(
343 Vector(base.x, base.y), size == SMALL);
345 bumpbrick(base.x, base.y);
346 Sector::current()->tryemptybox(Vector(base.x, base.y), RIGHT);
349 if (isbrick(base.x+ 31, base.y) ||
350 isfullbox(base.x+ 31, base.y))
352 Sector::current()->trygrabdistro(
353 Vector(base.x+ 31, base.y - 32), BOUNCE);
354 Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
357 Sector::current()->trybreakbrick(
358 Vector(base.x+ 31, base.y), size == SMALL);
360 bumpbrick(base.x+ 31, base.y);
361 Sector::current()->tryemptybox(Vector(base.x+ 31, base.y), LEFT);
373 /* Make sure jumping is off. */
379 /* ---- DONE HANDLING TUX! --- */
382 skidding_timer.check();
383 invincible_timer.check();
391 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
392 issolid(base.x + 1, base.y + base.height) ||
393 issolid(base.x + base.width - 1, base.y + base.height));
397 Player::under_solid()
399 return ( issolid(base.x + base.width / 2, base.y) ||
400 issolid(base.x + 1, base.y) ||
401 issolid(base.x + base.width - 1, base.y) );
405 Player::tiles_on_air(int tiles)
407 for(int t = 0; t != tiles; t++)
409 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
410 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
411 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
418 Player::handle_horizontal_input()
420 float vx = physic.get_velocity_x();
421 float vy = physic.get_velocity_y();
422 float ax = physic.get_acceleration_x();
423 float ay = physic.get_acceleration_y();
426 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
430 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
436 if (input.fire == UP) {
437 ax = dirsign * WALK_ACCELERATION_X;
439 if(vx >= MAX_WALK_XM && dirsign > 0) {
442 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
447 ax = dirsign * RUN_ACCELERATION_X;
449 if(vx >= MAX_RUN_XM && dirsign > 0) {
452 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
458 // we can reach WALK_SPEED without any acceleration
459 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
460 vx = dirsign * WALK_SPEED;
463 // changing directions?
464 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
465 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
466 skidding_timer.start(SKID_TIME);
467 SoundManager::get()->play_sound(IDToSound(SND_SKID));
474 // we get slower when not pressing any keys
476 if(fabs(vx) < WALK_SPEED) {
480 ax = WALK_ACCELERATION_X * 1.5;
482 ax = WALK_ACCELERATION_X * -1.5;
486 // if we're on ice slow down acceleration or deceleration
487 if (isice(base.x, base.y + base.height))
489 /* the acceleration/deceleration rate on ice is inversely proportional to
490 * the current velocity.
493 // increasing 1 will increase acceleration/deceleration rate
494 // decreasing 1 will decrease acceleration/deceleration rate
495 // must stay above zero, though
496 if (ax != 0) ax *= 1 / fabs(vx);
499 physic.set_velocity(vx, vy);
500 physic.set_acceleration(ax, ay);
504 Player::handle_vertical_input()
508 fall_mode = ON_GROUND;
509 last_ground_y = base.y;
511 if(base.y > last_ground_y)
513 else if(fall_mode == ON_GROUND)
518 if(input.up == DOWN && can_jump && on_ground())
520 if(duck) { // only jump a little bit when in duck mode {
521 physic.set_velocity_y(3);
523 // jump higher if we are running
524 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
525 physic.set_velocity_y(5.8);
527 physic.set_velocity_y(5.2);
534 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
536 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
538 // Let go of jump key
539 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
542 physic.set_velocity_y(0);
545 /* In case the player has pressed Down while in a certain range of air,
546 enable butt jump action */
547 if (input.down == DOWN && !butt_jump && !duck)
548 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
551 /* When Down is not held anymore, disable butt jump */
552 if(butt_jump && input.down == UP)
556 if (butt_jump && on_ground() && size == BIG)
560 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y));
562 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y + 32));
566 // Break bricks beneath Tux
567 if(Sector::current()->trybreakbrick(
568 Vector(base.x + 1, base.y + base.height), false)
569 || Sector::current()->trybreakbrick(
570 Vector(base.x + base.width - 1, base.y + base.height), false))
572 physic.set_velocity_y(2);
576 // Kill nearby badguys
577 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
578 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
579 i != gameobjects.end();
582 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
586 if (fabsf(base.x - badguy->base.x) < 150 &&
587 fabsf(base.y - badguy->base.y) < 60 &&
588 (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
589 issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
595 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
596 issolid(base.x + 1, base.y + base.height + 64) ||
597 issolid(base.x + base.width - 1, base.y + base.height + 64))
601 && input.old_up == UP)
606 if(on_ground()) /* Make sure jumping is off. */
609 input.old_up = input.up;
613 Player::handle_input()
615 /* Handle horizontal movement: */
616 handle_horizontal_input();
620 if (on_ground() && input.up == UP)
622 handle_vertical_input();
625 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
627 if(Sector::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
628 physic.get_velocity_x(), dir))
629 shooting_timer.start(SHOOTING_TIME);
630 input.old_fire = DOWN;
633 /* tux animations: */
634 if(!frame_timer.check())
636 frame_timer.start(25);
637 if (input.right == UP && input.left == UP)
644 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
645 (global_frame_counter % 4) == 0)
646 frame_main = (frame_main + 1) % 4;
656 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
661 // changing base size confuses collision otherwise
662 old_base = previous_base = base;
664 else if(input.down == UP && size == BIG && duck)
666 // try if we can really unduck
669 // when unducking in air we need some space to do so
670 if(on_ground() || !collision_object_map(base)) {
672 // changing base size confuses collision otherwise
673 old_base = previous_base = base;
675 // undo the ducking changes
683 Player::grow(bool animate)
693 growing_timer.start(GROWING_TIME);
695 old_base = previous_base = base;
699 Player::grabdistros()
704 Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
705 Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
706 Sector::current()->trygrabdistro(
707 Vector(base.x, base.y + base.height), NO_BOUNCE);
708 Sector::current()->trygrabdistro(
709 Vector(base.x+ 31, base.y + base.height), NO_BOUNCE);
713 Sector::current()->trygrabdistro(
714 Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
715 Sector::current()->trygrabdistro(
716 Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
721 /* Enough distros for a One-up? */
722 if (player_status.distros >= DISTROS_LIFEUP)
724 player_status.distros = player_status.distros - DISTROS_LIFEUP;
725 if(player_status.lives < MAX_LIVES)
726 ++player_status.lives;
727 /*We want to hear the sound even, if MAX_LIVES is reached*/
728 SoundManager::get()->play_sound(IDToSound(SND_LIFEUP));
733 Player::draw(DrawingContext& context)
735 TuxBodyParts* tux_body;
738 tux_body = small_tux;
739 else if (got_power == FIRE_POWER)
741 else if (got_power == ICE_POWER)
746 int layer = LAYER_OBJECTS - 1;
747 Vector pos = Vector(base.x, base.y);
749 /* Set Tux sprite action */
750 if (duck && size == BIG)
753 tux_body->set_action("duck-left");
755 tux_body->set_action("duck-right");
757 else if (skidding_timer.started())
760 tux_body->set_action("skid-left");
762 tux_body->set_action("skid-right");
764 else if (kick_timer.started())
767 tux_body->set_action("kick-left");
769 tux_body->set_action("kick-right");
774 tux_body->set_action("buttjump-left");
776 tux_body->set_action("buttjump-right");
778 else if (physic.get_velocity_y() != 0)
781 tux_body->set_action("jump-left");
783 tux_body->set_action("jump-right");
787 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
790 tux_body->set_action("stand-left");
792 tux_body->set_action("stand-right");
797 tux_body->set_action("walk-left");
799 tux_body->set_action("walk-right");
803 if(idle_timer.get_left() < 0)
808 tux_body->head->set_action("idle-left");
810 tux_body->head->set_action("idle-right");
812 tux_body->head->start_animation(1);
815 idle_timer.start(IDLE_TIME);
818 // Tux is holding something
819 if ((holding_something && physic.get_velocity_y() == 0) ||
820 shooting_timer.check())
825 tux_body->arms->set_action("duck+grab-left");
827 tux_body->arms->set_action("duck+grab-right");
832 tux_body->arms->set_action("grab-left");
834 tux_body->arms->set_action("grab-right");
839 if (dying == DYING_SQUISHED)
841 smalltux_gameover->draw(context, pos, LAYER_FOREGROUNDTILES+1);
843 else if(growing_timer.check())
848 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
849 ((growing_timer.get_gone() *
850 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
852 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
853 ((growing_timer.get_gone() *
854 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
859 context.draw_surface(growingtux_right[(growing_timer.get_gone() *
860 GROWING_FRAMES) / GROWING_TIME], pos, layer);
862 context.draw_surface(growingtux_left[(growing_timer.get_gone() *
863 GROWING_FRAMES) / GROWING_TIME], pos, layer);
866 else if (safe_timer.started() && global_frame_counter%2)
869 tux_body->draw(context, pos, layer, dir == LEFT ? HORIZONTAL_FLIP : NONE_EFFECT);
871 // Draw blinking star overlay
872 if (invincible_timer.started() &&
873 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3)
876 if (size == SMALL || duck)
877 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
879 bigtux_star->draw(context, pos, LAYER_OBJECTS + 2);
883 context.draw_filled_rect(Vector(base.x, base.y),
884 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
888 Player::collision(const MovingObject& other, int collision_type)
891 (void) collision_type;
892 // will be implemented later
896 Player::collision(void* p_c_object, int c_object)
898 BadGuy* pbad_c = NULL;
899 Trampoline* ptramp_c = NULL;
900 FlyingPlatform* pplatform_c = NULL;
905 pbad_c = (BadGuy*) p_c_object;
907 /* Hurt player if he touches a badguy */
908 if (!pbad_c->dying && !dying &&
909 !safe_timer.started() &&
910 pbad_c->mode != BadGuy::HELD)
912 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
913 && !holding_something)
915 holding_something = true;
916 pbad_c->mode = BadGuy::HELD;
919 else if (pbad_c->mode == BadGuy::FLAT)
921 // Don't get hurt if we're kicking a flat badguy!
923 else if (pbad_c->mode == BadGuy::KICK)
925 /* Hurt if you get hit by kicked laptop: */
926 if (!invincible_timer.started())
933 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
934 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
935 || pbad_c->kind == BAD_SPIKY))
939 if (!invincible_timer.started())
948 player_status.score_multiplier++;
953 ptramp_c = (Trampoline*) p_c_object;
955 // Pick up trampoline
956 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
958 holding_something = true;
959 ptramp_c->mode = Trampoline::M_HELD;
960 ptramp_c->base.y -= 8;
962 // Set down trampoline
963 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
965 holding_something = false;
966 ptramp_c->mode = Trampoline::M_NORMAL;
967 ptramp_c->base.y += 8;
968 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
971 // ptramp_c->base.x = base.x + base.width+1;
973 // ptramp_c->base.x = base.x - base.width-1;
976 // Don't let tux walk through trampoline
977 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
979 if (physic.get_velocity_x() > 0) // RIGHT
981 physic.set_velocity_x(0);
982 base.x = ptramp_c->base.x - base.width;
984 else if (physic.get_velocity_x() < 0) // LEFT
986 physic.set_velocity_x(0);
987 base.x = ptramp_c->base.x + ptramp_c->base.width;
992 case CO_FLYING_PLATFORM:
993 pplatform_c = (FlyingPlatform*) p_c_object;
995 base.y = pplatform_c->base.y - base.height;
996 physic.set_velocity_x(pplatform_c->get_vel_x());
998 physic.enable_gravity(false);
1000 fall_mode = ON_GROUND;
1012 Player::kill(HurtMode mode)
1017 SoundManager::get()->play_sound(IDToSound(SND_HURT));
1019 physic.set_velocity_x(0);
1021 if (mode == SHRINK && size == BIG)
1023 if (got_power != NONE_POWER)
1025 safe_timer.start(TUX_SAFE_TIME);
1026 got_power = NONE_POWER;
1030 growing_timer.start(GROWING_TIME);
1031 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
1039 physic.enable_gravity(true);
1040 physic.set_acceleration(0, 0);
1041 physic.set_velocity(0, 7);
1042 --player_status.lives;
1043 dying = DYING_SQUISHED;
1044 dying_timer.start(3000);
1048 /* Remove Tux's power ups */
1050 Player::remove_powerups()
1052 got_power = NONE_POWER;
1058 Player::move(const Vector& vector)
1062 old_base = previous_base = base;
1066 Player::check_bounds(Camera* camera)
1068 /* Keep tux in bounds: */
1070 { // Lock Tux to the size of the level, so that he doesn't fall of
1075 /* Keep in-bounds, vertically: */
1076 if (base.y > Sector::current()->solids->get_height() * 32)
1082 bool adjust = false;
1083 // can happen if back scrolling is disabled
1084 if(base.x < camera->get_translation().x) {
1085 base.x = camera->get_translation().x;
1088 if(base.x >= camera->get_translation().x + screen->w - base.width) {
1089 base.x = camera->get_translation().x + screen->w - base.width;
1095 if(collision_object_map(base)) {
1103 Player::bounce(BadGuy* badguy)
1106 physic.set_velocity_y(5.2);
1108 physic.set_velocity_y(2);
1110 // Move the player a little bit above the badguy to avoid collision
1111 // between badguy and player directly after the bounce has happend
1112 base.y = badguy->base.y - base.height - 2;