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;
67 Level* plevel = World::current()->get_level();
75 base.x = plevel->start_pos_x;
76 base.y = plevel->start_pos_y;
79 previous_base = old_base = base;
89 player_input_init(&input);
91 keymap.jump = SDLK_UP;
92 keymap.duck = SDLK_DOWN;
93 keymap.left = SDLK_LEFT;
94 keymap.right = SDLK_RIGHT;
95 keymap.fire = SDLK_LCTRL;
97 invincible_timer.init(true);
98 skidding_timer.init(true);
99 safe_timer.init(true);
100 frame_timer.init(true);
106 Player::key_event(SDLKey key, int state)
108 if(key == keymap.right)
113 else if(key == keymap.left)
118 else if(key == keymap.jump)
123 else if(key == keymap.duck)
128 else if(key == keymap.fire)
138 Player::level_begin()
144 previous_base = old_base = base;
149 player_input_init(&input);
151 invincible_timer.init(true);
152 skidding_timer.init(true);
153 safe_timer.init(true);
154 frame_timer.init(true);
160 Player::action(double frame_ratio)
162 bool jumped_in_solid = false;
164 /* --- HANDLE TUX! --- */
166 if(dying == DYING_NOT)
170 previous_base = base;
172 physic.apply(frame_ratio, base.x, base.y);
173 if(dying == DYING_NOT) {
174 collision_swept_object_map(&old_base, &base);
175 // special exception for cases where we're stuck under tiles after
176 // being ducked. In this case we drift out
177 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
178 && collision_object_map(&base)) {
179 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
180 previous_base = old_base = base;
185 if (dying == DYING_NOT)
192 physic.enable_gravity(true);
196 physic.set_velocity(physic.get_velocity_x(), 0);
197 jumped_in_solid = true;
203 if (physic.get_velocity_y() < 0)
205 base.y = (int)(((int)base.y / 32) * 32);
206 physic.set_velocity(physic.get_velocity_x(), 0);
209 physic.enable_gravity(false);
210 /* Reset score multiplier (for multi-hits): */
211 player_status.score_multiplier = 1;
216 if (isbrick(base.x, base.y) ||
217 isfullbox(base.x, base.y))
219 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
220 World::current()->trybumpbadguy(base.x, base.y - 64);
222 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
224 bumpbrick(base.x, base.y);
225 World::current()->tryemptybox(base.x, base.y, RIGHT);
228 if (isbrick(base.x+ 31, base.y) ||
229 isfullbox(base.x+ 31, base.y))
231 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
232 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
235 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
237 bumpbrick(base.x+ 31, base.y);
238 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
250 /* Make sure jumping is off. */
257 /* ---- DONE HANDLING TUX! --- */
259 /* Handle invincibility timer: */
260 if (get_current_music() == HERRING_MUSIC && !invincible_timer.check())
263 no, we are no more invincible
264 or we were not in invincible mode
265 but are we in hurry ?
268 // FIXME: Move this to gamesession
269 if (GameSession::current()->time_left.get_left() < TIME_WARNING)
271 /* yes, we are in hurry
272 stop the herring_song, prepare to play the correct
275 set_current_music(HURRYUP_MUSIC);
279 set_current_music(LEVEL_MUSIC);
282 /* start playing it */
283 play_current_music();
287 skidding_timer.check();
288 invincible_timer.check();
295 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
296 issolid(base.x + 1, base.y + base.height) ||
297 issolid(base.x + base.width - 1, base.y + base.height) );
301 Player::under_solid()
303 return ( issolid(base.x + base.width / 2, base.y) ||
304 issolid(base.x + 1, base.y) ||
305 issolid(base.x + base.width - 1, base.y) );
309 Player::handle_horizontal_input()
311 float vx = physic.get_velocity_x();
312 float vy = physic.get_velocity_y();
313 float ax = physic.get_acceleration_x();
314 float ay = physic.get_acceleration_y();
317 if(!duck && input.left == DOWN && input.right == UP) {
320 } else if(!duck && input.left == UP && input.right == DOWN) {
325 if (input.fire == UP) {
326 ax = dirsign * WALK_ACCELERATION_X;
328 if(vx >= MAX_WALK_XM && dirsign > 0) {
331 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
336 ax = dirsign * RUN_ACCELERATION_X;
338 if(vx >= MAX_RUN_XM && dirsign > 0) {
341 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
347 // we can reach WALK_SPEED without any acceleration
348 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
349 vx = dirsign * WALK_SPEED;
352 // changing directions?
353 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
354 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
355 skidding_timer.start(SKID_TIME);
356 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
363 // we get slower when not pressing any keys
365 if(fabs(vx) < WALK_SPEED) {
369 ax = WALK_ACCELERATION_X * 1.5;
371 ax = WALK_ACCELERATION_X * -1.5;
375 // if we're on ice slow down acceleration or deceleration
376 if (isice(base.x, base.y + base.height))
378 /* the acceleration/deceleration rate on ice is inversely proportional to
379 * the current velocity.
382 // increasing 1 will increase acceleration/deceleration rate
383 // decreasing 1 will decrease acceleration/deceleration rate
384 // must stay above zero, though
385 if (ax != 0) ax *= 1 / fabs(vx);
388 physic.set_velocity(vx, vy);
389 physic.set_acceleration(ax, ay);
393 Player::handle_vertical_input()
397 if (on_ground() && !duck)
400 physic.set_velocity(physic.get_velocity_x(), 5.5);
404 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
406 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
409 else if(input.up == UP && jumping)
412 if(physic.get_velocity_y() > 0) {
413 physic.set_velocity(physic.get_velocity_x(), 0);
419 Player::handle_input()
421 /* Handle horizontal movement: */
422 handle_horizontal_input();
426 if ( input.up == DOWN || (input.up == UP && jumping))
428 handle_vertical_input();
433 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
435 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
438 /* tux animations: */
439 if(!frame_timer.check())
441 frame_timer.start(25);
442 if (input.right == UP && input.left == UP)
449 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
450 (global_frame_counter % 4) == 0)
451 frame_main = (frame_main + 1) % 4;
461 if (input.down == DOWN && size == BIG && !duck)
466 // changing base size confuses collision otherwise
467 old_base = previous_base = base;
469 else if(input.down == UP && size == BIG && duck)
474 old_base = previous_base = base;
479 Player::grabdistros()
484 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
485 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
487 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
488 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
492 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
493 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
498 /* Enough distros for a One-up? */
499 if (player_status.distros >= DISTROS_LIFEUP)
501 player_status.distros = player_status.distros - DISTROS_LIFEUP;
502 if(player_status.lives < MAX_LIVES)
503 ++player_status.lives;
504 /*We want to hear the sound even, if MAX_LIVES is reached*/
505 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
512 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
516 if (invincible_timer.started())
521 cape_right[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
523 cape_left[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
529 if (physic.get_velocity_y() != 0)
532 smalltux_jump_right->draw( base.x - scroll_x, base.y - 10);
534 smalltux_jump_left->draw( base.x - scroll_x, base.y - 10);
538 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
541 smalltux_stand_right->draw( base.x - scroll_x, base.y - 9);
543 smalltux_stand_left->draw( base.x - scroll_x, base.y - 9);
548 tux_right[(global_frame_counter/2) % tux_right.size()]->draw(base.x - scroll_x, base.y - 9);
550 tux_left[(global_frame_counter/2) % tux_left.size()]->draw(base.x - scroll_x, base.y - 9);
556 /* Tux got coffee! */
560 firetux_right[frame_]->draw( base.x- scroll_x, base.y);
564 firetux_left[frame_]->draw( base.x- scroll_x, base.y);
570 if (invincible_timer.started())
572 float capex = base.x + (base.width - bigcape_right[0]->w) / 2;
574 float capey = base.y + (base.height - bigcape_right[0]->h) / 2;
576 /* Draw cape (just not in ducked mode since that looks silly): */
578 bigcape_right[global_frame_counter % 2]->draw(capex, capey);
580 bigcape_left[global_frame_counter % 2]->draw(capex, capey);
587 if (!skidding_timer.started())
589 if (physic.get_velocity_y() == 0)
592 bigtux_right->draw(base.x - scroll_x, base.y);
594 bigtux_left->draw(base.x - scroll_x, base.y);
599 bigtux_right_jump->draw(base.x - scroll_x, base.y);
601 bigtux_left_jump->draw(base.x - scroll_x, base.y);
607 skidtux_right->draw(base.x - scroll_x - 8, base.y);
609 skidtux_left->draw(base.x - scroll_x - 8, base.y);
615 ducktux_right->draw(base.x - scroll_x, base.y);
617 ducktux_left->draw(base.x - scroll_x, base.y);
622 /* Tux has coffee! */
625 if (!skidding_timer.started())
627 if (!jumping || physic.get_velocity_y() > 0)
630 bigfiretux_right[frame_]->draw(base.x- scroll_x - 8, base.y);
632 bigfiretux_left[frame_]->draw(base.x- scroll_x - 8, base.y);
637 bigfiretux_right_jump->draw(base.x- scroll_x - 8, base.y);
639 bigfiretux_left_jump->draw(base.x- scroll_x - 8, base.y);
645 skidfiretux_right->draw(base.x- scroll_x - 8, base.y);
647 skidfiretux_left->draw(base.x- scroll_x - 8, base.y);
653 duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
655 duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
662 fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
666 Player::collision(void* p_c_object, int c_object)
668 BadGuy* pbad_c = NULL;
673 pbad_c = (BadGuy*) p_c_object;
675 /* Hurt player if he touches a badguy */
676 if (!pbad_c->dying && !dying &&
677 !safe_timer.started() &&
678 pbad_c->mode != HELD)
680 if (pbad_c->mode == FLAT && input.fire == DOWN)
685 else if (pbad_c->mode == FLAT)
687 // Don't get hurt if we're kicking a flat badguy!
689 else if (pbad_c->mode == KICK)
691 /* Hurt if you get hit by kicked laptop: */
692 if (!invincible_timer.started())
698 pbad_c->dying = DYING_FALLING;
699 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
700 World::current()->add_score(pbad_c->base.x - scroll_x,
702 25 * player_status.score_multiplier);
707 if (!invincible_timer.started())
716 player_status.score_multiplier++;
728 Player::kill(int mode)
730 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
732 physic.set_velocity(0, physic.get_velocity_y());
734 if (mode == SHRINK && size == BIG)
743 safe_timer.start(TUX_SAFE_TIME);
747 physic.enable_gravity(true);
748 physic.set_acceleration(0, 0);
749 physic.set_velocity(0, 7);
750 dying = DYING_SQUISHED;
761 bool Player::is_dead()
763 if(base.y > screen->h)
769 /* Remove Tux's power ups */
771 Player::remove_powerups()
779 Player::keep_in_bounds()
781 Level* plevel = World::current()->get_level();
783 /* Keep tux in bounds: */
785 { // Lock Tux to the size of the level, so that he doesn't fall of
789 else if (base.x < scroll_x)
794 /* Keep in-bounds, vertically: */
795 if (base.y > screen->h)
800 int scroll_threshold = screen->w/2 - 80;
803 scroll_x += screen->w/2;
804 // Backscrolling for debug mode
805 if (scroll_x < base.x - 80)
806 scroll_x = base.x - 80;
807 else if (scroll_x > base.x + 80)
808 scroll_x = base.x + 80;
809 scroll_x -= screen->w/2;
816 if (base.x > scroll_threshold + scroll_x
817 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
819 // FIXME: Scrolling needs to be handled by a seperate View
820 // class, doing it as a player huck is ugly
822 // Scroll the screen in past center:
823 scroll_x = base.x - scroll_threshold;
825 // Lock the scrolling to the levelsize, so that we don't
826 // scroll over the right border
827 if (scroll_x > 32 * plevel->width - screen->w)
828 scroll_x = 32 * plevel->width - screen->w;