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.
33 #define AUTOSCROLL_DEAD_INTERVAL 300
37 Sprite* smalltux_gameover;
38 Sprite* smalltux_star;
39 Sprite* largetux_star;
41 PlayerSprite smalltux;
42 PlayerSprite largetux;
48 PlayerKeymap::PlayerKeymap()
50 keymap.jump = SDLK_UP;
51 keymap.duck = SDLK_DOWN;
52 keymap.left = SDLK_LEFT;
53 keymap.right = SDLK_RIGHT;
54 keymap.fire = SDLK_LCTRL;
57 void player_input_init(player_input_type* pplayer_input)
59 pplayer_input->down = UP;
60 pplayer_input->fire = UP;
61 pplayer_input->left = UP;
62 pplayer_input->old_fire = UP;
63 pplayer_input->right = UP;
64 pplayer_input->up = UP;
65 pplayer_input->old_up = UP;
71 Level* plevel = World::current()->get_level();
73 holding_something = false;
79 got_power = NONE_POWER;
81 base.x = plevel->start_pos_x;
82 base.y = plevel->start_pos_y;
85 previous_base = old_base = base;
98 player_input_init(&input);
100 invincible_timer.init(true);
101 skidding_timer.init(true);
102 safe_timer.init(true);
103 frame_timer.init(true);
104 kick_timer.init(true);
110 Player::key_event(SDLKey key, int state)
112 if(key == keymap.right)
117 else if(key == keymap.left)
122 else if(key == keymap.jump)
127 else if(key == keymap.duck)
132 else if(key == keymap.fire)
144 Player::level_begin()
150 previous_base = old_base = base;
155 player_input_init(&input);
157 invincible_timer.init(true);
158 skidding_timer.init(true);
159 safe_timer.init(true);
160 frame_timer.init(true);
166 Player::action(double frame_ratio)
168 bool jumped_in_solid = false;
170 if (input.fire == UP)
171 holding_something = false;
174 previous_base = base;
176 /* --- HANDLE TUX! --- */
177 if(dying == DYING_NOT)
180 physic.apply(frame_ratio, base.x, base.y);
182 if(dying == DYING_NOT)
184 base_type target = base;
186 collision_swept_object_map(&old_base, &base);
188 // Don't accelerate Tux if he is running against a wall
189 if (target.x != base.x)
191 physic.set_velocity_x(0);
194 // special exception for cases where we're stuck under tiles after
195 // being ducked. In this case we drift out
196 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
197 && collision_object_map(base))
199 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
200 previous_base = old_base = base;
206 physic.enable_gravity(true);
210 physic.set_velocity_y(0);
211 jumped_in_solid = true;
217 if (physic.get_velocity_y() < 0)
219 base.y = (int)(((int)base.y / 32) * 32);
220 physic.set_velocity_y(0);
223 physic.enable_gravity(false);
224 /* Reset score multiplier (for multi-hits): */
225 if (!invincible_timer.started())
226 player_status.score_multiplier = 1;
231 if (isbrick(base.x, base.y) ||
232 isfullbox(base.x, base.y))
234 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
235 World::current()->trybumpbadguy(base.x, base.y - 64);
237 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
239 bumpbrick(base.x, base.y);
240 World::current()->tryemptybox(base.x, base.y, RIGHT);
243 if (isbrick(base.x+ 31, base.y) ||
244 isfullbox(base.x+ 31, base.y))
246 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
247 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
250 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
252 bumpbrick(base.x+ 31, base.y);
253 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
265 /* Make sure jumping is off. */
271 /* ---- DONE HANDLING TUX! --- */
274 skidding_timer.check();
275 invincible_timer.check();
283 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
284 issolid(base.x + 1, base.y + base.height) ||
285 issolid(base.x + base.width - 1, base.y + base.height) );
289 Player::under_solid()
291 return ( issolid(base.x + base.width / 2, base.y) ||
292 issolid(base.x + 1, base.y) ||
293 issolid(base.x + base.width - 1, base.y) );
297 Player::handle_horizontal_input()
299 float vx = physic.get_velocity_x();
300 float vy = physic.get_velocity_y();
301 float ax = physic.get_acceleration_x();
302 float ay = physic.get_acceleration_y();
305 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
309 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
315 if (input.fire == UP) {
316 ax = dirsign * WALK_ACCELERATION_X;
318 if(vx >= MAX_WALK_XM && dirsign > 0) {
321 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
326 ax = dirsign * RUN_ACCELERATION_X;
328 if(vx >= MAX_RUN_XM && dirsign > 0) {
331 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
337 // we can reach WALK_SPEED without any acceleration
338 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
339 vx = dirsign * WALK_SPEED;
342 // changing directions?
343 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
344 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
345 skidding_timer.start(SKID_TIME);
346 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
353 // we get slower when not pressing any keys
355 if(fabs(vx) < WALK_SPEED) {
359 ax = WALK_ACCELERATION_X * 1.5;
361 ax = WALK_ACCELERATION_X * -1.5;
365 // if we're on ice slow down acceleration or deceleration
366 if (isice(base.x, base.y + base.height))
368 /* the acceleration/deceleration rate on ice is inversely proportional to
369 * the current velocity.
372 // increasing 1 will increase acceleration/deceleration rate
373 // decreasing 1 will decrease acceleration/deceleration rate
374 // must stay above zero, though
375 if (ax != 0) ax *= 1 / fabs(vx);
378 physic.set_velocity(vx, vy);
379 physic.set_acceleration(ax, ay);
383 Player::handle_vertical_input()
386 if(input.up == DOWN && can_jump)
390 // jump higher if we are running
391 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
392 physic.set_velocity_y(5.8);
394 physic.set_velocity_y(5.2);
400 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
402 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
405 // Let go of jump key
406 else if(input.up == UP && jumping)
409 if(physic.get_velocity_y() > 0) {
410 physic.set_velocity_y(0);
414 if (input.down == DOWN && !on_ground() && !duck)
416 else if (input.down == UP)
418 if (input.down == DOWN && butt_jump && on_ground())
420 if (isbrick(base.x, base.y + base.height))
421 World::current()->trybreakbrick(base.x, base.y + base.height, false);
422 if (isbrick(base.x + base.width, base.y + base.height))
423 World::current()->trybreakbrick(base.x + base.width, base.y + base.height, false);
429 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
430 issolid(base.x + 1, base.y + base.height + 64) ||
431 issolid(base.x + base.width - 1, base.y + base.height + 64))
435 && input.old_up == UP)
440 input.old_up = input.up;
444 Player::handle_input()
446 /* Handle horizontal movement: */
447 handle_horizontal_input();
451 if (on_ground() && input.up == UP)
453 if (input.up == DOWN || (input.up == UP && jumping))
455 handle_vertical_input();
460 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
462 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
463 input.old_fire = DOWN;
466 /* tux animations: */
467 if(!frame_timer.check())
469 frame_timer.start(25);
470 if (input.right == UP && input.left == UP)
477 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
478 (global_frame_counter % 4) == 0)
479 frame_main = (frame_main + 1) % 4;
489 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
494 // changing base size confuses collision otherwise
495 old_base = previous_base = base;
497 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
502 // changing base size confuses collision otherwise
503 old_base = previous_base = base;
517 old_base = previous_base = base;
521 Player::grabdistros()
526 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
527 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
529 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
530 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
534 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
535 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
540 /* Enough distros for a One-up? */
541 if (player_status.distros >= DISTROS_LIFEUP)
543 player_status.distros = player_status.distros - DISTROS_LIFEUP;
544 if(player_status.lives < MAX_LIVES)
545 ++player_status.lives;
546 /*We want to hear the sound even, if MAX_LIVES is reached*/
547 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
554 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
556 if (dying == DYING_SQUISHED)
558 smalltux_gameover->draw(base.x, base.y);
562 PlayerSprite* sprite;
566 else if (got_power == FIRE_POWER)
568 else if (got_power == ICE_POWER)
573 if (duck && size != SMALL)
576 sprite->duck_right->draw(base.x, base.y);
578 sprite->duck_left->draw(base.x, base.y);
580 else if (skidding_timer.started())
583 sprite->skid_right->draw(base.x, base.y);
585 sprite->skid_left->draw(base.x, base.y);
587 else if (kick_timer.started())
590 sprite->kick_right->draw(base.x, base.y);
592 sprite->kick_left->draw(base.x, base.y);
594 else if (physic.get_velocity_y() != 0)
597 sprite->jump_right->draw(base.x, base.y);
599 sprite->jump_left->draw(base.x, base.y);
603 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
606 sprite->stand_right->draw( base.x, base.y);
608 sprite->stand_left->draw( base.x, base.y);
613 sprite->walk_right->draw(base.x, base.y);
615 sprite->walk_left->draw(base.x, base.y);
619 // Draw arm overlay graphics when Tux is holding something
620 if (holding_something && physic.get_velocity_y() == 0)
623 sprite->grab_right->draw(base.x, base.y);
625 sprite->grab_left->draw(base.x, base.y);
628 // Draw blinking star overlay
629 if (invincible_timer.started() &&
630 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
632 if (size == SMALL || duck)
633 smalltux_star->draw(base.x, base.y);
635 largetux_star->draw(base.x, base.y);
641 fillrect(base.x - scroll_x, base.y - scroll_y,
642 base.width, base.height, 75,75,75, 150);
646 Player::collision(void* p_c_object, int c_object)
648 BadGuy* pbad_c = NULL;
649 Trampoline* ptramp_c = NULL;
654 pbad_c = (BadGuy*) p_c_object;
656 /* Hurt player if he touches a badguy */
657 if (!pbad_c->dying && !dying &&
658 !safe_timer.started() &&
659 pbad_c->mode != BadGuy::HELD)
661 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
662 && !holding_something)
664 holding_something = true;
665 pbad_c->mode = BadGuy::HELD;
668 else if (pbad_c->mode == BadGuy::FLAT)
670 // Don't get hurt if we're kicking a flat badguy!
672 else if (pbad_c->mode == BadGuy::KICK)
674 /* Hurt if you get hit by kicked laptop: */
675 if (!invincible_timer.started())
682 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
683 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
684 || pbad_c->kind == BAD_SPIKY))
688 if (!invincible_timer.started())
697 player_status.score_multiplier++;
702 ptramp_c = (Trampoline*) p_c_object;
704 // Pick up trampoline
705 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something)
707 holding_something = true;
708 ptramp_c->mode = Trampoline::M_HELD;
709 ptramp_c->base.y -= 8;
711 // Set down trampoline
712 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
714 holding_something = false;
715 ptramp_c->mode = Trampoline::M_NORMAL;
716 ptramp_c->base.y += 8;
717 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
720 // ptramp_c->base.x = base.x + base.width+1;
722 // ptramp_c->base.x = base.x - base.width-1;
725 // Don't let tux walk through trampoline
726 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
728 if (physic.get_velocity_x() > 0) // RIGHT
730 physic.set_velocity_x(0);
731 base.x = ptramp_c->base.x - base.width;
733 else if (physic.get_velocity_x() < 0) // LEFT
735 physic.set_velocity_x(0);
736 base.x = ptramp_c->base.x + ptramp_c->base.width;
752 Player::kill(HurtMode mode)
754 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
756 physic.set_velocity_x(0);
758 if (mode == SHRINK && size == BIG)
760 if (got_power != NONE_POWER)
762 got_power = NONE_POWER;
770 safe_timer.start(TUX_SAFE_TIME);
774 physic.enable_gravity(true);
775 physic.set_acceleration(0, 0);
776 physic.set_velocity(0, 7);
777 if(dying != DYING_SQUISHED)
778 --player_status.lives;
779 dying = DYING_SQUISHED;
790 bool Player::is_dead()
792 if(base.y > screen->h || base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // last condition can happen in auto-scrolling
798 /* Remove Tux's power ups */
800 Player::remove_powerups()
802 got_power = NONE_POWER;
808 Player::check_bounds(bool back_scrolling, bool hor_autoscroll)
810 /* Keep tux in bounds: */
812 { // Lock Tux to the size of the level, so that he doesn't fall of
817 /* Keep in-bounds, vertically: */
818 if (base.y > screen->h)
823 if(base.x < scroll_x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled
828 if(base.x == scroll_x)
829 if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32)))
832 if(base.x + base.width > scroll_x + screen->w)
833 base.x = scroll_x + screen->w - base.width;