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! --- */
164 previous_base = base;
166 base.x += base.xm * frame_ratio;
167 base.y += base.ym * frame_ratio;
173 collision_swept_object_map(&old_base,&base);
183 physic_set_state(&vphysic,PH_VT);
184 physic_set_start_vy(&vphysic,0);
185 jumped_in_solid = true;
189 if(!physic_is_set(&vphysic))
191 physic_set_state(&vphysic,PH_VT);
192 physic_set_start_vy(&vphysic,0);
195 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);
222 trybreakbrick(base.x, base.y, size == SMALL);
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, size == SMALL);
237 bumpbrick(base.x+ 31, base.y);
238 tryemptybox(base.x+ 31, base.y, LEFT);
250 /* Make sure jumping is off. */
257 base.ym = physic_get_velocity(&vphysic);
259 timer_check(&safe_timer);
262 /* ---- DONE HANDLING TUX! --- */
264 /* Handle invincibility timer: */
267 if (get_current_music() == HERRING_MUSIC && !timer_check(&invincible_timer))
270 no, we are no more invincible
271 or we were not in invincible mode
272 but are we in hurry ?
276 if (timer_get_left(&time_left) < TIME_WARNING)
278 /* yes, we are in hurry
279 stop the herring_song, prepare to play the correct
282 set_current_music(HURRYUP_MUSIC);
286 set_current_music(LEVEL_MUSIC);
289 /* start playing it */
290 play_current_music();
293 /* Handle skidding: */
295 timer_check(&skidding_timer);
299 if (base.x >= endpos && endpos != 0)
309 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
310 issolid(base.x + 1, base.y + base.height) ||
311 issolid(base.x + base.width - 1, base.y + base.height) );
315 Player::under_solid()
317 return ( issolid(base.x + base.width / 2, base.y) ||
318 issolid(base.x + 1, base.y) ||
319 issolid(base.x + base.width - 1, base.y) );
323 Player::handle_horizontal_input(int newdir)
325 if ((newdir ? (base.xm < -SKID_XM) : (base.xm > SKID_XM)) && !timer_started(&skidding_timer) &&
326 dir == !newdir && on_ground())
328 timer_start(&skidding_timer, SKID_TIME);
330 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
336 if ((newdir ? (base.xm < 0) : (base.xm > 0)) && !isice(base.x, base.y + base.height) &&
337 !timer_started(&skidding_timer))
346 /* Facing the direction we're jumping? Go full-speed: */
348 if (input.fire == UP)
350 base.xm = base.xm + ( newdir ? WALK_SPEED : -WALK_SPEED) * frame_ratio;
354 if (base.xm > MAX_WALK_XM)
355 base.xm = MAX_WALK_XM;
359 if (base.xm < -MAX_WALK_XM)
360 base.xm = -MAX_WALK_XM;
363 else if ( input.fire == DOWN)
365 base.xm = base.xm + ( newdir ? RUN_SPEED : -RUN_SPEED) * frame_ratio;
369 if (base.xm > MAX_RUN_XM)
370 base.xm = MAX_RUN_XM;
374 if (base.xm < -MAX_RUN_XM)
375 base.xm = -MAX_RUN_XM;
380 /* Not facing the direction we're jumping?
383 base.xm = base.xm + ( newdir ? (WALK_SPEED / 2) : -(WALK_SPEED / 2)) * frame_ratio;
387 if (base.xm > MAX_WALK_XM / 2)
388 base.xm = MAX_WALK_XM / 2;
392 if (base.xm < -MAX_WALK_XM / 2)
393 base.xm = -MAX_WALK_XM / 2;
402 Player::handle_vertical_input()
408 if(!physic_is_set(&vphysic))
410 physic_set_state(&vphysic,PH_VT);
411 physic_set_start_vy(&vphysic,5.5);
415 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
417 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
421 else if(input.up == UP && jumping)
425 physic_init(&vphysic);
431 if(physic_is_set(&vphysic))
433 if(physic_get_velocity(&vphysic) < 0.)
435 physic_set_state(&vphysic,PH_VT);
436 physic_set_start_vy(&vphysic,0);
441 if(!physic_is_set(&vphysic))
443 physic_set_state(&vphysic,PH_VT);
451 Player::handle_input()
453 /* Handle key and joystick state: */
456 if (input.right == DOWN && input.left == UP)
458 handle_horizontal_input(RIGHT);
460 else if (input.left == DOWN && input.right == UP)
462 handle_horizontal_input(LEFT);
468 base.xm = (int)(base.xm - frame_ratio);
474 base.xm = (int)(base.xm + frame_ratio);
483 if ( input.up == DOWN || (input.up == UP && jumping))
485 handle_vertical_input();
490 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
492 add_bullet(base.x, base.y, base.xm, dir);
498 if (input.down == DOWN)
500 if (size == BIG && duck != true)
509 if (size == BIG && duck)
511 /* Make sure we're not standing back up into a solid! */
515 if (!collision_object_map(&base) /*issolid(base.x + 16, base.y - 16)*/)
520 old_base.height = 64;
536 if(!timer_check(&frame_timer))
538 timer_start(&frame_timer,25);
539 if (input.right == UP && input.left == UP)
546 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
547 (global_frame_counter % 4) == 0)
548 frame_main = (frame_main + 1) % 4;
560 Player::grabdistros()
565 trygrabdistro(base.x, base.y, NO_BOUNCE);
566 trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
568 trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
569 trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
573 trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
574 trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
579 /* Enough distros for a One-up? */
580 if (distros >= DISTROS_LIFEUP)
582 distros = distros - DISTROS_LIFEUP;
583 if(lives < MAX_LIVES)
585 /*We want to hear the sound even, if MAX_LIVES is reached*/
586 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
593 if (!timer_started(&safe_timer) || (global_frame_counter % 2) == 0)
597 if (timer_started(&invincible_timer))
603 texture_draw(&cape_right[global_frame_counter % 2],
604 base.x- scroll_x, base.y);
608 texture_draw(&cape_left[global_frame_counter % 2],
609 base.x- scroll_x, base.y);
618 texture_draw(&tux_right[frame_], base.x- scroll_x, base.y);
622 texture_draw(&tux_left[frame_], base.x- scroll_x, base.y);
627 /* Tux got coffee! */
631 texture_draw(&firetux_right[frame_], base.x- scroll_x, base.y);
635 texture_draw(&firetux_left[frame_], base.x- scroll_x, base.y);
641 if (timer_started(&invincible_timer))
646 texture_draw(&bigcape_right[global_frame_counter % 2],
647 base.x- scroll_x - 8, base.y);
651 texture_draw(&bigcape_left[global_frame_counter % 2],
652 base.x-scroll_x - 8, base.y);
660 if (!timer_started(&skidding_timer))
662 if (!jumping || base.ym > 0)
666 texture_draw(&bigtux_right[frame_],
667 base.x- scroll_x - 8, base.y);
671 texture_draw(&bigtux_left[frame_],
672 base.x- scroll_x - 8, base.y);
679 texture_draw(&bigtux_right_jump,
680 base.x- scroll_x - 8, base.y);
684 texture_draw(&bigtux_left_jump,
685 base.x- scroll_x - 8, base.y);
693 texture_draw(&skidtux_right,
694 base.x- scroll_x - 8, base.y);
698 texture_draw(&skidtux_left,
699 base.x- scroll_x - 8, base.y);
707 texture_draw(&ducktux_right, base.x- scroll_x - 8, base.y - 16);
711 texture_draw(&ducktux_left, base.x- scroll_x - 8, base.y - 16);
717 /* Tux has coffee! */
721 if (!timer_started(&skidding_timer))
723 if (!jumping || base.ym > 0)
727 texture_draw(&bigfiretux_right[frame_],
728 base.x- scroll_x - 8, base.y);
732 texture_draw(&bigfiretux_left[frame_],
733 base.x- scroll_x - 8, base.y);
740 texture_draw(&bigfiretux_right_jump,
741 base.x- scroll_x - 8, base.y);
745 texture_draw(&bigfiretux_left_jump,
746 base.x- scroll_x - 8, base.y);
754 texture_draw(&skidfiretux_right,
755 base.x- scroll_x - 8, base.y);
759 texture_draw(&skidfiretux_left,
760 base.x- scroll_x - 8, base.y);
768 texture_draw(&duckfiretux_right, base.x- scroll_x - 8, base.y - 16);
772 texture_draw(&duckfiretux_left, base.x- scroll_x - 8, base.y - 16);
779 text_drawf(&gold_text,"Penguins can fly !:",0,0,A_HMIDDLE,A_VMIDDLE,1);
783 Player::collision(void* p_c_object, int c_object)
785 BadGuy* pbad_c = NULL;
790 pbad_c = (BadGuy*) p_c_object;
791 /* Hurt the player if he just touched it: */
793 if (!pbad_c->dying && !dying &&
794 !timer_started(&safe_timer) &&
795 pbad_c->mode != HELD)
797 if (pbad_c->mode == FLAT && input.fire != DOWN)
802 play_sound(sounds[SND_KICK], SOUND_CENTER_SPEAKER);
804 if (base.x < pbad_c->base.x + (pbad_c->base.width/2))
807 pbad_c->base.x = pbad_c->base.x + 16;
812 pbad_c->base.x = pbad_c->base.x - 32;
815 timer_start(&pbad_c->timer,5000);
817 else if (pbad_c->mode == FLAT && input.fire == DOWN)
822 else if (pbad_c->mode == KICK)
824 if (base.y < pbad_c->base.y - 16 &&
825 timer_started(&pbad_c->timer))
827 /* Step on (stop being kicked) */
830 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
831 timer_start(&pbad_c->timer, 10000);
835 /* Hurt if you get hit by kicked laptop: */
837 if (timer_started(&pbad_c->timer))
839 if (!timer_started(&invincible_timer))
845 pbad_c->dying = DYING_FALLING;
846 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
847 add_score(pbad_c->base.x - scroll_x,
849 25 * score_multiplier);
856 if (!timer_started(&invincible_timer ))
862 pbad_c->dying = DYING_FALLING;
863 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
864 add_score(pbad_c->base.x - scroll_x,
866 25 * score_multiplier);
881 Player::kill(int mode)
884 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
888 if (mode == SHRINK && size == BIG)
896 timer_start(&safe_timer,TUX_SAFE_TIME);
902 physic_set_state(&vphysic,PH_VT);
903 physic_set_start_vy(&vphysic,7);
904 dying = DYING_SQUISHED;
911 base.ym = base.ym + gravity;
920 bool Player::is_dead()
922 if(base.y > screen->h)
928 /* Remove Tux's power ups */
930 Player::remove_powerups()
938 Player::keep_in_bounds()
940 /* Keep tux in bounds: */
943 else if(base.x< scroll_x)
945 else if (base.x< 160 + scroll_x && scroll_x > 0 && debug_mode)
947 scroll_x = base.x- 160;
954 else if (base.x> screen->w / 2 + scroll_x && scroll_x < ((current_level.width * 32) - screen->w))
956 /* Scroll the screen in past center: */
958 scroll_x = base.x- screen->w / 2;
959 /*base.x= 320 + scroll_x;*/
961 if (scroll_x > ((current_level.width * 32) - screen->w))
962 scroll_x = ((current_level.width * 32) - screen->w);
964 else if (base.x> 608 + scroll_x)
966 /* ... unless there's no more to scroll! */
968 /*base.x= 608 + scroll_x;*/
971 /* Keep in-bounds, vertically: */
973 if (base.y > screen->h)