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;
67 Player::Player(DisplayManager& display_manager)
69 display_manager.add_drawable(this, LAYER_OBJECTS-1); // for tux himself
70 display_manager.add_drawable(this, LAYER_OBJECTS+1); // for the arm
81 Level* plevel = World::current()->get_level();
83 holding_something = false;
89 got_power = NONE_POWER;
91 base.x = plevel->start_pos_x;
92 base.y = plevel->start_pos_y;
93 previous_base = old_base = base;
106 player_input_init(&input);
108 invincible_timer.init(true);
109 skidding_timer.init(true);
110 safe_timer.init(true);
111 frame_timer.init(true);
112 kick_timer.init(true);
118 Player::key_event(SDLKey key, int state)
120 if(key == keymap.right)
125 else if(key == keymap.left)
130 else if(key == keymap.jump)
135 else if(key == keymap.duck)
140 else if(key == keymap.fire)
152 Player::level_begin()
156 previous_base = old_base = base;
161 player_input_init(&input);
163 invincible_timer.init(true);
164 skidding_timer.init(true);
165 safe_timer.init(true);
166 frame_timer.init(true);
172 Player::action(float elapsed_time)
174 bool jumped_in_solid = false;
176 if (input.fire == UP)
177 holding_something = false;
180 previous_base = base;
182 /* --- HANDLE TUX! --- */
183 if(dying == DYING_NOT)
186 physic.apply(elapsed_time, base.x, base.y);
188 if(dying == DYING_NOT)
190 base_type target = base;
192 collision_swept_object_map(&old_base, &base);
194 if (!invincible_timer.started()
195 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
196 || isspike(base.x, base.y + base.height)
197 || isspike(base.x + base.width, base.y + base.height)))
202 // Don't accelerate Tux if he is running against a wall
203 if (target.x != base.x)
205 physic.set_velocity_x(0);
208 // special exception for cases where we're stuck under tiles after
209 // being ducked. In this case we drift out
210 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
211 && collision_object_map(base))
213 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
214 previous_base = old_base = base;
220 physic.enable_gravity(true);
224 physic.set_velocity_y(0);
225 jumped_in_solid = true;
231 if (physic.get_velocity_y() < 0)
233 base.y = (int)(((int)base.y / 32) * 32);
234 physic.set_velocity_y(0);
237 physic.enable_gravity(false);
238 /* Reset score multiplier (for multi-hits): */
239 if (!invincible_timer.started())
240 player_status.score_multiplier = 1;
245 if (isbrick(base.x, base.y) ||
246 isfullbox(base.x, base.y))
248 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
249 World::current()->trybumpbadguy(base.x, base.y - 64);
251 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
253 bumpbrick(base.x, base.y);
254 World::current()->tryemptybox(base.x, base.y, RIGHT);
257 if (isbrick(base.x+ 31, base.y) ||
258 isfullbox(base.x+ 31, base.y))
260 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
261 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
264 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
266 bumpbrick(base.x+ 31, base.y);
267 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
279 /* Make sure jumping is off. */
285 /* ---- DONE HANDLING TUX! --- */
288 skidding_timer.check();
289 invincible_timer.check();
297 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
298 issolid(base.x + 1, base.y + base.height) ||
299 issolid(base.x + base.width - 1, base.y + base.height) );
303 Player::under_solid()
305 return ( issolid(base.x + base.width / 2, base.y) ||
306 issolid(base.x + 1, base.y) ||
307 issolid(base.x + base.width - 1, base.y) );
311 Player::handle_horizontal_input()
313 float vx = physic.get_velocity_x();
314 float vy = physic.get_velocity_y();
315 float ax = physic.get_acceleration_x();
316 float ay = physic.get_acceleration_y();
319 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
323 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
329 if (input.fire == UP) {
330 ax = dirsign * WALK_ACCELERATION_X;
332 if(vx >= MAX_WALK_XM && dirsign > 0) {
335 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
340 ax = dirsign * RUN_ACCELERATION_X;
342 if(vx >= MAX_RUN_XM && dirsign > 0) {
345 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
351 // we can reach WALK_SPEED without any acceleration
352 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
353 vx = dirsign * WALK_SPEED;
356 // changing directions?
357 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
358 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
359 skidding_timer.start(SKID_TIME);
360 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
367 // we get slower when not pressing any keys
369 if(fabs(vx) < WALK_SPEED) {
373 ax = WALK_ACCELERATION_X * 1.5;
375 ax = WALK_ACCELERATION_X * -1.5;
379 // if we're on ice slow down acceleration or deceleration
380 if (isice(base.x, base.y + base.height))
382 /* the acceleration/deceleration rate on ice is inversely proportional to
383 * the current velocity.
386 // increasing 1 will increase acceleration/deceleration rate
387 // decreasing 1 will decrease acceleration/deceleration rate
388 // must stay above zero, though
389 if (ax != 0) ax *= 1 / fabs(vx);
392 physic.set_velocity(vx, vy);
393 physic.set_acceleration(ax, ay);
397 Player::handle_vertical_input()
400 if(input.up == DOWN && can_jump && on_ground())
402 if(duck) { // only jump a little bit when in duck mode {
403 physic.set_velocity_y(3);
405 // jump higher if we are running
406 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
407 physic.set_velocity_y(5.8);
409 physic.set_velocity_y(5.2);
415 butt_jump = true; // player started jumping, enable butt jump
417 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
419 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
421 // Let go of jump key
422 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
425 physic.set_velocity_y(0);
426 butt_jump = false; // jump was not full, disable butt jump
429 /* Do butt jump, in case the player has done the combination
430 (full jump and hold DOWN) */
431 if (input.down == UP && physic.get_velocity_y() == World::current()->get_level()->gravity && butt_jump)
432 butt_jump = false; // in case DOWN is not hold after the full jump, disable it
434 if (input.down == DOWN && butt_jump && on_ground() && size == BIG)
436 if(World::current()->trybreakbrick(base.x, base.y + base.height, false)
437 || World::current()->trybreakbrick(
438 base.x + base.width, base.y + base.height, false)) {
439 // make tux jumping a little bit again after breaking the bricks
440 physic.set_velocity_y(2);
442 // butt_jump = false;
445 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
446 issolid(base.x + 1, base.y + base.height + 64) ||
447 issolid(base.x + base.width - 1, base.y + base.height + 64))
451 && input.old_up == UP)
456 if(on_ground()) /* Make sure jumping is off. */
459 input.old_up = input.up;
463 Player::handle_input()
465 /* Handle horizontal movement: */
466 handle_horizontal_input();
470 if (on_ground() && input.up == UP)
472 handle_vertical_input();
475 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
477 World::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
478 physic.get_velocity_x(), dir);
479 input.old_fire = DOWN;
482 /* tux animations: */
483 if(!frame_timer.check())
485 frame_timer.start(25);
486 if (input.right == UP && input.left == UP)
493 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
494 (global_frame_counter % 4) == 0)
495 frame_main = (frame_main + 1) % 4;
505 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
510 // changing base size confuses collision otherwise
511 old_base = previous_base = base;
513 else if(input.down == UP && size == BIG && duck)
515 // try if we can really unduck
518 // when unducking in air we need some space to do so
519 if(on_ground() || !collision_object_map(base)) {
521 // changing base size confuses collision otherwise
522 old_base = previous_base = base;
524 // undo the ducking changes
541 old_base = previous_base = base;
545 Player::grabdistros()
550 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
551 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
553 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
554 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
558 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
559 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
564 /* Enough distros for a One-up? */
565 if (player_status.distros >= DISTROS_LIFEUP)
567 player_status.distros = player_status.distros - DISTROS_LIFEUP;
568 if(player_status.lives < MAX_LIVES)
569 ++player_status.lives;
570 /*We want to hear the sound even, if MAX_LIVES is reached*/
571 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
576 Player::draw(ViewPort& viewport, int layer)
578 PlayerSprite* sprite;
582 else if (got_power == FIRE_POWER)
584 else if (got_power == ICE_POWER)
589 Vector pos = viewport.world2screen(Vector(base.x, base.y));
591 if(layer == LAYER_OBJECTS + 1) {
592 // Draw arm overlay graphics when Tux is holding something
593 if (holding_something && physic.get_velocity_y() == 0)
596 sprite->grab_right->draw(pos);
598 sprite->grab_left->draw(pos);
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(pos);
608 largetux_star->draw(pos);
614 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
616 if (dying == DYING_SQUISHED)
618 smalltux_gameover->draw(pos);
622 if (duck && size != SMALL)
625 sprite->duck_right->draw(pos);
627 sprite->duck_left->draw(pos);
629 else if (skidding_timer.started())
632 sprite->skid_right->draw(pos);
634 sprite->skid_left->draw(pos);
636 else if (kick_timer.started())
639 sprite->kick_right->draw(pos);
641 sprite->kick_left->draw(pos);
643 else if (physic.get_velocity_y() != 0)
646 sprite->jump_right->draw(pos);
648 sprite->jump_left->draw(pos);
652 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
655 sprite->stand_right->draw(pos);
657 sprite->stand_left->draw(pos);
662 sprite->walk_right->draw(pos);
664 sprite->walk_left->draw(pos);
671 fillrect(base.x - viewport.get_translation().x,
672 base.y - viewport.get_translation().y,
673 base.width, base.height, 75,75,75, 150);
677 Player::collision(const MovingObject& other, int collision_type)
680 (void) collision_type;
681 // will be implemented later
685 Player::collision(void* p_c_object, int c_object)
687 BadGuy* pbad_c = NULL;
688 Trampoline* ptramp_c = NULL;
693 pbad_c = (BadGuy*) p_c_object;
695 /* Hurt player if he touches a badguy */
696 if (!pbad_c->dying && !dying &&
697 !safe_timer.started() &&
698 pbad_c->mode != BadGuy::HELD)
700 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
701 && !holding_something)
703 holding_something = true;
704 pbad_c->mode = BadGuy::HELD;
707 else if (pbad_c->mode == BadGuy::FLAT)
709 // Don't get hurt if we're kicking a flat badguy!
711 else if (pbad_c->mode == BadGuy::KICK)
713 /* Hurt if you get hit by kicked laptop: */
714 if (!invincible_timer.started())
721 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
722 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
723 || pbad_c->kind == BAD_SPIKY))
727 if (!invincible_timer.started())
736 player_status.score_multiplier++;
741 ptramp_c = (Trampoline*) p_c_object;
743 // Pick up trampoline
744 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
746 holding_something = true;
747 ptramp_c->mode = Trampoline::M_HELD;
748 ptramp_c->base.y -= 8;
750 // Set down trampoline
751 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
753 holding_something = false;
754 ptramp_c->mode = Trampoline::M_NORMAL;
755 ptramp_c->base.y += 8;
756 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
759 // ptramp_c->base.x = base.x + base.width+1;
761 // ptramp_c->base.x = base.x - base.width-1;
764 // Don't let tux walk through trampoline
765 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
767 if (physic.get_velocity_x() > 0) // RIGHT
769 physic.set_velocity_x(0);
770 base.x = ptramp_c->base.x - base.width;
772 else if (physic.get_velocity_x() < 0) // LEFT
774 physic.set_velocity_x(0);
775 base.x = ptramp_c->base.x + ptramp_c->base.width;
791 Player::kill(HurtMode mode)
793 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
795 physic.set_velocity_x(0);
797 if (mode == SHRINK && size == BIG)
799 if (got_power != NONE_POWER)
801 got_power = NONE_POWER;
809 safe_timer.start(TUX_SAFE_TIME);
813 physic.enable_gravity(true);
814 physic.set_acceleration(0, 0);
815 physic.set_velocity(0, 7);
816 if(dying != DYING_SQUISHED)
817 --player_status.lives;
818 dying = DYING_SQUISHED;
829 bool Player::is_dead()
832 World::current()->displaymanager.get_viewport().get_translation().x;
834 World::current()->displaymanager.get_viewport().get_translation().y;
835 if(base.y > screen->h + scroll_y || base.y > World::current()->get_level()->height*32 ||
836 base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // can happen in auto-scrolling
842 /* Remove Tux's power ups */
844 Player::remove_powerups()
846 got_power = NONE_POWER;
852 Player::check_bounds(ViewPort& viewport,
853 bool back_scrolling, bool hor_autoscroll)
855 /* Keep tux in bounds: */
857 { // Lock Tux to the size of the level, so that he doesn't fall of
862 /* Keep in-bounds, vertically: */
863 if (base.y > World::current()->get_level()->height * /*TILE_HEIGHT*/ 32)
868 if(base.x < viewport.get_translation().x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled
869 base.x = viewport.get_translation().x;
873 if(base.x == viewport.get_translation().x)
874 if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32)))
877 if(base.x + base.width > viewport.get_translation().x + screen->w)
878 base.x = viewport.get_translation().x + screen->w - base.width;