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 Sprite* smalltux_stand_left;
38 Sprite* smalltux_stand_right;
39 Sprite* smalltux_walk_right;
40 Sprite* smalltux_walk_left;
41 Sprite* smalltux_jump_left;
42 Sprite* smalltux_jump_right;
43 Sprite* smalltux_kick_left;
44 Sprite* smalltux_kick_right;
45 Sprite* smalltux_skid_left;
46 Sprite* smalltux_skid_right;
47 Sprite* smalltux_grab_left;
48 Sprite* smalltux_grab_right;
50 Sprite* largetux_stand_left;
51 Sprite* largetux_stand_right;
52 Sprite* largetux_walk_right;
53 Sprite* largetux_walk_left;
54 Sprite* largetux_jump_right;
55 Sprite* largetux_jump_left;
56 Sprite* largetux_kick_left;
57 Sprite* largetux_kick_right;
58 Sprite* largetux_skid_right;
59 Sprite* largetux_skid_left;
60 Sprite* largetux_grab_left;
61 Sprite* largetux_grab_right;
62 Sprite* largetux_duck_right;
63 Sprite* largetux_duck_left;
65 Surface* firetux_right[3];
66 Surface* firetux_left[3];
67 Surface* bigfiretux_right[3];
68 Surface* bigfiretux_left[3];
69 Surface* bigfiretux_right_jump;
70 Surface* bigfiretux_left_jump;
71 Surface* duckfiretux_right;
72 Surface* duckfiretux_left;
73 Surface* skidfiretux_right;
74 Surface* skidfiretux_left;
78 PlayerKeymap::PlayerKeymap()
80 keymap.jump = SDLK_UP;
81 keymap.duck = SDLK_DOWN;
82 keymap.left = SDLK_LEFT;
83 keymap.right = SDLK_RIGHT;
84 keymap.fire = SDLK_LCTRL;
87 void player_input_init(player_input_type* pplayer_input)
89 pplayer_input->down = UP;
90 pplayer_input->fire = UP;
91 pplayer_input->left = UP;
92 pplayer_input->old_fire = UP;
93 pplayer_input->right = UP;
94 pplayer_input->up = UP;
100 Level* plevel = World::current()->get_level();
102 holding_something = false;
110 base.x = plevel->start_pos_x;
111 base.y = plevel->start_pos_y;
114 previous_base = old_base = base;
124 player_input_init(&input);
126 invincible_timer.init(true);
127 skidding_timer.init(true);
128 safe_timer.init(true);
129 frame_timer.init(true);
135 Player::key_event(SDLKey key, int state)
137 if(key == keymap.right)
142 else if(key == keymap.left)
147 else if(key == keymap.jump)
152 else if(key == keymap.duck)
157 else if(key == keymap.fire)
167 Player::level_begin()
173 previous_base = old_base = base;
178 player_input_init(&input);
180 invincible_timer.init(true);
181 skidding_timer.init(true);
182 safe_timer.init(true);
183 frame_timer.init(true);
189 Player::action(double frame_ratio)
191 bool jumped_in_solid = false;
193 if (input.fire == UP)
194 holding_something = false;
197 previous_base = base;
199 /* --- HANDLE TUX! --- */
200 if(dying == DYING_NOT)
203 physic.apply(frame_ratio, base.x, base.y);
205 if(dying == DYING_NOT)
207 base_type target = base;
209 collision_swept_object_map(&old_base, &base);
211 // Don't accelerate Tux if he is running against a wall
212 if (target.x != base.x)
214 physic.set_velocity_x(0);
217 // special exception for cases where we're stuck under tiles after
218 // being ducked. In this case we drift out
219 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
220 && collision_object_map(base))
222 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
223 previous_base = old_base = base;
230 physic.enable_gravity(true);
234 physic.set_velocity_y(0);
235 jumped_in_solid = true;
241 if (physic.get_velocity_y() < 0)
243 base.y = (int)(((int)base.y / 32) * 32);
244 physic.set_velocity_y(0);
247 physic.enable_gravity(false);
248 /* Reset score multiplier (for multi-hits): */
249 player_status.score_multiplier = 1;
254 if (isbrick(base.x, base.y) ||
255 isfullbox(base.x, base.y))
257 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
258 World::current()->trybumpbadguy(base.x, base.y - 64);
260 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
262 bumpbrick(base.x, base.y);
263 World::current()->tryemptybox(base.x, base.y, RIGHT);
266 if (isbrick(base.x+ 31, base.y) ||
267 isfullbox(base.x+ 31, base.y))
269 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
270 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
273 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
275 bumpbrick(base.x+ 31, base.y);
276 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
288 /* Make sure jumping is off. */
294 /* ---- DONE HANDLING TUX! --- */
297 skidding_timer.check();
298 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::handle_horizontal_input()
321 float vx = physic.get_velocity_x();
322 float vy = physic.get_velocity_y();
323 float ax = physic.get_acceleration_x();
324 float ay = physic.get_acceleration_y();
327 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
330 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
335 if (input.fire == UP) {
336 ax = dirsign * WALK_ACCELERATION_X;
338 if(vx >= MAX_WALK_XM && dirsign > 0) {
341 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
346 ax = dirsign * RUN_ACCELERATION_X;
348 if(vx >= MAX_RUN_XM && dirsign > 0) {
351 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
357 // we can reach WALK_SPEED without any acceleration
358 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
359 vx = dirsign * WALK_SPEED;
362 // changing directions?
363 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
364 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
365 skidding_timer.start(SKID_TIME);
366 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
373 // we get slower when not pressing any keys
375 if(fabs(vx) < WALK_SPEED) {
379 ax = WALK_ACCELERATION_X * 1.5;
381 ax = WALK_ACCELERATION_X * -1.5;
385 // if we're on ice slow down acceleration or deceleration
386 if (isice(base.x, base.y + base.height))
388 /* the acceleration/deceleration rate on ice is inversely proportional to
389 * the current velocity.
392 // increasing 1 will increase acceleration/deceleration rate
393 // decreasing 1 will decrease acceleration/deceleration rate
394 // must stay above zero, though
395 if (ax != 0) ax *= 1 / fabs(vx);
398 physic.set_velocity(vx, vy);
399 physic.set_acceleration(ax, ay);
403 Player::handle_vertical_input()
409 // jump higher if we are running
410 if (physic.get_velocity_x() > MAX_WALK_XM)
411 physic.set_velocity_y(5.8);
413 physic.set_velocity_y(5.2);
418 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
420 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
423 else if(input.up == UP && jumping)
426 if(physic.get_velocity_y() > 0) {
427 physic.set_velocity_y(0);
433 Player::handle_input()
435 /* Handle horizontal movement: */
436 handle_horizontal_input();
440 if ( input.up == DOWN || (input.up == UP && jumping))
442 handle_vertical_input();
447 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
449 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
452 /* tux animations: */
453 if(!frame_timer.check())
455 frame_timer.start(25);
456 if (input.right == UP && input.left == UP)
463 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
464 (global_frame_counter % 4) == 0)
465 frame_main = (frame_main + 1) % 4;
475 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0)
480 // changing base size confuses collision otherwise
481 old_base = previous_base = base;
483 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0 && on_ground())
488 old_base = previous_base = base;
493 Player::grabdistros()
498 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
499 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
501 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
502 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
506 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
507 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
512 /* Enough distros for a One-up? */
513 if (player_status.distros >= DISTROS_LIFEUP)
515 player_status.distros = player_status.distros - DISTROS_LIFEUP;
516 if(player_status.lives < MAX_LIVES)
517 ++player_status.lives;
518 /*We want to hear the sound even, if MAX_LIVES is reached*/
519 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
526 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
528 if (dying == DYING_SQUISHED)
530 smalltux_gameover->draw(base.x - scroll_x, base.y);
536 if (!skidding_timer.started())
538 if (physic.get_velocity_y() != 0)
541 smalltux_jump_right->draw(base.x - scroll_x, base.y);
543 smalltux_jump_left->draw(base.x - scroll_x, base.y);
547 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
550 smalltux_stand_right->draw( base.x - scroll_x, base.y - 9);
552 smalltux_stand_left->draw( base.x - scroll_x, base.y - 9);
557 smalltux_walk_right->draw(base.x - scroll_x, base.y);
559 smalltux_walk_left->draw(base.x - scroll_x, base.y);
566 smalltux_skid_right->draw(base.x - scroll_x, base.y);
568 smalltux_skid_left->draw(base.x - scroll_x, base.y);
571 if (holding_something && physic.get_velocity_y() == 0)
574 smalltux_grab_right->draw(base.x - scroll_x, base.y);
576 smalltux_grab_left->draw(base.x - scroll_x, base.y);
579 if (invincible_timer.started())
580 smalltux_star->draw(base.x - scroll_x, base.y);
588 if (!skidding_timer.started())
590 if (physic.get_velocity_y() == 0)
592 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
595 largetux_stand_right->draw(base.x - scroll_x, base.y);
597 largetux_stand_left->draw(base.x - scroll_x, base.y);
602 largetux_walk_right->draw(base.x - scroll_x, base.y);
604 largetux_walk_left->draw(base.x - scroll_x, base.y);
610 largetux_jump_right->draw(base.x - scroll_x, base.y);
612 largetux_jump_left->draw(base.x - scroll_x, base.y);
618 largetux_skid_right->draw(base.x - scroll_x - 8, base.y);
620 largetux_skid_left->draw(base.x - scroll_x - 8, base.y);
626 largetux_duck_right->draw(base.x - scroll_x, base.y);
628 largetux_duck_left->draw(base.x - scroll_x, base.y);
633 /* Tux has coffee! */
636 if (!skidding_timer.started())
638 if (!jumping || physic.get_velocity_y() > 0)
641 bigfiretux_right[frame_]->draw(base.x- scroll_x - 8, base.y);
643 bigfiretux_left[frame_]->draw(base.x- scroll_x - 8, base.y);
648 bigfiretux_right_jump->draw(base.x- scroll_x - 8, base.y);
650 bigfiretux_left_jump->draw(base.x- scroll_x - 8, base.y);
656 skidfiretux_right->draw(base.x- scroll_x - 8, base.y);
658 skidfiretux_left->draw(base.x- scroll_x - 8, base.y);
664 duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
666 duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
670 if (holding_something && !duck && physic.get_velocity_y() == 0)
673 largetux_grab_right->draw(base.x - scroll_x, base.y);
675 largetux_grab_left->draw(base.x - scroll_x, base.y);
678 if (invincible_timer.started())
679 largetux_star->draw(base.x - scroll_x, base.y);
685 fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
689 Player::collision(void* p_c_object, int c_object)
691 BadGuy* pbad_c = NULL;
696 pbad_c = (BadGuy*) p_c_object;
698 /* Hurt player if he touches a badguy */
699 if (!pbad_c->dying && !dying &&
700 !safe_timer.started() &&
701 pbad_c->mode != BadGuy::HELD)
703 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN)
705 holding_something = true;
706 pbad_c->mode = BadGuy::HELD;
709 else if (pbad_c->mode == BadGuy::FLAT)
711 // Don't get hurt if we're kicking a flat badguy!
713 else if (pbad_c->mode == BadGuy::KICK)
715 /* Hurt if you get hit by kicked laptop: */
716 if (!invincible_timer.started())
722 pbad_c->dying = DYING_FALLING;
723 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
724 World::current()->add_score(pbad_c->base.x - scroll_x,
726 25 * player_status.score_multiplier);
731 if (!invincible_timer.started())
740 player_status.score_multiplier++;
752 Player::kill(HurtMode mode)
754 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
756 physic.set_velocity_x(0);
758 if (mode == SHRINK && size == BIG)
770 safe_timer.start(TUX_SAFE_TIME);
774 physic.enable_gravity(true);
775 physic.set_acceleration(0, 0);
776 physic.set_velocity(0, 7);
777 dying = DYING_SQUISHED;
788 bool Player::is_dead()
790 if(base.y > screen->h)
796 /* Remove Tux's power ups */
798 Player::remove_powerups()
806 Player::keep_in_bounds()
808 Level* plevel = World::current()->get_level();
810 /* Keep tux in bounds: */
812 { // Lock Tux to the size of the level, so that he doesn't fall of
816 else if (base.x < scroll_x)
821 /* Keep in-bounds, vertically: */
822 if (base.y > screen->h)
827 int scroll_threshold = screen->w/2 - 80;
830 scroll_x += screen->w/2;
831 // Backscrolling for debug mode
832 if (scroll_x < base.x - 80)
833 scroll_x = base.x - 80;
834 else if (scroll_x > base.x + 80)
835 scroll_x = base.x + 80;
836 scroll_x -= screen->w/2;
843 if (base.x > scroll_threshold + scroll_x
844 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
846 // FIXME: Scrolling needs to be handled by a seperate View
847 // class, doing it as a player huck is ugly
849 // Scroll the screen in past center:
850 scroll_x = base.x - scroll_threshold;
852 // Lock the scrolling to the levelsize, so that we don't
853 // scroll over the right border
854 if (scroll_x > 32 * plevel->width - screen->w)
855 scroll_x = 32 * plevel->width - screen->w;