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 #define AUTOSCROLL_DEAD_INTERVAL 300
36 Sprite* smalltux_gameover;
37 Sprite* smalltux_star;
38 Sprite* largetux_star;
40 PlayerSprite smalltux;
41 PlayerSprite largetux;
47 PlayerKeymap::PlayerKeymap()
49 keymap.jump = SDLK_UP;
50 keymap.duck = SDLK_DOWN;
51 keymap.left = SDLK_LEFT;
52 keymap.right = SDLK_RIGHT;
53 keymap.fire = SDLK_LCTRL;
56 void player_input_init(player_input_type* pplayer_input)
58 pplayer_input->down = UP;
59 pplayer_input->fire = UP;
60 pplayer_input->left = UP;
61 pplayer_input->old_fire = UP;
62 pplayer_input->right = UP;
63 pplayer_input->up = UP;
64 pplayer_input->old_up = UP;
70 Level* plevel = World::current()->get_level();
72 holding_something = false;
78 got_power = NONE_POWER;
80 base.x = plevel->start_pos_x;
81 base.y = plevel->start_pos_y;
84 previous_base = old_base = base;
97 player_input_init(&input);
99 invincible_timer.init(true);
100 skidding_timer.init(true);
101 safe_timer.init(true);
102 frame_timer.init(true);
103 kick_timer.init(true);
109 Player::key_event(SDLKey key, int state)
111 if(key == keymap.right)
116 else if(key == keymap.left)
121 else if(key == keymap.jump)
126 else if(key == keymap.duck)
131 else if(key == keymap.fire)
143 Player::level_begin()
149 previous_base = old_base = base;
154 player_input_init(&input);
156 invincible_timer.init(true);
157 skidding_timer.init(true);
158 safe_timer.init(true);
159 frame_timer.init(true);
165 Player::action(double frame_ratio)
167 bool jumped_in_solid = false;
169 if (input.fire == UP)
170 holding_something = false;
173 previous_base = base;
175 /* --- HANDLE TUX! --- */
176 if(dying == DYING_NOT)
179 physic.apply(frame_ratio, base.x, base.y);
181 if(dying == DYING_NOT)
183 base_type target = base;
185 collision_swept_object_map(&old_base, &base);
187 if (!invincible_timer.started()
188 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
189 || isspike(base.x, base.y + base.height)
190 || isspike(base.x + base.width, base.y + base.height)))
195 // Don't accelerate Tux if he is running against a wall
196 if (target.x != base.x)
198 physic.set_velocity_x(0);
201 // special exception for cases where we're stuck under tiles after
202 // being ducked. In this case we drift out
203 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
204 && collision_object_map(base))
206 base.x += frame_ratio * WALK_SPEED * (dir ? 1: -1);
207 previous_base = old_base = base;
213 physic.enable_gravity(true);
217 physic.set_velocity_y(0);
218 jumped_in_solid = true;
224 if (physic.get_velocity_y() < 0)
226 base.y = (int)(((int)base.y / 32) * 32);
227 physic.set_velocity_y(0);
230 physic.enable_gravity(false);
231 /* Reset score multiplier (for multi-hits): */
232 if (!invincible_timer.started())
233 player_status.score_multiplier = 1;
238 if (isbrick(base.x, base.y) ||
239 isfullbox(base.x, base.y))
241 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
242 World::current()->trybumpbadguy(base.x, base.y - 64);
244 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
246 bumpbrick(base.x, base.y);
247 World::current()->tryemptybox(base.x, base.y, RIGHT);
250 if (isbrick(base.x+ 31, base.y) ||
251 isfullbox(base.x+ 31, base.y))
253 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
254 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
257 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
259 bumpbrick(base.x+ 31, base.y);
260 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
272 /* Make sure jumping is off. */
278 /* ---- DONE HANDLING TUX! --- */
281 skidding_timer.check();
282 invincible_timer.check();
290 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
291 issolid(base.x + 1, base.y + base.height) ||
292 issolid(base.x + base.width - 1, base.y + base.height) );
296 Player::under_solid()
298 return ( issolid(base.x + base.width / 2, base.y) ||
299 issolid(base.x + 1, base.y) ||
300 issolid(base.x + base.width - 1, base.y) );
304 Player::handle_horizontal_input()
306 float vx = physic.get_velocity_x();
307 float vy = physic.get_velocity_y();
308 float ax = physic.get_acceleration_x();
309 float ay = physic.get_acceleration_y();
312 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
316 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
322 if (input.fire == UP) {
323 ax = dirsign * WALK_ACCELERATION_X;
325 if(vx >= MAX_WALK_XM && dirsign > 0) {
328 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
333 ax = dirsign * RUN_ACCELERATION_X;
335 if(vx >= MAX_RUN_XM && dirsign > 0) {
338 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
344 // we can reach WALK_SPEED without any acceleration
345 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
346 vx = dirsign * WALK_SPEED;
349 // changing directions?
350 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
351 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
352 skidding_timer.start(SKID_TIME);
353 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
360 // we get slower when not pressing any keys
362 if(fabs(vx) < WALK_SPEED) {
366 ax = WALK_ACCELERATION_X * 1.5;
368 ax = WALK_ACCELERATION_X * -1.5;
372 // if we're on ice slow down acceleration or deceleration
373 if (isice(base.x, base.y + base.height))
375 /* the acceleration/deceleration rate on ice is inversely proportional to
376 * the current velocity.
379 // increasing 1 will increase acceleration/deceleration rate
380 // decreasing 1 will decrease acceleration/deceleration rate
381 // must stay above zero, though
382 if (ax != 0) ax *= 1 / fabs(vx);
385 physic.set_velocity(vx, vy);
386 physic.set_acceleration(ax, ay);
390 Player::handle_vertical_input()
393 if(input.up == DOWN && can_jump && on_ground())
395 if(duck) { // only jump a little bit when in duck mode {
396 physic.set_velocity_y(3);
398 // jump higher if we are running
399 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
400 physic.set_velocity_y(5.8);
402 physic.set_velocity_y(5.2);
409 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
411 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
413 // Let go of jump key
414 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
417 physic.set_velocity_y(0);
420 if (input.down == DOWN && !on_ground() && !duck && size == BIG)
422 else if (input.down == UP)
424 if (input.down == DOWN && butt_jump && on_ground())
426 if(World::current()->trybreakbrick(base.x, base.y + base.height, false)
427 || World::current()->trybreakbrick(
428 base.x + base.width, base.y + base.height, false)) {
429 // make tux jumping a little bit again after breaking the bricks
430 physic.set_velocity_y(2);
435 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
436 issolid(base.x + 1, base.y + base.height + 64) ||
437 issolid(base.x + base.width - 1, base.y + base.height + 64))
441 && input.old_up == UP)
446 input.old_up = input.up;
450 Player::handle_input()
452 /* Handle horizontal movement: */
453 handle_horizontal_input();
457 if (on_ground() && input.up == UP)
459 handle_vertical_input();
462 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
464 holding_something = true;
465 World::current()->add_bullet(base.x, base.y + (base.height/2), physic.get_velocity_x(), dir);
466 input.old_fire = DOWN;
469 /* tux animations: */
470 if(!frame_timer.check())
472 frame_timer.start(25);
473 if (input.right == UP && input.left == UP)
480 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
481 (global_frame_counter % 4) == 0)
482 frame_main = (frame_main + 1) % 4;
492 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
497 // changing base size confuses collision otherwise
498 old_base = previous_base = base;
500 else if(input.down == UP && size == BIG && duck)
502 // try if we can really unduck
505 // when unducking in air we need some space to do so
506 if(on_ground() || !collision_object_map(base)) {
508 // changing base size confuses collision otherwise
509 old_base = previous_base = base;
511 // undo the ducking changes
528 old_base = previous_base = base;
532 Player::grabdistros()
537 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
538 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
540 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
541 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
545 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
546 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
551 /* Enough distros for a One-up? */
552 if (player_status.distros >= DISTROS_LIFEUP)
554 player_status.distros = player_status.distros - DISTROS_LIFEUP;
555 if(player_status.lives < MAX_LIVES)
556 ++player_status.lives;
557 /*We want to hear the sound even, if MAX_LIVES is reached*/
558 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
565 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
567 if (dying == DYING_SQUISHED)
569 smalltux_gameover->draw(base.x, base.y);
573 PlayerSprite* sprite;
577 else if (got_power == FIRE_POWER)
579 else if (got_power == ICE_POWER)
584 if (duck && size != SMALL)
587 sprite->duck_right->draw(base.x, base.y);
589 sprite->duck_left->draw(base.x, base.y);
591 else if (skidding_timer.started())
594 sprite->skid_right->draw(base.x, base.y);
596 sprite->skid_left->draw(base.x, base.y);
598 else if (kick_timer.started())
601 sprite->kick_right->draw(base.x, base.y);
603 sprite->kick_left->draw(base.x, base.y);
605 else if (physic.get_velocity_y() != 0)
608 sprite->jump_right->draw(base.x, base.y);
610 sprite->jump_left->draw(base.x, base.y);
614 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
617 sprite->stand_right->draw( base.x, base.y);
619 sprite->stand_left->draw( base.x, base.y);
624 sprite->walk_right->draw(base.x, base.y);
626 sprite->walk_left->draw(base.x, base.y);
630 // Draw arm overlay graphics when Tux is holding something
631 if (holding_something && physic.get_velocity_y() == 0)
634 sprite->grab_right->draw(base.x, base.y);
636 sprite->grab_left->draw(base.x, base.y);
639 // Draw blinking star overlay
640 if (invincible_timer.started() &&
641 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
643 if (size == SMALL || duck)
644 smalltux_star->draw(base.x, base.y);
646 largetux_star->draw(base.x, base.y);
652 fillrect(base.x - scroll_x, base.y - scroll_y,
653 base.width, base.height, 75,75,75, 150);
657 Player::collision(void* p_c_object, int c_object)
659 BadGuy* pbad_c = NULL;
660 Trampoline* ptramp_c = NULL;
665 pbad_c = (BadGuy*) p_c_object;
667 /* Hurt player if he touches a badguy */
668 if (!pbad_c->dying && !dying &&
669 !safe_timer.started() &&
670 pbad_c->mode != BadGuy::HELD)
672 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
673 && !holding_something)
675 holding_something = true;
676 pbad_c->mode = BadGuy::HELD;
679 else if (pbad_c->mode == BadGuy::FLAT)
681 // Don't get hurt if we're kicking a flat badguy!
683 else if (pbad_c->mode == BadGuy::KICK)
685 /* Hurt if you get hit by kicked laptop: */
686 if (!invincible_timer.started())
693 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
694 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
695 || pbad_c->kind == BAD_SPIKY))
699 if (!invincible_timer.started())
708 player_status.score_multiplier++;
713 ptramp_c = (Trampoline*) p_c_object;
715 // Pick up trampoline
716 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
718 holding_something = true;
719 ptramp_c->mode = Trampoline::M_HELD;
720 ptramp_c->base.y -= 8;
722 // Set down trampoline
723 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
725 holding_something = false;
726 ptramp_c->mode = Trampoline::M_NORMAL;
727 ptramp_c->base.y += 8;
728 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
731 // ptramp_c->base.x = base.x + base.width+1;
733 // ptramp_c->base.x = base.x - base.width-1;
736 // Don't let tux walk through trampoline
737 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
739 if (physic.get_velocity_x() > 0) // RIGHT
741 physic.set_velocity_x(0);
742 base.x = ptramp_c->base.x - base.width;
744 else if (physic.get_velocity_x() < 0) // LEFT
746 physic.set_velocity_x(0);
747 base.x = ptramp_c->base.x + ptramp_c->base.width;
763 Player::kill(HurtMode mode)
765 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
767 physic.set_velocity_x(0);
769 if (mode == SHRINK && size == BIG)
771 if (got_power != NONE_POWER)
773 got_power = NONE_POWER;
781 safe_timer.start(TUX_SAFE_TIME);
785 physic.enable_gravity(true);
786 physic.set_acceleration(0, 0);
787 physic.set_velocity(0, 7);
788 if(dying != DYING_SQUISHED)
789 --player_status.lives;
790 dying = DYING_SQUISHED;
801 bool Player::is_dead()
803 if(base.y > screen->h + scroll_y || base.y > World::current()->get_level()->height*32 ||
804 base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // can happen in auto-scrolling
810 /* Remove Tux's power ups */
812 Player::remove_powerups()
814 got_power = NONE_POWER;
820 Player::check_bounds(bool back_scrolling, bool hor_autoscroll)
822 /* Keep tux in bounds: */
824 { // Lock Tux to the size of the level, so that he doesn't fall of
829 /* Keep in-bounds, vertically: */
830 if (base.y > World::current()->get_level()->height * /*TILE_HEIGHT*/ 32)
835 if(base.x < scroll_x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled
840 if(base.x == scroll_x)
841 if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32)))
844 if(base.x + base.width > scroll_x + screen->w)
845 base.x = scroll_x + screen->w - base.width;