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 Tile* tile2 = gettile(base.x + 31, base.y);
246 /* Get a distro from a brick? */
249 add_bouncy_distro((((int)base.x)
251 ((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;
265 else if (tile2->brick)
267 add_bouncy_distro((((int)base.x + 31)
269 ((int)base.y / 32) * 32);
270 if (counting_distros == false)
272 counting_distros = true;
273 distro_counter = 100;
276 if (distro_counter <= 0)
277 level_change(¤t_level,base.x+ 31, base.y, TM_IA, tile2->next_tile);
279 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
280 score = score + SCORE_DISTRO;
294 /* Make sure jumping is off. */
301 timer_check(&safe_timer);
304 /* ---- DONE HANDLING TUX! --- */
306 /* Handle invincibility timer: */
309 if (get_current_music() == HERRING_MUSIC && !timer_check(&invincible_timer))
312 no, we are no more invincible
313 or we were not in invincible mode
314 but are we in hurry ?
318 if (timer_get_left(&time_left) < TIME_WARNING)
320 /* yes, we are in hurry
321 stop the herring_song, prepare to play the correct
324 set_current_music(HURRYUP_MUSIC);
328 set_current_music(LEVEL_MUSIC);
331 /* start playing it */
332 play_current_music();
335 /* Handle skidding: */
337 timer_check(&skidding_timer);
341 if (base.x >= endpos && endpos != 0)
351 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
352 issolid(base.x + 1, base.y + base.height) ||
353 issolid(base.x + base.width - 1, base.y + base.height) );
357 Player::under_solid()
359 return ( issolid(base.x + base.width / 2, base.y) ||
360 issolid(base.x + 1, base.y) ||
361 issolid(base.x + base.width - 1, base.y) );
365 Player::handle_horizontal_input(int newdir)
367 if ((newdir ? (base.xm < -SKID_XM) : (base.xm > SKID_XM)) && !timer_started(&skidding_timer) &&
368 dir == !newdir && on_ground())
370 timer_start(&skidding_timer, SKID_TIME);
372 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
378 if ((newdir ? (base.xm < 0) : (base.xm > 0)) && !isice(base.x, base.y + base.height) &&
379 !timer_started(&skidding_timer))
388 /* Facing the direction we're jumping? Go full-speed: */
390 if (input.fire == UP)
392 base.xm = base.xm + ( newdir ? WALK_SPEED : -WALK_SPEED) * frame_ratio;
396 if (base.xm > MAX_WALK_XM)
397 base.xm = MAX_WALK_XM;
401 if (base.xm < -MAX_WALK_XM)
402 base.xm = -MAX_WALK_XM;
405 else if ( input.fire == DOWN)
407 base.xm = base.xm + ( newdir ? RUN_SPEED : -RUN_SPEED) * frame_ratio;
411 if (base.xm > MAX_RUN_XM)
412 base.xm = MAX_RUN_XM;
416 if (base.xm < -MAX_RUN_XM)
417 base.xm = -MAX_RUN_XM;
422 /* Not facing the direction we're jumping?
425 base.xm = base.xm + ( newdir ? (WALK_SPEED / 2) : -(WALK_SPEED / 2)) * frame_ratio;
429 if (base.xm > MAX_WALK_XM / 2)
430 base.xm = MAX_WALK_XM / 2;
434 if (base.xm < -MAX_WALK_XM / 2)
435 base.xm = -MAX_WALK_XM / 2;
444 Player::handle_vertical_input()
450 if(!physic_is_set(&vphysic))
452 physic_set_state(&vphysic,PH_VT);
453 physic_set_start_vy(&vphysic,5.5);
457 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
459 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
463 else if(input.up == UP && jumping)
467 physic_init(&vphysic);
473 if(physic_is_set(&vphysic))
475 if(physic_get_velocity(&vphysic) < 0.)
477 physic_set_state(&vphysic,PH_VT);
478 physic_set_start_vy(&vphysic,0);
483 if(!physic_is_set(&vphysic))
485 physic_set_state(&vphysic,PH_VT);
493 Player::handle_input()
495 /* Handle key and joystick state: */
498 if (input.right == DOWN && input.left == UP)
500 handle_horizontal_input(RIGHT);
502 else if (input.left == DOWN && input.right == UP)
504 handle_horizontal_input(LEFT);
510 base.xm = (int)(base.xm - frame_ratio);
516 base.xm = (int)(base.xm + frame_ratio);
525 if ( input.up == DOWN || (input.up == UP && jumping))
527 handle_vertical_input();
532 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
534 add_bullet(base.x, base.y, base.xm, dir);
540 if (input.down == DOWN)
542 if (size == BIG && duck != true)
551 if (size == BIG && duck)
553 /* Make sure we're not standing back up into a solid! */
557 if (!collision_object_map(&base) /*issolid(base.x + 16, base.y - 16)*/)
562 old_base.height = 64;
578 if(!timer_check(&frame_timer))
580 timer_start(&frame_timer,25);
581 if (input.right == UP && input.left == UP)
588 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
589 (global_frame_counter % 4) == 0)
590 frame_main = (frame_main + 1) % 4;
602 Player::grabdistros()
607 trygrabdistro(base.x, base.y, NO_BOUNCE);
608 trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
610 trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
611 trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
615 trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
616 trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
621 /* Enough distros for a One-up? */
622 if (distros >= DISTROS_LIFEUP)
624 distros = distros - DISTROS_LIFEUP;
625 if(lives < MAX_LIVES)
627 /*We want to hear the sound even, if MAX_LIVES is reached*/
628 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
635 if (!timer_started(&safe_timer) || (global_frame_counter % 2) == 0)
639 if (timer_started(&invincible_timer))
645 texture_draw(&cape_right[global_frame_counter % 2],
646 base.x- scroll_x, base.y);
650 texture_draw(&cape_left[global_frame_counter % 2],
651 base.x- scroll_x, base.y);
660 texture_draw(&tux_right[frame_], base.x- scroll_x, base.y);
664 texture_draw(&tux_left[frame_], base.x- scroll_x, base.y);
669 /* Tux got coffee! */
673 texture_draw(&firetux_right[frame_], base.x- scroll_x, base.y);
677 texture_draw(&firetux_left[frame_], base.x- scroll_x, base.y);
683 if (timer_started(&invincible_timer))
688 texture_draw(&bigcape_right[global_frame_counter % 2],
689 base.x- scroll_x - 8, base.y);
693 texture_draw(&bigcape_left[global_frame_counter % 2],
694 base.x-scroll_x - 8, base.y);
702 if (!timer_started(&skidding_timer))
704 if (!jumping || base.ym > 0)
708 texture_draw(&bigtux_right[frame_],
709 base.x- scroll_x - 8, base.y);
713 texture_draw(&bigtux_left[frame_],
714 base.x- scroll_x - 8, base.y);
721 texture_draw(&bigtux_right_jump,
722 base.x- scroll_x - 8, base.y);
726 texture_draw(&bigtux_left_jump,
727 base.x- scroll_x - 8, base.y);
735 texture_draw(&skidtux_right,
736 base.x- scroll_x - 8, base.y);
740 texture_draw(&skidtux_left,
741 base.x- scroll_x - 8, base.y);
749 texture_draw(&ducktux_right, base.x- scroll_x - 8, base.y - 16);
753 texture_draw(&ducktux_left, base.x- scroll_x - 8, base.y - 16);
759 /* Tux has coffee! */
763 if (!timer_started(&skidding_timer))
765 if (!jumping || base.ym > 0)
769 texture_draw(&bigfiretux_right[frame_],
770 base.x- scroll_x - 8, base.y);
774 texture_draw(&bigfiretux_left[frame_],
775 base.x- scroll_x - 8, base.y);
782 texture_draw(&bigfiretux_right_jump,
783 base.x- scroll_x - 8, base.y);
787 texture_draw(&bigfiretux_left_jump,
788 base.x- scroll_x - 8, base.y);
796 texture_draw(&skidfiretux_right,
797 base.x- scroll_x - 8, base.y);
801 texture_draw(&skidfiretux_left,
802 base.x- scroll_x - 8, base.y);
810 texture_draw(&duckfiretux_right, base.x- scroll_x - 8, base.y - 16);
814 texture_draw(&duckfiretux_left, base.x- scroll_x - 8, base.y - 16);
823 Player::collision(void* p_c_object, int c_object)
825 BadGuy* pbad_c = NULL;
830 pbad_c = (BadGuy*) p_c_object;
831 /* Hurt the player if he just touched it: */
833 if (!pbad_c->dying && !dying &&
834 !timer_started(&safe_timer) &&
835 pbad_c->mode != HELD)
837 if (pbad_c->mode == FLAT && input.fire != DOWN)
842 play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
844 if (base.x < pbad_c->base.x + (pbad_c->base.width/2))
847 pbad_c->base.x = pbad_c->base.x + 16;
852 pbad_c->base.x = pbad_c->base.x - 32;
855 timer_start(&pbad_c->timer,5000);
857 else if (pbad_c->mode == FLAT && input.fire == DOWN)
862 else if (pbad_c->mode == KICK)
864 if (base.y < pbad_c->base.y - 16 &&
865 timer_started(&pbad_c->timer))
867 /* Step on (stop being kicked) */
870 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
871 timer_start(&pbad_c->timer, 10000);
875 /* Hurt if you get hit by kicked laptop: */
877 if (timer_started(&pbad_c->timer))
879 if (!timer_started(&invincible_timer))
885 pbad_c->dying = DYING_FALLING;
886 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
887 add_score(pbad_c->base.x - scroll_x,
889 25 * score_multiplier);
896 if (!timer_started(&invincible_timer ))
902 pbad_c->dying = DYING_FALLING;
903 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
904 add_score(pbad_c->base.x - scroll_x,
906 25 * score_multiplier);
921 Player::kill(int mode)
925 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
929 else if (dir == LEFT)
932 if (mode == SHRINK && size == BIG)
940 timer_start(&safe_timer,TUX_SAFE_TIME);
944 dying = DYING_SQUISHED;
951 base.ym = base.ym + gravity;
963 /* Remove Tux's power ups */
965 Player::remove_powerups()
973 Player::keep_in_bounds()
975 /* Keep tux in bounds: */
978 else if(base.x< scroll_x)
980 else if (base.x< 160 + scroll_x && scroll_x > 0 && debug_mode)
982 scroll_x = base.x- 160;
989 else if (base.x> screen->w / 2 + scroll_x && scroll_x < ((current_level.width * 32) - screen->w))
991 /* Scroll the screen in past center: */
993 scroll_x = base.x- screen->w / 2;
994 /*base.x= 320 + scroll_x;*/
996 if (scroll_x > ((current_level.width * 32) - screen->w))
997 scroll_x = ((current_level.width * 32) - screen->w);
999 else if (base.x> 608 + scroll_x)
1001 /* ... unless there's no more to scroll! */
1003 /*base.x= 608 + scroll_x;*/
1006 /* Keep in-bounds, vertically: */
1008 if (base.y > screen->h)