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.
25 #include "app/globals.h"
30 #include "special/sprite.h"
35 #include "resources.h"
36 #include "interactive_object.h"
37 #include "video/screen.h"
39 // behavior definitions:
40 #define TILES_FOR_BUTTJUMP 3
41 // animation times (in ms):
42 #define SHOOTING_TIME 320
44 #define AUTOSCROLL_DEAD_INTERVAL 300
46 // time before idle animation starts
47 #define IDLE_TIME 2500
50 Surface* growingtux_left[GROWING_FRAMES];
51 Surface* growingtux_right[GROWING_FRAMES];
55 Sprite* smalltux_gameover;
56 Sprite* smalltux_star;
59 TuxBodyParts* small_tux;
60 TuxBodyParts* big_tux;
61 TuxBodyParts* fire_tux;
62 TuxBodyParts* ice_tux;
66 PlayerKeymap::PlayerKeymap()
68 keymap.jump = SDLK_SPACE;
69 keymap.activate = SDLK_UP;
70 keymap.duck = SDLK_DOWN;
71 keymap.left = SDLK_LEFT;
72 keymap.right = SDLK_RIGHT;
73 keymap.fire = SDLK_LCTRL;
76 void player_input_init(player_input_type* pplayer_input)
78 pplayer_input->down = UP;
79 pplayer_input->fire = UP;
80 pplayer_input->left = UP;
81 pplayer_input->old_fire = UP;
82 pplayer_input->right = UP;
83 pplayer_input->up = UP;
84 pplayer_input->old_up = UP;
85 pplayer_input->activate = UP;
89 TuxBodyParts::set_action(std::string action)
91 head->set_action(action);
92 body->set_action(action);
93 arms->set_action(action);
94 feet->set_action(action);
98 TuxBodyParts::one_time_animation()
100 head->start_animation(1);
101 body->start_animation(1);
102 arms->start_animation(1);
103 feet->start_animation(1);
107 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer,
108 Uint32 drawing_effect)
110 head->draw(context, pos, layer, drawing_effect);
111 body->draw(context, pos, layer, drawing_effect);
112 arms->draw(context, pos, layer, drawing_effect);
113 feet->draw(context, pos, layer, drawing_effect);
128 holding_something = false;
134 got_power = NONE_POWER;
138 previous_base = old_base = base;
146 fall_mode = ON_GROUND;
154 player_input_init(&input);
156 invincible_timer.init(true);
157 skidding_timer.init(true);
158 safe_timer.init(true);
159 frame_timer.init(true);
160 kick_timer.init(true);
161 shooting_timer.init(true);
162 growing_timer.init(true);
163 idle_timer.init(true);
169 Player::key_event(SDLKey key, int state)
171 idle_timer.start(IDLE_TIME);
173 if(key == keymap.right)
178 else if(key == keymap.left)
183 else if(key == keymap.jump)
188 else if(key == keymap.duck)
193 else if(key == keymap.fire)
200 else if(key == keymap.activate)
202 input.activate = state;
205 /** check for interactive objects */
206 for(Sector::InteractiveObjects::iterator i
207 = Sector::current()->interactive_objects.begin();
208 i != Sector::current()->interactive_objects.end(); ++i) {
209 if(rectcollision(base, (*i)->get_area())) {
210 (*i)->interaction(INTERACTION_ACTIVATE);
222 Player::level_begin()
226 previous_base = old_base = base;
231 player_input_init(&input);
233 invincible_timer.init(true);
234 skidding_timer.init(true);
235 safe_timer.init(true);
236 frame_timer.init(true);
237 growing_timer.init(true);
238 idle_timer.init(true);
244 Player::action(float elapsed_time)
246 bool jumped_in_solid = false;
248 if(dying && !dying_timer.check()) {
253 if (input.fire == UP)
254 holding_something = false;
257 previous_base = base;
259 /* --- HANDLE TUX! --- */
260 if(dying == DYING_NOT)
263 physic.apply(elapsed_time, base.x, base.y, Sector::current()->gravity);
265 if(dying == DYING_NOT)
267 base_type target = base;
269 collision_swept_object_map(&old_base, &base);
271 if ((!invincible_timer.started() && !safe_timer.started())
272 && (isspike(base.x, base.y) || isspike(base.x + base.width, base.y)
273 || isspike(base.x, base.y + base.height)
274 || isspike(base.x + base.width, base.y + base.height)))
279 // Don't accelerate Tux if he is running against a wall
280 if (target.x != base.x)
282 physic.set_velocity_x(0);
285 // special exception for cases where we're stuck under tiles after
286 // being ducked. In this case we drift out
287 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
288 && collision_object_map(base))
290 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
291 previous_base = old_base = base;
297 physic.enable_gravity(true);
301 physic.set_velocity_y(0);
302 jumped_in_solid = true;
309 if (physic.get_velocity_y() < 0)
311 base.y = (int)(((int)base.y / 32) * 32);
312 physic.set_velocity_y(0);
315 physic.enable_gravity(false);
316 /* Reset score multiplier (for multi-hits): */
317 if (!invincible_timer.started())
318 player_status.score_multiplier = 1;
323 if (isbrick(base.x, base.y) ||
324 isfullbox(base.x, base.y))
326 Sector::current()->trygrabdistro(
327 Vector(base.x, base.y - 32), BOUNCE);
328 Sector::current()->trybumpbadguy(Vector(base.x, base.y - 64));
330 Sector::current()->trybreakbrick(
331 Vector(base.x, base.y), size == SMALL);
333 bumpbrick(base.x, base.y);
334 Sector::current()->tryemptybox(Vector(base.x, base.y), RIGHT);
337 if (isbrick(base.x+ 31, base.y) ||
338 isfullbox(base.x+ 31, base.y))
340 Sector::current()->trygrabdistro(
341 Vector(base.x+ 31, base.y - 32), BOUNCE);
342 Sector::current()->trybumpbadguy(Vector(base.x+ 31, base.y - 64));
345 Sector::current()->trybreakbrick(
346 Vector(base.x+ 31, base.y), size == SMALL);
348 bumpbrick(base.x+ 31, base.y);
349 Sector::current()->tryemptybox(Vector(base.x+ 31, base.y), LEFT);
361 /* Make sure jumping is off. */
367 /* ---- DONE HANDLING TUX! --- */
370 skidding_timer.check();
371 invincible_timer.check();
379 return ( issolid(base.x + base.width / 2, base.y + base.height) ||
380 issolid(base.x + 1, base.y + base.height) ||
381 issolid(base.x + base.width - 1, base.y + base.height));
385 Player::under_solid()
387 return ( issolid(base.x + base.width / 2, base.y) ||
388 issolid(base.x + 1, base.y) ||
389 issolid(base.x + base.width - 1, base.y) );
393 Player::tiles_on_air(int tiles)
395 for(int t = 0; t != tiles; t++)
397 if(issolid(base.x + base.width / 2, base.y + base.height + (tiles*32)) ||
398 issolid(base.x + 1, base.y + base.height + (tiles*32)) ||
399 issolid(base.x + base.width - 1, base.y + base.height + (tiles*32)))
406 Player::handle_horizontal_input()
408 float vx = physic.get_velocity_x();
409 float vy = physic.get_velocity_y();
410 float ax = physic.get_acceleration_x();
411 float ay = physic.get_acceleration_y();
414 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
418 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
424 if (input.fire == UP) {
425 ax = dirsign * WALK_ACCELERATION_X;
427 if(vx >= MAX_WALK_XM && dirsign > 0) {
430 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
435 ax = dirsign * RUN_ACCELERATION_X;
437 if(vx >= MAX_RUN_XM && dirsign > 0) {
440 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
446 // we can reach WALK_SPEED without any acceleration
447 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
448 vx = dirsign * WALK_SPEED;
451 // changing directions?
452 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
453 if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
454 skidding_timer.start(SKID_TIME);
455 SoundManager::get()->play_sound(IDToSound(SND_SKID));
462 // we get slower when not pressing any keys
464 if(fabs(vx) < WALK_SPEED) {
468 ax = WALK_ACCELERATION_X * 1.5;
470 ax = WALK_ACCELERATION_X * -1.5;
474 // if we're on ice slow down acceleration or deceleration
475 if (isice(base.x, base.y + base.height))
477 /* the acceleration/deceleration rate on ice is inversely proportional to
478 * the current velocity.
481 // increasing 1 will increase acceleration/deceleration rate
482 // decreasing 1 will decrease acceleration/deceleration rate
483 // must stay above zero, though
484 if (ax != 0) ax *= 1 / fabs(vx);
487 physic.set_velocity(vx, vy);
488 physic.set_acceleration(ax, ay);
492 Player::handle_vertical_input()
496 fall_mode = ON_GROUND;
497 last_ground_y = base.y;
499 if(base.y > last_ground_y)
501 else if(fall_mode == ON_GROUND)
506 if(input.up == DOWN && can_jump && on_ground())
508 if(duck) { // only jump a little bit when in duck mode {
509 physic.set_velocity_y(3);
511 // jump higher if we are running
512 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
513 physic.set_velocity_y(5.8);
515 physic.set_velocity_y(5.2);
522 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
524 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
526 // Let go of jump key
527 else if(input.up == UP && jumping && physic.get_velocity_y() > 0)
530 physic.set_velocity_y(0);
533 /* In case the player has pressed Down while in a certain range of air,
534 enable butt jump action */
535 if (input.down == DOWN && !butt_jump && !duck)
536 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
539 /* When Down is not held anymore, disable butt jump */
540 if(butt_jump && input.down == UP)
544 if (butt_jump && on_ground() && size == BIG)
548 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y));
550 Sector::current()->add_smoke_cloud(Vector(base.x - 32, base.y + 32));
554 // Break bricks beneath Tux
555 if(Sector::current()->trybreakbrick(
556 Vector(base.x + 1, base.y + base.height), false)
557 || Sector::current()->trybreakbrick(
558 Vector(base.x + base.width - 1, base.y + base.height), false))
560 physic.set_velocity_y(2);
564 // Kill nearby badguys
565 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
566 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
567 i != gameobjects.end();
570 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
574 if (fabsf(base.x - badguy->base.x) < 150 &&
575 fabsf(base.y - badguy->base.y) < 60 &&
576 (issolid(badguy->base.x + 1, badguy->base.y + badguy->base.height) ||
577 issolid(badguy->base.x + badguy->base.width - 1, badguy->base.y + badguy->base.height)))
583 if ( (issolid(base.x + base.width / 2, base.y + base.height + 64) ||
584 issolid(base.x + 1, base.y + base.height + 64) ||
585 issolid(base.x + base.width - 1, base.y + base.height + 64))
589 && input.old_up == UP)
594 if(on_ground()) /* Make sure jumping is off. */
597 input.old_up = input.up;
601 Player::handle_input()
603 /* Handle horizontal movement: */
604 handle_horizontal_input();
608 if (on_ground() && input.up == UP)
610 handle_vertical_input();
613 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER)
615 if(Sector::current()->add_bullet(Vector(base.x, base.y + (base.height/2)),
616 physic.get_velocity_x(), dir))
617 shooting_timer.start(SHOOTING_TIME);
618 input.old_fire = DOWN;
621 /* tux animations: */
622 if(!frame_timer.check())
624 frame_timer.start(25);
625 if (input.right == UP && input.left == UP)
632 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
633 (global_frame_counter % 4) == 0)
634 frame_main = (frame_main + 1) % 4;
644 if (input.down == DOWN && size == BIG && !duck && physic.get_velocity_y() == 0 && on_ground())
649 // changing base size confuses collision otherwise
650 old_base = previous_base = base;
652 else if(input.down == UP && size == BIG && duck)
654 // try if we can really unduck
657 // when unducking in air we need some space to do so
658 if(on_ground() || !collision_object_map(base)) {
660 // changing base size confuses collision otherwise
661 old_base = previous_base = base;
663 // undo the ducking changes
671 Player::grow(bool animate)
681 growing_timer.start(GROWING_TIME);
683 old_base = previous_base = base;
687 Player::grabdistros()
692 Sector::current()->trygrabdistro(Vector(base.x, base.y), NO_BOUNCE);
693 Sector::current()->trygrabdistro(Vector(base.x+ 31, base.y), NO_BOUNCE);
694 Sector::current()->trygrabdistro(
695 Vector(base.x, base.y + base.height), NO_BOUNCE);
696 Sector::current()->trygrabdistro(
697 Vector(base.x+ 31, base.y + base.height), NO_BOUNCE);
701 Sector::current()->trygrabdistro(
702 Vector(base.x, base.y + base.height / 2), NO_BOUNCE);
703 Sector::current()->trygrabdistro(
704 Vector(base.x+ 31, base.y + base.height / 2), NO_BOUNCE);
709 /* Enough distros for a One-up? */
710 if (player_status.distros >= DISTROS_LIFEUP)
712 player_status.distros = player_status.distros - DISTROS_LIFEUP;
713 if(player_status.lives < MAX_LIVES)
714 ++player_status.lives;
715 /*We want to hear the sound even, if MAX_LIVES is reached*/
716 SoundManager::get()->play_sound(IDToSound(SND_LIFEUP));
721 Player::draw(DrawingContext& context)
723 TuxBodyParts* tux_body;
726 tux_body = small_tux;
727 else if (got_power == FIRE_POWER)
729 else if (got_power == ICE_POWER)
734 int layer = LAYER_OBJECTS - 1;
735 Vector pos = Vector(base.x, base.y);
737 if ((!safe_timer.started() || growing_timer.started()) && (global_frame_counter % 2))
739 if (dying == DYING_SQUISHED)
741 smalltux_gameover->draw(context, pos, LAYER_FOREGROUNDTILES+1);
745 if(growing_timer.check())
750 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
751 ((growing_timer.get_gone() *
752 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
754 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
755 ((growing_timer.get_gone() *
756 GROWING_FRAMES) / GROWING_TIME)], pos, layer);
761 context.draw_surface(growingtux_right[(growing_timer.get_gone() *
762 GROWING_FRAMES) / GROWING_TIME], pos, layer);
764 context.draw_surface(growingtux_left[(growing_timer.get_gone() *
765 GROWING_FRAMES) / GROWING_TIME], pos, layer);
768 else if (duck && size != SMALL)
771 tux_body->set_action("duck-right");
773 tux_body->set_action("duck-left");
776 else if (skidding_timer.started())
779 tux_body->set_action("skid-right");
781 tux_body->set_action("skid-left");
783 else if (kick_timer.started())
786 tux_body->set_action("kick-right");
788 tux_body->set_action("kick-left");
793 tux_body->set_action("buttjump-right");
795 tux_body->set_action("buttjump-left");
797 else if (physic.get_velocity_y() != 0)
800 tux_body->set_action("jump-right");
802 tux_body->set_action("jump-left");
806 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
809 tux_body->set_action("stand-right");
811 tux_body->set_action("stand-left");
816 tux_body->set_action("walk-right");
818 tux_body->set_action("walk-left");
824 if(idle_timer.get_left() < 0)
828 tux_body->set_action("idle-right");
829 tux_body->one_time_animation();
833 tux_body->set_action("idle-left");
834 tux_body->one_time_animation();
836 idle_timer.start(IDLE_TIME);
839 // Tux is holding something
840 if ((holding_something && physic.get_velocity_y() == 0) ||
841 shooting_timer.check() && !duck)
844 tux_body->arms->set_action("grab-right");
846 tux_body->arms->set_action("grab-left");
849 if(dying != DYING_SQUISHED && !growing_timer.check())
850 tux_body->draw(context, pos, layer);
852 // Draw blinking star overlay
853 if (invincible_timer.started() &&
854 (invincible_timer.get_left() > TUX_INVINCIBLE_TIME_WARNING || global_frame_counter % 3))
856 if (size == SMALL || duck)
857 smalltux_star->draw(context, pos, LAYER_OBJECTS + 2);
859 bigtux_star->draw(context, pos, LAYER_OBJECTS + 2);
863 context.draw_filled_rect(Vector(base.x, base.y),
864 Vector(base.width, base.height), Color(75,75,75, 150), LAYER_OBJECTS+1);
868 Player::collision(const MovingObject& other, int collision_type)
871 (void) collision_type;
872 // will be implemented later
876 Player::collision(void* p_c_object, int c_object)
878 BadGuy* pbad_c = NULL;
879 Trampoline* ptramp_c = NULL;
880 FlyingPlatform* pplatform_c = NULL;
885 pbad_c = (BadGuy*) p_c_object;
887 /* Hurt player if he touches a badguy */
888 if (!pbad_c->dying && !dying &&
889 !safe_timer.started() &&
890 pbad_c->mode != BadGuy::HELD)
892 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
893 && !holding_something)
895 holding_something = true;
896 pbad_c->mode = BadGuy::HELD;
899 else if (pbad_c->mode == BadGuy::FLAT)
901 // Don't get hurt if we're kicking a flat badguy!
903 else if (pbad_c->mode == BadGuy::KICK)
905 /* Hurt if you get hit by kicked laptop: */
906 if (!invincible_timer.started())
913 else if (pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
914 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
915 || pbad_c->kind == BAD_SPIKY))
919 if (!invincible_timer.started())
928 player_status.score_multiplier++;
933 ptramp_c = (Trampoline*) p_c_object;
935 // Pick up trampoline
936 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
938 holding_something = true;
939 ptramp_c->mode = Trampoline::M_HELD;
940 ptramp_c->base.y -= 8;
942 // Set down trampoline
943 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
945 holding_something = false;
946 ptramp_c->mode = Trampoline::M_NORMAL;
947 ptramp_c->base.y += 8;
948 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
951 // ptramp_c->base.x = base.x + base.width+1;
953 // ptramp_c->base.x = base.x - base.width-1;
956 // Don't let tux walk through trampoline
957 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
959 if (physic.get_velocity_x() > 0) // RIGHT
961 physic.set_velocity_x(0);
962 base.x = ptramp_c->base.x - base.width;
964 else if (physic.get_velocity_x() < 0) // LEFT
966 physic.set_velocity_x(0);
967 base.x = ptramp_c->base.x + ptramp_c->base.width;
972 case CO_FLYING_PLATFORM:
973 pplatform_c = (FlyingPlatform*) p_c_object;
975 base.y = pplatform_c->base.y - base.height;
976 physic.set_velocity_x(pplatform_c->get_vel_x());
978 physic.enable_gravity(false);
980 fall_mode = ON_GROUND;
992 Player::kill(HurtMode mode)
997 SoundManager::get()->play_sound(IDToSound(SND_HURT));
999 physic.set_velocity_x(0);
1001 if (mode == SHRINK && size == BIG)
1003 if (got_power != NONE_POWER)
1005 safe_timer.start(TUX_SAFE_TIME);
1006 got_power = NONE_POWER;
1010 growing_timer.start(GROWING_TIME);
1011 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
1019 physic.enable_gravity(true);
1020 physic.set_acceleration(0, 0);
1021 physic.set_velocity(0, 7);
1022 --player_status.lives;
1023 dying = DYING_SQUISHED;
1024 dying_timer.start(3000);
1028 /* Remove Tux's power ups */
1030 Player::remove_powerups()
1032 got_power = NONE_POWER;
1038 Player::move(const Vector& vector)
1042 old_base = previous_base = base;
1046 Player::check_bounds(Camera* camera)
1048 /* Keep tux in bounds: */
1050 { // Lock Tux to the size of the level, so that he doesn't fall of
1055 /* Keep in-bounds, vertically: */
1056 if (base.y > Sector::current()->solids->get_height() * 32)
1062 bool adjust = false;
1063 // can happen if back scrolling is disabled
1064 if(base.x < camera->get_translation().x) {
1065 base.x = camera->get_translation().x;
1068 if(base.x >= camera->get_translation().x + screen->w - base.width) {
1069 base.x = camera->get_translation().x + screen->w - base.width;
1075 if(collision_object_map(base)) {
1083 Player::bounce(BadGuy* badguy)
1086 physic.set_velocity_y(5.2);
1088 physic.set_velocity_y(2);
1090 // Move the player a little bit above the badguy to avoid collision
1091 // between badguy and player directly after the bounce has happend
1092 base.y = badguy->base.y - base.height - 2;