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 texture_type tux_right[3];
22 texture_type tux_left[3];
23 texture_type bigtux_right[3];
24 texture_type bigtux_left[3];
25 texture_type bigtux_right_jump;
26 texture_type bigtux_left_jump;
27 texture_type ducktux_right;
28 texture_type ducktux_left;
29 texture_type skidtux_right;
30 texture_type skidtux_left;
31 texture_type firetux_right[3];
32 texture_type firetux_left[3];
33 texture_type bigfiretux_right[3];
34 texture_type bigfiretux_left[3];
35 texture_type bigfiretux_right_jump;
36 texture_type bigfiretux_left_jump;
37 texture_type duckfiretux_right;
38 texture_type duckfiretux_left;
39 texture_type skidfiretux_right;
40 texture_type skidfiretux_left;
41 texture_type cape_right[2];
42 texture_type cape_left[2];
43 texture_type bigcape_right[2];
44 texture_type bigcape_left[2];
46 void player_input_init(player_input_type* pplayer_input)
48 pplayer_input->down = UP;
49 pplayer_input->fire = UP;
50 pplayer_input->left = UP;
51 pplayer_input->old_fire = UP;
52 pplayer_input->right = UP;
53 pplayer_input->up = UP;
65 // FIXME: Make the start position configurable via the levelfile
84 player_input_init(&input);
86 keymap.jump = SDLK_UP;
87 keymap.duck = SDLK_DOWN;
88 keymap.left = SDLK_LEFT;
89 keymap.right = SDLK_RIGHT;
90 keymap.fire = SDLK_LCTRL;
92 timer_init(&invincible_timer,true);
93 timer_init(&skidding_timer,true);
94 timer_init(&safe_timer,true);
95 timer_init(&frame_timer,true);
96 physic_init(&hphysic);
97 physic_init(&vphysic);
101 Player::key_event(SDLKey key, int state)
103 if(key == keymap.right)
108 else if(key == keymap.left)
113 else if(key == keymap.jump)
118 else if(key == keymap.duck)
123 else if(key == keymap.fire)
133 Player::level_begin()
141 player_input_init(&input);
143 timer_init(&invincible_timer,true);
144 timer_init(&skidding_timer,true);
145 timer_init(&safe_timer,true);
146 timer_init(&frame_timer,true);
147 physic_init(&hphysic);
148 physic_init(&vphysic);
154 bool jumped_in_solid = false;
156 /* --- HANDLE TUX! --- */
162 previous_base = base;
164 base.x += base.xm * frame_ratio;
165 base.y += base.ym * frame_ratio;
167 collision_swept_object_map(&old_base,&base);
181 physic_set_state(&vphysic,PH_VT);
182 physic_set_start_vy(&vphysic,0);
183 jumped_in_solid = true;
187 if(!physic_is_set(&vphysic))
189 physic_set_state(&vphysic,PH_VT);
190 physic_set_start_vy(&vphysic,0);
193 base.ym = physic_get_velocity(&vphysic);
203 base.y = (int)(((int)base.y / 32) * 32);
207 physic_init(&vphysic);
209 /* Reset score multiplier (for multi-hits): */
211 score_multiplier = 1;
216 if (isbrick(base.x, base.y) ||
217 isfullbox(base.x, base.y))
219 trygrabdistro(base.x, base.y - 32,BOUNCE);
220 trybumpbadguy(base.x, base.y - 64);
223 trybreakbrick(base.x, base.y);
225 bumpbrick(base.x, base.y);
226 tryemptybox(base.x, base.y, RIGHT);
229 if (isbrick(base.x+ 31, base.y) ||
230 isfullbox(base.x+ 31, base.y))
232 trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
233 trybumpbadguy(base.x+ 31, base.y - 64);
236 trybreakbrick(base.x+ 31, base.y);
238 bumpbrick(base.x+ 31, base.y);
239 tryemptybox(base.x+ 31, base.y, LEFT);
245 /* Get a distro from a brick? */
246 if (shape(base.x, base.y) == 'x' ||
247 shape(base.x, base.y) == 'y')
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, 'a');
261 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
262 score = score + SCORE_DISTRO;
265 else if (shape(base.x+ 31, base.y) == 'x' ||
266 shape(base.x+ 31, base.y) == 'y')
268 add_bouncy_distro((((int)base.x + 31)
270 ((int)base.y / 32) * 32);
271 if (counting_distros == false)
273 counting_distros = true;
274 distro_counter = 100;
277 if (distro_counter <= 0)
278 level_change(¤t_level,base.x+ 31, base.y, TM_IA, 'a');
280 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
281 score = score + SCORE_DISTRO;
295 /* Make sure jumping is off. */
302 timer_check(&safe_timer);
305 /* ---- DONE HANDLING TUX! --- */
307 /* Handle invincibility timer: */
310 if (get_current_music() == HERRING_MUSIC && !timer_check(&invincible_timer))
313 no, we are no more invincible
314 or we were not in invincible mode
315 but are we in hurry ?
319 if (timer_get_left(&time_left) < TIME_WARNING)
321 /* yes, we are in hurry
322 stop the herring_song, prepare to play the correct
325 set_current_music(HURRYUP_MUSIC);
329 set_current_music(LEVEL_MUSIC);
332 /* start playing it */
333 play_current_music();
336 /* Handle skidding: */
338 timer_check(&skidding_timer);
342 if (base.x >= endpos && endpos != 0)
352 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
353 issolid(base.x + 1, base.y + base.height) ||
354 issolid(base.x + base.width - 1, base.y + base.height) );
358 Player::under_solid()
360 return ( issolid(base.x + base.width / 2, base.y) ||
361 issolid(base.x + 1, base.y) ||
362 issolid(base.x + base.width - 1, base.y) );
366 Player::handle_horizontal_input(int newdir)
368 if ((newdir ? (base.xm < -SKID_XM) : (base.xm > SKID_XM)) && !timer_started(&skidding_timer) &&
369 dir == !newdir && on_ground())
371 timer_start(&skidding_timer, SKID_TIME);
373 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
379 if ((newdir ? (base.xm < 0) : (base.xm > 0)) && !isice(base.x, base.y + base.height) &&
380 !timer_started(&skidding_timer))
389 /* Facing the direction we're jumping? Go full-speed: */
391 if (input.fire == UP)
393 base.xm = base.xm + ( newdir ? WALK_SPEED : -WALK_SPEED) * frame_ratio;
397 if (base.xm > MAX_WALK_XM)
398 base.xm = MAX_WALK_XM;
402 if (base.xm < -MAX_WALK_XM)
403 base.xm = -MAX_WALK_XM;
406 else if ( input.fire == DOWN)
408 base.xm = base.xm + ( newdir ? RUN_SPEED : -RUN_SPEED) * frame_ratio;
412 if (base.xm > MAX_RUN_XM)
413 base.xm = MAX_RUN_XM;
417 if (base.xm < -MAX_RUN_XM)
418 base.xm = -MAX_RUN_XM;
423 /* Not facing the direction we're jumping?
426 base.xm = base.xm + ( newdir ? (WALK_SPEED / 2) : -(WALK_SPEED / 2)) * frame_ratio;
430 if (base.xm > MAX_WALK_XM / 2)
431 base.xm = MAX_WALK_XM / 2;
435 if (base.xm < -MAX_WALK_XM / 2)
436 base.xm = -MAX_WALK_XM / 2;
445 Player::handle_vertical_input()
451 if(!physic_is_set(&vphysic))
453 physic_set_state(&vphysic,PH_VT);
454 physic_set_start_vy(&vphysic,5.5);
458 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
460 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
464 else if(input.up == UP && jumping)
468 physic_init(&vphysic);
474 if(physic_is_set(&vphysic))
476 if(physic_get_velocity(&vphysic) < 0.)
478 physic_set_state(&vphysic,PH_VT);
479 physic_set_start_vy(&vphysic,0);
484 if(!physic_is_set(&vphysic))
486 physic_set_state(&vphysic,PH_VT);
494 Player::handle_input()
496 /* Handle key and joystick state: */
499 if (input.right == DOWN && input.left == UP)
501 handle_horizontal_input(RIGHT);
503 else if (input.left == DOWN && input.right == UP)
505 handle_horizontal_input(LEFT);
511 base.xm = (int)(base.xm - frame_ratio);
517 base.xm = (int)(base.xm + frame_ratio);
526 if ( input.up == DOWN || (input.up == UP && jumping))
528 handle_vertical_input();
533 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
535 add_bullet(base.x, base.y, base.xm, dir);
541 if (input.down == DOWN)
543 if (size == BIG && duck != true)
552 if (size == BIG && duck)
554 /* Make sure we're not standing back up into a solid! */
558 if (!collision_object_map(&base) /*issolid(base.x + 16, base.y - 16)*/)
563 old_base.height = 64;
579 if(!timer_check(&frame_timer))
581 timer_start(&frame_timer,25);
582 if (input.right == UP && input.left == UP)
589 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
590 (global_frame_counter % 4) == 0)
591 frame_main = (frame_main + 1) % 4;
603 Player::grabdistros()
608 trygrabdistro(base.x, base.y, NO_BOUNCE);
609 trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
611 trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
612 trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
616 trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
617 trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
622 /* Enough distros for a One-up? */
623 if (distros >= DISTROS_LIFEUP)
625 distros = distros - DISTROS_LIFEUP;
626 if(lives < MAX_LIVES)
628 /*We want to hear the sound even, if MAX_LIVES is reached*/
629 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
636 if (!timer_started(&safe_timer) || (global_frame_counter % 2) == 0)
640 if (timer_started(&invincible_timer))
646 texture_draw(&cape_right[global_frame_counter % 2],
647 base.x- scroll_x, base.y);
651 texture_draw(&cape_left[global_frame_counter % 2],
652 base.x- scroll_x, base.y);
661 texture_draw(&tux_right[frame_], base.x- scroll_x, base.y);
665 texture_draw(&tux_left[frame_], base.x- scroll_x, base.y);
670 /* Tux got coffee! */
674 texture_draw(&firetux_right[frame_], base.x- scroll_x, base.y);
678 texture_draw(&firetux_left[frame_], base.x- scroll_x, base.y);
684 if (timer_started(&invincible_timer))
689 texture_draw(&bigcape_right[global_frame_counter % 2],
690 base.x- scroll_x - 8, base.y);
694 texture_draw(&bigcape_left[global_frame_counter % 2],
695 base.x-scroll_x - 8, base.y);
703 if (!timer_started(&skidding_timer))
705 if (!jumping || base.ym > 0)
709 texture_draw(&bigtux_right[frame_],
710 base.x- scroll_x - 8, base.y);
714 texture_draw(&bigtux_left[frame_],
715 base.x- scroll_x - 8, base.y);
722 texture_draw(&bigtux_right_jump,
723 base.x- scroll_x - 8, base.y);
727 texture_draw(&bigtux_left_jump,
728 base.x- scroll_x - 8, base.y);
736 texture_draw(&skidtux_right,
737 base.x- scroll_x - 8, base.y);
741 texture_draw(&skidtux_left,
742 base.x- scroll_x - 8, base.y);
750 texture_draw(&ducktux_right, base.x- scroll_x - 8, base.y - 16);
754 texture_draw(&ducktux_left, base.x- scroll_x - 8, base.y - 16);
760 /* Tux has coffee! */
764 if (!timer_started(&skidding_timer))
766 if (!jumping || base.ym > 0)
770 texture_draw(&bigfiretux_right[frame_],
771 base.x- scroll_x - 8, base.y);
775 texture_draw(&bigfiretux_left[frame_],
776 base.x- scroll_x - 8, base.y);
783 texture_draw(&bigfiretux_right_jump,
784 base.x- scroll_x - 8, base.y);
788 texture_draw(&bigfiretux_left_jump,
789 base.x- scroll_x - 8, base.y);
797 texture_draw(&skidfiretux_right,
798 base.x- scroll_x - 8, base.y);
802 texture_draw(&skidfiretux_left,
803 base.x- scroll_x - 8, base.y);
811 texture_draw(&duckfiretux_right, base.x- scroll_x - 8, base.y - 16);
815 texture_draw(&duckfiretux_left, base.x- scroll_x - 8, base.y - 16);
824 Player::collision(void* p_c_object, int c_object)
826 BadGuy* pbad_c = NULL;
831 pbad_c = (BadGuy*) p_c_object;
832 /* Hurt the player if he just touched it: */
834 if (!pbad_c->dying && !dying &&
835 !timer_started(&safe_timer) &&
836 pbad_c->mode != HELD)
838 if (pbad_c->mode == FLAT && input.fire != DOWN)
843 play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
845 if (base.x < pbad_c->base.x + (pbad_c->base.width/2))
848 pbad_c->base.x = pbad_c->base.x + 16;
853 pbad_c->base.x = pbad_c->base.x - 32;
856 timer_start(&pbad_c->timer,5000);
858 else if (pbad_c->mode == FLAT && input.fire == DOWN)
863 else if (pbad_c->mode == KICK)
865 if (base.y < pbad_c->base.y - 16 &&
866 timer_started(&pbad_c->timer))
868 /* Step on (stop being kicked) */
871 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
872 timer_start(&pbad_c->timer, 10000);
876 /* Hurt if you get hit by kicked laptop: */
878 if (timer_started(&pbad_c->timer))
880 if (!timer_started(&invincible_timer))
886 pbad_c->dying = DYING_FALLING;
887 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
888 add_score(pbad_c->base.x - scroll_x,
890 25 * score_multiplier);
897 if (!timer_started(&invincible_timer ))
903 pbad_c->dying = DYING_FALLING;
904 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
905 add_score(pbad_c->base.x - scroll_x,
907 25 * score_multiplier);
922 Player::kill(int mode)
926 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
930 else if (dir == LEFT)
933 if (mode == SHRINK && size == BIG)
941 timer_start(&safe_timer,TUX_SAFE_TIME);
945 dying = DYING_SQUISHED;
952 base.ym = base.ym + gravity;
964 /* Remove Tux's power ups */
966 Player::remove_powerups()
974 Player::keep_in_bounds()
976 /* Keep tux in bounds: */
979 else if(base.x< scroll_x)
981 else if (base.x< 160 + scroll_x && scroll_x > 0 && debug_mode)
983 scroll_x = base.x- 160;
990 else if (base.x> screen->w / 2 + scroll_x && scroll_x < ((current_level.width * 32) - screen->w))
992 /* Scroll the screen in past center: */
994 scroll_x = base.x- screen->w / 2;
995 /*base.x= 320 + scroll_x;*/
997 if (scroll_x > ((current_level.width * 32) - screen->w))
998 scroll_x = ((current_level.width * 32) - screen->w);
1000 else if (base.x> 608 + scroll_x)
1002 /* ... unless there's no more to scroll! */
1004 /*base.x= 608 + scroll_x;*/
1007 /* Keep in-bounds, vertically: */
1009 if (base.y > screen->h)