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;
105 fall_mode = ON_GROUND;
113 player_input_init(&input);
115 invincible_timer.init(true);
116 skidding_timer.init(true);
117 safe_timer.init(true);
118 frame_timer.init(true);
119 kick_timer.init(true);
120 shooting_timer.init(true);
126 Player::key_event(SDLKey key, int state)
128 if(key == keymap.right)
133 else if(key == keymap.left)
138 else if(key == keymap.jump)
143 else if(key == keymap.duck)
148 else if(key == keymap.fire)
160 Player::level_begin()
164 previous_base = old_base = base;
169 player_input_init(&input);
171 invincible_timer.init(true);
172 skidding_timer.init(true);
173 safe_timer.init(true);
174 frame_timer.init(true);
180 Player::action(float elapsed_time)
182 bool jumped_in_solid = false;
184 if (input.fire == UP)
185 holding_something = false;
188 previous_base = base;
190 /* --- HANDLE TUX! --- */
191 if(dying == DYING_NOT)
194 physic.apply(elapsed_time, base.x, base.y);
196 if(dying == DYING_NOT)
198 base_type target = base;
200 collision_swept_object_map(&old_base, &base);
202 if (!invincible_timer.started()
203 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
204 || isspike(base.x, base.y + base.height)
205 || isspike(base.x + base.width, base.y + base.height)))
210 // Don't accelerate Tux if he is running against a wall
211 if (target.x != base.x)
213 physic.set_velocity_x(0);
216 // special exception for cases where we're stuck under tiles after
217 // being ducked. In this case we drift out
218 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
219 && collision_object_map(base))
221 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
222 previous_base = old_base = base;
228 physic.enable_gravity(true);
232 physic.set_velocity_y(0);
233 jumped_in_solid = true;
239 if (physic.get_velocity_y() < 0)
241 base.y = (int)(((int)base.y / 32) * 32);
242 physic.set_velocity_y(0);
245 physic.enable_gravity(false);
246 /* Reset score multiplier (for multi-hits): */
247 if (!invincible_timer.started())
248 player_status.score_multiplier = 1;
253 if (isbrick(base.x, base.y) ||
254 isfullbox(base.x, base.y))
256 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
257 World::current()->trybumpbadguy(base.x, base.y - 64);
259 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
261 bumpbrick(base.x, base.y);
262 World::current()->tryemptybox(base.x, base.y, RIGHT);
265 if (isbrick(base.x+ 31, base.y) ||
266 isfullbox(base.x+ 31, base.y))
268 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
269 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
272 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
274 bumpbrick(base.x+ 31, base.y);
275 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
287 /* Make sure jumping is off. */
293 /* ---- DONE HANDLING TUX! --- */
296 skidding_timer.check();
297 invincible_timer.check();
305 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
306 issolid(base.x + 1, base.y + base.height) ||
307 issolid(base.x + base.width - 1, base.y + base.height) );
311 Player::under_solid()
313 return ( issolid(base.x + base.width / 2, base.y) ||
314 issolid(base.x + 1, base.y) ||
315 issolid(base.x + base.width - 1, base.y) );
319 Player::tiles_on_air(int tiles)
321 for(int t = 0; t != tiles; t++)
323 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
324 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
325 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
332 Player::handle_horizontal_input()
334 float vx = physic.get_velocity_x();
335 float vy = physic.get_velocity_y();
336 float ax = physic.get_acceleration_x();
337 float ay = physic.get_acceleration_y();
340 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
344 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
350 if (input.fire == UP) {
351 ax = dirsign * WALK_ACCELERATION_X;
353 if(vx >= MAX_WALK_XM && dirsign > 0) {
356 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
361 ax = dirsign * RUN_ACCELERATION_X;
363 if(vx >= MAX_RUN_XM && dirsign > 0) {
366 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
372 // we can reach WALK_SPEED without any acceleration
373 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
374 vx = dirsign * WALK_SPEED;
377 // changing directions?
378 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
379 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
380 skidding_timer.start(SKID_TIME);
381 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
388 // we get slower when not pressing any keys
390 if(fabs(vx) < WALK_SPEED) {
394 ax = WALK_ACCELERATION_X * 1.5;
396 ax = WALK_ACCELERATION_X * -1.5;
400 // if we're on ice slow down acceleration or deceleration
401 if (isice(base.x, base.y + base.height))
403 /* the acceleration/deceleration rate on ice is inversely proportional to
404 * the current velocity.
407 // increasing 1 will increase acceleration/deceleration rate
408 // decreasing 1 will decrease acceleration/deceleration rate
409 // must stay above zero, though
410 if (ax != 0) ax *= 1 / fabs(vx);
413 physic.set_velocity(vx, vy);
414 physic.set_acceleration(ax, ay);
418 Player::handle_vertical_input()
422 fall_mode = ON_GROUND;
423 last_ground_y = base.y;
425 if(base.y > last_ground_y)
427 else if(fall_mode == ON_GROUND)
432 if(input.up == DOWN && can_jump && on_ground())
434 if(duck) { // only jump a little bit when in duck mode {
435 physic.set_velocity_y(3);
437 // jump higher if we are running
438 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
439 physic.set_velocity_y(5.8);
441 physic.set_velocity_y(5.2);
448 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
450 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
452 // Let go of jump key
453 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
456 physic.set_velocity_y(0);
459 /* In case the player has pressed Down while in a certain range of air,
460 enable butt jump action */
461 if (input.down == DOWN && !butt_jump)
462 if(tiles_on_air(TILES_FOR_BUTTJUMP))
465 /* When Down is not held anymore, disable butt jump */
466 if(butt_jump && input.down == UP)
469 if (butt_jump && on_ground() && size == BIG)
471 if(World::current()->trybreakbrick(base.x, base.y + base.height, false)
472 || World::current()->trybreakbrick(
473 base.x + base.width, base.y + base.height, false)) {
474 // make tux jumping a little bit again after breaking the bricks
475 physic.set_velocity_y(2);
477 // butt_jump = false; // comment this, in case you won't to disable the continued use of buttjump
480 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
481 issolid(base.x + 1, base.y + base.height + 64) ||
482 issolid(base.x + base.width - 1, base.y + base.height + 64))
486 && input.old_up == UP)
491 if(on_ground()) /* Make sure jumping is off. */
494 input.old_up = input.up;
498 Player::handle_input()
500 /* Handle horizontal movement: */
501 handle_horizontal_input();
505 if (on_ground() && input.up == UP)
507 handle_vertical_input();
510 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
512 if(World::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
513 physic.get_velocity_x(), dir))
514 shooting_timer.start(SHOOTING_TIME);
515 input.old_fire = DOWN;
518 /* tux animations: */
519 if(!frame_timer.check())
521 frame_timer.start(25);
522 if (input.right == UP && input.left == UP)
529 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
530 (global_frame_counter % 4) == 0)
531 frame_main = (frame_main + 1) % 4;
541 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
546 // changing base size confuses collision otherwise
547 old_base = previous_base = base;
549 else if(input.down == UP && size == BIG && duck)
551 // try if we can really unduck
554 // when unducking in air we need some space to do so
555 if(on_ground() || !collision_object_map(base)) {
557 // changing base size confuses collision otherwise
558 old_base = previous_base = base;
560 // undo the ducking changes
577 old_base = previous_base = base;
581 Player::grabdistros()
586 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
587 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
589 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
590 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
594 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
595 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
600 /* Enough distros for a One-up? */
601 if (player_status.distros >= DISTROS_LIFEUP)
603 player_status.distros = player_status.distros - DISTROS_LIFEUP;
604 if(player_status.lives < MAX_LIVES)
605 ++player_status.lives;
606 /*We want to hear the sound even, if MAX_LIVES is reached*/
607 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
612 Player::draw(Camera& viewport, int layer)
614 PlayerSprite* sprite;
618 else if (got_power == FIRE_POWER)
620 else if (got_power == ICE_POWER)
625 Vector pos = viewport.world2screen(Vector(base.x, base.y));
627 if(layer == LAYER_OBJECTS + 1) {
628 // Draw arm overlay graphics when Tux is holding something
629 if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check() && !duck)
632 sprite->grab_right->draw(pos);
634 sprite->grab_left->draw(pos);
637 // Draw blinking star overlay
638 if (invincible_timer.started() &&
639 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
641 if (size == SMALL || duck)
642 smalltux_star->draw(pos);
644 largetux_star->draw(pos);
650 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
652 if (dying == DYING_SQUISHED)
654 smalltux_gameover->draw(pos);
658 if (duck && size != SMALL)
661 sprite->duck_right->draw(pos);
663 sprite->duck_left->draw(pos);
665 else if (skidding_timer.started())
668 sprite->skid_right->draw(pos);
670 sprite->skid_left->draw(pos);
672 else if (kick_timer.started())
675 sprite->kick_right->draw(pos);
677 sprite->kick_left->draw(pos);
679 else if (physic.get_velocity_y() != 0)
682 sprite->jump_right->draw(pos);
684 sprite->jump_left->draw(pos);
688 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
691 sprite->stand_right->draw(pos);
693 sprite->stand_left->draw(pos);
698 sprite->walk_right->draw(pos);
700 sprite->walk_left->draw(pos);
707 fillrect(base.x - viewport.get_translation().x,
708 base.y - viewport.get_translation().y,
709 base.width, base.height, 75,75,75, 150);
713 Player::collision(const MovingObject& other, int collision_type)
716 (void) collision_type;
717 // will be implemented later
721 Player::collision(void* p_c_object, int c_object)
723 BadGuy* pbad_c = NULL;
724 Trampoline* ptramp_c = NULL;
725 FlyingPlatform* pplatform_c = NULL;
730 pbad_c = (BadGuy*) p_c_object;
732 /* Hurt player if he touches a badguy */
733 if (!pbad_c->dying && !dying &&
734 !safe_timer.started() &&
735 pbad_c->mode != BadGuy::HELD)
737 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
738 && !holding_something)
740 holding_something = true;
741 pbad_c->mode = BadGuy::HELD;
744 else if (pbad_c->mode == BadGuy::FLAT)
746 // Don't get hurt if we're kicking a flat badguy!
748 else if (pbad_c->mode == BadGuy::KICK)
750 /* Hurt if you get hit by kicked laptop: */
751 if (!invincible_timer.started())
758 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
759 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
760 || pbad_c->kind == BAD_SPIKY))
764 if (!invincible_timer.started())
773 player_status.score_multiplier++;
778 ptramp_c = (Trampoline*) p_c_object;
780 // Pick up trampoline
781 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
783 holding_something = true;
784 ptramp_c->mode = Trampoline::M_HELD;
785 ptramp_c->base.y -= 8;
787 // Set down trampoline
788 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
790 holding_something = false;
791 ptramp_c->mode = Trampoline::M_NORMAL;
792 ptramp_c->base.y += 8;
793 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
796 // ptramp_c->base.x = base.x + base.width+1;
798 // ptramp_c->base.x = base.x - base.width-1;
801 // Don't let tux walk through trampoline
802 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
804 if (physic.get_velocity_x() > 0) // RIGHT
806 physic.set_velocity_x(0);
807 base.x = ptramp_c->base.x - base.width;
809 else if (physic.get_velocity_x() < 0) // LEFT
811 physic.set_velocity_x(0);
812 base.x = ptramp_c->base.x + ptramp_c->base.width;
817 case CO_FLYING_PLATFORM:
818 pplatform_c = (FlyingPlatform*) p_c_object;
820 base.y = pplatform_c->base.y - base.height;
832 Player::kill(HurtMode mode)
834 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
836 physic.set_velocity_x(0);
838 if (mode == SHRINK && size == BIG)
840 if (got_power != NONE_POWER)
842 got_power = NONE_POWER;
850 safe_timer.start(TUX_SAFE_TIME);
854 physic.enable_gravity(true);
855 physic.set_acceleration(0, 0);
856 physic.set_velocity(0, 7);
857 if(dying != DYING_SQUISHED)
858 --player_status.lives;
859 dying = DYING_SQUISHED;
870 bool Player::is_dead()
873 World::current()->camera->get_translation().x;
875 World::current()->camera->get_translation().y;
876 if(base.y > screen->h + scroll_y || base.y > World::current()->get_level()->height*32 ||
877 base.x < scroll_x - AUTOSCROLL_DEAD_INTERVAL) // can happen in auto-scrolling
883 /* Remove Tux's power ups */
885 Player::remove_powerups()
887 got_power = NONE_POWER;
893 Player::check_bounds(Camera& viewport,
894 bool back_scrolling, bool hor_autoscroll)
896 /* Keep tux in bounds: */
898 { // Lock Tux to the size of the level, so that he doesn't fall of
903 /* Keep in-bounds, vertically: */
904 if (base.y > World::current()->get_level()->height * /*TILE_HEIGHT*/ 32)
909 if(base.x < viewport.get_translation().x && (!back_scrolling || hor_autoscroll)) // can happen if back scrolling is disabled
910 base.x = viewport.get_translation().x;
914 if(base.x == viewport.get_translation().x)
915 if(issolid(base.x+32, base.y) || (size != SMALL && issolid(base.x+32, base.y+32)))
918 if(base.x + base.width > viewport.get_translation().x + screen->w)
919 base.x = viewport.get_translation().x + screen->w - base.width;