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.
30 #define AUTOSCROLL_DEAD_INTERVAL 300
34 Sprite* smalltux_gameover;
35 Sprite* smalltux_star;
36 Sprite* largetux_star;
38 PlayerSprite smalltux;
39 PlayerSprite largetux;
44 PlayerKeymap::PlayerKeymap()
46 keymap.jump = SDLK_UP;
47 keymap.duck = SDLK_DOWN;
48 keymap.left = SDLK_LEFT;
49 keymap.right = SDLK_RIGHT;
50 keymap.fire = SDLK_LCTRL;
53 void player_input_init(player_input_type* pplayer_input)
55 pplayer_input->down = UP;
56 pplayer_input->fire = UP;
57 pplayer_input->left = UP;
58 pplayer_input->old_fire = UP;
59 pplayer_input->right = UP;
60 pplayer_input->up = UP;
61 pplayer_input->old_up = UP;
67 Level* plevel = World::current()->get_level();
69 holding_something = false;
77 base.x = plevel->start_pos_x;
78 base.y = plevel->start_pos_y;
81 previous_base = old_base = base;
94 player_input_init(&input);
96 invincible_timer.init(true);
97 skidding_timer.init(true);
98 safe_timer.init(true);
99 frame_timer.init(true);
100 kick_timer.init(true);
106 Player::key_event(SDLKey key, int state)
108 if(key == keymap.right)
113 else if(key == keymap.left)
118 else if(key == keymap.jump)
123 else if(key == keymap.duck)
128 else if(key == keymap.fire)
140 Player::level_begin()
146 previous_base = old_base = base;
151 player_input_init(&input);
153 invincible_timer.init(true);
154 skidding_timer.init(true);
155 safe_timer.init(true);
156 frame_timer.init(true);
162 Player::action(double frame_ratio)
164 bool jumped_in_solid = false;
166 if (input.fire == UP)
167 holding_something = false;
170 previous_base = base;
172 /* --- HANDLE TUX! --- */
173 if(dying == DYING_NOT)
176 physic.apply(frame_ratio, base.x, base.y);
178 if(dying == DYING_NOT)
180 base_type target = base;
182 collision_swept_object_map(&old_base, &base);
184 // Don't accelerate Tux if he is running against a wall
185 if (target.x != base.x)
187 physic.set_velocity_x(0);
190 // special exception for cases where we're stuck under tiles after
191 // being ducked. In this case we drift out
192 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
193 && collision_object_map(base))
195 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
196 previous_base = old_base = base;
202 physic.enable_gravity(true);
206 physic.set_velocity_y(0);
207 jumped_in_solid = true;
213 if (physic.get_velocity_y() < 0)
215 base.y = (int)(((int)base.y / 32) * 32);
216 physic.set_velocity_y(0);
219 physic.enable_gravity(false);
220 /* Reset score multiplier (for multi-hits): */
221 if (!invincible_timer.started())
222 player_status.score_multiplier = 1;
227 if (isbrick(base.x, base.y) ||
228 isfullbox(base.x, base.y))
230 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
231 World::current()->trybumpbadguy(base.x, base.y - 64);
233 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
235 bumpbrick(base.x, base.y);
236 World::current()->tryemptybox(base.x, base.y, RIGHT);
239 if (isbrick(base.x+ 31, base.y) ||
240 isfullbox(base.x+ 31, base.y))
242 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
243 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
246 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
248 bumpbrick(base.x+ 31, base.y);
249 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
261 /* Make sure jumping is off. */
267 /* ---- DONE HANDLING TUX! --- */
270 skidding_timer.check();
271 invincible_timer.check();
279 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
280 issolid(base.x + 1, base.y + base.height) ||
281 issolid(base.x + base.width - 1, base.y + base.height) );
285 Player::under_solid()
287 return ( issolid(base.x + base.width / 2, base.y) ||
288 issolid(base.x + 1, base.y) ||
289 issolid(base.x + base.width - 1, base.y) );
293 Player::handle_horizontal_input()
295 float vx = physic.get_velocity_x();
296 float vy = physic.get_velocity_y();
297 float ax = physic.get_acceleration_x();
298 float ay = physic.get_acceleration_y();
301 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
305 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
311 if (input.fire == UP) {
312 ax = dirsign * WALK_ACCELERATION_X;
314 if(vx >= MAX_WALK_XM && dirsign > 0) {
317 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
322 ax = dirsign * RUN_ACCELERATION_X;
324 if(vx >= MAX_RUN_XM && dirsign > 0) {
327 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
333 // we can reach WALK_SPEED without any acceleration
334 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
335 vx = dirsign * WALK_SPEED;
338 // changing directions?
339 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
340 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
341 skidding_timer.start(SKID_TIME);
342 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
349 // we get slower when not pressing any keys
351 if(fabs(vx) < WALK_SPEED) {
355 ax = WALK_ACCELERATION_X * 1.5;
357 ax = WALK_ACCELERATION_X * -1.5;
361 // if we're on ice slow down acceleration or deceleration
362 if (isice(base.x, base.y + base.height))
364 /* the acceleration/deceleration rate on ice is inversely proportional to
365 * the current velocity.
368 // increasing 1 will increase acceleration/deceleration rate
369 // decreasing 1 will decrease acceleration/deceleration rate
370 // must stay above zero, though
371 if (ax != 0) ax *= 1 / fabs(vx);
374 physic.set_velocity(vx, vy);
375 physic.set_acceleration(ax, ay);
379 Player::handle_vertical_input()
382 if(input.up == DOWN && can_jump)
386 // jump higher if we are running
387 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
388 physic.set_velocity_y(5.8);
390 physic.set_velocity_y(5.2);
396 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
398 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
401 // Let go of jump key
402 else if(input.up == UP && jumping)
405 if(physic.get_velocity_y() > 0) {
406 physic.set_velocity_y(0);
410 if (input.down == DOWN && !on_ground() && !duck)
412 else if (input.down == UP)
414 if (input.down == DOWN && butt_jump && on_ground())
416 if (isbrick(base.x, base.y + base.height))
417 World::current()->trybreakbrick(base.x, base.y + base.height, false);
418 if (isbrick(base.x + base.width, base.y + base.height))
419 World::current()->trybreakbrick(base.x + base.width, base.y + base.height, false);
425 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
426 issolid(base.x + 1, base.y + base.height + 64) ||
427 issolid(base.x + base.width - 1, base.y + base.height + 64))
431 && input.old_up == UP)
436 input.old_up = input.up;
440 Player::handle_input()
442 /* Handle horizontal movement: */
443 handle_horizontal_input();
447 if (on_ground() && input.up == UP)
449 if (input.up == DOWN || (input.up == UP && jumping))
451 handle_vertical_input();
456 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
458 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
459 input.old_fire = DOWN;
462 /* tux animations: */
463 if(!frame_timer.check())
465 frame_timer.start(25);
466 if (input.right == UP && input.left == UP)
473 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
474 (global_frame_counter % 4) == 0)
475 frame_main = (frame_main + 1) % 4;
485 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
490 // changing base size confuses collision otherwise
491 old_base = previous_base = base;
493 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
498 // changing base size confuses collision otherwise
499 old_base = previous_base = base;
513 old_base = previous_base = base;
517 Player::grabdistros()
522 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
523 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
525 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
526 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
530 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
531 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
536 /* Enough distros for a One-up? */
537 if (player_status.distros >= DISTROS_LIFEUP)
539 player_status.distros = player_status.distros - DISTROS_LIFEUP;
540 if(player_status.lives < MAX_LIVES)
541 ++player_status.lives;
542 /*We want to hear the sound even, if MAX_LIVES is reached*/
543 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
550 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
552 if (dying == DYING_SQUISHED)
554 smalltux_gameover->draw(base.x - scroll_x, base.y);
558 PlayerSprite* sprite;
567 if (duck && size != SMALL)
570 sprite->duck_right->draw(base.x - scroll_x, base.y);
572 sprite->duck_left->draw(base.x - scroll_x, base.y);
574 else if (skidding_timer.started())
577 sprite->skid_right->draw(base.x - scroll_x, base.y);
579 sprite->skid_left->draw(base.x - scroll_x, base.y);
581 else if (kick_timer.started())
584 sprite->kick_right->draw(base.x - scroll_x, base.y);
586 sprite->kick_left->draw(base.x - scroll_x, base.y);
588 else if (physic.get_velocity_y() != 0)
591 sprite->jump_right->draw(base.x - scroll_x, base.y);
593 sprite->jump_left->draw(base.x - scroll_x, base.y);
597 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
600 sprite->stand_right->draw( base.x - scroll_x, base.y);
602 sprite->stand_left->draw( base.x - scroll_x, base.y);
607 sprite->walk_right->draw(base.x - scroll_x, base.y);
609 sprite->walk_left->draw(base.x - scroll_x, base.y);
613 // Draw arm overlay graphics when Tux is holding something
614 if (holding_something && physic.get_velocity_y() == 0)
617 sprite->grab_right->draw(base.x - scroll_x, base.y);
619 sprite->grab_left->draw(base.x - scroll_x, base.y);
622 // Draw blinking star overlay
623 if (invincible_timer.started() &&
624 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
626 if (size == SMALL || duck)
627 smalltux_star->draw(base.x - scroll_x, base.y);
629 largetux_star->draw(base.x - scroll_x, base.y);
635 fillrect(base.x - scroll_x, base.y,
636 base.width, base.height, 75,75,75, 150);
640 Player::collision(void* p_c_object, int c_object)
642 BadGuy* pbad_c = NULL;
647 pbad_c = (BadGuy*) p_c_object;
649 /* Hurt player if he touches a badguy */
650 if (!pbad_c->dying && !dying &&
651 !safe_timer.started() &&
652 pbad_c->mode != BadGuy::HELD)
654 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
655 && !holding_something)
657 holding_something = true;
658 pbad_c->mode = BadGuy::HELD;
661 else if (pbad_c->mode == BadGuy::FLAT)
663 // Don't get hurt if we're kicking a flat badguy!
665 else if (pbad_c->mode == BadGuy::KICK)
667 /* Hurt if you get hit by kicked laptop: */
668 if (!invincible_timer.started())
674 pbad_c->dying = DYING_FALLING;
675 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
676 World::current()->add_score(pbad_c->base.x - scroll_x,
678 25 * player_status.score_multiplier);
683 if (!invincible_timer.started())
692 player_status.score_multiplier++;
704 Player::kill(HurtMode mode)
706 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
708 physic.set_velocity_x(0);
710 if (mode == SHRINK && size == BIG)
722 safe_timer.start(TUX_SAFE_TIME);
726 physic.enable_gravity(true);
727 physic.set_acceleration(0, 0);
728 physic.set_velocity(0, 7);
729 if(dying != DYING_SQUISHED)
730 --player_status.lives;
731 dying = DYING_SQUISHED;
742 bool Player::is_dead()
744 if(base.y > screen->h || base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // last condition can happen in auto-scrolling
750 /* Remove Tux's power ups */
752 Player::remove_powerups()
760 Player::check_bounds(bool back_scrolling, bool hor_autoscroll)
762 /* Keep tux in bounds: */
764 { // Lock Tux to the size of the level, so that he doesn't fall of
769 /* Keep in-bounds, vertically: */
770 if (base.y > screen->h)
775 if(base.x < scroll_x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled
780 if(base.x == scroll_x)
781 if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32)))
784 if(base.x + base.width > scroll_x + screen->w)
785 base.x = scroll_x + screen->w - base.width;