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;
45 PlayerKeymap::PlayerKeymap()
47 keymap.jump = SDLK_UP;
48 keymap.duck = SDLK_DOWN;
49 keymap.left = SDLK_LEFT;
50 keymap.right = SDLK_RIGHT;
51 keymap.fire = SDLK_LCTRL;
54 void player_input_init(player_input_type* pplayer_input)
56 pplayer_input->down = UP;
57 pplayer_input->fire = UP;
58 pplayer_input->left = UP;
59 pplayer_input->old_fire = UP;
60 pplayer_input->right = UP;
61 pplayer_input->up = UP;
62 pplayer_input->old_up = UP;
68 Level* plevel = World::current()->get_level();
70 holding_something = false;
76 got_power = NONE_POWER;
78 base.x = plevel->start_pos_x;
79 base.y = plevel->start_pos_y;
82 previous_base = old_base = base;
95 player_input_init(&input);
97 invincible_timer.init(true);
98 skidding_timer.init(true);
99 safe_timer.init(true);
100 frame_timer.init(true);
101 kick_timer.init(true);
107 Player::key_event(SDLKey key, int state)
109 if(key == keymap.right)
114 else if(key == keymap.left)
119 else if(key == keymap.jump)
124 else if(key == keymap.duck)
129 else if(key == keymap.fire)
141 Player::level_begin()
147 previous_base = old_base = base;
152 player_input_init(&input);
154 invincible_timer.init(true);
155 skidding_timer.init(true);
156 safe_timer.init(true);
157 frame_timer.init(true);
163 Player::action(double frame_ratio)
165 bool jumped_in_solid = false;
167 if (input.fire == UP)
168 holding_something = false;
171 previous_base = base;
173 /* --- HANDLE TUX! --- */
174 if(dying == DYING_NOT)
177 physic.apply(frame_ratio, base.x, base.y);
179 if(dying == DYING_NOT)
181 base_type target = base;
183 collision_swept_object_map(&old_base, &base);
185 // Don't accelerate Tux if he is running against a wall
186 if (target.x != base.x)
188 physic.set_velocity_x(0);
191 // special exception for cases where we're stuck under tiles after
192 // being ducked. In this case we drift out
193 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
194 && collision_object_map(base))
196 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
197 previous_base = old_base = base;
203 physic.enable_gravity(true);
207 physic.set_velocity_y(0);
208 jumped_in_solid = true;
214 if (physic.get_velocity_y() < 0)
216 base.y = (int)(((int)base.y / 32) * 32);
217 physic.set_velocity_y(0);
220 physic.enable_gravity(false);
221 /* Reset score multiplier (for multi-hits): */
222 if (!invincible_timer.started())
223 player_status.score_multiplier = 1;
228 if (isbrick(base.x, base.y) ||
229 isfullbox(base.x, base.y))
231 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
232 World::current()->trybumpbadguy(base.x, base.y - 64);
234 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
236 bumpbrick(base.x, base.y);
237 World::current()->tryemptybox(base.x, base.y, RIGHT);
240 if (isbrick(base.x+ 31, base.y) ||
241 isfullbox(base.x+ 31, base.y))
243 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
244 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
247 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
249 bumpbrick(base.x+ 31, base.y);
250 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
262 /* Make sure jumping is off. */
268 /* ---- DONE HANDLING TUX! --- */
271 skidding_timer.check();
272 invincible_timer.check();
280 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
281 issolid(base.x + 1, base.y + base.height) ||
282 issolid(base.x + base.width - 1, base.y + base.height) );
286 Player::under_solid()
288 return ( issolid(base.x + base.width / 2, base.y) ||
289 issolid(base.x + 1, base.y) ||
290 issolid(base.x + base.width - 1, base.y) );
294 Player::handle_horizontal_input()
296 float vx = physic.get_velocity_x();
297 float vy = physic.get_velocity_y();
298 float ax = physic.get_acceleration_x();
299 float ay = physic.get_acceleration_y();
302 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
306 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
312 if (input.fire == UP) {
313 ax = dirsign * WALK_ACCELERATION_X;
315 if(vx >= MAX_WALK_XM && dirsign > 0) {
318 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
323 ax = dirsign * RUN_ACCELERATION_X;
325 if(vx >= MAX_RUN_XM && dirsign > 0) {
328 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
334 // we can reach WALK_SPEED without any acceleration
335 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
336 vx = dirsign * WALK_SPEED;
339 // changing directions?
340 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
341 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
342 skidding_timer.start(SKID_TIME);
343 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
350 // we get slower when not pressing any keys
352 if(fabs(vx) < WALK_SPEED) {
356 ax = WALK_ACCELERATION_X * 1.5;
358 ax = WALK_ACCELERATION_X * -1.5;
362 // if we're on ice slow down acceleration or deceleration
363 if (isice(base.x, base.y + base.height))
365 /* the acceleration/deceleration rate on ice is inversely proportional to
366 * the current velocity.
369 // increasing 1 will increase acceleration/deceleration rate
370 // decreasing 1 will decrease acceleration/deceleration rate
371 // must stay above zero, though
372 if (ax != 0) ax *= 1 / fabs(vx);
375 physic.set_velocity(vx, vy);
376 physic.set_acceleration(ax, ay);
380 Player::handle_vertical_input()
383 if(input.up == DOWN && can_jump)
387 // jump higher if we are running
388 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
389 physic.set_velocity_y(5.8);
391 physic.set_velocity_y(5.2);
397 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
399 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
402 // Let go of jump key
403 else if(input.up == UP && jumping)
406 if(physic.get_velocity_y() > 0) {
407 physic.set_velocity_y(0);
411 if (input.down == DOWN && !on_ground() && !duck)
413 else if (input.down == UP)
415 if (input.down == DOWN && butt_jump && on_ground())
417 if (isbrick(base.x, base.y + base.height))
418 World::current()->trybreakbrick(base.x, base.y + base.height, false);
419 if (isbrick(base.x + base.width, base.y + base.height))
420 World::current()->trybreakbrick(base.x + base.width, base.y + base.height, false);
426 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
427 issolid(base.x + 1, base.y + base.height + 64) ||
428 issolid(base.x + base.width - 1, base.y + base.height + 64))
432 && input.old_up == UP)
437 input.old_up = input.up;
441 Player::handle_input()
443 /* Handle horizontal movement: */
444 handle_horizontal_input();
448 if (on_ground() && input.up == UP)
450 if (input.up == DOWN || (input.up == UP && jumping))
452 handle_vertical_input();
457 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
459 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
460 input.old_fire = DOWN;
463 /* tux animations: */
464 if(!frame_timer.check())
466 frame_timer.start(25);
467 if (input.right == UP && input.left == UP)
474 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
475 (global_frame_counter % 4) == 0)
476 frame_main = (frame_main + 1) % 4;
486 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
491 // changing base size confuses collision otherwise
492 old_base = previous_base = base;
494 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
499 // changing base size confuses collision otherwise
500 old_base = previous_base = base;
514 old_base = previous_base = base;
518 Player::grabdistros()
523 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
524 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
526 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
527 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
531 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
532 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
537 /* Enough distros for a One-up? */
538 if (player_status.distros >= DISTROS_LIFEUP)
540 player_status.distros = player_status.distros - DISTROS_LIFEUP;
541 if(player_status.lives < MAX_LIVES)
542 ++player_status.lives;
543 /*We want to hear the sound even, if MAX_LIVES is reached*/
544 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
551 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
553 if (dying == DYING_SQUISHED)
555 smalltux_gameover->draw(base.x, base.y);
559 PlayerSprite* sprite;
563 else if (got_power == FIRE_POWER)
565 else if (got_power == ICE_POWER)
570 if (duck && size != SMALL)
573 sprite->duck_right->draw(base.x, base.y);
575 sprite->duck_left->draw(base.x, base.y);
577 else if (skidding_timer.started())
580 sprite->skid_right->draw(base.x, base.y);
582 sprite->skid_left->draw(base.x, base.y);
584 else if (kick_timer.started())
587 sprite->kick_right->draw(base.x, base.y);
589 sprite->kick_left->draw(base.x, base.y);
591 else if (physic.get_velocity_y() != 0)
594 sprite->jump_right->draw(base.x, base.y);
596 sprite->jump_left->draw(base.x, base.y);
600 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
603 sprite->stand_right->draw( base.x, base.y);
605 sprite->stand_left->draw( base.x, base.y);
610 sprite->walk_right->draw(base.x, base.y);
612 sprite->walk_left->draw(base.x, base.y);
616 // Draw arm overlay graphics when Tux is holding something
617 if (holding_something && physic.get_velocity_y() == 0)
620 sprite->grab_right->draw(base.x, base.y);
622 sprite->grab_left->draw(base.x, base.y);
625 // Draw blinking star overlay
626 if (invincible_timer.started() &&
627 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
629 if (size == SMALL || duck)
630 smalltux_star->draw(base.x, base.y);
632 largetux_star->draw(base.x, base.y);
638 fillrect(base.x - scroll_x, base.y - scroll_y,
639 base.width, base.height, 75,75,75, 150);
643 Player::collision(void* p_c_object, int c_object)
645 BadGuy* pbad_c = NULL;
650 pbad_c = (BadGuy*) p_c_object;
652 /* Hurt player if he touches a badguy */
653 if (!pbad_c->dying && !dying &&
654 !safe_timer.started() &&
655 pbad_c->mode != BadGuy::HELD)
657 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
658 && !holding_something)
660 holding_something = true;
661 pbad_c->mode = BadGuy::HELD;
664 else if (pbad_c->mode == BadGuy::FLAT)
666 // Don't get hurt if we're kicking a flat badguy!
668 else if (pbad_c->mode == BadGuy::KICK)
670 /* Hurt if you get hit by kicked laptop: */
671 if (!invincible_timer.started())
678 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
679 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
680 || pbad_c->kind == BAD_SPIKY))
684 if (!invincible_timer.started())
693 player_status.score_multiplier++;
705 Player::kill(HurtMode mode)
707 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
709 physic.set_velocity_x(0);
711 if (mode == SHRINK && size == BIG)
713 if (got_power != NONE_POWER)
715 got_power = NONE_POWER;
723 safe_timer.start(TUX_SAFE_TIME);
727 physic.enable_gravity(true);
728 physic.set_acceleration(0, 0);
729 physic.set_velocity(0, 7);
730 if(dying != DYING_SQUISHED)
731 --player_status.lives;
732 dying = DYING_SQUISHED;
743 bool Player::is_dead()
745 if(base.y > screen->h || base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // last condition can happen in auto-scrolling
751 /* Remove Tux's power ups */
753 Player::remove_powerups()
755 got_power = NONE_POWER;
761 Player::check_bounds(bool back_scrolling, bool hor_autoscroll)
763 /* Keep tux in bounds: */
765 { // Lock Tux to the size of the level, so that he doesn't fall of
770 /* Keep in-bounds, vertically: */
771 if (base.y > screen->h)
776 if(base.x < scroll_x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled
781 if(base.x == scroll_x)
782 if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32)))
785 if(base.x + base.width > scroll_x + screen->w)
786 base.x = scroll_x + screen->w - base.width;