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;
44 PlayerKeymap::PlayerKeymap()
46 keymap.jump = SDLK_UP;
47 keymap.duck = SDLK_DOWN;
48 keymap.left = SDLK_LEFT;
49 keymap.right = SDLK_RIGHT;
50 keymap.fire = SDLK_LCTRL;
53 void player_input_init(player_input_type* pplayer_input)
55 pplayer_input->down = UP;
56 pplayer_input->fire = UP;
57 pplayer_input->left = UP;
58 pplayer_input->old_fire = UP;
59 pplayer_input->right = UP;
60 pplayer_input->up = UP;
61 pplayer_input->old_up = UP;
67 Level* plevel = World::current()->get_level();
69 holding_something = false;
77 base.x = plevel->start_pos_x;
78 base.y = plevel->start_pos_y;
81 previous_base = old_base = base;
93 player_input_init(&input);
95 invincible_timer.init(true);
96 skidding_timer.init(true);
97 safe_timer.init(true);
98 frame_timer.init(true);
99 kick_timer.init(true);
105 Player::key_event(SDLKey key, int state)
107 if(key == keymap.right)
112 else if(key == keymap.left)
117 else if(key == keymap.jump)
122 else if(key == keymap.duck)
127 else if(key == keymap.fire)
139 Player::level_begin()
145 previous_base = old_base = base;
150 player_input_init(&input);
152 invincible_timer.init(true);
153 skidding_timer.init(true);
154 safe_timer.init(true);
155 frame_timer.init(true);
161 Player::action(double frame_ratio)
163 bool jumped_in_solid = false;
165 if (input.fire == UP)
166 holding_something = false;
169 previous_base = base;
171 /* --- HANDLE TUX! --- */
172 if(dying == DYING_NOT)
175 physic.apply(frame_ratio, base.x, base.y);
177 if(dying == DYING_NOT)
179 base_type target = base;
181 collision_swept_object_map(&old_base, &base);
183 // Don't accelerate Tux if he is running against a wall
184 if (target.x != base.x)
186 physic.set_velocity_x(0);
189 // special exception for cases where we're stuck under tiles after
190 // being ducked. In this case we drift out
191 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
192 && collision_object_map(base))
194 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
195 previous_base = old_base = base;
202 physic.enable_gravity(true);
206 physic.set_velocity_y(0);
207 jumped_in_solid = true;
213 if (physic.get_velocity_y() < 0)
215 base.y = (int)(((int)base.y / 32) * 32);
216 physic.set_velocity_y(0);
219 physic.enable_gravity(false);
220 /* Reset score multiplier (for multi-hits): */
221 player_status.score_multiplier = 1;
226 if (isbrick(base.x, base.y) ||
227 isfullbox(base.x, base.y))
229 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
230 World::current()->trybumpbadguy(base.x, base.y - 64);
232 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
234 bumpbrick(base.x, base.y);
235 World::current()->tryemptybox(base.x, base.y, RIGHT);
238 if (isbrick(base.x+ 31, base.y) ||
239 isfullbox(base.x+ 31, base.y))
241 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
242 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
245 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
247 bumpbrick(base.x+ 31, base.y);
248 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
260 /* Make sure jumping is off. */
266 /* ---- DONE HANDLING TUX! --- */
269 skidding_timer.check();
270 invincible_timer.check();
278 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
279 issolid(base.x + 1, base.y + base.height) ||
280 issolid(base.x + base.width - 1, base.y + base.height) );
284 Player::under_solid()
286 return ( issolid(base.x + base.width / 2, base.y) ||
287 issolid(base.x + 1, base.y) ||
288 issolid(base.x + base.width - 1, base.y) );
292 Player::handle_horizontal_input()
294 float vx = physic.get_velocity_x();
295 float vy = physic.get_velocity_y();
296 float ax = physic.get_acceleration_x();
297 float ay = physic.get_acceleration_y();
300 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
304 } 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()
381 if(input.up == DOWN && can_jump)
385 // jump higher if we are running
386 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
387 physic.set_velocity_y(5.8);
389 physic.set_velocity_y(5.2);
395 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
397 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
400 // Let go of jump key
401 else if(input.up == UP && jumping)
404 if(physic.get_velocity_y() > 0) {
405 physic.set_velocity_y(0);
409 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
410 issolid(base.x + 1, base.y + base.height + 64) ||
411 issolid(base.x + base.width - 1, base.y + base.height + 64))
415 && input.old_up == UP)
420 input.old_up = input.up;
424 Player::handle_input()
426 /* Handle horizontal movement: */
427 handle_horizontal_input();
431 if (on_ground() && input.up == UP)
433 if (input.up == DOWN || (input.up == UP && jumping))
435 handle_vertical_input();
440 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
442 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
443 input.old_fire = DOWN;
446 /* tux animations: */
447 if(!frame_timer.check())
449 frame_timer.start(25);
450 if (input.right == UP && input.left == UP)
457 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
458 (global_frame_counter % 4) == 0)
459 frame_main = (frame_main + 1) % 4;
469 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
474 // changing base size confuses collision otherwise
475 old_base = previous_base = base;
477 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
482 // changing base size confuses collision otherwise
483 old_base = previous_base = base;
497 old_base = previous_base = base;
501 Player::grabdistros()
506 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
507 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
509 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
510 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
514 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
515 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
520 /* Enough distros for a One-up? */
521 if (player_status.distros >= DISTROS_LIFEUP)
523 player_status.distros = player_status.distros - DISTROS_LIFEUP;
524 if(player_status.lives < MAX_LIVES)
525 ++player_status.lives;
526 /*We want to hear the sound even, if MAX_LIVES is reached*/
527 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
534 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
536 if (dying == DYING_SQUISHED)
538 smalltux_gameover->draw(base.x - scroll_x, base.y);
542 PlayerSprite* sprite;
551 if (duck && size != SMALL)
554 sprite->duck_right->draw(base.x - scroll_x, base.y);
556 sprite->duck_left->draw(base.x - scroll_x, base.y);
558 else if (skidding_timer.started())
561 sprite->skid_right->draw(base.x - scroll_x, base.y);
563 sprite->skid_left->draw(base.x - scroll_x, base.y);
565 else if (kick_timer.started())
568 sprite->kick_right->draw(base.x - scroll_x, base.y);
570 sprite->kick_left->draw(base.x - scroll_x, base.y);
572 else if (physic.get_velocity_y() != 0)
575 sprite->jump_right->draw(base.x - scroll_x, base.y);
577 sprite->jump_left->draw(base.x - scroll_x, base.y);
581 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
584 sprite->stand_right->draw( base.x - scroll_x, base.y);
586 sprite->stand_left->draw( base.x - scroll_x, base.y);
591 sprite->walk_right->draw(base.x - scroll_x, base.y);
593 sprite->walk_left->draw(base.x - scroll_x, base.y);
597 // Draw arm overlay graphics when Tux is holding something
598 if (holding_something && physic.get_velocity_y() == 0)
601 sprite->grab_right->draw(base.x - scroll_x, base.y);
603 sprite->grab_left->draw(base.x - scroll_x, base.y);
606 // Draw blinking star overlay
607 if (invincible_timer.started() &&
608 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
610 if (size == SMALL || duck)
611 smalltux_star->draw(base.x - scroll_x, base.y);
613 largetux_star->draw(base.x - scroll_x, base.y);
619 fillrect(base.x - scroll_x, base.y,
620 base.width, base.height, 75,75,75, 150);
624 Player::collision(void* p_c_object, int c_object)
626 BadGuy* pbad_c = NULL;
631 pbad_c = (BadGuy*) p_c_object;
633 /* Hurt player if he touches a badguy */
634 if (!pbad_c->dying && !dying &&
635 !safe_timer.started() &&
636 pbad_c->mode != BadGuy::HELD)
638 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
639 && !holding_something)
641 holding_something = true;
642 pbad_c->mode = BadGuy::HELD;
645 else if (pbad_c->mode == BadGuy::FLAT)
647 // Don't get hurt if we're kicking a flat badguy!
649 else if (pbad_c->mode == BadGuy::KICK)
651 /* Hurt if you get hit by kicked laptop: */
652 if (!invincible_timer.started())
658 pbad_c->dying = DYING_FALLING;
659 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
660 World::current()->add_score(pbad_c->base.x - scroll_x,
662 25 * player_status.score_multiplier);
667 if (!invincible_timer.started())
676 player_status.score_multiplier++;
688 Player::kill(HurtMode mode)
690 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
692 physic.set_velocity_x(0);
694 if (mode == SHRINK && size == BIG)
706 safe_timer.start(TUX_SAFE_TIME);
710 physic.enable_gravity(true);
711 physic.set_acceleration(0, 0);
712 physic.set_velocity(0, 7);
713 dying = DYING_SQUISHED;
724 bool Player::is_dead()
726 if(base.y > screen->h || base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // last condition can happen in auto-scrolling
732 /* Remove Tux's power ups */
734 Player::remove_powerups()
742 Player::check_bounds()
744 /* Keep tux in bounds: */
746 { // Lock Tux to the size of the level, so that he doesn't fall of
751 /* Keep in-bounds, vertically: */
752 if (base.y > screen->h)
757 if(base.x < scroll_x) // can happen if back scrolling is disabled
760 if(base.x == scroll_x)
761 if(issolid(base.x, base.y) || issolid(base.x, base.y+32))
764 if(base.x + base.width > scroll_x + screen->w)
765 base.x = scroll_x + screen->w - base.width;