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;
41 Sprite* largetux_stand_left;
42 Sprite* largetux_stand_right;
46 Sprite* bigtux_right_jump;
47 Sprite* bigtux_left_jump;
48 Sprite* ducktux_right;
50 Surface* skidtux_right;
51 Surface* skidtux_left;
52 Surface* firetux_right[3];
53 Surface* firetux_left[3];
54 Surface* bigfiretux_right[3];
55 Surface* bigfiretux_left[3];
56 Surface* bigfiretux_right_jump;
57 Surface* bigfiretux_left_jump;
58 Surface* duckfiretux_right;
59 Surface* duckfiretux_left;
60 Surface* skidfiretux_right;
61 Surface* skidfiretux_left;
62 Surface* cape_right[2];
63 Surface* cape_left[2];
64 Surface* bigcape_right[2];
65 Surface* bigcape_left[2];
69 PlayerKeymap::PlayerKeymap()
71 keymap.jump = SDLK_UP;
72 keymap.duck = SDLK_DOWN;
73 keymap.left = SDLK_LEFT;
74 keymap.right = SDLK_RIGHT;
75 keymap.fire = SDLK_LCTRL;
78 void player_input_init(player_input_type* pplayer_input)
80 pplayer_input->down = UP;
81 pplayer_input->fire = UP;
82 pplayer_input->left = UP;
83 pplayer_input->old_fire = UP;
84 pplayer_input->right = UP;
85 pplayer_input->up = UP;
91 Level* plevel = World::current()->get_level();
99 base.x = plevel->start_pos_x;
100 base.y = plevel->start_pos_y;
103 previous_base = old_base = base;
113 player_input_init(&input);
115 invincible_timer.init(true);
116 skidding_timer.init(true);
117 safe_timer.init(true);
118 frame_timer.init(true);
124 Player::key_event(SDLKey key, int state)
126 if(key == keymap.right)
131 else if(key == keymap.left)
136 else if(key == keymap.jump)
141 else if(key == keymap.duck)
146 else if(key == keymap.fire)
156 Player::level_begin()
162 previous_base = old_base = base;
167 player_input_init(&input);
169 invincible_timer.init(true);
170 skidding_timer.init(true);
171 safe_timer.init(true);
172 frame_timer.init(true);
178 Player::action(double frame_ratio)
180 bool jumped_in_solid = false;
183 previous_base = base;
185 /* --- HANDLE TUX! --- */
186 if(dying == DYING_NOT)
189 physic.apply(frame_ratio, base.x, base.y);
191 if(dying == DYING_NOT)
193 base_type target = base;
195 collision_swept_object_map(&old_base, &base);
197 // Don't accelerate Tux if he is running against a wall
198 if (target.x != base.x)
200 physic.set_velocity_x(0);
203 // special exception for cases where we're stuck under tiles after
204 // being ducked. In this case we drift out
205 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
206 && collision_object_map(&base)) {
207 base.x += frame_ratio * WALK_SPEED * (dir ? 1 : -1);
208 previous_base = old_base = base;
215 physic.enable_gravity(true);
219 physic.set_velocity_y(0);
220 jumped_in_solid = true;
226 if (physic.get_velocity_y() < 0)
228 base.y = (int)(((int)base.y / 32) * 32);
229 physic.set_velocity_y(0);
232 physic.enable_gravity(false);
233 /* Reset score multiplier (for multi-hits): */
234 player_status.score_multiplier = 1;
239 if (isbrick(base.x, base.y) ||
240 isfullbox(base.x, base.y))
242 World::current()->trygrabdistro(base.x, base.y - 32,BOUNCE);
243 World::current()->trybumpbadguy(base.x, base.y - 64);
245 World::current()->trybreakbrick(base.x, base.y, size == SMALL);
247 bumpbrick(base.x, base.y);
248 World::current()->tryemptybox(base.x, base.y, RIGHT);
251 if (isbrick(base.x+ 31, base.y) ||
252 isfullbox(base.x+ 31, base.y))
254 World::current()->trygrabdistro(base.x+ 31, base.y - 32,BOUNCE);
255 World::current()->trybumpbadguy(base.x+ 31, base.y - 64);
258 World::current()->trybreakbrick(base.x+ 31, base.y, size == SMALL);
260 bumpbrick(base.x+ 31, base.y);
261 World::current()->tryemptybox(base.x+ 31, base.y, LEFT);
273 /* Make sure jumping is off. */
279 /* ---- DONE HANDLING TUX! --- */
282 skidding_timer.check();
283 invincible_timer.check();
290 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
291 issolid(base.x + 1, base.y + base.height) ||
292 issolid(base.x + base.width - 1, base.y + base.height) );
296 Player::under_solid()
298 return ( issolid(base.x + base.width / 2, base.y) ||
299 issolid(base.x + 1, base.y) ||
300 issolid(base.x + base.width - 1, base.y) );
304 Player::handle_horizontal_input()
306 float vx = physic.get_velocity_x();
307 float vy = physic.get_velocity_y();
308 float ax = physic.get_acceleration_x();
309 float ay = physic.get_acceleration_y();
312 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
315 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
320 if (input.fire == UP) {
321 ax = dirsign * WALK_ACCELERATION_X;
323 if(vx >= MAX_WALK_XM && dirsign > 0) {
326 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
331 ax = dirsign * RUN_ACCELERATION_X;
333 if(vx >= MAX_RUN_XM && dirsign > 0) {
336 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
342 // we can reach WALK_SPEED without any acceleration
343 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
344 vx = dirsign * WALK_SPEED;
347 // changing directions?
348 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
349 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
350 skidding_timer.start(SKID_TIME);
351 play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
358 // we get slower when not pressing any keys
360 if(fabs(vx) < WALK_SPEED) {
364 ax = WALK_ACCELERATION_X * 1.5;
366 ax = WALK_ACCELERATION_X * -1.5;
370 // if we're on ice slow down acceleration or deceleration
371 if (isice(base.x, base.y + base.height))
373 /* the acceleration/deceleration rate on ice is inversely proportional to
374 * the current velocity.
377 // increasing 1 will increase acceleration/deceleration rate
378 // decreasing 1 will decrease acceleration/deceleration rate
379 // must stay above zero, though
380 if (ax != 0) ax *= 1 / fabs(vx);
383 physic.set_velocity(vx, vy);
384 physic.set_acceleration(ax, ay);
388 Player::handle_vertical_input()
394 // jump higher if we are running
395 if (physic.get_velocity_x() > MAX_WALK_XM)
396 physic.set_velocity_y(5.8);
398 physic.set_velocity_y(5.2);
403 play_sound(sounds[SND_JUMP], SOUND_CENTER_SPEAKER);
405 play_sound(sounds[SND_BIGJUMP], SOUND_CENTER_SPEAKER);
408 else if(input.up == UP && jumping)
411 if(physic.get_velocity_y() > 0) {
412 physic.set_velocity_y(0);
418 Player::handle_input()
420 /* Handle horizontal movement: */
421 handle_horizontal_input();
425 if ( input.up == DOWN || (input.up == UP && jumping))
427 handle_vertical_input();
432 if (input.fire == DOWN && input.old_fire == UP && got_coffee)
434 World::current()->add_bullet(base.x, base.y, physic.get_velocity_x(), dir);
437 /* tux animations: */
438 if(!frame_timer.check())
440 frame_timer.start(25);
441 if (input.right == UP && input.left == UP)
448 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
449 (global_frame_counter % 4) == 0)
450 frame_main = (frame_main + 1) % 4;
460 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0)
465 // changing base size confuses collision otherwise
466 old_base = previous_base = base;
468 else if(input.down == UP && size == BIG && duck && physic.get_velocity_y() == 0)
473 old_base = previous_base = base;
478 Player::grabdistros()
483 World::current()->trygrabdistro(base.x, base.y, NO_BOUNCE);
484 World::current()->trygrabdistro(base.x+ 31, base.y, NO_BOUNCE);
486 World::current()->trygrabdistro(base.x, base.y + base.height, NO_BOUNCE);
487 World::current()->trygrabdistro(base.x+ 31, base.y + base.height, NO_BOUNCE);
491 World::current()->trygrabdistro(base.x, base.y + base.height / 2, NO_BOUNCE);
492 World::current()->trygrabdistro(base.x+ 31, base.y + base.height / 2, NO_BOUNCE);
497 /* Enough distros for a One-up? */
498 if (player_status.distros >= DISTROS_LIFEUP)
500 player_status.distros = player_status.distros - DISTROS_LIFEUP;
501 if(player_status.lives < MAX_LIVES)
502 ++player_status.lives;
503 /*We want to hear the sound even, if MAX_LIVES is reached*/
504 play_sound(sounds[SND_LIFEUP], SOUND_CENTER_SPEAKER);
511 if (!safe_timer.started() || (global_frame_counter % 2) == 0)
513 if (dying == DYING_SQUISHED)
515 smalltux_gameover->draw(base.x - scroll_x, base.y);
521 if (invincible_timer.started())
524 cape_right[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
526 cape_left[global_frame_counter % 2]->draw(base.x- scroll_x, base.y);
531 if (!skidding_timer.started())
533 if (physic.get_velocity_y() != 0)
536 smalltux_jump_right->draw( base.x - scroll_x, base.y - 10);
538 smalltux_jump_left->draw( base.x - scroll_x, base.y - 10);
542 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
545 smalltux_stand_right->draw( base.x - scroll_x, base.y - 9);
547 smalltux_stand_left->draw( base.x - scroll_x, base.y - 9);
552 tux_right[(global_frame_counter/2) % tux_right.size()]->draw(base.x - scroll_x, base.y - 9);
554 tux_left[(global_frame_counter/2) % tux_left.size()]->draw(base.x - scroll_x, base.y - 9);
561 smalltux_skid_right->draw(base.x - scroll_x, base.y);
563 smalltux_skid_left->draw(base.x - scroll_x, base.y);
568 /* Tux got coffee! */
572 firetux_right[frame_]->draw( base.x- scroll_x, base.y);
576 firetux_left[frame_]->draw( base.x- scroll_x, base.y);
582 if (invincible_timer.started())
584 float capex = base.x + (base.width - bigcape_right[0]->w) / 2;
586 float capey = base.y + (base.height - bigcape_right[0]->h) / 2;
588 /* Draw cape (just not in ducked mode since that looks silly): */
590 bigcape_right[global_frame_counter % 2]->draw(capex, capey);
592 bigcape_left[global_frame_counter % 2]->draw(capex, capey);
599 if (!skidding_timer.started())
601 if (physic.get_velocity_y() == 0)
603 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
606 largetux_stand_right->draw(base.x - scroll_x, base.y);
608 largetux_stand_left->draw(base.x - scroll_x, base.y);
613 bigtux_right->draw(base.x - scroll_x, base.y);
615 bigtux_left->draw(base.x - scroll_x, base.y);
621 bigtux_right_jump->draw(base.x - scroll_x, base.y);
623 bigtux_left_jump->draw(base.x - scroll_x, base.y);
629 skidtux_right->draw(base.x - scroll_x - 8, base.y);
631 skidtux_left->draw(base.x - scroll_x - 8, base.y);
637 ducktux_right->draw(base.x - scroll_x, base.y);
639 ducktux_left->draw(base.x - scroll_x, base.y);
644 /* Tux has coffee! */
647 if (!skidding_timer.started())
649 if (!jumping || physic.get_velocity_y() > 0)
652 bigfiretux_right[frame_]->draw(base.x- scroll_x - 8, base.y);
654 bigfiretux_left[frame_]->draw(base.x- scroll_x - 8, base.y);
659 bigfiretux_right_jump->draw(base.x- scroll_x - 8, base.y);
661 bigfiretux_left_jump->draw(base.x- scroll_x - 8, base.y);
667 skidfiretux_right->draw(base.x- scroll_x - 8, base.y);
669 skidfiretux_left->draw(base.x- scroll_x - 8, base.y);
675 duckfiretux_right->draw( base.x- scroll_x - 8, base.y - 16);
677 duckfiretux_left->draw( base.x- scroll_x - 8, base.y - 16);
685 fillrect(base.x - scroll_x, base.y, 32, 32, 75,75,75, 150);
689 Player::collision(void* p_c_object, int c_object)
691 BadGuy* pbad_c = NULL;
696 pbad_c = (BadGuy*) p_c_object;
698 /* Hurt player if he touches a badguy */
699 if (!pbad_c->dying && !dying &&
700 !safe_timer.started() &&
701 pbad_c->mode != BadGuy::HELD)
703 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN)
705 pbad_c->mode = BadGuy::HELD;
708 else if (pbad_c->mode == BadGuy::FLAT)
710 // Don't get hurt if we're kicking a flat badguy!
712 else if (pbad_c->mode == BadGuy::KICK)
714 /* Hurt if you get hit by kicked laptop: */
715 if (!invincible_timer.started())
721 pbad_c->dying = DYING_FALLING;
722 play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
723 World::current()->add_score(pbad_c->base.x - scroll_x,
725 25 * player_status.score_multiplier);
730 if (!invincible_timer.started())
739 player_status.score_multiplier++;
751 Player::kill(int mode)
753 play_sound(sounds[SND_HURT], SOUND_CENTER_SPEAKER);
755 physic.set_velocity_x(0);
757 if (mode == SHRINK && size == BIG)
766 safe_timer.start(TUX_SAFE_TIME);
770 physic.enable_gravity(true);
771 physic.set_acceleration(0, 0);
772 physic.set_velocity(0, 7);
773 dying = DYING_SQUISHED;
784 bool Player::is_dead()
786 if(base.y > screen->h)
792 /* Remove Tux's power ups */
794 Player::remove_powerups()
802 Player::keep_in_bounds()
804 Level* plevel = World::current()->get_level();
806 /* Keep tux in bounds: */
808 { // Lock Tux to the size of the level, so that he doesn't fall of
812 else if (base.x < scroll_x)
817 /* Keep in-bounds, vertically: */
818 if (base.y > screen->h)
823 int scroll_threshold = screen->w/2 - 80;
826 scroll_x += screen->w/2;
827 // Backscrolling for debug mode
828 if (scroll_x < base.x - 80)
829 scroll_x = base.x - 80;
830 else if (scroll_x > base.x + 80)
831 scroll_x = base.x + 80;
832 scroll_x -= screen->w/2;
839 if (base.x > scroll_threshold + scroll_x
840 && scroll_x < ((World::current()->get_level()->width * 32) - screen->w))
842 // FIXME: Scrolling needs to be handled by a seperate View
843 // class, doing it as a player huck is ugly
845 // Scroll the screen in past center:
846 scroll_x = base.x - scroll_threshold;
848 // Lock the scrolling to the levelsize, so that we don't
849 // scroll over the right border
850 if (scroll_x > 32 * plevel->width - screen->w)
851 scroll_x = 32 * plevel->width - screen->w;