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 "gettext.hpp"
27 #include "sprite/sprite_manager.hpp"
28 #include "audio/sound_manager.hpp"
31 #include "sprite/sprite.hpp"
33 #include "resources.hpp"
34 #include "video/screen.hpp"
35 #include "statistics.hpp"
36 #include "game_session.hpp"
37 #include "object/tilemap.hpp"
38 #include "object/camera.hpp"
39 #include "object/gameobjs.hpp"
40 #include "object/portable.hpp"
41 #include "trigger/trigger_base.hpp"
42 #include "control/joystickkeyboardcontroller.hpp"
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;
70 TuxBodyParts::set_action(std::string action, int loops)
73 head->set_action(action, loops);
75 body->set_action(action, loops);
77 arms->set_action(action, loops);
79 feet->set_action(action, loops);
83 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
86 head->draw(context, pos, layer-1);
88 body->draw(context, pos, layer-3);
90 arms->draw(context, pos, layer);
92 feet->draw(context, pos, layer-2);
95 Player::Player(PlayerStatus* _player_status)
96 : player_status(_player_status), grabbed_object(0)
98 controller = main_controller;
99 smalltux_gameover = sprite_manager->create("smalltux-gameover");
100 smalltux_star = sprite_manager->create("smalltux-star");
101 bigtux_star = sprite_manager->create("bigtux-star");
107 delete smalltux_gameover;
108 delete smalltux_star;
116 bbox.set_size(31.8, 63.8);
118 bbox.set_size(31.8, 31.8);
127 fall_mode = ON_GROUND;
132 falling_from_flap = false;
133 enable_hover = false;
137 flapping_velocity = 0;
139 // temporary to help player's choosing a flapping
140 flapping_mode = NO_FLAP;
142 // Ricardo's flapping
145 on_ground_flag = false;
152 Player::set_controller(Controller* controller)
154 this->controller = controller;
158 Player::update(float elapsed_time)
160 if(dying && dying_timer.check()) {
165 if(!controller->hold(Controller::ACTION) && grabbed_object) {
167 // move the grabbed object a bit away from tux
168 Vector pos = get_pos() +
169 Vector(dir == LEFT ? -bbox.get_width() : bbox.get_width(),
170 bbox.get_height()*0.66666 - 32);
171 MovingObject* object = dynamic_cast<MovingObject*> (grabbed_object);
173 object->set_pos(pos);
176 std::cout << "Non MovingObjetc grabbed?!?\n";
181 if(!dying && !deactivated)
184 movement = physic.get_movement(elapsed_time);
185 on_ground_flag = false;
188 // special exception for cases where we're stuck under tiles after
189 // being ducked. In this case we drift out
190 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
191 && collision_object_map(base)) {
192 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
193 previous_base = old_base = base;
197 if(grabbed_object != 0) {
198 Vector pos = get_pos() +
199 Vector(dir == LEFT ? -16 : 16,
200 bbox.get_height()*0.66666 - 32);
201 grabbed_object->grab(*this, pos);
208 return on_ground_flag;
214 if(player_status->bonus == NO_BONUS)
221 Player::handle_horizontal_input()
223 float vx = physic.get_velocity_x();
224 float vy = physic.get_velocity_y();
225 float ax = physic.get_acceleration_x();
226 float ay = physic.get_acceleration_y();
229 if(!duck || physic.get_velocity_y() != 0) {
230 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
234 } else if(!controller->hold(Controller::LEFT)
235 && controller->hold(Controller::RIGHT)) {
242 if (!controller->hold(Controller::ACTION)) {
243 ax = dirsign * WALK_ACCELERATION_X;
245 if(vx >= MAX_WALK_XM && dirsign > 0) {
248 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
253 ax = dirsign * RUN_ACCELERATION_X;
255 if(vx >= MAX_RUN_XM && dirsign > 0) {
258 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
264 // we can reach WALK_SPEED without any acceleration
265 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
266 vx = dirsign * WALK_SPEED;
269 // changing directions?
270 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
272 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
273 skidding_timer.start(SKID_TIME);
274 sound_manager->play("sounds/skid.wav");
275 // dust some particles
276 Sector::current()->add_object(
278 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
280 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
281 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
290 // we get slower when not pressing any keys
292 if(fabs(vx) < WALK_SPEED) {
296 ax = WALK_ACCELERATION_X * 1.5;
298 ax = WALK_ACCELERATION_X * -1.5;
303 // if we're on ice slow down acceleration or deceleration
304 if (isice(base.x, base.y + base.height))
306 /* the acceleration/deceleration rate on ice is inversely proportional to
307 * the current velocity.
310 // increasing 1 will increase acceleration/deceleration rate
311 // decreasing 1 will decrease acceleration/deceleration rate
312 // must stay above zero, though
313 if (ax != 0) ax *= 1 / fabs(vx);
317 // extend/shrink tux collision rectangle so that we fall through/walk over 1
319 if(fabsf(vx) > MAX_WALK_XM) {
322 bbox.set_width(31.8);
325 physic.set_velocity(vx, vy);
326 physic.set_acceleration(ax, ay);
330 Player::handle_vertical_input()
334 fall_mode = ON_GROUND;
335 last_ground_y = get_pos().y;
337 if(get_pos().y > last_ground_y)
339 else if(fall_mode == ON_GROUND)
343 if(on_ground()) { /* Make sure jumping is off. */
346 falling_from_flap = false;
347 if (flapping_timer.started()) {
348 flapping_timer.stop();
351 physic.set_acceleration_y(0); //for flapping
355 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
356 if (duck) // only jump a little bit when in duck mode
357 physic.set_velocity_y(300);
358 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
359 physic.set_velocity_y(580);
361 physic.set_velocity_y(520);
363 //bbox.move(Vector(0, -1));
368 flaps_nb = 0; // Ricardo's flapping
370 sound_manager->play("sounds/bigjump.wav");
372 sound_manager->play("sounds/jump.wav");
373 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
374 if (!flapping && !duck && !falling_from_flap && !on_ground()) {
377 if (jumping && physic.get_velocity_y() > 0) {
379 physic.set_velocity_y(0);
383 // temporary to help players choosing a flapping
384 if(flapping_mode == RICARDO_FLAP) {
385 // Flapping, Ricardo's version
386 // similar to SM3 Fox
387 if(controller->pressed(Controller::JUMP) && can_flap && flaps_nb < 3) {
388 physic.set_velocity_y(350);
389 physic.set_velocity_x(physic.get_velocity_x() * 35);
392 } else if(flapping_mode == MAREK_FLAP) {
394 // Flapping, Marek and Ondra's version
395 if (controller->hold(Controller::JUMP) && can_flap)
397 if (flapping_timer.check())
401 falling_from_flap = true;
403 else if (!flapping_timer.started())
405 flapping_timer.start(TUX_FLAPPING_TIME);
406 flapping_velocity = physic.get_velocity_x();
412 float cv = flapping_velocity * sqrt(
413 TUX_FLAPPING_TIME - flapping_timer.get_timegone()
414 / TUX_FLAPPING_TIME);
416 //Handle change of direction while flapping
417 if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
421 if (controller->hold(Controller::LEFT)) {
422 cv = -TUX_FLAPPING_LEAST_X;
424 else if (controller->hold(Controller::RIGHT)) {
425 cv = TUX_FLAPPING_LEAST_X;
428 physic.set_velocity_x(cv);
429 physic.set_velocity_y(flapping_timer.get_timegone()
430 * TUX_FLAPPING_STRENGTH);
431 //std::cout << "Timegone: " << flapping_timer.get_timegone() << ", Y velocity: " << physic.get_velocity_y() << "\n";
435 } else if(flapping_mode == RYAN_FLAP) {
436 // Flapping, Ryan's version
437 if (controller->hold(Controller::JUMP) && can_flap)
439 if (!flapping_timer.started())
441 flapping_timer.start(TUX_FLAPPING_TIME);
443 if (flapping_timer.check())
446 falling_from_flap = true;
450 if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
451 && physic.get_velocity_y() < 0)
453 float gravity = Sector::current()->gravity;
455 float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
457 // XXX: magic numbers. should be a percent of gravity
458 // gravity is (by default) -0.1f
459 physic.set_acceleration_y(12 + 1*xr);
462 // To slow down x-vel when flapping (not working)
463 if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
465 if (physic.get_velocity_x() < 0)
466 physic.set_acceleration_x(1.0f);
467 else if (physic.get_velocity_x() > 0)
468 physic.set_acceleration_x(-1.0f);
473 physic.set_acceleration_y(0);
478 /* In case the player has pressed Down while in a certain range of air,
479 enable butt jump action */
480 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
481 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
484 /* When Down is not held anymore, disable butt jump */
485 if(butt_jump && !controller->hold(Controller::DOWN))
490 if (butt_jump && on_ground() && is_big()) {
493 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
495 Sector::current()->add_smoke_cloud(
496 Vector(get_pos().x - 32, get_pos().y + 32));
500 // Break bricks beneath Tux
501 if(Sector::current()->trybreakbrick(
502 Vector(base.x + 1, base.y + base.height), false)
503 || Sector::current()->trybreakbrick(
504 Vector(base.x + base.width - 1, base.y + base.height), false)) {
505 physic.set_velocity_y(2);
509 // Kill nearby badguys
510 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
511 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
512 i != gameobjects.end();
514 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
516 // don't kill when badguys are already dying or in a certain mode
517 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
518 badguy->mode != BadGuy::BOMB_EXPLODE) {
519 if (fabsf(base.x - badguy->base.x) < 96 &&
520 fabsf(base.y - badguy->base.y) < 64)
528 /** jumping is only allowed if we're about to touch ground soon and if the
529 * button has been up in between the last jump
533 if ( (issolid(get_pos().x + bbox.get_width() / 2,
534 get_pos().y + bbox.get_height() + 64) ||
535 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
536 issolid(get_pos().x + bbox.get_width() - 1,
537 get_pos().y + bbox.get_height() + 64))
540 && input.jump && !input.old_jump)
548 Player::handle_input()
550 /* Handle horizontal movement: */
551 handle_horizontal_input();
554 if (on_ground() && !controller->hold(Controller::JUMP))
556 handle_vertical_input();
559 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
560 if(Sector::current()->add_bullet(
561 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
562 : Vector(32, bbox.get_height()/2)),
563 physic.get_velocity_x(), dir))
564 shooting_timer.start(SHOOTING_TIME);
568 if (controller->hold(Controller::DOWN) && is_big() && !duck
569 && physic.get_velocity_y() == 0 && on_ground()) {
571 bbox.move(Vector(0, 32));
572 bbox.set_height(31.8);
573 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
574 // try if we can really unduck
575 bbox.move(Vector(0, -32));
576 bbox.set_height(63.8);
580 // when unducking in air we need some space to do so
581 if(on_ground() || !collision_object_map(bbox)) {
584 // undo the ducking changes
585 bbox.move(Vector(0, 32));
586 bbox.set_height(31.8);
593 Player::set_bonus(BonusType type, bool animate)
595 if(player_status->bonus >= type)
598 if(player_status->bonus == NO_BONUS) {
599 bbox.set_height(63.8);
600 bbox.move(Vector(0, -32));
602 growing_timer.start(GROWING_TIME);
605 player_status->bonus = type;
609 Player::draw(DrawingContext& context)
611 TuxBodyParts* tux_body;
613 if (player_status->bonus == GROWUP_BONUS)
615 else if (player_status->bonus == FIRE_BONUS)
617 else if (player_status->bonus == ICE_BONUS)
620 tux_body = small_tux;
622 int layer = LAYER_OBJECTS + 10;
624 /* Set Tux sprite action */
625 if (duck && is_big())
628 tux_body->set_action("duck-left");
630 tux_body->set_action("duck-right");
632 else if (skidding_timer.started() && !skidding_timer.check())
635 tux_body->set_action("skid-left");
637 tux_body->set_action("skid-right");
639 else if (kick_timer.started() && !kick_timer.check())
642 tux_body->set_action("kick-left");
644 tux_body->set_action("kick-right");
646 else if (butt_jump && is_big())
649 tux_body->set_action("buttjump-left");
651 tux_body->set_action("buttjump-right");
653 else if (physic.get_velocity_y() != 0)
656 tux_body->set_action("jump-left");
658 tux_body->set_action("jump-right");
662 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
665 tux_body->set_action("stand-left");
667 tux_body->set_action("stand-right");
672 tux_body->set_action("walk-left");
674 tux_body->set_action("walk-right");
678 if(idle_timer.check())
683 tux_body->head->set_action("idle-left", 1);
685 tux_body->head->set_action("idle-right", 1);
690 // Tux is holding something
691 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
692 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
697 tux_body->arms->set_action("duck+grab-left");
699 tux_body->arms->set_action("duck+grab-right");
704 tux_body->arms->set_action("grab-left");
706 tux_body->arms->set_action("grab-right");
712 smalltux_gameover->draw(context, get_pos(), layer);
713 } else if(growing_timer.get_timeleft() > 0) {
717 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
718 int((growing_timer.get_timegone() *
719 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
721 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
722 int((growing_timer.get_timegone() *
723 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
728 context.draw_surface(growingtux_right[
729 int((growing_timer.get_timegone() *
730 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
732 context.draw_surface(growingtux_left[
733 int((growing_timer.get_timegone() *
734 GROWING_FRAMES) / GROWING_TIME)],
738 else if (safe_timer.started() && size_t(game_time*40)%2)
741 tux_body->draw(context, get_pos(), layer);
743 // Draw blinking star overlay
744 if (invincible_timer.started() &&
745 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
746 || size_t(game_time*20)%2)
749 if (!is_big() || duck)
750 smalltux_star->draw(context, get_pos(), layer + 5);
752 bigtux_star->draw(context, get_pos(), layer + 5);
757 Player::collision(GameObject& other, const CollisionHit& hit)
759 Portable* portable = dynamic_cast<Portable*> (&other);
760 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
761 && fabsf(hit.normal.x) > .9) {
762 grabbed_object = portable;
766 if(other.get_flags() & FLAG_SOLID) {
767 TileMap* tilemap = dynamic_cast<TileMap*> (&other);
769 const TilemapCollisionHit* thit =
770 static_cast<const TilemapCollisionHit*> (&hit);
771 if(thit->tileflags & Tile::SPIKE)
774 if(! (thit->tileflags & Tile::SOLID))
778 if(hit.normal.y < 0) { // landed on floor?
779 if (physic.get_velocity_y() < 0)
780 physic.set_velocity_y(0);
781 on_ground_flag = true;
782 } else if(hit.normal.y > 0) { // bumped against the roof
783 physic.set_velocity_y(.1);
786 if(fabsf(hit.normal.x) > .9) { // hit on the side?
787 physic.set_velocity_x(0);
793 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
795 if(controller->pressed(Controller::UP))
796 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
803 Player::make_invincible()
805 sound_manager->play("sounds/invincible.wav");
806 invincible_timer.start(TUX_INVINCIBLE_TIME);
807 Sector::current()->play_music(HERRING_MUSIC);
812 Player::kill(HurtMode mode)
814 if(dying || deactivated)
818 safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
821 sound_manager->play("sounds/hurt.wav");
823 physic.set_velocity_x(0);
825 if (mode == SHRINK && is_big())
827 if (player_status->bonus == FIRE_BONUS
828 || player_status->bonus == ICE_BONUS)
830 safe_timer.start(TUX_SAFE_TIME);
831 player_status->bonus = GROWUP_BONUS;
835 growing_timer.start(GROWING_TIME);
836 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
837 bbox.set_height(31.8);
839 player_status->bonus = NO_BONUS;
844 physic.enable_gravity(true);
845 physic.set_acceleration(0, 0);
846 physic.set_velocity(0, 700);
847 player_status->lives -= 1;
848 player_status->bonus = NO_BONUS;
850 dying_timer.start(3.0);
851 flags |= FLAG_NO_COLLDET;
856 Player::move(const Vector& vector)
858 bbox.set_pos(vector);
860 bbox.set_size(31.8, 63.8);
862 bbox.set_size(31.8, 31.8);
863 on_ground_flag = false;
865 last_ground_y = vector.y;
871 Player::check_bounds(Camera* camera)
873 /* Keep tux in bounds: */
875 { // Lock Tux to the size of the level, so that he doesn't fall of
877 bbox.set_pos(Vector(0, get_pos().y));
880 /* Keep in-bounds, vertically: */
881 if (get_pos().y > Sector::current()->solids->get_height() * 32)
888 // can happen if back scrolling is disabled
889 if(get_pos().x < camera->get_translation().x) {
890 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
893 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
896 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
905 if(collision_object_map(bbox)) {
914 Player::bounce(BadGuy& )
916 //Make sure we stopped flapping
918 falling_from_flap = false;
920 if(controller->hold(Controller::JUMP))
921 physic.set_velocity_y(520);
923 physic.set_velocity_y(300);
926 //Scripting Functions Below
932 physic.set_velocity_x(0);
933 physic.set_velocity_y(0);
942 void Player::walk(float speed)
944 physic.set_velocity_x(speed);