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);
553 /* In case the player has pressed Down while in a certain range of air,
554 enable butt jump action */
555 if (input.down && !butt_jump && !duck)
556 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
559 /* When Down is not held anymore, disable butt jump */
560 if(butt_jump && !input.down)
565 if (butt_jump && on_ground() && is_big())
569 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
571 Sector::current()->add_smoke_cloud(
572 Vector(get_pos().x - 32, get_pos().y + 32));
576 // Break bricks beneath Tux
577 if(Sector::current()->trybreakbrick(
578 Vector(base.x + 1, base.y + base.height), false)
579 || Sector::current()->trybreakbrick(
580 Vector(base.x + base.width - 1, base.y + base.height), false))
582 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 && !input.old_jump)
626 // FIXME: why the heck is this here and not somewhere where the keys are
628 input.old_jump = input.jump;
632 Player::handle_input()
634 /* Handle horizontal movement: */
635 handle_horizontal_input();
638 if (on_ground() && !input.jump)
640 handle_vertical_input();
643 if (input.fire && !input.old_fire && player_status->bonus == FIRE_BONUS) {
644 if(Sector::current()->add_bullet(
645 // get_pos() + Vector(0, bbox.get_height()/2),
646 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
647 : Vector(32, bbox.get_height()/2)),
648 physic.get_velocity_x(), dir))
649 shooting_timer.start(SHOOTING_TIME);
650 // FIXME: why the heck is this here
651 input.old_fire = false;
655 if (input.down && is_big() && !duck
656 && physic.get_velocity_y() == 0 && on_ground())
659 bbox.move(Vector(0, 32));
660 bbox.set_height(31.8);
662 else if(!input.down && is_big() && duck)
664 // try if we can really unduck
665 bbox.move(Vector(0, -32));
666 bbox.set_height(63.8);
670 // when unducking in air we need some space to do so
671 if(on_ground() || !collision_object_map(bbox)) {
674 // undo the ducking changes
675 bbox.move(Vector(0, 32));
676 bbox.set_height(31.8);
683 Player::set_bonus(BonusType type, bool animate)
685 if(player_status->bonus == type)
688 if(player_status->bonus == NO_BONUS) {
689 bbox.set_height(63.8);
690 bbox.move(Vector(0, -32));
692 growing_timer.start(GROWING_TIME);
695 player_status->bonus = type;
699 Player::draw(DrawingContext& context)
701 TuxBodyParts* tux_body;
703 if (player_status->bonus == GROWUP_BONUS)
705 else if (player_status->bonus == FIRE_BONUS)
707 else if (player_status->bonus == ICE_BONUS)
710 tux_body = small_tux;
712 int layer = LAYER_OBJECTS + 10;
714 /* Set Tux sprite action */
715 if (duck && is_big())
718 tux_body->set_action("duck-left");
720 tux_body->set_action("duck-right");
722 else if (skidding_timer.started() && !skidding_timer.check())
725 tux_body->set_action("skid-left");
727 tux_body->set_action("skid-right");
729 else if (kick_timer.started() && !kick_timer.check())
732 tux_body->set_action("kick-left");
734 tux_body->set_action("kick-right");
736 else if (butt_jump && is_big())
739 tux_body->set_action("buttjump-left");
741 tux_body->set_action("buttjump-right");
743 else if (physic.get_velocity_y() != 0)
746 tux_body->set_action("jump-left");
748 tux_body->set_action("jump-right");
752 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
755 tux_body->set_action("stand-left");
757 tux_body->set_action("stand-right");
762 tux_body->set_action("walk-left");
764 tux_body->set_action("walk-right");
768 if(idle_timer.check())
773 tux_body->head->set_action("idle-left", 1);
775 tux_body->head->set_action("idle-right", 1);
780 // Tux is holding something
781 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
782 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
787 tux_body->arms->set_action("duck+grab-left");
789 tux_body->arms->set_action("duck+grab-right");
794 tux_body->arms->set_action("grab-left");
796 tux_body->arms->set_action("grab-right");
802 smalltux_gameover->draw(context, get_pos(), layer);
803 } else if(growing_timer.get_timeleft() > 0) {
807 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
808 int((growing_timer.get_timegone() *
809 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
811 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
812 int((growing_timer.get_timegone() *
813 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
818 context.draw_surface(growingtux_right[
819 int((growing_timer.get_timegone() *
820 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
822 context.draw_surface(growingtux_left[
823 int((growing_timer.get_timegone() *
824 GROWING_FRAMES) / GROWING_TIME)],
828 else if (safe_timer.started() && size_t(global_time*40)%2)
831 tux_body->draw(context, get_pos(), layer);
833 // Draw blinking star overlay
834 if (invincible_timer.started() &&
835 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
836 || size_t(global_time*20)%2)
839 if (!is_big() || duck)
840 smalltux_star->draw(context, get_pos(), layer + 5);
842 bigtux_star->draw(context, get_pos(), layer + 5);
846 context.draw_filled_rect(get_pos(),
847 Vector(bbox.get_width(), bbox.get_height()),
848 Color(75,75,75, 150), LAYER_OBJECTS+20);
852 Player::collision(GameObject& other, const CollisionHit& hit)
854 Portable* portable = dynamic_cast<Portable*> (&other);
855 if(portable && grabbed_object == 0 && input.fire
856 && fabsf(hit.normal.x) > .9) {
857 grabbed_object = portable;
861 if(other.get_flags() & FLAG_SOLID) {
862 if(hit.normal.y < 0) { // landed on floor?
863 if (physic.get_velocity_y() < 0)
864 physic.set_velocity_y(0);
865 on_ground_flag = true;
866 } else if(hit.normal.y > 0) { // bumped against the roof
867 physic.set_velocity_y(.1);
870 if(fabsf(hit.normal.x) > .9) { // hit on the side?
871 physic.set_velocity_x(0);
877 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
879 if(input.up && !input.old_up)
880 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
887 Player::make_invincible()
889 SoundManager::get()->play_sound(IDToSound(SND_HERRING));
890 invincible_timer.start(TUX_INVINCIBLE_TIME);
891 Sector::current()->play_music(HERRING_MUSIC);
896 Player::kill(HurtMode mode)
901 if(safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
904 SoundManager::get()->play_sound(IDToSound(SND_HURT));
906 physic.set_velocity_x(0);
908 if (mode == SHRINK && is_big())
910 if (player_status->bonus == FIRE_BONUS
911 || player_status->bonus == ICE_BONUS)
913 safe_timer.start(TUX_SAFE_TIME);
914 player_status->bonus = GROWUP_BONUS;
918 growing_timer.start(GROWING_TIME);
919 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
920 bbox.set_height(31.8);
922 player_status->bonus = NO_BONUS;
927 physic.enable_gravity(true);
928 physic.set_acceleration(0, 0);
929 physic.set_velocity(0, 700);
930 player_status->lives -= 1;
931 player_status->bonus = NO_BONUS;
933 dying_timer.start(3.0);
934 flags |= FLAG_NO_COLLDET;
939 Player::move(const Vector& vector)
941 bbox.set_pos(vector);
943 bbox.set_size(31.8, 63.8);
945 bbox.set_size(31.8, 31.8);
946 on_ground_flag = false;
948 last_ground_y = vector.y;
955 Player::check_bounds(Camera* camera)
957 /* Keep tux in bounds: */
959 { // Lock Tux to the size of the level, so that he doesn't fall of
961 bbox.set_pos(Vector(0, get_pos().y));
964 /* Keep in-bounds, vertically: */
965 if (get_pos().y > Sector::current()->solids->get_height() * 32)
972 // can happen if back scrolling is disabled
973 if(get_pos().x < camera->get_translation().x) {
974 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
977 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
980 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
989 if(collision_object_map(bbox)) {
998 Player::bounce(BadGuy& )
1000 //Make sure we stopped flapping
1002 falling_from_flap = false;
1005 physic.set_velocity_y(520);
1007 physic.set_velocity_y(200);