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.
32 std::vector<Surface*> tux_right;
33 std::vector<Surface*> tux_left;
34 Surface* smalltux_jump_left;
35 Surface* smalltux_jump_right;
36 Surface* smalltux_stand_left;
37 Surface* smalltux_stand_right;
38 Sprite* smalltux_gameover;
39 Sprite* smalltux_skid_left;
40 Sprite* smalltux_skid_right;
42 Sprite* smalltux_kick_left;
43 Sprite* smalltux_kick_right;
44 Sprite* smalltux_grab_left;
45 Sprite* smalltux_grab_right;
47 Sprite* smalltux_star;
48 Sprite* largetux_star;
50 Sprite* largetux_kick_left;
51 Sprite* largetux_kick_right;
52 Sprite* largetux_grab_left;
53 Sprite* largetux_grab_right;
55 Sprite* largetux_stand_left;
56 Sprite* largetux_stand_right;
60 Sprite* bigtux_right_jump;
61 Sprite* bigtux_left_jump;
62 Sprite* ducktux_right;
64 Surface* skidtux_right;
65 Surface* skidtux_left;
66 Surface* firetux_right[3];
67 Surface* firetux_left[3];
68 Surface* bigfiretux_right[3];
69 Surface* bigfiretux_left[3];
70 Surface* bigfiretux_right_jump;
71 Surface* bigfiretux_left_jump;
72 Surface* duckfiretux_right;
73 Surface* duckfiretux_left;
74 Surface* skidfiretux_right;
75 Surface* skidfiretux_left;
76 Surface* cape_right[2];
77 Surface* cape_left[2];
81 PlayerKeymap::PlayerKeymap()
83 keymap.jump = SDLK_UP;
84 keymap.duck = SDLK_DOWN;
85 keymap.left = SDLK_LEFT;
86 keymap.right = SDLK_RIGHT;
87 keymap.fire = SDLK_LCTRL;
90 void player_input_init(player_input_type* pplayer_input)
92 pplayer_input->down = UP;
93 pplayer_input->fire = UP;
94 pplayer_input->left = UP;
95 pplayer_input->old_fire = UP;
96 pplayer_input->right = UP;
97 pplayer_input->up = UP;
103 Level* plevel = World::current()->get_level();
105 holding_something = false;
113 base.x = plevel->start_pos_x;
114 base.y = plevel->start_pos_y;
117 previous_base = old_base = base;
127 player_input_init(&input);
129 invincible_timer.init(true);
130 skidding_timer.init(true);
131 safe_timer.init(true);
132 frame_timer.init(true);
138 Player::key_event(SDLKey key, int state)
140 if(key == keymap.right)
145 else if(key == keymap.left)
150 else if(key == keymap.jump)
155 else if(key == keymap.duck)
160 else if(key == keymap.fire)
170 Player::level_begin()
176 previous_base = old_base = base;
181 player_input_init(&input);
183 invincible_timer.init(true);
184 skidding_timer.init(true);
185 safe_timer.init(true);
186 frame_timer.init(true);
192 Player::action(double frame_ratio)
194 bool jumped_in_solid = false;
196 if (input.fire == UP)
197 holding_something = false;
200 previous_base = base;
202 /* --- HANDLE TUX! --- */
203 if(dying == DYING_NOT)
206 physic.apply(frame_ratio, base.x, base.y);
208 if(dying == DYING_NOT)
210 base_type target = base;
212 collision_swept_object_map(&old_base, &base);
214 // Don't accelerate Tux if he is running against a wall
215 if (target.x != base.x)
217 physic.set_velocity_x(0);
220 // special exception for cases where we're stuck under tiles after
221 // being ducked. In this case we drift out
222 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
223 && collision_object_map(base))
225 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
226 previous_base = old_base = base;
233 physic.enable_gravity(true);
237 physic.set_velocity_y(0);
238 jumped_in_solid = true;
244 if (physic.get_velocity_y() < 0)
246 base.y = (int)(((int)base.y / 32) * 32);
247 physic.set_velocity_y(0);
250 physic.enable_gravity(false);
251 /* Reset score multiplier (for multi-hits): */
252 player_status.score_multiplier = 1;
257 if (isbrick(base.x, base.y) ||
258 isfullbox(base.x, base.y))
260 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
261 World::current()->trybumpbadguy(base.x, base.y - 64);
263 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
265 bumpbrick(base.x, base.y);
266 World::current()->tryemptybox(base.x, base.y, RIGHT);
269 if (isbrick(base.x+ 31, base.y) ||
270 isfullbox(base.x+ 31, base.y))
272 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
273 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
276 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
278 bumpbrick(base.x+ 31, base.y);
279 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
291 /* Make sure jumping is off. */
297 /* ---- DONE HANDLING TUX! --- */
300 skidding_timer.check();
301 invincible_timer.check();
308 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
309 issolid(base.x + 1, base.y + base.height) ||
310 issolid(base.x + base.width - 1, base.y + base.height) );
314 Player::under_solid()
316 return ( issolid(base.x + base.width / 2, base.y) ||
317 issolid(base.x + 1, base.y) ||
318 issolid(base.x + base.width - 1, base.y) );
322 Player::handle_horizontal_input()
324 float vx = physic.get_velocity_x();
325 float vy = physic.get_velocity_y();
326 float ax = physic.get_acceleration_x();
327 float ay = physic.get_acceleration_y();
330 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
333 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
338 if (input.fire == UP) {
339 ax = dirsign * WALK_ACCELERATION_X;
341 if(vx >= MAX_WALK_XM && dirsign > 0) {
344 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
349 ax = dirsign * RUN_ACCELERATION_X;
351 if(vx >= MAX_RUN_XM && dirsign > 0) {
354 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
360 // we can reach WALK_SPEED without any acceleration
361 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
362 vx = dirsign * WALK_SPEED;
365 // changing directions?
366 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
367 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
368 skidding_timer.start(SKID_TIME);
369 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
376 // we get slower when not pressing any keys
378 if(fabs(vx) < WALK_SPEED) {
382 ax = WALK_ACCELERATION_X * 1.5;
384 ax = WALK_ACCELERATION_X * -1.5;
388 // if we're on ice slow down acceleration or deceleration
389 if (isice(base.x, base.y + base.height))
391 /* the acceleration/deceleration rate on ice is inversely proportional to
392 * the current velocity.
395 // increasing 1 will increase acceleration/deceleration rate
396 // decreasing 1 will decrease acceleration/deceleration rate
397 // must stay above zero, though
398 if (ax != 0) ax *= 1 / fabs(vx);
401 physic.set_velocity(vx, vy);
402 physic.set_acceleration(ax, ay);
406 Player::handle_vertical_input()
412 // jump higher if we are running
413 if (physic.get_velocity_x() > MAX_WALK_XM)
414 physic.set_velocity_y(5.8);
416 physic.set_velocity_y(5.2);
421 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
423 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
426 else if(input.up == UP && jumping)
429 if(physic.get_velocity_y() > 0) {
430 physic.set_velocity_y(0);
436 Player::handle_input()
438 /* Handle horizontal movement: */
439 handle_horizontal_input();
443 if ( input.up == DOWN || (input.up == UP && jumping))
445 handle_vertical_input();
450 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
452 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
455 /* tux animations: */
456 if(!frame_timer.check())
458 frame_timer.start(25);
459 if (input.right == UP && input.left == UP)
466 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
467 (global_frame_counter % 4) == 0)
468 frame_main = (frame_main + 1) % 4;
478 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0)
483 // changing base size confuses collision otherwise
484 old_base = previous_base = base;
486 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
491 old_base = previous_base = base;
496 Player::grabdistros()
501 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
502 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
504 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
505 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
509 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
510 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
515 /* Enough distros for a One-up? */
516 if (player_status.distros >= DISTROS_LIFEUP)
518 player_status.distros = player_status.distros - DISTROS_LIFEUP;
519 if(player_status.lives < MAX_LIVES)
520 ++player_status.lives;
521 /*We want to hear the sound even, if MAX_LIVES is reached*/
522 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
529 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
531 if (dying == DYING_SQUISHED)
533 smalltux_gameover->draw(base.x - scroll_x, base.y);
539 if (!skidding_timer.started())
541 if (physic.get_velocity_y() != 0)
544 smalltux_jump_right->draw( base.x - scroll_x, base.y - 10);
546 smalltux_jump_left->draw( base.x - scroll_x, base.y - 10);
550 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
553 smalltux_stand_right->draw( base.x - scroll_x, base.y - 9);
555 smalltux_stand_left->draw( base.x - scroll_x, base.y - 9);
560 tux_right[(global_frame_counter/2) % tux_right.size()]->draw(base.x - scroll_x, base.y - 9);
562 tux_left[(global_frame_counter/2) % tux_left.size()]->draw(base.x - scroll_x, base.y - 9);
569 smalltux_skid_right->draw(base.x - scroll_x, base.y);
571 smalltux_skid_left->draw(base.x - scroll_x, base.y);
574 if (holding_something && physic.get_velocity_y() == 0)
577 smalltux_grab_right->draw(base.x - scroll_x, base.y);
579 smalltux_grab_left->draw(base.x - scroll_x, base.y);
582 if (invincible_timer.started())
583 smalltux_star->draw(base.x - scroll_x, base.y);
591 if (!skidding_timer.started())
593 if (physic.get_velocity_y() == 0)
595 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
598 largetux_stand_right->draw(base.x - scroll_x, base.y);
600 largetux_stand_left->draw(base.x - scroll_x, base.y);
605 bigtux_right->draw(base.x - scroll_x, base.y);
607 bigtux_left->draw(base.x - scroll_x, base.y);
613 bigtux_right_jump->draw(base.x - scroll_x, base.y);
615 bigtux_left_jump->draw(base.x - scroll_x, base.y);
621 skidtux_right->draw(base.x - scroll_x - 8, base.y);
623 skidtux_left->draw(base.x - scroll_x - 8, base.y);
629 ducktux_right->draw(base.x - scroll_x, base.y);
631 ducktux_left->draw(base.x - scroll_x, base.y);
636 /* Tux has coffee! */
639 if (!skidding_timer.started())
641 if (!jumping || physic.get_velocity_y() > 0)
644 bigfiretux_right[frame_]->draw(base.x- scroll_x - 8, base.y);
646 bigfiretux_left[frame_]->draw(base.x- scroll_x - 8, base.y);
651 bigfiretux_right_jump->draw(base.x- scroll_x - 8, base.y);
653 bigfiretux_left_jump->draw(base.x- scroll_x - 8, base.y);
659 skidfiretux_right->draw(base.x- scroll_x - 8, base.y);
661 skidfiretux_left->draw(base.x- scroll_x - 8, base.y);
667 duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
669 duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
673 if (holding_something && !duck && physic.get_velocity_y() == 0)
676 largetux_grab_right->draw(base.x - scroll_x, base.y);
678 largetux_grab_left->draw(base.x - scroll_x, base.y);
681 if (invincible_timer.started())
682 largetux_star->draw(base.x - scroll_x, base.y);
688 fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
692 Player::collision(void* p_c_object, int c_object)
694 BadGuy* pbad_c = NULL;
699 pbad_c = (BadGuy*) p_c_object;
701 /* Hurt player if he touches a badguy */
702 if (!pbad_c->dying && !dying &&
703 !safe_timer.started() &&
704 pbad_c->mode != BadGuy::HELD)
706 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN)
708 holding_something = true;
709 pbad_c->mode = BadGuy::HELD;
712 else if (pbad_c->mode == BadGuy::FLAT)
714 // Don't get hurt if we're kicking a flat badguy!
716 else if (pbad_c->mode == BadGuy::KICK)
718 /* Hurt if you get hit by kicked laptop: */
719 if (!invincible_timer.started())
725 pbad_c->dying = DYING_FALLING;
726 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
727 World::current()->add_score(pbad_c->base.x - scroll_x,
729 25 * player_status.score_multiplier);
734 if (!invincible_timer.started())
743 player_status.score_multiplier++;
755 Player::kill(HurtMode mode)
757 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
759 physic.set_velocity_x(0);
761 if (mode == SHRINK && size == BIG)
773 safe_timer.start(TUX_SAFE_TIME);
777 physic.enable_gravity(true);
778 physic.set_acceleration(0, 0);
779 physic.set_velocity(0, 7);
780 dying = DYING_SQUISHED;
791 bool Player::is_dead()
793 if(base.y > screen->h)
799 /* Remove Tux's power ups */
801 Player::remove_powerups()
809 Player::keep_in_bounds()
811 Level* plevel = World::current()->get_level();
813 /* Keep tux in bounds: */
815 { // Lock Tux to the size of the level, so that he doesn't fall of
819 else if (base.x < scroll_x)
824 /* Keep in-bounds, vertically: */
825 if (base.y > screen->h)
830 int scroll_threshold = screen->w/2 - 80;
833 scroll_x += screen->w/2;
834 // Backscrolling for debug mode
835 if (scroll_x < base.x - 80)
836 scroll_x = base.x - 80;
837 else if (scroll_x > base.x + 80)
838 scroll_x = base.x + 80;
839 scroll_x -= screen->w/2;
846 if (base.x > scroll_threshold + scroll_x
847 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
849 // FIXME: Scrolling needs to be handled by a seperate View
850 // class, doing it as a player huck is ugly
852 // Scroll the screen in past center:
853 scroll_x = base.x - scroll_threshold;
855 // Lock the scrolling to the levelsize, so that we don't
856 // scroll over the right border
857 if (scroll_x > 32 * plevel->width - screen->w)
858 scroll_x = 32 * plevel->width - screen->w;