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 std::vector<Surface*> tux_right;
33 std::vector<Surface*> tux_left;
34 Surface* smalltux_jump_left;
35 Surface* smalltux_jump_right;
36 Surface* smalltux_stand_left;
37 Surface* smalltux_stand_right;
41 Sprite* bigtux_right_jump;
42 Sprite* bigtux_left_jump;
43 Sprite* ducktux_right;
45 Surface* skidtux_right;
46 Surface* skidtux_left;
47 Surface* firetux_right[3];
48 Surface* firetux_left[3];
49 Surface* bigfiretux_right[3];
50 Surface* bigfiretux_left[3];
51 Surface* bigfiretux_right_jump;
52 Surface* bigfiretux_left_jump;
53 Surface* duckfiretux_right;
54 Surface* duckfiretux_left;
55 Surface* skidfiretux_right;
56 Surface* skidfiretux_left;
57 Surface* cape_right[2];
58 Surface* cape_left[2];
59 Surface* bigcape_right[2];
60 Surface* bigcape_left[2];
64 PlayerKeymap::PlayerKeymap()
66 keymap.jump = SDLK_UP;
67 keymap.duck = SDLK_DOWN;
68 keymap.left = SDLK_LEFT;
69 keymap.right = SDLK_RIGHT;
70 keymap.fire = SDLK_LCTRL;
73 void player_input_init(player_input_type* pplayer_input)
75 pplayer_input->down = UP;
76 pplayer_input->fire = UP;
77 pplayer_input->left = UP;
78 pplayer_input->old_fire = UP;
79 pplayer_input->right = UP;
80 pplayer_input->up = UP;
86 Level* plevel = World::current()->get_level();
94 base.x = plevel->start_pos_x;
95 base.y = plevel->start_pos_y;
98 previous_base = old_base = base;
108 player_input_init(&input);
110 invincible_timer.init(true);
111 skidding_timer.init(true);
112 safe_timer.init(true);
113 frame_timer.init(true);
119 Player::key_event(SDLKey key, int state)
121 if(key == keymap.right)
126 else if(key == keymap.left)
131 else if(key == keymap.jump)
136 else if(key == keymap.duck)
141 else if(key == keymap.fire)
151 Player::level_begin()
157 previous_base = old_base = base;
162 player_input_init(&input);
164 invincible_timer.init(true);
165 skidding_timer.init(true);
166 safe_timer.init(true);
167 frame_timer.init(true);
173 Player::action(double frame_ratio)
175 bool jumped_in_solid = false;
177 /* --- HANDLE TUX! --- */
179 if(dying == DYING_NOT)
183 previous_base = base;
185 physic.apply(frame_ratio, base.x, base.y);
186 if(dying == DYING_NOT) {
187 collision_swept_object_map(&old_base, &base);
188 // special exception for cases where we're stuck under tiles after
189 // being ducked. In this case we drift out
190 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
191 && collision_object_map(&base)) {
192 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
193 previous_base = old_base = base;
198 if (dying == DYING_NOT)
205 physic.enable_gravity(true);
209 physic.set_velocity_y(0);
210 jumped_in_solid = true;
216 if (physic.get_velocity_y() < 0)
218 base.y = (int)(((int)base.y / 32) * 32);
219 physic.set_velocity_y(0);
222 physic.enable_gravity(false);
223 /* Reset score multiplier (for multi-hits): */
224 player_status.score_multiplier = 1;
229 if (isbrick(base.x, base.y) ||
230 isfullbox(base.x, base.y))
232 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
233 World::current()->trybumpbadguy(base.x, base.y - 64);
235 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
237 bumpbrick(base.x, base.y);
238 World::current()->tryemptybox(base.x, base.y, RIGHT);
241 if (isbrick(base.x+ 31, base.y) ||
242 isfullbox(base.x+ 31, base.y))
244 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
245 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
248 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
250 bumpbrick(base.x+ 31, base.y);
251 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
263 /* Make sure jumping is off. */
270 /* ---- DONE HANDLING TUX! --- */
272 /* Handle invincibility timer: */
273 if (get_current_music() == HERRING_MUSIC && !invincible_timer.check())
276 no, we are no more invincible
277 or we were not in invincible mode
278 but are we in hurry ?
281 // FIXME: Move this to gamesession
282 if (GameSession::current()->time_left.get_left() < TIME_WARNING)
284 /* yes, we are in hurry
285 stop the herring_song, prepare to play the correct
288 set_current_music(HURRYUP_MUSIC);
292 set_current_music(LEVEL_MUSIC);
295 /* start playing it */
296 play_current_music();
300 skidding_timer.check();
301 invincible_timer.check();
308 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
309 issolid(base.x + 1, base.y + base.height) ||
310 issolid(base.x + base.width - 1, base.y + base.height) );
314 Player::under_solid()
316 return ( issolid(base.x + base.width / 2, base.y) ||
317 issolid(base.x + 1, base.y) ||
318 issolid(base.x + base.width - 1, base.y) );
322 Player::handle_horizontal_input()
324 float vx = physic.get_velocity_x();
325 float vy = physic.get_velocity_y();
326 float ax = physic.get_acceleration_x();
327 float ay = physic.get_acceleration_y();
330 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
333 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
338 if (input.fire == UP) {
339 ax = dirsign * WALK_ACCELERATION_X;
341 if(vx >= MAX_WALK_XM && dirsign > 0) {
344 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
349 ax = dirsign * RUN_ACCELERATION_X;
351 if(vx >= MAX_RUN_XM && dirsign > 0) {
354 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
360 // we can reach WALK_SPEED without any acceleration
361 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
362 vx = dirsign * WALK_SPEED;
365 // changing directions?
366 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
367 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
368 skidding_timer.start(SKID_TIME);
369 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
376 // we get slower when not pressing any keys
378 if(fabs(vx) < WALK_SPEED) {
382 ax = WALK_ACCELERATION_X * 1.5;
384 ax = WALK_ACCELERATION_X * -1.5;
388 // if we're on ice slow down acceleration or deceleration
389 if (isice(base.x, base.y + base.height))
391 /* the acceleration/deceleration rate on ice is inversely proportional to
392 * the current velocity.
395 // increasing 1 will increase acceleration/deceleration rate
396 // decreasing 1 will decrease acceleration/deceleration rate
397 // must stay above zero, though
398 if (ax != 0) ax *= 1 / fabs(vx);
401 physic.set_velocity(vx, vy);
402 physic.set_acceleration(ax, ay);
406 Player::handle_vertical_input()
413 if (physic.get_velocity_x() > MAX_WALK_XM)
414 physic.set_velocity_y(5.8);
416 physic.set_velocity_y(5.2);
421 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
423 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
426 else if(input.up == UP && jumping)
429 if(physic.get_velocity_y() > 0) {
430 physic.set_velocity_y(0);
436 Player::handle_input()
438 /* Handle horizontal movement: */
439 handle_horizontal_input();
443 if ( input.up == DOWN || (input.up == UP && jumping))
445 handle_vertical_input();
450 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
452 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
455 /* tux animations: */
456 if(!frame_timer.check())
458 frame_timer.start(25);
459 if (input.right == UP && input.left == UP)
466 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
467 (global_frame_counter % 4) == 0)
468 frame_main = (frame_main + 1) % 4;
478 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0)
483 // changing base size confuses collision otherwise
484 old_base = previous_base = base;
486 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0)
491 old_base = previous_base = base;
496 Player::grabdistros()
501 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
502 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
504 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
505 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
509 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
510 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
515 /* Enough distros for a One-up? */
516 if (player_status.distros >= DISTROS_LIFEUP)
518 player_status.distros = player_status.distros - DISTROS_LIFEUP;
519 if(player_status.lives < MAX_LIVES)
520 ++player_status.lives;
521 /*We want to hear the sound even, if MAX_LIVES is reached*/
522 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
529 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
533 if (invincible_timer.started())
538 cape_right[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
540 cape_left[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
546 if (physic.get_velocity_y() != 0)
549 smalltux_jump_right->draw( base.x - scroll_x, base.y - 10);
551 smalltux_jump_left->draw( base.x - scroll_x, base.y - 10);
555 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
558 smalltux_stand_right->draw( base.x - scroll_x, base.y - 9);
560 smalltux_stand_left->draw( base.x - scroll_x, base.y - 9);
565 tux_right[(global_frame_counter/2) % tux_right.size()]->draw(base.x - scroll_x, base.y - 9);
567 tux_left[(global_frame_counter/2) % tux_left.size()]->draw(base.x - scroll_x, base.y - 9);
573 /* Tux got coffee! */
577 firetux_right[frame_]->draw( base.x- scroll_x, base.y);
581 firetux_left[frame_]->draw( base.x- scroll_x, base.y);
587 if (invincible_timer.started())
589 float capex = base.x + (base.width - bigcape_right[0]->w) / 2;
591 float capey = base.y + (base.height - bigcape_right[0]->h) / 2;
593 /* Draw cape (just not in ducked mode since that looks silly): */
595 bigcape_right[global_frame_counter % 2]->draw(capex, capey);
597 bigcape_left[global_frame_counter % 2]->draw(capex, capey);
604 if (!skidding_timer.started())
606 if (physic.get_velocity_y() == 0)
609 bigtux_right->draw(base.x - scroll_x, base.y);
611 bigtux_left->draw(base.x - scroll_x, base.y);
616 bigtux_right_jump->draw(base.x - scroll_x, base.y);
618 bigtux_left_jump->draw(base.x - scroll_x, base.y);
624 skidtux_right->draw(base.x - scroll_x - 8, base.y);
626 skidtux_left->draw(base.x - scroll_x - 8, base.y);
632 ducktux_right->draw(base.x - scroll_x, base.y);
634 ducktux_left->draw(base.x - scroll_x, base.y);
639 /* Tux has coffee! */
642 if (!skidding_timer.started())
644 if (!jumping || physic.get_velocity_y() > 0)
647 bigfiretux_right[frame_]->draw(base.x- scroll_x - 8, base.y);
649 bigfiretux_left[frame_]->draw(base.x- scroll_x - 8, base.y);
654 bigfiretux_right_jump->draw(base.x- scroll_x - 8, base.y);
656 bigfiretux_left_jump->draw(base.x- scroll_x - 8, base.y);
662 skidfiretux_right->draw(base.x- scroll_x - 8, base.y);
664 skidfiretux_left->draw(base.x- scroll_x - 8, base.y);
670 duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
672 duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
679 fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
683 Player::collision(void* p_c_object, int c_object)
685 BadGuy* pbad_c = NULL;
690 pbad_c = (BadGuy*) p_c_object;
692 /* Hurt player if he touches a badguy */
693 if (!pbad_c->dying && !dying &&
694 !safe_timer.started() &&
695 pbad_c->mode != HELD)
697 if (pbad_c->mode == FLAT && input.fire == DOWN)
702 else if (pbad_c->mode == FLAT)
704 // Don't get hurt if we're kicking a flat badguy!
706 else if (pbad_c->mode == KICK)
708 /* Hurt if you get hit by kicked laptop: */
709 if (!invincible_timer.started())
715 pbad_c->dying = DYING_FALLING;
716 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
717 World::current()->add_score(pbad_c->base.x - scroll_x,
719 25 * player_status.score_multiplier);
724 if (!invincible_timer.started())
733 player_status.score_multiplier++;
745 Player::kill(int mode)
747 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
749 physic.set_velocity(0, physic.get_velocity_y());
751 if (mode == SHRINK && size == BIG)
760 safe_timer.start(TUX_SAFE_TIME);
764 physic.enable_gravity(true);
765 physic.set_acceleration(0, 0);
766 physic.set_velocity(0, 7);
767 dying = DYING_SQUISHED;
778 bool Player::is_dead()
780 if(base.y > screen->h)
786 /* Remove Tux's power ups */
788 Player::remove_powerups()
796 Player::keep_in_bounds()
798 Level* plevel = World::current()->get_level();
800 /* Keep tux in bounds: */
802 { // Lock Tux to the size of the level, so that he doesn't fall of
806 else if (base.x < scroll_x)
811 /* Keep in-bounds, vertically: */
812 if (base.y > screen->h)
817 int scroll_threshold = screen->w/2 - 80;
820 scroll_x += screen->w/2;
821 // Backscrolling for debug mode
822 if (scroll_x < base.x - 80)
823 scroll_x = base.x - 80;
824 else if (scroll_x > base.x + 80)
825 scroll_x = base.x + 80;
826 scroll_x -= screen->w/2;
833 if (base.x > scroll_threshold + scroll_x
834 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
836 // FIXME: Scrolling needs to be handled by a seperate View
837 // class, doing it as a player huck is ugly
839 // Scroll the screen in past center:
840 scroll_x = base.x - scroll_threshold;
842 // Lock the scrolling to the levelsize, so that we don't
843 // scroll over the right border
844 if (scroll_x > 32 * plevel->width - screen->w)
845 scroll_x = 32 * plevel->width - screen->w;