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;
56 TuxBodyParts* small_tux;
57 TuxBodyParts* big_tux;
58 TuxBodyParts* fire_tux;
59 TuxBodyParts* ice_tux;
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;
86 TuxBodyParts::set_action(std::string action)
88 head->set_action(action);
89 body->set_action(action);
90 arms->set_action(action);
91 feet->set_action(action);
95 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer,
96 Uint32 drawing_effect)
98 head->draw(context, pos, layer, drawing_effect);
99 body->draw(context, pos, layer, drawing_effect);
100 arms->draw(context, pos, layer, drawing_effect);
101 feet->draw(context, pos, layer, drawing_effect);
116 holding_something = false;
122 got_power = NONE_POWER;
126 previous_base = old_base = base;
134 fall_mode = ON_GROUND;
142 player_input_init(&input);
144 invincible_timer.init(true);
145 skidding_timer.init(true);
146 safe_timer.init(true);
147 frame_timer.init(true);
148 kick_timer.init(true);
149 shooting_timer.init(true);
150 growing_timer.init(true);
156 Player::key_event(SDLKey key, int state)
158 if(key == keymap.right)
163 else if(key == keymap.left)
168 else if(key == keymap.jump)
173 else if(key == keymap.duck)
178 else if(key == keymap.fire)
185 else if(key == keymap.activate)
187 input.activate = state;
190 /** check for interactive objects */
191 for(Sector::InteractiveObjects::iterator i
192 = Sector::current()->interactive_objects.begin();
193 i != Sector::current()->interactive_objects.end(); ++i) {
194 if(rectcollision(base, (*i)->get_area())) {
195 (*i)->interaction(INTERACTION_ACTIVATE);
207 Player::level_begin()
211 previous_base = old_base = base;
216 player_input_init(&input);
218 invincible_timer.init(true);
219 skidding_timer.init(true);
220 safe_timer.init(true);
221 frame_timer.init(true);
222 growing_timer.init(true);
228 Player::action(float elapsed_time)
230 bool jumped_in_solid = false;
232 if(dying && !dying_timer.check()) {
237 if (input.fire == UP)
238 holding_something = false;
241 previous_base = base;
243 /* --- HANDLE TUX! --- */
244 if(dying == DYING_NOT)
247 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
249 if(dying == DYING_NOT)
251 base_type target = base;
253 collision_swept_object_map(&old_base, &base);
255 if ((!invincible_timer.started() && !safe_timer.started())
256 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
257 || isspike(base.x, base.y + base.height)
258 || isspike(base.x + base.width, base.y + base.height)))
263 // Don't accelerate Tux if he is running against a wall
264 if (target.x != base.x)
266 physic.set_velocity_x(0);
269 // special exception for cases where we're stuck under tiles after
270 // being ducked. In this case we drift out
271 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
272 && collision_object_map(base))
274 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
275 previous_base = old_base = base;
281 physic.enable_gravity(true);
285 physic.set_velocity_y(0);
286 jumped_in_solid = true;
293 if (physic.get_velocity_y() < 0)
295 base.y = (int)(((int)base.y / 32) * 32);
296 physic.set_velocity_y(0);
299 physic.enable_gravity(false);
300 /* Reset score multiplier (for multi-hits): */
301 if (!invincible_timer.started())
302 player_status.score_multiplier = 1;
307 if (isbrick(base.x, base.y) ||
308 isfullbox(base.x, base.y))
310 Sector::current()->trygrabdistro(
311 Vector(base.x, base.y - 32), BOUNCE);
312 Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
314 Sector::current()->trybreakbrick(
315 Vector(base.x, base.y), size == SMALL);
317 bumpbrick(base.x, base.y);
318 Sector::current()->tryemptybox(Vector(base.x, base.y), RIGHT);
321 if (isbrick(base.x+ 31, base.y) ||
322 isfullbox(base.x+ 31, base.y))
324 Sector::current()->trygrabdistro(
325 Vector(base.x+ 31, base.y - 32), BOUNCE);
326 Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
329 Sector::current()->trybreakbrick(
330 Vector(base.x+ 31, base.y), size == SMALL);
332 bumpbrick(base.x+ 31, base.y);
333 Sector::current()->tryemptybox(Vector(base.x+ 31, base.y), LEFT);
345 /* Make sure jumping is off. */
351 /* ---- DONE HANDLING TUX! --- */
354 skidding_timer.check();
355 invincible_timer.check();
363 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
364 issolid(base.x + 1, base.y + base.height) ||
365 issolid(base.x + base.width - 1, base.y + base.height));
369 Player::under_solid()
371 return ( issolid(base.x + base.width / 2, base.y) ||
372 issolid(base.x + 1, base.y) ||
373 issolid(base.x + base.width - 1, base.y) );
377 Player::tiles_on_air(int tiles)
379 for(int t = 0; t != tiles; t++)
381 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
382 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
383 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
390 Player::handle_horizontal_input()
392 float vx = physic.get_velocity_x();
393 float vy = physic.get_velocity_y();
394 float ax = physic.get_acceleration_x();
395 float ay = physic.get_acceleration_y();
398 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
402 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
408 if (input.fire == UP) {
409 ax = dirsign * WALK_ACCELERATION_X;
411 if(vx >= MAX_WALK_XM && dirsign > 0) {
414 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
419 ax = dirsign * RUN_ACCELERATION_X;
421 if(vx >= MAX_RUN_XM && dirsign > 0) {
424 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
430 // we can reach WALK_SPEED without any acceleration
431 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
432 vx = dirsign * WALK_SPEED;
435 // changing directions?
436 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
437 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
438 skidding_timer.start(SKID_TIME);
439 SoundManager::get()->play_sound(IDToSound(SND_SKID));
446 // we get slower when not pressing any keys
448 if(fabs(vx) < WALK_SPEED) {
452 ax = WALK_ACCELERATION_X * 1.5;
454 ax = WALK_ACCELERATION_X * -1.5;
458 // if we're on ice slow down acceleration or deceleration
459 if (isice(base.x, base.y + base.height))
461 /* the acceleration/deceleration rate on ice is inversely proportional to
462 * the current velocity.
465 // increasing 1 will increase acceleration/deceleration rate
466 // decreasing 1 will decrease acceleration/deceleration rate
467 // must stay above zero, though
468 if (ax != 0) ax *= 1 / fabs(vx);
471 physic.set_velocity(vx, vy);
472 physic.set_acceleration(ax, ay);
476 Player::handle_vertical_input()
480 fall_mode = ON_GROUND;
481 last_ground_y = base.y;
483 if(base.y > last_ground_y)
485 else if(fall_mode == ON_GROUND)
490 if(input.up == DOWN && can_jump && on_ground())
492 if(duck) { // only jump a little bit when in duck mode {
493 physic.set_velocity_y(3);
495 // jump higher if we are running
496 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
497 physic.set_velocity_y(5.8);
499 physic.set_velocity_y(5.2);
506 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
508 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
510 // Let go of jump key
511 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
514 physic.set_velocity_y(0);
517 /* In case the player has pressed Down while in a certain range of air,
518 enable butt jump action */
519 if (input.down == DOWN && !butt_jump && !duck)
520 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
523 /* When Down is not held anymore, disable butt jump */
524 if(butt_jump && input.down == UP)
528 if (butt_jump && on_ground() && size == BIG)
532 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y));
534 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y + 32));
538 // Break bricks beneath Tux
539 if(Sector::current()->trybreakbrick(
540 Vector(base.x + 1, base.y + base.height), false)
541 || Sector::current()->trybreakbrick(
542 Vector(base.x + base.width - 1, base.y + base.height), false))
544 physic.set_velocity_y(2);
548 // Kill nearby badguys
549 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
550 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
551 i != gameobjects.end();
554 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
558 if (fabsf(base.x - badguy->base.x) < 150 &&
559 fabsf(base.y - badguy->base.y) < 60 &&
560 (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
561 issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
567 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
568 issolid(base.x + 1, base.y + base.height + 64) ||
569 issolid(base.x + base.width - 1, base.y + base.height + 64))
573 && input.old_up == UP)
578 if(on_ground()) /* Make sure jumping is off. */
581 input.old_up = input.up;
585 Player::handle_input()
587 /* Handle horizontal movement: */
588 handle_horizontal_input();
592 if (on_ground() && input.up == UP)
594 handle_vertical_input();
597 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
599 if(Sector::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
600 physic.get_velocity_x(), dir))
601 shooting_timer.start(SHOOTING_TIME);
602 input.old_fire = DOWN;
605 /* tux animations: */
606 if(!frame_timer.check())
608 frame_timer.start(25);
609 if (input.right == UP && input.left == UP)
616 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
617 (global_frame_counter % 4) == 0)
618 frame_main = (frame_main + 1) % 4;
628 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
633 // changing base size confuses collision otherwise
634 old_base = previous_base = base;
636 else if(input.down == UP && size == BIG && duck)
638 // try if we can really unduck
641 // when unducking in air we need some space to do so
642 if(on_ground() || !collision_object_map(base)) {
644 // changing base size confuses collision otherwise
645 old_base = previous_base = base;
647 // undo the ducking changes
655 Player::grow(bool animate)
665 growing_timer.start(GROWING_TIME);
667 old_base = previous_base = base;
671 Player::grabdistros()
676 Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
677 Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
678 Sector::current()->trygrabdistro(
679 Vector(base.x, base.y + base.height), NO_BOUNCE);
680 Sector::current()->trygrabdistro(
681 Vector(base.x+ 31, base.y + base.height), NO_BOUNCE);
685 Sector::current()->trygrabdistro(
686 Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
687 Sector::current()->trygrabdistro(
688 Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
693 /* Enough distros for a One-up? */
694 if (player_status.distros >= DISTROS_LIFEUP)
696 player_status.distros = player_status.distros - DISTROS_LIFEUP;
697 if(player_status.lives < MAX_LIVES)
698 ++player_status.lives;
699 /*We want to hear the sound even, if MAX_LIVES is reached*/
700 SoundManager::get()->play_sound(IDToSound(SND_LIFEUP));
705 Player::draw(DrawingContext& context)
707 TuxBodyParts* tux_body;
710 tux_body = small_tux;
711 else if (got_power == FIRE_POWER)
713 else if (got_power == ICE_POWER)
718 int layer = LAYER_OBJECTS - 1;
719 Vector pos = Vector(base.x, base.y);
721 if ((!safe_timer.started() || growing_timer.started()) || (global_frame_counter % 2) == 0)
723 if (dying == DYING_SQUISHED)
725 smalltux_gameover->draw(context, pos, LAYER_FOREGROUNDTILES+1);
729 if(growing_timer.check())
734 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
735 ((growing_timer.get_gone() *
736 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
738 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
739 ((growing_timer.get_gone() *
740 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
745 context.draw_surface(growingtux_right[(growing_timer.get_gone() *
746 GROWING_FRAMES) / GROWING_TIME], pos, layer);
748 context.draw_surface(growingtux_left[(growing_timer.get_gone() *
749 GROWING_FRAMES) / GROWING_TIME], pos, layer);
752 else if (duck && size != SMALL)
755 tux_body->set_action("duck-right");
757 tux_body->set_action("duck-left");
760 else if (skidding_timer.started())
763 tux_body->set_action("skid-right");
765 tux_body->set_action("skid-left");
767 else if (kick_timer.started())
770 tux_body->set_action("kick-right");
772 tux_body->set_action("kick-left");
774 else if (physic.get_velocity_y() != 0)
777 tux_body->set_action("jump-right");
779 tux_body->set_action("jump-left");
783 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
786 tux_body->set_action("stand-right");
788 tux_body->set_action("stand-left");
793 tux_body->set_action("walk-right");
795 tux_body->set_action("walk-left");
801 // Tux is holding something
802 if ((holding_something && physic.get_velocity_y() == 0) ||
803 shooting_timer.check() && !duck)
806 tux_body->arms->set_action("grab-right");
808 tux_body->arms->set_action("grab-left");
811 if(dying != DYING_SQUISHED && !growing_timer.check())
812 tux_body->draw(context, pos, layer);
814 // Draw blinking star overlay
815 if (invincible_timer.started() &&
816 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
818 if (size == SMALL || duck)
819 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
821 bigtux_star->draw(context, pos, LAYER_OBJECTS + 2);
825 context.draw_filled_rect(Vector(base.x, base.y),
826 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
830 Player::collision(const MovingObject& other, int collision_type)
833 (void) collision_type;
834 // will be implemented later
838 Player::collision(void* p_c_object, int c_object)
840 BadGuy* pbad_c = NULL;
841 Trampoline* ptramp_c = NULL;
842 FlyingPlatform* pplatform_c = NULL;
847 pbad_c = (BadGuy*) p_c_object;
849 /* Hurt player if he touches a badguy */
850 if (!pbad_c->dying && !dying &&
851 !safe_timer.started() &&
852 pbad_c->mode != BadGuy::HELD)
854 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
855 && !holding_something)
857 holding_something = true;
858 pbad_c->mode = BadGuy::HELD;
861 else if (pbad_c->mode == BadGuy::FLAT)
863 // Don't get hurt if we're kicking a flat badguy!
865 else if (pbad_c->mode == BadGuy::KICK)
867 /* Hurt if you get hit by kicked laptop: */
868 if (!invincible_timer.started())
875 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
876 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
877 || pbad_c->kind == BAD_SPIKY))
881 if (!invincible_timer.started())
890 player_status.score_multiplier++;
895 ptramp_c = (Trampoline*) p_c_object;
897 // Pick up trampoline
898 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
900 holding_something = true;
901 ptramp_c->mode = Trampoline::M_HELD;
902 ptramp_c->base.y -= 8;
904 // Set down trampoline
905 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
907 holding_something = false;
908 ptramp_c->mode = Trampoline::M_NORMAL;
909 ptramp_c->base.y += 8;
910 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
913 // ptramp_c->base.x = base.x + base.width+1;
915 // ptramp_c->base.x = base.x - base.width-1;
918 // Don't let tux walk through trampoline
919 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
921 if (physic.get_velocity_x() > 0) // RIGHT
923 physic.set_velocity_x(0);
924 base.x = ptramp_c->base.x - base.width;
926 else if (physic.get_velocity_x() < 0) // LEFT
928 physic.set_velocity_x(0);
929 base.x = ptramp_c->base.x + ptramp_c->base.width;
934 case CO_FLYING_PLATFORM:
935 pplatform_c = (FlyingPlatform*) p_c_object;
937 base.y = pplatform_c->base.y - base.height;
938 physic.set_velocity_x(pplatform_c->get_vel_x());
940 physic.enable_gravity(false);
942 fall_mode = ON_GROUND;
954 Player::kill(HurtMode mode)
959 SoundManager::get()->play_sound(IDToSound(SND_HURT));
961 physic.set_velocity_x(0);
963 if (mode == SHRINK && size == BIG)
965 if (got_power != NONE_POWER)
967 safe_timer.start(TUX_SAFE_TIME);
968 got_power = NONE_POWER;
972 growing_timer.start(GROWING_TIME);
973 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
981 physic.enable_gravity(true);
982 physic.set_acceleration(0, 0);
983 physic.set_velocity(0, 7);
984 --player_status.lives;
985 dying = DYING_SQUISHED;
986 dying_timer.start(3000);
990 /* Remove Tux's power ups */
992 Player::remove_powerups()
994 got_power = NONE_POWER;
1000 Player::move(const Vector& vector)
1004 old_base = previous_base = base;
1008 Player::check_bounds(Camera* camera)
1010 /* Keep tux in bounds: */
1012 { // Lock Tux to the size of the level, so that he doesn't fall of
1017 /* Keep in-bounds, vertically: */
1018 if (base.y > Sector::current()->solids->get_height() * 32)
1024 bool adjust = false;
1025 // can happen if back scrolling is disabled
1026 if(base.x < camera->get_translation().x) {
1027 base.x = camera->get_translation().x;
1030 if(base.x >= camera->get_translation().x + screen->w - base.width) {
1031 base.x = camera->get_translation().x + screen->w - base.width;
1037 if(collision_object_map(base)) {
1045 Player::bounce(BadGuy* badguy)
1048 physic.set_velocity_y(5.2);
1050 physic.set_velocity_y(2);
1052 // Move the player a little bit above the badguy to avoid collision
1053 // between badguy and player directly after the bounce has happend
1054 base.y = badguy->base.y - base.height - 2;