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.
31 #define AUTOSCROLL_DEAD_INTERVAL 300
35 Sprite* smalltux_gameover;
36 Sprite* smalltux_star;
37 Sprite* largetux_star;
39 PlayerSprite smalltux;
40 PlayerSprite largetux;
46 PlayerKeymap::PlayerKeymap()
48 keymap.jump = SDLK_UP;
49 keymap.duck = SDLK_DOWN;
50 keymap.left = SDLK_LEFT;
51 keymap.right = SDLK_RIGHT;
52 keymap.fire = SDLK_LCTRL;
55 void player_input_init(player_input_type* pplayer_input)
57 pplayer_input->down = UP;
58 pplayer_input->fire = UP;
59 pplayer_input->left = UP;
60 pplayer_input->old_fire = UP;
61 pplayer_input->right = UP;
62 pplayer_input->up = UP;
63 pplayer_input->old_up = UP;
69 Level* plevel = World::current()->get_level();
71 holding_something = false;
77 got_power = NONE_POWER;
79 base.x = plevel->start_pos_x;
80 base.y = plevel->start_pos_y;
83 previous_base = old_base = base;
96 player_input_init(&input);
98 invincible_timer.init(true);
99 skidding_timer.init(true);
100 safe_timer.init(true);
101 frame_timer.init(true);
102 kick_timer.init(true);
108 Player::key_event(SDLKey key, int state)
110 if(key == keymap.right)
115 else if(key == keymap.left)
120 else if(key == keymap.jump)
125 else if(key == keymap.duck)
130 else if(key == keymap.fire)
142 Player::level_begin()
148 previous_base = old_base = base;
153 player_input_init(&input);
155 invincible_timer.init(true);
156 skidding_timer.init(true);
157 safe_timer.init(true);
158 frame_timer.init(true);
164 Player::action(double frame_ratio)
166 bool jumped_in_solid = false;
168 if (input.fire == UP)
169 holding_something = false;
172 previous_base = base;
174 /* --- HANDLE TUX! --- */
175 if(dying == DYING_NOT)
178 physic.apply(frame_ratio, base.x, base.y);
180 if(dying == DYING_NOT)
182 base_type target = base;
184 collision_swept_object_map(&old_base, &base);
186 // Don't accelerate Tux if he is running against a wall
187 if (target.x != base.x)
189 physic.set_velocity_x(0);
192 // special exception for cases where we're stuck under tiles after
193 // being ducked. In this case we drift out
194 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
195 && collision_object_map(base))
197 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
198 previous_base = old_base = base;
204 physic.enable_gravity(true);
208 physic.set_velocity_y(0);
209 jumped_in_solid = true;
215 if (physic.get_velocity_y() < 0)
217 base.y = (int)(((int)base.y / 32) * 32);
218 physic.set_velocity_y(0);
221 physic.enable_gravity(false);
222 /* Reset score multiplier (for multi-hits): */
223 if (!invincible_timer.started())
224 player_status.score_multiplier = 1;
229 if (isbrick(base.x, base.y) ||
230 isfullbox(base.x, base.y))
232 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
233 World::current()->trybumpbadguy(base.x, base.y - 64);
235 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
237 bumpbrick(base.x, base.y);
238 World::current()->tryemptybox(base.x, base.y, RIGHT);
241 if (isbrick(base.x+ 31, base.y) ||
242 isfullbox(base.x+ 31, base.y))
244 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
245 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
248 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
250 bumpbrick(base.x+ 31, base.y);
251 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
263 /* Make sure jumping is off. */
269 /* ---- DONE HANDLING TUX! --- */
272 skidding_timer.check();
273 invincible_timer.check();
281 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
282 issolid(base.x + 1, base.y + base.height) ||
283 issolid(base.x + base.width - 1, base.y + base.height) );
287 Player::under_solid()
289 return ( issolid(base.x + base.width / 2, base.y) ||
290 issolid(base.x + 1, base.y) ||
291 issolid(base.x + base.width - 1, base.y) );
295 Player::handle_horizontal_input()
297 float vx = physic.get_velocity_x();
298 float vy = physic.get_velocity_y();
299 float ax = physic.get_acceleration_x();
300 float ay = physic.get_acceleration_y();
303 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
307 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
313 if (input.fire == UP) {
314 ax = dirsign * WALK_ACCELERATION_X;
316 if(vx >= MAX_WALK_XM && dirsign > 0) {
319 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
324 ax = dirsign * RUN_ACCELERATION_X;
326 if(vx >= MAX_RUN_XM && dirsign > 0) {
329 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
335 // we can reach WALK_SPEED without any acceleration
336 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
337 vx = dirsign * WALK_SPEED;
340 // changing directions?
341 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
342 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
343 skidding_timer.start(SKID_TIME);
344 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
351 // we get slower when not pressing any keys
353 if(fabs(vx) < WALK_SPEED) {
357 ax = WALK_ACCELERATION_X * 1.5;
359 ax = WALK_ACCELERATION_X * -1.5;
363 // if we're on ice slow down acceleration or deceleration
364 if (isice(base.x, base.y + base.height))
366 /* the acceleration/deceleration rate on ice is inversely proportional to
367 * the current velocity.
370 // increasing 1 will increase acceleration/deceleration rate
371 // decreasing 1 will decrease acceleration/deceleration rate
372 // must stay above zero, though
373 if (ax != 0) ax *= 1 / fabs(vx);
376 physic.set_velocity(vx, vy);
377 physic.set_acceleration(ax, ay);
381 Player::handle_vertical_input()
384 if(input.up == DOWN && can_jump)
388 // jump higher if we are running
389 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
390 physic.set_velocity_y(5.8);
392 physic.set_velocity_y(5.2);
398 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
400 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
403 // Let go of jump key
404 else if(input.up == UP && jumping)
407 if(physic.get_velocity_y() > 0) {
408 physic.set_velocity_y(0);
412 if (input.down == DOWN && !on_ground() && !duck)
414 else if (input.down == UP)
416 if (input.down == DOWN && butt_jump && on_ground())
418 if (isbrick(base.x, base.y + base.height))
419 World::current()->trybreakbrick(base.x, base.y + base.height, false);
420 if (isbrick(base.x + base.width, base.y + base.height))
421 World::current()->trybreakbrick(base.x + base.width, base.y + base.height, false);
427 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
428 issolid(base.x + 1, base.y + base.height + 64) ||
429 issolid(base.x + base.width - 1, base.y + base.height + 64))
433 && input.old_up == UP)
438 input.old_up = input.up;
442 Player::handle_input()
444 /* Handle horizontal movement: */
445 handle_horizontal_input();
449 if (on_ground() && input.up == UP)
451 if (input.up == DOWN || (input.up == UP && jumping))
453 handle_vertical_input();
458 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
460 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
461 input.old_fire = DOWN;
464 /* tux animations: */
465 if(!frame_timer.check())
467 frame_timer.start(25);
468 if (input.right == UP && input.left == UP)
475 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
476 (global_frame_counter % 4) == 0)
477 frame_main = (frame_main + 1) % 4;
487 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
492 // changing base size confuses collision otherwise
493 old_base = previous_base = base;
495 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
500 // changing base size confuses collision otherwise
501 old_base = previous_base = base;
515 old_base = previous_base = base;
519 Player::grabdistros()
524 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
525 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
527 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
528 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
532 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
533 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
538 /* Enough distros for a One-up? */
539 if (player_status.distros >= DISTROS_LIFEUP)
541 player_status.distros = player_status.distros - DISTROS_LIFEUP;
542 if(player_status.lives < MAX_LIVES)
543 ++player_status.lives;
544 /*We want to hear the sound even, if MAX_LIVES is reached*/
545 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
552 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
554 if (dying == DYING_SQUISHED)
556 smalltux_gameover->draw(base.x, base.y);
560 PlayerSprite* sprite;
564 else if (got_power == FIRE_POWER)
566 else if (got_power == ICE_POWER)
571 if (duck && size != SMALL)
574 sprite->duck_right->draw(base.x, base.y);
576 sprite->duck_left->draw(base.x, base.y);
578 else if (skidding_timer.started())
581 sprite->skid_right->draw(base.x, base.y);
583 sprite->skid_left->draw(base.x, base.y);
585 else if (kick_timer.started())
588 sprite->kick_right->draw(base.x, base.y);
590 sprite->kick_left->draw(base.x, base.y);
592 else if (physic.get_velocity_y() != 0)
595 sprite->jump_right->draw(base.x, base.y);
597 sprite->jump_left->draw(base.x, base.y);
601 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
604 sprite->stand_right->draw( base.x, base.y);
606 sprite->stand_left->draw( base.x, base.y);
611 sprite->walk_right->draw(base.x, base.y);
613 sprite->walk_left->draw(base.x, base.y);
617 // Draw arm overlay graphics when Tux is holding something
618 if (holding_something && physic.get_velocity_y() == 0)
621 sprite->grab_right->draw(base.x, base.y);
623 sprite->grab_left->draw(base.x, base.y);
626 // Draw blinking star overlay
627 if (invincible_timer.started() &&
628 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
630 if (size == SMALL || duck)
631 smalltux_star->draw(base.x, base.y);
633 largetux_star->draw(base.x, base.y);
639 fillrect(base.x - scroll_x, base.y - scroll_y,
640 base.width, base.height, 75,75,75, 150);
644 Player::collision(void* p_c_object, int c_object)
646 BadGuy* pbad_c = NULL;
647 Trampoline* ptramp_c = NULL;
652 pbad_c = (BadGuy*) p_c_object;
654 /* Hurt player if he touches a badguy */
655 if (!pbad_c->dying && !dying &&
656 !safe_timer.started() &&
657 pbad_c->mode != BadGuy::HELD)
659 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
660 && !holding_something)
662 holding_something = true;
663 pbad_c->mode = BadGuy::HELD;
666 else if (pbad_c->mode == BadGuy::FLAT)
668 // Don't get hurt if we're kicking a flat badguy!
670 else if (pbad_c->mode == BadGuy::KICK)
672 /* Hurt if you get hit by kicked laptop: */
673 if (!invincible_timer.started())
680 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
681 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
682 || pbad_c->kind == BAD_SPIKY))
686 if (!invincible_timer.started())
695 player_status.score_multiplier++;
700 ptramp_c = (Trampoline*) p_c_object;
702 if (physic.get_velocity_x() > 0) // RIGHT
704 physic.set_velocity_x(0);
705 base.x = ptramp_c->base.x - base.width;
707 else if (physic.get_velocity_x() < 0) // LEFT
709 physic.set_velocity_x(0);
710 base.x = ptramp_c->base.x + ptramp_c->base.width;
726 Player::kill(HurtMode mode)
728 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
730 physic.set_velocity_x(0);
732 if (mode == SHRINK && size == BIG)
734 if (got_power != NONE_POWER)
736 got_power = NONE_POWER;
744 safe_timer.start(TUX_SAFE_TIME);
748 physic.enable_gravity(true);
749 physic.set_acceleration(0, 0);
750 physic.set_velocity(0, 7);
751 if(dying != DYING_SQUISHED)
752 --player_status.lives;
753 dying = DYING_SQUISHED;
764 bool Player::is_dead()
766 if(base.y > screen->h || base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // last condition can happen in auto-scrolling
772 /* Remove Tux's power ups */
774 Player::remove_powerups()
776 got_power = NONE_POWER;
782 Player::check_bounds(bool back_scrolling, bool hor_autoscroll)
784 /* Keep tux in bounds: */
786 { // Lock Tux to the size of the level, so that he doesn't fall of
791 /* Keep in-bounds, vertically: */
792 if (base.y > screen->h)
797 if(base.x < scroll_x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled
802 if(base.x == scroll_x)
803 if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32)))
806 if(base.x + base.width > scroll_x + screen->w)
807 base.x = scroll_x + screen->w - base.width;