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
33 // animation times (in ms):
34 #define SHOOTING_TIME 320
38 Sprite* smalltux_gameover;
39 Sprite* smalltux_star;
40 Sprite* largetux_star;
42 PlayerSprite smalltux;
43 PlayerSprite largetux;
49 PlayerKeymap::PlayerKeymap()
51 keymap.jump = SDLK_UP;
52 keymap.duck = SDLK_DOWN;
53 keymap.left = SDLK_LEFT;
54 keymap.right = SDLK_RIGHT;
55 keymap.fire = SDLK_LCTRL;
58 void player_input_init(player_input_type* pplayer_input)
60 pplayer_input->down = UP;
61 pplayer_input->fire = UP;
62 pplayer_input->left = UP;
63 pplayer_input->old_fire = UP;
64 pplayer_input->right = UP;
65 pplayer_input->up = UP;
66 pplayer_input->old_up = UP;
69 Player::Player(DisplayManager& display_manager)
71 display_manager.add_drawable(this, LAYER_OBJECTS-1); // for tux himself
72 display_manager.add_drawable(this, LAYER_OBJECTS+1); // for the arm
83 Level* plevel = World::current()->get_level();
85 holding_something = false;
91 got_power = NONE_POWER;
93 base.x = plevel->start_pos_x;
94 base.y = plevel->start_pos_y;
95 previous_base = old_base = base;
108 player_input_init(&input);
110 invincible_timer.init(true);
111 skidding_timer.init(true);
112 safe_timer.init(true);
113 frame_timer.init(true);
114 kick_timer.init(true);
115 shooting_timer.init(true);
121 Player::key_event(SDLKey key, int state)
123 if(key == keymap.right)
128 else if(key == keymap.left)
133 else if(key == keymap.jump)
138 else if(key == keymap.duck)
143 else if(key == keymap.fire)
155 Player::level_begin()
159 previous_base = old_base = base;
164 player_input_init(&input);
166 invincible_timer.init(true);
167 skidding_timer.init(true);
168 safe_timer.init(true);
169 frame_timer.init(true);
175 Player::action(float elapsed_time)
177 bool jumped_in_solid = false;
179 if (input.fire == UP)
180 holding_something = false;
183 previous_base = base;
185 /* --- HANDLE TUX! --- */
186 if(dying == DYING_NOT)
189 physic.apply(elapsed_time, base.x, base.y);
191 if(dying == DYING_NOT)
193 base_type target = base;
195 collision_swept_object_map(&old_base, &base);
197 if (!invincible_timer.started()
198 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
199 || isspike(base.x, base.y + base.height)
200 || isspike(base.x + base.width, base.y + base.height)))
205 // Don't accelerate Tux if he is running against a wall
206 if (target.x != base.x)
208 physic.set_velocity_x(0);
211 // special exception for cases where we're stuck under tiles after
212 // being ducked. In this case we drift out
213 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
214 && collision_object_map(base))
216 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
217 previous_base = old_base = base;
223 physic.enable_gravity(true);
227 physic.set_velocity_y(0);
228 jumped_in_solid = true;
234 if (physic.get_velocity_y() < 0)
236 base.y = (int)(((int)base.y / 32) * 32);
237 physic.set_velocity_y(0);
240 physic.enable_gravity(false);
241 /* Reset score multiplier (for multi-hits): */
242 if (!invincible_timer.started())
243 player_status.score_multiplier = 1;
248 if (isbrick(base.x, base.y) ||
249 isfullbox(base.x, base.y))
251 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
252 World::current()->trybumpbadguy(base.x, base.y - 64);
254 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
256 bumpbrick(base.x, base.y);
257 World::current()->tryemptybox(base.x, base.y, RIGHT);
260 if (isbrick(base.x+ 31, base.y) ||
261 isfullbox(base.x+ 31, base.y))
263 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
264 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
267 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
269 bumpbrick(base.x+ 31, base.y);
270 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
282 /* Make sure jumping is off. */
288 /* ---- DONE HANDLING TUX! --- */
291 skidding_timer.check();
292 invincible_timer.check();
300 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
301 issolid(base.x + 1, base.y + base.height) ||
302 issolid(base.x + base.width - 1, base.y + base.height) );
306 Player::under_solid()
308 return ( issolid(base.x + base.width / 2, base.y) ||
309 issolid(base.x + 1, base.y) ||
310 issolid(base.x + base.width - 1, base.y) );
314 Player::handle_horizontal_input()
316 float vx = physic.get_velocity_x();
317 float vy = physic.get_velocity_y();
318 float ax = physic.get_acceleration_x();
319 float ay = physic.get_acceleration_y();
322 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
326 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
332 if (input.fire == UP) {
333 ax = dirsign * WALK_ACCELERATION_X;
335 if(vx >= MAX_WALK_XM && dirsign > 0) {
338 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
343 ax = dirsign * RUN_ACCELERATION_X;
345 if(vx >= MAX_RUN_XM && dirsign > 0) {
348 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
354 // we can reach WALK_SPEED without any acceleration
355 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
356 vx = dirsign * WALK_SPEED;
359 // changing directions?
360 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
361 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
362 skidding_timer.start(SKID_TIME);
363 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
370 // we get slower when not pressing any keys
372 if(fabs(vx) < WALK_SPEED) {
376 ax = WALK_ACCELERATION_X * 1.5;
378 ax = WALK_ACCELERATION_X * -1.5;
382 // if we're on ice slow down acceleration or deceleration
383 if (isice(base.x, base.y + base.height))
385 /* the acceleration/deceleration rate on ice is inversely proportional to
386 * the current velocity.
389 // increasing 1 will increase acceleration/deceleration rate
390 // decreasing 1 will decrease acceleration/deceleration rate
391 // must stay above zero, though
392 if (ax != 0) ax *= 1 / fabs(vx);
395 physic.set_velocity(vx, vy);
396 physic.set_acceleration(ax, ay);
400 Player::handle_vertical_input()
403 if(input.up == DOWN && can_jump && on_ground())
405 if(duck) { // only jump a little bit when in duck mode {
406 physic.set_velocity_y(3);
408 // jump higher if we are running
409 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
410 physic.set_velocity_y(5.8);
412 physic.set_velocity_y(5.2);
418 butt_jump = true; // player started jumping, enable butt jump
420 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
422 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
424 // Let go of jump key
425 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
428 physic.set_velocity_y(0);
429 butt_jump = false; // jump was not full, disable butt jump
432 /* Do butt jump, in case the player has done the combination
433 (full jump and hold DOWN) */
434 if (input.down == UP && physic.get_velocity_y() == World::current()->get_level()->gravity && butt_jump)
435 butt_jump = false; // in case DOWN is not hold after the full jump, disable it
437 if (input.down == DOWN && butt_jump && on_ground() && size == BIG)
439 if(World::current()->trybreakbrick(base.x, base.y + base.height, false)
440 || World::current()->trybreakbrick(
441 base.x + base.width, base.y + base.height, false)) {
442 // make tux jumping a little bit again after breaking the bricks
443 physic.set_velocity_y(2);
445 // butt_jump = false;
448 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
449 issolid(base.x + 1, base.y + base.height + 64) ||
450 issolid(base.x + base.width - 1, base.y + base.height + 64))
454 && input.old_up == UP)
459 if(on_ground()) /* Make sure jumping is off. */
462 input.old_up = input.up;
466 Player::handle_input()
468 /* Handle horizontal movement: */
469 handle_horizontal_input();
473 if (on_ground() && input.up == UP)
475 handle_vertical_input();
478 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
480 if(World::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
481 physic.get_velocity_x(), dir))
482 shooting_timer.start(SHOOTING_TIME);
483 input.old_fire = DOWN;
486 /* tux animations: */
487 if(!frame_timer.check())
489 frame_timer.start(25);
490 if (input.right == UP && input.left == UP)
497 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
498 (global_frame_counter % 4) == 0)
499 frame_main = (frame_main + 1) % 4;
509 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
514 // changing base size confuses collision otherwise
515 old_base = previous_base = base;
517 else if(input.down == UP && size == BIG && duck)
519 // try if we can really unduck
522 // when unducking in air we need some space to do so
523 if(on_ground() || !collision_object_map(base)) {
525 // changing base size confuses collision otherwise
526 old_base = previous_base = base;
528 // undo the ducking changes
545 old_base = previous_base = base;
549 Player::grabdistros()
554 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
555 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
557 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
558 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
562 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
563 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
568 /* Enough distros for a One-up? */
569 if (player_status.distros >= DISTROS_LIFEUP)
571 player_status.distros = player_status.distros - DISTROS_LIFEUP;
572 if(player_status.lives < MAX_LIVES)
573 ++player_status.lives;
574 /*We want to hear the sound even, if MAX_LIVES is reached*/
575 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
580 Player::draw(ViewPort& viewport, int layer)
582 PlayerSprite* sprite;
586 else if (got_power == FIRE_POWER)
588 else if (got_power == ICE_POWER)
593 Vector pos = viewport.world2screen(Vector(base.x, base.y));
595 if(layer == LAYER_OBJECTS + 1) {
596 // Draw arm overlay graphics when Tux is holding something
597 if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check())
600 sprite->grab_right->draw(pos);
602 sprite->grab_left->draw(pos);
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(pos);
612 largetux_star->draw(pos);
618 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
620 if (dying == DYING_SQUISHED)
622 smalltux_gameover->draw(pos);
626 if (duck && size != SMALL)
629 sprite->duck_right->draw(pos);
631 sprite->duck_left->draw(pos);
633 else if (skidding_timer.started())
636 sprite->skid_right->draw(pos);
638 sprite->skid_left->draw(pos);
640 else if (kick_timer.started())
643 sprite->kick_right->draw(pos);
645 sprite->kick_left->draw(pos);
647 else if (physic.get_velocity_y() != 0)
650 sprite->jump_right->draw(pos);
652 sprite->jump_left->draw(pos);
656 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
659 sprite->stand_right->draw(pos);
661 sprite->stand_left->draw(pos);
666 sprite->walk_right->draw(pos);
668 sprite->walk_left->draw(pos);
675 fillrect(base.x - viewport.get_translation().x,
676 base.y - viewport.get_translation().y,
677 base.width, base.height, 75,75,75, 150);
681 Player::collision(const MovingObject& other, int collision_type)
684 (void) collision_type;
685 // will be implemented later
689 Player::collision(void* p_c_object, int c_object)
691 BadGuy* pbad_c = NULL;
692 Trampoline* ptramp_c = NULL;
697 pbad_c = (BadGuy*) p_c_object;
699 /* Hurt player if he touches a badguy */
700 if (!pbad_c->dying && !dying &&
701 !safe_timer.started() &&
702 pbad_c->mode != BadGuy::HELD)
704 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
705 && !holding_something)
707 holding_something = true;
708 pbad_c->mode = BadGuy::HELD;
711 else if (pbad_c->mode == BadGuy::FLAT)
713 // Don't get hurt if we're kicking a flat badguy!
715 else if (pbad_c->mode == BadGuy::KICK)
717 /* Hurt if you get hit by kicked laptop: */
718 if (!invincible_timer.started())
725 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
726 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
727 || pbad_c->kind == BAD_SPIKY))
731 if (!invincible_timer.started())
740 player_status.score_multiplier++;
745 ptramp_c = (Trampoline*) p_c_object;
747 // Pick up trampoline
748 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
750 holding_something = true;
751 ptramp_c->mode = Trampoline::M_HELD;
752 ptramp_c->base.y -= 8;
754 // Set down trampoline
755 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
757 holding_something = false;
758 ptramp_c->mode = Trampoline::M_NORMAL;
759 ptramp_c->base.y += 8;
760 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
763 // ptramp_c->base.x = base.x + base.width+1;
765 // ptramp_c->base.x = base.x - base.width-1;
768 // Don't let tux walk through trampoline
769 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
771 if (physic.get_velocity_x() > 0) // RIGHT
773 physic.set_velocity_x(0);
774 base.x = ptramp_c->base.x - base.width;
776 else if (physic.get_velocity_x() < 0) // LEFT
778 physic.set_velocity_x(0);
779 base.x = ptramp_c->base.x + ptramp_c->base.width;
795 Player::kill(HurtMode mode)
797 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
799 physic.set_velocity_x(0);
801 if (mode == SHRINK && size == BIG)
803 if (got_power != NONE_POWER)
805 got_power = NONE_POWER;
813 safe_timer.start(TUX_SAFE_TIME);
817 physic.enable_gravity(true);
818 physic.set_acceleration(0, 0);
819 physic.set_velocity(0, 7);
820 if(dying != DYING_SQUISHED)
821 --player_status.lives;
822 dying = DYING_SQUISHED;
833 bool Player::is_dead()
836 World::current()->displaymanager.get_viewport().get_translation().x;
838 World::current()->displaymanager.get_viewport().get_translation().y;
839 if(base.y > screen->h + scroll_y || base.y > World::current()->get_level()->height*32 ||
840 base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // can happen in auto-scrolling
846 /* Remove Tux's power ups */
848 Player::remove_powerups()
850 got_power = NONE_POWER;
856 Player::check_bounds(ViewPort& viewport,
857 bool back_scrolling, bool hor_autoscroll)
859 /* Keep tux in bounds: */
861 { // Lock Tux to the size of the level, so that he doesn't fall of
866 /* Keep in-bounds, vertically: */
867 if (base.y > World::current()->get_level()->height * /*TILE_HEIGHT*/ 32)
872 if(base.x < viewport.get_translation().x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled
873 base.x = viewport.get_translation().x;
877 if(base.x == viewport.get_translation().x)
878 if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32)))
881 if(base.x + base.width > viewport.get_translation().x + screen->w)
882 base.x = viewport.get_translation().x + screen->w - base.width;