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 Sprite* smalltux_gameover;
34 Sprite* smalltux_star;
35 Sprite* largetux_star;
37 PlayerSprite smalltux;
38 PlayerSprite largetux;
43 PlayerKeymap::PlayerKeymap()
45 keymap.jump = SDLK_UP;
46 keymap.duck = SDLK_DOWN;
47 keymap.left = SDLK_LEFT;
48 keymap.right = SDLK_RIGHT;
49 keymap.fire = SDLK_LCTRL;
52 void player_input_init(player_input_type* pplayer_input)
54 pplayer_input->down = UP;
55 pplayer_input->fire = UP;
56 pplayer_input->left = UP;
57 pplayer_input->old_fire = UP;
58 pplayer_input->right = UP;
59 pplayer_input->up = UP;
65 Level* plevel = World::current()->get_level();
67 holding_something = false;
75 base.x = plevel->start_pos_x;
76 base.y = plevel->start_pos_y;
79 previous_base = old_base = base;
89 player_input_init(&input);
91 invincible_timer.init(true);
92 skidding_timer.init(true);
93 safe_timer.init(true);
94 frame_timer.init(true);
95 kick_timer.init(true);
101 Player::key_event(SDLKey key, int state)
103 if(key == keymap.right)
108 else if(key == keymap.left)
113 else if(key == keymap.jump)
118 else if(key == keymap.duck)
123 else if(key == keymap.fire)
135 Player::level_begin()
141 previous_base = old_base = base;
146 player_input_init(&input);
148 invincible_timer.init(true);
149 skidding_timer.init(true);
150 safe_timer.init(true);
151 frame_timer.init(true);
157 Player::action(double frame_ratio)
159 bool jumped_in_solid = false;
161 if (input.fire == UP)
162 holding_something = false;
165 previous_base = base;
167 /* --- HANDLE TUX! --- */
168 if(dying == DYING_NOT)
171 physic.apply(frame_ratio, base.x, base.y);
173 if(dying == DYING_NOT)
175 base_type target = base;
177 collision_swept_object_map(&old_base, &base);
179 // Don't accelerate Tux if he is running against a wall
180 if (target.x != base.x)
182 physic.set_velocity_x(0);
185 // special exception for cases where we're stuck under tiles after
186 // being ducked. In this case we drift out
187 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
188 && collision_object_map(base))
190 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
191 previous_base = old_base = base;
198 physic.enable_gravity(true);
202 physic.set_velocity_y(0);
203 jumped_in_solid = true;
209 if (physic.get_velocity_y() < 0)
211 base.y = (int)(((int)base.y / 32) * 32);
212 physic.set_velocity_y(0);
215 physic.enable_gravity(false);
216 /* Reset score multiplier (for multi-hits): */
217 player_status.score_multiplier = 1;
222 if (isbrick(base.x, base.y) ||
223 isfullbox(base.x, base.y))
225 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
226 World::current()->trybumpbadguy(base.x, base.y - 64);
228 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
230 bumpbrick(base.x, base.y);
231 World::current()->tryemptybox(base.x, base.y, RIGHT);
234 if (isbrick(base.x+ 31, base.y) ||
235 isfullbox(base.x+ 31, base.y))
237 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
238 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
241 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
243 bumpbrick(base.x+ 31, base.y);
244 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
256 /* Make sure jumping is off. */
262 /* ---- DONE HANDLING TUX! --- */
265 skidding_timer.check();
266 invincible_timer.check();
274 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
275 issolid(base.x + 1, base.y + base.height) ||
276 issolid(base.x + base.width - 1, base.y + base.height) );
280 Player::under_solid()
282 return ( issolid(base.x + base.width / 2, base.y) ||
283 issolid(base.x + 1, base.y) ||
284 issolid(base.x + base.width - 1, base.y) );
288 Player::handle_horizontal_input()
290 float vx = physic.get_velocity_x();
291 float vy = physic.get_velocity_y();
292 float ax = physic.get_acceleration_x();
293 float ay = physic.get_acceleration_y();
296 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
299 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
304 if (input.fire == UP) {
305 ax = dirsign * WALK_ACCELERATION_X;
307 if(vx >= MAX_WALK_XM && dirsign > 0) {
310 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
315 ax = dirsign * RUN_ACCELERATION_X;
317 if(vx >= MAX_RUN_XM && dirsign > 0) {
320 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
326 // we can reach WALK_SPEED without any acceleration
327 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
328 vx = dirsign * WALK_SPEED;
331 // changing directions?
332 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
333 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
334 skidding_timer.start(SKID_TIME);
335 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
342 // we get slower when not pressing any keys
344 if(fabs(vx) < WALK_SPEED) {
348 ax = WALK_ACCELERATION_X * 1.5;
350 ax = WALK_ACCELERATION_X * -1.5;
354 // if we're on ice slow down acceleration or deceleration
355 if (isice(base.x, base.y + base.height))
357 /* the acceleration/deceleration rate on ice is inversely proportional to
358 * the current velocity.
361 // increasing 1 will increase acceleration/deceleration rate
362 // decreasing 1 will decrease acceleration/deceleration rate
363 // must stay above zero, though
364 if (ax != 0) ax *= 1 / fabs(vx);
367 physic.set_velocity(vx, vy);
368 physic.set_acceleration(ax, ay);
372 Player::handle_vertical_input()
378 // jump higher if we are running
379 if (physic.get_velocity_x() > MAX_WALK_XM)
380 physic.set_velocity_y(5.8);
382 physic.set_velocity_y(5.2);
387 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
389 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
392 else if(input.up == UP && jumping)
395 if(physic.get_velocity_y() > 0) {
396 physic.set_velocity_y(0);
402 Player::handle_input()
404 /* Handle horizontal movement: */
405 handle_horizontal_input();
409 if ( input.up == DOWN || (input.up == UP && jumping))
411 handle_vertical_input();
416 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
418 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
419 input.old_fire = DOWN;
422 /* tux animations: */
423 if(!frame_timer.check())
425 frame_timer.start(25);
426 if (input.right == UP && input.left == UP)
433 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
434 (global_frame_counter % 4) == 0)
435 frame_main = (frame_main + 1) % 4;
445 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
450 // changing base size confuses collision otherwise
451 old_base = previous_base = base;
453 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
470 // eventually go in duck mode if there's no space
471 if(collision_object_map(base))
478 old_base = previous_base = base;
482 Player::grabdistros()
487 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
488 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
490 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
491 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
495 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
496 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
501 /* Enough distros for a One-up? */
502 if (player_status.distros >= DISTROS_LIFEUP)
504 player_status.distros = player_status.distros - DISTROS_LIFEUP;
505 if(player_status.lives < MAX_LIVES)
506 ++player_status.lives;
507 /*We want to hear the sound even, if MAX_LIVES is reached*/
508 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
515 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
517 if (dying == DYING_SQUISHED)
519 smalltux_gameover->draw(base.x - scroll_x, base.y);
523 PlayerSprite* sprite;
532 if (duck && size != SMALL)
535 sprite->duck_right->draw(base.x - scroll_x, base.y);
537 sprite->duck_left->draw(base.x - scroll_x, base.y);
539 else if (skidding_timer.started())
542 sprite->skid_right->draw(base.x - scroll_x, base.y);
544 sprite->skid_left->draw(base.x - scroll_x, base.y);
546 else if (kick_timer.started())
549 sprite->kick_right->draw(base.x - scroll_x, base.y);
551 sprite->kick_left->draw(base.x - scroll_x, base.y);
553 else if (physic.get_velocity_y() != 0)
556 sprite->jump_right->draw(base.x - scroll_x, base.y);
558 sprite->jump_left->draw(base.x - scroll_x, base.y);
562 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
565 sprite->stand_right->draw( base.x - scroll_x, base.y);
567 sprite->stand_left->draw( base.x - scroll_x, base.y);
572 sprite->walk_right->draw(base.x - scroll_x, base.y);
574 sprite->walk_left->draw(base.x - scroll_x, base.y);
578 // Draw arm overlay graphics when Tux is holding something
579 if (holding_something && physic.get_velocity_y() == 0)
582 sprite->grab_right->draw(base.x - scroll_x, base.y);
584 sprite->grab_left->draw(base.x - scroll_x, base.y);
587 // Draw blinking star overlay
588 if (invincible_timer.started())
590 if (size == SMALL || duck)
591 smalltux_star->draw(base.x - scroll_x, base.y);
593 largetux_star->draw(base.x - scroll_x, base.y);
599 fillrect(base.x - scroll_x, base.y,
600 base.width, base.height, 75,75,75, 150);
604 Player::collision(void* p_c_object, int c_object)
606 BadGuy* pbad_c = NULL;
611 pbad_c = (BadGuy*) p_c_object;
613 /* Hurt player if he touches a badguy */
614 if (!pbad_c->dying && !dying &&
615 !safe_timer.started() &&
616 pbad_c->mode != BadGuy::HELD)
618 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
619 && !holding_something)
621 holding_something = true;
622 pbad_c->mode = BadGuy::HELD;
625 else if (pbad_c->mode == BadGuy::FLAT)
627 // Don't get hurt if we're kicking a flat badguy!
629 else if (pbad_c->mode == BadGuy::KICK)
631 /* Hurt if you get hit by kicked laptop: */
632 if (!invincible_timer.started())
638 pbad_c->dying = DYING_FALLING;
639 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
640 World::current()->add_score(pbad_c->base.x - scroll_x,
642 25 * player_status.score_multiplier);
647 if (!invincible_timer.started())
656 player_status.score_multiplier++;
668 Player::kill(HurtMode mode)
670 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
672 physic.set_velocity_x(0);
674 if (mode == SHRINK && size == BIG)
686 safe_timer.start(TUX_SAFE_TIME);
690 physic.enable_gravity(true);
691 physic.set_acceleration(0, 0);
692 physic.set_velocity(0, 7);
693 dying = DYING_SQUISHED;
704 bool Player::is_dead()
706 if(base.y > screen->h)
712 /* Remove Tux's power ups */
714 Player::remove_powerups()
722 Player::keep_in_bounds()
724 Level* plevel = World::current()->get_level();
726 /* Keep tux in bounds: */
728 { // Lock Tux to the size of the level, so that he doesn't fall of
732 else if (base.x < scroll_x)
737 /* Keep in-bounds, vertically: */
738 if (base.y > screen->h)
743 int scroll_threshold = screen->w/2 - 80;
746 scroll_x += screen->w/2;
747 // Backscrolling for debug mode
748 if (scroll_x < base.x - 80)
749 scroll_x = base.x - 80;
750 else if (scroll_x > base.x + 80)
751 scroll_x = base.x + 80;
752 scroll_x -= screen->w/2;
759 if (base.x > scroll_threshold + scroll_x
760 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
762 // FIXME: Scrolling needs to be handled by a seperate View
763 // class, doing it as a player huck is ugly
765 // Scroll the screen in past center:
766 scroll_x = base.x - scroll_threshold;
768 // Lock the scrolling to the levelsize, so that we don't
769 // scroll over the right border
770 if (scroll_x > 32 * plevel->width - screen->w)
771 scroll_x = 32 * plevel->width - screen->w;