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 Sprite* ducktux_right;
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())
520 cape_right[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
522 cape_left[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
528 if (physic.get_velocity_y() != 0)
531 smalltux_jump_right->draw( base.x - scroll_x, base.y - 10);
533 smalltux_jump_left->draw( base.x - scroll_x, base.y - 10);
537 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
540 smalltux_stand_right->draw( base.x - scroll_x, base.y - 9);
542 smalltux_stand_left->draw( base.x - scroll_x, base.y - 9);
547 tux_right[(global_frame_counter/2) % tux_right.size()]->draw(base.x - scroll_x, base.y - 9);
549 tux_left[(global_frame_counter/2) % tux_left.size()]->draw(base.x - scroll_x, base.y - 9);
555 /* Tux got coffee! */
559 firetux_right[frame_]->draw( base.x- scroll_x, base.y);
563 firetux_left[frame_]->draw( base.x- scroll_x, base.y);
569 if (invincible_timer.started())
571 float capex = base.x + (base.width - bigcape_right[0]->w) / 2;
573 float capey = base.y + (base.height - bigcape_right[0]->h) / 2;
575 /* Draw cape (just not in ducked mode since that looks silly): */
577 bigcape_right[global_frame_counter % 2]->draw(capex, capey);
579 bigcape_left[global_frame_counter % 2]->draw(capex, capey);
586 if (!skidding_timer.started())
588 if (physic.get_velocity_y() == 0)
591 bigtux_right->draw(base.x - scroll_x, base.y);
593 bigtux_left->draw(base.x - scroll_x, base.y);
598 bigtux_right_jump->draw(base.x - scroll_x, base.y);
600 bigtux_left_jump->draw(base.x - scroll_x, base.y);
606 skidtux_right->draw(base.x - scroll_x - 8, base.y);
608 skidtux_left->draw(base.x - scroll_x - 8, base.y);
614 ducktux_right->draw(base.x - scroll_x, base.y);
616 ducktux_left->draw(base.x - scroll_x, base.y);
621 /* Tux has coffee! */
624 if (!skidding_timer.started())
626 if (!jumping || physic.get_velocity_y() > 0)
629 bigfiretux_right[frame_]->draw(base.x- scroll_x - 8, base.y);
631 bigfiretux_left[frame_]->draw(base.x- scroll_x - 8, base.y);
636 bigfiretux_right_jump->draw(base.x- scroll_x - 8, base.y);
638 bigfiretux_left_jump->draw(base.x- scroll_x - 8, base.y);
644 skidfiretux_right->draw(base.x- scroll_x - 8, base.y);
646 skidfiretux_left->draw(base.x- scroll_x - 8, base.y);
652 duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
654 duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
661 fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
665 Player::collision(void* p_c_object, int c_object)
667 BadGuy* pbad_c = NULL;
672 pbad_c = (BadGuy*) p_c_object;
674 /* Hurt player if he touches a badguy */
675 if (!pbad_c->dying && !dying &&
676 !safe_timer.started() &&
677 pbad_c->mode != HELD)
679 if (pbad_c->mode == FLAT && input.fire == DOWN)
684 else if (pbad_c->mode == FLAT)
686 // Don't get hurt if we're kicking a flat badguy!
688 else if (pbad_c->mode == KICK)
690 /* Hurt if you get hit by kicked laptop: */
691 if (!invincible_timer.started())
697 pbad_c->dying = DYING_FALLING;
698 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
699 World::current()->add_score(pbad_c->base.x - scroll_x,
701 25 * player_status.score_multiplier);
706 if (!invincible_timer.started())
715 player_status.score_multiplier++;
727 Player::kill(int mode)
729 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
731 physic.set_velocity(0, physic.get_velocity_y());
733 if (mode == SHRINK && size == BIG)
742 safe_timer.start(TUX_SAFE_TIME);
746 physic.enable_gravity(true);
747 physic.set_acceleration(0, 0);
748 physic.set_velocity(0, 7);
749 dying = DYING_SQUISHED;
760 bool Player::is_dead()
762 if(base.y > screen->h)
768 /* Remove Tux's power ups */
770 Player::remove_powerups()
778 Player::keep_in_bounds()
780 Level* plevel = World::current()->get_level();
782 /* Keep tux in bounds: */
784 { // Lock Tux to the size of the level, so that he doesn't fall of
788 else if (base.x < scroll_x)
793 /* Keep in-bounds, vertically: */
794 if (base.y > screen->h)
799 int scroll_threshold = screen->w/2 - 80;
802 scroll_x += screen->w/2;
803 // Backscrolling for debug mode
804 if (scroll_x < base.x - 80)
805 scroll_x = base.x - 80;
806 else if (scroll_x > base.x + 80)
807 scroll_x = base.x + 80;
808 scroll_x -= screen->w/2;
815 if (base.x > scroll_threshold + scroll_x
816 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
818 // FIXME: Scrolling needs to be handled by a seperate View
819 // class, doing it as a player huck is ugly
821 // Scroll the screen in past center:
822 scroll_x = base.x - scroll_threshold;
824 // Lock the scrolling to the levelsize, so that we don't
825 // scroll over the right border
826 if (scroll_x > 32 * plevel->width - screen->w)
827 scroll_x = 32 * plevel->width - screen->w;