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;
40 Surface* firetux_right[3];
41 Surface* firetux_left[3];
42 Surface* bigfiretux_right[3];
43 Surface* bigfiretux_left[3];
44 Surface* bigfiretux_right_jump;
45 Surface* bigfiretux_left_jump;
46 Surface* duckfiretux_right;
47 Surface* duckfiretux_left;
48 Surface* skidfiretux_right;
49 Surface* skidfiretux_left;
53 PlayerKeymap::PlayerKeymap()
55 keymap.jump = SDLK_UP;
56 keymap.duck = SDLK_DOWN;
57 keymap.left = SDLK_LEFT;
58 keymap.right = SDLK_RIGHT;
59 keymap.fire = SDLK_LCTRL;
62 void player_input_init(player_input_type* pplayer_input)
64 pplayer_input->down = UP;
65 pplayer_input->fire = UP;
66 pplayer_input->left = UP;
67 pplayer_input->old_fire = UP;
68 pplayer_input->right = UP;
69 pplayer_input->up = UP;
75 Level* plevel = World::current()->get_level();
77 holding_something = false;
85 base.x = plevel->start_pos_x;
86 base.y = plevel->start_pos_y;
89 previous_base = old_base = base;
99 player_input_init(&input);
101 invincible_timer.init(true);
102 skidding_timer.init(true);
103 safe_timer.init(true);
104 frame_timer.init(true);
110 Player::key_event(SDLKey key, int state)
112 if(key == keymap.right)
117 else if(key == keymap.left)
122 else if(key == keymap.jump)
127 else if(key == keymap.duck)
132 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;
205 physic.enable_gravity(true);
209 physic.set_velocity_y(0);
210 jumped_in_solid = true;
216 if (physic.get_velocity_y() < 0)
218 base.y = (int)(((int)base.y / 32) * 32);
219 physic.set_velocity_y(0);
222 physic.enable_gravity(false);
223 /* Reset score multiplier (for multi-hits): */
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();
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)) {
305 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
310 if (input.fire == UP) {
311 ax = dirsign * WALK_ACCELERATION_X;
313 if(vx >= MAX_WALK_XM && dirsign > 0) {
316 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
321 ax = dirsign * RUN_ACCELERATION_X;
323 if(vx >= MAX_RUN_XM && dirsign > 0) {
326 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
332 // we can reach WALK_SPEED without any acceleration
333 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
334 vx = dirsign * WALK_SPEED;
337 // changing directions?
338 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
339 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
340 skidding_timer.start(SKID_TIME);
341 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
348 // we get slower when not pressing any keys
350 if(fabs(vx) < WALK_SPEED) {
354 ax = WALK_ACCELERATION_X * 1.5;
356 ax = WALK_ACCELERATION_X * -1.5;
360 // if we're on ice slow down acceleration or deceleration
361 if (isice(base.x, base.y + base.height))
363 /* the acceleration/deceleration rate on ice is inversely proportional to
364 * the current velocity.
367 // increasing 1 will increase acceleration/deceleration rate
368 // decreasing 1 will decrease acceleration/deceleration rate
369 // must stay above zero, though
370 if (ax != 0) ax *= 1 / fabs(vx);
373 physic.set_velocity(vx, vy);
374 physic.set_acceleration(ax, ay);
378 Player::handle_vertical_input()
384 // jump higher if we are running
385 if (physic.get_velocity_x() > MAX_WALK_XM)
386 physic.set_velocity_y(5.8);
388 physic.set_velocity_y(5.2);
393 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
395 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
398 else if(input.up == UP && jumping)
401 if(physic.get_velocity_y() > 0) {
402 physic.set_velocity_y(0);
408 Player::handle_input()
410 /* Handle horizontal movement: */
411 handle_horizontal_input();
415 if ( input.up == DOWN || (input.up == UP && jumping))
417 handle_vertical_input();
422 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
424 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
427 /* tux animations: */
428 if(!frame_timer.check())
430 frame_timer.start(25);
431 if (input.right == UP && input.left == UP)
438 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
439 (global_frame_counter % 4) == 0)
440 frame_main = (frame_main + 1) % 4;
450 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0)
455 // changing base size confuses collision otherwise
456 old_base = previous_base = base;
458 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
463 old_base = previous_base = base;
468 Player::grabdistros()
473 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
474 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
476 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
477 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
481 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
482 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
487 /* Enough distros for a One-up? */
488 if (player_status.distros >= DISTROS_LIFEUP)
490 player_status.distros = player_status.distros - DISTROS_LIFEUP;
491 if(player_status.lives < MAX_LIVES)
492 ++player_status.lives;
493 /*We want to hear the sound even, if MAX_LIVES is reached*/
494 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
501 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
503 if (dying == DYING_SQUISHED)
505 smalltux_gameover->draw(base.x - scroll_x, base.y);
511 if (!skidding_timer.started())
513 if (physic.get_velocity_y() != 0)
516 smalltux.jump_right->draw(base.x - scroll_x, base.y);
518 smalltux.jump_left->draw(base.x - scroll_x, base.y);
522 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
525 smalltux.stand_right->draw( base.x - scroll_x, base.y - 9);
527 smalltux.stand_left->draw( base.x - scroll_x, base.y - 9);
532 smalltux.walk_right->draw(base.x - scroll_x, base.y);
534 smalltux.walk_left->draw(base.x - scroll_x, base.y);
541 smalltux.skid_right->draw(base.x - scroll_x, base.y);
543 smalltux.skid_left->draw(base.x - scroll_x, base.y);
546 if (holding_something && physic.get_velocity_y() == 0)
549 smalltux.grab_right->draw(base.x - scroll_x, base.y);
551 smalltux.grab_left->draw(base.x - scroll_x, base.y);
554 if (invincible_timer.started())
555 smalltux_star->draw(base.x - scroll_x, base.y);
563 if (!skidding_timer.started())
565 if (physic.get_velocity_y() == 0)
567 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
570 largetux.stand_right->draw(base.x - scroll_x, base.y);
572 largetux.stand_left->draw(base.x - scroll_x, base.y);
577 largetux.walk_right->draw(base.x - scroll_x, base.y);
579 largetux.walk_left->draw(base.x - scroll_x, base.y);
585 largetux.jump_right->draw(base.x - scroll_x, base.y);
587 largetux.jump_left->draw(base.x - scroll_x, base.y);
593 largetux.skid_right->draw(base.x - scroll_x - 8, base.y);
595 largetux.skid_left->draw(base.x - scroll_x - 8, base.y);
601 largetux.duck_right->draw(base.x - scroll_x, base.y);
603 largetux.duck_left->draw(base.x - scroll_x, base.y);
608 /* Tux has coffee! */
611 if (!skidding_timer.started())
613 if (!jumping || physic.get_velocity_y() > 0)
616 bigfiretux_right[frame_]->draw(base.x- scroll_x - 8, base.y);
618 bigfiretux_left[frame_]->draw(base.x- scroll_x - 8, base.y);
623 bigfiretux_right_jump->draw(base.x- scroll_x - 8, base.y);
625 bigfiretux_left_jump->draw(base.x- scroll_x - 8, base.y);
631 skidfiretux_right->draw(base.x- scroll_x - 8, base.y);
633 skidfiretux_left->draw(base.x- scroll_x - 8, base.y);
639 duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
641 duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
645 if (holding_something && !duck && physic.get_velocity_y() == 0)
648 largetux.grab_right->draw(base.x - scroll_x, base.y);
650 largetux.grab_left->draw(base.x - scroll_x, base.y);
653 if (invincible_timer.started())
654 largetux_star->draw(base.x - scroll_x, base.y);
660 fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
664 Player::collision(void* p_c_object, int c_object)
666 BadGuy* pbad_c = NULL;
671 pbad_c = (BadGuy*) p_c_object;
673 /* Hurt player if he touches a badguy */
674 if (!pbad_c->dying && !dying &&
675 !safe_timer.started() &&
676 pbad_c->mode != BadGuy::HELD)
678 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN)
680 holding_something = true;
681 pbad_c->mode = BadGuy::HELD;
684 else if (pbad_c->mode == BadGuy::FLAT)
686 // Don't get hurt if we're kicking a flat badguy!
688 else if (pbad_c->mode == BadGuy::KICK)
690 /* Hurt if you get hit by kicked laptop: */
691 if (!invincible_timer.started())
697 pbad_c->dying = DYING_FALLING;
698 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
699 World::current()->add_score(pbad_c->base.x - scroll_x,
701 25 * player_status.score_multiplier);
706 if (!invincible_timer.started())
715 player_status.score_multiplier++;
727 Player::kill(HurtMode mode)
729 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
731 physic.set_velocity_x(0);
733 if (mode == SHRINK && size == BIG)
745 safe_timer.start(TUX_SAFE_TIME);
749 physic.enable_gravity(true);
750 physic.set_acceleration(0, 0);
751 physic.set_velocity(0, 7);
752 dying = DYING_SQUISHED;
763 bool Player::is_dead()
765 if(base.y > screen->h)
771 /* Remove Tux's power ups */
773 Player::remove_powerups()
781 Player::keep_in_bounds()
783 Level* plevel = World::current()->get_level();
785 /* Keep tux in bounds: */
787 { // Lock Tux to the size of the level, so that he doesn't fall of
791 else if (base.x < scroll_x)
796 /* Keep in-bounds, vertically: */
797 if (base.y > screen->h)
802 int scroll_threshold = screen->w/2 - 80;
805 scroll_x += screen->w/2;
806 // Backscrolling for debug mode
807 if (scroll_x < base.x - 80)
808 scroll_x = base.x - 80;
809 else if (scroll_x > base.x + 80)
810 scroll_x = base.x + 80;
811 scroll_x -= screen->w/2;
818 if (base.x > scroll_threshold + scroll_x
819 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
821 // FIXME: Scrolling needs to be handled by a seperate View
822 // class, doing it as a player huck is ugly
824 // Scroll the screen in past center:
825 scroll_x = base.x - scroll_threshold;
827 // Lock the scrolling to the levelsize, so that we don't
828 // scroll over the right border
829 if (scroll_x > 32 * plevel->width - screen->w)
830 scroll_x = 32 * plevel->width - screen->w;