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 skidding_timer.check();
286 invincible_timer.check();
293 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
294 issolid(base.x + 1, base.y + base.height) ||
295 issolid(base.x + base.width - 1, base.y + base.height) );
299 Player::under_solid()
301 return ( issolid(base.x + base.width / 2, base.y) ||
302 issolid(base.x + 1, base.y) ||
303 issolid(base.x + base.width - 1, base.y) );
307 Player::handle_horizontal_input()
309 float vx = physic.get_velocity_x();
310 float vy = physic.get_velocity_y();
311 float ax = physic.get_acceleration_x();
312 float ay = physic.get_acceleration_y();
315 if(!duck && input.left == DOWN && input.right == UP) {
318 } else if(!duck && input.left == UP && input.right == DOWN) {
323 if (input.fire == UP) {
324 ax = dirsign * WALK_ACCELERATION_X;
326 if(vx >= MAX_WALK_XM && dirsign > 0) {
329 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
334 ax = dirsign * RUN_ACCELERATION_X;
336 if(vx >= MAX_RUN_XM && dirsign > 0) {
339 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
345 // we can reach WALK_SPEED without any acceleration
346 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
347 vx = dirsign * WALK_SPEED;
350 // changing directions?
351 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
352 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
353 skidding_timer.start(SKID_TIME);
354 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
361 // we get slower when not pressing any keys
363 if(fabs(vx) < WALK_SPEED) {
367 ax = WALK_ACCELERATION_X * 1.5;
369 ax = WALK_ACCELERATION_X * -1.5;
373 // if we're on ice slow down acceleration or deceleration
374 if (isice(base.x, base.y + base.height))
376 /* the acceleration/deceleration rate on ice is inversely proportional to
377 * the current velocity.
380 // increasing 1 will increase acceleration/deceleration rate
381 // decreasing 1 will decrease acceleration/deceleration rate
382 // must stay above zero, though
383 if (ax != 0) ax *= 1 / fabs(vx);
386 physic.set_velocity(vx, vy);
387 physic.set_acceleration(ax, ay);
391 Player::handle_vertical_input()
395 if (on_ground() && !duck)
398 physic.set_velocity(physic.get_velocity_x(), 5.5);
402 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
404 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
407 else if(input.up == UP && jumping)
410 if(physic.get_velocity_y() > 0) {
411 physic.set_velocity(physic.get_velocity_x(), 0);
417 Player::handle_input()
419 /* Handle horizontal movement: */
420 handle_horizontal_input();
424 if ( input.up == DOWN || (input.up == UP && jumping))
426 handle_vertical_input();
431 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
433 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
436 /* tux animations: */
437 if(!frame_timer.check())
439 frame_timer.start(25);
440 if (input.right == UP && input.left == UP)
447 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
448 (global_frame_counter % 4) == 0)
449 frame_main = (frame_main + 1) % 4;
459 if (input.down == DOWN && size == BIG && !duck)
464 // changing base size confuses collision otherwise
465 old_base = previous_base = base;
467 else if(input.down == UP && size == BIG && duck)
472 old_base = previous_base = base;
477 Player::grabdistros()
482 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
483 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
485 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
486 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
490 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
491 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
496 /* Enough distros for a One-up? */
497 if (player_status.distros >= DISTROS_LIFEUP)
499 player_status.distros = player_status.distros - DISTROS_LIFEUP;
500 if(player_status.lives < MAX_LIVES)
501 ++player_status.lives;
502 /*We want to hear the sound even, if MAX_LIVES is reached*/
503 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
510 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
514 if (invincible_timer.started())
520 cape_right[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
524 cape_left[global_frame_counter % 2]->draw(
525 base.x- scroll_x, base.y);
532 if (physic.get_velocity_y() != 0)
535 smalltux_jump_right->draw( base.x - scroll_x, base.y - 10);
537 smalltux_jump_left->draw( base.x - scroll_x, base.y - 10);
541 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
544 smalltux_stand_right->draw( base.x - scroll_x, base.y - 9);
546 smalltux_stand_left->draw( base.x - scroll_x, base.y - 9);
551 tux_right[(global_frame_counter/2) % tux_right.size()]->draw(
552 base.x - scroll_x, base.y - 9);
554 tux_left[(global_frame_counter/2) % tux_left.size()]->draw(
555 base.x - scroll_x, base.y - 9);
561 /* Tux got coffee! */
565 firetux_right[frame_]->draw( base.x- scroll_x, base.y);
569 firetux_left[frame_]->draw( base.x- scroll_x, base.y);
575 if (invincible_timer.started())
577 float capex = base.x + (base.width - bigcape_right[0]->w) / 2;
579 float capey = base.y + (base.height - bigcape_right[0]->h) / 2;
581 /* Draw cape (just not in ducked mode since that looks silly): */
584 bigcape_right[global_frame_counter % 2]->draw(
589 bigcape_left[global_frame_counter % 2]->draw(
598 if (!skidding_timer.started())
600 if (!jumping || physic.get_velocity_y() > 0)
604 bigtux_right[frame_]->draw(
605 base.x- scroll_x - 8, base.y);
609 bigtux_left[frame_]->draw(
610 base.x- scroll_x - 8, base.y);
617 bigtux_right_jump->draw(
618 base.x- scroll_x - 8, base.y);
622 bigtux_left_jump->draw(
623 base.x- scroll_x - 8, base.y);
632 base.x- scroll_x - 8, base.y);
637 base.x- scroll_x - 8, base.y);
645 ducktux_right->draw( base.x- scroll_x - 8, base.y - 16);
649 ducktux_left->draw( base.x- scroll_x - 8, base.y - 16);
655 /* Tux has coffee! */
658 if (!skidding_timer.started())
660 if (!jumping || physic.get_velocity_y() > 0)
664 bigfiretux_right[frame_]->draw(
665 base.x- scroll_x - 8, base.y);
669 bigfiretux_left[frame_]->draw(
670 base.x- scroll_x - 8, base.y);
677 bigfiretux_right_jump->draw(
678 base.x- scroll_x - 8, base.y);
682 bigfiretux_left_jump->draw(
683 base.x- scroll_x - 8, base.y);
691 skidfiretux_right->draw(
692 base.x- scroll_x - 8, base.y);
696 skidfiretux_left->draw(
697 base.x- scroll_x - 8, base.y);
705 duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
709 duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
717 fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
721 Player::collision(void* p_c_object, int c_object)
723 BadGuy* pbad_c = NULL;
728 pbad_c = (BadGuy*) p_c_object;
730 /* Hurt player if he touches a badguy */
731 if (!pbad_c->dying && !dying &&
732 !safe_timer.started() &&
733 pbad_c->mode != HELD)
735 if (pbad_c->mode == FLAT && input.fire == DOWN)
740 else if (pbad_c->mode == FLAT)
742 // Don't get hurt if we're kicking a flat badguy!
744 else if (pbad_c->mode == KICK)
746 /* Hurt if you get hit by kicked laptop: */
747 if (!invincible_timer.started())
753 pbad_c->dying = DYING_FALLING;
754 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
755 World::current()->add_score(pbad_c->base.x - scroll_x,
757 25 * player_status.score_multiplier);
762 if (!invincible_timer.started())
771 player_status.score_multiplier++;
783 Player::kill(int mode)
785 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
787 physic.set_velocity(0, physic.get_velocity_y());
789 if (mode == SHRINK && size == BIG)
798 safe_timer.start(TUX_SAFE_TIME);
802 physic.enable_gravity(true);
803 physic.set_acceleration(0, 0);
804 physic.set_velocity(0, 7);
805 dying = DYING_SQUISHED;
816 bool Player::is_dead()
818 if(base.y > screen->h)
824 /* Remove Tux's power ups */
826 Player::remove_powerups()
834 Player::keep_in_bounds()
836 Level* plevel = World::current()->get_level();
838 /* Keep tux in bounds: */
840 { // Lock Tux to the size of the level, so that he doesn't fall of
844 else if (base.x < scroll_x)
849 /* Keep in-bounds, vertically: */
850 if (base.y > screen->h)
855 int scroll_threshold = screen->w/2 - 80;
858 scroll_x += screen->w/2;
859 // Backscrolling for debug mode
860 if (scroll_x < base.x - 80)
861 scroll_x = base.x - 80;
862 else if (scroll_x > base.x + 80)
863 scroll_x = base.x + 80;
864 scroll_x -= screen->w/2;
871 if (base.x > scroll_threshold + scroll_x
872 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
874 // FIXME: Scrolling needs to be handled by a seperate View
875 // class, doing it as a player huck is ugly
877 // Scroll the screen in past center:
878 scroll_x = base.x - scroll_threshold;
880 // Lock the scrolling to the levelsize, so that we don't
881 // scroll over the right border
882 if (scroll_x > 32 * plevel->width - screen->w)
883 scroll_x = 32 * plevel->width - screen->w;