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;
82 player_input_init(&input_);
84 keymap.jump = SDLK_UP;
85 keymap.duck = SDLK_DOWN;
86 keymap.left = SDLK_LEFT;
87 keymap.right = SDLK_RIGHT;
88 keymap.fire = SDLK_LCTRL;
90 timer_init(&invincible_timer,true);
91 timer_init(&skidding_timer,true);
92 timer_init(&safe_timer,true);
93 timer_init(&frame_timer,true);
94 physic_init(&hphysic);
95 physic_init(&vphysic);
99 Player::key_event(SDLKey key, int state)
101 if(key == keymap.right)
103 input_.right = state;
106 else if(key == keymap.left)
111 else if(key == keymap.jump)
116 else if(key == keymap.duck)
121 else if(key == keymap.fire)
131 Player::level_begin()
139 player_input_init(&input_);
141 timer_init(&invincible_timer,true);
142 timer_init(&skidding_timer,true);
143 timer_init(&safe_timer,true);
144 timer_init(&frame_timer,true);
145 physic_init(&hphysic);
146 physic_init(&vphysic);
152 bool jumped_in_solid = false;
154 /* --- HANDLE TUX! --- */
160 previous_base = base;
162 base.x += base.xm * frame_ratio;
163 base.y += base.ym * frame_ratio;
165 collision_swept_object_map(&old_base,&base);
179 physic_set_state(&vphysic,PH_VT);
180 physic_set_start_vy(&vphysic,0);
181 jumped_in_solid = true;
185 if(!physic_is_set(&vphysic))
187 physic_set_state(&vphysic,PH_VT);
188 physic_set_start_vy(&vphysic,0);
191 base.ym = physic_get_velocity(&vphysic);
201 base.y = (int)(((int)base.y / 32) * 32);
205 physic_init(&vphysic);
207 /* Reset score multiplier (for multi-hits): */
209 score_multiplier = 1;
214 if (isbrick(base.x, base.y) ||
215 isfullbox(base.x, base.y))
217 trygrabdistro(base.x, base.y - 32,BOUNCE);
218 trybumpbadguy(base.x, base.y - 64);
221 trybreakbrick(base.x, base.y);
223 bumpbrick(base.x, base.y);
224 tryemptybox(base.x, base.y, RIGHT);
227 if (isbrick(base.x+ 31, base.y) ||
228 isfullbox(base.x+ 31, base.y))
230 trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
231 trybumpbadguy(base.x+ 31, base.y - 64);
234 trybreakbrick(base.x+ 31, base.y);
236 bumpbrick(base.x+ 31, base.y);
237 tryemptybox(base.x+ 31, base.y, LEFT);
243 /* Get a distro from a brick? */
245 if (shape(base.x, base.y) == 'x' ||
246 shape(base.x, base.y) == 'y')
248 add_bouncy_distro((((int)base.x)
250 ((int)base.y / 32) * 32);
251 if (counting_distros == false)
253 counting_distros = true;
254 distro_counter = 100;
257 if (distro_counter <= 0)
258 level_change(¤t_level,base.x,base.y - 1, 'a');
260 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
261 score = score + SCORE_DISTRO;
264 else if (shape(base.x+ 31, base.y) == 'x' ||
265 shape(base.x+ 31, base.y) == 'y')
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, 'a');
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()
446 if(input_.up == DOWN)
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);
495 /* 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)