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;
38 Sprite* smalltux_gameover;
39 Sprite* smalltux_skid_left;
40 Sprite* smalltux_skid_right;
42 Sprite* smalltux_kick_left;
43 Sprite* smalltux_kick_right;
44 Sprite* smalltux_grab_left;
45 Sprite* smalltux_grab_right;
47 Sprite* largetux_kick_left;
48 Sprite* largetux_kick_right;
49 Sprite* largetux_grab_left;
50 Sprite* largetux_grab_right;
52 Sprite* largetux_stand_left;
53 Sprite* largetux_stand_right;
57 Sprite* bigtux_right_jump;
58 Sprite* bigtux_left_jump;
59 Sprite* ducktux_right;
61 Surface* skidtux_right;
62 Surface* skidtux_left;
63 Surface* firetux_right[3];
64 Surface* firetux_left[3];
65 Surface* bigfiretux_right[3];
66 Surface* bigfiretux_left[3];
67 Surface* bigfiretux_right_jump;
68 Surface* bigfiretux_left_jump;
69 Surface* duckfiretux_right;
70 Surface* duckfiretux_left;
71 Surface* skidfiretux_right;
72 Surface* skidfiretux_left;
73 Surface* cape_right[2];
74 Surface* cape_left[2];
75 Surface* bigcape_right[2];
76 Surface* bigcape_left[2];
80 PlayerKeymap::PlayerKeymap()
82 keymap.jump = SDLK_UP;
83 keymap.duck = SDLK_DOWN;
84 keymap.left = SDLK_LEFT;
85 keymap.right = SDLK_RIGHT;
86 keymap.fire = SDLK_LCTRL;
89 void player_input_init(player_input_type* pplayer_input)
91 pplayer_input->down = UP;
92 pplayer_input->fire = UP;
93 pplayer_input->left = UP;
94 pplayer_input->old_fire = UP;
95 pplayer_input->right = UP;
96 pplayer_input->up = UP;
102 Level* plevel = World::current()->get_level();
104 holding_something = false;
112 base.x = plevel->start_pos_x;
113 base.y = plevel->start_pos_y;
116 previous_base = old_base = base;
126 player_input_init(&input);
128 invincible_timer.init(true);
129 skidding_timer.init(true);
130 safe_timer.init(true);
131 frame_timer.init(true);
137 Player::key_event(SDLKey key, int state)
139 if(key == keymap.right)
144 else if(key == keymap.left)
149 else if(key == keymap.jump)
154 else if(key == keymap.duck)
159 else if(key == keymap.fire)
169 Player::level_begin()
175 previous_base = old_base = base;
180 player_input_init(&input);
182 invincible_timer.init(true);
183 skidding_timer.init(true);
184 safe_timer.init(true);
185 frame_timer.init(true);
191 Player::action(double frame_ratio)
193 bool jumped_in_solid = false;
195 if (input.fire == UP)
196 holding_something = false;
199 previous_base = base;
201 /* --- HANDLE TUX! --- */
202 if(dying == DYING_NOT)
205 physic.apply(frame_ratio, base.x, base.y);
207 if(dying == DYING_NOT)
209 base_type target = base;
211 collision_swept_object_map(&old_base, &base);
213 // Don't accelerate Tux if he is running against a wall
214 if (target.x != base.x)
216 physic.set_velocity_x(0);
219 // special exception for cases where we're stuck under tiles after
220 // being ducked. In this case we drift out
221 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
222 && collision_object_map(base))
224 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
225 previous_base = old_base = base;
232 physic.enable_gravity(true);
236 physic.set_velocity_y(0);
237 jumped_in_solid = true;
243 if (physic.get_velocity_y() < 0)
245 base.y = (int)(((int)base.y / 32) * 32);
246 physic.set_velocity_y(0);
249 physic.enable_gravity(false);
250 /* Reset score multiplier (for multi-hits): */
251 player_status.score_multiplier = 1;
256 if (isbrick(base.x, base.y) ||
257 isfullbox(base.x, base.y))
259 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
260 World::current()->trybumpbadguy(base.x, base.y - 64);
262 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
264 bumpbrick(base.x, base.y);
265 World::current()->tryemptybox(base.x, base.y, RIGHT);
268 if (isbrick(base.x+ 31, base.y) ||
269 isfullbox(base.x+ 31, base.y))
271 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
272 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
275 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
277 bumpbrick(base.x+ 31, base.y);
278 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
290 /* Make sure jumping is off. */
296 /* ---- DONE HANDLING TUX! --- */
299 skidding_timer.check();
300 invincible_timer.check();
307 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
308 issolid(base.x + 1, base.y + base.height) ||
309 issolid(base.x + base.width - 1, base.y + base.height) );
313 Player::under_solid()
315 return ( issolid(base.x + base.width / 2, base.y) ||
316 issolid(base.x + 1, base.y) ||
317 issolid(base.x + base.width - 1, base.y) );
321 Player::handle_horizontal_input()
323 float vx = physic.get_velocity_x();
324 float vy = physic.get_velocity_y();
325 float ax = physic.get_acceleration_x();
326 float ay = physic.get_acceleration_y();
329 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
332 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
337 if (input.fire == UP) {
338 ax = dirsign * WALK_ACCELERATION_X;
340 if(vx >= MAX_WALK_XM && dirsign > 0) {
343 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
348 ax = dirsign * RUN_ACCELERATION_X;
350 if(vx >= MAX_RUN_XM && dirsign > 0) {
353 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
359 // we can reach WALK_SPEED without any acceleration
360 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
361 vx = dirsign * WALK_SPEED;
364 // changing directions?
365 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
366 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
367 skidding_timer.start(SKID_TIME);
368 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
375 // we get slower when not pressing any keys
377 if(fabs(vx) < WALK_SPEED) {
381 ax = WALK_ACCELERATION_X * 1.5;
383 ax = WALK_ACCELERATION_X * -1.5;
387 // if we're on ice slow down acceleration or deceleration
388 if (isice(base.x, base.y + base.height))
390 /* the acceleration/deceleration rate on ice is inversely proportional to
391 * the current velocity.
394 // increasing 1 will increase acceleration/deceleration rate
395 // decreasing 1 will decrease acceleration/deceleration rate
396 // must stay above zero, though
397 if (ax != 0) ax *= 1 / fabs(vx);
400 physic.set_velocity(vx, vy);
401 physic.set_acceleration(ax, ay);
405 Player::handle_vertical_input()
411 // jump higher if we are running
412 if (physic.get_velocity_x() > MAX_WALK_XM)
413 physic.set_velocity_y(5.8);
415 physic.set_velocity_y(5.2);
420 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
422 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
425 else if(input.up == UP && jumping)
428 if(physic.get_velocity_y() > 0) {
429 physic.set_velocity_y(0);
435 Player::handle_input()
437 /* Handle horizontal movement: */
438 handle_horizontal_input();
442 if ( input.up == DOWN || (input.up == UP && jumping))
444 handle_vertical_input();
449 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
451 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
454 /* tux animations: */
455 if(!frame_timer.check())
457 frame_timer.start(25);
458 if (input.right == UP && input.left == UP)
465 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
466 (global_frame_counter % 4) == 0)
467 frame_main = (frame_main + 1) % 4;
477 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0)
482 // changing base size confuses collision otherwise
483 old_base = previous_base = base;
485 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0)
490 old_base = previous_base = base;
495 Player::grabdistros()
500 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
501 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
503 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
504 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
508 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
509 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
514 /* Enough distros for a One-up? */
515 if (player_status.distros >= DISTROS_LIFEUP)
517 player_status.distros = player_status.distros - DISTROS_LIFEUP;
518 if(player_status.lives < MAX_LIVES)
519 ++player_status.lives;
520 /*We want to hear the sound even, if MAX_LIVES is reached*/
521 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
528 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
530 if (dying == DYING_SQUISHED)
532 smalltux_gameover->draw(base.x - scroll_x, base.y);
538 if (invincible_timer.started())
541 cape_right[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
543 cape_left[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
546 if (!skidding_timer.started())
548 if (physic.get_velocity_y() != 0)
551 smalltux_jump_right->draw( base.x - scroll_x, base.y - 10);
553 smalltux_jump_left->draw( base.x - scroll_x, base.y - 10);
557 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
560 smalltux_stand_right->draw( base.x - scroll_x, base.y - 9);
562 smalltux_stand_left->draw( base.x - scroll_x, base.y - 9);
567 tux_right[(global_frame_counter/2) % tux_right.size()]->draw(base.x - scroll_x, base.y - 9);
569 tux_left[(global_frame_counter/2) % tux_left.size()]->draw(base.x - scroll_x, base.y - 9);
576 smalltux_skid_right->draw(base.x - scroll_x, base.y);
578 smalltux_skid_left->draw(base.x - scroll_x, base.y);
581 if (holding_something && physic.get_velocity_y() == 0)
584 smalltux_grab_right->draw(base.x - scroll_x, base.y);
586 smalltux_grab_left->draw(base.x - scroll_x, base.y);
591 if (invincible_timer.started())
593 float capex = base.x + (base.width - bigcape_right[0]->w) / 2;
595 float capey = base.y + (base.height - bigcape_right[0]->h) / 2;
597 /* Draw cape (just not in ducked mode since that looks silly): */
599 bigcape_right[global_frame_counter % 2]->draw(capex, capey);
601 bigcape_left[global_frame_counter % 2]->draw(capex, capey);
608 if (!skidding_timer.started())
610 if (physic.get_velocity_y() == 0)
612 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
615 largetux_stand_right->draw(base.x - scroll_x, base.y);
617 largetux_stand_left->draw(base.x - scroll_x, base.y);
622 bigtux_right->draw(base.x - scroll_x, base.y);
624 bigtux_left->draw(base.x - scroll_x, base.y);
630 bigtux_right_jump->draw(base.x - scroll_x, base.y);
632 bigtux_left_jump->draw(base.x - scroll_x, base.y);
638 skidtux_right->draw(base.x - scroll_x - 8, base.y);
640 skidtux_left->draw(base.x - scroll_x - 8, base.y);
646 ducktux_right->draw(base.x - scroll_x, base.y);
648 ducktux_left->draw(base.x - scroll_x, base.y);
653 /* Tux has coffee! */
656 if (!skidding_timer.started())
658 if (!jumping || physic.get_velocity_y() > 0)
661 bigfiretux_right[frame_]->draw(base.x- scroll_x - 8, base.y);
663 bigfiretux_left[frame_]->draw(base.x- scroll_x - 8, base.y);
668 bigfiretux_right_jump->draw(base.x- scroll_x - 8, base.y);
670 bigfiretux_left_jump->draw(base.x- scroll_x - 8, base.y);
676 skidfiretux_right->draw(base.x- scroll_x - 8, base.y);
678 skidfiretux_left->draw(base.x- scroll_x - 8, base.y);
684 duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
686 duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
690 if (holding_something && !duck && physic.get_velocity_y() == 0)
693 largetux_grab_right->draw(base.x - scroll_x, base.y);
695 largetux_grab_left->draw(base.x - scroll_x, base.y);
702 fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
706 Player::collision(void* p_c_object, int c_object)
708 BadGuy* pbad_c = NULL;
713 pbad_c = (BadGuy*) p_c_object;
715 /* Hurt player if he touches a badguy */
716 if (!pbad_c->dying && !dying &&
717 !safe_timer.started() &&
718 pbad_c->mode != BadGuy::HELD)
720 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN)
722 holding_something = true;
723 pbad_c->mode = BadGuy::HELD;
726 else if (pbad_c->mode == BadGuy::FLAT)
728 // Don't get hurt if we're kicking a flat badguy!
730 else if (pbad_c->mode == BadGuy::KICK)
732 /* Hurt if you get hit by kicked laptop: */
733 if (!invincible_timer.started())
739 pbad_c->dying = DYING_FALLING;
740 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
741 World::current()->add_score(pbad_c->base.x - scroll_x,
743 25 * player_status.score_multiplier);
748 if (!invincible_timer.started())
757 player_status.score_multiplier++;
769 Player::kill(HurtMode mode)
771 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
773 physic.set_velocity_x(0);
775 if (mode == SHRINK && size == BIG)
787 safe_timer.start(TUX_SAFE_TIME);
791 physic.enable_gravity(true);
792 physic.set_acceleration(0, 0);
793 physic.set_velocity(0, 7);
794 dying = DYING_SQUISHED;
805 bool Player::is_dead()
807 if(base.y > screen->h)
813 /* Remove Tux's power ups */
815 Player::remove_powerups()
823 Player::keep_in_bounds()
825 Level* plevel = World::current()->get_level();
827 /* Keep tux in bounds: */
829 { // Lock Tux to the size of the level, so that he doesn't fall of
833 else if (base.x < scroll_x)
838 /* Keep in-bounds, vertically: */
839 if (base.y > screen->h)
844 int scroll_threshold = screen->w/2 - 80;
847 scroll_x += screen->w/2;
848 // Backscrolling for debug mode
849 if (scroll_x < base.x - 80)
850 scroll_x = base.x - 80;
851 else if (scroll_x > base.x + 80)
852 scroll_x = base.x + 80;
853 scroll_x -= screen->w/2;
860 if (base.x > scroll_threshold + scroll_x
861 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
863 // FIXME: Scrolling needs to be handled by a seperate View
864 // class, doing it as a player huck is ugly
866 // Scroll the screen in past center:
867 scroll_x = base.x - scroll_threshold;
869 // Lock the scrolling to the levelsize, so that we don't
870 // scroll over the right border
871 if (scroll_x > 32 * plevel->width - screen->w)
872 scroll_x = 32 * plevel->width - screen->w;