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)
267 movement = physic.get_movement(elapsed_time);
268 on_ground_flag = false;
271 // special exception for cases where we're stuck under tiles after
272 // being ducked. In this case we drift out
273 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
274 && collision_object_map(base)) {
275 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
276 previous_base = old_base = base;
280 if(grabbed_object != 0) {
281 Vector pos = get_pos() +
282 Vector(dir == LEFT ? -16 : 16,
283 bbox.get_height()*0.66666 - 32);
284 grabbed_object->grab(*this, pos);
291 return on_ground_flag;
295 Player::handle_horizontal_input()
297 float vx = physic.get_velocity_x();
298 float vy = physic.get_velocity_y();
299 float ax = physic.get_acceleration_x();
300 float ay = physic.get_acceleration_y();
303 if(!duck || physic.get_velocity_y() != 0) {
304 if(input.left && !input.right) {
308 } else if(!input.left && input.right) {
316 ax = dirsign * WALK_ACCELERATION_X;
318 if(vx >= MAX_WALK_XM && dirsign > 0) {
321 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
326 ax = dirsign * RUN_ACCELERATION_X;
328 if(vx >= MAX_RUN_XM && dirsign > 0) {
331 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
337 // we can reach WALK_SPEED without any acceleration
338 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
339 vx = dirsign * WALK_SPEED;
342 // changing directions?
343 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
345 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
346 skidding_timer.start(SKID_TIME);
347 SoundManager::get()->play_sound(IDToSound(SND_SKID));
348 // dust some partcles
349 Sector::current()->add_object(
351 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
353 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
354 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
363 // we get slower when not pressing any keys
365 if(fabs(vx) < WALK_SPEED) {
369 ax = WALK_ACCELERATION_X * 1.5;
371 ax = WALK_ACCELERATION_X * -1.5;
376 // if we're on ice slow down acceleration or deceleration
377 if (isice(base.x, base.y + base.height))
379 /* the acceleration/deceleration rate on ice is inversely proportional to
380 * the current velocity.
383 // increasing 1 will increase acceleration/deceleration rate
384 // decreasing 1 will decrease acceleration/deceleration rate
385 // must stay above zero, though
386 if (ax != 0) ax *= 1 / fabs(vx);
390 // extend/shrink tux collision rectangle so that we fall through/walk over 1
392 if(fabsf(vx) > MAX_WALK_XM) {
395 bbox.set_width(31.8);
398 physic.set_velocity(vx, vy);
399 physic.set_acceleration(ax, ay);
403 Player::handle_vertical_input()
407 fall_mode = ON_GROUND;
408 last_ground_y = get_pos().y;
410 if(get_pos().y > last_ground_y)
412 else if(fall_mode == ON_GROUND)
416 if(on_ground()) { /* Make sure jumping is off. */
419 falling_from_flap = false;
420 if (flapping_timer.started()) {
421 flapping_timer.start(0);
424 physic.set_acceleration_y(0); //for flapping
428 if(input.jump && can_jump && on_ground())
430 if(duck) { // only jump a little bit when in duck mode {
431 physic.set_velocity_y(300);
433 // jump higher if we are running
434 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
435 physic.set_velocity_y(580);
437 physic.set_velocity_y(520);
440 //bbox.move(Vector(0, -1));
445 flaps_nb = 0; // Ricardo's flapping
447 SoundManager::get()->play_sound(IDToSound(SND_JUMP));
449 SoundManager::get()->play_sound(IDToSound(SND_BIGJUMP));
451 // Let go of jump key
454 if (!flapping && !duck && !falling_from_flap && !on_ground())
458 if (jumping && physic.get_velocity_y() > 0)
461 physic.set_velocity_y(0);
465 // temporary to help player's choosing a flapping
466 if(flapping_mode == RICARDO_FLAP)
468 // Flapping, Ricardo's version
469 // similar to SM3 Fox
470 if(input.jump && !input.old_jump && can_flap && flaps_nb < 3)
472 physic.set_velocity_y(350);
473 physic.set_velocity_x(physic.get_velocity_x() * 35);
477 else if(flapping_mode == MAREK_FLAP)
479 // Flapping, Marek's version
480 if (input.jump && can_flap)
482 if (!flapping_timer.started())
484 flapping_timer.start(TUX_FLAPPING_TIME);
485 flapping_velocity = physic.get_velocity_x();
487 if (flapping_timer.check())
490 falling_from_flap = true;
494 if (!flapping_timer.check()) {
495 float cv = flapping_velocity * sqrt(
496 TUX_FLAPPING_TIME - flapping_timer.get_timegone()
497 / TUX_FLAPPING_TIME);
499 //Handle change of direction while flapping
500 if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
503 physic.set_velocity_x(cv);
504 physic.set_velocity_y(
505 flapping_timer.get_timegone()/.850);
509 else if(flapping_mode == RYAN_FLAP)
511 // Flapping, Ryan's version
512 if (input.jump && can_flap)
514 if (!flapping_timer.started())
516 flapping_timer.start(TUX_FLAPPING_TIME);
518 if (flapping_timer.check())
521 falling_from_flap = true;
525 if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
526 && physic.get_velocity_y() < 0)
528 float gravity = Sector::current()->gravity;
530 float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
532 // XXX: magic numbers. should be a percent of gravity
533 // gravity is (by default) -0.1f
534 physic.set_acceleration_y(12 + 1*xr);
537 // To slow down x-vel when flapping (not working)
538 if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
540 if (physic.get_velocity_x() < 0)
541 physic.set_acceleration_x(1.0f);
542 else if (physic.get_velocity_x() > 0)
543 physic.set_acceleration_x(-1.0f);
550 physic.set_acceleration_y(0);
555 /* In case the player has pressed Down while in a certain range of air,
556 enable butt jump action */
557 if (input.down && !butt_jump && !duck)
558 if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
562 /* When Down is not held anymore, disable butt jump */
563 if(butt_jump && !input.down)
567 if (butt_jump && on_ground() && size == BIG)
571 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
573 Sector::current()->add_smoke_cloud(
574 Vector(get_pos().x - 32, get_pos().y + 32));
579 // Break bricks beneath Tux
580 if(Sector::current()->trybreakbrick(
581 Vector(base.x + 1, base.y + base.height), false)
582 || Sector::current()->trybreakbrick(
583 Vector(base.x + base.width - 1, base.y + base.height), false))
585 physic.set_velocity_y(2);
591 // Kill nearby badguys
592 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
593 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
594 i != gameobjects.end();
597 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
600 // don't kill when badguys are already dying or in a certain mode
601 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
602 badguy->mode != BadGuy::BOMB_EXPLODE)
604 if (fabsf(base.x - badguy->base.x) < 96 &&
605 fabsf(base.y - badguy->base.y) < 64)
613 /** jumping is only allowed if we're about to touch ground soon and if the
614 * button has been up in between the last jump
618 if ( (issolid(get_pos().x + bbox.get_width() / 2,
619 get_pos().y + bbox.get_height() + 64) ||
620 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
621 issolid(get_pos().x + bbox.get_width() - 1,
622 get_pos().y + bbox.get_height() + 64))
625 && input.jump && !input.old_jump)
631 // FIXME: why the heck is this here and not somewhere where the keys are
633 input.old_jump = input.jump;
637 Player::handle_input()
639 /* Handle horizontal movement: */
640 handle_horizontal_input();
643 if (on_ground() && !input.jump)
645 handle_vertical_input();
648 if (input.fire && !input.old_fire && got_power != NONE_POWER) {
649 if(Sector::current()->add_bullet(
650 // get_pos() + Vector(0, bbox.get_height()/2),
651 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
652 : Vector(32, bbox.get_height()/2)),
653 physic.get_velocity_x(), dir))
654 shooting_timer.start(SHOOTING_TIME);
655 // FIXME: why the heck is this here
656 input.old_fire = false;
660 if (input.down && size == BIG && !duck
661 && physic.get_velocity_y() == 0 && on_ground())
664 bbox.move(Vector(0, 32));
665 bbox.set_height(31.8);
667 else if(!input.down && size == BIG && duck)
669 // try if we can really unduck
670 bbox.move(Vector(0, -32));
671 bbox.set_height(63.8);
675 // when unducking in air we need some space to do so
676 if(on_ground() || !collision_object_map(bbox)) {
679 // undo the ducking changes
680 bbox.move(Vector(0, 32));
681 bbox.set_height(31.8);
688 Player::grow(bool animate)
694 bbox.set_height(63.8);
695 bbox.move(Vector(0, -32));
698 growing_timer.start(GROWING_TIME);
702 Player::draw(DrawingContext& context)
704 TuxBodyParts* tux_body;
707 tux_body = small_tux;
708 else if (got_power == FIRE_POWER)
710 else if (got_power == ICE_POWER)
715 int layer = LAYER_OBJECTS + 10;
717 /* Set Tux sprite action */
718 if (duck && size == BIG)
721 tux_body->set_action("duck-left");
723 tux_body->set_action("duck-right");
725 else if (skidding_timer.started() && !skidding_timer.check())
728 tux_body->set_action("skid-left");
730 tux_body->set_action("skid-right");
732 else if (kick_timer.started() && !kick_timer.check())
735 tux_body->set_action("kick-left");
737 tux_body->set_action("kick-right");
739 else if (butt_jump && size == BIG)
742 tux_body->set_action("buttjump-left");
744 tux_body->set_action("buttjump-right");
746 else if (physic.get_velocity_y() != 0)
749 tux_body->set_action("jump-left");
751 tux_body->set_action("jump-right");
755 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
758 tux_body->set_action("stand-left");
760 tux_body->set_action("stand-right");
765 tux_body->set_action("walk-left");
767 tux_body->set_action("walk-right");
771 if(idle_timer.check())
776 tux_body->head->set_action("idle-left", 1);
778 tux_body->head->set_action("idle-right", 1);
783 // Tux is holding something
784 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
785 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
790 tux_body->arms->set_action("duck+grab-left");
792 tux_body->arms->set_action("duck+grab-right");
797 tux_body->arms->set_action("grab-left");
799 tux_body->arms->set_action("grab-right");
805 smalltux_gameover->draw(context, get_pos(), layer);
806 } else if(growing_timer.get_timeleft() > 0) {
810 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
811 int((growing_timer.get_timegone() *
812 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
814 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
815 int((growing_timer.get_timegone() *
816 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
821 context.draw_surface(growingtux_right[
822 int((growing_timer.get_timegone() *
823 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
825 context.draw_surface(growingtux_left[
826 int((growing_timer.get_timegone() *
827 GROWING_FRAMES) / GROWING_TIME)],
831 else if (safe_timer.started() && size_t(global_time*40)%2)
834 tux_body->draw(context, get_pos(), layer);
836 // Draw blinking star overlay
837 if (invincible_timer.started() &&
838 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
839 || size_t(global_time*20)%2)
842 if (size == SMALL || duck)
843 smalltux_star->draw(context, get_pos(), layer + 5);
845 bigtux_star->draw(context, get_pos(), layer + 5);
849 context.draw_filled_rect(get_pos(),
850 Vector(bbox.get_width(), bbox.get_height()),
851 Color(75,75,75, 150), LAYER_OBJECTS+20);
855 Player::collision(GameObject& other, const CollisionHit& hit)
857 Portable* portable = dynamic_cast<Portable*> (&other);
858 if(portable && grabbed_object == 0 && input.fire
859 && fabsf(hit.normal.x) > .9) {
860 grabbed_object = portable;
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 && !input.old_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);
923 bbox.set_height(31.8);
929 physic.enable_gravity(true);
930 physic.set_acceleration(0, 0);
931 physic.set_velocity(0, 700);
932 --player_status.lives;
934 dying_timer.start(3.0);
935 flags |= FLAG_NO_COLLDET;
939 /* Remove Tux's power ups */
941 Player::remove_powerups()
943 got_power = NONE_POWER;
945 bbox.set_height(31.8);
949 Player::move(const Vector& vector)
951 bbox.set_pos(vector);
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->w - bbox.get_width())
980 camera->get_translation().x + screen->w - 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);