2 // C Implementation: player/tux
7 // Author: Tobias Glaesser <tobi.web@gmx.de> & Bill Kendrick, (C) 2004
9 // Copyright: See COPYING file that comes with this distribution
23 std::vector<Surface*> tux_right;
24 std::vector<Surface*> tux_left;
25 Surface* smalltux_jump_left;
26 Surface* smalltux_jump_right;
27 Surface* smalltux_stand_left;
28 Surface* smalltux_stand_right;
30 Surface* bigtux_right[3];
31 Surface* bigtux_left[3];
32 Surface* bigtux_right_jump;
33 Surface* bigtux_left_jump;
34 Surface* ducktux_right;
35 Surface* ducktux_left;
36 Surface* skidtux_right;
37 Surface* skidtux_left;
38 Surface* firetux_right[3];
39 Surface* firetux_left[3];
40 Surface* bigfiretux_right[3];
41 Surface* bigfiretux_left[3];
42 Surface* bigfiretux_right_jump;
43 Surface* bigfiretux_left_jump;
44 Surface* duckfiretux_right;
45 Surface* duckfiretux_left;
46 Surface* skidfiretux_right;
47 Surface* skidfiretux_left;
48 Surface* cape_right[2];
49 Surface* cape_left[2];
50 Surface* bigcape_right[2];
51 Surface* bigcape_left[2];
53 void player_input_init(player_input_type* pplayer_input)
55 pplayer_input->down = UP;
56 pplayer_input->fire = UP;
57 pplayer_input->left = UP;
58 pplayer_input->old_fire = UP;
59 pplayer_input->right = UP;
60 pplayer_input->up = UP;
72 // FIXME: Make the start position configurable via the levelfile
77 previous_base = old_base = base;
87 player_input_init(&input);
89 keymap.jump = SDLK_UP;
90 keymap.duck = SDLK_DOWN;
91 keymap.left = SDLK_LEFT;
92 keymap.right = SDLK_RIGHT;
93 keymap.fire = SDLK_LCTRL;
95 invincible_timer.init(true);
96 skidding_timer.init(true);
97 safe_timer.init(true);
98 frame_timer.init(true);
104 Player::key_event(SDLKey key, int state)
106 if(key == keymap.right)
111 else if(key == keymap.left)
116 else if(key == keymap.jump)
121 else if(key == keymap.duck)
126 else if(key == keymap.fire)
136 Player::level_begin()
142 previous_base = old_base = base;
147 player_input_init(&input);
149 invincible_timer.init(true);
150 skidding_timer.init(true);
151 safe_timer.init(true);
152 frame_timer.init(true);
158 Player::action(double frame_ratio)
160 bool jumped_in_solid = false;
162 /* --- HANDLE TUX! --- */
164 if(dying == DYING_NOT)
168 previous_base = base;
170 physic.apply(frame_ratio, base.x, base.y);
171 if(dying == DYING_NOT) {
172 collision_swept_object_map(&old_base, &base);
173 // special exception for cases where we're stuck under tiles after
174 // being ducked. In this case we drift out
175 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
176 && collision_object_map(&base)) {
177 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
178 previous_base = old_base = base;
183 if (dying == DYING_NOT)
190 physic.enable_gravity(true);
194 physic.set_velocity(physic.get_velocity_x(), 0);
195 jumped_in_solid = true;
201 if (physic.get_velocity_y() < 0)
203 base.y = (int)(((int)base.y / 32) * 32);
204 physic.set_velocity(physic.get_velocity_x(), 0);
207 physic.enable_gravity(false);
208 /* Reset score multiplier (for multi-hits): */
209 player_status.score_multiplier = 1;
214 if (isbrick(base.x, base.y) ||
215 isfullbox(base.x, base.y))
217 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
218 World::current()->trybumpbadguy(base.x, base.y - 64);
220 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
222 bumpbrick(base.x, base.y);
223 World::current()->tryemptybox(base.x, base.y, RIGHT);
226 if (isbrick(base.x+ 31, base.y) ||
227 isfullbox(base.x+ 31, base.y))
229 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
230 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
233 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
235 bumpbrick(base.x+ 31, base.y);
236 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
248 /* Make sure jumping is off. */
255 /* ---- DONE HANDLING TUX! --- */
257 /* Handle invincibility timer: */
258 if (get_current_music() == HERRING_MUSIC && !invincible_timer.check())
261 no, we are no more invincible
262 or we were not in invincible mode
263 but are we in hurry ?
266 // FIXME: Move this to gamesession
267 if (GameSession::current()->time_left.get_left() < TIME_WARNING)
269 /* yes, we are in hurry
270 stop the herring_song, prepare to play the correct
273 set_current_music(HURRYUP_MUSIC);
277 set_current_music(LEVEL_MUSIC);
280 /* start playing it */
281 play_current_music();
285 if (base.x >= World::current()->get_level()->endpos
286 && World::current()->get_level()->endpos != 0)
288 player_status.next_level = 1;
292 skidding_timer.check();
293 invincible_timer.check();
300 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
301 issolid(base.x + 1, base.y + base.height) ||
302 issolid(base.x + base.width - 1, base.y + base.height) );
306 Player::under_solid()
308 return ( issolid(base.x + base.width / 2, base.y) ||
309 issolid(base.x + 1, base.y) ||
310 issolid(base.x + base.width - 1, base.y) );
314 Player::handle_horizontal_input()
316 float vx = physic.get_velocity_x();
317 float vy = physic.get_velocity_y();
318 float ax = physic.get_acceleration_x();
319 float ay = physic.get_acceleration_y();
322 if(!duck && input.left == DOWN && input.right == UP) {
325 } else if(!duck && input.left == UP && input.right == DOWN) {
330 if (input.fire == UP) {
331 ax = dirsign * WALK_ACCELERATION_X;
333 if(vx >= MAX_WALK_XM && dirsign > 0) {
336 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
341 ax = dirsign * RUN_ACCELERATION_X;
343 if(vx >= MAX_RUN_XM && dirsign > 0) {
346 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
352 // we can reach WALK_SPEED without any acceleration
353 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
354 vx = dirsign * WALK_SPEED;
357 // changing directions?
358 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
359 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
360 skidding_timer.start(SKID_TIME);
361 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
368 // we get slower when not pressing any keys
370 if(fabs(vx) < WALK_SPEED) {
374 ax = WALK_ACCELERATION_X * 1.5;
376 ax = WALK_ACCELERATION_X * -1.5;
380 // if we're on ice slow down acceleration or deceleration
381 if (isice(base.x, base.y + base.height))
383 /* the acceleration/deceleration rate on ice is inversely proportional to
384 * the current velocity.
387 // increasing 1 will increase acceleration/deceleration rate
388 // decreasing 1 will decrease acceleration/deceleration rate
389 // must stay above zero, though
390 if (ax != 0) ax *= 1 / fabs(vx);
393 physic.set_velocity(vx, vy);
394 physic.set_acceleration(ax, ay);
398 Player::handle_vertical_input()
402 if (on_ground() && !duck)
405 physic.set_velocity(physic.get_velocity_x(), 5.5);
409 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
411 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
414 else if(input.up == UP && jumping)
417 if(physic.get_velocity_y() > 0) {
418 physic.set_velocity(physic.get_velocity_x(), 0);
424 Player::handle_input()
426 /* Handle horizontal movement: */
427 handle_horizontal_input();
431 if ( input.up == DOWN || (input.up == UP && jumping))
433 handle_vertical_input();
438 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
440 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
443 /* tux animations: */
444 if(!frame_timer.check())
446 frame_timer.start(25);
447 if (input.right == UP && input.left == UP)
454 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
455 (global_frame_counter % 4) == 0)
456 frame_main = (frame_main + 1) % 4;
466 if (input.down == DOWN && size == BIG && !duck)
471 // changing base size confuses collision otherwise
472 old_base = previous_base = base;
474 else if(input.down == UP && size == BIG && duck)
479 old_base = previous_base = base;
484 Player::grabdistros()
489 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
490 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
492 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
493 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
497 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
498 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
503 /* Enough distros for a One-up? */
504 if (player_status.distros >= DISTROS_LIFEUP)
506 player_status.distros = player_status.distros - DISTROS_LIFEUP;
507 if(player_status.lives < MAX_LIVES)
508 ++player_status.lives;
509 /*We want to hear the sound even, if MAX_LIVES is reached*/
510 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
517 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
521 if (invincible_timer.started())
527 cape_right[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
531 cape_left[global_frame_counter % 2]->draw(
532 base.x- scroll_x, base.y);
539 if (physic.get_velocity_y() != 0)
542 smalltux_jump_right->draw( base.x - scroll_x, base.y - 10);
544 smalltux_jump_left->draw( base.x - scroll_x, base.y - 10);
548 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
551 smalltux_stand_right->draw( base.x - scroll_x, base.y - 9);
553 smalltux_stand_left->draw( base.x - scroll_x, base.y - 9);
558 tux_right[(global_frame_counter/2) % tux_right.size()]->draw(
559 base.x - scroll_x, base.y - 9);
561 tux_left[(global_frame_counter/2) % tux_left.size()]->draw(
562 base.x - scroll_x, base.y - 9);
568 /* Tux got coffee! */
572 firetux_right[frame_]->draw( base.x- scroll_x, base.y);
576 firetux_left[frame_]->draw( base.x- scroll_x, base.y);
582 if (invincible_timer.started())
584 float capex = base.x + (base.width - bigcape_right[0]->w) / 2;
586 float capey = base.y + (base.height - bigcape_right[0]->h) / 2;
588 /* Draw cape (just not in ducked mode since that looks silly): */
591 bigcape_right[global_frame_counter % 2]->draw(
596 bigcape_left[global_frame_counter % 2]->draw(
605 if (!skidding_timer.started())
607 if (!jumping || physic.get_velocity_y() > 0)
611 bigtux_right[frame_]->draw(
612 base.x- scroll_x - 8, base.y);
616 bigtux_left[frame_]->draw(
617 base.x- scroll_x - 8, base.y);
624 bigtux_right_jump->draw(
625 base.x- scroll_x - 8, base.y);
629 bigtux_left_jump->draw(
630 base.x- scroll_x - 8, base.y);
639 base.x- scroll_x - 8, base.y);
644 base.x- scroll_x - 8, base.y);
652 ducktux_right->draw( base.x- scroll_x - 8, base.y - 16);
656 ducktux_left->draw( base.x- scroll_x - 8, base.y - 16);
662 /* Tux has coffee! */
665 if (!skidding_timer.started())
667 if (!jumping || physic.get_velocity_y() > 0)
671 bigfiretux_right[frame_]->draw(
672 base.x- scroll_x - 8, base.y);
676 bigfiretux_left[frame_]->draw(
677 base.x- scroll_x - 8, base.y);
684 bigfiretux_right_jump->draw(
685 base.x- scroll_x - 8, base.y);
689 bigfiretux_left_jump->draw(
690 base.x- scroll_x - 8, base.y);
698 skidfiretux_right->draw(
699 base.x- scroll_x - 8, base.y);
703 skidfiretux_left->draw(
704 base.x- scroll_x - 8, base.y);
712 duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
716 duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
724 fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
728 Player::collision(void* p_c_object, int c_object)
730 BadGuy* pbad_c = NULL;
735 pbad_c = (BadGuy*) p_c_object;
736 /* Hurt the player if he just touched it: */
738 if (!pbad_c->dying && !dying &&
739 !safe_timer.started() &&
740 pbad_c->mode != HELD)
742 if (pbad_c->mode == FLAT && input.fire == DOWN)
747 else if (pbad_c->mode == KICK)
749 if (base.y < pbad_c->base.y - 16)
751 /* Step on (stop being kicked) */
754 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
758 /* Hurt if you get hit by kicked laptop: */
759 if (!invincible_timer.started())
765 pbad_c->dying = DYING_FALLING;
766 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
767 World::current()->add_score(pbad_c->base.x - scroll_x,
769 25 * player_status.score_multiplier);
775 if (!invincible_timer.started())
784 player_status.score_multiplier++;
796 Player::kill(int mode)
798 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
800 physic.set_velocity(0, physic.get_velocity_y());
802 if (mode == SHRINK && size == BIG)
811 safe_timer.start(TUX_SAFE_TIME);
815 physic.enable_gravity(true);
816 physic.set_acceleration(0, 0);
817 physic.set_velocity(0, 7);
818 dying = DYING_SQUISHED;
827 --player_status.lives;
832 bool Player::is_dead()
834 if(base.y > screen->h)
840 /* Remove Tux's power ups */
842 Player::remove_powerups()
850 Player::keep_in_bounds()
852 Level* plevel = World::current()->get_level();
854 /* Keep tux in bounds: */
856 { // Lock Tux to the size of the level, so that he doesn't fall of
860 else if (base.x < scroll_x)
865 /* Keep in-bounds, vertically: */
866 if (base.y > screen->h)
871 int scroll_threshold = screen->w/2 - 80;
874 scroll_x += screen->w/2;
875 // Backscrolling for debug mode
876 if (scroll_x < base.x - 80)
877 scroll_x = base.x - 80;
878 else if (scroll_x > base.x + 80)
879 scroll_x = base.x + 80;
880 scroll_x -= screen->w/2;
887 if (base.x > scroll_threshold + scroll_x
888 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
890 // FIXME: Scrolling needs to be handled by a seperate View
891 // class, doing it as a player huck is ugly
893 // Scroll the screen in past center:
894 scroll_x = base.x - scroll_threshold;
896 // Lock the scrolling to the levelsize, so that we don't
897 // scroll over the right border
898 if (scroll_x > 32 * plevel->width - screen->w)
899 scroll_x = 32 * plevel->width - screen->w;