3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 std::vector<Surface*> tux_right;
33 std::vector<Surface*> tux_left;
34 Surface* smalltux_jump_left;
35 Surface* smalltux_jump_right;
36 Surface* smalltux_stand_left;
37 Surface* smalltux_stand_right;
41 Sprite* bigtux_right_jump;
42 Sprite* bigtux_left_jump;
43 Sprite* ducktux_right;
45 Surface* skidtux_right;
46 Surface* skidtux_left;
47 Surface* firetux_right[3];
48 Surface* firetux_left[3];
49 Surface* bigfiretux_right[3];
50 Surface* bigfiretux_left[3];
51 Surface* bigfiretux_right_jump;
52 Surface* bigfiretux_left_jump;
53 Surface* duckfiretux_right;
54 Surface* duckfiretux_left;
55 Surface* skidfiretux_right;
56 Surface* skidfiretux_left;
57 Surface* cape_right[2];
58 Surface* cape_left[2];
59 Surface* bigcape_right[2];
60 Surface* bigcape_left[2];
62 void player_input_init(player_input_type* pplayer_input)
64 pplayer_input->down = UP;
65 pplayer_input->fire = UP;
66 pplayer_input->left = UP;
67 pplayer_input->old_fire = UP;
68 pplayer_input->right = UP;
69 pplayer_input->up = UP;
75 Level* plevel = World::current()->get_level();
83 base.x = plevel->start_pos_x;
84 base.y = plevel->start_pos_y;
87 previous_base = old_base = base;
97 player_input_init(&input);
99 keymap.jump = SDLK_UP;
100 keymap.duck = SDLK_DOWN;
101 keymap.left = SDLK_LEFT;
102 keymap.right = SDLK_RIGHT;
103 keymap.fire = SDLK_LCTRL;
105 invincible_timer.init(true);
106 skidding_timer.init(true);
107 safe_timer.init(true);
108 frame_timer.init(true);
114 Player::key_event(SDLKey key, int state)
116 if(key == keymap.right)
121 else if(key == keymap.left)
126 else if(key == keymap.jump)
131 else if(key == keymap.duck)
136 else if(key == keymap.fire)
146 Player::level_begin()
152 previous_base = old_base = base;
157 player_input_init(&input);
159 invincible_timer.init(true);
160 skidding_timer.init(true);
161 safe_timer.init(true);
162 frame_timer.init(true);
168 Player::action(double frame_ratio)
170 bool jumped_in_solid = false;
172 /* --- HANDLE TUX! --- */
174 if(dying == DYING_NOT)
178 previous_base = base;
180 physic.apply(frame_ratio, base.x, base.y);
181 if(dying == DYING_NOT) {
182 collision_swept_object_map(&old_base, &base);
183 // special exception for cases where we're stuck under tiles after
184 // being ducked. In this case we drift out
185 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
186 && collision_object_map(&base)) {
187 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
188 previous_base = old_base = base;
193 if (dying == DYING_NOT)
200 physic.enable_gravity(true);
204 physic.set_velocity(physic.get_velocity_x(), 0);
205 jumped_in_solid = true;
211 if (physic.get_velocity_y() < 0)
213 base.y = (int)(((int)base.y / 32) * 32);
214 physic.set_velocity(physic.get_velocity_x(), 0);
217 physic.enable_gravity(false);
218 /* Reset score multiplier (for multi-hits): */
219 player_status.score_multiplier = 1;
224 if (isbrick(base.x, base.y) ||
225 isfullbox(base.x, base.y))
227 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
228 World::current()->trybumpbadguy(base.x, base.y - 64);
230 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
232 bumpbrick(base.x, base.y);
233 World::current()->tryemptybox(base.x, base.y, RIGHT);
236 if (isbrick(base.x+ 31, base.y) ||
237 isfullbox(base.x+ 31, base.y))
239 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
240 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
243 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
245 bumpbrick(base.x+ 31, base.y);
246 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
258 /* Make sure jumping is off. */
265 /* ---- DONE HANDLING TUX! --- */
267 /* Handle invincibility timer: */
268 if (get_current_music() == HERRING_MUSIC && !invincible_timer.check())
271 no, we are no more invincible
272 or we were not in invincible mode
273 but are we in hurry ?
276 // FIXME: Move this to gamesession
277 if (GameSession::current()->time_left.get_left() < TIME_WARNING)
279 /* yes, we are in hurry
280 stop the herring_song, prepare to play the correct
283 set_current_music(HURRYUP_MUSIC);
287 set_current_music(LEVEL_MUSIC);
290 /* start playing it */
291 play_current_music();
295 skidding_timer.check();
296 invincible_timer.check();
303 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
304 issolid(base.x + 1, base.y + base.height) ||
305 issolid(base.x + base.width - 1, base.y + base.height) );
309 Player::under_solid()
311 return ( issolid(base.x + base.width / 2, base.y) ||
312 issolid(base.x + 1, base.y) ||
313 issolid(base.x + base.width - 1, base.y) );
317 Player::handle_horizontal_input()
319 float vx = physic.get_velocity_x();
320 float vy = physic.get_velocity_y();
321 float ax = physic.get_acceleration_x();
322 float ay = physic.get_acceleration_y();
325 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
328 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
333 if (input.fire == UP) {
334 ax = dirsign * WALK_ACCELERATION_X;
336 if(vx >= MAX_WALK_XM && dirsign > 0) {
339 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
344 ax = dirsign * RUN_ACCELERATION_X;
346 if(vx >= MAX_RUN_XM && dirsign > 0) {
349 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
355 // we can reach WALK_SPEED without any acceleration
356 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
357 vx = dirsign * WALK_SPEED;
360 // changing directions?
361 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
362 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
363 skidding_timer.start(SKID_TIME);
364 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
371 // we get slower when not pressing any keys
373 if(fabs(vx) < WALK_SPEED) {
377 ax = WALK_ACCELERATION_X * 1.5;
379 ax = WALK_ACCELERATION_X * -1.5;
383 // if we're on ice slow down acceleration or deceleration
384 if (isice(base.x, base.y + base.height))
386 /* the acceleration/deceleration rate on ice is inversely proportional to
387 * the current velocity.
390 // increasing 1 will increase acceleration/deceleration rate
391 // decreasing 1 will decrease acceleration/deceleration rate
392 // must stay above zero, though
393 if (ax != 0) ax *= 1 / fabs(vx);
396 physic.set_velocity(vx, vy);
397 physic.set_acceleration(ax, ay);
401 Player::handle_vertical_input()
408 physic.set_velocity(physic.get_velocity_x(), 5.5);
412 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
414 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
417 else if(input.up == UP && jumping)
420 if(physic.get_velocity_y() > 0) {
421 physic.set_velocity(physic.get_velocity_x(), 0);
427 Player::handle_input()
429 /* Handle horizontal movement: */
430 handle_horizontal_input();
434 if ( input.up == DOWN || (input.up == UP && jumping))
436 handle_vertical_input();
441 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
443 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
446 /* tux animations: */
447 if(!frame_timer.check())
449 frame_timer.start(25);
450 if (input.right == UP && input.left == UP)
457 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
458 (global_frame_counter % 4) == 0)
459 frame_main = (frame_main + 1) % 4;
469 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0)
474 // changing base size confuses collision otherwise
475 old_base = previous_base = base;
477 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0)
482 old_base = previous_base = base;
487 Player::grabdistros()
492 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
493 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
495 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
496 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
500 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
501 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
506 /* Enough distros for a One-up? */
507 if (player_status.distros >= DISTROS_LIFEUP)
509 player_status.distros = player_status.distros - DISTROS_LIFEUP;
510 if(player_status.lives < MAX_LIVES)
511 ++player_status.lives;
512 /*We want to hear the sound even, if MAX_LIVES is reached*/
513 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
520 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
524 if (invincible_timer.started())
529 cape_right[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
531 cape_left[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
537 if (physic.get_velocity_y() != 0)
540 smalltux_jump_right->draw( base.x - scroll_x, base.y - 10);
542 smalltux_jump_left->draw( base.x - scroll_x, base.y - 10);
546 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
549 smalltux_stand_right->draw( base.x - scroll_x, base.y - 9);
551 smalltux_stand_left->draw( base.x - scroll_x, base.y - 9);
556 tux_right[(global_frame_counter/2) % tux_right.size()]->draw(base.x - scroll_x, base.y - 9);
558 tux_left[(global_frame_counter/2) % tux_left.size()]->draw(base.x - scroll_x, base.y - 9);
564 /* Tux got coffee! */
568 firetux_right[frame_]->draw( base.x- scroll_x, base.y);
572 firetux_left[frame_]->draw( base.x- scroll_x, base.y);
578 if (invincible_timer.started())
580 float capex = base.x + (base.width - bigcape_right[0]->w) / 2;
582 float capey = base.y + (base.height - bigcape_right[0]->h) / 2;
584 /* Draw cape (just not in ducked mode since that looks silly): */
586 bigcape_right[global_frame_counter % 2]->draw(capex, capey);
588 bigcape_left[global_frame_counter % 2]->draw(capex, capey);
595 if (!skidding_timer.started())
597 if (physic.get_velocity_y() == 0)
600 bigtux_right->draw(base.x - scroll_x, base.y);
602 bigtux_left->draw(base.x - scroll_x, base.y);
607 bigtux_right_jump->draw(base.x - scroll_x, base.y);
609 bigtux_left_jump->draw(base.x - scroll_x, base.y);
615 skidtux_right->draw(base.x - scroll_x - 8, base.y);
617 skidtux_left->draw(base.x - scroll_x - 8, base.y);
623 ducktux_right->draw(base.x - scroll_x, base.y);
625 ducktux_left->draw(base.x - scroll_x, base.y);
630 /* Tux has coffee! */
633 if (!skidding_timer.started())
635 if (!jumping || physic.get_velocity_y() > 0)
638 bigfiretux_right[frame_]->draw(base.x- scroll_x - 8, base.y);
640 bigfiretux_left[frame_]->draw(base.x- scroll_x - 8, base.y);
645 bigfiretux_right_jump->draw(base.x- scroll_x - 8, base.y);
647 bigfiretux_left_jump->draw(base.x- scroll_x - 8, base.y);
653 skidfiretux_right->draw(base.x- scroll_x - 8, base.y);
655 skidfiretux_left->draw(base.x- scroll_x - 8, base.y);
661 duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
663 duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
670 fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
674 Player::collision(void* p_c_object, int c_object)
676 BadGuy* pbad_c = NULL;
681 pbad_c = (BadGuy*) p_c_object;
683 /* Hurt player if he touches a badguy */
684 if (!pbad_c->dying && !dying &&
685 !safe_timer.started() &&
686 pbad_c->mode != HELD)
688 if (pbad_c->mode == FLAT && input.fire == DOWN)
693 else if (pbad_c->mode == FLAT)
695 // Don't get hurt if we're kicking a flat badguy!
697 else if (pbad_c->mode == KICK)
699 /* Hurt if you get hit by kicked laptop: */
700 if (!invincible_timer.started())
706 pbad_c->dying = DYING_FALLING;
707 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
708 World::current()->add_score(pbad_c->base.x - scroll_x,
710 25 * player_status.score_multiplier);
715 if (!invincible_timer.started())
724 player_status.score_multiplier++;
736 Player::kill(int mode)
738 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
740 physic.set_velocity(0, physic.get_velocity_y());
742 if (mode == SHRINK && size == BIG)
751 safe_timer.start(TUX_SAFE_TIME);
755 physic.enable_gravity(true);
756 physic.set_acceleration(0, 0);
757 physic.set_velocity(0, 7);
758 dying = DYING_SQUISHED;
769 bool Player::is_dead()
771 if(base.y > screen->h)
777 /* Remove Tux's power ups */
779 Player::remove_powerups()
787 Player::keep_in_bounds()
789 Level* plevel = World::current()->get_level();
791 /* Keep tux in bounds: */
793 { // Lock Tux to the size of the level, so that he doesn't fall of
797 else if (base.x < scroll_x)
802 /* Keep in-bounds, vertically: */
803 if (base.y > screen->h)
808 int scroll_threshold = screen->w/2 - 80;
811 scroll_x += screen->w/2;
812 // Backscrolling for debug mode
813 if (scroll_x < base.x - 80)
814 scroll_x = base.x - 80;
815 else if (scroll_x > base.x + 80)
816 scroll_x = base.x + 80;
817 scroll_x -= screen->w/2;
824 if (base.x > scroll_threshold + scroll_x
825 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
827 // FIXME: Scrolling needs to be handled by a seperate View
828 // class, doing it as a player huck is ugly
830 // Scroll the screen in past center:
831 scroll_x = base.x - scroll_threshold;
833 // Lock the scrolling to the levelsize, so that we don't
834 // scroll over the right border
835 if (scroll_x > 32 * plevel->width - screen->w)
836 scroll_x = 32 * plevel->width - screen->w;