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);
182 physic_set_state(&vphysic,PH_VT);
183 physic_set_start_vy(&vphysic,0);
184 jumped_in_solid = true;
188 if(!physic_is_set(&vphysic))
190 physic_set_state(&vphysic,PH_VT);
191 physic_set_start_vy(&vphysic,0);
194 base.ym = physic_get_velocity(&vphysic);
204 base.y = (int)(((int)base.y / 32) * 32);
208 physic_init(&vphysic);
210 /* Reset score multiplier (for multi-hits): */
212 score_multiplier = 1;
217 if (isbrick(base.x, base.y) ||
218 isfullbox(base.x, base.y))
220 trygrabdistro(base.x, base.y - 32,BOUNCE);
221 trybumpbadguy(base.x, base.y - 64);
224 trybreakbrick(base.x, base.y);
226 bumpbrick(base.x, base.y);
227 tryemptybox(base.x, base.y, RIGHT);
230 if (isbrick(base.x+ 31, base.y) ||
231 isfullbox(base.x+ 31, base.y))
233 trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
234 trybumpbadguy(base.x+ 31, base.y - 64);
237 trybreakbrick(base.x+ 31, base.y);
239 bumpbrick(base.x+ 31, base.y);
240 tryemptybox(base.x+ 31, base.y, LEFT);
246 /* Get a distro from a brick? */
247 if (gettile(base.x, base.y)->brick ||
248 gettile(base.x, base.y)->brick)
250 add_bouncy_distro((((int)base.x)
252 ((int)base.y / 32) * 32);
253 if (counting_distros == false)
255 counting_distros = true;
256 distro_counter = 100;
259 if (distro_counter <= 0)
260 level_change(¤t_level,base.x,base.y - 1, TM_IA, 'a');
262 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
263 score = score + SCORE_DISTRO;
266 else if (gettile(base.x+ 31, base.y)->brick ||
267 gettile(base.x+ 31, base.y)->brick)
269 add_bouncy_distro((((int)base.x + 31)
271 ((int)base.y / 32) * 32);
272 if (counting_distros == false)
274 counting_distros = true;
275 distro_counter = 100;
278 if (distro_counter <= 0)
279 level_change(¤t_level,base.x+ 31, base.y, TM_IA, 'a');
281 play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
282 score = score + SCORE_DISTRO;
296 /* Make sure jumping is off. */
303 timer_check(&safe_timer);
306 /* ---- DONE HANDLING TUX! --- */
308 /* Handle invincibility timer: */
311 if (get_current_music() == HERRING_MUSIC && !timer_check(&invincible_timer))
314 no, we are no more invincible
315 or we were not in invincible mode
316 but are we in hurry ?
320 if (timer_get_left(&time_left) < TIME_WARNING)
322 /* yes, we are in hurry
323 stop the herring_song, prepare to play the correct
326 set_current_music(HURRYUP_MUSIC);
330 set_current_music(LEVEL_MUSIC);
333 /* start playing it */
334 play_current_music();
337 /* Handle skidding: */
339 timer_check(&skidding_timer);
343 if (base.x >= endpos && endpos != 0)
353 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
354 issolid(base.x + 1, base.y + base.height) ||
355 issolid(base.x + base.width - 1, base.y + base.height) );
359 Player::under_solid()
361 return ( issolid(base.x + base.width / 2, base.y) ||
362 issolid(base.x + 1, base.y) ||
363 issolid(base.x + base.width - 1, base.y) );
367 Player::handle_horizontal_input(int newdir)
369 if ((newdir ? (base.xm < -SKID_XM) : (base.xm > SKID_XM)) && !timer_started(&skidding_timer) &&
370 dir == !newdir && on_ground())
372 timer_start(&skidding_timer, SKID_TIME);
374 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
380 if ((newdir ? (base.xm < 0) : (base.xm > 0)) && !isice(base.x, base.y + base.height) &&
381 !timer_started(&skidding_timer))
390 /* Facing the direction we're jumping? Go full-speed: */
392 if (input.fire == UP)
394 base.xm = base.xm + ( newdir ? WALK_SPEED : -WALK_SPEED) * frame_ratio;
398 if (base.xm > MAX_WALK_XM)
399 base.xm = MAX_WALK_XM;
403 if (base.xm < -MAX_WALK_XM)
404 base.xm = -MAX_WALK_XM;
407 else if ( input.fire == DOWN)
409 base.xm = base.xm + ( newdir ? RUN_SPEED : -RUN_SPEED) * frame_ratio;
413 if (base.xm > MAX_RUN_XM)
414 base.xm = MAX_RUN_XM;
418 if (base.xm < -MAX_RUN_XM)
419 base.xm = -MAX_RUN_XM;
424 /* Not facing the direction we're jumping?
427 base.xm = base.xm + ( newdir ? (WALK_SPEED / 2) : -(WALK_SPEED / 2)) * frame_ratio;
431 if (base.xm > MAX_WALK_XM / 2)
432 base.xm = MAX_WALK_XM / 2;
436 if (base.xm < -MAX_WALK_XM / 2)
437 base.xm = -MAX_WALK_XM / 2;
446 Player::handle_vertical_input()
452 if(!physic_is_set(&vphysic))
454 physic_set_state(&vphysic,PH_VT);
455 physic_set_start_vy(&vphysic,5.5);
459 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
461 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
465 else if(input.up == UP && jumping)
469 physic_init(&vphysic);
475 if(physic_is_set(&vphysic))
477 if(physic_get_velocity(&vphysic) < 0.)
479 physic_set_state(&vphysic,PH_VT);
480 physic_set_start_vy(&vphysic,0);
485 if(!physic_is_set(&vphysic))
487 physic_set_state(&vphysic,PH_VT);
495 Player::handle_input()
497 /* Handle key and joystick state: */
500 if (input.right == DOWN && input.left == UP)
502 handle_horizontal_input(RIGHT);
504 else if (input.left == DOWN && input.right == UP)
506 handle_horizontal_input(LEFT);
512 base.xm = (int)(base.xm - frame_ratio);
518 base.xm = (int)(base.xm + frame_ratio);
527 if ( input.up == DOWN || (input.up == UP && jumping))
529 handle_vertical_input();
534 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
536 add_bullet(base.x, base.y, base.xm, dir);
542 if (input.down == DOWN)
544 if (size == BIG && duck != true)
553 if (size == BIG && duck)
555 /* Make sure we're not standing back up into a solid! */
559 if (!collision_object_map(&base) /*issolid(base.x + 16, base.y - 16)*/)
564 old_base.height = 64;
580 if(!timer_check(&frame_timer))
582 timer_start(&frame_timer,25);
583 if (input.right == UP && input.left == UP)
590 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
591 (global_frame_counter % 4) == 0)
592 frame_main = (frame_main + 1) % 4;
604 Player::grabdistros()
609 trygrabdistro(base.x, base.y, NO_BOUNCE);
610 trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
612 trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
613 trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
617 trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
618 trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
623 /* Enough distros for a One-up? */
624 if (distros >= DISTROS_LIFEUP)
626 distros = distros - DISTROS_LIFEUP;
627 if(lives < MAX_LIVES)
629 /*We want to hear the sound even, if MAX_LIVES is reached*/
630 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
637 if (!timer_started(&safe_timer) || (global_frame_counter % 2) == 0)
641 if (timer_started(&invincible_timer))
647 texture_draw(&cape_right[global_frame_counter % 2],
648 base.x- scroll_x, base.y);
652 texture_draw(&cape_left[global_frame_counter % 2],
653 base.x- scroll_x, base.y);
662 texture_draw(&tux_right[frame_], base.x- scroll_x, base.y);
666 texture_draw(&tux_left[frame_], base.x- scroll_x, base.y);
671 /* Tux got coffee! */
675 texture_draw(&firetux_right[frame_], base.x- scroll_x, base.y);
679 texture_draw(&firetux_left[frame_], base.x- scroll_x, base.y);
685 if (timer_started(&invincible_timer))
690 texture_draw(&bigcape_right[global_frame_counter % 2],
691 base.x- scroll_x - 8, base.y);
695 texture_draw(&bigcape_left[global_frame_counter % 2],
696 base.x-scroll_x - 8, base.y);
704 if (!timer_started(&skidding_timer))
706 if (!jumping || base.ym > 0)
710 texture_draw(&bigtux_right[frame_],
711 base.x- scroll_x - 8, base.y);
715 texture_draw(&bigtux_left[frame_],
716 base.x- scroll_x - 8, base.y);
723 texture_draw(&bigtux_right_jump,
724 base.x- scroll_x - 8, base.y);
728 texture_draw(&bigtux_left_jump,
729 base.x- scroll_x - 8, base.y);
737 texture_draw(&skidtux_right,
738 base.x- scroll_x - 8, base.y);
742 texture_draw(&skidtux_left,
743 base.x- scroll_x - 8, base.y);
751 texture_draw(&ducktux_right, base.x- scroll_x - 8, base.y - 16);
755 texture_draw(&ducktux_left, base.x- scroll_x - 8, base.y - 16);
761 /* Tux has coffee! */
765 if (!timer_started(&skidding_timer))
767 if (!jumping || base.ym > 0)
771 texture_draw(&bigfiretux_right[frame_],
772 base.x- scroll_x - 8, base.y);
776 texture_draw(&bigfiretux_left[frame_],
777 base.x- scroll_x - 8, base.y);
784 texture_draw(&bigfiretux_right_jump,
785 base.x- scroll_x - 8, base.y);
789 texture_draw(&bigfiretux_left_jump,
790 base.x- scroll_x - 8, base.y);
798 texture_draw(&skidfiretux_right,
799 base.x- scroll_x - 8, base.y);
803 texture_draw(&skidfiretux_left,
804 base.x- scroll_x - 8, base.y);
812 texture_draw(&duckfiretux_right, base.x- scroll_x - 8, base.y - 16);
816 texture_draw(&duckfiretux_left, base.x- scroll_x - 8, base.y - 16);
825 Player::collision(void* p_c_object, int c_object)
827 BadGuy* pbad_c = NULL;
832 pbad_c = (BadGuy*) p_c_object;
833 /* Hurt the player if he just touched it: */
835 if (!pbad_c->dying && !dying &&
836 !timer_started(&safe_timer) &&
837 pbad_c->mode != HELD)
839 if (pbad_c->mode == FLAT && input.fire != DOWN)
844 play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
846 if (base.x < pbad_c->base.x + (pbad_c->base.width/2))
849 pbad_c->base.x = pbad_c->base.x + 16;
854 pbad_c->base.x = pbad_c->base.x - 32;
857 timer_start(&pbad_c->timer,5000);
859 else if (pbad_c->mode == FLAT && input.fire == DOWN)
864 else if (pbad_c->mode == KICK)
866 if (base.y < pbad_c->base.y - 16 &&
867 timer_started(&pbad_c->timer))
869 /* Step on (stop being kicked) */
872 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
873 timer_start(&pbad_c->timer, 10000);
877 /* Hurt if you get hit by kicked laptop: */
879 if (timer_started(&pbad_c->timer))
881 if (!timer_started(&invincible_timer))
887 pbad_c->dying = DYING_FALLING;
888 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
889 add_score(pbad_c->base.x - scroll_x,
891 25 * score_multiplier);
898 if (!timer_started(&invincible_timer ))
904 pbad_c->dying = DYING_FALLING;
905 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
906 add_score(pbad_c->base.x - scroll_x,
908 25 * score_multiplier);
923 Player::kill(int mode)
927 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
931 else if (dir == LEFT)
934 if (mode == SHRINK && size == BIG)
942 timer_start(&safe_timer,TUX_SAFE_TIME);
946 dying = DYING_SQUISHED;
953 base.ym = base.ym + gravity;
965 /* Remove Tux's power ups */
967 Player::remove_powerups()
975 Player::keep_in_bounds()
977 /* Keep tux in bounds: */
980 else if(base.x< scroll_x)
982 else if (base.x< 160 + scroll_x && scroll_x > 0 && debug_mode)
984 scroll_x = base.x- 160;
991 else if (base.x> screen->w / 2 + scroll_x && scroll_x < ((current_level.width * 32) - screen->w))
993 /* Scroll the screen in past center: */
995 scroll_x = base.x- screen->w / 2;
996 /*base.x= 320 + scroll_x;*/
998 if (scroll_x > ((current_level.width * 32) - screen->w))
999 scroll_x = ((current_level.width * 32) - screen->w);
1001 else if (base.x> 608 + scroll_x)
1003 /* ... unless there's no more to scroll! */
1005 /*base.x= 608 + scroll_x;*/
1008 /* Keep in-bounds, vertically: */
1010 if (base.y > screen->h)