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
20 texture_type tux_life,
21 tux_right[3], tux_left[3],
22 bigtux_right[3], bigtux_left[3],
23 bigtux_right_jump, bigtux_left_jump,
24 ducktux_right, ducktux_left,
25 skidtux_right, skidtux_left,
26 firetux_right[3], firetux_left[3],
27 bigfiretux_right[3], bigfiretux_left[3],
28 bigfiretux_right_jump, bigfiretux_left_jump,
29 duckfiretux_right, duckfiretux_left,
30 skidfiretux_right, skidfiretux_left,
31 cape_right[2], cape_left[2],
32 bigcape_right[2], bigcape_left[2];
34 void player_input_init(player_input_type* pplayer_input)
36 pplayer_input->down = UP;
37 pplayer_input->fire = UP;
38 pplayer_input->left = UP;
39 pplayer_input->old_fire = UP;
40 pplayer_input->right = UP;
41 pplayer_input->up = UP;
44 void player_init(player_type* pplayer)
46 pplayer->base.width = 32;
47 pplayer->base.height = 32;
49 pplayer->size = SMALL;
50 pplayer->got_coffee = NO;
53 pplayer->base.y = 240;
56 pplayer->old_base = pplayer->base;
61 pplayer->jumping = NO;
63 pplayer->frame_main = 0;
69 player_input_init(&pplayer->input);
71 pplayer->keymap.jump = SDLK_UP;
72 pplayer->keymap.duck = SDLK_DOWN;
73 pplayer->keymap.left = SDLK_LEFT;
74 pplayer->keymap.right = SDLK_RIGHT;
75 pplayer->keymap.fire = SDLK_LCTRL;
77 timer_init(&pplayer->invincible_timer,YES);
78 timer_init(&pplayer->skidding_timer,YES);
79 timer_init(&pplayer->safe_timer,YES);
80 timer_init(&pplayer->frame_timer,YES);
81 physic_init(&pplayer->hphysic);
82 physic_init(&pplayer->vphysic);
85 int player_key_event(player_type* pplayer, SDLKey key, int state)
87 if(key == pplayer->keymap.right)
89 pplayer->input.right = state;
92 else if( key == pplayer->keymap.left)
94 pplayer->input.left = state;
97 else if(key == pplayer->keymap.jump)
99 pplayer->input.up = state;
102 else if(key == pplayer->keymap.duck)
104 pplayer->input.down = state;
107 else if(key == pplayer->keymap.fire)
109 pplayer->input.fire = state;
116 void player_level_begin(player_type* pplayer)
119 pplayer->base.y = 240;
120 pplayer->base.xm = 0;
121 pplayer->base.ym = 0;
122 pplayer->old_base = pplayer->base;
124 player_input_init(&pplayer->input);
126 timer_init(&pplayer->invincible_timer,YES);
127 timer_init(&pplayer->skidding_timer,YES);
128 timer_init(&pplayer->safe_timer,YES);
129 timer_init(&pplayer->frame_timer,YES);
130 physic_init(&pplayer->hphysic);
131 physic_init(&pplayer->vphysic);
134 void player_action(player_type* pplayer)
136 int i, jumped_in_solid;
137 jumped_in_solid = NO;
139 /* --- HANDLE TUX! --- */
141 player_input(pplayer);
145 pplayer->base.x += pplayer->base.xm * frame_ratio;
146 pplayer->base.y += pplayer->base.ym * frame_ratio;
148 collision_swept_object_map(&pplayer->old_base,&pplayer->base);
150 player_keep_in_bounds(pplayer);
158 if( !player_on_ground(pplayer))
160 if(player_under_solid(pplayer))
162 physic_set_state(&pplayer->vphysic,PH_VT);
163 physic_set_start_vy(&pplayer->vphysic,0);
164 jumped_in_solid = YES;
168 if(!physic_is_set(&pplayer->vphysic))
170 physic_set_state(&pplayer->vphysic,PH_VT);
171 physic_set_start_vy(&pplayer->vphysic,0);
174 pplayer->base.ym = physic_get_velocity(&pplayer->vphysic);
182 if (pplayer->base.ym > 0)
184 pplayer->base.y = (int)(((int)pplayer->base.y / 32) * 32);
185 pplayer->base.ym = 0;
188 physic_init(&pplayer->vphysic);
190 /* Reset score multiplier (for multi-hits): */
192 score_multiplier = 1;
195 if(jumped_in_solid == YES)
198 if (isbrick(pplayer->base.x, pplayer->base.y) ||
199 isfullbox(pplayer->base.x, pplayer->base.y))
201 trygrabdistro(pplayer->base.x, pplayer->base.y - 32,BOUNCE);
202 trybumpbadguy(pplayer->base.x, pplayer->base.y - 64);
204 if(pplayer->size == BIG)
205 trybreakbrick(pplayer->base.x, pplayer->base.y);
207 bumpbrick(pplayer->base.x, pplayer->base.y);
208 tryemptybox(pplayer->base.x, pplayer->base.y);
211 if (isbrick(pplayer->base.x+ 31, pplayer->base.y) ||
212 isfullbox(pplayer->base.x+ 31, pplayer->base.y))
214 trygrabdistro(pplayer->base.x+ 31, pplayer->base.y - 32,BOUNCE);
215 trybumpbadguy(pplayer->base.x+ 31, pplayer->base.y - 64);
217 if(pplayer->size == BIG)
218 trybreakbrick(pplayer->base.x+ 31, pplayer->base.y);
220 bumpbrick(pplayer->base.x+ 31, pplayer->base.y);
221 tryemptybox(pplayer->base.x+ 31, pplayer->base.y);
225 if(pplayer->size == SMALL)
227 /* Get a distro from a brick? */
229 if (shape(pplayer->base.x, pplayer->base.y) == 'x' ||
230 shape(pplayer->base.x, pplayer->base.y) == 'y')
232 add_bouncy_distro((((int)pplayer->base.x)
234 ((int)pplayer->base.y / 32) * 32);
235 if (counting_distros == NO)
237 counting_distros = YES;
238 distro_counter = 100;
241 if (distro_counter <= 0)
242 level_change(¤t_level,pplayer->base.x,pplayer->base.y - 1, 'a');
244 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
245 score = score + SCORE_DISTRO;
248 else if (shape(pplayer->base.x+ 31, pplayer->base.y) == 'x' ||
249 shape(pplayer->base.x+ 31, pplayer->base.y) == 'y')
251 add_bouncy_distro((((int)pplayer->base.x + 31)
253 ((int)pplayer->base.y / 32) * 32);
254 if (counting_distros == NO)
256 counting_distros = YES;
257 distro_counter = 100;
260 if (distro_counter <= 0)
261 level_change(¤t_level,pplayer->base.x+ 31, pplayer->base.y, 'a');
263 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
264 score = score + SCORE_DISTRO;
270 player_grabdistros(pplayer);
271 if(jumped_in_solid == YES)
274 ++pplayer->old_base.y;
275 if(player_on_ground(pplayer))
277 /* Make sure jumping is off. */
278 pplayer->jumping = NO;
284 timer_check(&pplayer->safe_timer);
287 /* ---- DONE HANDLING TUX! --- */
289 /* Handle invincibility timer: */
292 if (get_current_music() == HERRING_MUSIC && !timer_check(&pplayer->invincible_timer))
295 no, we are no more invincible
296 or we were not in invincible mode
297 but are we in hurry ?
301 if (timer_get_left(&time_left) < TIME_WARNING)
303 /* yes, we are in hurry
304 stop the herring_song, prepare to play the correct
307 set_current_music(HURRYUP_MUSIC);
311 set_current_music(LEVEL_MUSIC);
314 /* start playing it */
315 play_current_music();
318 /* Handle skidding: */
320 timer_check(&pplayer->skidding_timer);
324 if (pplayer->base.x - scroll_x >= endpos && endpos != 0)
331 int player_on_ground(player_type *pplayer)
333 if( issolid(pplayer->base.x + pplayer->base.width / 2, pplayer->base.y + pplayer->base.height) ||
334 issolid(pplayer->base.x + 1, pplayer->base.y + pplayer->base.height) ||
335 issolid(pplayer->base.x + pplayer->base.width - 1, pplayer->base.y + pplayer->base.height) )
345 int player_under_solid(player_type *pplayer)
347 if( issolid(pplayer->base.x + pplayer->base.width / 2, pplayer->base.y) ||
348 issolid(pplayer->base.x + 1, pplayer->base.y) ||
349 issolid(pplayer->base.x + pplayer->base.width - 1, pplayer->base.y) )
359 void player_handle_horizontal_input(player_type *pplayer, int dir)
362 if ((dir ? (pplayer->base.xm < -SKID_XM) : (pplayer->base.xm > SKID_XM)) && !timer_started(&pplayer->skidding_timer) &&
363 pplayer->dir == !dir)
365 timer_start(&pplayer->skidding_timer, SKID_TIME);
367 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
373 if ((dir ? (pplayer->base.xm < 0) : (pplayer->base.xm > 0)) && !isice(pplayer->base.x, pplayer->base.y + pplayer->base.height) &&
374 !timer_started(&pplayer->skidding_timer))
376 pplayer->base.xm = 0;
381 if (pplayer->dir == dir)
383 /* Facing the direction we're jumping? Go full-speed: */
385 if (pplayer->input.fire == UP)
387 pplayer->base.xm = pplayer->base.xm + ( dir ? WALK_SPEED : -WALK_SPEED) * frame_ratio;
391 if (pplayer->base.xm > MAX_WALK_XM)
392 pplayer->base.xm = MAX_WALK_XM;
396 if (pplayer->base.xm < -MAX_WALK_XM)
397 pplayer->base.xm = -MAX_WALK_XM;
400 else if ( pplayer->input.fire == DOWN)
402 pplayer->base.xm = pplayer->base.xm + ( dir ? RUN_SPEED : -RUN_SPEED) * frame_ratio;
406 if (pplayer->base.xm > MAX_RUN_XM)
407 pplayer->base.xm = MAX_RUN_XM;
411 if (pplayer->base.xm < -MAX_RUN_XM)
412 pplayer->base.xm = -MAX_RUN_XM;
417 /* Not facing the direction we're jumping?
420 pplayer->base.xm = pplayer->base.xm + ( dir ? (WALK_SPEED / 2) : -(WALK_SPEED / 2)) * frame_ratio;
424 if (pplayer->base.xm > MAX_WALK_XM / 2)
425 pplayer->base.xm = MAX_WALK_XM / 2;
429 if (pplayer->base.xm < -MAX_WALK_XM / 2)
430 pplayer->base.xm = -MAX_WALK_XM / 2;
438 void player_handle_vertical_input(player_type *pplayer)
440 if(pplayer->input.up == DOWN)
442 if (player_on_ground(pplayer))
444 if(!physic_is_set(&pplayer->vphysic))
446 physic_set_state(&pplayer->vphysic,PH_VT);
447 physic_set_start_vy(&pplayer->vphysic,5.5);
449 pplayer->jumping = YES;
450 if (pplayer->size == SMALL)
451 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
453 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
457 else if(pplayer->input.up == UP && pplayer->jumping == YES)
459 if (player_on_ground(pplayer))
461 physic_init(&pplayer->vphysic);
462 pplayer->jumping == NO;
466 pplayer->jumping = NO;
467 if(physic_is_set(&pplayer->vphysic))
469 if(physic_get_velocity(&pplayer->vphysic) < 0.)
471 physic_set_state(&pplayer->vphysic,PH_VT);
472 physic_set_start_vy(&pplayer->vphysic,0);
477 if(!physic_is_set(&pplayer->vphysic))
479 physic_set_state(&pplayer->vphysic,PH_VT);
486 void player_input(player_type *pplayer)
488 /* Handle key and joystick state: */
490 if(pplayer->duck == NO)
492 if (pplayer->input.right == DOWN && pplayer->input.left == UP)
494 player_handle_horizontal_input(pplayer,RIGHT);
496 else if (pplayer->input.left == DOWN && pplayer->input.right == UP)
498 player_handle_horizontal_input(pplayer,LEFT);
502 if(pplayer->base.xm > 0)
504 pplayer->base.xm = (int)(pplayer->base.xm - frame_ratio);
505 if(pplayer->base.xm < 0)
506 pplayer->base.xm = 0;
508 else if(pplayer->base.xm < 0)
510 pplayer->base.xm = (int)(pplayer->base.xm + frame_ratio);
511 if(pplayer->base.xm > 0)
512 pplayer->base.xm = 0;
519 if ( pplayer->input.up == DOWN || (pplayer->input.up == UP && pplayer->jumping == YES))
521 player_handle_vertical_input(pplayer);
526 if (pplayer->input.fire == DOWN && pplayer->input.old_fire == UP && pplayer->got_coffee)
528 add_bullet(pplayer->base.x, pplayer->base.y, pplayer->base.xm, pplayer->dir);
534 if (pplayer->input.down == DOWN)
536 if (pplayer->size == BIG && pplayer->duck != YES)
539 pplayer->base.height = 32;
540 pplayer->base.y += 32;
545 if (pplayer->size == BIG && pplayer->duck == YES)
547 /* Make sure we're not standing back up into a solid! */
548 pplayer->base.height = 64;
549 pplayer->base.y -= 32;
551 if (!collision_object_map(&pplayer->base) /*issolid(pplayer->base.x + 16, pplayer->base.y - 16)*/)
554 pplayer->base.height = 64;
555 pplayer->old_base.y -= 32;
556 pplayer->old_base.height = 64;
560 pplayer->base.height = 32;
561 pplayer->base.y += 32;
572 if(!timer_check(&pplayer->frame_timer))
574 timer_start(&pplayer->frame_timer,25);
575 if (pplayer->input.right == UP && pplayer->input.left == UP)
577 pplayer->frame_main = 1;
582 if ((pplayer->input.fire == DOWN && (frame % 2) == 0) ||
584 pplayer->frame_main = (pplayer->frame_main + 1) % 4;
586 pplayer->frame = pplayer->frame_main;
588 if (pplayer->frame == 3)
595 void player_grabdistros(player_type *pplayer)
600 trygrabdistro(pplayer->base.x, pplayer->base.y, NO_BOUNCE);
601 trygrabdistro(pplayer->base.x+ 31, pplayer->base.y, NO_BOUNCE);
603 trygrabdistro(pplayer->base.x, pplayer->base.y + pplayer->base.height, NO_BOUNCE);
604 trygrabdistro(pplayer->base.x+ 31, pplayer->base.y + pplayer->base.height, NO_BOUNCE);
606 if(pplayer->size == BIG)
608 trygrabdistro(pplayer->base.x, pplayer->base.y + pplayer->base.height / 2, NO_BOUNCE);
609 trygrabdistro(pplayer->base.x+ 31, pplayer->base.y + pplayer->base.height / 2, NO_BOUNCE);
615 /* Enough distros for a One-up? */
617 if (distros >= DISTROS_LIFEUP)
619 distros = distros - DISTROS_LIFEUP;
620 if(pplayer->lives < MAX_LIVES)
622 /*We want to hear the sound even, if MAX_LIVES is reached*/
623 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
627 void player_draw(player_type* pplayer)
629 if (!timer_started(&pplayer->safe_timer) || (frame % 2) == 0)
631 if (pplayer->size == SMALL)
633 if (timer_started(&pplayer->invincible_timer))
637 if (pplayer->dir == RIGHT)
639 texture_draw(&cape_right[frame % 2],
640 pplayer->base.x- scroll_x, pplayer->base.y,
645 texture_draw(&cape_left[frame % 2],
646 pplayer->base.x- scroll_x, pplayer->base.y,
652 if (!pplayer->got_coffee)
654 if (pplayer->dir == RIGHT)
656 texture_draw(&tux_right[pplayer->frame], pplayer->base.x- scroll_x, pplayer->base.y, NO_UPDATE);
660 texture_draw(&tux_left[pplayer->frame], pplayer->base.x- scroll_x, pplayer->base.y, NO_UPDATE);
665 /* Tux got coffee! */
667 if (pplayer->dir == RIGHT)
669 texture_draw(&firetux_right[pplayer->frame], pplayer->base.x- scroll_x, pplayer->base.y, NO_UPDATE);
673 texture_draw(&firetux_left[pplayer->frame], pplayer->base.x- scroll_x, pplayer->base.y, NO_UPDATE);
679 if (timer_started(&pplayer->invincible_timer))
683 if (pplayer->dir == RIGHT)
685 texture_draw(&bigcape_right[frame % 2],
686 pplayer->base.x- scroll_x - 8, pplayer->base.y,
691 texture_draw(&bigcape_left[frame % 2],
692 pplayer->base.x-scroll_x - 8, pplayer->base.y,
697 if (!pplayer->got_coffee)
701 if (!timer_started(&pplayer->skidding_timer))
703 if (!pplayer->jumping || pplayer->base.ym > 0)
705 if (pplayer->dir == RIGHT)
707 texture_draw(&bigtux_right[pplayer->frame],
708 pplayer->base.x- scroll_x - 8, pplayer->base.y,
713 texture_draw(&bigtux_left[pplayer->frame],
714 pplayer->base.x- scroll_x - 8, pplayer->base.y,
720 if (pplayer->dir == RIGHT)
722 texture_draw(&bigtux_right_jump,
723 pplayer->base.x- scroll_x - 8, pplayer->base.y,
728 texture_draw(&bigtux_left_jump,
729 pplayer->base.x- scroll_x - 8, pplayer->base.y,
736 if (pplayer->dir == RIGHT)
738 texture_draw(&skidtux_right,
739 pplayer->base.x- scroll_x - 8, pplayer->base.y,
744 texture_draw(&skidtux_left,
745 pplayer->base.x- scroll_x - 8, pplayer->base.y,
752 if (pplayer->dir == RIGHT)
754 texture_draw(&ducktux_right, pplayer->base.x- scroll_x - 8, pplayer->base.y - 16,
759 texture_draw(&ducktux_left, pplayer->base.x- scroll_x - 8, pplayer->base.y - 16,
766 /* Tux has coffee! */
770 if (!timer_started(&pplayer->skidding_timer))
772 if (!pplayer->jumping || pplayer->base.ym > 0)
774 if (pplayer->dir == RIGHT)
776 texture_draw(&bigfiretux_right[pplayer->frame],
777 pplayer->base.x- scroll_x - 8, pplayer->base.y,
782 texture_draw(&bigfiretux_left[pplayer->frame],
783 pplayer->base.x- scroll_x - 8, pplayer->base.y,
789 if (pplayer->dir == RIGHT)
791 texture_draw(&bigfiretux_right_jump,
792 pplayer->base.x- scroll_x - 8, pplayer->base.y,
797 texture_draw(&bigfiretux_left_jump,
798 pplayer->base.x- scroll_x - 8, pplayer->base.y,
805 if (pplayer->dir == RIGHT)
807 texture_draw(&skidfiretux_right,
808 pplayer->base.x- scroll_x - 8, pplayer->base.y,
813 texture_draw(&skidfiretux_left,
814 pplayer->base.x- scroll_x - 8, pplayer->base.y,
821 if (pplayer->dir == RIGHT)
823 texture_draw(&duckfiretux_right, pplayer->base.x- scroll_x - 8, pplayer->base.y - 16,
828 texture_draw(&duckfiretux_left, pplayer->base.x- scroll_x - 8, pplayer->base.y - 16,
837 void player_collision(player_type* pplayer, void* p_c_object, int c_object)
839 bad_guy_type* pbad_c = NULL;
844 pbad_c = (bad_guy_type*) p_c_object;
845 /* Hurt the player if he just touched it: */
847 if (!pbad_c->dying && !pplayer->dying &&
848 !timer_started(&pplayer->safe_timer) &&
849 pbad_c->mode != HELD)
851 if (pbad_c->mode == FLAT && pplayer->input.fire != DOWN)
856 play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
858 if (pplayer->base.x<= pbad_c->base.x)
861 pbad_c->base.x = pbad_c->base.x + 16;
866 pbad_c->base.x = pbad_c->base.x - 32;
869 timer_start(&pbad_c->timer,5000);
871 else if (pbad_c->mode == FLAT && pplayer->input.fire == DOWN)
876 else if (pbad_c->mode == KICK)
878 if (pplayer->base.y < pbad_c->base.y - 16 &&
879 timer_started(&pbad_c->timer))
881 /* Step on (stop being kicked) */
884 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
885 timer_start(&pbad_c->timer, 10000);
889 /* Hurt if you get hit by kicked laptop: */
891 if (timer_started(&pbad_c->timer))
893 if (!timer_started(&pplayer->invincible_timer))
895 player_kill(pplayer,SHRINK);
899 pbad_c->dying = FALLING;
900 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
901 add_score(pbad_c->base.x - scroll_x,
903 25 * score_multiplier);
910 if (!timer_started(&pplayer->invincible_timer ))
912 player_kill(pplayer,SHRINK);
916 pbad_c->dying = FALLING;
917 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
918 add_score(pbad_c->base.x - scroll_x,
920 25 * score_multiplier);
934 void player_kill(player_type* pplayer, int mode)
936 pplayer->base.ym = -5;
938 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
940 if (pplayer->dir == RIGHT)
941 pplayer->base.xm = -8;
942 else if (pplayer->dir == LEFT)
943 pplayer->base.xm = 8;
945 if (mode == SHRINK && pplayer->size == BIG)
947 if (pplayer->got_coffee)
948 pplayer->got_coffee = NO;
950 pplayer->size = SMALL;
951 pplayer->base.height = 32;
953 timer_start(&pplayer->safe_timer,TUX_SAFE_TIME);
961 void player_dying(player_type *pplayer)
963 pplayer->base.ym = pplayer->base.ym + gravity;
968 player_remove_powerups(pplayer);
971 player_level_begin(pplayer);
975 /* Remove Tux's power ups */
976 void player_remove_powerups(player_type* pplayer)
978 pplayer->got_coffee = NO;
979 pplayer->size = SMALL;
980 pplayer->base.height = 32;
983 void player_keep_in_bounds(player_type* pplayer)
985 /* Keep tux in bounds: */
986 if (pplayer->base.x< 0)
988 else if(pplayer->base.x< scroll_x)
989 pplayer->base.x= scroll_x;
990 else if (pplayer->base.x< 160 + scroll_x && scroll_x > 0 && debug_mode == YES)
992 scroll_x = pplayer->base.x- 160;
993 /*pplayer->base.x+= 160;*/
999 else if (pplayer->base.x> screen->w / 2 + scroll_x && scroll_x < ((current_level.width * 32) - screen->w))
1001 /* Scroll the screen in past center: */
1003 scroll_x = pplayer->base.x- screen->w / 2;
1004 /*pplayer->base.x= 320 + scroll_x;*/
1006 if (scroll_x > ((current_level.width * 32) - screen->w))
1007 scroll_x = ((current_level.width * 32) - screen->w);
1009 else if (pplayer->base.x> 608 + scroll_x)
1011 /* ... unless there's no more to scroll! */
1013 /*pplayer->base.x= 608 + scroll_x;*/
1016 /* Keep in-bounds, vertically: */
1018 if (pplayer->base.y > screen->h)
1020 player_kill(&tux,KILL);