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;
56 PlayerSprite smalltux;
57 PlayerSprite largetux;
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;
97 holding_something = false;
103 got_power = NONE_POWER;
107 previous_base = old_base = base;
115 fall_mode = ON_GROUND;
123 player_input_init(&input);
125 invincible_timer.init(true);
126 skidding_timer.init(true);
127 safe_timer.init(true);
128 frame_timer.init(true);
129 kick_timer.init(true);
130 shooting_timer.init(true);
131 growing_timer.init(true);
137 Player::key_event(SDLKey key, int state)
139 if(key == keymap.right)
144 else if(key == keymap.left)
149 else if(key == keymap.jump)
154 else if(key == keymap.duck)
159 else if(key == keymap.fire)
166 else if(key == keymap.activate)
168 input.activate = state;
171 /** check for interactive objects */
172 for(Sector::InteractiveObjects::iterator i
173 = Sector::current()->interactive_objects.begin();
174 i != Sector::current()->interactive_objects.end(); ++i) {
175 if(rectcollision(base, (*i)->get_area())) {
176 (*i)->interaction(INTERACTION_ACTIVATE);
188 Player::level_begin()
192 previous_base = old_base = base;
197 player_input_init(&input);
199 invincible_timer.init(true);
200 skidding_timer.init(true);
201 safe_timer.init(true);
202 frame_timer.init(true);
203 growing_timer.init(true);
209 Player::action(float elapsed_time)
211 bool jumped_in_solid = false;
213 if(dying && !dying_timer.check()) {
218 if (input.fire == UP)
219 holding_something = false;
222 previous_base = base;
224 /* --- HANDLE TUX! --- */
225 if(dying == DYING_NOT)
228 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
230 if(dying == DYING_NOT)
232 base_type target = base;
234 collision_swept_object_map(&old_base, &base);
236 if ((!invincible_timer.started() && !safe_timer.started())
237 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
238 || isspike(base.x, base.y + base.height)
239 || isspike(base.x + base.width, base.y + base.height)))
244 // Don't accelerate Tux if he is running against a wall
245 if (target.x != base.x)
247 physic.set_velocity_x(0);
250 // special exception for cases where we're stuck under tiles after
251 // being ducked. In this case we drift out
252 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
253 && collision_object_map(base))
255 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
256 previous_base = old_base = base;
262 physic.enable_gravity(true);
266 physic.set_velocity_y(0);
267 jumped_in_solid = true;
274 if (physic.get_velocity_y() < 0)
276 base.y = (int)(((int)base.y / 32) * 32);
277 physic.set_velocity_y(0);
280 physic.enable_gravity(false);
281 /* Reset score multiplier (for multi-hits): */
282 if (!invincible_timer.started())
283 player_status.score_multiplier = 1;
288 if (isbrick(base.x, base.y) ||
289 isfullbox(base.x, base.y))
291 Sector::current()->trygrabdistro(
292 Vector(base.x, base.y - 32), BOUNCE);
293 Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
295 Sector::current()->trybreakbrick(
296 Vector(base.x, base.y), size == SMALL);
298 bumpbrick(base.x, base.y);
299 Sector::current()->tryemptybox(Vector(base.x, base.y), RIGHT);
302 if (isbrick(base.x+ 31, base.y) ||
303 isfullbox(base.x+ 31, base.y))
305 Sector::current()->trygrabdistro(
306 Vector(base.x+ 31, base.y - 32), BOUNCE);
307 Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
310 Sector::current()->trybreakbrick(
311 Vector(base.x+ 31, base.y), size == SMALL);
313 bumpbrick(base.x+ 31, base.y);
314 Sector::current()->tryemptybox(Vector(base.x+ 31, base.y), LEFT);
326 /* Make sure jumping is off. */
332 /* ---- DONE HANDLING TUX! --- */
335 skidding_timer.check();
336 invincible_timer.check();
344 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
345 issolid(base.x + 1, base.y + base.height) ||
346 issolid(base.x + base.width - 1, base.y + base.height));
350 Player::under_solid()
352 return ( issolid(base.x + base.width / 2, base.y) ||
353 issolid(base.x + 1, base.y) ||
354 issolid(base.x + base.width - 1, base.y) );
358 Player::tiles_on_air(int tiles)
360 for(int t = 0; t != tiles; t++)
362 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
363 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
364 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
371 Player::handle_horizontal_input()
373 float vx = physic.get_velocity_x();
374 float vy = physic.get_velocity_y();
375 float ax = physic.get_acceleration_x();
376 float ay = physic.get_acceleration_y();
379 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
383 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
389 if (input.fire == UP) {
390 ax = dirsign * WALK_ACCELERATION_X;
392 if(vx >= MAX_WALK_XM && dirsign > 0) {
395 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
400 ax = dirsign * RUN_ACCELERATION_X;
402 if(vx >= MAX_RUN_XM && dirsign > 0) {
405 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
411 // we can reach WALK_SPEED without any acceleration
412 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
413 vx = dirsign * WALK_SPEED;
416 // changing directions?
417 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
418 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
419 skidding_timer.start(SKID_TIME);
420 SoundManager::get()->play_sound(IDToSound(SND_SKID));
427 // we get slower when not pressing any keys
429 if(fabs(vx) < WALK_SPEED) {
433 ax = WALK_ACCELERATION_X * 1.5;
435 ax = WALK_ACCELERATION_X * -1.5;
439 // if we're on ice slow down acceleration or deceleration
440 if (isice(base.x, base.y + base.height))
442 /* the acceleration/deceleration rate on ice is inversely proportional to
443 * the current velocity.
446 // increasing 1 will increase acceleration/deceleration rate
447 // decreasing 1 will decrease acceleration/deceleration rate
448 // must stay above zero, though
449 if (ax != 0) ax *= 1 / fabs(vx);
452 physic.set_velocity(vx, vy);
453 physic.set_acceleration(ax, ay);
457 Player::handle_vertical_input()
461 fall_mode = ON_GROUND;
462 last_ground_y = base.y;
464 if(base.y > last_ground_y)
466 else if(fall_mode == ON_GROUND)
471 if(input.up == DOWN && can_jump && on_ground())
473 if(duck) { // only jump a little bit when in duck mode {
474 physic.set_velocity_y(3);
476 // jump higher if we are running
477 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
478 physic.set_velocity_y(5.8);
480 physic.set_velocity_y(5.2);
487 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
489 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
491 // Let go of jump key
492 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
495 physic.set_velocity_y(0);
498 /* In case the player has pressed Down while in a certain range of air,
499 enable butt jump action */
500 if (input.down == DOWN && !butt_jump && !duck)
501 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
504 /* When Down is not held anymore, disable butt jump */
505 if(butt_jump && input.down == UP)
509 if (butt_jump && on_ground() && size == BIG)
513 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y));
515 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y + 32));
519 // Break bricks beneath Tux
520 if(Sector::current()->trybreakbrick(
521 Vector(base.x + 1, base.y + base.height), false)
522 || Sector::current()->trybreakbrick(
523 Vector(base.x + base.width - 1, base.y + base.height), false))
525 physic.set_velocity_y(2);
529 // Kill nearby badguys
530 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
531 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
532 i != gameobjects.end();
535 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
539 if (fabsf(base.x - badguy->base.x) < 150 &&
540 fabsf(base.y - badguy->base.y) < 60 &&
541 (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
542 issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
548 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
549 issolid(base.x + 1, base.y + base.height + 64) ||
550 issolid(base.x + base.width - 1, base.y + base.height + 64))
554 && input.old_up == UP)
559 if(on_ground()) /* Make sure jumping is off. */
562 input.old_up = input.up;
566 Player::handle_input()
568 /* Handle horizontal movement: */
569 handle_horizontal_input();
573 if (on_ground() && input.up == UP)
575 handle_vertical_input();
578 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
580 if(Sector::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
581 physic.get_velocity_x(), dir))
582 shooting_timer.start(SHOOTING_TIME);
583 input.old_fire = DOWN;
586 /* tux animations: */
587 if(!frame_timer.check())
589 frame_timer.start(25);
590 if (input.right == UP && input.left == UP)
597 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
598 (global_frame_counter % 4) == 0)
599 frame_main = (frame_main + 1) % 4;
609 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
614 // changing base size confuses collision otherwise
615 old_base = previous_base = base;
617 else if(input.down == UP && size == BIG && duck)
619 // try if we can really unduck
622 // when unducking in air we need some space to do so
623 if(on_ground() || !collision_object_map(base)) {
625 // changing base size confuses collision otherwise
626 old_base = previous_base = base;
628 // undo the ducking changes
636 Player::grow(bool animate)
646 growing_timer.start(GROWING_TIME);
648 old_base = previous_base = base;
652 Player::grabdistros()
657 Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
658 Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
659 Sector::current()->trygrabdistro(
660 Vector(base.x, base.y + base.height), NO_BOUNCE);
661 Sector::current()->trygrabdistro(
662 Vector(base.x+ 31, base.y + base.height), NO_BOUNCE);
666 Sector::current()->trygrabdistro(
667 Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
668 Sector::current()->trygrabdistro(
669 Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
674 /* Enough distros for a One-up? */
675 if (player_status.distros >= DISTROS_LIFEUP)
677 player_status.distros = player_status.distros - DISTROS_LIFEUP;
678 if(player_status.lives < MAX_LIVES)
679 ++player_status.lives;
680 /*We want to hear the sound even, if MAX_LIVES is reached*/
681 SoundManager::get()->play_sound(IDToSound(SND_LIFEUP));
686 Player::draw(DrawingContext& context)
688 PlayerSprite* sprite;
692 else if (got_power == FIRE_POWER)
694 else if (got_power == ICE_POWER)
699 int layer = LAYER_OBJECTS - 1;
700 Vector pos = Vector(base.x, base.y);
702 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
704 if (dying == DYING_SQUISHED)
706 smalltux_gameover->draw(context, pos, LAYER_FOREGROUNDTILES+1);
710 if(growing_timer.check())
713 context.draw_surface(growingtux_right[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
715 context.draw_surface(growingtux_left[(growing_timer.get_gone() * GROWING_FRAMES) / GROWING_TIME], pos, layer);
717 else if (duck && size != SMALL)
720 sprite->duck_right->draw(context, pos, layer);
722 sprite->duck_left->draw(context, pos, layer);
724 else if (skidding_timer.started())
727 sprite->skid_right->draw(context, pos, layer);
729 sprite->skid_left->draw(context, pos, layer);
731 else if (kick_timer.started())
734 sprite->kick_right->draw(context, pos, layer);
736 sprite->kick_left->draw(context, pos, layer);
738 else if (physic.get_velocity_y() != 0)
741 sprite->jump_right->draw(context, pos, layer);
743 sprite->jump_left->draw(context, pos, layer);
747 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
750 sprite->stand_right->draw(context, pos, layer);
752 sprite->stand_left->draw(context, pos, layer);
757 sprite->walk_right->draw(context, pos, layer);
759 sprite->walk_left->draw(context, pos, layer);
765 // Draw arm overlay graphics when Tux is holding something
766 if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check() && !duck)
769 sprite->grab_right->draw(context, pos, LAYER_OBJECTS + 1);
771 sprite->grab_left->draw(context, pos, LAYER_OBJECTS + 1);
774 // Draw blinking star overlay
775 if (invincible_timer.started() &&
776 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
778 if (size == SMALL || duck)
779 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
781 largetux_star->draw(context, pos, LAYER_OBJECTS + 2);
785 context.draw_filled_rect(Vector(base.x, base.y),
786 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
790 Player::collision(const MovingObject& other, int collision_type)
793 (void) collision_type;
794 // will be implemented later
798 Player::collision(void* p_c_object, int c_object)
800 BadGuy* pbad_c = NULL;
801 Trampoline* ptramp_c = NULL;
802 FlyingPlatform* pplatform_c = NULL;
807 pbad_c = (BadGuy*) p_c_object;
809 /* Hurt player if he touches a badguy */
810 if (!pbad_c->dying && !dying &&
811 !safe_timer.started() &&
812 pbad_c->mode != BadGuy::HELD)
814 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
815 && !holding_something)
817 holding_something = true;
818 pbad_c->mode = BadGuy::HELD;
821 else if (pbad_c->mode == BadGuy::FLAT)
823 // Don't get hurt if we're kicking a flat badguy!
825 else if (pbad_c->mode == BadGuy::KICK)
827 /* Hurt if you get hit by kicked laptop: */
828 if (!invincible_timer.started())
835 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
836 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
837 || pbad_c->kind == BAD_SPIKY))
841 if (!invincible_timer.started())
850 player_status.score_multiplier++;
855 ptramp_c = (Trampoline*) p_c_object;
857 // Pick up trampoline
858 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
860 holding_something = true;
861 ptramp_c->mode = Trampoline::M_HELD;
862 ptramp_c->base.y -= 8;
864 // Set down trampoline
865 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
867 holding_something = false;
868 ptramp_c->mode = Trampoline::M_NORMAL;
869 ptramp_c->base.y += 8;
870 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
873 // ptramp_c->base.x = base.x + base.width+1;
875 // ptramp_c->base.x = base.x - base.width-1;
878 // Don't let tux walk through trampoline
879 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
881 if (physic.get_velocity_x() > 0) // RIGHT
883 physic.set_velocity_x(0);
884 base.x = ptramp_c->base.x - base.width;
886 else if (physic.get_velocity_x() < 0) // LEFT
888 physic.set_velocity_x(0);
889 base.x = ptramp_c->base.x + ptramp_c->base.width;
894 case CO_FLYING_PLATFORM:
895 pplatform_c = (FlyingPlatform*) p_c_object;
897 base.y = pplatform_c->base.y - base.height;
898 physic.set_velocity_x(pplatform_c->get_vel_x());
900 physic.enable_gravity(false);
902 fall_mode = ON_GROUND;
914 Player::kill(HurtMode mode)
919 SoundManager::get()->play_sound(IDToSound(SND_HURT));
921 physic.set_velocity_x(0);
923 if (mode == SHRINK && size == BIG)
925 if (got_power != NONE_POWER)
927 got_power = NONE_POWER;
935 safe_timer.start(TUX_SAFE_TIME);
939 physic.enable_gravity(true);
940 physic.set_acceleration(0, 0);
941 physic.set_velocity(0, 7);
942 --player_status.lives;
943 dying = DYING_SQUISHED;
944 dying_timer.start(3000);
948 /* Remove Tux's power ups */
950 Player::remove_powerups()
952 got_power = NONE_POWER;
958 Player::move(const Vector& vector)
962 old_base = previous_base = base;
966 Player::check_bounds(Camera* camera)
968 /* Keep tux in bounds: */
970 { // Lock Tux to the size of the level, so that he doesn't fall of
975 /* Keep in-bounds, vertically: */
976 if (base.y > Sector::current()->solids->get_height() * 32)
983 // can happen if back scrolling is disabled
984 if(base.x < camera->get_translation().x) {
985 base.x = camera->get_translation().x;
988 if(base.x >= camera->get_translation().x + screen->w - base.width) {
989 base.x = camera->get_translation().x + screen->w - base.width;
995 if(collision_object_map(base)) {
1003 Player::bounce(BadGuy* badguy)
1006 physic.set_velocity_y(5.2);
1008 physic.set_velocity_y(2);
1010 // Move the player a little bit above the badguy to avoid collision
1011 // between badguy and player directly after the bounce has happend
1012 base.y = badguy->base.y - base.height - 2;