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
24 std::vector<Surface*> tux_right;
25 std::vector<Surface*> tux_left;
26 Surface* smalltux_jump_left;
27 Surface* smalltux_jump_right;
28 Surface* smalltux_stand_left;
29 Surface* smalltux_stand_right;
33 Sprite* bigtux_right_jump;
34 Sprite* bigtux_left_jump;
35 Surface* ducktux_right;
36 Surface* ducktux_left;
37 Surface* skidtux_right;
38 Surface* skidtux_left;
39 Surface* firetux_right[3];
40 Surface* firetux_left[3];
41 Surface* bigfiretux_right[3];
42 Surface* bigfiretux_left[3];
43 Surface* bigfiretux_right_jump;
44 Surface* bigfiretux_left_jump;
45 Surface* duckfiretux_right;
46 Surface* duckfiretux_left;
47 Surface* skidfiretux_right;
48 Surface* skidfiretux_left;
49 Surface* cape_right[2];
50 Surface* cape_left[2];
51 Surface* bigcape_right[2];
52 Surface* bigcape_left[2];
54 void player_input_init(player_input_type* pplayer_input)
56 pplayer_input->down = UP;
57 pplayer_input->fire = UP;
58 pplayer_input->left = UP;
59 pplayer_input->old_fire = UP;
60 pplayer_input->right = UP;
61 pplayer_input->up = UP;
73 // FIXME: Make the start position configurable via the levelfile
78 previous_base = old_base = base;
88 player_input_init(&input);
90 keymap.jump = SDLK_UP;
91 keymap.duck = SDLK_DOWN;
92 keymap.left = SDLK_LEFT;
93 keymap.right = SDLK_RIGHT;
94 keymap.fire = SDLK_LCTRL;
96 invincible_timer.init(true);
97 skidding_timer.init(true);
98 safe_timer.init(true);
99 frame_timer.init(true);
105 Player::key_event(SDLKey key, int state)
107 if(key == keymap.right)
112 else if(key == keymap.left)
117 else if(key == keymap.jump)
122 else if(key == keymap.duck)
127 else if(key == keymap.fire)
137 Player::level_begin()
143 previous_base = old_base = base;
148 player_input_init(&input);
150 invincible_timer.init(true);
151 skidding_timer.init(true);
152 safe_timer.init(true);
153 frame_timer.init(true);
159 Player::action(double frame_ratio)
161 bool jumped_in_solid = false;
163 /* --- HANDLE TUX! --- */
165 if(dying == DYING_NOT)
169 previous_base = base;
171 physic.apply(frame_ratio, base.x, base.y);
172 if(dying == DYING_NOT) {
173 collision_swept_object_map(&old_base, &base);
174 // special exception for cases where we're stuck under tiles after
175 // being ducked. In this case we drift out
176 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
177 && collision_object_map(&base)) {
178 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
179 previous_base = old_base = base;
184 if (dying == DYING_NOT)
191 physic.enable_gravity(true);
195 physic.set_velocity(physic.get_velocity_x(), 0);
196 jumped_in_solid = true;
202 if (physic.get_velocity_y() < 0)
204 base.y = (int)(((int)base.y / 32) * 32);
205 physic.set_velocity(physic.get_velocity_x(), 0);
208 physic.enable_gravity(false);
209 /* Reset score multiplier (for multi-hits): */
210 player_status.score_multiplier = 1;
215 if (isbrick(base.x, base.y) ||
216 isfullbox(base.x, base.y))
218 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
219 World::current()->trybumpbadguy(base.x, base.y - 64);
221 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
223 bumpbrick(base.x, base.y);
224 World::current()->tryemptybox(base.x, base.y, RIGHT);
227 if (isbrick(base.x+ 31, base.y) ||
228 isfullbox(base.x+ 31, base.y))
230 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
231 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
234 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
236 bumpbrick(base.x+ 31, base.y);
237 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
249 /* Make sure jumping is off. */
256 /* ---- DONE HANDLING TUX! --- */
258 /* Handle invincibility timer: */
259 if (get_current_music() == HERRING_MUSIC && !invincible_timer.check())
262 no, we are no more invincible
263 or we were not in invincible mode
264 but are we in hurry ?
267 // FIXME: Move this to gamesession
268 if (GameSession::current()->time_left.get_left() < TIME_WARNING)
270 /* yes, we are in hurry
271 stop the herring_song, prepare to play the correct
274 set_current_music(HURRYUP_MUSIC);
278 set_current_music(LEVEL_MUSIC);
281 /* start playing it */
282 play_current_music();
286 skidding_timer.check();
287 invincible_timer.check();
294 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
295 issolid(base.x + 1, base.y + base.height) ||
296 issolid(base.x + base.width - 1, base.y + base.height) );
300 Player::under_solid()
302 return ( issolid(base.x + base.width / 2, base.y) ||
303 issolid(base.x + 1, base.y) ||
304 issolid(base.x + base.width - 1, base.y) );
308 Player::handle_horizontal_input()
310 float vx = physic.get_velocity_x();
311 float vy = physic.get_velocity_y();
312 float ax = physic.get_acceleration_x();
313 float ay = physic.get_acceleration_y();
316 if(!duck && input.left == DOWN && input.right == UP) {
319 } else if(!duck && input.left == UP && input.right == DOWN) {
324 if (input.fire == UP) {
325 ax = dirsign * WALK_ACCELERATION_X;
327 if(vx >= MAX_WALK_XM && dirsign > 0) {
330 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
335 ax = dirsign * RUN_ACCELERATION_X;
337 if(vx >= MAX_RUN_XM && dirsign > 0) {
340 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
346 // we can reach WALK_SPEED without any acceleration
347 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
348 vx = dirsign * WALK_SPEED;
351 // changing directions?
352 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
353 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
354 skidding_timer.start(SKID_TIME);
355 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
362 // we get slower when not pressing any keys
364 if(fabs(vx) < WALK_SPEED) {
368 ax = WALK_ACCELERATION_X * 1.5;
370 ax = WALK_ACCELERATION_X * -1.5;
374 // if we're on ice slow down acceleration or deceleration
375 if (isice(base.x, base.y + base.height))
377 /* the acceleration/deceleration rate on ice is inversely proportional to
378 * the current velocity.
381 // increasing 1 will increase acceleration/deceleration rate
382 // decreasing 1 will decrease acceleration/deceleration rate
383 // must stay above zero, though
384 if (ax != 0) ax *= 1 / fabs(vx);
387 physic.set_velocity(vx, vy);
388 physic.set_acceleration(ax, ay);
392 Player::handle_vertical_input()
396 if (on_ground() && !duck)
399 physic.set_velocity(physic.get_velocity_x(), 5.5);
403 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
405 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
408 else if(input.up == UP && jumping)
411 if(physic.get_velocity_y() > 0) {
412 physic.set_velocity(physic.get_velocity_x(), 0);
418 Player::handle_input()
420 /* Handle horizontal movement: */
421 handle_horizontal_input();
425 if ( input.up == DOWN || (input.up == UP && jumping))
427 handle_vertical_input();
432 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
434 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
437 /* tux animations: */
438 if(!frame_timer.check())
440 frame_timer.start(25);
441 if (input.right == UP && input.left == UP)
448 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
449 (global_frame_counter % 4) == 0)
450 frame_main = (frame_main + 1) % 4;
460 if (input.down == DOWN && size == BIG && !duck)
465 // changing base size confuses collision otherwise
466 old_base = previous_base = base;
468 else if(input.down == UP && size == BIG && duck)
473 old_base = previous_base = base;
478 Player::grabdistros()
483 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
484 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
486 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
487 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
491 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
492 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
497 /* Enough distros for a One-up? */
498 if (player_status.distros >= DISTROS_LIFEUP)
500 player_status.distros = player_status.distros - DISTROS_LIFEUP;
501 if(player_status.lives < MAX_LIVES)
502 ++player_status.lives;
503 /*We want to hear the sound even, if MAX_LIVES is reached*/
504 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
511 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
515 if (invincible_timer.started())
521 cape_right[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
525 cape_left[global_frame_counter % 2]->draw(
526 base.x- scroll_x, base.y);
533 if (physic.get_velocity_y() != 0)
536 smalltux_jump_right->draw( base.x - scroll_x, base.y - 10);
538 smalltux_jump_left->draw( base.x - scroll_x, base.y - 10);
542 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
545 smalltux_stand_right->draw( base.x - scroll_x, base.y - 9);
547 smalltux_stand_left->draw( base.x - scroll_x, base.y - 9);
552 tux_right[(global_frame_counter/2) % tux_right.size()]->draw(
553 base.x - scroll_x, base.y - 9);
555 tux_left[(global_frame_counter/2) % tux_left.size()]->draw(
556 base.x - scroll_x, base.y - 9);
562 /* Tux got coffee! */
566 firetux_right[frame_]->draw( base.x- scroll_x, base.y);
570 firetux_left[frame_]->draw( base.x- scroll_x, base.y);
576 if (invincible_timer.started())
578 float capex = base.x + (base.width - bigcape_right[0]->w) / 2;
580 float capey = base.y + (base.height - bigcape_right[0]->h) / 2;
582 /* Draw cape (just not in ducked mode since that looks silly): */
585 bigcape_right[global_frame_counter % 2]->draw(
590 bigcape_left[global_frame_counter % 2]->draw(
599 if (!skidding_timer.started())
601 if (physic.get_velocity_y() == 0)
604 bigtux_right->draw(base.x- scroll_x - 8, base.y);
606 bigtux_left->draw(base.x- scroll_x - 8, base.y);
611 bigtux_right_jump->draw(base.x- scroll_x - 8, base.y);
613 bigtux_left_jump->draw(base.x- scroll_x - 8, base.y);
619 skidtux_right->draw(base.x- scroll_x - 8, base.y);
621 skidtux_left->draw(base.x- scroll_x - 8, base.y);
628 ducktux_right->draw( base.x- scroll_x - 8, base.y - 16);
632 ducktux_left->draw( base.x- scroll_x - 8, base.y - 16);
638 /* Tux has coffee! */
641 if (!skidding_timer.started())
643 if (!jumping || physic.get_velocity_y() > 0)
647 bigfiretux_right[frame_]->draw(
648 base.x- scroll_x - 8, base.y);
652 bigfiretux_left[frame_]->draw(
653 base.x- scroll_x - 8, base.y);
660 bigfiretux_right_jump->draw(
661 base.x- scroll_x - 8, base.y);
665 bigfiretux_left_jump->draw(
666 base.x- scroll_x - 8, base.y);
674 skidfiretux_right->draw(
675 base.x- scroll_x - 8, base.y);
679 skidfiretux_left->draw(
680 base.x- scroll_x - 8, base.y);
688 duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
692 duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
700 fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
704 Player::collision(void* p_c_object, int c_object)
706 BadGuy* pbad_c = NULL;
711 pbad_c = (BadGuy*) p_c_object;
713 /* Hurt player if he touches a badguy */
714 if (!pbad_c->dying && !dying &&
715 !safe_timer.started() &&
716 pbad_c->mode != HELD)
718 if (pbad_c->mode == FLAT && input.fire == DOWN)
723 else if (pbad_c->mode == FLAT)
725 // Don't get hurt if we're kicking a flat badguy!
727 else if (pbad_c->mode == KICK)
729 /* Hurt if you get hit by kicked laptop: */
730 if (!invincible_timer.started())
736 pbad_c->dying = DYING_FALLING;
737 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
738 World::current()->add_score(pbad_c->base.x - scroll_x,
740 25 * player_status.score_multiplier);
745 if (!invincible_timer.started())
754 player_status.score_multiplier++;
766 Player::kill(int mode)
768 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
770 physic.set_velocity(0, physic.get_velocity_y());
772 if (mode == SHRINK && size == BIG)
781 safe_timer.start(TUX_SAFE_TIME);
785 physic.enable_gravity(true);
786 physic.set_acceleration(0, 0);
787 physic.set_velocity(0, 7);
788 dying = DYING_SQUISHED;
799 bool Player::is_dead()
801 if(base.y > screen->h)
807 /* Remove Tux's power ups */
809 Player::remove_powerups()
817 Player::keep_in_bounds()
819 Level* plevel = World::current()->get_level();
821 /* Keep tux in bounds: */
823 { // Lock Tux to the size of the level, so that he doesn't fall of
827 else if (base.x < scroll_x)
832 /* Keep in-bounds, vertically: */
833 if (base.y > screen->h)
838 int scroll_threshold = screen->w/2 - 80;
841 scroll_x += screen->w/2;
842 // Backscrolling for debug mode
843 if (scroll_x < base.x - 80)
844 scroll_x = base.x - 80;
845 else if (scroll_x > base.x + 80)
846 scroll_x = base.x + 80;
847 scroll_x -= screen->w/2;
854 if (base.x > scroll_threshold + scroll_x
855 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
857 // FIXME: Scrolling needs to be handled by a seperate View
858 // class, doing it as a player huck is ugly
860 // Scroll the screen in past center:
861 scroll_x = base.x - scroll_threshold;
863 // Lock the scrolling to the levelsize, so that we don't
864 // scroll over the right border
865 if (scroll_x > 32 * plevel->width - screen->w)
866 scroll_x = 32 * plevel->width - screen->w;