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;
91 player_input_init(&input);
93 invincible_timer.init(true);
94 skidding_timer.init(true);
95 safe_timer.init(true);
96 frame_timer.init(true);
97 kick_timer.init(true);
103 Player::key_event(SDLKey key, int state)
105 if(key == keymap.right)
110 else if(key == keymap.left)
115 else if(key == keymap.jump)
120 else if(key == keymap.duck)
125 else if(key == keymap.fire)
137 Player::level_begin()
143 previous_base = old_base = base;
148 player_input_init(&input);
150 invincible_timer.init(true);
151 skidding_timer.init(true);
152 safe_timer.init(true);
153 frame_timer.init(true);
159 Player::action(double frame_ratio)
161 bool jumped_in_solid = false;
163 if (input.fire == UP)
164 holding_something = false;
167 previous_base = base;
169 /* --- HANDLE TUX! --- */
170 if(dying == DYING_NOT)
173 physic.apply(frame_ratio, base.x, base.y);
175 if(dying == DYING_NOT)
177 base_type target = base;
179 collision_swept_object_map(&old_base, &base);
181 // Don't accelerate Tux if he is running against a wall
182 if (target.x != base.x)
184 physic.set_velocity_x(0);
187 // special exception for cases where we're stuck under tiles after
188 // being ducked. In this case we drift out
189 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
190 && collision_object_map(base))
192 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
193 previous_base = old_base = base;
200 physic.enable_gravity(true);
204 physic.set_velocity_y(0);
205 jumped_in_solid = true;
211 if (physic.get_velocity_y() < 0)
213 base.y = (int)(((int)base.y / 32) * 32);
214 physic.set_velocity_y(0);
217 physic.enable_gravity(false);
218 /* Reset score multiplier (for multi-hits): */
219 player_status.score_multiplier = 1;
224 if (isbrick(base.x, base.y) ||
225 isfullbox(base.x, base.y))
227 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
228 World::current()->trybumpbadguy(base.x, base.y - 64);
230 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
232 bumpbrick(base.x, base.y);
233 World::current()->tryemptybox(base.x, base.y, RIGHT);
236 if (isbrick(base.x+ 31, base.y) ||
237 isfullbox(base.x+ 31, base.y))
239 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
240 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
243 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
245 bumpbrick(base.x+ 31, base.y);
246 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
258 /* Make sure jumping is off. */
264 /* ---- DONE HANDLING TUX! --- */
267 skidding_timer.check();
268 invincible_timer.check();
276 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
277 issolid(base.x + 1, base.y + base.height) ||
278 issolid(base.x + base.width - 1, base.y + base.height) );
282 Player::under_solid()
284 return ( issolid(base.x + base.width / 2, base.y) ||
285 issolid(base.x + 1, base.y) ||
286 issolid(base.x + base.width - 1, base.y) );
290 Player::handle_horizontal_input()
292 float vx = physic.get_velocity_x();
293 float vy = physic.get_velocity_y();
294 float ax = physic.get_acceleration_x();
295 float ay = physic.get_acceleration_y();
298 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
302 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
308 if (input.fire == UP) {
309 ax = dirsign * WALK_ACCELERATION_X;
311 if(vx >= MAX_WALK_XM && dirsign > 0) {
314 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
319 ax = dirsign * RUN_ACCELERATION_X;
321 if(vx >= MAX_RUN_XM && dirsign > 0) {
324 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
330 // we can reach WALK_SPEED without any acceleration
331 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
332 vx = dirsign * WALK_SPEED;
335 // changing directions?
336 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
337 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
338 skidding_timer.start(SKID_TIME);
339 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
346 // we get slower when not pressing any keys
348 if(fabs(vx) < WALK_SPEED) {
352 ax = WALK_ACCELERATION_X * 1.5;
354 ax = WALK_ACCELERATION_X * -1.5;
358 // if we're on ice slow down acceleration or deceleration
359 if (isice(base.x, base.y + base.height))
361 /* the acceleration/deceleration rate on ice is inversely proportional to
362 * the current velocity.
365 // increasing 1 will increase acceleration/deceleration rate
366 // decreasing 1 will decrease acceleration/deceleration rate
367 // must stay above zero, though
368 if (ax != 0) ax *= 1 / fabs(vx);
371 physic.set_velocity(vx, vy);
372 physic.set_acceleration(ax, ay);
376 Player::handle_vertical_input()
379 if(input.up == DOWN && can_jump)
383 // jump higher if we are running
384 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
385 physic.set_velocity_y(5.8);
387 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 // Let go of jump key
399 else if(input.up == UP && jumping)
402 if(physic.get_velocity_y() > 0) {
403 physic.set_velocity_y(0);
407 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
408 issolid(base.x + 1, base.y + base.height + 64) ||
409 issolid(base.x + base.width - 1, base.y + base.height + 64))
413 && input.old_up == UP)
418 input.old_up = input.up;
422 Player::handle_input()
424 /* Handle horizontal movement: */
425 handle_horizontal_input();
429 if (on_ground() && input.up == UP)
431 if (input.up == DOWN || (input.up == UP && jumping))
433 handle_vertical_input();
438 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
440 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
441 input.old_fire = DOWN;
444 /* tux animations: */
445 if(!frame_timer.check())
447 frame_timer.start(25);
448 if (input.right == UP && input.left == UP)
455 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
456 (global_frame_counter % 4) == 0)
457 frame_main = (frame_main + 1) % 4;
467 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
472 // changing base size confuses collision otherwise
473 old_base = previous_base = base;
475 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
480 // changing base size confuses collision otherwise
481 old_base = previous_base = base;
495 old_base = previous_base = base;
499 Player::grabdistros()
504 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
505 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
507 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
508 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
512 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
513 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
518 /* Enough distros for a One-up? */
519 if (player_status.distros >= DISTROS_LIFEUP)
521 player_status.distros = player_status.distros - DISTROS_LIFEUP;
522 if(player_status.lives < MAX_LIVES)
523 ++player_status.lives;
524 /*We want to hear the sound even, if MAX_LIVES is reached*/
525 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
532 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
534 if (dying == DYING_SQUISHED)
536 smalltux_gameover->draw(base.x - scroll_x, base.y);
540 PlayerSprite* sprite;
549 if (duck && size != SMALL)
552 sprite->duck_right->draw(base.x - scroll_x, base.y);
554 sprite->duck_left->draw(base.x - scroll_x, base.y);
556 else if (skidding_timer.started())
559 sprite->skid_right->draw(base.x - scroll_x, base.y);
561 sprite->skid_left->draw(base.x - scroll_x, base.y);
563 else if (kick_timer.started())
566 sprite->kick_right->draw(base.x - scroll_x, base.y);
568 sprite->kick_left->draw(base.x - scroll_x, base.y);
570 else if (physic.get_velocity_y() != 0)
573 sprite->jump_right->draw(base.x - scroll_x, base.y);
575 sprite->jump_left->draw(base.x - scroll_x, base.y);
579 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
582 sprite->stand_right->draw( base.x - scroll_x, base.y);
584 sprite->stand_left->draw( base.x - scroll_x, base.y);
589 sprite->walk_right->draw(base.x - scroll_x, base.y);
591 sprite->walk_left->draw(base.x - scroll_x, base.y);
595 // Draw arm overlay graphics when Tux is holding something
596 if (holding_something && physic.get_velocity_y() == 0)
599 sprite->grab_right->draw(base.x - scroll_x, base.y);
601 sprite->grab_left->draw(base.x - scroll_x, base.y);
604 // Draw blinking star overlay
605 if (invincible_timer.started() &&
606 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
608 if (size == SMALL || duck)
609 smalltux_star->draw(base.x - scroll_x, base.y);
611 largetux_star->draw(base.x - scroll_x, base.y);
617 fillrect(base.x - scroll_x, base.y,
618 base.width, base.height, 75,75,75, 150);
622 Player::collision(void* p_c_object, int c_object)
624 BadGuy* pbad_c = NULL;
629 pbad_c = (BadGuy*) p_c_object;
631 /* Hurt player if he touches a badguy */
632 if (!pbad_c->dying && !dying &&
633 !safe_timer.started() &&
634 pbad_c->mode != BadGuy::HELD)
636 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
637 && !holding_something)
639 holding_something = true;
640 pbad_c->mode = BadGuy::HELD;
643 else if (pbad_c->mode == BadGuy::FLAT)
645 // Don't get hurt if we're kicking a flat badguy!
647 else if (pbad_c->mode == BadGuy::KICK)
649 /* Hurt if you get hit by kicked laptop: */
650 if (!invincible_timer.started())
656 pbad_c->dying = DYING_FALLING;
657 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
658 World::current()->add_score(pbad_c->base.x - scroll_x,
660 25 * player_status.score_multiplier);
665 if (!invincible_timer.started())
674 player_status.score_multiplier++;
686 Player::kill(HurtMode mode)
688 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
690 physic.set_velocity_x(0);
692 if (mode == SHRINK && size == BIG)
704 safe_timer.start(TUX_SAFE_TIME);
708 physic.enable_gravity(true);
709 physic.set_acceleration(0, 0);
710 physic.set_velocity(0, 7);
711 dying = DYING_SQUISHED;
722 bool Player::is_dead()
724 if(base.y > screen->h)
730 /* Remove Tux's power ups */
732 Player::remove_powerups()
740 Player::check_bounds()
742 /* Keep tux in bounds: */
744 { // Lock Tux to the size of the level, so that he doesn't fall of
749 /* Keep in-bounds, vertically: */
750 if (base.y > screen->h)
755 if(base.x < scroll_x) // can happen if back scrolling is disabled