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 "player_status.h"
32 #include "special/sprite.h"
34 #include "resources.h"
35 #include "video/screen.h"
36 #include "statistics.h"
38 #include "object/tilemap.h"
39 #include "object/camera.h"
40 #include "object/gameobjs.h"
41 #include "object/portable.h"
42 #include "trigger/trigger_base.h"
44 static const int TILES_FOR_BUTTJUMP = 3;
45 static const float SHOOTING_TIME = .150;
46 /// time before idle animation starts
47 static const float IDLE_TIME = 2.5;
49 static const float WALK_ACCELERATION_X = 300;
50 static const float RUN_ACCELERATION_X = 400;
51 static const float SKID_XM = 200;
52 static const float SKID_TIME = .3;
53 static const float MAX_WALK_XM = 230;
54 static const float MAX_RUN_XM = 320;
55 static const float WALK_SPEED = 100;
58 Surface* growingtux_left[GROWING_FRAMES];
59 Surface* growingtux_right[GROWING_FRAMES];
61 Surface* tux_life = 0;
63 TuxBodyParts* small_tux = 0;
64 TuxBodyParts* big_tux = 0;
65 TuxBodyParts* fire_tux = 0;
66 TuxBodyParts* ice_tux = 0;
70 PlayerKeymap::PlayerKeymap()
73 keymap.down = SDLK_DOWN;
74 keymap.left = SDLK_LEFT;
75 keymap.right = SDLK_RIGHT;
77 keymap.power = SDLK_LCTRL;
78 keymap.jump = SDLK_SPACE;
81 PlayerInputType::PlayerInputType()
87 PlayerInputType::reset()
102 TuxBodyParts::set_action(std::string action, int loops)
105 head->set_action(action, loops);
107 body->set_action(action, loops);
109 arms->set_action(action, loops);
111 feet->set_action(action, loops);
115 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer,
116 Uint32 drawing_effect)
119 head->draw(context, pos, layer-1, drawing_effect);
121 body->draw(context, pos, layer-3, drawing_effect);
123 arms->draw(context, pos, layer, drawing_effect);
125 feet->draw(context, pos, layer-2, drawing_effect);
131 smalltux_gameover = sprite_manager->create("smalltux-gameover");
132 smalltux_star = sprite_manager->create("smalltux-star");
133 bigtux_star = sprite_manager->create("bigtux-star");
139 delete smalltux_gameover;
140 delete smalltux_star;
147 bbox.set_size(31.8, 31.8);
150 got_power = NONE_POWER;
159 fall_mode = ON_GROUND;
164 falling_from_flap = false;
165 enable_hover = false;
168 flapping_velocity = 0;
170 // temporary to help player's choosing a flapping
171 flapping_mode = MAREK_FLAP;
173 // Ricardo's flapping
176 on_ground_flag = false;
184 Player::key_event(SDLKey key, bool state)
186 idle_timer.start(IDLE_TIME, true);
188 if(key == keymap.right)
193 else if(key == keymap.left)
198 else if(key == keymap.up)
201 input.old_up = false;
203 /* Up key also opens activates stuff */
204 input.activate = state;
207 else if(key == keymap.down)
212 else if(key == keymap.power)
215 input.old_fire = false;
220 else if(key == keymap.jump)
223 input.old_jump = false;
232 Player::level_begin()
234 move(Vector(100, 170));
241 on_ground_flag = false;
249 return player_status;
253 Player::action(float elapsed_time)
255 if(dying && dying_timer.check()) {
260 if(input.fire == false && grabbed_object) {
262 // move the grabbed object a bit away from tux
263 Vector pos = get_pos() +
264 Vector(dir == LEFT ? -bbox.get_width() : bbox.get_width(),
265 bbox.get_height()*0.66666 - 32);
266 MovingObject* object = dynamic_cast<MovingObject*> (grabbed_object);
268 object->set_pos(pos);
271 std::cout << "Non MovingObjetc grabbed?!?\n";
279 movement = physic.get_movement(elapsed_time);
280 on_ground_flag = false;
283 // special exception for cases where we're stuck under tiles after
284 // being ducked. In this case we drift out
285 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
286 && collision_object_map(base)) {
287 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
288 previous_base = old_base = base;
292 if(grabbed_object != 0) {
293 Vector pos = get_pos() +
294 Vector(dir == LEFT ? -16 : 16,
295 bbox.get_height()*0.66666 - 32);
296 grabbed_object->grab(*this, pos);
303 return on_ground_flag;
307 Player::handle_horizontal_input()
309 float vx = physic.get_velocity_x();
310 float vy = physic.get_velocity_y();
311 float ax = physic.get_acceleration_x();
312 float ay = physic.get_acceleration_y();
315 if(!duck || physic.get_velocity_y() != 0) {
316 if(input.left && !input.right) {
320 } else if(!input.left && input.right) {
328 ax = dirsign * WALK_ACCELERATION_X;
330 if(vx >= MAX_WALK_XM && dirsign > 0) {
333 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
338 ax = dirsign * RUN_ACCELERATION_X;
340 if(vx >= MAX_RUN_XM && dirsign > 0) {
343 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
349 // we can reach WALK_SPEED without any acceleration
350 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
351 vx = dirsign * WALK_SPEED;
354 // changing directions?
355 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
357 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
358 skidding_timer.start(SKID_TIME);
359 SoundManager::get()->play_sound(IDToSound(SND_SKID));
360 // dust some partcles
361 Sector::current()->add_object(
363 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
365 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
366 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
375 // we get slower when not pressing any keys
377 if(fabs(vx) < WALK_SPEED) {
381 ax = WALK_ACCELERATION_X * 1.5;
383 ax = WALK_ACCELERATION_X * -1.5;
388 // if we're on ice slow down acceleration or deceleration
389 if (isice(base.x, base.y + base.height))
391 /* the acceleration/deceleration rate on ice is inversely proportional to
392 * the current velocity.
395 // increasing 1 will increase acceleration/deceleration rate
396 // decreasing 1 will decrease acceleration/deceleration rate
397 // must stay above zero, though
398 if (ax != 0) ax *= 1 / fabs(vx);
402 // extend/shrink tux collision rectangle so that we fall through/walk over 1
404 if(fabsf(vx) > MAX_WALK_XM) {
407 bbox.set_width(31.8);
410 physic.set_velocity(vx, vy);
411 physic.set_acceleration(ax, ay);
415 Player::handle_vertical_input()
419 fall_mode = ON_GROUND;
420 last_ground_y = get_pos().y;
422 if(get_pos().y > last_ground_y)
424 else if(fall_mode == ON_GROUND)
428 if(on_ground()) { /* Make sure jumping is off. */
431 falling_from_flap = false;
432 if (flapping_timer.started()) {
433 flapping_timer.start(0);
436 physic.set_acceleration_y(0); //for flapping
440 if(input.jump && can_jump && on_ground())
442 if(duck) { // only jump a little bit when in duck mode {
443 physic.set_velocity_y(300);
445 // jump higher if we are running
446 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
447 physic.set_velocity_y(580);
449 physic.set_velocity_y(520);
452 //bbox.move(Vector(0, -1));
457 flaps_nb = 0; // Ricardo's flapping
459 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
461 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
463 // Let go of jump key
466 if (!flapping && !duck && !falling_from_flap && !on_ground())
470 if (jumping && physic.get_velocity_y() > 0)
473 physic.set_velocity_y(0);
477 // temporary to help player's choosing a flapping
478 if(flapping_mode == RICARDO_FLAP)
480 // Flapping, Ricardo's version
481 // similar to SM3 Fox
482 if(input.jump && !input.old_jump && can_flap && flaps_nb < 3)
484 physic.set_velocity_y(350);
485 physic.set_velocity_x(physic.get_velocity_x() * 35);
489 else if(flapping_mode == MAREK_FLAP)
491 // Flapping, Marek's version
492 if (input.jump && can_flap)
494 if (!flapping_timer.started())
496 flapping_timer.start(TUX_FLAPPING_TIME);
497 flapping_velocity = physic.get_velocity_x();
499 if (flapping_timer.check())
502 falling_from_flap = true;
506 if (!flapping_timer.check()) {
507 float cv = flapping_velocity * sqrt(
508 TUX_FLAPPING_TIME - flapping_timer.get_timegone()
509 / TUX_FLAPPING_TIME);
511 //Handle change of direction while flapping
512 if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
515 physic.set_velocity_x(cv);
516 physic.set_velocity_y(
517 flapping_timer.get_timegone()/.850);
521 else if(flapping_mode == RYAN_FLAP)
523 // Flapping, Ryan's version
524 if (input.jump && can_flap)
526 if (!flapping_timer.started())
528 flapping_timer.start(TUX_FLAPPING_TIME);
530 if (flapping_timer.check())
533 falling_from_flap = true;
537 if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
538 && physic.get_velocity_y() < 0)
540 float gravity = Sector::current()->gravity;
542 float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
544 // XXX: magic numbers. should be a percent of gravity
545 // gravity is (by default) -0.1f
546 physic.set_acceleration_y(12 + 1*xr);
549 // To slow down x-vel when flapping (not working)
550 if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
552 if (physic.get_velocity_x() < 0)
553 physic.set_acceleration_x(1.0f);
554 else if (physic.get_velocity_x() > 0)
555 physic.set_acceleration_x(-1.0f);
562 physic.set_acceleration_y(0);
567 /* In case the player has pressed Down while in a certain range of air,
568 enable butt jump action */
569 if (input.down && !butt_jump && !duck)
570 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
574 /* When Down is not held anymore, disable butt jump */
575 if(butt_jump && !input.down)
579 if (butt_jump && on_ground() && size == BIG)
583 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
585 Sector::current()->add_smoke_cloud(
586 Vector(get_pos().x - 32, get_pos().y + 32));
591 // Break bricks beneath Tux
592 if(Sector::current()->trybreakbrick(
593 Vector(base.x + 1, base.y + base.height), false)
594 || Sector::current()->trybreakbrick(
595 Vector(base.x + base.width - 1, base.y + base.height), false))
597 physic.set_velocity_y(2);
603 // Kill nearby badguys
604 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
605 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
606 i != gameobjects.end();
609 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
612 // don't kill when badguys are already dying or in a certain mode
613 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
614 badguy->mode != BadGuy::BOMB_EXPLODE)
616 if (fabsf(base.x - badguy->base.x) < 96 &&
617 fabsf(base.y - badguy->base.y) < 64)
625 /** jumping is only allowed if we're about to touch ground soon and if the
626 * button has been up in between the last jump
630 if ( (issolid(get_pos().x + bbox.get_width() / 2,
631 get_pos().y + bbox.get_height() + 64) ||
632 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
633 issolid(get_pos().x + bbox.get_width() - 1,
634 get_pos().y + bbox.get_height() + 64))
637 && input.jump && !input.old_jump)
643 // FIXME: why the heck is this here and not somewhere where the keys are
645 input.old_jump = input.jump;
649 Player::handle_input()
651 /* Handle horizontal movement: */
652 handle_horizontal_input();
655 if (on_ground() && !input.jump)
657 handle_vertical_input();
660 if (input.fire && !input.old_fire && got_power != NONE_POWER) {
661 if(Sector::current()->add_bullet(
662 // get_pos() + Vector(0, bbox.get_height()/2),
663 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
664 : Vector(32, bbox.get_height()/2)),
665 physic.get_velocity_x(), dir))
666 shooting_timer.start(SHOOTING_TIME);
667 // FIXME: why the heck is this here
668 input.old_fire = false;
672 if (input.down && size == BIG && !duck
673 && physic.get_velocity_y() == 0 && on_ground())
676 bbox.move(Vector(0, 32));
677 bbox.set_height(31.8);
679 else if(!input.down && size == BIG && duck)
681 // try if we can really unduck
682 bbox.move(Vector(0, -32));
683 bbox.set_height(63.8);
687 // when unducking in air we need some space to do so
688 if(on_ground() || !collision_object_map(bbox)) {
691 // undo the ducking changes
692 bbox.move(Vector(0, 32));
693 bbox.set_height(31.8);
700 Player::grow(bool animate)
706 bbox.set_height(63.8);
707 bbox.move(Vector(0, -32));
710 growing_timer.start(GROWING_TIME);
714 Player::draw(DrawingContext& context)
716 TuxBodyParts* tux_body;
719 tux_body = small_tux;
720 else if (got_power == FIRE_POWER)
722 else if (got_power == ICE_POWER)
727 int layer = LAYER_OBJECTS + 10;
729 /* Set Tux sprite action */
730 if (duck && size == BIG)
733 tux_body->set_action("duck-left");
735 tux_body->set_action("duck-right");
737 else if (skidding_timer.started() && !skidding_timer.check())
740 tux_body->set_action("skid-left");
742 tux_body->set_action("skid-right");
744 else if (kick_timer.started() && !kick_timer.check())
747 tux_body->set_action("kick-left");
749 tux_body->set_action("kick-right");
751 else if (butt_jump && size == BIG)
754 tux_body->set_action("buttjump-left");
756 tux_body->set_action("buttjump-right");
758 else if (physic.get_velocity_y() != 0)
761 tux_body->set_action("jump-left");
763 tux_body->set_action("jump-right");
767 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
770 tux_body->set_action("stand-left");
772 tux_body->set_action("stand-right");
777 tux_body->set_action("walk-left");
779 tux_body->set_action("walk-right");
783 if(idle_timer.check())
788 tux_body->head->set_action("idle-left", 1);
790 tux_body->head->set_action("idle-right", 1);
795 // Tux is holding something
796 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
797 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
802 tux_body->arms->set_action("duck+grab-left");
804 tux_body->arms->set_action("duck+grab-right");
809 tux_body->arms->set_action("grab-left");
811 tux_body->arms->set_action("grab-right");
817 smalltux_gameover->draw(context, get_pos(), layer);
818 } else if(growing_timer.get_timeleft() > 0) {
822 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
823 int((growing_timer.get_timegone() *
824 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
826 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
827 int((growing_timer.get_timegone() *
828 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
833 context.draw_surface(growingtux_right[
834 int((growing_timer.get_timegone() *
835 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
837 context.draw_surface(growingtux_left[
838 int((growing_timer.get_timegone() *
839 GROWING_FRAMES) / GROWING_TIME)],
843 else if (safe_timer.started() && size_t(global_time*40)%2)
846 tux_body->draw(context, get_pos(), layer);
848 // Draw blinking star overlay
849 if (invincible_timer.started() &&
850 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
851 || size_t(global_time*20)%2)
854 if (size == SMALL || duck)
855 smalltux_star->draw(context, get_pos(), layer + 5);
857 bigtux_star->draw(context, get_pos(), layer + 5);
861 context.draw_filled_rect(get_pos(),
862 Vector(bbox.get_width(), bbox.get_height()),
863 Color(75,75,75, 150), LAYER_OBJECTS+20);
867 Player::collision(GameObject& other, const CollisionHit& hit)
869 Portable* portable = dynamic_cast<Portable*> (&other);
870 if(portable && grabbed_object == 0 && input.fire
871 && fabsf(hit.normal.x) > .9) {
872 grabbed_object = portable;
876 if(other.get_flags() & FLAG_SOLID) {
877 if(hit.normal.y < 0) { // landed on floor?
878 if (physic.get_velocity_y() < 0)
879 physic.set_velocity_y(0);
880 on_ground_flag = true;
881 } else if(hit.normal.y > 0) { // bumped against the roof
882 physic.set_velocity_y(.1);
885 if(fabsf(hit.normal.x) > .9) { // hit on the side?
886 physic.set_velocity_x(0);
892 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
894 if(input.up && !input.old_up)
895 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
902 Player::make_invincible()
904 SoundManager::get()->play_sound(IDToSound(SND_HERRING));
905 invincible_timer.start(TUX_INVINCIBLE_TIME);
906 Sector::current()->play_music(HERRING_MUSIC);
911 Player::kill(HurtMode mode)
916 if(safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
919 SoundManager::get()->play_sound(IDToSound(SND_HURT));
921 physic.set_velocity_x(0);
923 if (mode == SHRINK && size == BIG)
925 if (got_power != NONE_POWER)
927 safe_timer.start(TUX_SAFE_TIME);
928 got_power = NONE_POWER;
932 growing_timer.start(GROWING_TIME);
933 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
935 bbox.set_height(31.8);
941 physic.enable_gravity(true);
942 physic.set_acceleration(0, 0);
943 physic.set_velocity(0, 700);
944 --player_status.lives;
946 dying_timer.start(3.0);
947 flags |= FLAG_NO_COLLDET;
951 /* Remove Tux's power ups */
953 Player::remove_powerups()
955 got_power = NONE_POWER;
957 bbox.set_height(31.8);
961 Player::move(const Vector& vector)
963 bbox.set_pos(vector);
967 Player::check_bounds(Camera* camera)
969 /* Keep tux in bounds: */
971 { // Lock Tux to the size of the level, so that he doesn't fall of
973 bbox.set_pos(Vector(0, get_pos().y));
976 /* Keep in-bounds, vertically: */
977 if (get_pos().y > Sector::current()->solids->get_height() * 32)
984 // can happen if back scrolling is disabled
985 if(get_pos().x < camera->get_translation().x) {
986 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
989 if(get_pos().x >= camera->get_translation().x + screen->w - bbox.get_width())
992 camera->get_translation().x + screen->w - bbox.get_width(),
1001 if(collision_object_map(bbox)) {
1010 Player::bounce(BadGuy& )
1012 //Make sure we stopped flapping
1014 falling_from_flap = false;
1017 physic.set_velocity_y(520);
1019 physic.set_velocity_y(200);