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);
200 base.y = (int)(((int)base.y / 32) * 32);
204 physic_init(&vphysic);
206 /* Reset score multiplier (for multi-hits): */
208 score_multiplier = 1;
213 if (isbrick(base.x, base.y) ||
214 isfullbox(base.x, base.y))
216 trygrabdistro(base.x, base.y - 32,BOUNCE);
217 trybumpbadguy(base.x, base.y - 64);
219 trybreakbrick(base.x, base.y, size == SMALL);
221 bumpbrick(base.x, base.y);
222 tryemptybox(base.x, base.y, RIGHT);
225 if (isbrick(base.x+ 31, base.y) ||
226 isfullbox(base.x+ 31, base.y))
228 trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
229 trybumpbadguy(base.x+ 31, base.y - 64);
232 trybreakbrick(base.x+ 31, base.y, size == SMALL);
234 bumpbrick(base.x+ 31, base.y);
235 tryemptybox(base.x+ 31, base.y, LEFT);
247 /* Make sure jumping is off. */
254 timer_check(&safe_timer);
257 /* ---- DONE HANDLING TUX! --- */
259 /* Handle invincibility timer: */
262 if (get_current_music() == HERRING_MUSIC && !timer_check(&invincible_timer))
265 no, we are no more invincible
266 or we were not in invincible mode
267 but are we in hurry ?
271 if (timer_get_left(&time_left) < TIME_WARNING)
273 /* yes, we are in hurry
274 stop the herring_song, prepare to play the correct
277 set_current_music(HURRYUP_MUSIC);
281 set_current_music(LEVEL_MUSIC);
284 /* start playing it */
285 play_current_music();
288 /* Handle skidding: */
290 timer_check(&skidding_timer);
294 if (base.x >= endpos && endpos != 0)
304 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
305 issolid(base.x + 1, base.y + base.height) ||
306 issolid(base.x + base.width - 1, base.y + base.height) );
310 Player::under_solid()
312 return ( issolid(base.x + base.width / 2, base.y) ||
313 issolid(base.x + 1, base.y) ||
314 issolid(base.x + base.width - 1, base.y) );
318 Player::handle_horizontal_input(int newdir)
320 if ((newdir ? (base.xm < -SKID_XM) : (base.xm > SKID_XM)) && !timer_started(&skidding_timer) &&
321 dir == !newdir && on_ground())
323 timer_start(&skidding_timer, SKID_TIME);
325 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
331 if ((newdir ? (base.xm < 0) : (base.xm > 0)) && !isice(base.x, base.y + base.height) &&
332 !timer_started(&skidding_timer))
341 /* Facing the direction we're jumping? Go full-speed: */
343 if (input.fire == UP)
345 base.xm = base.xm + ( newdir ? WALK_SPEED : -WALK_SPEED) * frame_ratio;
349 if (base.xm > MAX_WALK_XM)
350 base.xm = MAX_WALK_XM;
354 if (base.xm < -MAX_WALK_XM)
355 base.xm = -MAX_WALK_XM;
358 else if ( input.fire == DOWN)
360 base.xm = base.xm + ( newdir ? RUN_SPEED : -RUN_SPEED) * frame_ratio;
364 if (base.xm > MAX_RUN_XM)
365 base.xm = MAX_RUN_XM;
369 if (base.xm < -MAX_RUN_XM)
370 base.xm = -MAX_RUN_XM;
375 /* Not facing the direction we're jumping?
378 base.xm = base.xm + ( newdir ? (WALK_SPEED / 2) : -(WALK_SPEED / 2)) * frame_ratio;
382 if (base.xm > MAX_WALK_XM / 2)
383 base.xm = MAX_WALK_XM / 2;
387 if (base.xm < -MAX_WALK_XM / 2)
388 base.xm = -MAX_WALK_XM / 2;
397 Player::handle_vertical_input()
403 if(!physic_is_set(&vphysic))
405 physic_set_state(&vphysic,PH_VT);
406 physic_set_start_vy(&vphysic,5.5);
410 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
412 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
416 else if(input.up == UP && jumping)
420 physic_init(&vphysic);
426 if(physic_is_set(&vphysic))
428 if(physic_get_velocity(&vphysic) < 0.)
430 physic_set_state(&vphysic,PH_VT);
431 physic_set_start_vy(&vphysic,0);
436 if(!physic_is_set(&vphysic))
438 physic_set_state(&vphysic,PH_VT);
446 Player::handle_input()
448 /* Handle key and joystick state: */
451 if (input.right == DOWN && input.left == UP)
453 handle_horizontal_input(RIGHT);
455 else if (input.left == DOWN && input.right == UP)
457 handle_horizontal_input(LEFT);
463 base.xm = (int)(base.xm - frame_ratio);
469 base.xm = (int)(base.xm + frame_ratio);
478 if ( input.up == DOWN || (input.up == UP && jumping))
480 handle_vertical_input();
485 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
487 add_bullet(base.x, base.y, base.xm, dir);
493 if (input.down == DOWN)
495 if (size == BIG && duck != true)
504 if (size == BIG && duck)
506 /* Make sure we're not standing back up into a solid! */
510 if (!collision_object_map(&base) /*issolid(base.x + 16, base.y - 16)*/)
515 old_base.height = 64;
531 if(!timer_check(&frame_timer))
533 timer_start(&frame_timer,25);
534 if (input.right == UP && input.left == UP)
541 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
542 (global_frame_counter % 4) == 0)
543 frame_main = (frame_main + 1) % 4;
555 Player::grabdistros()
560 trygrabdistro(base.x, base.y, NO_BOUNCE);
561 trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
563 trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
564 trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
568 trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
569 trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
574 /* Enough distros for a One-up? */
575 if (distros >= DISTROS_LIFEUP)
577 distros = distros - DISTROS_LIFEUP;
578 if(lives < MAX_LIVES)
580 /*We want to hear the sound even, if MAX_LIVES is reached*/
581 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
588 if (!timer_started(&safe_timer) || (global_frame_counter % 2) == 0)
592 if (timer_started(&invincible_timer))
598 texture_draw(&cape_right[global_frame_counter % 2],
599 base.x- scroll_x, base.y);
603 texture_draw(&cape_left[global_frame_counter % 2],
604 base.x- scroll_x, base.y);
613 texture_draw(&tux_right[frame_], base.x- scroll_x, base.y);
617 texture_draw(&tux_left[frame_], base.x- scroll_x, base.y);
622 /* Tux got coffee! */
626 texture_draw(&firetux_right[frame_], base.x- scroll_x, base.y);
630 texture_draw(&firetux_left[frame_], base.x- scroll_x, base.y);
636 if (timer_started(&invincible_timer))
641 texture_draw(&bigcape_right[global_frame_counter % 2],
642 base.x- scroll_x - 8, base.y);
646 texture_draw(&bigcape_left[global_frame_counter % 2],
647 base.x-scroll_x - 8, base.y);
655 if (!timer_started(&skidding_timer))
657 if (!jumping || base.ym > 0)
661 texture_draw(&bigtux_right[frame_],
662 base.x- scroll_x - 8, base.y);
666 texture_draw(&bigtux_left[frame_],
667 base.x- scroll_x - 8, base.y);
674 texture_draw(&bigtux_right_jump,
675 base.x- scroll_x - 8, base.y);
679 texture_draw(&bigtux_left_jump,
680 base.x- scroll_x - 8, base.y);
688 texture_draw(&skidtux_right,
689 base.x- scroll_x - 8, base.y);
693 texture_draw(&skidtux_left,
694 base.x- scroll_x - 8, base.y);
702 texture_draw(&ducktux_right, base.x- scroll_x - 8, base.y - 16);
706 texture_draw(&ducktux_left, base.x- scroll_x - 8, base.y - 16);
712 /* Tux has coffee! */
716 if (!timer_started(&skidding_timer))
718 if (!jumping || base.ym > 0)
722 texture_draw(&bigfiretux_right[frame_],
723 base.x- scroll_x - 8, base.y);
727 texture_draw(&bigfiretux_left[frame_],
728 base.x- scroll_x - 8, base.y);
735 texture_draw(&bigfiretux_right_jump,
736 base.x- scroll_x - 8, base.y);
740 texture_draw(&bigfiretux_left_jump,
741 base.x- scroll_x - 8, base.y);
749 texture_draw(&skidfiretux_right,
750 base.x- scroll_x - 8, base.y);
754 texture_draw(&skidfiretux_left,
755 base.x- scroll_x - 8, base.y);
763 texture_draw(&duckfiretux_right, base.x- scroll_x - 8, base.y - 16);
767 texture_draw(&duckfiretux_left, base.x- scroll_x - 8, base.y - 16);
776 Player::collision(void* p_c_object, int c_object)
778 BadGuy* pbad_c = NULL;
783 pbad_c = (BadGuy*) p_c_object;
784 /* Hurt the player if he just touched it: */
786 if (!pbad_c->dying && !dying &&
787 !timer_started(&safe_timer) &&
788 pbad_c->mode != HELD)
790 if (pbad_c->mode == FLAT && input.fire != DOWN)
795 play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
797 if (base.x < pbad_c->base.x + (pbad_c->base.width/2))
800 pbad_c->base.x = pbad_c->base.x + 16;
805 pbad_c->base.x = pbad_c->base.x - 32;
808 timer_start(&pbad_c->timer,5000);
810 else if (pbad_c->mode == FLAT && input.fire == DOWN)
815 else if (pbad_c->mode == KICK)
817 if (base.y < pbad_c->base.y - 16 &&
818 timer_started(&pbad_c->timer))
820 /* Step on (stop being kicked) */
823 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
824 timer_start(&pbad_c->timer, 10000);
828 /* Hurt if you get hit by kicked laptop: */
830 if (timer_started(&pbad_c->timer))
832 if (!timer_started(&invincible_timer))
838 pbad_c->dying = DYING_FALLING;
839 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
840 add_score(pbad_c->base.x - scroll_x,
842 25 * score_multiplier);
849 if (!timer_started(&invincible_timer ))
855 pbad_c->dying = DYING_FALLING;
856 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
857 add_score(pbad_c->base.x - scroll_x,
859 25 * score_multiplier);
874 Player::kill(int mode)
878 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
882 else if (dir == LEFT)
885 if (mode == SHRINK && size == BIG)
893 timer_start(&safe_timer,TUX_SAFE_TIME);
897 dying = DYING_SQUISHED;
904 base.ym = base.ym + gravity;
916 /* Remove Tux's power ups */
918 Player::remove_powerups()
926 Player::keep_in_bounds()
928 /* Keep tux in bounds: */
931 else if(base.x< scroll_x)
933 else if (base.x< 160 + scroll_x && scroll_x > 0 && debug_mode)
935 scroll_x = base.x- 160;
942 else if (base.x> screen->w / 2 + scroll_x && scroll_x < ((current_level.width * 32) - screen->w))
944 /* Scroll the screen in past center: */
946 scroll_x = base.x- screen->w / 2;
947 /*base.x= 320 + scroll_x;*/
949 if (scroll_x > ((current_level.width * 32) - screen->w))
950 scroll_x = ((current_level.width * 32) - screen->w);
952 else if (base.x> 608 + scroll_x)
954 /* ... unless there's no more to scroll! */
956 /*base.x= 608 + scroll_x;*/
959 /* Keep in-bounds, vertically: */
961 if (base.y > screen->h)