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;
90 player_input_init(&input);
92 keymap.jump = SDLK_UP;
93 keymap.duck = SDLK_DOWN;
94 keymap.left = SDLK_LEFT;
95 keymap.right = SDLK_RIGHT;
96 keymap.fire = SDLK_LCTRL;
98 invincible_timer.init(true);
99 skidding_timer.init(true);
100 safe_timer.init(true);
101 frame_timer.init(true);
107 Player::key_event(SDLKey key, int state)
109 if(key == keymap.right)
114 else if(key == keymap.left)
119 else if(key == keymap.jump)
124 else if(key == keymap.duck)
129 else if(key == keymap.fire)
139 Player::level_begin()
145 previous_base = old_base = base;
150 player_input_init(&input);
152 invincible_timer.init(true);
153 skidding_timer.init(true);
154 safe_timer.init(true);
155 frame_timer.init(true);
161 Player::action(double frame_ratio)
163 bool jumped_in_solid = false;
165 /* --- HANDLE TUX! --- */
167 if(dying == DYING_NOT)
171 previous_base = base;
174 physic.apply(frame_ratio, base.x, base.y);
175 if(dying == DYING_NOT) {
176 collision_swept_object_map(&old_base, &base);
177 // special exception for cases where we're stuck under tiles after
178 // being ducked. In this case we drift out
179 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
180 && collision_object_map(&base)) {
181 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
182 previous_base = old_base = base;
187 if (dying == DYING_NOT)
194 physic.enable_gravity(true);
198 physic.set_velocity(physic.get_velocity_x(), 0);
199 jumped_in_solid = true;
205 if (physic.get_velocity_y() < 0)
207 base.y = (int)(((int)base.y / 32) * 32);
208 physic.set_velocity(physic.get_velocity_x(), 0);
211 physic.enable_gravity(false);
212 /* Reset score multiplier (for multi-hits): */
213 player_status.score_multiplier = 1;
218 if (isbrick(base.x, base.y) ||
219 isfullbox(base.x, base.y))
221 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
222 World::current()->trybumpbadguy(base.x, base.y - 64);
224 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
226 bumpbrick(base.x, base.y);
227 World::current()->tryemptybox(base.x, base.y, RIGHT);
230 if (isbrick(base.x+ 31, base.y) ||
231 isfullbox(base.x+ 31, base.y))
233 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
234 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
237 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
239 bumpbrick(base.x+ 31, base.y);
240 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
252 /* Make sure jumping is off. */
259 /* ---- DONE HANDLING TUX! --- */
261 /* Handle invincibility timer: */
262 if (get_current_music() == HERRING_MUSIC && !invincible_timer.check())
265 no, we are no more invincible
266 or we were not in invincible mode
267 but are we in hurry ?
270 // FIXME: Move this to gamesession
271 if (GameSession::current()->time_left.get_left() < TIME_WARNING)
273 /* yes, we are in hurry
274 stop the herring_song, prepare to play the correct
277 set_current_music(HURRYUP_MUSIC);
281 set_current_music(LEVEL_MUSIC);
284 /* start playing it */
285 play_current_music();
289 if (base.x >= World::current()->get_level()->endpos
290 && World::current()->get_level()->endpos != 0)
292 player_status.next_level = 1;
296 if (input.down == DOWN && size == BIG && !duck)
301 // changing base size confuses collision otherwise
302 old_base = previous_base = base;
304 else if(input.down == UP && size == BIG && duck)
309 old_base = previous_base = base;
313 skidding_timer.check();
314 invincible_timer.check();
321 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
322 issolid(base.x + 1, base.y + base.height) ||
323 issolid(base.x + base.width - 1, base.y + base.height) );
327 Player::under_solid()
329 return ( issolid(base.x + base.width / 2, base.y) ||
330 issolid(base.x + 1, base.y) ||
331 issolid(base.x + base.width - 1, base.y) );
335 Player::handle_horizontal_input()
337 float vx = physic.get_velocity_x();
338 float vy = physic.get_velocity_y();
339 float ax = physic.get_acceleration_x();
340 float ay = physic.get_acceleration_y();
343 if(!duck && input.left == DOWN && input.right == UP) {
346 } else if(!duck && input.left == UP && input.right == DOWN) {
351 if (input.fire == UP) {
352 ax = dirsign * WALK_ACCELERATION_X;
354 if(vx >= MAX_WALK_XM && dirsign > 0) {
357 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
362 ax = dirsign * RUN_ACCELERATION_X;
364 if(vx >= MAX_RUN_XM && dirsign > 0) {
367 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
373 // we can reach WALK_SPEED without any acceleration
374 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
375 vx = dirsign * WALK_SPEED;
378 // changing directions?
379 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
380 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
381 skidding_timer.start(SKID_TIME);
382 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
389 // we get slower when not pressing any keys
391 if(fabs(vx) < WALK_SPEED) {
395 ax = WALK_ACCELERATION_X * 1.5;
397 ax = WALK_ACCELERATION_X * -1.5;
401 physic.set_velocity(vx, vy);
402 physic.set_acceleration(ax, ay);
406 Player::handle_vertical_input()
410 if (on_ground() && !duck)
413 physic.set_velocity(physic.get_velocity_x(), 5.5);
417 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
419 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
422 else if(input.up == UP && jumping)
425 if(physic.get_velocity_y() > 0) {
426 physic.set_velocity(physic.get_velocity_x(), 0);
432 Player::handle_input()
434 /* Handle horizontal movement: */
435 handle_horizontal_input();
439 if ( input.up == DOWN || (input.up == UP && jumping))
441 handle_vertical_input();
446 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
448 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
451 /* tux animations: */
452 if(!frame_timer.check())
454 frame_timer.start(25);
455 if (input.right == UP && input.left == UP)
462 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
463 (global_frame_counter % 4) == 0)
464 frame_main = (frame_main + 1) % 4;
474 if (input.down == DOWN && size == BIG && !duck)
479 // changing base size confuses collision otherwise
480 old_base = previous_base = base;
482 else if(input.down == UP && size == BIG && duck)
487 old_base = previous_base = base;
492 Player::grabdistros()
497 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
498 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
500 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
501 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
505 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
506 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
511 /* Enough distros for a One-up? */
512 if (distros >= DISTROS_LIFEUP)
514 distros = distros - DISTROS_LIFEUP;
515 if(lives < MAX_LIVES)
517 /*We want to hear the sound even, if MAX_LIVES is reached*/
518 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
525 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
529 if (invincible_timer.started())
535 cape_right[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
539 cape_left[global_frame_counter % 2]->draw(
540 base.x- scroll_x, base.y);
547 if (physic.get_velocity_y() != 0)
550 smalltux_jump_right->draw( base.x - scroll_x, base.y - 10);
552 smalltux_jump_left->draw( base.x - scroll_x, base.y - 10);
556 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
559 smalltux_stand_right->draw( base.x - scroll_x, base.y - 9);
561 smalltux_stand_left->draw( base.x - scroll_x, base.y - 9);
566 tux_right[(global_frame_counter/2) % tux_right.size()]->draw(
567 base.x - scroll_x, base.y - 9);
569 tux_left[(global_frame_counter/2) % tux_left.size()]->draw(
570 base.x - scroll_x, base.y - 9);
576 /* Tux got coffee! */
580 firetux_right[frame_]->draw( base.x- scroll_x, base.y);
584 firetux_left[frame_]->draw( base.x- scroll_x, base.y);
590 if (invincible_timer.started())
592 float capex = base.x + (base.width - bigcape_right[0]->w) / 2;
594 float capey = base.y + (base.height - bigcape_right[0]->h) / 2;
596 /* Draw cape (just not in ducked mode since that looks silly): */
599 bigcape_right[global_frame_counter % 2]->draw(
604 bigcape_left[global_frame_counter % 2]->draw(
613 if (!skidding_timer.started())
615 if (!jumping || physic.get_velocity_y() > 0)
619 bigtux_right[frame_]->draw(
620 base.x- scroll_x - 8, base.y);
624 bigtux_left[frame_]->draw(
625 base.x- scroll_x - 8, base.y);
632 bigtux_right_jump->draw(
633 base.x- scroll_x - 8, base.y);
637 bigtux_left_jump->draw(
638 base.x- scroll_x - 8, base.y);
647 base.x- scroll_x - 8, base.y);
652 base.x- scroll_x - 8, base.y);
660 ducktux_right->draw( base.x- scroll_x - 8, base.y - 16);
664 ducktux_left->draw( base.x- scroll_x - 8, base.y - 16);
670 /* Tux has coffee! */
673 if (!skidding_timer.started())
675 if (!jumping || physic.get_velocity_y() > 0)
679 bigfiretux_right[frame_]->draw(
680 base.x- scroll_x - 8, base.y);
684 bigfiretux_left[frame_]->draw(
685 base.x- scroll_x - 8, base.y);
692 bigfiretux_right_jump->draw(
693 base.x- scroll_x - 8, base.y);
697 bigfiretux_left_jump->draw(
698 base.x- scroll_x - 8, base.y);
706 skidfiretux_right->draw(
707 base.x- scroll_x - 8, base.y);
711 skidfiretux_left->draw(
712 base.x- scroll_x - 8, base.y);
720 duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
724 duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
732 text_drawf(&gold_text,"Penguins can fly !:",0,0,A_HMIDDLE,A_VMIDDLE,1);
735 fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
739 Player::collision(void* p_c_object, int c_object)
741 BadGuy* pbad_c = NULL;
746 pbad_c = (BadGuy*) p_c_object;
747 /* Hurt the player if he just touched it: */
749 if (!pbad_c->dying && !dying &&
750 !safe_timer.started() &&
751 pbad_c->mode != HELD)
753 if (pbad_c->mode == FLAT && input.fire == DOWN)
758 else if (pbad_c->mode == KICK)
760 if (base.y < pbad_c->base.y - 16)
762 /* Step on (stop being kicked) */
765 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
769 /* Hurt if you get hit by kicked laptop: */
770 if (!invincible_timer.started())
776 pbad_c->dying = DYING_FALLING;
777 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
778 World::current()->add_score(pbad_c->base.x - scroll_x,
780 25 * player_status.score_multiplier);
786 if (!invincible_timer.started())
795 player_status.score_multiplier++;
807 Player::kill(int mode)
809 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
811 physic.set_velocity(0, physic.get_velocity_y());
813 if (mode == SHRINK && size == BIG)
822 safe_timer.start(TUX_SAFE_TIME);
826 physic.enable_gravity(true);
827 physic.set_acceleration(0, 0);
828 physic.set_velocity(0, 7);
829 dying = DYING_SQUISHED;
843 bool Player::is_dead()
845 if(base.y > screen->h)
851 /* Remove Tux's power ups */
853 Player::remove_powerups()
861 Player::keep_in_bounds()
863 Level* plevel = World::current()->get_level();
865 /* Keep tux in bounds: */
867 { // Lock Tux to the size of the level, so that he doesn't fall of
871 else if (base.x < scroll_x)
876 /* Keep in-bounds, vertically: */
877 if (base.y > screen->h)
882 int scroll_threshold = screen->w/2 - 80;
885 scroll_x += screen->w/2;
886 // Backscrolling for debug mode
887 if (scroll_x < base.x - 80)
888 scroll_x = base.x - 80;
889 else if (scroll_x > base.x + 80)
890 scroll_x = base.x + 80;
891 scroll_x -= screen->w/2;
898 if (base.x > scroll_threshold + scroll_x
899 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
901 // FIXME: Scrolling needs to be handled by a seperate View
902 // class, doing it as a player huck is ugly
904 // Scroll the screen in past center:
905 scroll_x = base.x - scroll_threshold;
907 // Lock the scrolling to the levelsize, so that we don't
908 // scroll over the right border
909 if (scroll_x > 32 * plevel->width - screen->w)
910 scroll_x = 32 * plevel->width - screen->w;