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 holding_something = false;
92 got_power = NONE_POWER;
96 previous_base = old_base = base;
104 fall_mode = ON_GROUND;
112 player_input_init(&input);
114 invincible_timer.init(true);
115 skidding_timer.init(true);
116 safe_timer.init(true);
117 frame_timer.init(true);
118 kick_timer.init(true);
119 shooting_timer.init(true);
125 Player::key_event(SDLKey key, int state)
127 if(key == keymap.right)
132 else if(key == keymap.left)
137 else if(key == keymap.jump)
142 else if(key == keymap.duck)
147 else if(key == keymap.fire)
159 Player::level_begin()
163 previous_base = old_base = base;
168 player_input_init(&input);
170 invincible_timer.init(true);
171 skidding_timer.init(true);
172 safe_timer.init(true);
173 frame_timer.init(true);
179 Player::action(float elapsed_time)
181 bool jumped_in_solid = false;
183 if(dying && !dying_timer.check()) {
188 if (input.fire == UP)
189 holding_something = false;
192 previous_base = base;
194 /* --- HANDLE TUX! --- */
195 if(dying == DYING_NOT)
198 physic.apply(elapsed_time, base.x, base.y);
200 if(dying == DYING_NOT)
202 base_type target = base;
204 collision_swept_object_map(&old_base, &base);
206 if ((!invincible_timer.started() && !safe_timer.started())
207 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
208 || isspike(base.x, base.y + base.height)
209 || isspike(base.x + base.width, base.y + base.height)))
214 // Don't accelerate Tux if he is running against a wall
215 if (target.x != base.x)
217 physic.set_velocity_x(0);
220 // special exception for cases where we're stuck under tiles after
221 // being ducked. In this case we drift out
222 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
223 && collision_object_map(base))
225 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
226 previous_base = old_base = base;
232 physic.enable_gravity(true);
236 physic.set_velocity_y(0);
237 jumped_in_solid = true;
243 if (physic.get_velocity_y() < 0)
245 base.y = (int)(((int)base.y / 32) * 32);
246 physic.set_velocity_y(0);
249 physic.enable_gravity(false);
250 /* Reset score multiplier (for multi-hits): */
251 if (!invincible_timer.started())
252 player_status.score_multiplier = 1;
257 if (isbrick(base.x, base.y) ||
258 isfullbox(base.x, base.y))
260 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
261 World::current()->trybumpbadguy(base.x, base.y - 64);
263 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
265 bumpbrick(base.x, base.y);
266 World::current()->tryemptybox(base.x, base.y, RIGHT);
269 if (isbrick(base.x+ 31, base.y) ||
270 isfullbox(base.x+ 31, base.y))
272 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
273 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
276 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
278 bumpbrick(base.x+ 31, base.y);
279 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
291 /* Make sure jumping is off. */
297 /* ---- DONE HANDLING TUX! --- */
300 skidding_timer.check();
301 invincible_timer.check();
309 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
310 issolid(base.x + 1, base.y + base.height) ||
311 issolid(base.x + base.width - 1, base.y + base.height) );
315 Player::under_solid()
317 return ( issolid(base.x + base.width / 2, base.y) ||
318 issolid(base.x + 1, base.y) ||
319 issolid(base.x + base.width - 1, base.y) );
323 Player::tiles_on_air(int tiles)
325 for(int t = 0; t != tiles; t++)
327 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
328 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
329 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
336 Player::handle_horizontal_input()
338 float vx = physic.get_velocity_x();
339 float vy = physic.get_velocity_y();
340 float ax = physic.get_acceleration_x();
341 float ay = physic.get_acceleration_y();
344 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
348 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
354 if (input.fire == UP) {
355 ax = dirsign * WALK_ACCELERATION_X;
357 if(vx >= MAX_WALK_XM && dirsign > 0) {
360 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
365 ax = dirsign * RUN_ACCELERATION_X;
367 if(vx >= MAX_RUN_XM && dirsign > 0) {
370 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
376 // we can reach WALK_SPEED without any acceleration
377 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
378 vx = dirsign * WALK_SPEED;
381 // changing directions?
382 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
383 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
384 skidding_timer.start(SKID_TIME);
385 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
392 // we get slower when not pressing any keys
394 if(fabs(vx) < WALK_SPEED) {
398 ax = WALK_ACCELERATION_X * 1.5;
400 ax = WALK_ACCELERATION_X * -1.5;
404 // if we're on ice slow down acceleration or deceleration
405 if (isice(base.x, base.y + base.height))
407 /* the acceleration/deceleration rate on ice is inversely proportional to
408 * the current velocity.
411 // increasing 1 will increase acceleration/deceleration rate
412 // decreasing 1 will decrease acceleration/deceleration rate
413 // must stay above zero, though
414 if (ax != 0) ax *= 1 / fabs(vx);
417 physic.set_velocity(vx, vy);
418 physic.set_acceleration(ax, ay);
422 Player::handle_vertical_input()
426 fall_mode = ON_GROUND;
427 last_ground_y = base.y;
429 if(base.y > last_ground_y)
431 else if(fall_mode == ON_GROUND)
436 if(input.up == DOWN && can_jump && on_ground())
438 if(duck) { // only jump a little bit when in duck mode {
439 physic.set_velocity_y(3);
441 // jump higher if we are running
442 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
443 physic.set_velocity_y(5.8);
445 physic.set_velocity_y(5.2);
452 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
454 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
456 // Let go of jump key
457 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
460 physic.set_velocity_y(0);
463 /* In case the player has pressed Down while in a certain range of air,
464 enable butt jump action */
465 if (input.down == DOWN && !butt_jump)
466 if(tiles_on_air(TILES_FOR_BUTTJUMP))
469 /* When Down is not held anymore, disable butt jump */
470 if(butt_jump && input.down == UP)
473 if (butt_jump && on_ground() && size == BIG)
475 if(World::current()->trybreakbrick(base.x, base.y + base.height, false)
476 || World::current()->trybreakbrick(
477 base.x + base.width, base.y + base.height, false)) {
478 // make tux jumping a little bit again after breaking the bricks
479 physic.set_velocity_y(2);
481 // butt_jump = false; // comment this, in case you won't to disable the continued use of buttjump
484 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
485 issolid(base.x + 1, base.y + base.height + 64) ||
486 issolid(base.x + base.width - 1, base.y + base.height + 64))
490 && input.old_up == UP)
495 if(on_ground()) /* Make sure jumping is off. */
498 input.old_up = input.up;
502 Player::handle_input()
504 /* Handle horizontal movement: */
505 handle_horizontal_input();
509 if (on_ground() && input.up == UP)
511 handle_vertical_input();
514 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
516 if(World::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
517 physic.get_velocity_x(), dir))
518 shooting_timer.start(SHOOTING_TIME);
519 input.old_fire = DOWN;
522 /* tux animations: */
523 if(!frame_timer.check())
525 frame_timer.start(25);
526 if (input.right == UP && input.left == UP)
533 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
534 (global_frame_counter % 4) == 0)
535 frame_main = (frame_main + 1) % 4;
545 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
550 // changing base size confuses collision otherwise
551 old_base = previous_base = base;
553 else if(input.down == UP && size == BIG && duck)
555 // try if we can really unduck
558 // when unducking in air we need some space to do so
559 if(on_ground() || !collision_object_map(base)) {
561 // changing base size confuses collision otherwise
562 old_base = previous_base = base;
564 // undo the ducking changes
581 old_base = previous_base = base;
585 Player::grabdistros()
590 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
591 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
593 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
594 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
598 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
599 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
604 /* Enough distros for a One-up? */
605 if (player_status.distros >= DISTROS_LIFEUP)
607 player_status.distros = player_status.distros - DISTROS_LIFEUP;
608 if(player_status.lives < MAX_LIVES)
609 ++player_status.lives;
610 /*We want to hear the sound even, if MAX_LIVES is reached*/
611 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
616 Player::draw(Camera& viewport, int layer)
618 PlayerSprite* sprite;
622 else if (got_power == FIRE_POWER)
624 else if (got_power == ICE_POWER)
629 Vector pos = viewport.world2screen(Vector(base.x, base.y));
631 if(layer == LAYER_OBJECTS + 1) {
632 // Draw arm overlay graphics when Tux is holding something
633 if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check() && !duck)
636 sprite->grab_right->draw(pos);
638 sprite->grab_left->draw(pos);
641 // Draw blinking star overlay
642 if (invincible_timer.started() &&
643 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
645 if (size == SMALL || duck)
646 smalltux_star->draw(pos);
648 largetux_star->draw(pos);
654 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
656 if (dying == DYING_SQUISHED)
658 smalltux_gameover->draw(pos);
662 if (duck && size != SMALL)
665 sprite->duck_right->draw(pos);
667 sprite->duck_left->draw(pos);
669 else if (skidding_timer.started())
672 sprite->skid_right->draw(pos);
674 sprite->skid_left->draw(pos);
676 else if (kick_timer.started())
679 sprite->kick_right->draw(pos);
681 sprite->kick_left->draw(pos);
683 else if (physic.get_velocity_y() != 0)
686 sprite->jump_right->draw(pos);
688 sprite->jump_left->draw(pos);
692 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
695 sprite->stand_right->draw(pos);
697 sprite->stand_left->draw(pos);
702 sprite->walk_right->draw(pos);
704 sprite->walk_left->draw(pos);
711 fillrect(base.x - viewport.get_translation().x,
712 base.y - viewport.get_translation().y,
713 base.width, base.height, 75,75,75, 150);
717 Player::collision(const MovingObject& other, int collision_type)
720 (void) collision_type;
721 // will be implemented later
725 Player::collision(void* p_c_object, int c_object)
727 BadGuy* pbad_c = NULL;
728 Trampoline* ptramp_c = NULL;
729 FlyingPlatform* pplatform_c = NULL;
734 pbad_c = (BadGuy*) p_c_object;
736 /* Hurt player if he touches a badguy */
737 if (!pbad_c->dying && !dying &&
738 !safe_timer.started() &&
739 pbad_c->mode != BadGuy::HELD)
741 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
742 && !holding_something)
744 holding_something = true;
745 pbad_c->mode = BadGuy::HELD;
748 else if (pbad_c->mode == BadGuy::FLAT)
750 // Don't get hurt if we're kicking a flat badguy!
752 else if (pbad_c->mode == BadGuy::KICK)
754 /* Hurt if you get hit by kicked laptop: */
755 if (!invincible_timer.started())
762 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
763 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
764 || pbad_c->kind == BAD_SPIKY))
768 if (!invincible_timer.started())
777 player_status.score_multiplier++;
782 ptramp_c = (Trampoline*) p_c_object;
784 // Pick up trampoline
785 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
787 holding_something = true;
788 ptramp_c->mode = Trampoline::M_HELD;
789 ptramp_c->base.y -= 8;
791 // Set down trampoline
792 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
794 holding_something = false;
795 ptramp_c->mode = Trampoline::M_NORMAL;
796 ptramp_c->base.y += 8;
797 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
800 // ptramp_c->base.x = base.x + base.width+1;
802 // ptramp_c->base.x = base.x - base.width-1;
805 // Don't let tux walk through trampoline
806 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
808 if (physic.get_velocity_x() > 0) // RIGHT
810 physic.set_velocity_x(0);
811 base.x = ptramp_c->base.x - base.width;
813 else if (physic.get_velocity_x() < 0) // LEFT
815 physic.set_velocity_x(0);
816 base.x = ptramp_c->base.x + ptramp_c->base.width;
821 case CO_FLYING_PLATFORM:
822 pplatform_c = (FlyingPlatform*) p_c_object;
824 base.y = pplatform_c->base.y - base.height;
825 physic.enable_gravity(false);
837 Player::kill(HurtMode mode)
842 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
844 physic.set_velocity_x(0);
846 if (mode == SHRINK && size == BIG)
848 if (got_power != NONE_POWER)
850 got_power = NONE_POWER;
858 safe_timer.start(TUX_SAFE_TIME);
862 physic.enable_gravity(true);
863 physic.set_acceleration(0, 0);
864 physic.set_velocity(0, 7);
865 --player_status.lives;
866 dying = DYING_SQUISHED;
867 dying_timer.start(3000);
871 /* Remove Tux's power ups */
873 Player::remove_powerups()
875 got_power = NONE_POWER;
881 Player::move(const Vector& vector)
885 old_base = previous_base = base;
889 Player::check_bounds(Camera& viewport)
891 /* Keep tux in bounds: */
893 { // Lock Tux to the size of the level, so that he doesn't fall of
898 /* Keep in-bounds, vertically: */
899 if (base.y > World::current()->get_level()->height * /*TILE_HEIGHT*/ 32)
906 // can happen if back scrolling is disabled
907 if(base.x < viewport.get_translation().x) {
908 base.x = viewport.get_translation().x;
911 if(base.x >= viewport.get_translation().x + screen->w - base.width) {
912 base.x = viewport.get_translation().x + screen->w - base.width;
918 if(collision_object_map(base)) {