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)
361 if (pplayer->jumping == NO)
363 if ((dir ? (pplayer->base.xm < -SKID_XM) : (pplayer->base.xm > SKID_XM)) && !timer_started(&pplayer->skidding_timer) &&
364 pplayer->dir == !dir)
366 timer_start(&pplayer->skidding_timer, SKID_TIME);
368 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
374 if ((dir ? (pplayer->base.xm < 0) : (pplayer->base.xm > 0)) && !isice(pplayer->base.x, pplayer->base.y + pplayer->base.height) &&
375 !timer_started(&pplayer->skidding_timer))
377 pplayer->base.xm = 0;
382 if (pplayer->dir == dir)
384 /* Facing the direction we're jumping? Go full-speed: */
386 if (pplayer->input.fire == UP)
388 pplayer->base.xm = pplayer->base.xm + ( dir ? WALK_SPEED : -WALK_SPEED) * frame_ratio;
392 if (pplayer->base.xm > MAX_WALK_XM)
393 pplayer->base.xm = MAX_WALK_XM;
397 if (pplayer->base.xm < -MAX_WALK_XM)
398 pplayer->base.xm = -MAX_WALK_XM;
401 else if ( pplayer->input.fire == DOWN)
403 pplayer->base.xm = pplayer->base.xm + ( dir ? RUN_SPEED : -RUN_SPEED) * frame_ratio;
407 if (pplayer->base.xm > MAX_RUN_XM)
408 pplayer->base.xm = MAX_RUN_XM;
412 if (pplayer->base.xm < -MAX_RUN_XM)
413 pplayer->base.xm = -MAX_RUN_XM;
418 /* Not facing the direction we're jumping?
421 pplayer->base.xm = pplayer->base.xm + ( dir ? (WALK_SPEED / 2) : -(WALK_SPEED / 2)) * frame_ratio;
425 if (pplayer->base.xm > MAX_WALK_XM / 2)
426 pplayer->base.xm = MAX_WALK_XM / 2;
430 if (pplayer->base.xm < -MAX_WALK_XM / 2)
431 pplayer->base.xm = -MAX_WALK_XM / 2;
439 void player_handle_vertical_input(player_type *pplayer)
441 if(pplayer->input.up == DOWN)
443 if (player_on_ground(pplayer))
445 if(!physic_is_set(&pplayer->vphysic))
447 physic_set_state(&pplayer->vphysic,PH_VT);
448 physic_set_start_vy(&pplayer->vphysic,5.5);
450 pplayer->jumping = YES;
451 if (pplayer->size == SMALL)
452 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
454 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
458 else if(pplayer->input.up == UP && pplayer->jumping == YES)
460 if (player_on_ground(pplayer))
462 physic_init(&pplayer->vphysic);
463 pplayer->jumping == NO;
467 pplayer->jumping = NO;
468 if(physic_is_set(&pplayer->vphysic))
470 if(physic_get_velocity(&pplayer->vphysic) < 0.)
472 physic_set_state(&pplayer->vphysic,PH_VT);
473 physic_set_start_vy(&pplayer->vphysic,0);
478 if(!physic_is_set(&pplayer->vphysic))
480 physic_set_state(&pplayer->vphysic,PH_VT);
487 void player_input(player_type *pplayer)
489 /* Handle key and joystick state: */
491 if(pplayer->duck == NO)
493 if (pplayer->input.right == DOWN && pplayer->input.left == UP)
495 player_handle_horizontal_input(pplayer,RIGHT);
497 else if (pplayer->input.left == DOWN && pplayer->input.right == UP)
499 player_handle_horizontal_input(pplayer,LEFT);
503 if(pplayer->base.xm > 0)
505 pplayer->base.xm = (int)(pplayer->base.xm - frame_ratio);
506 if(pplayer->base.xm < 0)
507 pplayer->base.xm = 0;
509 else if(pplayer->base.xm < 0)
511 pplayer->base.xm = (int)(pplayer->base.xm + frame_ratio);
512 if(pplayer->base.xm > 0)
513 pplayer->base.xm = 0;
520 if ( pplayer->input.up == DOWN || (pplayer->input.up == UP && pplayer->jumping == YES))
522 player_handle_vertical_input(pplayer);
527 if (pplayer->input.fire == DOWN && pplayer->input.old_fire == UP && pplayer->got_coffee)
529 add_bullet(pplayer->base.x, pplayer->base.y, pplayer->base.xm, pplayer->dir);
535 if (pplayer->input.down == DOWN)
537 if (pplayer->size == BIG && pplayer->duck != YES)
540 pplayer->base.height = 32;
541 pplayer->base.y += 32;
546 if (pplayer->size == BIG && pplayer->duck == YES)
548 /* Make sure we're not standing back up into a solid! */
549 pplayer->base.height = 64;
550 pplayer->base.y -= 32;
552 if (!collision_object_map(&pplayer->base) /*issolid(pplayer->base.x + 16, pplayer->base.y - 16)*/)
555 pplayer->base.height = 64;
556 pplayer->old_base.y -= 32;
557 pplayer->old_base.height = 64;
561 pplayer->base.height = 32;
562 pplayer->base.y += 32;
573 if(!timer_check(&pplayer->frame_timer))
575 timer_start(&pplayer->frame_timer,25);
576 if (pplayer->input.right == UP && pplayer->input.left == UP)
578 pplayer->frame_main = 1;
583 if ((pplayer->input.fire == DOWN && (frame % 2) == 0) ||
585 pplayer->frame_main = (pplayer->frame_main + 1) % 4;
587 pplayer->frame = pplayer->frame_main;
589 if (pplayer->frame == 3)
596 void player_grabdistros(player_type *pplayer)
601 trygrabdistro(pplayer->base.x, pplayer->base.y, NO_BOUNCE);
602 trygrabdistro(pplayer->base.x+ 31, pplayer->base.y, NO_BOUNCE);
604 trygrabdistro(pplayer->base.x, pplayer->base.y + pplayer->base.height, NO_BOUNCE);
605 trygrabdistro(pplayer->base.x+ 31, pplayer->base.y + pplayer->base.height, NO_BOUNCE);
607 if(pplayer->size == BIG)
609 trygrabdistro(pplayer->base.x, pplayer->base.y + pplayer->base.height / 2, NO_BOUNCE);
610 trygrabdistro(pplayer->base.x+ 31, pplayer->base.y + pplayer->base.height / 2, NO_BOUNCE);
616 /* Enough distros for a One-up? */
618 if (distros >= DISTROS_LIFEUP)
620 distros = distros - DISTROS_LIFEUP;
621 if(pplayer->lives < MAX_LIVES)
623 /*We want to hear the sound even, if MAX_LIVES is reached*/
624 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
628 void player_draw(player_type* pplayer)
630 if (!timer_started(&pplayer->safe_timer) || (frame % 2) == 0)
632 if (pplayer->size == SMALL)
634 if (timer_started(&pplayer->invincible_timer))
638 if (pplayer->dir == RIGHT)
640 texture_draw(&cape_right[frame % 2],
641 pplayer->base.x- scroll_x, pplayer->base.y,
646 texture_draw(&cape_left[frame % 2],
647 pplayer->base.x- scroll_x, pplayer->base.y,
653 if (!pplayer->got_coffee)
655 if (pplayer->dir == RIGHT)
657 texture_draw(&tux_right[pplayer->frame], pplayer->base.x- scroll_x, pplayer->base.y, NO_UPDATE);
661 texture_draw(&tux_left[pplayer->frame], pplayer->base.x- scroll_x, pplayer->base.y, NO_UPDATE);
666 /* Tux got coffee! */
668 if (pplayer->dir == RIGHT)
670 texture_draw(&firetux_right[pplayer->frame], pplayer->base.x- scroll_x, pplayer->base.y, NO_UPDATE);
674 texture_draw(&firetux_left[pplayer->frame], pplayer->base.x- scroll_x, pplayer->base.y, NO_UPDATE);
680 if (timer_started(&pplayer->invincible_timer))
684 if (pplayer->dir == RIGHT)
686 texture_draw(&bigcape_right[frame % 2],
687 pplayer->base.x- scroll_x - 8, pplayer->base.y,
692 texture_draw(&bigcape_left[frame % 2],
693 pplayer->base.x-scroll_x - 8, pplayer->base.y,
698 if (!pplayer->got_coffee)
702 if (!timer_started(&pplayer->skidding_timer))
704 if (!pplayer->jumping || pplayer->base.ym > 0)
706 if (pplayer->dir == RIGHT)
708 texture_draw(&bigtux_right[pplayer->frame],
709 pplayer->base.x- scroll_x - 8, pplayer->base.y,
714 texture_draw(&bigtux_left[pplayer->frame],
715 pplayer->base.x- scroll_x - 8, pplayer->base.y,
721 if (pplayer->dir == RIGHT)
723 texture_draw(&bigtux_right_jump,
724 pplayer->base.x- scroll_x - 8, pplayer->base.y,
729 texture_draw(&bigtux_left_jump,
730 pplayer->base.x- scroll_x - 8, pplayer->base.y,
737 if (pplayer->dir == RIGHT)
739 texture_draw(&skidtux_right,
740 pplayer->base.x- scroll_x - 8, pplayer->base.y,
745 texture_draw(&skidtux_left,
746 pplayer->base.x- scroll_x - 8, pplayer->base.y,
753 if (pplayer->dir == RIGHT)
755 texture_draw(&ducktux_right, pplayer->base.x- scroll_x - 8, pplayer->base.y - 16,
760 texture_draw(&ducktux_left, pplayer->base.x- scroll_x - 8, pplayer->base.y - 16,
767 /* Tux has coffee! */
771 if (!timer_started(&pplayer->skidding_timer))
773 if (!pplayer->jumping || pplayer->base.ym > 0)
775 if (pplayer->dir == RIGHT)
777 texture_draw(&bigfiretux_right[pplayer->frame],
778 pplayer->base.x- scroll_x - 8, pplayer->base.y,
783 texture_draw(&bigfiretux_left[pplayer->frame],
784 pplayer->base.x- scroll_x - 8, pplayer->base.y,
790 if (pplayer->dir == RIGHT)
792 texture_draw(&bigfiretux_right_jump,
793 pplayer->base.x- scroll_x - 8, pplayer->base.y,
798 texture_draw(&bigfiretux_left_jump,
799 pplayer->base.x- scroll_x - 8, pplayer->base.y,
806 if (pplayer->dir == RIGHT)
808 texture_draw(&skidfiretux_right,
809 pplayer->base.x- scroll_x - 8, pplayer->base.y,
814 texture_draw(&skidfiretux_left,
815 pplayer->base.x- scroll_x - 8, pplayer->base.y,
822 if (pplayer->dir == RIGHT)
824 texture_draw(&duckfiretux_right, pplayer->base.x- scroll_x - 8, pplayer->base.y - 16,
829 texture_draw(&duckfiretux_left, pplayer->base.x- scroll_x - 8, pplayer->base.y - 16,
838 void player_collision(player_type* pplayer, void* p_c_object, int c_object)
840 bad_guy_type* pbad_c = NULL;
845 pbad_c = (bad_guy_type*) p_c_object;
846 /* Hurt the player if he just touched it: */
848 if (!pbad_c->dying && !pplayer->dying &&
849 !timer_started(&pplayer->safe_timer) &&
850 pbad_c->mode != HELD)
852 if (pbad_c->mode == FLAT && pplayer->input.fire != DOWN)
857 play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
859 if (pplayer->base.x<= pbad_c->base.x)
862 pbad_c->base.x = pbad_c->base.x + 16;
867 pbad_c->base.x = pbad_c->base.x - 32;
870 timer_start(&pbad_c->timer,5000);
872 else if (pbad_c->mode == FLAT && pplayer->input.fire == DOWN)
877 else if (pbad_c->mode == KICK)
879 if (pplayer->base.y < pbad_c->base.y - 16 &&
880 timer_started(&pbad_c->timer))
882 /* Step on (stop being kicked) */
885 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
886 timer_start(&pbad_c->timer, 10000);
890 /* Hurt if you get hit by kicked laptop: */
892 if (timer_started(&pbad_c->timer))
894 if (!timer_started(&pplayer->invincible_timer))
896 player_kill(pplayer,SHRINK);
900 pbad_c->dying = FALLING;
901 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
902 add_score(pbad_c->base.x - scroll_x,
904 25 * score_multiplier);
911 if (!timer_started(&pplayer->invincible_timer ))
913 player_kill(pplayer,SHRINK);
917 pbad_c->dying = FALLING;
918 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
919 add_score(pbad_c->base.x - scroll_x,
921 25 * score_multiplier);
935 void player_kill(player_type* pplayer, int mode)
937 pplayer->base.ym = -5;
939 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
941 if (pplayer->dir == RIGHT)
942 pplayer->base.xm = -8;
943 else if (pplayer->dir == LEFT)
944 pplayer->base.xm = 8;
946 if (mode == SHRINK && pplayer->size == BIG)
948 if (pplayer->got_coffee)
949 pplayer->got_coffee = NO;
951 pplayer->size = SMALL;
952 pplayer->base.height = 32;
954 timer_start(&pplayer->safe_timer,TUX_SAFE_TIME);
962 void player_dying(player_type *pplayer)
964 pplayer->base.ym = pplayer->base.ym + gravity;
969 player_remove_powerups(pplayer);
972 player_level_begin(pplayer);
976 /* Remove Tux's power ups */
977 void player_remove_powerups(player_type* pplayer)
979 pplayer->got_coffee = NO;
980 pplayer->size = SMALL;
981 pplayer->base.height = 32;
984 void player_keep_in_bounds(player_type* pplayer)
986 /* Keep tux in bounds: */
987 if (pplayer->base.x< 0)
989 else if(pplayer->base.x< scroll_x)
990 pplayer->base.x= scroll_x;
991 else if (pplayer->base.x< 160 + scroll_x && scroll_x > 0 && debug_mode == YES)
993 scroll_x = pplayer->base.x- 160;
994 /*pplayer->base.x+= 160;*/
1000 else if (pplayer->base.x> screen->w / 2 + scroll_x && scroll_x < ((current_level.width * 32) - screen->w))
1002 /* Scroll the screen in past center: */
1004 scroll_x = pplayer->base.x- screen->w / 2;
1005 /*pplayer->base.x= 320 + scroll_x;*/
1007 if (scroll_x > ((current_level.width * 32) - screen->w))
1008 scroll_x = ((current_level.width * 32) - screen->w);
1010 else if (pplayer->base.x> 608 + scroll_x)
1012 /* ... unless there's no more to scroll! */
1014 /*pplayer->base.x= 608 + scroll_x;*/
1017 /* Keep in-bounds, vertically: */
1019 if (pplayer->base.y > screen->h)
1021 player_kill(&tux,KILL);