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
22 texture_type tux_life;
23 std::vector<texture_type> tux_right;
24 std::vector<texture_type> tux_left;
25 texture_type smalltux_jump_left;
26 texture_type smalltux_jump_right;
27 texture_type smalltux_stand_left;
28 texture_type smalltux_stand_right;
30 texture_type bigtux_right[3];
31 texture_type bigtux_left[3];
32 texture_type bigtux_right_jump;
33 texture_type bigtux_left_jump;
34 texture_type ducktux_right;
35 texture_type ducktux_left;
36 texture_type skidtux_right;
37 texture_type skidtux_left;
38 texture_type firetux_right[3];
39 texture_type firetux_left[3];
40 texture_type bigfiretux_right[3];
41 texture_type bigfiretux_left[3];
42 texture_type bigfiretux_right_jump;
43 texture_type bigfiretux_left_jump;
44 texture_type duckfiretux_right;
45 texture_type duckfiretux_left;
46 texture_type skidfiretux_right;
47 texture_type skidfiretux_left;
48 texture_type cape_right[2];
49 texture_type cape_left[2];
50 texture_type bigcape_right[2];
51 texture_type bigcape_left[2];
53 void player_input_init(player_input_type* pplayer_input)
55 pplayer_input->down = UP;
56 pplayer_input->fire = UP;
57 pplayer_input->left = UP;
58 pplayer_input->old_fire = UP;
59 pplayer_input->right = UP;
60 pplayer_input->up = UP;
72 // FIXME: Make the start position configurable via the levelfile
90 player_input_init(&input);
92 keymap.jump = SDLK_UP;
93 keymap.duck = SDLK_DOWN;
94 keymap.left = SDLK_LEFT;
95 keymap.right = SDLK_RIGHT;
96 keymap.fire = SDLK_LCTRL;
98 timer_init(&invincible_timer,true);
99 timer_init(&skidding_timer,true);
100 timer_init(&safe_timer,true);
101 timer_init(&frame_timer,true);
107 Player::key_event(SDLKey key, int state)
109 if(key == keymap.right)
114 else if(key == keymap.left)
119 else if(key == keymap.jump)
124 else if(key == keymap.duck)
129 else if(key == keymap.fire)
139 Player::level_begin()
146 previous_base = base;
150 player_input_init(&input);
152 timer_init(&invincible_timer,true);
153 timer_init(&skidding_timer,true);
154 timer_init(&safe_timer,true);
155 timer_init(&frame_timer,true);
160 Player::action(double frame_ratio)
162 bool jumped_in_solid = false;
164 /* --- HANDLE TUX! --- */
170 previous_base = base;
172 physic.apply(frame_ratio, base.x, base.y);
177 collision_swept_object_map(&old_base,&base);
185 physic.enable_gravity(true);
189 physic.set_velocity(physic.get_velocity_x(), 0);
190 jumped_in_solid = true;
196 if (physic.get_velocity_y() < 0)
198 base.y = (int)(((int)base.y / 32) * 32);
199 physic.set_velocity(physic.get_velocity_x(), 0);
202 physic.enable_gravity(false);
203 /* Reset score multiplier (for multi-hits): */
204 player_status.score_multiplier = 1;
209 if (isbrick(base.x, base.y) ||
210 isfullbox(base.x, base.y))
212 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
213 World::current()->trybumpbadguy(base.x, base.y - 64);
215 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
217 bumpbrick(base.x, base.y);
218 World::current()->tryemptybox(base.x, base.y, RIGHT);
221 if (isbrick(base.x+ 31, base.y) ||
222 isfullbox(base.x+ 31, base.y))
224 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
225 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
228 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
230 bumpbrick(base.x+ 31, base.y);
231 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
243 /* Make sure jumping is off. */
250 timer_check(&safe_timer);
253 /* ---- DONE HANDLING TUX! --- */
255 /* Handle invincibility timer: */
256 if (get_current_music() == HERRING_MUSIC && !timer_check(&invincible_timer))
259 no, we are no more invincible
260 or we were not in invincible mode
261 but are we in hurry ?
264 // FIXME: Move this to gamesession
265 if (timer_get_left(&GameSession::current()->time_left) < TIME_WARNING)
267 /* yes, we are in hurry
268 stop the herring_song, prepare to play the correct
271 set_current_music(HURRYUP_MUSIC);
275 set_current_music(LEVEL_MUSIC);
278 /* start playing it */
279 play_current_music();
282 /* Handle skidding: */
284 // timer_check(&skidding_timer); // disabled
287 if (base.x >= World::current()->get_level()->endpos
288 && World::current()->get_level()->endpos != 0)
290 player_status.next_level = 1;
298 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
299 issolid(base.x + 1, base.y + base.height) ||
300 issolid(base.x + base.width - 1, base.y + base.height) );
304 Player::under_solid()
306 return ( issolid(base.x + base.width / 2, base.y) ||
307 issolid(base.x + 1, base.y) ||
308 issolid(base.x + base.width - 1, base.y) );
312 Player::handle_horizontal_input(int newdir)
317 float vx = physic.get_velocity_x();
318 float vy = physic.get_velocity_y();
321 // skid if we're too fast
322 if(dir != newdir && on_ground() && fabs(physic.get_velocity_x()) > SKID_XM
323 && !timer_started(&skidding_timer))
325 timer_start(&skidding_timer, SKID_TIME);
326 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
330 if ((newdir ? (vx < 0) : (vx > 0)) && !isice(base.x, base.y + base.height) &&
331 !timer_started(&skidding_timer))
336 /* Facing the direction we're jumping? Go full-speed: */
337 if (input.fire == UP)
339 if(vx >= MAX_WALK_XM) {
341 physic.set_acceleration(0, 0); // enough speedup
342 } else if(vx <= -MAX_WALK_XM) {
344 physic.set_acceleration(0, 0);
346 physic.set_acceleration(newdir ? 0.02 : -0.02, 0);
347 if(fabs(vx) < 1) // set some basic run speed
348 vx = newdir ? 1 : -1;
350 vx += ( newdir ? WALK_SPEED : -WALK_SPEED) * frame_ratio;
354 if (vx > MAX_WALK_XM)
359 if (vx < -MAX_WALK_XM)
364 else if ( input.fire == DOWN)
366 if(vx >= MAX_RUN_XM) {
368 physic.set_acceleration(0, 0); // enough speedup
369 } else if(vx <= -MAX_RUN_XM) {
371 physic.set_acceleration(0, 0);
373 physic.set_acceleration(newdir ? 0.03 : -0.03, 0);
374 if(fabs(vx) < 1) // set some basic run speed
375 vx = newdir ? 1 : -1;
378 vx = vx + ( newdir ? RUN_SPEED : -RUN_SPEED) * frame_ratio;
387 if (vx < -MAX_RUN_XM)
395 /* Not facing the direction we're jumping?
397 vx = vx + ( newdir ? (WALK_SPEED / 2) : -(WALK_SPEED / 2)) * frame_ratio;
401 if (vx > MAX_WALK_XM / 2)
402 vx = MAX_WALK_XM / 2;
406 if (vx < -MAX_WALK_XM / 2)
407 vx = -MAX_WALK_XM / 2;
412 physic.set_velocity(vx, vy);
416 Player::handle_vertical_input()
423 physic.set_velocity(physic.get_velocity_x(), 5.5);
427 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
429 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
432 else if(input.up == UP && jumping)
435 if(physic.get_velocity_y() > 0) {
436 physic.set_velocity(physic.get_velocity_x(), 0);
442 Player::handle_input()
444 /* Handle key and joystick state: */
447 if (input.right == DOWN && input.left == UP)
449 handle_horizontal_input(RIGHT);
451 else if (input.left == DOWN && input.right == UP)
453 handle_horizontal_input(LEFT);
457 float vx = physic.get_velocity_x();
458 if(fabs(vx) < 0.01) {
459 physic.set_velocity(0, physic.get_velocity_y());
460 physic.set_acceleration(0, 0);
462 physic.set_acceleration(0.1, 0);
464 physic.set_acceleration(-0.1, 0);
471 if ( input.up == DOWN || (input.up == UP && jumping))
473 handle_vertical_input();
478 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
480 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
486 if (input.down == DOWN)
488 if (size == BIG && duck != true)
497 if (size == BIG && duck)
499 /* Make sure we're not standing back up into a solid! */
503 if (!collision_object_map(&base) /*issolid(base.x + 16, base.y - 16)*/)
508 old_base.height = 64;
524 if(!timer_check(&frame_timer))
526 timer_start(&frame_timer,25);
527 if (input.right == UP && input.left == UP)
534 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
535 (global_frame_counter % 4) == 0)
536 frame_main = (frame_main + 1) % 4;
548 Player::grabdistros()
553 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
554 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
556 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
557 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
561 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
562 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
567 /* Enough distros for a One-up? */
568 if (distros >= DISTROS_LIFEUP)
570 distros = distros - DISTROS_LIFEUP;
571 if(lives < MAX_LIVES)
573 /*We want to hear the sound even, if MAX_LIVES is reached*/
574 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
581 if (!timer_started(&safe_timer) || (global_frame_counter % 2) == 0)
585 if (timer_started(&invincible_timer))
591 texture_draw(&cape_right[global_frame_counter % 2],
592 base.x- scroll_x, base.y);
596 texture_draw(&cape_left[global_frame_counter % 2],
597 base.x- scroll_x, base.y);
604 if (physic.get_velocity_y() != 0)
607 texture_draw(&smalltux_jump_right, base.x - scroll_x, base.y - 10);
609 texture_draw(&smalltux_jump_left, base.x - scroll_x, base.y - 10);
613 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
616 texture_draw(&smalltux_stand_right, base.x - scroll_x, base.y - 9);
618 texture_draw(&smalltux_stand_left, base.x - scroll_x, base.y - 9);
623 texture_draw(&tux_right[(global_frame_counter/2) % tux_right.size()],
624 base.x - scroll_x, base.y - 9);
626 texture_draw(&tux_left[(global_frame_counter/2) % tux_left.size()],
627 base.x - scroll_x, base.y - 9);
633 /* Tux got coffee! */
637 texture_draw(&firetux_right[frame_], base.x- scroll_x, base.y);
641 texture_draw(&firetux_left[frame_], base.x- scroll_x, base.y);
647 if (timer_started(&invincible_timer))
652 texture_draw(&bigcape_right[global_frame_counter % 2],
653 base.x- scroll_x - 8, base.y);
657 texture_draw(&bigcape_left[global_frame_counter % 2],
658 base.x-scroll_x - 8, base.y);
666 if (!timer_started(&skidding_timer))
668 if (!jumping || physic.get_velocity_y() > 0)
672 texture_draw(&bigtux_right[frame_],
673 base.x- scroll_x - 8, base.y);
677 texture_draw(&bigtux_left[frame_],
678 base.x- scroll_x - 8, base.y);
685 texture_draw(&bigtux_right_jump,
686 base.x- scroll_x - 8, base.y);
690 texture_draw(&bigtux_left_jump,
691 base.x- scroll_x - 8, base.y);
699 texture_draw(&skidtux_right,
700 base.x- scroll_x - 8, base.y);
704 texture_draw(&skidtux_left,
705 base.x- scroll_x - 8, base.y);
713 texture_draw(&ducktux_right, base.x- scroll_x - 8, base.y - 16);
717 texture_draw(&ducktux_left, base.x- scroll_x - 8, base.y - 16);
723 /* Tux has coffee! */
727 if (!timer_started(&skidding_timer))
729 if (!jumping || physic.get_velocity_y() > 0)
733 texture_draw(&bigfiretux_right[frame_],
734 base.x- scroll_x - 8, base.y);
738 texture_draw(&bigfiretux_left[frame_],
739 base.x- scroll_x - 8, base.y);
746 texture_draw(&bigfiretux_right_jump,
747 base.x- scroll_x - 8, base.y);
751 texture_draw(&bigfiretux_left_jump,
752 base.x- scroll_x - 8, base.y);
760 texture_draw(&skidfiretux_right,
761 base.x- scroll_x - 8, base.y);
765 texture_draw(&skidfiretux_left,
766 base.x- scroll_x - 8, base.y);
774 texture_draw(&duckfiretux_right, base.x- scroll_x - 8, base.y - 16);
778 texture_draw(&duckfiretux_left, base.x- scroll_x - 8, base.y - 16);
786 text_drawf(&gold_text,"Penguins can fly !:",0,0,A_HMIDDLE,A_VMIDDLE,1);
790 Player::collision(void* p_c_object, int c_object)
792 BadGuy* pbad_c = NULL;
797 pbad_c = (BadGuy*) p_c_object;
798 /* Hurt the player if he just touched it: */
800 if (!pbad_c->dying && !dying &&
801 !timer_started(&safe_timer) &&
802 pbad_c->mode != HELD)
804 if (pbad_c->mode == FLAT && input.fire == DOWN)
809 else if (pbad_c->mode == KICK)
811 if (base.y < pbad_c->base.y - 16)
813 /* Step on (stop being kicked) */
816 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
820 /* Hurt if you get hit by kicked laptop: */
821 if (!timer_started(&invincible_timer))
827 pbad_c->dying = DYING_FALLING;
828 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
829 World::current()->add_score(pbad_c->base.x - scroll_x,
831 25 * player_status.score_multiplier);
837 if (!timer_started(&invincible_timer ))
846 player_status.score_multiplier++;
858 Player::kill(int mode)
860 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
862 physic.set_velocity(0, physic.get_velocity_y());
864 if (mode == SHRINK && size == BIG)
872 timer_start(&safe_timer,TUX_SAFE_TIME);
879 physic.enable_gravity(true);
880 physic.set_acceleration(0, 0);
881 physic.set_velocity(0, 7);
882 dying = DYING_SQUISHED;
896 bool Player::is_dead()
898 if(base.y > screen->h)
904 /* Remove Tux's power ups */
906 Player::remove_powerups()
914 Player::keep_in_bounds()
916 Level* plevel = World::current()->get_level();
918 /* Keep tux in bounds: */
921 else if(base.x< scroll_x)
923 else if (base.x< 160 + scroll_x && scroll_x > 0 && debug_mode)
925 scroll_x = base.x- 160;
932 else if (base.x > screen->w / 2 + scroll_x
933 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
935 // FIXME: Scrolling needs to be handled by a seperate View
936 // class, doing it as a player huck is ugly
938 // Scroll the screen in past center:
939 scroll_x = base.x - screen->w / 2;
941 if (scroll_x > ((plevel->width * 32) - screen->w))
942 scroll_x = ((plevel->width * 32) - screen->w);
944 else if (base.x> 608 + scroll_x)
946 /* ... unless there's no more to scroll! */
948 /*base.x= 608 + scroll_x;*/
951 /* Keep in-bounds, vertically: */
953 if (base.y > screen->h)