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 static const int TILES_FOR_BUTTJUMP = 3;
44 static const float SHOOTING_TIME = .150;
45 /// time before idle animation starts
46 static const float IDLE_TIME = 2.5;
48 static const float WALK_ACCELERATION_X = 300;
49 static const float RUN_ACCELERATION_X = 400;
50 static const float SKID_XM = 200;
51 static const float SKID_TIME = .3;
52 static const float MAX_WALK_XM = 230;
53 static const float MAX_RUN_XM = 320;
54 static const float WALK_SPEED = 100;
57 Surface* growingtux_left[GROWING_FRAMES];
58 Surface* growingtux_right[GROWING_FRAMES];
60 Surface* tux_life = 0;
62 Sprite* smalltux_gameover = 0;
63 Sprite* smalltux_star = 0;
64 Sprite* bigtux_star = 0;
66 TuxBodyParts* small_tux = 0;
67 TuxBodyParts* big_tux = 0;
68 TuxBodyParts* fire_tux = 0;
69 TuxBodyParts* ice_tux = 0;
73 PlayerKeymap::PlayerKeymap()
76 keymap.down = SDLK_DOWN;
77 keymap.left = SDLK_LEFT;
78 keymap.right = SDLK_RIGHT;
80 keymap.power = SDLK_LCTRL;
81 keymap.jump = SDLK_SPACE;
84 void player_input_init(player_input_type* pplayer_input)
86 pplayer_input->up = UP;
87 pplayer_input->old_up = UP;
88 pplayer_input->down = UP;
89 pplayer_input->fire = UP;
90 pplayer_input->left = UP;
91 pplayer_input->old_fire = UP;
92 pplayer_input->right = UP;
93 pplayer_input->jump = UP;
94 pplayer_input->old_jump = UP;
95 pplayer_input->activate = UP;
99 TuxBodyParts::set_action(std::string action, int loops)
102 head->set_action(action, loops);
104 body->set_action(action, loops);
106 arms->set_action(action, loops);
108 feet->set_action(action, loops);
112 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer,
113 Uint32 drawing_effect)
116 head->draw(context, pos, layer-1, drawing_effect);
118 body->draw(context, pos, layer-3, drawing_effect);
120 arms->draw(context, pos, layer, drawing_effect);
122 feet->draw(context, pos, layer-2, drawing_effect);
137 holding_something = false;
139 bbox.set_size(31.8, 31.8);
142 got_power = NONE_POWER;
151 fall_mode = ON_GROUND;
156 falling_from_flap = false;
157 enable_hover = false;
160 flapping_velocity = 0;
162 // temporary to help player's choosing a flapping
163 flapping_mode = MAREK_FLAP;
165 // Ricardo's flapping
168 on_ground_flag = false;
170 player_input_init(&input);
176 Player::key_event(SDLKey key, int state)
178 idle_timer.start(IDLE_TIME, true);
180 if(key == keymap.right)
185 else if(key == keymap.left)
190 else if(key == keymap.up)
195 /* Up key also opens activates stuff */
196 input.activate = state;
199 else if(key == keymap.down)
204 else if(key == keymap.power)
212 else if(key == keymap.jump)
224 Player::level_begin()
226 move(Vector(100, 170));
231 player_input_init(&input);
233 on_ground_flag = false;
241 return player_status;
245 Player::action(float elapsed_time)
247 if(dying && dying_timer.check()) {
252 if (input.fire == UP)
253 holding_something = false;
255 if(dying == DYING_NOT)
258 movement = physic.get_movement(elapsed_time);
261 // special exception for cases where we're stuck under tiles after
262 // being ducked. In this case we drift out
263 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
264 && collision_object_map(base))
266 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
267 previous_base = old_base = base;
270 /* Reset score multiplier (for multi-hits): */
271 if (!invincible_timer.started())
273 if(player_status.score_multiplier > player_status.max_score_multiplier)
275 player_status.max_score_multiplier = player_status.score_multiplier;
279 sprintf(str, _("New max combo: %d"), player_status.max_score_multiplier-1);
280 Sector::current()->add_floating_text(base, str);
282 player_status.score_multiplier = 1;
289 on_ground_flag = false;
295 return on_ground_flag;
299 Player::handle_horizontal_input()
301 float vx = physic.get_velocity_x();
302 float vy = physic.get_velocity_y();
303 float ax = physic.get_acceleration_x();
304 float ay = physic.get_acceleration_y();
307 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
311 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
317 if (input.fire == UP) {
318 ax = dirsign * WALK_ACCELERATION_X;
320 if(vx >= MAX_WALK_XM && dirsign > 0) {
323 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
328 ax = dirsign * RUN_ACCELERATION_X;
330 if(vx >= MAX_RUN_XM && dirsign > 0) {
333 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
339 // we can reach WALK_SPEED without any acceleration
340 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
341 vx = dirsign * WALK_SPEED;
344 // changing directions?
345 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0)))
348 if(fabs(vx)>SKID_XM && !skidding_timer.started())
350 skidding_timer.start(SKID_TIME);
351 SoundManager::get()->play_sound(IDToSound(SND_SKID));
352 // dust some partcles
353 Sector::current()->add_object(
355 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
357 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
358 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
369 // we get slower when not pressing any keys
371 if(fabs(vx) < WALK_SPEED) {
375 ax = WALK_ACCELERATION_X * 1.5;
377 ax = WALK_ACCELERATION_X * -1.5;
382 // if we're on ice slow down acceleration or deceleration
383 if (isice(base.x, base.y + base.height))
385 /* the acceleration/deceleration rate on ice is inversely proportional to
386 * the current velocity.
389 // increasing 1 will increase acceleration/deceleration rate
390 // decreasing 1 will decrease acceleration/deceleration rate
391 // must stay above zero, though
392 if (ax != 0) ax *= 1 / fabs(vx);
396 // extend/shrink tux collision rectangle so that we fall through/walk over 1
398 if(fabsf(vx) > MAX_WALK_XM) {
401 bbox.set_width(31.8);
404 physic.set_velocity(vx, vy);
405 physic.set_acceleration(ax, ay);
409 Player::handle_vertical_input()
413 fall_mode = ON_GROUND;
414 last_ground_y = get_pos().y;
416 if(get_pos().y > last_ground_y)
418 else if(fall_mode == ON_GROUND)
422 if(on_ground()) { /* Make sure jumping is off. */
425 falling_from_flap = false;
426 if (flapping_timer.started()) {
427 flapping_timer.start(0);
430 physic.set_acceleration_y(0); //for flapping
434 if(input.jump == DOWN && can_jump && on_ground())
436 if(duck) { // only jump a little bit when in duck mode {
437 physic.set_velocity_y(300);
439 // jump higher if we are running
440 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
441 physic.set_velocity_y(580);
443 physic.set_velocity_y(520);
446 //bbox.move(Vector(0, -1));
451 flaps_nb = 0; // Ricardo's flapping
453 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
455 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
457 // Let go of jump key
458 else if(input.jump == UP)
460 if (!flapping && !duck && !falling_from_flap && !on_ground())
464 if (jumping && physic.get_velocity_y() > 0)
467 physic.set_velocity_y(0);
471 // temporary to help player's choosing a flapping
472 if(flapping_mode == RICARDO_FLAP)
474 // Flapping, Ricardo's version
475 // similar to SM3 Fox
476 if(input.jump == DOWN && input.old_jump == UP && can_flap &&
479 physic.set_velocity_y(350);
480 physic.set_velocity_x(physic.get_velocity_x() * 35);
484 else if(flapping_mode == MAREK_FLAP)
486 // Flapping, Marek's version
487 if (input.jump == DOWN && can_flap)
489 if (!flapping_timer.started())
491 flapping_timer.start(TUX_FLAPPING_TIME);
492 flapping_velocity = physic.get_velocity_x();
494 if (flapping_timer.check())
497 falling_from_flap = true;
501 if (!flapping_timer.check()) {
502 float cv = flapping_velocity * sqrt(
503 TUX_FLAPPING_TIME - flapping_timer.get_timegone()
504 / TUX_FLAPPING_TIME);
506 //Handle change of direction while flapping
507 if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
510 physic.set_velocity_x(cv);
511 physic.set_velocity_y(
512 flapping_timer.get_timegone()/.850);
516 else if(flapping_mode == RYAN_FLAP)
518 // Flapping, Ryan's version
519 if (input.jump == DOWN && can_flap)
521 if (!flapping_timer.started())
523 flapping_timer.start(TUX_FLAPPING_TIME);
525 if (flapping_timer.check())
528 falling_from_flap = true;
532 if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
533 && physic.get_velocity_y() < 0)
535 float gravity = Sector::current()->gravity;
537 float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
539 // XXX: magic numbers. should be a percent of gravity
540 // gravity is (by default) -0.1f
541 physic.set_acceleration_y(12 + 1*xr);
544 // To slow down x-vel when flapping (not working)
545 if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
547 if (physic.get_velocity_x() < 0)
548 physic.set_acceleration_x(1.0f);
549 else if (physic.get_velocity_x() > 0)
550 physic.set_acceleration_x(-1.0f);
557 physic.set_acceleration_y(0);
562 //(disabled by default, use cheat code "hover" to toggle on/off)
563 //TODO: needs some tweaking, especially when used together with double jump and jumping off badguys
564 if (enable_hover && input.jump == DOWN && !jumping && !butt_jump && physic.get_velocity_y() <= 0)
566 physic.set_velocity_y(-100);
570 /* In case the player has pressed Down while in a certain range of air,
571 enable butt jump action */
572 if (input.down == DOWN && !butt_jump && !duck)
573 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
577 /* When Down is not held anymore, disable butt jump */
578 if(butt_jump && input.down == UP)
582 if (butt_jump && on_ground() && size == BIG)
586 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
588 Sector::current()->add_smoke_cloud(
589 Vector(get_pos().x - 32, get_pos().y + 32));
594 // Break bricks beneath Tux
595 if(Sector::current()->trybreakbrick(
596 Vector(base.x + 1, base.y + base.height), false)
597 || Sector::current()->trybreakbrick(
598 Vector(base.x + base.width - 1, base.y + base.height), false))
600 physic.set_velocity_y(2);
606 // Kill nearby badguys
607 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
608 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
609 i != gameobjects.end();
612 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
615 // don't kill when badguys are already dying or in a certain mode
616 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
617 badguy->mode != BadGuy::BOMB_EXPLODE)
619 if (fabsf(base.x - badguy->base.x) < 96 &&
620 fabsf(base.y - badguy->base.y) < 64)
628 /** jumping is only allowed if we're about to touch ground soon and if the
629 * button has been up in between the last jump
633 if ( (issolid(get_pos().x + bbox.get_width() / 2,
634 get_pos().y + bbox.get_height() + 64) ||
635 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
636 issolid(get_pos().x + bbox.get_width() - 1,
637 get_pos().y + bbox.get_height() + 64))
640 && input.jump == DOWN
641 && input.old_jump == UP)
647 input.old_jump = input.jump;
651 Player::handle_input()
653 /* Handle horizontal movement: */
654 handle_horizontal_input();
657 if (on_ground() && input.jump == UP)
659 handle_vertical_input();
662 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER) {
663 if(Sector::current()->add_bullet(
664 get_pos() + Vector(0, bbox.get_height()/2),
665 physic.get_velocity_x(), dir))
666 shooting_timer.start(SHOOTING_TIME);
667 input.old_fire = DOWN;
671 if (input.down == DOWN && size == BIG && !duck
672 && physic.get_velocity_y() == 0 && on_ground())
675 bbox.move(Vector(0, 32));
676 bbox.set_height(31.8);
678 else if(input.down == UP && size == BIG && duck)
680 // try if we can really unduck
681 bbox.move(Vector(0, -32));
682 bbox.set_height(63.8);
686 // when unducking in air we need some space to do so
687 if(on_ground() || !collision_object_map(bbox)) {
690 // undo the ducking changes
691 bbox.move(Vector(0, 32));
692 bbox.set_height(31.8);
699 Player::grow(bool animate)
705 bbox.set_height(63.8);
706 bbox.move(Vector(0, -32));
709 growing_timer.start(GROWING_TIME);
713 Player::grabdistros()
718 Player::draw(DrawingContext& context)
720 TuxBodyParts* tux_body;
723 tux_body = small_tux;
724 else if (got_power == FIRE_POWER)
726 else if (got_power == ICE_POWER)
731 int layer = LAYER_OBJECTS + 10;
733 /* Set Tux sprite action */
734 if (duck && size == BIG)
737 tux_body->set_action("duck-left");
739 tux_body->set_action("duck-right");
741 else if (skidding_timer.started() && !skidding_timer.check())
744 tux_body->set_action("skid-left");
746 tux_body->set_action("skid-right");
748 else if (kick_timer.started() && !kick_timer.check())
751 tux_body->set_action("kick-left");
753 tux_body->set_action("kick-right");
755 else if (butt_jump && size == BIG)
758 tux_body->set_action("buttjump-left");
760 tux_body->set_action("buttjump-right");
762 else if (physic.get_velocity_y() != 0)
765 tux_body->set_action("jump-left");
767 tux_body->set_action("jump-right");
771 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
774 tux_body->set_action("stand-left");
776 tux_body->set_action("stand-right");
781 tux_body->set_action("walk-left");
783 tux_body->set_action("walk-right");
787 if(idle_timer.check())
792 tux_body->head->set_action("idle-left", 1);
794 tux_body->head->set_action("idle-right", 1);
799 // Tux is holding something
800 if ((holding_something && physic.get_velocity_y() == 0) ||
801 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
806 tux_body->arms->set_action("duck+grab-left");
808 tux_body->arms->set_action("duck+grab-right");
813 tux_body->arms->set_action("grab-left");
815 tux_body->arms->set_action("grab-right");
820 if (dying == DYING_SQUISHED) {
821 smalltux_gameover->draw(context, get_pos(), LAYER_FOREGROUNDTILES+1);
822 } else if(growing_timer.get_timeleft() > 0) {
826 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
827 int((growing_timer.get_timegone() *
828 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
830 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
831 int((growing_timer.get_timegone() *
832 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
837 context.draw_surface(growingtux_right[
838 int((growing_timer.get_timegone() *
839 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
841 context.draw_surface(growingtux_left[
842 int((growing_timer.get_timegone() *
843 GROWING_FRAMES) / GROWING_TIME)],
847 else if (safe_timer.started() && size_t(global_time*40)%2)
850 tux_body->draw(context, get_pos(), layer);
852 // Draw blinking star overlay
853 if (invincible_timer.started() &&
854 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
855 || size_t(global_time*20)%2)
858 if (size == SMALL || duck)
859 smalltux_star->draw(context, get_pos(), layer + 5);
861 bigtux_star->draw(context, get_pos(), layer + 5);
865 context.draw_filled_rect(get_pos(),
866 Vector(bbox.get_width(), bbox.get_height()),
867 Color(75,75,75, 150), LAYER_OBJECTS+20);
871 Player::collision(GameObject& other, const CollisionHit& hit)
873 if(other.get_flags() & FLAG_SOLID) {
874 if(hit.normal.y < 0) { // landed on floor?
875 if (physic.get_velocity_y() < 0)
876 physic.set_velocity_y(0);
877 on_ground_flag = true;
878 } else if(hit.normal.y > 0) { // bumped against the roof
879 physic.set_velocity_y(.1);
882 if(fabsf(hit.normal.x) > .9) { // hit on the side?
883 physic.set_velocity_x(0);
889 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
891 if(input.up == DOWN && input.old_up == UP)
892 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
899 Player::make_invincible()
901 SoundManager::get()->play_sound(IDToSound(SND_HERRING));
902 invincible_timer.start(TUX_INVINCIBLE_TIME);
903 Sector::current()->play_music(HERRING_MUSIC);
908 Player::kill(HurtMode mode)
913 if(safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
916 SoundManager::get()->play_sound(IDToSound(SND_HURT));
918 physic.set_velocity_x(0);
920 if (mode == SHRINK && size == BIG)
922 if (got_power != NONE_POWER)
924 safe_timer.start(TUX_SAFE_TIME);
925 got_power = NONE_POWER;
929 growing_timer.start(GROWING_TIME);
930 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
932 bbox.set_height(31.8);
938 physic.enable_gravity(true);
939 physic.set_acceleration(0, 0);
940 physic.set_velocity(0, 700);
941 --player_status.lives;
942 dying = DYING_SQUISHED;
943 dying_timer.start(3.0);
944 flags |= FLAG_NO_COLLDET;
948 /* Remove Tux's power ups */
950 Player::remove_powerups()
952 got_power = NONE_POWER;
954 bbox.set_height(31.8);
958 Player::move(const Vector& vector)
960 bbox.set_pos(vector);
964 Player::check_bounds(Camera* camera)
966 /* Keep tux in bounds: */
968 { // Lock Tux to the size of the level, so that he doesn't fall of
970 bbox.set_pos(Vector(0, get_pos().y));
973 /* Keep in-bounds, vertically: */
974 if (get_pos().y > Sector::current()->solids->get_height() * 32)
981 // can happen if back scrolling is disabled
982 if(get_pos().x < camera->get_translation().x) {
983 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
986 if(get_pos().x >= camera->get_translation().x + screen->w - bbox.get_width())
989 camera->get_translation().x + screen->w - bbox.get_width(),
998 if(collision_object_map(bbox)) {
1007 Player::bounce(BadGuy& )
1009 //Make sure we stopped flapping
1011 falling_from_flap = false;
1014 physic.set_velocity_y(520);
1016 physic.set_velocity_y(200);