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 // behavior definitions:
33 #define TILES_FOR_BUTTJUMP 3
34 // animation times (in ms):
35 #define SHOOTING_TIME 320
37 #define AUTOSCROLL_DEAD_INTERVAL 300
41 Sprite* smalltux_gameover;
42 Sprite* smalltux_star;
43 Sprite* largetux_star;
45 PlayerSprite smalltux;
46 PlayerSprite largetux;
52 PlayerKeymap::PlayerKeymap()
54 keymap.jump = SDLK_UP;
55 keymap.duck = SDLK_DOWN;
56 keymap.left = SDLK_LEFT;
57 keymap.right = SDLK_RIGHT;
58 keymap.fire = SDLK_LCTRL;
61 void player_input_init(player_input_type* pplayer_input)
63 pplayer_input->down = UP;
64 pplayer_input->fire = UP;
65 pplayer_input->left = UP;
66 pplayer_input->old_fire = UP;
67 pplayer_input->right = UP;
68 pplayer_input->up = UP;
69 pplayer_input->old_up = UP;
72 Player::Player(DisplayManager& display_manager)
74 display_manager.add_drawable(this, LAYER_OBJECTS-1); // for tux himself
75 display_manager.add_drawable(this, LAYER_OBJECTS+1); // for the arm
86 Level* plevel = World::current()->get_level();
88 holding_something = false;
94 got_power = NONE_POWER;
96 base.x = plevel->start_pos_x;
97 base.y = plevel->start_pos_y;
98 previous_base = old_base = base;
111 player_input_init(&input);
113 invincible_timer.init(true);
114 skidding_timer.init(true);
115 safe_timer.init(true);
116 frame_timer.init(true);
117 kick_timer.init(true);
118 shooting_timer.init(true);
124 Player::key_event(SDLKey key, int state)
126 if(key == keymap.right)
131 else if(key == keymap.left)
136 else if(key == keymap.jump)
141 else if(key == keymap.duck)
146 else if(key == keymap.fire)
158 Player::level_begin()
162 previous_base = old_base = base;
167 player_input_init(&input);
169 invincible_timer.init(true);
170 skidding_timer.init(true);
171 safe_timer.init(true);
172 frame_timer.init(true);
178 Player::action(float elapsed_time)
180 bool jumped_in_solid = false;
182 if (input.fire == UP)
183 holding_something = false;
186 previous_base = base;
188 /* --- HANDLE TUX! --- */
189 if(dying == DYING_NOT)
192 physic.apply(elapsed_time, base.x, base.y);
194 if(dying == DYING_NOT)
196 base_type target = base;
198 collision_swept_object_map(&old_base, &base);
200 if (!invincible_timer.started()
201 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
202 || isspike(base.x, base.y + base.height)
203 || isspike(base.x + base.width, base.y + base.height)))
208 // Don't accelerate Tux if he is running against a wall
209 if (target.x != base.x)
211 physic.set_velocity_x(0);
214 // special exception for cases where we're stuck under tiles after
215 // being ducked. In this case we drift out
216 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
217 && collision_object_map(base))
219 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
220 previous_base = old_base = base;
226 physic.enable_gravity(true);
230 physic.set_velocity_y(0);
231 jumped_in_solid = true;
237 if (physic.get_velocity_y() < 0)
239 base.y = (int)(((int)base.y / 32) * 32);
240 physic.set_velocity_y(0);
243 physic.enable_gravity(false);
244 /* Reset score multiplier (for multi-hits): */
245 if (!invincible_timer.started())
246 player_status.score_multiplier = 1;
251 if (isbrick(base.x, base.y) ||
252 isfullbox(base.x, base.y))
254 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
255 World::current()->trybumpbadguy(base.x, base.y - 64);
257 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
259 bumpbrick(base.x, base.y);
260 World::current()->tryemptybox(base.x, base.y, RIGHT);
263 if (isbrick(base.x+ 31, base.y) ||
264 isfullbox(base.x+ 31, base.y))
266 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
267 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
270 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
272 bumpbrick(base.x+ 31, base.y);
273 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
285 /* Make sure jumping is off. */
291 /* ---- DONE HANDLING TUX! --- */
294 skidding_timer.check();
295 invincible_timer.check();
303 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
304 issolid(base.x + 1, base.y + base.height) ||
305 issolid(base.x + base.width - 1, base.y + base.height) );
309 Player::under_solid()
311 return ( issolid(base.x + base.width / 2, base.y) ||
312 issolid(base.x + 1, base.y) ||
313 issolid(base.x + base.width - 1, base.y) );
317 Player::tiles_on_air(int tiles)
319 for(int t = 0; t != tiles; t++)
321 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
322 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
323 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
330 Player::handle_horizontal_input()
332 float vx = physic.get_velocity_x();
333 float vy = physic.get_velocity_y();
334 float ax = physic.get_acceleration_x();
335 float ay = physic.get_acceleration_y();
338 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
342 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
348 if (input.fire == UP) {
349 ax = dirsign * WALK_ACCELERATION_X;
351 if(vx >= MAX_WALK_XM && dirsign > 0) {
354 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
359 ax = dirsign * RUN_ACCELERATION_X;
361 if(vx >= MAX_RUN_XM && dirsign > 0) {
364 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
370 // we can reach WALK_SPEED without any acceleration
371 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
372 vx = dirsign * WALK_SPEED;
375 // changing directions?
376 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
377 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
378 skidding_timer.start(SKID_TIME);
379 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
386 // we get slower when not pressing any keys
388 if(fabs(vx) < WALK_SPEED) {
392 ax = WALK_ACCELERATION_X * 1.5;
394 ax = WALK_ACCELERATION_X * -1.5;
398 // if we're on ice slow down acceleration or deceleration
399 if (isice(base.x, base.y + base.height))
401 /* the acceleration/deceleration rate on ice is inversely proportional to
402 * the current velocity.
405 // increasing 1 will increase acceleration/deceleration rate
406 // decreasing 1 will decrease acceleration/deceleration rate
407 // must stay above zero, though
408 if (ax != 0) ax *= 1 / fabs(vx);
411 physic.set_velocity(vx, vy);
412 physic.set_acceleration(ax, ay);
416 Player::handle_vertical_input()
419 if(input.up == DOWN && can_jump && on_ground())
421 if(duck) { // only jump a little bit when in duck mode {
422 physic.set_velocity_y(3);
424 // jump higher if we are running
425 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
426 physic.set_velocity_y(5.8);
428 physic.set_velocity_y(5.2);
435 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
437 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
439 // Let go of jump key
440 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
443 physic.set_velocity_y(0);
446 /* In case the player has pressed Down while in a certain range of air,
447 enable butt jump action */
448 if (input.down == DOWN && !butt_jump)
449 if(tiles_on_air(TILES_FOR_BUTTJUMP))
452 /* When Down is not held anymore, disable butt jump */
453 if(butt_jump && input.down == UP)
456 if (butt_jump && on_ground() && size == BIG)
458 if(World::current()->trybreakbrick(base.x, base.y + base.height, false)
459 || World::current()->trybreakbrick(
460 base.x + base.width, base.y + base.height, false)) {
461 // make tux jumping a little bit again after breaking the bricks
462 physic.set_velocity_y(2);
464 // butt_jump = false; // comment this, in case you won't to disable the continued use of buttjump
467 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
468 issolid(base.x + 1, base.y + base.height + 64) ||
469 issolid(base.x + base.width - 1, base.y + base.height + 64))
473 && input.old_up == UP)
478 if(on_ground()) /* Make sure jumping is off. */
481 input.old_up = input.up;
485 Player::handle_input()
487 /* Handle horizontal movement: */
488 handle_horizontal_input();
492 if (on_ground() && input.up == UP)
494 handle_vertical_input();
497 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
499 if(World::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
500 physic.get_velocity_x(), dir))
501 shooting_timer.start(SHOOTING_TIME);
502 input.old_fire = DOWN;
505 /* tux animations: */
506 if(!frame_timer.check())
508 frame_timer.start(25);
509 if (input.right == UP && input.left == UP)
516 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
517 (global_frame_counter % 4) == 0)
518 frame_main = (frame_main + 1) % 4;
528 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
533 // changing base size confuses collision otherwise
534 old_base = previous_base = base;
536 else if(input.down == UP && size == BIG && duck)
538 // try if we can really unduck
541 // when unducking in air we need some space to do so
542 if(on_ground() || !collision_object_map(base)) {
544 // changing base size confuses collision otherwise
545 old_base = previous_base = base;
547 // undo the ducking changes
564 old_base = previous_base = base;
568 Player::grabdistros()
573 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
574 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
576 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
577 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
581 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
582 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
587 /* Enough distros for a One-up? */
588 if (player_status.distros >= DISTROS_LIFEUP)
590 player_status.distros = player_status.distros - DISTROS_LIFEUP;
591 if(player_status.lives < MAX_LIVES)
592 ++player_status.lives;
593 /*We want to hear the sound even, if MAX_LIVES is reached*/
594 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
599 Player::draw(ViewPort& viewport, int layer)
601 PlayerSprite* sprite;
605 else if (got_power == FIRE_POWER)
607 else if (got_power == ICE_POWER)
612 Vector pos = viewport.world2screen(Vector(base.x, base.y));
614 if(layer == LAYER_OBJECTS + 1) {
615 // Draw arm overlay graphics when Tux is holding something
616 if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check())
619 sprite->grab_right->draw(pos);
621 sprite->grab_left->draw(pos);
624 // Draw blinking star overlay
625 if (invincible_timer.started() &&
626 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
628 if (size == SMALL || duck)
629 smalltux_star->draw(pos);
631 largetux_star->draw(pos);
637 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
639 if (dying == DYING_SQUISHED)
641 smalltux_gameover->draw(pos);
645 if (duck && size != SMALL)
648 sprite->duck_right->draw(pos);
650 sprite->duck_left->draw(pos);
652 else if (skidding_timer.started())
655 sprite->skid_right->draw(pos);
657 sprite->skid_left->draw(pos);
659 else if (kick_timer.started())
662 sprite->kick_right->draw(pos);
664 sprite->kick_left->draw(pos);
666 else if (physic.get_velocity_y() != 0)
669 sprite->jump_right->draw(pos);
671 sprite->jump_left->draw(pos);
675 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
678 sprite->stand_right->draw(pos);
680 sprite->stand_left->draw(pos);
685 sprite->walk_right->draw(pos);
687 sprite->walk_left->draw(pos);
694 fillrect(base.x - viewport.get_translation().x,
695 base.y - viewport.get_translation().y,
696 base.width, base.height, 75,75,75, 150);
700 Player::collision(const MovingObject& other, int collision_type)
703 (void) collision_type;
704 // will be implemented later
708 Player::collision(void* p_c_object, int c_object)
710 BadGuy* pbad_c = NULL;
711 Trampoline* ptramp_c = NULL;
712 FlyingPlatform* pplatform_c = NULL;
717 pbad_c = (BadGuy*) p_c_object;
719 /* Hurt player if he touches a badguy */
720 if (!pbad_c->dying && !dying &&
721 !safe_timer.started() &&
722 pbad_c->mode != BadGuy::HELD)
724 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
725 && !holding_something)
727 holding_something = true;
728 pbad_c->mode = BadGuy::HELD;
731 else if (pbad_c->mode == BadGuy::FLAT)
733 // Don't get hurt if we're kicking a flat badguy!
735 else if (pbad_c->mode == BadGuy::KICK)
737 /* Hurt if you get hit by kicked laptop: */
738 if (!invincible_timer.started())
745 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
746 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
747 || pbad_c->kind == BAD_SPIKY))
751 if (!invincible_timer.started())
760 player_status.score_multiplier++;
765 ptramp_c = (Trampoline*) p_c_object;
767 // Pick up trampoline
768 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
770 holding_something = true;
771 ptramp_c->mode = Trampoline::M_HELD;
772 ptramp_c->base.y -= 8;
774 // Set down trampoline
775 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
777 holding_something = false;
778 ptramp_c->mode = Trampoline::M_NORMAL;
779 ptramp_c->base.y += 8;
780 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
783 // ptramp_c->base.x = base.x + base.width+1;
785 // ptramp_c->base.x = base.x - base.width-1;
788 // Don't let tux walk through trampoline
789 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
791 if (physic.get_velocity_x() > 0) // RIGHT
793 physic.set_velocity_x(0);
794 base.x = ptramp_c->base.x - base.width;
796 else if (physic.get_velocity_x() < 0) // LEFT
798 physic.set_velocity_x(0);
799 base.x = ptramp_c->base.x + ptramp_c->base.width;
804 case CO_FLYING_PLATFORM:
805 pplatform_c = (FlyingPlatform*) p_c_object;
807 base.y = pplatform_c->base.y - base.height;
819 Player::kill(HurtMode mode)
821 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
823 physic.set_velocity_x(0);
825 if (mode == SHRINK && size == BIG)
827 if (got_power != NONE_POWER)
829 got_power = NONE_POWER;
837 safe_timer.start(TUX_SAFE_TIME);
841 physic.enable_gravity(true);
842 physic.set_acceleration(0, 0);
843 physic.set_velocity(0, 7);
844 if(dying != DYING_SQUISHED)
845 --player_status.lives;
846 dying = DYING_SQUISHED;
857 bool Player::is_dead()
860 World::current()->displaymanager.get_viewport().get_translation().x;
862 World::current()->displaymanager.get_viewport().get_translation().y;
863 if(base.y > screen->h + scroll_y || base.y > World::current()->get_level()->height*32 ||
864 base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // can happen in auto-scrolling
870 /* Remove Tux's power ups */
872 Player::remove_powerups()
874 got_power = NONE_POWER;
880 Player::check_bounds(ViewPort& viewport,
881 bool back_scrolling, bool hor_autoscroll)
883 /* Keep tux in bounds: */
885 { // Lock Tux to the size of the level, so that he doesn't fall of
890 /* Keep in-bounds, vertically: */
891 if (base.y > World::current()->get_level()->height * /*TILE_HEIGHT*/ 32)
896 if(base.x < viewport.get_translation().x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled
897 base.x = viewport.get_translation().x;
901 if(base.x == viewport.get_translation().x)
902 if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32)))
905 if(base.x + base.width > viewport.get_translation().x + screen->w)
906 base.x = viewport.get_translation().x + screen->w - base.width;