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.
31 #include "screen/screen.h"
33 // behavior definitions:
34 #define TILES_FOR_BUTTJUMP 3
35 // animation times (in ms):
36 #define SHOOTING_TIME 320
38 #define AUTOSCROLL_DEAD_INTERVAL 300
42 Sprite* smalltux_gameover;
43 Sprite* smalltux_star;
44 Sprite* largetux_star;
45 Sprite* growingtux_left;
46 Sprite* growingtux_right;
48 PlayerSprite smalltux;
49 PlayerSprite largetux;
55 PlayerKeymap::PlayerKeymap()
57 keymap.jump = SDLK_UP;
58 keymap.duck = SDLK_DOWN;
59 keymap.left = SDLK_LEFT;
60 keymap.right = SDLK_RIGHT;
61 keymap.fire = SDLK_LCTRL;
64 void player_input_init(player_input_type* pplayer_input)
66 pplayer_input->down = UP;
67 pplayer_input->fire = UP;
68 pplayer_input->left = UP;
69 pplayer_input->old_fire = UP;
70 pplayer_input->right = UP;
71 pplayer_input->up = UP;
72 pplayer_input->old_up = UP;
87 holding_something = false;
93 got_power = NONE_POWER;
97 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);
121 growing_timer.init(true);
127 Player::key_event(SDLKey key, int state)
129 if(key == keymap.right)
134 else if(key == keymap.left)
139 else if(key == keymap.jump)
144 else if(key == keymap.duck)
149 else if(key == keymap.fire)
161 Player::level_begin()
165 previous_base = old_base = base;
170 player_input_init(&input);
172 invincible_timer.init(true);
173 skidding_timer.init(true);
174 safe_timer.init(true);
175 frame_timer.init(true);
176 growing_timer.init(true);
182 Player::action(float elapsed_time)
184 bool jumped_in_solid = false;
186 if(dying && !dying_timer.check()) {
191 if (input.fire == UP)
192 holding_something = false;
195 previous_base = base;
197 /* --- HANDLE TUX! --- */
198 if(dying == DYING_NOT)
201 physic.apply(elapsed_time, base.x, base.y);
203 if(dying == DYING_NOT)
205 base_type target = base;
207 collision_swept_object_map(&old_base, &base);
209 if ((!invincible_timer.started() && !safe_timer.started())
210 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
211 || isspike(base.x, base.y + base.height)
212 || isspike(base.x + base.width, base.y + base.height)))
217 // Don't accelerate Tux if he is running against a wall
218 if (target.x != base.x)
220 physic.set_velocity_x(0);
223 // special exception for cases where we're stuck under tiles after
224 // being ducked. In this case we drift out
225 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
226 && collision_object_map(base))
228 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
229 previous_base = old_base = base;
235 physic.enable_gravity(true);
239 physic.set_velocity_y(0);
240 jumped_in_solid = true;
247 if (physic.get_velocity_y() < 0)
249 base.y = (int)(((int)base.y / 32) * 32);
250 physic.set_velocity_y(0);
253 physic.enable_gravity(false);
254 /* Reset score multiplier (for multi-hits): */
255 if (!invincible_timer.started())
256 player_status.score_multiplier = 1;
261 if (isbrick(base.x, base.y) ||
262 isfullbox(base.x, base.y))
264 World::current()->trygrabdistro(base.x, base.y - 32, BOUNCE);
265 World::current()->trybumpbadguy(base.x, base.y - 64);
267 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
269 bumpbrick(base.x, base.y);
270 World::current()->tryemptybox(base.x, base.y, RIGHT);
273 if (isbrick(base.x+ 31, base.y) ||
274 isfullbox(base.x+ 31, base.y))
276 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
277 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
280 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
282 bumpbrick(base.x+ 31, base.y);
283 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
295 /* Make sure jumping is off. */
301 /* ---- DONE HANDLING TUX! --- */
304 skidding_timer.check();
305 invincible_timer.check();
313 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
314 issolid(base.x + 1, base.y + base.height) ||
315 issolid(base.x + base.width - 1, base.y + base.height));
319 Player::under_solid()
321 return ( issolid(base.x + base.width / 2, base.y) ||
322 issolid(base.x + 1, base.y) ||
323 issolid(base.x + base.width - 1, base.y) );
327 Player::tiles_on_air(int tiles)
329 for(int t = 0; t != tiles; t++)
331 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
332 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
333 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
340 Player::handle_horizontal_input()
342 float vx = physic.get_velocity_x();
343 float vy = physic.get_velocity_y();
344 float ax = physic.get_acceleration_x();
345 float ay = physic.get_acceleration_y();
348 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
352 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
358 if (input.fire == UP) {
359 ax = dirsign * WALK_ACCELERATION_X;
361 if(vx >= MAX_WALK_XM && dirsign > 0) {
364 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
369 ax = dirsign * RUN_ACCELERATION_X;
371 if(vx >= MAX_RUN_XM && dirsign > 0) {
374 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
380 // we can reach WALK_SPEED without any acceleration
381 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
382 vx = dirsign * WALK_SPEED;
385 // changing directions?
386 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
387 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
388 skidding_timer.start(SKID_TIME);
389 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
396 // we get slower when not pressing any keys
398 if(fabs(vx) < WALK_SPEED) {
402 ax = WALK_ACCELERATION_X * 1.5;
404 ax = WALK_ACCELERATION_X * -1.5;
408 // if we're on ice slow down acceleration or deceleration
409 if (isice(base.x, base.y + base.height))
411 /* the acceleration/deceleration rate on ice is inversely proportional to
412 * the current velocity.
415 // increasing 1 will increase acceleration/deceleration rate
416 // decreasing 1 will decrease acceleration/deceleration rate
417 // must stay above zero, though
418 if (ax != 0) ax *= 1 / fabs(vx);
421 physic.set_velocity(vx, vy);
422 physic.set_acceleration(ax, ay);
426 Player::handle_vertical_input()
430 fall_mode = ON_GROUND;
431 last_ground_y = base.y;
433 if(base.y > last_ground_y)
435 else if(fall_mode == ON_GROUND)
440 if(input.up == DOWN && can_jump && on_ground())
442 if(duck) { // only jump a little bit when in duck mode {
443 physic.set_velocity_y(3);
445 // jump higher if we are running
446 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
447 physic.set_velocity_y(5.8);
449 physic.set_velocity_y(5.2);
456 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
458 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
460 // Let go of jump key
461 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
464 physic.set_velocity_y(0);
467 /* In case the player has pressed Down while in a certain range of air,
468 enable butt jump action */
469 if (input.down == DOWN && !butt_jump)
470 if(tiles_on_air(TILES_FOR_BUTTJUMP))
473 /* When Down is not held anymore, disable butt jump */
474 if(butt_jump && input.down == UP)
477 if (butt_jump && on_ground() && size == BIG)
479 if(World::current()->trybreakbrick(base.x, base.y + base.height, false)
480 || World::current()->trybreakbrick(
481 base.x + base.width, base.y + base.height, false)) {
482 // make tux jumping a little bit again after breaking the bricks
483 physic.set_velocity_y(2);
485 // butt_jump = false; // comment this, in case you won't to disable the continued use of buttjump
488 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
489 issolid(base.x + 1, base.y + base.height + 64) ||
490 issolid(base.x + base.width - 1, base.y + base.height + 64))
494 && input.old_up == UP)
499 if(on_ground()) /* Make sure jumping is off. */
502 input.old_up = input.up;
506 Player::handle_input()
508 /* Handle horizontal movement: */
509 handle_horizontal_input();
513 if (on_ground() && input.up == UP)
515 handle_vertical_input();
518 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
520 if(World::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
521 physic.get_velocity_x(), dir))
522 shooting_timer.start(SHOOTING_TIME);
523 input.old_fire = DOWN;
526 /* tux animations: */
527 if(!frame_timer.check())
529 frame_timer.start(25);
530 if (input.right == UP && input.left == UP)
537 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
538 (global_frame_counter % 4) == 0)
539 frame_main = (frame_main + 1) % 4;
549 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
554 // changing base size confuses collision otherwise
555 old_base = previous_base = base;
557 else if(input.down == UP && size == BIG && duck)
559 // try if we can really unduck
562 // when unducking in air we need some space to do so
563 if(on_ground() || !collision_object_map(base)) {
565 // changing base size confuses collision otherwise
566 old_base = previous_base = base;
568 // undo the ducking changes
576 Player::grow(bool animate)
586 growing_timer.start((int)((growingtux_left->get_frames() / growingtux_left->get_fps()) * 1000));
588 old_base = previous_base = base;
592 Player::grabdistros()
597 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
598 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
600 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
601 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
605 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
606 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
611 /* Enough distros for a One-up? */
612 if (player_status.distros >= DISTROS_LIFEUP)
614 player_status.distros = player_status.distros - DISTROS_LIFEUP;
615 if(player_status.lives < MAX_LIVES)
616 ++player_status.lives;
617 /*We want to hear the sound even, if MAX_LIVES is reached*/
618 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
623 Player::draw(DrawingContext& context)
625 PlayerSprite* sprite;
629 else if (got_power == FIRE_POWER)
631 else if (got_power == ICE_POWER)
636 int layer = LAYER_OBJECTS - 1;
637 Vector pos = Vector(base.x, base.y);
639 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
641 if (dying == DYING_SQUISHED)
643 smalltux_gameover->draw(context, pos, LAYER_OBJECTS);
647 if(growing_timer.check())
650 growingtux_right->draw(context, pos, layer);
652 growingtux_left->draw(context, pos, layer);
654 else if (duck && size != SMALL)
657 sprite->duck_right->draw(context, pos, layer);
659 sprite->duck_left->draw(context, pos, layer);
661 else if (skidding_timer.started())
664 sprite->skid_right->draw(context, pos, layer);
666 sprite->skid_left->draw(context, pos, layer);
668 else if (kick_timer.started())
671 sprite->kick_right->draw(context, pos, layer);
673 sprite->kick_left->draw(context, pos, layer);
675 else if (physic.get_velocity_y() != 0)
678 sprite->jump_right->draw(context, pos, layer);
680 sprite->jump_left->draw(context, pos, layer);
684 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
687 sprite->stand_right->draw(context, pos, layer);
689 sprite->stand_left->draw(context, pos, layer);
694 sprite->walk_right->draw(context, pos, layer);
696 sprite->walk_left->draw(context, pos, layer);
702 // Draw arm overlay graphics when Tux is holding something
703 if ((holding_something && physic.get_velocity_y() == 0) || shooting_timer.check() && !duck)
706 sprite->grab_right->draw(context, pos, LAYER_OBJECTS + 1);
708 sprite->grab_left->draw(context, pos, LAYER_OBJECTS + 1);
711 // Draw blinking star overlay
712 if (invincible_timer.started() &&
713 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
715 if (size == SMALL || duck)
716 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
718 largetux_star->draw(context, pos, LAYER_OBJECTS + 2);
723 fillrect(base.x - viewport.get_translation().x,
724 base.y - viewport.get_translation().y,
725 base.width, base.height, 75,75,75, 150);
730 Player::collision(const MovingObject& other, int collision_type)
733 (void) collision_type;
734 // will be implemented later
738 Player::collision(void* p_c_object, int c_object)
740 BadGuy* pbad_c = NULL;
741 Trampoline* ptramp_c = NULL;
742 FlyingPlatform* pplatform_c = NULL;
747 pbad_c = (BadGuy*) p_c_object;
749 /* Hurt player if he touches a badguy */
750 if (!pbad_c->dying && !dying &&
751 !safe_timer.started() &&
752 pbad_c->mode != BadGuy::HELD)
754 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
755 && !holding_something)
757 holding_something = true;
758 pbad_c->mode = BadGuy::HELD;
761 else if (pbad_c->mode == BadGuy::FLAT)
763 // Don't get hurt if we're kicking a flat badguy!
765 else if (pbad_c->mode == BadGuy::KICK)
767 /* Hurt if you get hit by kicked laptop: */
768 if (!invincible_timer.started())
775 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
776 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
777 || pbad_c->kind == BAD_SPIKY))
781 if (!invincible_timer.started())
790 player_status.score_multiplier++;
795 ptramp_c = (Trampoline*) p_c_object;
797 // Pick up trampoline
798 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
800 holding_something = true;
801 ptramp_c->mode = Trampoline::M_HELD;
802 ptramp_c->base.y -= 8;
804 // Set down trampoline
805 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
807 holding_something = false;
808 ptramp_c->mode = Trampoline::M_NORMAL;
809 ptramp_c->base.y += 8;
810 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
813 // ptramp_c->base.x = base.x + base.width+1;
815 // ptramp_c->base.x = base.x - base.width-1;
818 // Don't let tux walk through trampoline
819 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
821 if (physic.get_velocity_x() > 0) // RIGHT
823 physic.set_velocity_x(0);
824 base.x = ptramp_c->base.x - base.width;
826 else if (physic.get_velocity_x() < 0) // LEFT
828 physic.set_velocity_x(0);
829 base.x = ptramp_c->base.x + ptramp_c->base.width;
834 case CO_FLYING_PLATFORM:
835 pplatform_c = (FlyingPlatform*) p_c_object;
837 base.y = pplatform_c->base.y - base.height;
838 physic.set_velocity_x(pplatform_c->get_vel_x());
840 physic.enable_gravity(false);
842 fall_mode = ON_GROUND;
854 Player::kill(HurtMode mode)
859 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
861 physic.set_velocity_x(0);
863 if (mode == SHRINK && size == BIG)
865 if (got_power != NONE_POWER)
867 got_power = NONE_POWER;
875 safe_timer.start(TUX_SAFE_TIME);
879 physic.enable_gravity(true);
880 physic.set_acceleration(0, 0);
881 physic.set_velocity(0, 7);
882 --player_status.lives;
883 dying = DYING_SQUISHED;
884 dying_timer.start(3000);
888 /* Remove Tux's power ups */
890 Player::remove_powerups()
892 got_power = NONE_POWER;
898 Player::move(const Vector& vector)
902 old_base = previous_base = base;
906 Player::check_bounds(DrawingContext& viewport)
908 /* Keep tux in bounds: */
910 { // Lock Tux to the size of the level, so that he doesn't fall of
915 /* Keep in-bounds, vertically: */
916 if (base.y > World::current()->get_level()->height * /*TILE_HEIGHT*/ 32)
923 // can happen if back scrolling is disabled
924 if(base.x < viewport.get_translation().x) {
925 base.x = viewport.get_translation().x;
928 if(base.x >= viewport.get_translation().x + screen->w - base.width) {
929 base.x = viewport.get_translation().x + screen->w - base.width;
935 if(collision_object_map(base)) {