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.
33 Sprite* smalltux_gameover;
34 Sprite* smalltux_star;
35 Sprite* largetux_star;
37 PlayerSprite smalltux;
38 PlayerSprite largetux;
43 PlayerKeymap::PlayerKeymap()
45 keymap.jump = SDLK_UP;
46 keymap.duck = SDLK_DOWN;
47 keymap.left = SDLK_LEFT;
48 keymap.right = SDLK_RIGHT;
49 keymap.fire = SDLK_LCTRL;
52 void player_input_init(player_input_type* pplayer_input)
54 pplayer_input->down = UP;
55 pplayer_input->fire = UP;
56 pplayer_input->left = UP;
57 pplayer_input->old_fire = UP;
58 pplayer_input->right = UP;
59 pplayer_input->up = UP;
65 Level* plevel = World::current()->get_level();
67 holding_something = false;
75 base.x = plevel->start_pos_x;
76 base.y = plevel->start_pos_y;
79 previous_base = old_base = base;
89 player_input_init(&input);
91 invincible_timer.init(true);
92 skidding_timer.init(true);
93 safe_timer.init(true);
94 frame_timer.init(true);
100 Player::key_event(SDLKey key, int state)
102 if(key == keymap.right)
107 else if(key == keymap.left)
112 else if(key == keymap.jump)
117 else if(key == keymap.duck)
122 else if(key == keymap.fire)
134 Player::level_begin()
140 previous_base = old_base = base;
145 player_input_init(&input);
147 invincible_timer.init(true);
148 skidding_timer.init(true);
149 safe_timer.init(true);
150 frame_timer.init(true);
156 Player::action(double frame_ratio)
158 bool jumped_in_solid = false;
160 if (input.fire == UP)
161 holding_something = false;
164 previous_base = base;
166 /* --- HANDLE TUX! --- */
167 if(dying == DYING_NOT)
170 physic.apply(frame_ratio, base.x, base.y);
172 if(dying == DYING_NOT)
174 base_type target = base;
176 collision_swept_object_map(&old_base, &base);
178 // Don't accelerate Tux if he is running against a wall
179 if (target.x != base.x)
181 physic.set_velocity_x(0);
184 // special exception for cases where we're stuck under tiles after
185 // being ducked. In this case we drift out
186 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
187 && collision_object_map(base))
189 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
190 previous_base = old_base = base;
197 physic.enable_gravity(true);
201 physic.set_velocity_y(0);
202 jumped_in_solid = true;
208 if (physic.get_velocity_y() < 0)
210 base.y = (int)(((int)base.y / 32) * 32);
211 physic.set_velocity_y(0);
214 physic.enable_gravity(false);
215 /* Reset score multiplier (for multi-hits): */
216 player_status.score_multiplier = 1;
221 if (isbrick(base.x, base.y) ||
222 isfullbox(base.x, base.y))
224 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
225 World::current()->trybumpbadguy(base.x, base.y - 64);
227 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
229 bumpbrick(base.x, base.y);
230 World::current()->tryemptybox(base.x, base.y, RIGHT);
233 if (isbrick(base.x+ 31, base.y) ||
234 isfullbox(base.x+ 31, base.y))
236 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
237 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
240 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
242 bumpbrick(base.x+ 31, base.y);
243 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
255 /* Make sure jumping is off. */
261 /* ---- DONE HANDLING TUX! --- */
264 skidding_timer.check();
265 invincible_timer.check();
272 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
273 issolid(base.x + 1, base.y + base.height) ||
274 issolid(base.x + base.width - 1, base.y + base.height) );
278 Player::under_solid()
280 return ( issolid(base.x + base.width / 2, base.y) ||
281 issolid(base.x + 1, base.y) ||
282 issolid(base.x + base.width - 1, base.y) );
286 Player::handle_horizontal_input()
288 float vx = physic.get_velocity_x();
289 float vy = physic.get_velocity_y();
290 float ax = physic.get_acceleration_x();
291 float ay = physic.get_acceleration_y();
294 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
297 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
302 if (input.fire == UP) {
303 ax = dirsign * WALK_ACCELERATION_X;
305 if(vx >= MAX_WALK_XM && dirsign > 0) {
308 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
313 ax = dirsign * RUN_ACCELERATION_X;
315 if(vx >= MAX_RUN_XM && dirsign > 0) {
318 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
324 // we can reach WALK_SPEED without any acceleration
325 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
326 vx = dirsign * WALK_SPEED;
329 // changing directions?
330 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
331 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
332 skidding_timer.start(SKID_TIME);
333 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
340 // we get slower when not pressing any keys
342 if(fabs(vx) < WALK_SPEED) {
346 ax = WALK_ACCELERATION_X * 1.5;
348 ax = WALK_ACCELERATION_X * -1.5;
352 // if we're on ice slow down acceleration or deceleration
353 if (isice(base.x, base.y + base.height))
355 /* the acceleration/deceleration rate on ice is inversely proportional to
356 * the current velocity.
359 // increasing 1 will increase acceleration/deceleration rate
360 // decreasing 1 will decrease acceleration/deceleration rate
361 // must stay above zero, though
362 if (ax != 0) ax *= 1 / fabs(vx);
365 physic.set_velocity(vx, vy);
366 physic.set_acceleration(ax, ay);
370 Player::handle_vertical_input()
376 // jump higher if we are running
377 if (physic.get_velocity_x() > MAX_WALK_XM)
378 physic.set_velocity_y(5.8);
380 physic.set_velocity_y(5.2);
385 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
387 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
390 else if(input.up == UP && jumping)
393 if(physic.get_velocity_y() > 0) {
394 physic.set_velocity_y(0);
400 Player::handle_input()
402 /* Handle horizontal movement: */
403 handle_horizontal_input();
407 if ( input.up == DOWN || (input.up == UP && jumping))
409 handle_vertical_input();
414 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
416 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
417 input.old_fire = DOWN;
420 /* tux animations: */
421 if(!frame_timer.check())
423 frame_timer.start(25);
424 if (input.right == UP && input.left == UP)
431 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
432 (global_frame_counter % 4) == 0)
433 frame_main = (frame_main + 1) % 4;
443 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0)
448 // changing base size confuses collision otherwise
449 old_base = previous_base = base;
451 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
456 old_base = previous_base = base;
461 Player::grabdistros()
466 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
467 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
469 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
470 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
474 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
475 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
480 /* Enough distros for a One-up? */
481 if (player_status.distros >= DISTROS_LIFEUP)
483 player_status.distros = player_status.distros - DISTROS_LIFEUP;
484 if(player_status.lives < MAX_LIVES)
485 ++player_status.lives;
486 /*We want to hear the sound even, if MAX_LIVES is reached*/
487 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
494 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
496 if (dying == DYING_SQUISHED)
498 smalltux_gameover->draw(base.x - scroll_x, base.y);
502 PlayerSprite* sprite;
511 if (skidding_timer.started())
514 sprite->skid_right->draw(base.x - scroll_x, base.y);
516 sprite->skid_left->draw(base.x - scroll_x, base.y);
523 sprite->duck_right->draw(base.x - scroll_x, base.y);
525 sprite->duck_left->draw(base.x - scroll_x, base.y);
527 else if (physic.get_velocity_y() != 0)
530 sprite->jump_right->draw(base.x - scroll_x, base.y);
532 sprite->jump_left->draw(base.x - scroll_x, base.y);
536 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
539 sprite->stand_right->draw( base.x - scroll_x, base.y);
541 sprite->stand_left->draw( base.x - scroll_x, base.y);
546 sprite->walk_right->draw(base.x - scroll_x, base.y);
548 sprite->walk_left->draw(base.x - scroll_x, base.y);
553 // Draw arm overlay graphics when Tux is holding something
554 if (holding_something && physic.get_velocity_y() == 0)
557 sprite->grab_right->draw(base.x - scroll_x, base.y);
559 sprite->grab_left->draw(base.x - scroll_x, base.y);
562 // Draw blinking star overlay
563 if (invincible_timer.started())
565 if (size == SMALL || duck)
566 smalltux_star->draw(base.x - scroll_x, base.y);
568 largetux_star->draw(base.x - scroll_x, base.y);
574 fillrect(base.x - scroll_x, base.y,
575 base.width, base.height, 75,75,75, 150);
579 Player::collision(void* p_c_object, int c_object)
581 BadGuy* pbad_c = NULL;
586 pbad_c = (BadGuy*) p_c_object;
588 /* Hurt player if he touches a badguy */
589 if (!pbad_c->dying && !dying &&
590 !safe_timer.started() &&
591 pbad_c->mode != BadGuy::HELD)
593 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN)
595 holding_something = true;
596 pbad_c->mode = BadGuy::HELD;
599 else if (pbad_c->mode == BadGuy::FLAT)
601 // Don't get hurt if we're kicking a flat badguy!
603 else if (pbad_c->mode == BadGuy::KICK)
605 /* Hurt if you get hit by kicked laptop: */
606 if (!invincible_timer.started())
612 pbad_c->dying = DYING_FALLING;
613 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
614 World::current()->add_score(pbad_c->base.x - scroll_x,
616 25 * player_status.score_multiplier);
621 if (!invincible_timer.started())
630 player_status.score_multiplier++;
642 Player::kill(HurtMode mode)
644 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
646 physic.set_velocity_x(0);
648 if (mode == SHRINK && size == BIG)
660 safe_timer.start(TUX_SAFE_TIME);
664 physic.enable_gravity(true);
665 physic.set_acceleration(0, 0);
666 physic.set_velocity(0, 7);
667 dying = DYING_SQUISHED;
678 bool Player::is_dead()
680 if(base.y > screen->h)
686 /* Remove Tux's power ups */
688 Player::remove_powerups()
696 Player::keep_in_bounds()
698 Level* plevel = World::current()->get_level();
700 /* Keep tux in bounds: */
702 { // Lock Tux to the size of the level, so that he doesn't fall of
706 else if (base.x < scroll_x)
711 /* Keep in-bounds, vertically: */
712 if (base.y > screen->h)
717 int scroll_threshold = screen->w/2 - 80;
720 scroll_x += screen->w/2;
721 // Backscrolling for debug mode
722 if (scroll_x < base.x - 80)
723 scroll_x = base.x - 80;
724 else if (scroll_x > base.x + 80)
725 scroll_x = base.x + 80;
726 scroll_x -= screen->w/2;
733 if (base.x > scroll_threshold + scroll_x
734 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
736 // FIXME: Scrolling needs to be handled by a seperate View
737 // class, doing it as a player huck is ugly
739 // Scroll the screen in past center:
740 scroll_x = base.x - scroll_threshold;
742 // Lock the scrolling to the levelsize, so that we don't
743 // scroll over the right border
744 if (scroll_x > 32 * plevel->width - screen->w)
745 scroll_x = 32 * plevel->width - screen->w;