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;
201 physic.enable_gravity(true);
205 physic.set_velocity_y(0);
206 jumped_in_solid = true;
212 if (physic.get_velocity_y() < 0)
214 base.y = (int)(((int)base.y / 32) * 32);
215 physic.set_velocity_y(0);
218 physic.enable_gravity(false);
219 /* Reset score multiplier (for multi-hits): */
220 player_status.score_multiplier = 1;
225 if (isbrick(base.x, base.y) ||
226 isfullbox(base.x, base.y))
228 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
229 World::current()->trybumpbadguy(base.x, base.y - 64);
231 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
233 bumpbrick(base.x, base.y);
234 World::current()->tryemptybox(base.x, base.y, RIGHT);
237 if (isbrick(base.x+ 31, base.y) ||
238 isfullbox(base.x+ 31, base.y))
240 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
241 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
244 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
246 bumpbrick(base.x+ 31, base.y);
247 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
259 /* Make sure jumping is off. */
265 /* ---- DONE HANDLING TUX! --- */
268 skidding_timer.check();
269 invincible_timer.check();
277 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
278 issolid(base.x + 1, base.y + base.height) ||
279 issolid(base.x + base.width - 1, base.y + base.height) );
283 Player::under_solid()
285 return ( issolid(base.x + base.width / 2, base.y) ||
286 issolid(base.x + 1, base.y) ||
287 issolid(base.x + base.width - 1, base.y) );
291 Player::handle_horizontal_input()
293 float vx = physic.get_velocity_x();
294 float vy = physic.get_velocity_y();
295 float ax = physic.get_acceleration_x();
296 float ay = physic.get_acceleration_y();
299 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
303 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
309 if (input.fire == UP) {
310 ax = dirsign * WALK_ACCELERATION_X;
312 if(vx >= MAX_WALK_XM && dirsign > 0) {
315 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
320 ax = dirsign * RUN_ACCELERATION_X;
322 if(vx >= MAX_RUN_XM && dirsign > 0) {
325 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
331 // we can reach WALK_SPEED without any acceleration
332 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
333 vx = dirsign * WALK_SPEED;
336 // changing directions?
337 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
338 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
339 skidding_timer.start(SKID_TIME);
340 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
347 // we get slower when not pressing any keys
349 if(fabs(vx) < WALK_SPEED) {
353 ax = WALK_ACCELERATION_X * 1.5;
355 ax = WALK_ACCELERATION_X * -1.5;
359 // if we're on ice slow down acceleration or deceleration
360 if (isice(base.x, base.y + base.height))
362 /* the acceleration/deceleration rate on ice is inversely proportional to
363 * the current velocity.
366 // increasing 1 will increase acceleration/deceleration rate
367 // decreasing 1 will decrease acceleration/deceleration rate
368 // must stay above zero, though
369 if (ax != 0) ax *= 1 / fabs(vx);
372 physic.set_velocity(vx, vy);
373 physic.set_acceleration(ax, ay);
377 Player::handle_vertical_input()
380 if(input.up == DOWN && can_jump)
384 // jump higher if we are running
385 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
386 physic.set_velocity_y(5.8);
388 physic.set_velocity_y(5.2);
394 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
396 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
399 // Let go of jump key
400 else if(input.up == UP && jumping)
403 if(physic.get_velocity_y() > 0) {
404 physic.set_velocity_y(0);
408 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
409 issolid(base.x + 1, base.y + base.height + 64) ||
410 issolid(base.x + base.width - 1, base.y + base.height + 64))
414 && input.old_up == UP)
419 input.old_up = input.up;
423 Player::handle_input()
425 /* Handle horizontal movement: */
426 handle_horizontal_input();
430 if (on_ground() && input.up == UP)
432 if (input.up == DOWN || (input.up == UP && jumping))
434 handle_vertical_input();
439 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
441 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
442 input.old_fire = DOWN;
445 /* tux animations: */
446 if(!frame_timer.check())
448 frame_timer.start(25);
449 if (input.right == UP && input.left == UP)
456 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
457 (global_frame_counter % 4) == 0)
458 frame_main = (frame_main + 1) % 4;
468 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
473 // changing base size confuses collision otherwise
474 old_base = previous_base = base;
476 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
481 // changing base size confuses collision otherwise
482 old_base = previous_base = base;
496 old_base = previous_base = base;
500 Player::grabdistros()
505 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
506 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
508 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
509 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
513 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
514 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
519 /* Enough distros for a One-up? */
520 if (player_status.distros >= DISTROS_LIFEUP)
522 player_status.distros = player_status.distros - DISTROS_LIFEUP;
523 if(player_status.lives < MAX_LIVES)
524 ++player_status.lives;
525 /*We want to hear the sound even, if MAX_LIVES is reached*/
526 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
533 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
535 if (dying == DYING_SQUISHED)
537 smalltux_gameover->draw(base.x - scroll_x, base.y);
541 PlayerSprite* sprite;
550 if (duck && size != SMALL)
553 sprite->duck_right->draw(base.x - scroll_x, base.y);
555 sprite->duck_left->draw(base.x - scroll_x, base.y);
557 else if (skidding_timer.started())
560 sprite->skid_right->draw(base.x - scroll_x, base.y);
562 sprite->skid_left->draw(base.x - scroll_x, base.y);
564 else if (kick_timer.started())
567 sprite->kick_right->draw(base.x - scroll_x, base.y);
569 sprite->kick_left->draw(base.x - scroll_x, base.y);
571 else if (physic.get_velocity_y() != 0)
574 sprite->jump_right->draw(base.x - scroll_x, base.y);
576 sprite->jump_left->draw(base.x - scroll_x, base.y);
580 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
583 sprite->stand_right->draw( base.x - scroll_x, base.y);
585 sprite->stand_left->draw( base.x - scroll_x, base.y);
590 sprite->walk_right->draw(base.x - scroll_x, base.y);
592 sprite->walk_left->draw(base.x - scroll_x, base.y);
596 // Draw arm overlay graphics when Tux is holding something
597 if (holding_something && physic.get_velocity_y() == 0)
600 sprite->grab_right->draw(base.x - scroll_x, base.y);
602 sprite->grab_left->draw(base.x - scroll_x, base.y);
605 // Draw blinking star overlay
606 if (invincible_timer.started() &&
607 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
609 if (size == SMALL || duck)
610 smalltux_star->draw(base.x - scroll_x, base.y);
612 largetux_star->draw(base.x - scroll_x, base.y);
618 fillrect(base.x - scroll_x, base.y,
619 base.width, base.height, 75,75,75, 150);
623 Player::collision(void* p_c_object, int c_object)
625 BadGuy* pbad_c = NULL;
630 pbad_c = (BadGuy*) p_c_object;
632 /* Hurt player if he touches a badguy */
633 if (!pbad_c->dying && !dying &&
634 !safe_timer.started() &&
635 pbad_c->mode != BadGuy::HELD)
637 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
638 && !holding_something)
640 holding_something = true;
641 pbad_c->mode = BadGuy::HELD;
644 else if (pbad_c->mode == BadGuy::FLAT)
646 // Don't get hurt if we're kicking a flat badguy!
648 else if (pbad_c->mode == BadGuy::KICK)
650 /* Hurt if you get hit by kicked laptop: */
651 if (!invincible_timer.started())
657 pbad_c->dying = DYING_FALLING;
658 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
659 World::current()->add_score(pbad_c->base.x - scroll_x,
661 25 * player_status.score_multiplier);
666 if (!invincible_timer.started())
675 player_status.score_multiplier++;
687 Player::kill(HurtMode mode)
689 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
691 physic.set_velocity_x(0);
693 if (mode == SHRINK && size == BIG)
705 safe_timer.start(TUX_SAFE_TIME);
709 physic.enable_gravity(true);
710 physic.set_acceleration(0, 0);
711 physic.set_velocity(0, 7);
712 dying = DYING_SQUISHED;
723 bool Player::is_dead()
725 if(base.y > screen->h || base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // last condition can happen in auto-scrolling
731 /* Remove Tux's power ups */
733 Player::remove_powerups()
741 Player::check_bounds(bool back_scrolling, bool hor_autoscroll)
743 /* Keep tux in bounds: */
745 { // Lock Tux to the size of the level, so that he doesn't fall of
750 /* Keep in-bounds, vertically: */
751 if (base.y > screen->h)
756 if(base.x < scroll_x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled
759 if(base.x == scroll_x && hor_autoscroll)
760 if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32)))
763 if(base.x + base.width > scroll_x + screen->w && hor_autoscroll)
764 base.x = scroll_x + screen->w - base.width;