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"
28 #include "special/sprite_manager.h"
33 #include "special/sprite.h"
35 #include "resources.h"
36 #include "video/screen.h"
37 #include "statistics.h"
39 #include "object/tilemap.h"
40 #include "object/camera.h"
41 #include "object/gameobjs.h"
42 #include "object/portable.h"
43 #include "trigger/trigger_base.h"
45 static const int TILES_FOR_BUTTJUMP = 3;
46 static const float SHOOTING_TIME = .150;
47 /// time before idle animation starts
48 static const float IDLE_TIME = 2.5;
50 static const float WALK_ACCELERATION_X = 300;
51 static const float RUN_ACCELERATION_X = 400;
52 static const float SKID_XM = 200;
53 static const float SKID_TIME = .3;
54 static const float MAX_WALK_XM = 230;
55 static const float MAX_RUN_XM = 320;
56 static const float WALK_SPEED = 100;
59 Surface* growingtux_left[GROWING_FRAMES];
60 Surface* growingtux_right[GROWING_FRAMES];
62 Surface* tux_life = 0;
64 TuxBodyParts* small_tux = 0;
65 TuxBodyParts* big_tux = 0;
66 TuxBodyParts* fire_tux = 0;
67 TuxBodyParts* ice_tux = 0;
71 PlayerKeymap::PlayerKeymap()
74 keymap.down = SDLK_DOWN;
75 keymap.left = SDLK_LEFT;
76 keymap.right = SDLK_RIGHT;
78 keymap.power = SDLK_LCTRL;
79 keymap.jump = SDLK_SPACE;
82 void player_input_init(player_input_type* pplayer_input)
84 pplayer_input->up = UP;
85 pplayer_input->old_up = UP;
86 pplayer_input->down = UP;
87 pplayer_input->fire = UP;
88 pplayer_input->left = UP;
89 pplayer_input->old_fire = UP;
90 pplayer_input->right = UP;
91 pplayer_input->jump = UP;
92 pplayer_input->old_jump = UP;
93 pplayer_input->activate = UP;
97 TuxBodyParts::set_action(std::string action, int loops)
100 head->set_action(action, loops);
102 body->set_action(action, loops);
104 arms->set_action(action, loops);
106 feet->set_action(action, loops);
110 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer,
111 Uint32 drawing_effect)
114 head->draw(context, pos, layer-1, drawing_effect);
116 body->draw(context, pos, layer-3, drawing_effect);
118 arms->draw(context, pos, layer, drawing_effect);
120 feet->draw(context, pos, layer-2, drawing_effect);
126 smalltux_gameover = sprite_manager->create("smalltux-gameover");
127 smalltux_star = sprite_manager->create("smalltux-star");
128 bigtux_star = sprite_manager->create("bigtux-star");
134 delete smalltux_gameover;
135 delete smalltux_star;
142 bbox.set_size(31.8, 31.8);
145 got_power = NONE_POWER;
154 fall_mode = ON_GROUND;
159 falling_from_flap = false;
160 enable_hover = false;
163 flapping_velocity = 0;
165 // temporary to help player's choosing a flapping
166 flapping_mode = MAREK_FLAP;
168 // Ricardo's flapping
171 on_ground_flag = false;
174 player_input_init(&input);
180 Player::key_event(SDLKey key, int state)
182 idle_timer.start(IDLE_TIME, true);
184 if(key == keymap.right)
189 else if(key == keymap.left)
194 else if(key == keymap.up)
199 /* Up key also opens activates stuff */
200 input.activate = state;
203 else if(key == keymap.down)
208 else if(key == keymap.power)
216 else if(key == keymap.jump)
228 Player::level_begin()
230 move(Vector(100, 170));
235 player_input_init(&input);
237 on_ground_flag = false;
245 return player_status;
249 Player::action(float elapsed_time)
251 if(dying && dying_timer.check()) {
260 if(dying == DYING_NOT)
263 movement = physic.get_movement(elapsed_time);
264 on_ground_flag = false;
267 // special exception for cases where we're stuck under tiles after
268 // being ducked. In this case we drift out
269 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
270 && collision_object_map(base)) {
271 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
272 previous_base = old_base = base;
276 if(grabbed_object != 0) {
277 Vector pos = get_pos() +
278 Vector(dir == LEFT ? -16 : 16,
279 bbox.get_height()*0.66666 - 32);
280 grabbed_object->grab(*this, pos);
287 return on_ground_flag;
291 Player::handle_horizontal_input()
293 float vx = physic.get_velocity_x();
294 float vy = physic.get_velocity_y();
295 float ax = physic.get_acceleration_x();
296 float ay = physic.get_acceleration_y();
299 if(input.left == DOWN && input.right == UP && (!duck || physic.get_velocity_y() != 0)) {
303 } else if(input.left == UP && input.right == DOWN && (!duck || physic.get_velocity_y() != 0)) {
309 if (input.fire == UP) {
310 ax = dirsign * WALK_ACCELERATION_X;
312 if(vx >= MAX_WALK_XM && dirsign > 0) {
315 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
320 ax = dirsign * RUN_ACCELERATION_X;
322 if(vx >= MAX_RUN_XM && dirsign > 0) {
325 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
331 // we can reach WALK_SPEED without any acceleration
332 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
333 vx = dirsign * WALK_SPEED;
336 // changing directions?
337 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
339 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
340 skidding_timer.start(SKID_TIME);
341 SoundManager::get()->play_sound(IDToSound(SND_SKID));
342 // dust some partcles
343 Sector::current()->add_object(
345 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
347 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
348 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
357 // we get slower when not pressing any keys
359 if(fabs(vx) < WALK_SPEED) {
363 ax = WALK_ACCELERATION_X * 1.5;
365 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);
384 // extend/shrink tux collision rectangle so that we fall through/walk over 1
386 if(fabsf(vx) > MAX_WALK_XM) {
389 bbox.set_width(31.8);
392 physic.set_velocity(vx, vy);
393 physic.set_acceleration(ax, ay);
397 Player::handle_vertical_input()
401 fall_mode = ON_GROUND;
402 last_ground_y = get_pos().y;
404 if(get_pos().y > last_ground_y)
406 else if(fall_mode == ON_GROUND)
410 if(on_ground()) { /* Make sure jumping is off. */
413 falling_from_flap = false;
414 if (flapping_timer.started()) {
415 flapping_timer.start(0);
418 physic.set_acceleration_y(0); //for flapping
422 if(input.jump == DOWN && can_jump && on_ground())
424 if(duck) { // only jump a little bit when in duck mode {
425 physic.set_velocity_y(300);
427 // jump higher if we are running
428 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
429 physic.set_velocity_y(580);
431 physic.set_velocity_y(520);
434 //bbox.move(Vector(0, -1));
439 flaps_nb = 0; // Ricardo's flapping
441 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
443 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
445 // Let go of jump key
446 else if(input.jump == UP)
448 if (!flapping && !duck && !falling_from_flap && !on_ground())
452 if (jumping && physic.get_velocity_y() > 0)
455 physic.set_velocity_y(0);
459 // temporary to help player's choosing a flapping
460 if(flapping_mode == RICARDO_FLAP)
462 // Flapping, Ricardo's version
463 // similar to SM3 Fox
464 if(input.jump == DOWN && input.old_jump == UP && can_flap &&
467 physic.set_velocity_y(350);
468 physic.set_velocity_x(physic.get_velocity_x() * 35);
472 else if(flapping_mode == MAREK_FLAP)
474 // Flapping, Marek's version
475 if (input.jump == DOWN && can_flap)
477 if (!flapping_timer.started())
479 flapping_timer.start(TUX_FLAPPING_TIME);
480 flapping_velocity = physic.get_velocity_x();
482 if (flapping_timer.check())
485 falling_from_flap = true;
489 if (!flapping_timer.check()) {
490 float cv = flapping_velocity * sqrt(
491 TUX_FLAPPING_TIME - flapping_timer.get_timegone()
492 / TUX_FLAPPING_TIME);
494 //Handle change of direction while flapping
495 if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
498 physic.set_velocity_x(cv);
499 physic.set_velocity_y(
500 flapping_timer.get_timegone()/.850);
504 else if(flapping_mode == RYAN_FLAP)
506 // Flapping, Ryan's version
507 if (input.jump == DOWN && can_flap)
509 if (!flapping_timer.started())
511 flapping_timer.start(TUX_FLAPPING_TIME);
513 if (flapping_timer.check())
516 falling_from_flap = true;
520 if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
521 && physic.get_velocity_y() < 0)
523 float gravity = Sector::current()->gravity;
525 float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
527 // XXX: magic numbers. should be a percent of gravity
528 // gravity is (by default) -0.1f
529 physic.set_acceleration_y(12 + 1*xr);
532 // To slow down x-vel when flapping (not working)
533 if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
535 if (physic.get_velocity_x() < 0)
536 physic.set_acceleration_x(1.0f);
537 else if (physic.get_velocity_x() > 0)
538 physic.set_acceleration_x(-1.0f);
545 physic.set_acceleration_y(0);
550 /* In case the player has pressed Down while in a certain range of air,
551 enable butt jump action */
552 if (input.down == DOWN && !butt_jump && !duck)
553 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
557 /* When Down is not held anymore, disable butt jump */
558 if(butt_jump && input.down == UP)
562 if (butt_jump && on_ground() && size == BIG)
566 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
568 Sector::current()->add_smoke_cloud(
569 Vector(get_pos().x - 32, get_pos().y + 32));
574 // Break bricks beneath Tux
575 if(Sector::current()->trybreakbrick(
576 Vector(base.x + 1, base.y + base.height), false)
577 || Sector::current()->trybreakbrick(
578 Vector(base.x + base.width - 1, base.y + base.height), false))
580 physic.set_velocity_y(2);
586 // Kill nearby badguys
587 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
588 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
589 i != gameobjects.end();
592 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
595 // don't kill when badguys are already dying or in a certain mode
596 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
597 badguy->mode != BadGuy::BOMB_EXPLODE)
599 if (fabsf(base.x - badguy->base.x) < 96 &&
600 fabsf(base.y - badguy->base.y) < 64)
608 /** jumping is only allowed if we're about to touch ground soon and if the
609 * button has been up in between the last jump
613 if ( (issolid(get_pos().x + bbox.get_width() / 2,
614 get_pos().y + bbox.get_height() + 64) ||
615 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
616 issolid(get_pos().x + bbox.get_width() - 1,
617 get_pos().y + bbox.get_height() + 64))
620 && input.jump == DOWN
621 && input.old_jump == UP)
627 input.old_jump = input.jump;
631 Player::handle_input()
633 /* Handle horizontal movement: */
634 handle_horizontal_input();
637 if (on_ground() && input.jump == UP)
639 handle_vertical_input();
642 if (input.fire == DOWN && input.old_fire == UP && got_power != NONE_POWER) {
643 if(Sector::current()->add_bullet(
644 // get_pos() + Vector(0, bbox.get_height()/2),
645 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
646 : Vector(32, bbox.get_height()/2)),
647 physic.get_velocity_x(), dir))
648 shooting_timer.start(SHOOTING_TIME);
649 input.old_fire = DOWN;
653 if (input.down == DOWN && size == BIG && !duck
654 && physic.get_velocity_y() == 0 && on_ground())
657 bbox.move(Vector(0, 32));
658 bbox.set_height(31.8);
660 else if(input.down == UP && size == BIG && duck)
662 // try if we can really unduck
663 bbox.move(Vector(0, -32));
664 bbox.set_height(63.8);
668 // when unducking in air we need some space to do so
669 if(on_ground() || !collision_object_map(bbox)) {
672 // undo the ducking changes
673 bbox.move(Vector(0, 32));
674 bbox.set_height(31.8);
681 Player::grow(bool animate)
687 bbox.set_height(63.8);
688 bbox.move(Vector(0, -32));
691 growing_timer.start(GROWING_TIME);
695 Player::draw(DrawingContext& context)
697 TuxBodyParts* tux_body;
700 tux_body = small_tux;
701 else if (got_power == FIRE_POWER)
703 else if (got_power == ICE_POWER)
708 int layer = LAYER_OBJECTS + 10;
710 /* Set Tux sprite action */
711 if (duck && size == BIG)
714 tux_body->set_action("duck-left");
716 tux_body->set_action("duck-right");
718 else if (skidding_timer.started() && !skidding_timer.check())
721 tux_body->set_action("skid-left");
723 tux_body->set_action("skid-right");
725 else if (kick_timer.started() && !kick_timer.check())
728 tux_body->set_action("kick-left");
730 tux_body->set_action("kick-right");
732 else if (butt_jump && size == BIG)
735 tux_body->set_action("buttjump-left");
737 tux_body->set_action("buttjump-right");
739 else if (physic.get_velocity_y() != 0)
742 tux_body->set_action("jump-left");
744 tux_body->set_action("jump-right");
748 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
751 tux_body->set_action("stand-left");
753 tux_body->set_action("stand-right");
758 tux_body->set_action("walk-left");
760 tux_body->set_action("walk-right");
764 if(idle_timer.check())
769 tux_body->head->set_action("idle-left", 1);
771 tux_body->head->set_action("idle-right", 1);
776 // Tux is holding something
777 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
778 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
783 tux_body->arms->set_action("duck+grab-left");
785 tux_body->arms->set_action("duck+grab-right");
790 tux_body->arms->set_action("grab-left");
792 tux_body->arms->set_action("grab-right");
797 if (dying == DYING_SQUISHED) {
798 smalltux_gameover->draw(context, get_pos(), LAYER_FOREGROUNDTILES+1);
799 } else if(growing_timer.get_timeleft() > 0) {
803 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
804 int((growing_timer.get_timegone() *
805 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
807 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
808 int((growing_timer.get_timegone() *
809 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
814 context.draw_surface(growingtux_right[
815 int((growing_timer.get_timegone() *
816 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
818 context.draw_surface(growingtux_left[
819 int((growing_timer.get_timegone() *
820 GROWING_FRAMES) / GROWING_TIME)],
824 else if (safe_timer.started() && size_t(global_time*40)%2)
827 tux_body->draw(context, get_pos(), layer);
829 // Draw blinking star overlay
830 if (invincible_timer.started() &&
831 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
832 || size_t(global_time*20)%2)
835 if (size == SMALL || duck)
836 smalltux_star->draw(context, get_pos(), layer + 5);
838 bigtux_star->draw(context, get_pos(), layer + 5);
842 context.draw_filled_rect(get_pos(),
843 Vector(bbox.get_width(), bbox.get_height()),
844 Color(75,75,75, 150), LAYER_OBJECTS+20);
848 Player::collision(GameObject& other, const CollisionHit& hit)
850 Portable* portable = dynamic_cast<Portable*> (&other);
851 if(portable && grabbed_object == 0 && input.fire == DOWN
852 && fabsf(hit.normal.x) > .9) {
853 grabbed_object = portable;
857 if(other.get_flags() & FLAG_SOLID) {
858 if(hit.normal.y < 0) { // landed on floor?
859 if (physic.get_velocity_y() < 0)
860 physic.set_velocity_y(0);
861 on_ground_flag = true;
862 } else if(hit.normal.y > 0) { // bumped against the roof
863 physic.set_velocity_y(.1);
866 if(fabsf(hit.normal.x) > .9) { // hit on the side?
867 physic.set_velocity_x(0);
873 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
875 if(input.up == DOWN && input.old_up == UP)
876 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
883 Player::make_invincible()
885 SoundManager::get()->play_sound(IDToSound(SND_HERRING));
886 invincible_timer.start(TUX_INVINCIBLE_TIME);
887 Sector::current()->play_music(HERRING_MUSIC);
892 Player::kill(HurtMode mode)
897 if(safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
900 SoundManager::get()->play_sound(IDToSound(SND_HURT));
902 physic.set_velocity_x(0);
904 if (mode == SHRINK && size == BIG)
906 if (got_power != NONE_POWER)
908 safe_timer.start(TUX_SAFE_TIME);
909 got_power = NONE_POWER;
913 growing_timer.start(GROWING_TIME);
914 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
916 bbox.set_height(31.8);
922 physic.enable_gravity(true);
923 physic.set_acceleration(0, 0);
924 physic.set_velocity(0, 700);
925 --player_status.lives;
926 dying = DYING_SQUISHED;
927 dying_timer.start(3.0);
928 flags |= FLAG_NO_COLLDET;
932 /* Remove Tux's power ups */
934 Player::remove_powerups()
936 got_power = NONE_POWER;
938 bbox.set_height(31.8);
942 Player::move(const Vector& vector)
944 bbox.set_pos(vector);
948 Player::check_bounds(Camera* camera)
950 /* Keep tux in bounds: */
952 { // Lock Tux to the size of the level, so that he doesn't fall of
954 bbox.set_pos(Vector(0, get_pos().y));
957 /* Keep in-bounds, vertically: */
958 if (get_pos().y > Sector::current()->solids->get_height() * 32)
965 // can happen if back scrolling is disabled
966 if(get_pos().x < camera->get_translation().x) {
967 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
970 if(get_pos().x >= camera->get_translation().x + screen->w - bbox.get_width())
973 camera->get_translation().x + screen->w - bbox.get_width(),
982 if(collision_object_map(bbox)) {
991 Player::bounce(BadGuy& )
993 //Make sure we stopped flapping
995 falling_from_flap = false;
998 physic.set_velocity_y(520);
1000 physic.set_velocity_y(200);