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 Sprite* smalltux_gameover;
33 Sprite* smalltux_star;
34 Sprite* largetux_star;
36 PlayerSprite smalltux;
37 PlayerSprite largetux;
42 PlayerKeymap::PlayerKeymap()
44 keymap.jump = SDLK_UP;
45 keymap.duck = SDLK_DOWN;
46 keymap.left = SDLK_LEFT;
47 keymap.right = SDLK_RIGHT;
48 keymap.fire = SDLK_LCTRL;
51 void player_input_init(player_input_type* pplayer_input)
53 pplayer_input->down = UP;
54 pplayer_input->fire = UP;
55 pplayer_input->left = UP;
56 pplayer_input->old_fire = UP;
57 pplayer_input->right = UP;
58 pplayer_input->up = UP;
59 pplayer_input->old_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;
90 player_input_init(&input);
92 invincible_timer.init(true);
93 skidding_timer.init(true);
94 safe_timer.init(true);
95 frame_timer.init(true);
96 kick_timer.init(true);
102 Player::key_event(SDLKey key, int state)
104 if(key == keymap.right)
109 else if(key == keymap.left)
114 else if(key == keymap.jump)
119 else if(key == keymap.duck)
124 else if(key == keymap.fire)
136 Player::level_begin()
142 previous_base = old_base = base;
147 player_input_init(&input);
149 invincible_timer.init(true);
150 skidding_timer.init(true);
151 safe_timer.init(true);
152 frame_timer.init(true);
158 Player::action(double frame_ratio)
160 bool jumped_in_solid = false;
162 if (input.fire == UP)
163 holding_something = false;
166 previous_base = base;
168 /* --- HANDLE TUX! --- */
169 if(dying == DYING_NOT)
172 physic.apply(frame_ratio, base.x, base.y);
174 if(dying == DYING_NOT)
176 base_type target = base;
178 collision_swept_object_map(&old_base, &base);
180 // Don't accelerate Tux if he is running against a wall
181 if (target.x != base.x)
183 physic.set_velocity_x(0);
186 // special exception for cases where we're stuck under tiles after
187 // being ducked. In this case we drift out
188 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
189 && collision_object_map(base))
191 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
192 previous_base = old_base = base;
199 physic.enable_gravity(true);
203 physic.set_velocity_y(0);
204 jumped_in_solid = true;
210 if (physic.get_velocity_y() < 0)
212 base.y = (int)(((int)base.y / 32) * 32);
213 physic.set_velocity_y(0);
216 physic.enable_gravity(false);
217 /* Reset score multiplier (for multi-hits): */
218 player_status.score_multiplier = 1;
223 if (isbrick(base.x, base.y) ||
224 isfullbox(base.x, base.y))
226 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
227 World::current()->trybumpbadguy(base.x, base.y - 64);
229 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
231 bumpbrick(base.x, base.y);
232 World::current()->tryemptybox(base.x, base.y, RIGHT);
235 if (isbrick(base.x+ 31, base.y) ||
236 isfullbox(base.x+ 31, base.y))
238 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
239 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
242 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
244 bumpbrick(base.x+ 31, base.y);
245 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
257 /* Make sure jumping is off. */
263 /* ---- DONE HANDLING TUX! --- */
266 skidding_timer.check();
267 invincible_timer.check();
275 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
276 issolid(base.x + 1, base.y + base.height) ||
277 issolid(base.x + base.width - 1, base.y + base.height) );
281 Player::under_solid()
283 return ( issolid(base.x + base.width / 2, base.y) ||
284 issolid(base.x + 1, base.y) ||
285 issolid(base.x + base.width - 1, base.y) );
289 Player::handle_horizontal_input()
291 float vx = physic.get_velocity_x();
292 float vy = physic.get_velocity_y();
293 float ax = physic.get_acceleration_x();
294 float ay = physic.get_acceleration_y();
297 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
300 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
305 if (input.fire == UP) {
306 ax = dirsign * WALK_ACCELERATION_X;
308 if(vx >= MAX_WALK_XM && dirsign > 0) {
311 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
316 ax = dirsign * RUN_ACCELERATION_X;
318 if(vx >= MAX_RUN_XM && dirsign > 0) {
321 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
327 // we can reach WALK_SPEED without any acceleration
328 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
329 vx = dirsign * WALK_SPEED;
332 // changing directions?
333 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
334 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
335 skidding_timer.start(SKID_TIME);
336 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
343 // we get slower when not pressing any keys
345 if(fabs(vx) < WALK_SPEED) {
349 ax = WALK_ACCELERATION_X * 1.5;
351 ax = WALK_ACCELERATION_X * -1.5;
355 // if we're on ice slow down acceleration or deceleration
356 if (isice(base.x, base.y + base.height))
358 /* the acceleration/deceleration rate on ice is inversely proportional to
359 * the current velocity.
362 // increasing 1 will increase acceleration/deceleration rate
363 // decreasing 1 will decrease acceleration/deceleration rate
364 // must stay above zero, though
365 if (ax != 0) ax *= 1 / fabs(vx);
368 physic.set_velocity(vx, vy);
369 physic.set_acceleration(ax, ay);
373 Player::handle_vertical_input()
376 if(input.up == DOWN && can_jump)
380 // jump higher if we are running
381 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
382 physic.set_velocity_y(5.8);
384 physic.set_velocity_y(5.2);
390 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
392 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
395 // Let go of jump key
396 else if(input.up == UP && jumping)
399 if(physic.get_velocity_y() > 0) {
400 physic.set_velocity_y(0);
404 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
405 issolid(base.x + 1, base.y + base.height + 64) ||
406 issolid(base.x + base.width - 1, base.y + base.height + 64))
410 && input.old_up == UP)
415 input.old_up = input.up;
419 Player::handle_input()
421 /* Handle horizontal movement: */
422 handle_horizontal_input();
426 if (on_ground() && input.up == UP)
428 if (input.up == DOWN || (input.up == UP && jumping))
430 handle_vertical_input();
435 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
437 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
438 input.old_fire = DOWN;
441 /* tux animations: */
442 if(!frame_timer.check())
444 frame_timer.start(25);
445 if (input.right == UP && input.left == UP)
452 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
453 (global_frame_counter % 4) == 0)
454 frame_main = (frame_main + 1) % 4;
464 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
469 // changing base size confuses collision otherwise
470 old_base = previous_base = base;
472 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
477 // changing base size confuses collision otherwise
478 old_base = previous_base = base;
492 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);
537 PlayerSprite* sprite;
546 if (duck && size != SMALL)
549 sprite->duck_right->draw(base.x - scroll_x, base.y);
551 sprite->duck_left->draw(base.x - scroll_x, base.y);
553 else if (skidding_timer.started())
556 sprite->skid_right->draw(base.x - scroll_x, base.y);
558 sprite->skid_left->draw(base.x - scroll_x, base.y);
560 else if (kick_timer.started())
563 sprite->kick_right->draw(base.x - scroll_x, base.y);
565 sprite->kick_left->draw(base.x - scroll_x, base.y);
567 else if (physic.get_velocity_y() != 0)
570 sprite->jump_right->draw(base.x - scroll_x, base.y);
572 sprite->jump_left->draw(base.x - scroll_x, base.y);
576 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
579 sprite->stand_right->draw( base.x - scroll_x, base.y);
581 sprite->stand_left->draw( base.x - scroll_x, base.y);
586 sprite->walk_right->draw(base.x - scroll_x, base.y);
588 sprite->walk_left->draw(base.x - scroll_x, base.y);
592 // Draw arm overlay graphics when Tux is holding something
593 if (holding_something && physic.get_velocity_y() == 0)
596 sprite->grab_right->draw(base.x - scroll_x, base.y);
598 sprite->grab_left->draw(base.x - scroll_x, base.y);
601 // Draw blinking star overlay
602 if (invincible_timer.started() &&
603 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
605 if (size == SMALL || duck)
606 smalltux_star->draw(base.x - scroll_x, base.y);
608 largetux_star->draw(base.x - scroll_x, base.y);
614 fillrect(base.x - scroll_x, base.y,
615 base.width, base.height, 75,75,75, 150);
619 Player::collision(void* p_c_object, int c_object)
621 BadGuy* pbad_c = NULL;
626 pbad_c = (BadGuy*) p_c_object;
628 /* Hurt player if he touches a badguy */
629 if (!pbad_c->dying && !dying &&
630 !safe_timer.started() &&
631 pbad_c->mode != BadGuy::HELD)
633 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
634 && !holding_something)
636 holding_something = true;
637 pbad_c->mode = BadGuy::HELD;
640 else if (pbad_c->mode == BadGuy::FLAT)
642 // Don't get hurt if we're kicking a flat badguy!
644 else if (pbad_c->mode == BadGuy::KICK)
646 /* Hurt if you get hit by kicked laptop: */
647 if (!invincible_timer.started())
653 pbad_c->dying = DYING_FALLING;
654 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
655 World::current()->add_score(pbad_c->base.x - scroll_x,
657 25 * player_status.score_multiplier);
662 if (!invincible_timer.started())
671 player_status.score_multiplier++;
683 Player::kill(HurtMode mode)
685 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
687 physic.set_velocity_x(0);
689 if (mode == SHRINK && size == BIG)
701 safe_timer.start(TUX_SAFE_TIME);
705 physic.enable_gravity(true);
706 physic.set_acceleration(0, 0);
707 physic.set_velocity(0, 7);
708 dying = DYING_SQUISHED;
719 bool Player::is_dead()
721 if(base.y > screen->h)
727 /* Remove Tux's power ups */
729 Player::remove_powerups()
737 Player::check_bounds()
739 /* Keep tux in bounds: */
741 { // Lock Tux to the size of the level, so that he doesn't fall of
746 /* Keep in-bounds, vertically: */
747 if (base.y > screen->h)
752 if(base.x < scroll_x) // can happen if back scrolling is disabled