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
21 texture_type tux_life;
22 texture_type tux_right[3];
23 texture_type tux_left[3];
24 texture_type bigtux_right[3];
25 texture_type bigtux_left[3];
26 texture_type bigtux_right_jump;
27 texture_type bigtux_left_jump;
28 texture_type ducktux_right;
29 texture_type ducktux_left;
30 texture_type skidtux_right;
31 texture_type skidtux_left;
32 texture_type firetux_right[3];
33 texture_type firetux_left[3];
34 texture_type bigfiretux_right[3];
35 texture_type bigfiretux_left[3];
36 texture_type bigfiretux_right_jump;
37 texture_type bigfiretux_left_jump;
38 texture_type duckfiretux_right;
39 texture_type duckfiretux_left;
40 texture_type skidfiretux_right;
41 texture_type skidfiretux_left;
42 texture_type cape_right[2];
43 texture_type cape_left[2];
44 texture_type bigcape_right[2];
45 texture_type bigcape_left[2];
47 void player_input_init(player_input_type* pplayer_input)
49 pplayer_input->down = UP;
50 pplayer_input->fire = UP;
51 pplayer_input->left = UP;
52 pplayer_input->old_fire = UP;
53 pplayer_input->right = UP;
54 pplayer_input->up = UP;
66 // FIXME: Make the start position configurable via the levelfile
85 player_input_init(&input);
87 keymap.jump = SDLK_UP;
88 keymap.duck = SDLK_DOWN;
89 keymap.left = SDLK_LEFT;
90 keymap.right = SDLK_RIGHT;
91 keymap.fire = SDLK_LCTRL;
93 timer_init(&invincible_timer,true);
94 timer_init(&skidding_timer,true);
95 timer_init(&safe_timer,true);
96 timer_init(&frame_timer,true);
97 physic_init(&hphysic);
98 physic_init(&vphysic);
102 Player::key_event(SDLKey key, int state)
104 if(key == keymap.right)
109 else if(key == keymap.left)
114 else if(key == keymap.jump)
119 else if(key == keymap.duck)
124 else if(key == keymap.fire)
134 Player::level_begin()
142 player_input_init(&input);
144 timer_init(&invincible_timer,true);
145 timer_init(&skidding_timer,true);
146 timer_init(&safe_timer,true);
147 timer_init(&frame_timer,true);
148 physic_init(&hphysic);
149 physic_init(&vphysic);
155 bool jumped_in_solid = false;
157 /* --- HANDLE TUX! --- */
163 previous_base = base;
165 base.x += base.xm * frame_ratio;
166 base.y += base.ym * frame_ratio;
168 collision_swept_object_map(&old_base,&base);
180 physic_set_state(&vphysic,PH_VT);
181 physic_set_start_vy(&vphysic,0);
182 jumped_in_solid = true;
186 if(!physic_is_set(&vphysic))
188 physic_set_state(&vphysic,PH_VT);
189 physic_set_start_vy(&vphysic,0);
192 base.ym = physic_get_velocity(&vphysic);
202 base.y = (int)(((int)base.y / 32) * 32);
206 physic_init(&vphysic);
208 /* Reset score multiplier (for multi-hits): */
210 score_multiplier = 1;
215 if (isbrick(base.x, base.y) ||
216 isfullbox(base.x, base.y))
218 trygrabdistro(base.x, base.y - 32,BOUNCE);
219 trybumpbadguy(base.x, base.y - 64);
222 trybreakbrick(base.x, base.y);
224 bumpbrick(base.x, base.y);
225 tryemptybox(base.x, base.y, RIGHT);
228 if (isbrick(base.x+ 31, base.y) ||
229 isfullbox(base.x+ 31, base.y))
231 trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
232 trybumpbadguy(base.x+ 31, base.y - 64);
235 trybreakbrick(base.x+ 31, base.y);
237 bumpbrick(base.x+ 31, base.y);
238 tryemptybox(base.x+ 31, base.y, LEFT);
244 Tile* tile = gettile(base.x, base.y);
245 /* Get a distro from a brick? */
248 add_bouncy_distro((((int)base.x)
250 ((int)base.y / 32) * 32);
252 if (counting_distros == false)
254 counting_distros = true;
255 distro_counter = 100;
258 if (distro_counter <= 0)
259 level_change(¤t_level,base.x,base.y - 1, TM_IA, tile->next_tile);
261 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
262 score = score + SCORE_DISTRO;
268 Tile* tile2 = gettile(base.x + 31, base.y);
271 add_bouncy_distro((((int)base.x + 31)
273 ((int)base.y / 32) * 32);
274 if (counting_distros == false)
276 counting_distros = true;
277 distro_counter = 100;
280 if (distro_counter <= 0)
281 level_change(¤t_level,base.x+ 31, base.y, TM_IA, tile2->next_tile);
283 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
284 score = score + SCORE_DISTRO;
298 /* Make sure jumping is off. */
305 timer_check(&safe_timer);
308 /* ---- DONE HANDLING TUX! --- */
310 /* Handle invincibility timer: */
313 if (get_current_music() == HERRING_MUSIC && !timer_check(&invincible_timer))
316 no, we are no more invincible
317 or we were not in invincible mode
318 but are we in hurry ?
322 if (timer_get_left(&time_left) < TIME_WARNING)
324 /* yes, we are in hurry
325 stop the herring_song, prepare to play the correct
328 set_current_music(HURRYUP_MUSIC);
332 set_current_music(LEVEL_MUSIC);
335 /* start playing it */
336 play_current_music();
339 /* Handle skidding: */
341 timer_check(&skidding_timer);
345 if (base.x >= endpos && endpos != 0)
355 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
356 issolid(base.x + 1, base.y + base.height) ||
357 issolid(base.x + base.width - 1, base.y + base.height) );
361 Player::under_solid()
363 return ( issolid(base.x + base.width / 2, base.y) ||
364 issolid(base.x + 1, base.y) ||
365 issolid(base.x + base.width - 1, base.y) );
369 Player::handle_horizontal_input(int newdir)
371 if ((newdir ? (base.xm < -SKID_XM) : (base.xm > SKID_XM)) && !timer_started(&skidding_timer) &&
372 dir == !newdir && on_ground())
374 timer_start(&skidding_timer, SKID_TIME);
376 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
382 if ((newdir ? (base.xm < 0) : (base.xm > 0)) && !isice(base.x, base.y + base.height) &&
383 !timer_started(&skidding_timer))
392 /* Facing the direction we're jumping? Go full-speed: */
394 if (input.fire == UP)
396 base.xm = base.xm + ( newdir ? WALK_SPEED : -WALK_SPEED) * frame_ratio;
400 if (base.xm > MAX_WALK_XM)
401 base.xm = MAX_WALK_XM;
405 if (base.xm < -MAX_WALK_XM)
406 base.xm = -MAX_WALK_XM;
409 else if ( input.fire == DOWN)
411 base.xm = base.xm + ( newdir ? RUN_SPEED : -RUN_SPEED) * frame_ratio;
415 if (base.xm > MAX_RUN_XM)
416 base.xm = MAX_RUN_XM;
420 if (base.xm < -MAX_RUN_XM)
421 base.xm = -MAX_RUN_XM;
426 /* Not facing the direction we're jumping?
429 base.xm = base.xm + ( newdir ? (WALK_SPEED / 2) : -(WALK_SPEED / 2)) * frame_ratio;
433 if (base.xm > MAX_WALK_XM / 2)
434 base.xm = MAX_WALK_XM / 2;
438 if (base.xm < -MAX_WALK_XM / 2)
439 base.xm = -MAX_WALK_XM / 2;
448 Player::handle_vertical_input()
454 if(!physic_is_set(&vphysic))
456 physic_set_state(&vphysic,PH_VT);
457 physic_set_start_vy(&vphysic,5.5);
461 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
463 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
467 else if(input.up == UP && jumping)
471 physic_init(&vphysic);
477 if(physic_is_set(&vphysic))
479 if(physic_get_velocity(&vphysic) < 0.)
481 physic_set_state(&vphysic,PH_VT);
482 physic_set_start_vy(&vphysic,0);
487 if(!physic_is_set(&vphysic))
489 physic_set_state(&vphysic,PH_VT);
497 Player::handle_input()
499 /* Handle key and joystick state: */
502 if (input.right == DOWN && input.left == UP)
504 handle_horizontal_input(RIGHT);
506 else if (input.left == DOWN && input.right == UP)
508 handle_horizontal_input(LEFT);
514 base.xm = (int)(base.xm - frame_ratio);
520 base.xm = (int)(base.xm + frame_ratio);
529 if ( input.up == DOWN || (input.up == UP && jumping))
531 handle_vertical_input();
536 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
538 add_bullet(base.x, base.y, base.xm, dir);
544 if (input.down == DOWN)
546 if (size == BIG && duck != true)
555 if (size == BIG && duck)
557 /* Make sure we're not standing back up into a solid! */
561 if (!collision_object_map(&base) /*issolid(base.x + 16, base.y - 16)*/)
566 old_base.height = 64;
582 if(!timer_check(&frame_timer))
584 timer_start(&frame_timer,25);
585 if (input.right == UP && input.left == UP)
592 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
593 (global_frame_counter % 4) == 0)
594 frame_main = (frame_main + 1) % 4;
606 Player::grabdistros()
611 trygrabdistro(base.x, base.y, NO_BOUNCE);
612 trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
614 trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
615 trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
619 trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
620 trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
625 /* Enough distros for a One-up? */
626 if (distros >= DISTROS_LIFEUP)
628 distros = distros - DISTROS_LIFEUP;
629 if(lives < MAX_LIVES)
631 /*We want to hear the sound even, if MAX_LIVES is reached*/
632 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
639 if (!timer_started(&safe_timer) || (global_frame_counter % 2) == 0)
643 if (timer_started(&invincible_timer))
649 texture_draw(&cape_right[global_frame_counter % 2],
650 base.x- scroll_x, base.y);
654 texture_draw(&cape_left[global_frame_counter % 2],
655 base.x- scroll_x, base.y);
664 texture_draw(&tux_right[frame_], base.x- scroll_x, base.y);
668 texture_draw(&tux_left[frame_], base.x- scroll_x, base.y);
673 /* Tux got coffee! */
677 texture_draw(&firetux_right[frame_], base.x- scroll_x, base.y);
681 texture_draw(&firetux_left[frame_], base.x- scroll_x, base.y);
687 if (timer_started(&invincible_timer))
692 texture_draw(&bigcape_right[global_frame_counter % 2],
693 base.x- scroll_x - 8, base.y);
697 texture_draw(&bigcape_left[global_frame_counter % 2],
698 base.x-scroll_x - 8, base.y);
706 if (!timer_started(&skidding_timer))
708 if (!jumping || base.ym > 0)
712 texture_draw(&bigtux_right[frame_],
713 base.x- scroll_x - 8, base.y);
717 texture_draw(&bigtux_left[frame_],
718 base.x- scroll_x - 8, base.y);
725 texture_draw(&bigtux_right_jump,
726 base.x- scroll_x - 8, base.y);
730 texture_draw(&bigtux_left_jump,
731 base.x- scroll_x - 8, base.y);
739 texture_draw(&skidtux_right,
740 base.x- scroll_x - 8, base.y);
744 texture_draw(&skidtux_left,
745 base.x- scroll_x - 8, base.y);
753 texture_draw(&ducktux_right, base.x- scroll_x - 8, base.y - 16);
757 texture_draw(&ducktux_left, base.x- scroll_x - 8, base.y - 16);
763 /* Tux has coffee! */
767 if (!timer_started(&skidding_timer))
769 if (!jumping || base.ym > 0)
773 texture_draw(&bigfiretux_right[frame_],
774 base.x- scroll_x - 8, base.y);
778 texture_draw(&bigfiretux_left[frame_],
779 base.x- scroll_x - 8, base.y);
786 texture_draw(&bigfiretux_right_jump,
787 base.x- scroll_x - 8, base.y);
791 texture_draw(&bigfiretux_left_jump,
792 base.x- scroll_x - 8, base.y);
800 texture_draw(&skidfiretux_right,
801 base.x- scroll_x - 8, base.y);
805 texture_draw(&skidfiretux_left,
806 base.x- scroll_x - 8, base.y);
814 texture_draw(&duckfiretux_right, base.x- scroll_x - 8, base.y - 16);
818 texture_draw(&duckfiretux_left, base.x- scroll_x - 8, base.y - 16);
827 Player::collision(void* p_c_object, int c_object)
829 BadGuy* pbad_c = NULL;
834 pbad_c = (BadGuy*) p_c_object;
835 /* Hurt the player if he just touched it: */
837 if (!pbad_c->dying && !dying &&
838 !timer_started(&safe_timer) &&
839 pbad_c->mode != HELD)
841 if (pbad_c->mode == FLAT && input.fire != DOWN)
846 play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
848 if (base.x < pbad_c->base.x + (pbad_c->base.width/2))
851 pbad_c->base.x = pbad_c->base.x + 16;
856 pbad_c->base.x = pbad_c->base.x - 32;
859 timer_start(&pbad_c->timer,5000);
861 else if (pbad_c->mode == FLAT && input.fire == DOWN)
866 else if (pbad_c->mode == KICK)
868 if (base.y < pbad_c->base.y - 16 &&
869 timer_started(&pbad_c->timer))
871 /* Step on (stop being kicked) */
874 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
875 timer_start(&pbad_c->timer, 10000);
879 /* Hurt if you get hit by kicked laptop: */
881 if (timer_started(&pbad_c->timer))
883 if (!timer_started(&invincible_timer))
889 pbad_c->dying = DYING_FALLING;
890 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
891 add_score(pbad_c->base.x - scroll_x,
893 25 * score_multiplier);
900 if (!timer_started(&invincible_timer ))
906 pbad_c->dying = DYING_FALLING;
907 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
908 add_score(pbad_c->base.x - scroll_x,
910 25 * score_multiplier);
925 Player::kill(int mode)
929 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
933 else if (dir == LEFT)
936 if (mode == SHRINK && size == BIG)
944 timer_start(&safe_timer,TUX_SAFE_TIME);
948 dying = DYING_SQUISHED;
955 base.ym = base.ym + gravity;
967 /* Remove Tux's power ups */
969 Player::remove_powerups()
977 Player::keep_in_bounds()
979 /* Keep tux in bounds: */
982 else if(base.x< scroll_x)
984 else if (base.x< 160 + scroll_x && scroll_x > 0 && debug_mode)
986 scroll_x = base.x- 160;
993 else if (base.x> screen->w / 2 + scroll_x && scroll_x < ((current_level.width * 32) - screen->w))
995 /* Scroll the screen in past center: */
997 scroll_x = base.x- screen->w / 2;
998 /*base.x= 320 + scroll_x;*/
1000 if (scroll_x > ((current_level.width * 32) - screen->w))
1001 scroll_x = ((current_level.width * 32) - screen->w);
1003 else if (base.x> 608 + scroll_x)
1005 /* ... unless there's no more to scroll! */
1007 /*base.x= 608 + scroll_x;*/
1010 /* Keep in-bounds, vertically: */
1012 if (base.y > screen->h)