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);
106 Player::key_event(SDLKey key, int state)
108 if(key == keymap.right)
113 else if(key == keymap.left)
118 else if(key == keymap.jump)
123 else if(key == keymap.duck)
128 else if(key == keymap.fire)
138 Player::level_begin()
145 previous_base = base;
149 player_input_init(&input);
151 timer_init(&invincible_timer,true);
152 timer_init(&skidding_timer,true);
153 timer_init(&safe_timer,true);
154 timer_init(&frame_timer,true);
161 bool jumped_in_solid = false;
163 /* --- HANDLE TUX! --- */
169 previous_base = base;
171 physic.apply(base.x, base.y);
176 collision_swept_object_map(&old_base,&base);
184 physic.enable_gravity(true);
188 physic.set_velocity(physic.get_velocity_x(), 0);
189 jumped_in_solid = true;
195 if (physic.get_velocity_y() < 0)
197 base.y = (int)(((int)base.y / 32) * 32);
198 physic.set_velocity(physic.get_velocity_x(), 0);
201 physic.enable_gravity(false);
202 /* Reset score multiplier (for multi-hits): */
203 score_multiplier = 1;
208 if (isbrick(base.x, base.y) ||
209 isfullbox(base.x, base.y))
211 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
212 World::current()->trybumpbadguy(base.x, base.y - 64);
214 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
216 bumpbrick(base.x, base.y);
217 World::current()->tryemptybox(base.x, base.y, RIGHT);
220 if (isbrick(base.x+ 31, base.y) ||
221 isfullbox(base.x+ 31, base.y))
223 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
224 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
227 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
229 bumpbrick(base.x+ 31, base.y);
230 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
242 /* Make sure jumping is off. */
249 timer_check(&safe_timer);
252 /* ---- DONE HANDLING TUX! --- */
254 /* Handle invincibility timer: */
257 if (get_current_music() == HERRING_MUSIC && !timer_check(&invincible_timer))
260 no, we are no more invincible
261 or we were not in invincible mode
262 but are we in hurry ?
266 if (timer_get_left(&time_left) < TIME_WARNING)
268 /* yes, we are in hurry
269 stop the herring_song, prepare to play the correct
272 set_current_music(HURRYUP_MUSIC);
276 set_current_music(LEVEL_MUSIC);
279 /* start playing it */
280 play_current_music();
283 /* Handle skidding: */
285 // timer_check(&skidding_timer); // disabled
289 if (base.x >= endpos && endpos != 0)
299 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
300 issolid(base.x + 1, base.y + base.height) ||
301 issolid(base.x + base.width - 1, base.y + base.height) );
305 Player::under_solid()
307 return ( issolid(base.x + base.width / 2, base.y) ||
308 issolid(base.x + 1, base.y) ||
309 issolid(base.x + base.width - 1, base.y) );
313 Player::handle_horizontal_input(int newdir)
318 float vx = physic.get_velocity_x();
319 float vy = physic.get_velocity_y();
322 // skid if we're too fast
323 if(dir != newdir && on_ground() && fabs(physic.get_velocity_x()) > SKID_XM
324 && !timer_started(&skidding_timer))
326 timer_start(&skidding_timer, SKID_TIME);
327 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
331 if ((newdir ? (vx < 0) : (vx > 0)) && !isice(base.x, base.y + base.height) &&
332 !timer_started(&skidding_timer))
337 /* Facing the direction we're jumping? Go full-speed: */
338 if (input.fire == UP)
340 if(vx >= MAX_WALK_XM) {
342 physic.set_acceleration(0, 0); // enough speedup
343 } else if(vx <= -MAX_WALK_XM) {
345 physic.set_acceleration(0, 0);
347 physic.set_acceleration(newdir ? 0.02 : -0.02, 0);
348 if(fabs(vx) < 1) // set some basic run speed
349 vx = newdir ? 1 : -1;
351 vx += ( newdir ? WALK_SPEED : -WALK_SPEED) * frame_ratio;
355 if (vx > MAX_WALK_XM)
360 if (vx < -MAX_WALK_XM)
365 else if ( input.fire == DOWN)
367 if(vx >= MAX_RUN_XM) {
369 physic.set_acceleration(0, 0); // enough speedup
370 } else if(vx <= -MAX_RUN_XM) {
372 physic.set_acceleration(0, 0);
374 physic.set_acceleration(newdir ? 0.03 : -0.03, 0);
375 if(fabs(vx) < 1) // set some basic run speed
376 vx = newdir ? 1 : -1;
379 vx = vx + ( newdir ? RUN_SPEED : -RUN_SPEED) * frame_ratio;
388 if (vx < -MAX_RUN_XM)
396 /* Not facing the direction we're jumping?
398 vx = vx + ( newdir ? (WALK_SPEED / 2) : -(WALK_SPEED / 2)) * frame_ratio;
402 if (vx > MAX_WALK_XM / 2)
403 vx = MAX_WALK_XM / 2;
407 if (vx < -MAX_WALK_XM / 2)
408 vx = -MAX_WALK_XM / 2;
413 physic.set_velocity(vx, vy);
417 Player::handle_vertical_input()
424 physic.set_velocity(physic.get_velocity_x(), 5.5);
428 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
430 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
433 else if(input.up == UP && jumping)
436 if(physic.get_velocity_y() > 0) {
437 physic.set_velocity(physic.get_velocity_x(), 0);
443 Player::handle_input()
445 /* Handle key and joystick state: */
448 if (input.right == DOWN && input.left == UP)
450 handle_horizontal_input(RIGHT);
452 else if (input.left == DOWN && input.right == UP)
454 handle_horizontal_input(LEFT);
458 float vx = physic.get_velocity_x();
459 if(fabs(vx) < 0.01) {
460 physic.set_velocity(0, physic.get_velocity_y());
461 physic.set_acceleration(0, 0);
463 physic.set_acceleration(0.1, 0);
465 physic.set_acceleration(-0.1, 0);
472 if ( input.up == DOWN || (input.up == UP && jumping))
474 handle_vertical_input();
479 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
481 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
487 if (input.down == DOWN)
489 if (size == BIG && duck != true)
498 if (size == BIG && duck)
500 /* Make sure we're not standing back up into a solid! */
504 if (!collision_object_map(&base) /*issolid(base.x + 16, base.y - 16)*/)
509 old_base.height = 64;
525 if(!timer_check(&frame_timer))
527 timer_start(&frame_timer,25);
528 if (input.right == UP && input.left == UP)
535 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
536 (global_frame_counter % 4) == 0)
537 frame_main = (frame_main + 1) % 4;
549 Player::grabdistros()
554 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
555 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
557 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
558 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
562 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
563 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
568 /* Enough distros for a One-up? */
569 if (distros >= DISTROS_LIFEUP)
571 distros = distros - DISTROS_LIFEUP;
572 if(lives < MAX_LIVES)
574 /*We want to hear the sound even, if MAX_LIVES is reached*/
575 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
582 if (!timer_started(&safe_timer) || (global_frame_counter % 2) == 0)
586 if (timer_started(&invincible_timer))
592 texture_draw(&cape_right[global_frame_counter % 2],
593 base.x- scroll_x, base.y);
597 texture_draw(&cape_left[global_frame_counter % 2],
598 base.x- scroll_x, base.y);
605 if (physic.get_velocity_y() != 0)
608 texture_draw(&smalltux_jump_right, base.x - scroll_x, base.y - 10);
610 texture_draw(&smalltux_jump_left, base.x - scroll_x, base.y - 10);
614 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
617 texture_draw(&smalltux_stand_right, base.x - scroll_x, base.y - 9);
619 texture_draw(&smalltux_stand_left, base.x - scroll_x, base.y - 9);
624 texture_draw(&tux_right[(global_frame_counter/2) % tux_right.size()],
625 base.x - scroll_x, base.y - 9);
627 texture_draw(&tux_left[(global_frame_counter/2) % tux_left.size()],
628 base.x - scroll_x, base.y - 9);
634 /* Tux got coffee! */
638 texture_draw(&firetux_right[frame_], base.x- scroll_x, base.y);
642 texture_draw(&firetux_left[frame_], base.x- scroll_x, base.y);
648 if (timer_started(&invincible_timer))
653 texture_draw(&bigcape_right[global_frame_counter % 2],
654 base.x- scroll_x - 8, base.y);
658 texture_draw(&bigcape_left[global_frame_counter % 2],
659 base.x-scroll_x - 8, base.y);
667 if (!timer_started(&skidding_timer))
669 if (!jumping || physic.get_velocity_y() > 0)
673 texture_draw(&bigtux_right[frame_],
674 base.x- scroll_x - 8, base.y);
678 texture_draw(&bigtux_left[frame_],
679 base.x- scroll_x - 8, base.y);
686 texture_draw(&bigtux_right_jump,
687 base.x- scroll_x - 8, base.y);
691 texture_draw(&bigtux_left_jump,
692 base.x- scroll_x - 8, base.y);
700 texture_draw(&skidtux_right,
701 base.x- scroll_x - 8, base.y);
705 texture_draw(&skidtux_left,
706 base.x- scroll_x - 8, base.y);
714 texture_draw(&ducktux_right, base.x- scroll_x - 8, base.y - 16);
718 texture_draw(&ducktux_left, base.x- scroll_x - 8, base.y - 16);
724 /* Tux has coffee! */
728 if (!timer_started(&skidding_timer))
730 if (!jumping || physic.get_velocity_y() > 0)
734 texture_draw(&bigfiretux_right[frame_],
735 base.x- scroll_x - 8, base.y);
739 texture_draw(&bigfiretux_left[frame_],
740 base.x- scroll_x - 8, base.y);
747 texture_draw(&bigfiretux_right_jump,
748 base.x- scroll_x - 8, base.y);
752 texture_draw(&bigfiretux_left_jump,
753 base.x- scroll_x - 8, base.y);
761 texture_draw(&skidfiretux_right,
762 base.x- scroll_x - 8, base.y);
766 texture_draw(&skidfiretux_left,
767 base.x- scroll_x - 8, base.y);
775 texture_draw(&duckfiretux_right, base.x- scroll_x - 8, base.y - 16);
779 texture_draw(&duckfiretux_left, base.x- scroll_x - 8, base.y - 16);
787 text_drawf(&gold_text,"Penguins can fly !:",0,0,A_HMIDDLE,A_VMIDDLE,1);
791 Player::collision(void* p_c_object, int c_object)
793 BadGuy* pbad_c = NULL;
798 pbad_c = (BadGuy*) p_c_object;
799 /* Hurt the player if he just touched it: */
801 if (!pbad_c->dying && !dying &&
802 !timer_started(&safe_timer) &&
803 pbad_c->mode != HELD)
805 if (pbad_c->mode == FLAT && input.fire == DOWN)
810 else if (pbad_c->mode == KICK)
812 if (base.y < pbad_c->base.y - 16)
814 /* Step on (stop being kicked) */
817 play_sound(sounds[SND_STOMP], SOUND_CENTER_SPEAKER);
821 /* Hurt if you get hit by kicked laptop: */
822 if (!timer_started(&invincible_timer))
828 pbad_c->dying = DYING_FALLING;
829 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
830 World::current()->add_score(pbad_c->base.x - scroll_x,
832 25 * score_multiplier);
838 if (!timer_started(&invincible_timer ))
859 Player::kill(int mode)
861 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
863 physic.set_velocity(0, physic.get_velocity_y());
865 if (mode == SHRINK && size == BIG)
873 timer_start(&safe_timer,TUX_SAFE_TIME);
880 physic.enable_gravity(true);
881 physic.set_acceleration(0, 0);
882 physic.set_velocity(0, 7);
883 dying = DYING_SQUISHED;
897 bool Player::is_dead()
899 if(base.y > screen->h)
905 /* Remove Tux's power ups */
907 Player::remove_powerups()
915 Player::keep_in_bounds()
917 Level* plevel = GameSession::current()->get_level();
919 /* Keep tux in bounds: */
922 else if(base.x< scroll_x)
924 else if (base.x< 160 + scroll_x && scroll_x > 0 && debug_mode)
926 scroll_x = base.x- 160;
933 else if (base.x > screen->w / 2 + scroll_x
934 && scroll_x < ((GameSession::current()->get_level()->width * 32) - screen->w))
936 // FIXME: Scrolling needs to be handled by a seperate View
937 // class, doing it as a player huck is ugly
939 // Scroll the screen in past center:
940 scroll_x = base.x - screen->w / 2;
942 if (scroll_x > ((plevel->width * 32) - screen->w))
943 scroll_x = ((plevel->width * 32) - screen->w);
945 else if (base.x> 608 + scroll_x)
947 /* ... unless there's no more to scroll! */
949 /*base.x= 608 + scroll_x;*/
952 /* Keep in-bounds, vertically: */
954 if (base.y > screen->h)