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_SPACE;
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, int loops)
97 head->set_action(action, loops);
99 body->set_action(action, loops);
101 arms->set_action(action, loops);
103 feet->set_action(action, loops);
107 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer,
108 Uint32 drawing_effect)
111 head->draw(context, pos, layer-1, drawing_effect);
113 body->draw(context, pos, layer-3, drawing_effect);
115 arms->draw(context, pos, layer, drawing_effect);
117 feet->draw(context, pos, layer-2, drawing_effect);
132 holding_something = false;
134 bbox.set_size(32, 32);
137 got_power = NONE_POWER;
146 fall_mode = ON_GROUND;
151 falling_from_flap = false;
152 enable_hover = false;
155 flapping_velocity = 0;
157 // temporary to help player's choosing a flapping
158 flapping_mode = MAREK_FLAP;
160 // Ricardo's flapping
163 on_ground_flag = false;
165 player_input_init(&input);
171 Player::key_event(SDLKey key, int state)
173 idle_timer.start(IDLE_TIME, true);
175 if(key == keymap.right)
180 else if(key == keymap.left)
185 else if(key == keymap.up)
190 /* Up key also opens activates stuff */
191 input.activate = state;
194 else if(key == keymap.down)
199 else if(key == keymap.power)
207 else if(key == keymap.jump)
219 Player::level_begin()
221 move(Vector(100, 170));
226 player_input_init(&input);
228 on_ground_flag = false;
236 return player_status;
240 Player::action(float elapsed_time)
242 if(dying && dying_timer.check()) {
247 if (input.fire == UP)
248 holding_something = false;
250 if(dying == DYING_NOT)
253 movement = physic.get_movement(elapsed_time);
256 // special exception for cases where we're stuck under tiles after
257 // being ducked. In this case we drift out
258 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
259 && collision_object_map(base))
261 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
262 previous_base = old_base = base;
265 /* Reset score multiplier (for multi-hits): */
266 if (!invincible_timer.started())
268 if(player_status.score_multiplier > player_status.max_score_multiplier)
270 player_status.max_score_multiplier = player_status.score_multiplier;
274 sprintf(str, _("New max combo: %d"), player_status.max_score_multiplier-1);
275 Sector::current()->add_floating_text(base, str);
277 player_status.score_multiplier = 1;
284 on_ground_flag = false;
290 return on_ground_flag;
294 Player::handle_horizontal_input()
296 float vx = physic.get_velocity_x();
297 float vy = physic.get_velocity_y();
298 float ax = physic.get_acceleration_x();
299 float ay = physic.get_acceleration_y();
302 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
306 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
312 if (input.fire == UP) {
313 ax = dirsign * WALK_ACCELERATION_X;
315 if(vx >= MAX_WALK_XM && dirsign > 0) {
318 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
323 ax = dirsign * RUN_ACCELERATION_X;
325 if(vx >= MAX_RUN_XM && dirsign > 0) {
328 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
334 // we can reach WALK_SPEED without any acceleration
335 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
336 vx = dirsign * WALK_SPEED;
339 // changing directions?
340 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0)))
343 if(fabs(vx)>SKID_XM && !skidding_timer.started())
345 skidding_timer.start(SKID_TIME);
346 SoundManager::get()->play_sound(IDToSound(SND_SKID));
347 // dust some partcles
348 Sector::current()->add_object(
350 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
352 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
353 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
364 // we get slower when not pressing any keys
366 if(fabs(vx) < WALK_SPEED) {
370 ax = WALK_ACCELERATION_X * 1.5;
372 ax = WALK_ACCELERATION_X * -1.5;
377 // if we're on ice slow down acceleration or deceleration
378 if (isice(base.x, base.y + base.height))
380 /* the acceleration/deceleration rate on ice is inversely proportional to
381 * the current velocity.
384 // increasing 1 will increase acceleration/deceleration rate
385 // decreasing 1 will decrease acceleration/deceleration rate
386 // must stay above zero, though
387 if (ax != 0) ax *= 1 / fabs(vx);
391 physic.set_velocity(vx, vy);
392 physic.set_acceleration(ax, ay);
396 Player::handle_vertical_input()
400 fall_mode = ON_GROUND;
401 last_ground_y = get_pos().y;
403 if(get_pos().y > last_ground_y)
405 else if(fall_mode == ON_GROUND)
409 if(on_ground()) { /* Make sure jumping is off. */
412 falling_from_flap = false;
413 if (flapping_timer.started()) {
414 flapping_timer.start(0);
417 physic.set_acceleration_y(0); //for flapping
421 if(input.jump == DOWN && can_jump && on_ground())
423 if(duck) { // only jump a little bit when in duck mode {
424 physic.set_velocity_y(300);
426 // jump higher if we are running
427 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
428 physic.set_velocity_y(580);
430 physic.set_velocity_y(520);
433 //bbox.move(Vector(0, -1));
438 flaps_nb = 0; // Ricardo's flapping
440 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
442 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
444 // Let go of jump key
445 else if(input.jump == UP)
447 if (!flapping && !duck && !falling_from_flap && !on_ground())
451 if (jumping && physic.get_velocity_y() > 0)
454 physic.set_velocity_y(0);
458 // temporary to help player's choosing a flapping
459 if(flapping_mode == RICARDO_FLAP)
461 // Flapping, Ricardo's version
462 // similar to SM3 Fox
463 if(input.jump == DOWN && input.old_jump == UP && can_flap &&
466 physic.set_velocity_y(350);
467 physic.set_velocity_x(physic.get_velocity_x() * 35);
471 else if(flapping_mode == MAREK_FLAP)
473 // Flapping, Marek's version
474 if (input.jump == DOWN && can_flap)
476 if (!flapping_timer.started())
478 flapping_timer.start(TUX_FLAPPING_TIME);
479 flapping_velocity = physic.get_velocity_x();
481 if (flapping_timer.check())
484 falling_from_flap = true;
488 if (!flapping_timer.check()) {
489 float cv = flapping_velocity * sqrt(
490 TUX_FLAPPING_TIME - flapping_timer.get_timegone()
491 / TUX_FLAPPING_TIME);
493 //Handle change of direction while flapping
494 if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
497 physic.set_velocity_x(cv);
498 physic.set_velocity_y(
499 flapping_timer.get_timegone()/.850);
503 else if(flapping_mode == RYAN_FLAP)
505 // Flapping, Ryan's version
506 if (input.jump == DOWN && can_flap)
508 if (!flapping_timer.started())
510 flapping_timer.start(TUX_FLAPPING_TIME);
512 if (flapping_timer.check())
515 falling_from_flap = true;
519 if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
520 && physic.get_velocity_y() < 0)
522 float gravity = Sector::current()->gravity;
524 float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
526 // XXX: magic numbers. should be a percent of gravity
527 // gravity is (by default) -0.1f
528 physic.set_acceleration_y(12 + 1*xr);
531 // To slow down x-vel when flapping (not working)
532 if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
534 if (physic.get_velocity_x() < 0)
535 physic.set_acceleration_x(1.0f);
536 else if (physic.get_velocity_x() > 0)
537 physic.set_acceleration_x(-1.0f);
544 physic.set_acceleration_y(0);
549 //(disabled by default, use cheat code "hover" to toggle on/off)
550 //TODO: needs some tweaking, especially when used together with double jump and jumping off badguys
551 if (enable_hover && input.jump == DOWN && !jumping && !butt_jump && physic.get_velocity_y() <= 0)
553 physic.set_velocity_y(-100);
557 /* In case the player has pressed Down while in a certain range of air,
558 enable butt jump action */
559 if (input.down == DOWN && !butt_jump && !duck)
560 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
564 /* When Down is not held anymore, disable butt jump */
565 if(butt_jump && input.down == UP)
569 if (butt_jump && on_ground() && size == BIG)
573 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
575 Sector::current()->add_smoke_cloud(
576 Vector(get_pos().x - 32, get_pos().y + 32));
581 // Break bricks beneath Tux
582 if(Sector::current()->trybreakbrick(
583 Vector(base.x + 1, base.y + base.height), false)
584 || Sector::current()->trybreakbrick(
585 Vector(base.x + base.width - 1, base.y + base.height), false))
587 physic.set_velocity_y(2);
593 // Kill nearby badguys
594 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
595 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
596 i != gameobjects.end();
599 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
602 // don't kill when badguys are already dying or in a certain mode
603 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
604 badguy->mode != BadGuy::BOMB_EXPLODE)
606 if (fabsf(base.x - badguy->base.x) < 96 &&
607 fabsf(base.y - badguy->base.y) < 64)
615 /** jumping is only allowed if we're about to touch ground soon and if the
616 * button has been up in between the last jump
620 if ( (issolid(get_pos().x + bbox.get_width() / 2,
621 get_pos().y + bbox.get_height() + 64) ||
622 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
623 issolid(get_pos().x + bbox.get_width() - 1,
624 get_pos().y + bbox.get_height() + 64))
627 && input.jump == DOWN
628 && input.old_jump == UP)
634 input.old_jump = input.jump;
638 Player::handle_input()
640 /* Handle horizontal movement: */
641 handle_horizontal_input();
644 if (on_ground() && input.jump == UP)
646 handle_vertical_input();
649 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER) {
650 if(Sector::current()->add_bullet(
651 get_pos() + Vector(0, bbox.get_height()/2),
652 physic.get_velocity_x(), dir))
653 shooting_timer.start(SHOOTING_TIME);
654 input.old_fire = DOWN;
658 if (input.down == DOWN && size == BIG && !duck
659 && physic.get_velocity_y() == 0 && on_ground())
662 bbox.move(Vector(0, 32));
665 else if(input.down == UP && size == BIG && duck)
667 // try if we can really unduck
668 bbox.move(Vector(0, -32));
673 // when unducking in air we need some space to do so
674 if(on_ground() || !collision_object_map(bbox)) {
677 // undo the ducking changes
678 bbox.move(Vector(0, 32));
686 Player::grow(bool animate)
693 bbox.move(Vector(0, -32));
696 growing_timer.start(GROWING_TIME);
700 Player::grabdistros()
705 Player::draw(DrawingContext& context)
707 TuxBodyParts* tux_body;
710 tux_body = small_tux;
711 else if (got_power == FIRE_POWER)
713 else if (got_power == ICE_POWER)
718 int layer = LAYER_OBJECTS + 10;
720 /* Set Tux sprite action */
721 if (duck && size == BIG)
724 tux_body->set_action("duck-left");
726 tux_body->set_action("duck-right");
728 else if (skidding_timer.started() && !skidding_timer.check())
731 tux_body->set_action("skid-left");
733 tux_body->set_action("skid-right");
735 else if (kick_timer.started() && !kick_timer.check())
738 tux_body->set_action("kick-left");
740 tux_body->set_action("kick-right");
742 else if (butt_jump && size == BIG)
745 tux_body->set_action("buttjump-left");
747 tux_body->set_action("buttjump-right");
749 else if (physic.get_velocity_y() != 0)
752 tux_body->set_action("jump-left");
754 tux_body->set_action("jump-right");
758 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
761 tux_body->set_action("stand-left");
763 tux_body->set_action("stand-right");
768 tux_body->set_action("walk-left");
770 tux_body->set_action("walk-right");
774 if(idle_timer.check())
779 tux_body->head->set_action("idle-left", 1);
781 tux_body->head->set_action("idle-right", 1);
786 // Tux is holding something
787 if ((holding_something && physic.get_velocity_y() == 0) ||
788 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
793 tux_body->arms->set_action("duck+grab-left");
795 tux_body->arms->set_action("duck+grab-right");
800 tux_body->arms->set_action("grab-left");
802 tux_body->arms->set_action("grab-right");
807 if (dying == DYING_SQUISHED) {
808 smalltux_gameover->draw(context, get_pos(), LAYER_FOREGROUNDTILES+1);
809 } else if(growing_timer.get_timeleft() > 0) {
813 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
814 int((growing_timer.get_timegone() *
815 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
817 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
818 int((growing_timer.get_timegone() *
819 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
824 context.draw_surface(growingtux_right[
825 int((growing_timer.get_timegone() *
826 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
828 context.draw_surface(growingtux_left[
829 int((growing_timer.get_timegone() *
830 GROWING_FRAMES) / GROWING_TIME)],
834 else if (safe_timer.started() && size_t(global_time*40)%2)
837 tux_body->draw(context, get_pos(), layer);
839 // Draw blinking star overlay
840 if (invincible_timer.started() &&
841 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
842 || size_t(global_time*20)%2)
845 if (size == SMALL || duck)
846 smalltux_star->draw(context, get_pos(), layer + 5);
848 bigtux_star->draw(context, get_pos(), layer + 5);
852 context.draw_filled_rect(get_pos(),
853 Vector(bbox.get_width(), bbox.get_height()),
854 Color(75,75,75, 150), LAYER_OBJECTS+20);
858 Player::collision(GameObject& other, const CollisionHit& hit)
864 if(other.get_flags() & FLAG_SOLID) {
865 if(hit.normal.y < 0) { // landed on floor?
866 if (physic.get_velocity_y() < 0)
867 physic.set_velocity_y(0);
868 on_ground_flag = true;
869 } else if(hit.normal.y > 0) { // bumped against the roof
870 physic.set_velocity_y(.1);
873 if(fabsf(hit.normal.x) > .9) { // hit on the side?
874 physic.set_velocity_x(0);
880 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
882 if(input.up == DOWN && input.old_up == UP)
883 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
890 Player::make_invincible()
892 SoundManager::get()->play_sound(IDToSound(SND_HERRING));
893 invincible_timer.start(TUX_INVINCIBLE_TIME);
894 Sector::current()->play_music(HERRING_MUSIC);
899 Player::kill(HurtMode mode)
904 if(safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
907 SoundManager::get()->play_sound(IDToSound(SND_HURT));
909 physic.set_velocity_x(0);
911 if (mode == SHRINK && size == BIG)
913 if (got_power != NONE_POWER)
915 safe_timer.start(TUX_SAFE_TIME);
916 got_power = NONE_POWER;
920 growing_timer.start(GROWING_TIME);
921 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
929 physic.enable_gravity(true);
930 physic.set_acceleration(0, 0);
931 physic.set_velocity(0, 700);
932 --player_status.lives;
933 dying = DYING_SQUISHED;
934 dying_timer.start(3.0);
938 /* Remove Tux's power ups */
940 Player::remove_powerups()
942 got_power = NONE_POWER;
948 Player::move(const Vector& vector)
950 bbox.set_pos(vector);
954 Player::check_bounds(Camera* camera)
956 /* Keep tux in bounds: */
958 { // Lock Tux to the size of the level, so that he doesn't fall of
960 bbox.set_pos(Vector(0, get_pos().y));
963 /* Keep in-bounds, vertically: */
964 if (get_pos().y > Sector::current()->solids->get_height() * 32)
971 // can happen if back scrolling is disabled
972 if(get_pos().x < camera->get_translation().x) {
973 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
976 if(get_pos().x >= camera->get_translation().x + screen->w - bbox.get_width())
979 camera->get_translation().x + screen->w - bbox.get_width(),
988 if(collision_object_map(bbox)) {
997 Player::bounce(BadGuy& )
999 //Make sure we stopped flapping
1001 falling_from_flap = false;
1004 physic.set_velocity_y(520);
1006 physic.set_velocity_y(200);