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"
31 #include "special/sprite.h"
33 #include "resources.h"
34 #include "video/screen.h"
35 #include "statistics.h"
37 #include "object/tilemap.h"
38 #include "object/camera.h"
39 #include "object/gameobjs.h"
40 #include "object/portable.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 TuxBodyParts* small_tux = 0;
63 TuxBodyParts* big_tux = 0;
64 TuxBodyParts* fire_tux = 0;
65 TuxBodyParts* ice_tux = 0;
69 PlayerKeymap::PlayerKeymap()
72 keymap.down = SDLK_DOWN;
73 keymap.left = SDLK_LEFT;
74 keymap.right = SDLK_RIGHT;
76 keymap.power = SDLK_LCTRL;
77 keymap.jump = SDLK_SPACE;
80 PlayerInputType::PlayerInputType()
86 PlayerInputType::reset()
101 TuxBodyParts::set_action(std::string action, int loops)
104 head->set_action(action, loops);
106 body->set_action(action, loops);
108 arms->set_action(action, loops);
110 feet->set_action(action, loops);
114 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer,
115 Uint32 drawing_effect)
118 head->draw(context, pos, layer-1, drawing_effect);
120 body->draw(context, pos, layer-3, drawing_effect);
122 arms->draw(context, pos, layer, drawing_effect);
124 feet->draw(context, pos, layer-2, drawing_effect);
127 Player::Player(PlayerStatus* _player_status)
128 : player_status(_player_status), grabbed_object(0)
130 smalltux_gameover = sprite_manager->create("smalltux-gameover");
131 smalltux_star = sprite_manager->create("smalltux-star");
132 bigtux_star = sprite_manager->create("bigtux-star");
138 delete smalltux_gameover;
139 delete smalltux_star;
147 bbox.set_size(31.8, 63.8);
149 bbox.set_size(31.8, 31.8);
158 fall_mode = ON_GROUND;
163 falling_from_flap = false;
164 enable_hover = false;
167 flapping_velocity = 0;
169 // temporary to help player's choosing a flapping
170 flapping_mode = NO_FLAP;
172 // Ricardo's flapping
175 on_ground_flag = false;
183 Player::key_event(SDLKey key, bool state)
185 idle_timer.start(IDLE_TIME, true);
187 if(key == keymap.right)
192 else if(key == keymap.left)
197 else if(key == keymap.up)
200 input.old_up = false;
202 /* Up key also opens activates stuff */
203 input.activate = state;
206 else if(key == keymap.down)
211 else if(key == keymap.power)
214 input.old_fire = false;
219 else if(key == keymap.jump)
222 input.old_jump = false;
231 Player::action(float elapsed_time)
233 if(dying && dying_timer.check()) {
238 if(input.fire == false && grabbed_object) {
240 // move the grabbed object a bit away from tux
241 Vector pos = get_pos() +
242 Vector(dir == LEFT ? -bbox.get_width() : bbox.get_width(),
243 bbox.get_height()*0.66666 - 32);
244 MovingObject* object = dynamic_cast<MovingObject*> (grabbed_object);
246 object->set_pos(pos);
249 std::cout << "Non MovingObjetc grabbed?!?\n";
257 movement = physic.get_movement(elapsed_time);
258 on_ground_flag = false;
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)) {
265 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
266 previous_base = old_base = base;
270 if(grabbed_object != 0) {
271 Vector pos = get_pos() +
272 Vector(dir == LEFT ? -16 : 16,
273 bbox.get_height()*0.66666 - 32);
274 grabbed_object->grab(*this, pos);
281 return on_ground_flag;
287 if(player_status->bonus == NO_BONUS)
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(!duck || physic.get_velocity_y() != 0) {
303 if(input.left && !input.right) {
307 } else if(!input.left && input.right) {
315 ax = dirsign * WALK_ACCELERATION_X;
317 if(vx >= MAX_WALK_XM && dirsign > 0) {
320 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
325 ax = dirsign * RUN_ACCELERATION_X;
327 if(vx >= MAX_RUN_XM && dirsign > 0) {
330 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
336 // we can reach WALK_SPEED without any acceleration
337 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
338 vx = dirsign * WALK_SPEED;
341 // changing directions?
342 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
344 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,
362 // we get slower when not pressing any keys
364 if(fabs(vx) < WALK_SPEED) {
368 ax = WALK_ACCELERATION_X * 1.5;
370 ax = WALK_ACCELERATION_X * -1.5;
375 // if we're on ice slow down acceleration or deceleration
376 if (isice(base.x, base.y + base.height))
378 /* the acceleration/deceleration rate on ice is inversely proportional to
379 * the current velocity.
382 // increasing 1 will increase acceleration/deceleration rate
383 // decreasing 1 will decrease acceleration/deceleration rate
384 // must stay above zero, though
385 if (ax != 0) ax *= 1 / fabs(vx);
389 // extend/shrink tux collision rectangle so that we fall through/walk over 1
391 if(fabsf(vx) > MAX_WALK_XM) {
394 bbox.set_width(31.8);
397 physic.set_velocity(vx, vy);
398 physic.set_acceleration(ax, ay);
402 Player::handle_vertical_input()
406 fall_mode = ON_GROUND;
407 last_ground_y = get_pos().y;
409 if(get_pos().y > last_ground_y)
411 else if(fall_mode == ON_GROUND)
415 if(on_ground()) { /* Make sure jumping is off. */
418 falling_from_flap = false;
419 if (flapping_timer.started()) {
420 flapping_timer.start(0);
423 physic.set_acceleration_y(0); //for flapping
427 if(input.jump && 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_BIGJUMP));
448 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
450 // Let go of jump key
453 if (!flapping && !duck && !falling_from_flap && !on_ground())
457 if (jumping && physic.get_velocity_y() > 0)
460 physic.set_velocity_y(0);
464 // temporary to help player's choosing a flapping
465 if(flapping_mode == RICARDO_FLAP)
467 // Flapping, Ricardo's version
468 // similar to SM3 Fox
469 if(input.jump && !input.old_jump && can_flap && flaps_nb < 3)
471 physic.set_velocity_y(350);
472 physic.set_velocity_x(physic.get_velocity_x() * 35);
476 else if(flapping_mode == MAREK_FLAP)
478 // Flapping, Marek's version
479 if (input.jump && can_flap)
481 if (!flapping_timer.started())
483 flapping_timer.start(TUX_FLAPPING_TIME);
484 flapping_velocity = physic.get_velocity_x();
486 if (flapping_timer.check())
489 falling_from_flap = true;
493 if (!flapping_timer.check()) {
494 float cv = flapping_velocity * sqrt(
495 TUX_FLAPPING_TIME - flapping_timer.get_timegone()
496 / TUX_FLAPPING_TIME);
498 //Handle change of direction while flapping
499 if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
502 physic.set_velocity_x(cv);
503 physic.set_velocity_y(
504 flapping_timer.get_timegone()/.850);
508 else if(flapping_mode == RYAN_FLAP)
510 // Flapping, Ryan's version
511 if (input.jump && can_flap)
513 if (!flapping_timer.started())
515 flapping_timer.start(TUX_FLAPPING_TIME);
517 if (flapping_timer.check())
520 falling_from_flap = true;
524 if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
525 && physic.get_velocity_y() < 0)
527 float gravity = Sector::current()->gravity;
529 float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
531 // XXX: magic numbers. should be a percent of gravity
532 // gravity is (by default) -0.1f
533 physic.set_acceleration_y(12 + 1*xr);
536 // To slow down x-vel when flapping (not working)
537 if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
539 if (physic.get_velocity_x() < 0)
540 physic.set_acceleration_x(1.0f);
541 else if (physic.get_velocity_x() > 0)
542 physic.set_acceleration_x(-1.0f);
549 physic.set_acceleration_y(0);
554 /* In case the player has pressed Down while in a certain range of air,
555 enable butt jump action */
556 if (input.down && !butt_jump && !duck)
557 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
561 /* When Down is not held anymore, disable butt jump */
562 if(butt_jump && !input.down)
566 if (butt_jump && on_ground() && is_big())
570 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
572 Sector::current()->add_smoke_cloud(
573 Vector(get_pos().x - 32, get_pos().y + 32));
578 // Break bricks beneath Tux
579 if(Sector::current()->trybreakbrick(
580 Vector(base.x + 1, base.y + base.height), false)
581 || Sector::current()->trybreakbrick(
582 Vector(base.x + base.width - 1, base.y + base.height), false))
584 physic.set_velocity_y(2);
590 // Kill nearby badguys
591 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
592 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
593 i != gameobjects.end();
596 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
599 // don't kill when badguys are already dying or in a certain mode
600 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
601 badguy->mode != BadGuy::BOMB_EXPLODE)
603 if (fabsf(base.x - badguy->base.x) < 96 &&
604 fabsf(base.y - badguy->base.y) < 64)
612 /** jumping is only allowed if we're about to touch ground soon and if the
613 * button has been up in between the last jump
617 if ( (issolid(get_pos().x + bbox.get_width() / 2,
618 get_pos().y + bbox.get_height() + 64) ||
619 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
620 issolid(get_pos().x + bbox.get_width() - 1,
621 get_pos().y + bbox.get_height() + 64))
624 && input.jump && !input.old_jump)
630 // FIXME: why the heck is this here and not somewhere where the keys are
632 input.old_jump = input.jump;
636 Player::handle_input()
638 /* Handle horizontal movement: */
639 handle_horizontal_input();
642 if (on_ground() && !input.jump)
644 handle_vertical_input();
647 if (input.fire && !input.old_fire && player_status->bonus == FIRE_BONUS) {
648 if(Sector::current()->add_bullet(
649 // get_pos() + Vector(0, bbox.get_height()/2),
650 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
651 : Vector(32, bbox.get_height()/2)),
652 physic.get_velocity_x(), dir))
653 shooting_timer.start(SHOOTING_TIME);
654 // FIXME: why the heck is this here
655 input.old_fire = false;
659 if (input.down && is_big() && !duck
660 && physic.get_velocity_y() == 0 && on_ground())
663 bbox.move(Vector(0, 32));
664 bbox.set_height(31.8);
666 else if(!input.down && is_big() && duck)
668 // try if we can really unduck
669 bbox.move(Vector(0, -32));
670 bbox.set_height(63.8);
674 // when unducking in air we need some space to do so
675 if(on_ground() || !collision_object_map(bbox)) {
678 // undo the ducking changes
679 bbox.move(Vector(0, 32));
680 bbox.set_height(31.8);
687 Player::set_bonus(BonusType type, bool animate)
689 if(player_status->bonus == type)
692 if(player_status->bonus == NO_BONUS) {
693 bbox.set_height(63.8);
694 bbox.move(Vector(0, -32));
696 growing_timer.start(GROWING_TIME);
699 player_status->bonus = type;
703 Player::draw(DrawingContext& context)
705 TuxBodyParts* tux_body;
707 if (player_status->bonus == GROWUP_BONUS)
709 else if (player_status->bonus == FIRE_BONUS)
711 else if (player_status->bonus == ICE_BONUS)
714 tux_body = small_tux;
716 int layer = LAYER_OBJECTS + 10;
718 /* Set Tux sprite action */
719 if (duck && is_big())
722 tux_body->set_action("duck-left");
724 tux_body->set_action("duck-right");
726 else if (skidding_timer.started() && !skidding_timer.check())
729 tux_body->set_action("skid-left");
731 tux_body->set_action("skid-right");
733 else if (kick_timer.started() && !kick_timer.check())
736 tux_body->set_action("kick-left");
738 tux_body->set_action("kick-right");
740 else if (butt_jump && is_big())
743 tux_body->set_action("buttjump-left");
745 tux_body->set_action("buttjump-right");
747 else if (physic.get_velocity_y() != 0)
750 tux_body->set_action("jump-left");
752 tux_body->set_action("jump-right");
756 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
759 tux_body->set_action("stand-left");
761 tux_body->set_action("stand-right");
766 tux_body->set_action("walk-left");
768 tux_body->set_action("walk-right");
772 if(idle_timer.check())
777 tux_body->head->set_action("idle-left", 1);
779 tux_body->head->set_action("idle-right", 1);
784 // Tux is holding something
785 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
786 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
791 tux_body->arms->set_action("duck+grab-left");
793 tux_body->arms->set_action("duck+grab-right");
798 tux_body->arms->set_action("grab-left");
800 tux_body->arms->set_action("grab-right");
806 smalltux_gameover->draw(context, get_pos(), layer);
807 } else if(growing_timer.get_timeleft() > 0) {
811 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
812 int((growing_timer.get_timegone() *
813 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
815 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
816 int((growing_timer.get_timegone() *
817 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
822 context.draw_surface(growingtux_right[
823 int((growing_timer.get_timegone() *
824 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
826 context.draw_surface(growingtux_left[
827 int((growing_timer.get_timegone() *
828 GROWING_FRAMES) / GROWING_TIME)],
832 else if (safe_timer.started() && size_t(global_time*40)%2)
835 tux_body->draw(context, get_pos(), layer);
837 // Draw blinking star overlay
838 if (invincible_timer.started() &&
839 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
840 || size_t(global_time*20)%2)
843 if (!is_big() || duck)
844 smalltux_star->draw(context, get_pos(), layer + 5);
846 bigtux_star->draw(context, get_pos(), layer + 5);
850 context.draw_filled_rect(get_pos(),
851 Vector(bbox.get_width(), bbox.get_height()),
852 Color(75,75,75, 150), LAYER_OBJECTS+20);
856 Player::collision(GameObject& other, const CollisionHit& hit)
858 Portable* portable = dynamic_cast<Portable*> (&other);
859 if(portable && grabbed_object == 0 && input.fire
860 && fabsf(hit.normal.x) > .9) {
861 grabbed_object = portable;
865 if(other.get_flags() & FLAG_SOLID) {
866 if(hit.normal.y < 0) { // landed on floor?
867 if (physic.get_velocity_y() < 0)
868 physic.set_velocity_y(0);
869 on_ground_flag = true;
870 } else if(hit.normal.y > 0) { // bumped against the roof
871 physic.set_velocity_y(.1);
874 if(fabsf(hit.normal.x) > .9) { // hit on the side?
875 physic.set_velocity_x(0);
881 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
883 if(input.up && !input.old_up)
884 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
891 Player::make_invincible()
893 SoundManager::get()->play_sound(IDToSound(SND_HERRING));
894 invincible_timer.start(TUX_INVINCIBLE_TIME);
895 Sector::current()->play_music(HERRING_MUSIC);
900 Player::kill(HurtMode mode)
905 if(safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
908 SoundManager::get()->play_sound(IDToSound(SND_HURT));
910 physic.set_velocity_x(0);
912 if (mode == SHRINK && is_big())
914 if (player_status->bonus == FIRE_BONUS
915 || player_status->bonus == ICE_BONUS)
917 safe_timer.start(TUX_SAFE_TIME);
918 player_status->bonus = GROWUP_BONUS;
922 growing_timer.start(GROWING_TIME);
923 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
924 bbox.set_height(31.8);
926 player_status->bonus = NO_BONUS;
931 physic.enable_gravity(true);
932 physic.set_acceleration(0, 0);
933 physic.set_velocity(0, 700);
934 player_status->lives -= 1;
936 dying_timer.start(3.0);
937 flags |= FLAG_NO_COLLDET;
942 Player::move(const Vector& vector)
944 bbox.set_pos(vector);
946 bbox.set_size(31.8, 63.8);
948 bbox.set_size(31.8, 31.8);
949 on_ground_flag = false;
957 Player::check_bounds(Camera* camera)
959 /* Keep tux in bounds: */
961 { // Lock Tux to the size of the level, so that he doesn't fall of
963 bbox.set_pos(Vector(0, get_pos().y));
966 /* Keep in-bounds, vertically: */
967 if (get_pos().y > Sector::current()->solids->get_height() * 32)
974 // can happen if back scrolling is disabled
975 if(get_pos().x < camera->get_translation().x) {
976 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
979 if(get_pos().x >= camera->get_translation().x + screen->w - bbox.get_width())
982 camera->get_translation().x + screen->w - bbox.get_width(),
991 if(collision_object_map(bbox)) {
1000 Player::bounce(BadGuy& )
1002 //Make sure we stopped flapping
1004 falling_from_flap = false;
1007 physic.set_velocity_y(520);
1009 physic.set_velocity_y(200);