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);
80 Level* plevel = World::current()->get_level();
82 holding_something = false;
88 got_power = NONE_POWER;
90 base.x = plevel->start_pos_x;
91 base.y = plevel->start_pos_y;
92 previous_base = old_base = base;
105 player_input_init(&input);
107 invincible_timer.init(true);
108 skidding_timer.init(true);
109 safe_timer.init(true);
110 frame_timer.init(true);
111 kick_timer.init(true);
117 Player::key_event(SDLKey key, int state)
119 if(key == keymap.right)
124 else if(key == keymap.left)
129 else if(key == keymap.jump)
134 else if(key == keymap.duck)
139 else if(key == keymap.fire)
151 Player::level_begin()
155 previous_base = old_base = base;
160 player_input_init(&input);
162 invincible_timer.init(true);
163 skidding_timer.init(true);
164 safe_timer.init(true);
165 frame_timer.init(true);
171 Player::action(float elapsed_time)
173 bool jumped_in_solid = false;
175 if (input.fire == UP)
176 holding_something = false;
179 previous_base = base;
181 /* --- HANDLE TUX! --- */
182 if(dying == DYING_NOT)
185 physic.apply(elapsed_time, base.x, base.y);
187 if(dying == DYING_NOT)
189 base_type target = base;
191 collision_swept_object_map(&old_base, &base);
193 if (!invincible_timer.started()
194 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
195 || isspike(base.x, base.y + base.height)
196 || isspike(base.x + base.width, base.y + base.height)))
201 // Don't accelerate Tux if he is running against a wall
202 if (target.x != base.x)
204 physic.set_velocity_x(0);
207 // special exception for cases where we're stuck under tiles after
208 // being ducked. In this case we drift out
209 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
210 && collision_object_map(base))
212 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
213 previous_base = old_base = base;
219 physic.enable_gravity(true);
223 physic.set_velocity_y(0);
224 jumped_in_solid = true;
230 if (physic.get_velocity_y() < 0)
232 base.y = (int)(((int)base.y / 32) * 32);
233 physic.set_velocity_y(0);
236 physic.enable_gravity(false);
237 /* Reset score multiplier (for multi-hits): */
238 if (!invincible_timer.started())
239 player_status.score_multiplier = 1;
244 if (isbrick(base.x, base.y) ||
245 isfullbox(base.x, base.y))
247 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
248 World::current()->trybumpbadguy(base.x, base.y - 64);
250 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
252 bumpbrick(base.x, base.y);
253 World::current()->tryemptybox(base.x, base.y, RIGHT);
256 if (isbrick(base.x+ 31, base.y) ||
257 isfullbox(base.x+ 31, base.y))
259 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
260 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
263 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
265 bumpbrick(base.x+ 31, base.y);
266 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
278 /* Make sure jumping is off. */
284 /* ---- DONE HANDLING TUX! --- */
287 skidding_timer.check();
288 invincible_timer.check();
296 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
297 issolid(base.x + 1, base.y + base.height) ||
298 issolid(base.x + base.width - 1, base.y + base.height) );
302 Player::under_solid()
304 return ( issolid(base.x + base.width / 2, base.y) ||
305 issolid(base.x + 1, base.y) ||
306 issolid(base.x + base.width - 1, base.y) );
310 Player::handle_horizontal_input()
312 float vx = physic.get_velocity_x();
313 float vy = physic.get_velocity_y();
314 float ax = physic.get_acceleration_x();
315 float ay = physic.get_acceleration_y();
318 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
322 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
328 if (input.fire == UP) {
329 ax = dirsign * WALK_ACCELERATION_X;
331 if(vx >= MAX_WALK_XM && dirsign > 0) {
334 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
339 ax = dirsign * RUN_ACCELERATION_X;
341 if(vx >= MAX_RUN_XM && dirsign > 0) {
344 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
350 // we can reach WALK_SPEED without any acceleration
351 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
352 vx = dirsign * WALK_SPEED;
355 // changing directions?
356 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
357 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
358 skidding_timer.start(SKID_TIME);
359 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
366 // we get slower when not pressing any keys
368 if(fabs(vx) < WALK_SPEED) {
372 ax = WALK_ACCELERATION_X * 1.5;
374 ax = WALK_ACCELERATION_X * -1.5;
378 // if we're on ice slow down acceleration or deceleration
379 if (isice(base.x, base.y + base.height))
381 /* the acceleration/deceleration rate on ice is inversely proportional to
382 * the current velocity.
385 // increasing 1 will increase acceleration/deceleration rate
386 // decreasing 1 will decrease acceleration/deceleration rate
387 // must stay above zero, though
388 if (ax != 0) ax *= 1 / fabs(vx);
391 physic.set_velocity(vx, vy);
392 physic.set_acceleration(ax, ay);
396 Player::handle_vertical_input()
399 if(input.up == DOWN && can_jump && on_ground())
401 if(duck) { // only jump a little bit when in duck mode {
402 physic.set_velocity_y(3);
404 // jump higher if we are running
405 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
406 physic.set_velocity_y(5.8);
408 physic.set_velocity_y(5.2);
414 butt_jump = true; // player started jumping, enable butt jump
416 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
418 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
420 // Let go of jump key
421 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
424 physic.set_velocity_y(0);
425 butt_jump = false; // jump was not full, disable butt jump
428 /* Do butt jump, in case the player has done the combination
429 (full jump and hold DOWN) */
430 if (input.down == UP && physic.get_velocity_y() == World::current()->get_level()->gravity && butt_jump)
431 butt_jump = false; // in case DOWN is not hold after the full jump, disable it
433 if (input.down == DOWN && butt_jump && on_ground() && size == BIG)
435 if(World::current()->trybreakbrick(base.x, base.y + base.height, false)
436 || World::current()->trybreakbrick(
437 base.x + base.width, base.y + base.height, false)) {
438 // make tux jumping a little bit again after breaking the bricks
439 physic.set_velocity_y(2);
441 // butt_jump = false;
444 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
445 issolid(base.x + 1, base.y + base.height + 64) ||
446 issolid(base.x + base.width - 1, base.y + base.height + 64))
450 && input.old_up == UP)
455 if(on_ground()) /* Make sure jumping is off. */
458 input.old_up = input.up;
462 Player::handle_input()
464 /* Handle horizontal movement: */
465 handle_horizontal_input();
469 if (on_ground() && input.up == UP)
471 handle_vertical_input();
474 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
476 holding_something = true;
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 )
578 Vector pos = viewport.world2screen(Vector(base.x, base.y));
580 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
582 if (dying == DYING_SQUISHED)
584 smalltux_gameover->draw(pos);
588 PlayerSprite* sprite;
592 else if (got_power == FIRE_POWER)
594 else if (got_power == ICE_POWER)
599 if (duck && size != SMALL)
602 sprite->duck_right->draw(pos);
604 sprite->duck_left->draw(pos);
606 else if (skidding_timer.started())
609 sprite->skid_right->draw(pos);
611 sprite->skid_left->draw(pos);
613 else if (kick_timer.started())
616 sprite->kick_right->draw(pos);
618 sprite->kick_left->draw(pos);
620 else if (physic.get_velocity_y() != 0)
623 sprite->jump_right->draw(pos);
625 sprite->jump_left->draw(pos);
629 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
632 sprite->stand_right->draw(pos);
634 sprite->stand_left->draw(pos);
639 sprite->walk_right->draw(pos);
641 sprite->walk_left->draw(pos);
645 // Draw arm overlay graphics when Tux is holding something
646 if (holding_something && physic.get_velocity_y() == 0)
649 sprite->grab_right->draw(pos);
651 sprite->grab_left->draw(pos);
654 // Draw blinking star overlay
655 if (invincible_timer.started() &&
656 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
658 if (size == SMALL || duck)
659 smalltux_star->draw(pos);
661 largetux_star->draw(pos);
667 fillrect(base.x - scroll_x, base.y - scroll_y,
668 base.width, base.height, 75,75,75, 150);
672 Player::collision(const MovingObject& other, int collision_type)
675 (void) collision_type;
676 // will be implemented later
680 Player::collision(void* p_c_object, int c_object)
682 BadGuy* pbad_c = NULL;
683 Trampoline* ptramp_c = NULL;
688 pbad_c = (BadGuy*) p_c_object;
690 /* Hurt player if he touches a badguy */
691 if (!pbad_c->dying && !dying &&
692 !safe_timer.started() &&
693 pbad_c->mode != BadGuy::HELD)
695 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
696 && !holding_something)
698 holding_something = true;
699 pbad_c->mode = BadGuy::HELD;
702 else if (pbad_c->mode == BadGuy::FLAT)
704 // Don't get hurt if we're kicking a flat badguy!
706 else if (pbad_c->mode == BadGuy::KICK)
708 /* Hurt if you get hit by kicked laptop: */
709 if (!invincible_timer.started())
716 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
717 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
718 || pbad_c->kind == BAD_SPIKY))
722 if (!invincible_timer.started())
731 player_status.score_multiplier++;
736 ptramp_c = (Trampoline*) p_c_object;
738 // Pick up trampoline
739 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
741 holding_something = true;
742 ptramp_c->mode = Trampoline::M_HELD;
743 ptramp_c->base.y -= 8;
745 // Set down trampoline
746 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
748 holding_something = false;
749 ptramp_c->mode = Trampoline::M_NORMAL;
750 ptramp_c->base.y += 8;
751 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
754 // ptramp_c->base.x = base.x + base.width+1;
756 // ptramp_c->base.x = base.x - base.width-1;
759 // Don't let tux walk through trampoline
760 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
762 if (physic.get_velocity_x() > 0) // RIGHT
764 physic.set_velocity_x(0);
765 base.x = ptramp_c->base.x - base.width;
767 else if (physic.get_velocity_x() < 0) // LEFT
769 physic.set_velocity_x(0);
770 base.x = ptramp_c->base.x + ptramp_c->base.width;
786 Player::kill(HurtMode mode)
788 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
790 physic.set_velocity_x(0);
792 if (mode == SHRINK && size == BIG)
794 if (got_power != NONE_POWER)
796 got_power = NONE_POWER;
804 safe_timer.start(TUX_SAFE_TIME);
808 physic.enable_gravity(true);
809 physic.set_acceleration(0, 0);
810 physic.set_velocity(0, 7);
811 if(dying != DYING_SQUISHED)
812 --player_status.lives;
813 dying = DYING_SQUISHED;
824 bool Player::is_dead()
826 if(base.y > screen->h + scroll_y || base.y > World::current()->get_level()->height*32 ||
827 base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // can happen in auto-scrolling
833 /* Remove Tux's power ups */
835 Player::remove_powerups()
837 got_power = NONE_POWER;
843 Player::check_bounds(bool back_scrolling, bool hor_autoscroll)
845 /* Keep tux in bounds: */
847 { // Lock Tux to the size of the level, so that he doesn't fall of
852 /* Keep in-bounds, vertically: */
853 if (base.y > World::current()->get_level()->height * /*TILE_HEIGHT*/ 32)
858 if(base.x < scroll_x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled
863 if(base.x == scroll_x)
864 if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32)))
867 if(base.x + base.width > scroll_x + screen->w)
868 base.x = scroll_x + screen->w - base.width;