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.
26 #include "app/globals.h"
27 #include "app/gettext.h"
32 #include "special/sprite.h"
37 #include "resources.h"
38 #include "video/screen.h"
39 #include "statistics.h"
41 #include "trigger/trigger_base.h"
43 // behavior definitions:
44 #define TILES_FOR_BUTTJUMP 3
45 // animation times (in ms):
46 #define SHOOTING_TIME .150
48 // time before idle animation starts
49 #define IDLE_TIME 2.500
52 Surface* growingtux_left[GROWING_FRAMES];
53 Surface* growingtux_right[GROWING_FRAMES];
55 Surface* tux_life = 0;
57 Sprite* smalltux_gameover = 0;
58 Sprite* smalltux_star = 0;
59 Sprite* bigtux_star = 0;
61 TuxBodyParts* small_tux = 0;
62 TuxBodyParts* big_tux = 0;
63 TuxBodyParts* fire_tux = 0;
64 TuxBodyParts* ice_tux = 0;
68 PlayerKeymap::PlayerKeymap()
71 keymap.down = SDLK_DOWN;
72 keymap.left = SDLK_LEFT;
73 keymap.right = SDLK_RIGHT;
75 keymap.power = SDLK_LCTRL;
76 keymap.jump = SDLK_LALT;
79 void player_input_init(player_input_type* pplayer_input)
81 pplayer_input->up = UP;
82 pplayer_input->old_up = UP;
83 pplayer_input->down = UP;
84 pplayer_input->fire = UP;
85 pplayer_input->left = UP;
86 pplayer_input->old_fire = UP;
87 pplayer_input->right = UP;
88 pplayer_input->jump = UP;
89 pplayer_input->old_jump = UP;
90 pplayer_input->activate = UP;
94 TuxBodyParts::set_action(std::string action)
97 head->set_action(action);
99 body->set_action(action);
101 arms->set_action(action);
103 feet->set_action(action);
107 TuxBodyParts::one_time_animation()
110 head->start_animation(1);
112 body->start_animation(1);
114 arms->start_animation(1);
116 feet->start_animation(1);
120 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer,
121 Uint32 drawing_effect)
124 head->draw(context, pos, layer-1, drawing_effect);
126 body->draw(context, pos, layer-3, drawing_effect);
128 arms->draw(context, pos, layer, drawing_effect);
130 feet->draw(context, pos, layer-2, drawing_effect);
145 holding_something = false;
147 bbox.set_size(32, 32);
150 got_power = NONE_POWER;
159 fall_mode = ON_GROUND;
164 falling_from_flap = false;
165 enable_hover = false;
168 flapping_velocity = 0;
170 // temporary to help player's choosing a flapping
171 flapping_mode = MAREK_FLAP;
173 // Ricardo's flapping
176 on_ground_flag = false;
181 player_input_init(&input);
183 frame_timer.start(.025, true);
189 Player::key_event(SDLKey key, int state)
191 idle_timer.start(IDLE_TIME, true);
193 if(key == keymap.right)
198 else if(key == keymap.left)
203 else if(key == keymap.up)
208 /* Up key also opens activates stuff */
209 input.activate = state;
212 else if(key == keymap.down)
217 else if(key == keymap.power)
225 else if(key == keymap.jump)
237 Player::level_begin()
239 move(Vector(100, 170));
244 player_input_init(&input);
246 on_ground_flag = false;
254 return player_status;
258 Player::action(float elapsed_time)
260 if(dying && dying_timer.check()) {
265 if (input.fire == UP)
266 holding_something = false;
268 if(dying == DYING_NOT)
271 movement = physic.get_movement(elapsed_time);
274 // special exception for cases where we're stuck under tiles after
275 // being ducked. In this case we drift out
276 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
277 && collision_object_map(base))
279 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
280 previous_base = old_base = base;
283 /* Reset score multiplier (for multi-hits): */
284 if (!invincible_timer.started())
286 if(player_status.score_multiplier > player_status.max_score_multiplier)
288 player_status.max_score_multiplier = player_status.score_multiplier;
292 sprintf(str, _("New max combo: %d"), player_status.max_score_multiplier-1);
293 Sector::current()->add_floating_text(base, str);
295 player_status.score_multiplier = 1;
302 on_ground_flag = false;
308 return on_ground_flag;
312 Player::handle_horizontal_input()
314 float vx = physic.get_velocity_x();
315 float vy = physic.get_velocity_y();
316 float ax = physic.get_acceleration_x();
317 float ay = physic.get_acceleration_y();
320 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
324 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
330 if (input.fire == UP) {
331 ax = dirsign * WALK_ACCELERATION_X;
333 if(vx >= MAX_WALK_XM && dirsign > 0) {
336 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
341 ax = dirsign * RUN_ACCELERATION_X;
343 if(vx >= MAX_RUN_XM && dirsign > 0) {
346 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
352 // we can reach WALK_SPEED without any acceleration
353 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
354 vx = dirsign * WALK_SPEED;
357 // changing directions?
358 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0)))
361 if(fabs(vx)>SKID_XM && !skidding_timer.started())
363 skidding_timer.start(SKID_TIME);
364 SoundManager::get()->play_sound(IDToSound(SND_SKID));
365 // dust some partcles
366 Sector::current()->add_particles(
367 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
369 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
370 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, 800,
381 // we get slower when not pressing any keys
383 if(fabs(vx) < WALK_SPEED) {
387 ax = WALK_ACCELERATION_X * 1.5;
389 ax = WALK_ACCELERATION_X * -1.5;
394 // if we're on ice slow down acceleration or deceleration
395 if (isice(base.x, base.y + base.height))
397 /* the acceleration/deceleration rate on ice is inversely proportional to
398 * the current velocity.
401 // increasing 1 will increase acceleration/deceleration rate
402 // decreasing 1 will decrease acceleration/deceleration rate
403 // must stay above zero, though
404 if (ax != 0) ax *= 1 / fabs(vx);
408 physic.set_velocity(vx, vy);
409 physic.set_acceleration(ax, ay);
413 Player::handle_vertical_input()
417 fall_mode = ON_GROUND;
418 last_ground_y = get_pos().y;
420 if(get_pos().y > last_ground_y)
422 else if(fall_mode == ON_GROUND)
427 if(input.jump == DOWN && can_jump && on_ground())
429 if(duck) { // only jump a little bit when in duck mode {
430 physic.set_velocity_y(300);
432 // jump higher if we are running
433 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
434 physic.set_velocity_y(580);
436 physic.set_velocity_y(520);
439 //bbox.move(Vector(0, -1));
444 flaps_nb = 0; // Ricardo's flapping
446 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
448 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
450 // Let go of jump key
451 else if(input.jump == UP)
453 if (!flapping && !duck && !falling_from_flap && !on_ground())
457 if (jumping && physic.get_velocity_y() > 0)
459 printf("jumpend.\n");
461 physic.set_velocity_y(0);
465 // temporary to help player's choosing a flapping
466 if(flapping_mode == RICARDO_FLAP)
468 // Flapping, Ricardo's version
469 // similar to SM3 Fox
470 if(input.jump == DOWN && input.old_jump == UP && can_flap &&
473 physic.set_velocity_y(350);
474 physic.set_velocity_x(physic.get_velocity_x() * 35);
478 else if(flapping_mode == MAREK_FLAP)
480 // Flapping, Marek's version
481 if (input.jump == DOWN && can_flap)
483 if (!flapping_timer.started())
485 flapping_timer.start(TUX_FLAPPING_TIME);
486 flapping_velocity = physic.get_velocity_x();
488 if (flapping_timer.check())
491 falling_from_flap = true;
495 if (!flapping_timer.check()) {
496 float cv = flapping_velocity * sqrt(
497 TUX_FLAPPING_TIME - flapping_timer.get_timegone()
498 / TUX_FLAPPING_TIME);
500 //Handle change of direction while flapping
501 if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
504 physic.set_velocity_x(cv);
505 physic.set_velocity_y(
506 flapping_timer.get_timegone()/.850);
510 else if(flapping_mode == RYAN_FLAP)
512 // Flapping, Ryan's version
513 if (input.jump == DOWN && can_flap)
515 if (!flapping_timer.started())
517 flapping_timer.start(TUX_FLAPPING_TIME);
519 if (flapping_timer.check())
522 falling_from_flap = true;
526 if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
527 && physic.get_velocity_y() < 0)
529 float gravity = Sector::current()->gravity;
531 float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
533 // XXX: magic numbers. should be a percent of gravity
534 // gravity is (by default) -0.1f
535 physic.set_acceleration_y(12 + 1*xr);
538 // To slow down x-vel when flapping (not working)
539 if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
541 if (physic.get_velocity_x() < 0)
542 physic.set_acceleration_x(1.0f);
543 else if (physic.get_velocity_x() > 0)
544 physic.set_acceleration_x(-1.0f);
551 physic.set_acceleration_y(0);
556 //(disabled by default, use cheat code "hover" to toggle on/off)
557 //TODO: needs some tweaking, especially when used together with double jump and jumping off badguys
558 if (enable_hover && input.jump == DOWN && !jumping && !butt_jump && physic.get_velocity_y() <= 0)
560 physic.set_velocity_y(-100);
564 /* In case the player has pressed Down while in a certain range of air,
565 enable butt jump action */
566 if (input.down == DOWN && !butt_jump && !duck)
567 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
571 /* When Down is not held anymore, disable butt jump */
572 if(butt_jump && input.down == UP)
576 if (butt_jump && on_ground() && size == BIG)
580 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
582 Sector::current()->add_smoke_cloud(
583 Vector(get_pos().x - 32, get_pos().y + 32));
588 // Break bricks beneath Tux
589 if(Sector::current()->trybreakbrick(
590 Vector(base.x + 1, base.y + base.height), false)
591 || Sector::current()->trybreakbrick(
592 Vector(base.x + base.width - 1, base.y + base.height), false))
594 physic.set_velocity_y(2);
600 // Kill nearby badguys
601 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
602 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
603 i != gameobjects.end();
606 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
609 // don't kill when badguys are already dying or in a certain mode
610 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
611 badguy->mode != BadGuy::BOMB_EXPLODE)
613 if (fabsf(base.x - badguy->base.x) < 96 &&
614 fabsf(base.y - badguy->base.y) < 64)
622 /** jumping is only allowed if we're about to touch ground soon and if the
623 * button has been up in between the last jump
627 if ( (issolid(get_pos().x + bbox.get_width() / 2,
628 get_pos().y + bbox.get_height() + 64) ||
629 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
630 issolid(get_pos().x + bbox.get_width() - 1,
631 get_pos().y + bbox.get_height() + 64))
634 && input.jump == DOWN
635 && input.old_jump == UP)
641 if(on_ground()) /* Make sure jumping is off. */
645 falling_from_flap = false;
646 if (flapping_timer.started()) {
647 flapping_timer.start(0);
650 physic.set_acceleration_y(0); //for flapping
653 input.old_jump = input.jump;
657 Player::handle_input()
659 /* Handle horizontal movement: */
660 handle_horizontal_input();
663 if (on_ground() && input.jump == UP)
665 handle_vertical_input();
668 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER) {
669 if(Sector::current()->add_bullet(
670 get_pos() + Vector(0, bbox.get_height()/2),
671 physic.get_velocity_x(), dir))
672 shooting_timer.start(SHOOTING_TIME);
673 input.old_fire = DOWN;
677 /* tux animations: */
678 if(frame_timer.check()) {
679 if (input.right == UP && input.left == UP)
686 if ((input.fire == DOWN && (global_frame_counter % 2) == 0) ||
687 (global_frame_counter % 4) == 0)
688 frame_main = (frame_main + 1) % 4;
699 if (input.down == DOWN && size == BIG && !duck
700 && physic.get_velocity_y() == 0 && on_ground())
703 bbox.move(Vector(0, 32));
706 else if(input.down == UP && size == BIG && duck)
708 // try if we can really unduck
709 bbox.move(Vector(0, -32));
714 // when unducking in air we need some space to do so
715 if(on_ground() || !collision_object_map(bbox)) {
718 // undo the ducking changes
719 bbox.move(Vector(0, 32));
727 Player::grow(bool animate)
734 bbox.move(Vector(0, -32));
737 growing_timer.start(GROWING_TIME);
741 Player::grabdistros()
746 Player::draw(DrawingContext& context)
748 TuxBodyParts* tux_body;
751 tux_body = small_tux;
752 else if (got_power == FIRE_POWER)
754 else if (got_power == ICE_POWER)
759 int layer = LAYER_OBJECTS - 1;
761 /* Set Tux sprite action */
762 if (duck && size == BIG)
765 tux_body->set_action("duck-left");
767 tux_body->set_action("duck-right");
769 else if (skidding_timer.started() && !skidding_timer.check())
772 tux_body->set_action("skid-left");
774 tux_body->set_action("skid-right");
776 else if (kick_timer.started() && !kick_timer.check())
779 tux_body->set_action("kick-left");
781 tux_body->set_action("kick-right");
783 else if (butt_jump && size == BIG)
786 tux_body->set_action("buttjump-left");
788 tux_body->set_action("buttjump-right");
790 else if (physic.get_velocity_y() != 0)
793 tux_body->set_action("jump-left");
795 tux_body->set_action("jump-right");
799 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
802 tux_body->set_action("stand-left");
804 tux_body->set_action("stand-right");
809 tux_body->set_action("walk-left");
811 tux_body->set_action("walk-right");
815 if(idle_timer.check())
820 tux_body->head->set_action("idle-left");
822 tux_body->head->set_action("idle-right");
824 tux_body->head->start_animation(1);
829 // Tux is holding something
830 if ((holding_something && physic.get_velocity_y() == 0) ||
831 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
836 tux_body->arms->set_action("duck+grab-left");
838 tux_body->arms->set_action("duck+grab-right");
843 tux_body->arms->set_action("grab-left");
845 tux_body->arms->set_action("grab-right");
850 if (dying == DYING_SQUISHED) {
851 smalltux_gameover->draw(context, get_pos(), LAYER_FOREGROUNDTILES+1);
852 } else if(growing_timer.get_timeleft() > 0) {
856 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
857 int((growing_timer.get_timegone() *
858 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
860 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
861 int((growing_timer.get_timegone() *
862 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
867 context.draw_surface(growingtux_right[
868 int((growing_timer.get_timegone() *
869 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
871 context.draw_surface(growingtux_left[
872 int((growing_timer.get_timegone() *
873 GROWING_FRAMES) / GROWING_TIME)],
877 else if (safe_timer.started() && global_frame_counter%2)
880 tux_body->draw(context, get_pos(), layer);
882 // Draw blinking star overlay
883 if (invincible_timer.started() &&
884 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
885 || global_frame_counter % 3)
888 if (size == SMALL || duck)
889 smalltux_star->draw(context, get_pos(), LAYER_OBJECTS + 2);
891 bigtux_star->draw(context, get_pos(), LAYER_OBJECTS + 2);
895 context.draw_filled_rect(get_pos(),
896 Vector(bbox.get_width(), bbox.get_height()),
897 Color(75,75,75, 150), LAYER_OBJECTS+1);
901 Player::collision(GameObject& other, const CollisionHit& hit)
907 if(other.get_flags() & FLAG_SOLID) {
908 if(hit.normal.y < 0) { // landed on floor?
909 if (physic.get_velocity_y() < 0)
910 physic.set_velocity_y(0);
911 on_ground_flag = true;
912 } else if(hit.normal.y > 0) { // bumped against the roof
913 physic.set_velocity_y(0);
916 if(hit.normal.x != 0) { // hit on the side?
917 if(hit.normal.y > 0.6) // limits the slopes we can move up...
918 physic.set_velocity_x(0);
924 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
926 if(input.up == DOWN && input.old_up == UP)
927 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
935 Player::collision(void* p_c_object, int c_object)
937 //BadGuy* pbad_c = NULL;
938 //Trampoline* ptramp_c = NULL;
939 //FlyingPlatform* pplatform_c = NULL;
944 pbad_c = (BadGuy*) p_c_object;
946 /* Hurt player if he touches a badguy */
947 if (!pbad_c->dying && !dying &&
948 !safe_timer.started() &&
949 pbad_c->mode != BadGuy::HELD)
951 if (pbad_c->mode == BadGuy::FLAT && input.fire == DOWN
952 && !holding_something)
954 holding_something = true;
955 pbad_c->mode = BadGuy::HELD;
958 else if (pbad_c->mode == BadGuy::FLAT)
960 // Don't get hurt if we're kicking a flat badguy!
962 else if (pbad_c->mode == BadGuy::KICK)
964 /* Hurt if you get hit by kicked laptop: */
965 if (!invincible_timer.started())
972 else if (!pbad_c->frozen_timer.check() && (pbad_c->kind == BAD_MRBOMB
973 || pbad_c->kind == BAD_JUMPY || pbad_c->kind == BAD_FISH
974 || pbad_c->kind == BAD_SPIKY))
978 if (!invincible_timer.started())
987 player_status.score_multiplier++;
992 ptramp_c = (Trampoline*) p_c_object;
994 // Pick up trampoline
995 if (ptramp_c->mode != Trampoline::M_HELD && input.fire == DOWN && !holding_something && on_ground())
997 holding_something = true;
998 ptramp_c->mode = Trampoline::M_HELD;
999 ptramp_c->base.y -= 8;
1001 // Set down trampoline
1002 else if (ptramp_c->mode == Trampoline::M_HELD && input.fire != DOWN)
1004 holding_something = false;
1005 ptramp_c->mode = Trampoline::M_NORMAL;
1006 ptramp_c->base.y += 8;
1007 ptramp_c->physic.set_velocity(physic.get_velocity_x(), physic.get_velocity_y());
1010 // ptramp_c->base.x = base.x + base.width+1;
1012 // ptramp_c->base.x = base.x - base.width-1;
1015 // Don't let tux walk through trampoline
1016 else if (ptramp_c->mode != Trampoline::M_HELD && on_ground())
1018 if (physic.get_velocity_x() > 0) // RIGHT
1020 physic.set_velocity_x(0);
1021 base.x = ptramp_c->base.x - base.width;
1023 else if (physic.get_velocity_x() < 0) // LEFT
1025 physic.set_velocity_x(0);
1026 base.x = ptramp_c->base.x + ptramp_c->base.width;
1031 case CO_FLYING_PLATFORM:
1032 pplatform_c = (FlyingPlatform*) p_c_object;
1034 base.y = pplatform_c->base.y - base.height;
1035 physic.set_velocity_x(pplatform_c->get_vel_x());
1037 physic.enable_gravity(false);
1039 fall_mode = ON_GROUND;
1050 Player::make_invincible()
1052 SoundManager::get()->play_sound(IDToSound(SND_HERRING));
1053 invincible_timer.start(TUX_INVINCIBLE_TIME);
1054 Sector::current()->play_music(HERRING_MUSIC);
1059 Player::kill(HurtMode mode)
1064 if(safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
1067 SoundManager::get()->play_sound(IDToSound(SND_HURT));
1069 physic.set_velocity_x(0);
1071 if (mode == SHRINK && size == BIG)
1073 if (got_power != NONE_POWER)
1075 safe_timer.start(TUX_SAFE_TIME);
1076 got_power = NONE_POWER;
1080 growing_timer.start(GROWING_TIME);
1081 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
1083 bbox.set_height(32);
1089 physic.enable_gravity(true);
1090 physic.set_acceleration(0, 0);
1091 physic.set_velocity(0, 700);
1092 --player_status.lives;
1093 dying = DYING_SQUISHED;
1094 dying_timer.start(3.0);
1098 /* Remove Tux's power ups */
1100 Player::remove_powerups()
1102 got_power = NONE_POWER;
1104 bbox.set_height(32);
1108 Player::move(const Vector& vector)
1110 bbox.set_pos(vector);
1114 Player::check_bounds(Camera* camera)
1116 /* Keep tux in bounds: */
1117 if (get_pos().x < 0)
1118 { // Lock Tux to the size of the level, so that he doesn't fall of
1120 bbox.set_pos(Vector(0, get_pos().y));
1123 /* Keep in-bounds, vertically: */
1124 if (get_pos().y > Sector::current()->solids->get_height() * 32)
1130 bool adjust = false;
1131 // can happen if back scrolling is disabled
1132 if(get_pos().x < camera->get_translation().x) {
1133 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
1136 if(get_pos().x >= camera->get_translation().x + screen->w - bbox.get_width())
1138 bbox.set_pos(Vector(
1139 camera->get_translation().x + screen->w - bbox.get_width(),
1148 if(collision_object_map(bbox)) {
1157 Player::bounce(BadGuy& badguy)
1159 //Make sure we stopped flapping
1161 falling_from_flap = false;
1164 physic.set_velocity_y(520);
1166 physic.set_velocity_y(200);