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"
44 #include "player_status.hpp"
46 static const int TILES_FOR_BUTTJUMP = 3;
47 static const float SHOOTING_TIME = .150;
48 /// time before idle animation starts
49 static const float IDLE_TIME = 2.5;
51 static const float WALK_ACCELERATION_X = 300;
52 static const float RUN_ACCELERATION_X = 400;
53 static const float SKID_XM = 200;
54 static const float SKID_TIME = .3;
55 static const float MAX_WALK_XM = 230;
56 static const float MAX_RUN_XM = 320;
57 static const float WALK_SPEED = 100;
60 Surface* growingtux_left[GROWING_FRAMES];
61 Surface* growingtux_right[GROWING_FRAMES];
63 Surface* tux_life = 0;
65 TuxBodyParts* small_tux = 0;
66 TuxBodyParts* big_tux = 0;
67 TuxBodyParts* fire_tux = 0;
68 TuxBodyParts* ice_tux = 0;
71 TuxBodyParts::set_action(std::string action, int loops)
74 head->set_action(action, loops);
76 body->set_action(action, loops);
78 arms->set_action(action, loops);
80 feet->set_action(action, loops);
84 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
87 head->draw(context, pos, layer-1);
89 body->draw(context, pos, layer-3);
91 arms->draw(context, pos, layer);
93 feet->draw(context, pos, layer-2);
96 Player::Player(PlayerStatus* _player_status)
97 : player_status(_player_status), grabbed_object(0)
99 controller = main_controller;
100 smalltux_gameover = sprite_manager->create("smalltux-gameover");
101 smalltux_star = sprite_manager->create("smalltux-star");
102 bigtux_star = sprite_manager->create("bigtux-star");
108 delete smalltux_gameover;
109 delete smalltux_star;
117 bbox.set_size(31.8, 63.8);
119 bbox.set_size(31.8, 31.8);
128 fall_mode = ON_GROUND;
133 falling_from_flap = false;
134 enable_hover = false;
138 flapping_velocity = 0;
140 // temporary to help player's choosing a flapping
141 flapping_mode = NO_FLAP;
143 // Ricardo's flapping
146 on_ground_flag = false;
153 Player::set_controller(Controller* controller)
155 this->controller = controller;
159 Player::update(float elapsed_time)
161 if(dying && dying_timer.check()) {
166 if(!controller->hold(Controller::ACTION) && grabbed_object) {
168 // move the grabbed object a bit away from tux
169 Vector pos = get_pos() +
170 Vector(dir == LEFT ? -bbox.get_width() : bbox.get_width(),
171 bbox.get_height()*0.66666 - 32);
172 MovingObject* object = dynamic_cast<MovingObject*> (grabbed_object);
174 object->set_pos(pos);
177 std::cout << "Non MovingObjetc grabbed?!?\n";
182 if(!dying && !deactivated)
185 movement = physic.get_movement(elapsed_time);
186 on_ground_flag = false;
189 // special exception for cases where we're stuck under tiles after
190 // being ducked. In this case we drift out
191 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
192 && collision_object_map(base)) {
193 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
194 previous_base = old_base = base;
198 if(grabbed_object != 0) {
199 Vector pos = get_pos() +
200 Vector(dir == LEFT ? -16 : 16,
201 bbox.get_height()*0.66666 - 32);
202 grabbed_object->grab(*this, pos);
209 return on_ground_flag;
215 if(player_status->bonus == NO_BONUS)
222 Player::handle_horizontal_input()
224 float vx = physic.get_velocity_x();
225 float vy = physic.get_velocity_y();
226 float ax = physic.get_acceleration_x();
227 float ay = physic.get_acceleration_y();
230 if(!duck || physic.get_velocity_y() != 0) {
231 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
235 } else if(!controller->hold(Controller::LEFT)
236 && controller->hold(Controller::RIGHT)) {
243 if (!controller->hold(Controller::ACTION)) {
244 ax = dirsign * WALK_ACCELERATION_X;
246 if(vx >= MAX_WALK_XM && dirsign > 0) {
249 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
254 ax = dirsign * RUN_ACCELERATION_X;
256 if(vx >= MAX_RUN_XM && dirsign > 0) {
259 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
265 // we can reach WALK_SPEED without any acceleration
266 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
267 vx = dirsign * WALK_SPEED;
270 // changing directions?
271 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
273 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
274 skidding_timer.start(SKID_TIME);
275 sound_manager->play("sounds/skid.wav");
276 // dust some particles
277 Sector::current()->add_object(
279 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
281 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
282 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
291 // we get slower when not pressing any keys
293 if(fabs(vx) < WALK_SPEED) {
297 ax = WALK_ACCELERATION_X * 1.5;
299 ax = WALK_ACCELERATION_X * -1.5;
304 // if we're on ice slow down acceleration or deceleration
305 if (isice(base.x, base.y + base.height))
307 /* the acceleration/deceleration rate on ice is inversely proportional to
308 * the current velocity.
311 // increasing 1 will increase acceleration/deceleration rate
312 // decreasing 1 will decrease acceleration/deceleration rate
313 // must stay above zero, though
314 if (ax != 0) ax *= 1 / fabs(vx);
318 // extend/shrink tux collision rectangle so that we fall through/walk over 1
320 if(fabsf(vx) > MAX_WALK_XM) {
323 bbox.set_width(31.8);
326 physic.set_velocity(vx, vy);
327 physic.set_acceleration(ax, ay);
331 Player::handle_vertical_input()
335 fall_mode = ON_GROUND;
336 last_ground_y = get_pos().y;
338 if(get_pos().y > last_ground_y)
340 else if(fall_mode == ON_GROUND)
344 if(on_ground()) { /* Make sure jumping is off. */
347 falling_from_flap = false;
348 if (flapping_timer.started()) {
349 flapping_timer.stop();
352 physic.set_acceleration_y(0); //for flapping
356 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
357 if (duck) // only jump a little bit when in duck mode
358 physic.set_velocity_y(300);
359 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
360 physic.set_velocity_y(580);
362 physic.set_velocity_y(520);
364 //bbox.move(Vector(0, -1));
369 flaps_nb = 0; // Ricardo's flapping
371 sound_manager->play("sounds/bigjump.wav");
373 sound_manager->play("sounds/jump.wav");
374 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
375 if (!flapping && !duck && !falling_from_flap && !on_ground()) {
378 if (jumping && physic.get_velocity_y() > 0) {
380 physic.set_velocity_y(0);
384 // temporary to help players choosing a flapping
385 if(flapping_mode == RICARDO_FLAP) {
386 // Flapping, Ricardo's version
387 // similar to SM3 Fox
388 if(controller->pressed(Controller::JUMP) && can_flap && flaps_nb < 3) {
389 physic.set_velocity_y(350);
390 physic.set_velocity_x(physic.get_velocity_x() * 35);
393 } else if(flapping_mode == MAREK_FLAP) {
395 // Flapping, Marek and Ondra's version
396 if (controller->hold(Controller::JUMP) && can_flap)
398 if (flapping_timer.check())
402 falling_from_flap = true;
404 else if (!flapping_timer.started())
406 flapping_timer.start(TUX_FLAPPING_TIME);
407 flapping_velocity = physic.get_velocity_x();
413 float cv = flapping_velocity * sqrt(
414 TUX_FLAPPING_TIME - flapping_timer.get_timegone()
415 / TUX_FLAPPING_TIME);
417 //Handle change of direction while flapping
418 if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
422 if (controller->hold(Controller::LEFT)) {
423 cv = -TUX_FLAPPING_LEAST_X;
425 else if (controller->hold(Controller::RIGHT)) {
426 cv = TUX_FLAPPING_LEAST_X;
429 physic.set_velocity_x(cv);
430 physic.set_velocity_y(flapping_timer.get_timegone()
431 * TUX_FLAPPING_STRENGTH);
432 //std::cout << "Timegone: " << flapping_timer.get_timegone() << ", Y velocity: " << physic.get_velocity_y() << "\n";
436 } else if(flapping_mode == RYAN_FLAP) {
437 // Flapping, Ryan's version
438 if (controller->hold(Controller::JUMP) && can_flap)
440 if (!flapping_timer.started())
442 flapping_timer.start(TUX_FLAPPING_TIME);
444 if (flapping_timer.check())
447 falling_from_flap = true;
451 if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
452 && physic.get_velocity_y() < 0)
454 float gravity = Sector::current()->gravity;
456 float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
458 // XXX: magic numbers. should be a percent of gravity
459 // gravity is (by default) -0.1f
460 physic.set_acceleration_y(12 + 1*xr);
463 // To slow down x-vel when flapping (not working)
464 if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
466 if (physic.get_velocity_x() < 0)
467 physic.set_acceleration_x(1.0f);
468 else if (physic.get_velocity_x() > 0)
469 physic.set_acceleration_x(-1.0f);
474 physic.set_acceleration_y(0);
479 /* In case the player has pressed Down while in a certain range of air,
480 enable butt jump action */
481 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
482 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
485 /* When Down is not held anymore, disable butt jump */
486 if(butt_jump && !controller->hold(Controller::DOWN))
491 if (butt_jump && on_ground() && is_big()) {
494 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
496 Sector::current()->add_smoke_cloud(
497 Vector(get_pos().x - 32, get_pos().y + 32));
501 // Break bricks beneath Tux
502 if(Sector::current()->trybreakbrick(
503 Vector(base.x + 1, base.y + base.height), false)
504 || Sector::current()->trybreakbrick(
505 Vector(base.x + base.width - 1, base.y + base.height), false)) {
506 physic.set_velocity_y(2);
510 // Kill nearby badguys
511 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
512 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
513 i != gameobjects.end();
515 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
517 // don't kill when badguys are already dying or in a certain mode
518 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
519 badguy->mode != BadGuy::BOMB_EXPLODE) {
520 if (fabsf(base.x - badguy->base.x) < 96 &&
521 fabsf(base.y - badguy->base.y) < 64)
529 /** jumping is only allowed if we're about to touch ground soon and if the
530 * button has been up in between the last jump
534 if ( (issolid(get_pos().x + bbox.get_width() / 2,
535 get_pos().y + bbox.get_height() + 64) ||
536 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
537 issolid(get_pos().x + bbox.get_width() - 1,
538 get_pos().y + bbox.get_height() + 64))
541 && input.jump && !input.old_jump)
549 Player::handle_input()
551 /* Handle horizontal movement: */
552 handle_horizontal_input();
555 if (on_ground() && !controller->hold(Controller::JUMP))
557 handle_vertical_input();
560 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
561 if(Sector::current()->add_bullet(
562 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
563 : Vector(32, bbox.get_height()/2)),
564 physic.get_velocity_x(), dir))
565 shooting_timer.start(SHOOTING_TIME);
569 if (controller->hold(Controller::DOWN) && is_big() && !duck
570 && physic.get_velocity_y() == 0 && on_ground()) {
572 bbox.move(Vector(0, 32));
573 bbox.set_height(31.8);
574 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
575 // try if we can really unduck
576 bbox.move(Vector(0, -32));
577 bbox.set_height(63.8);
581 // when unducking in air we need some space to do so
582 if(on_ground() || !collision_object_map(bbox)) {
585 // undo the ducking changes
586 bbox.move(Vector(0, 32));
587 bbox.set_height(31.8);
594 Player::set_bonus(BonusType type, bool animate)
596 if(player_status->bonus >= type)
599 if(player_status->bonus == NO_BONUS) {
600 bbox.set_height(63.8);
601 bbox.move(Vector(0, -32));
603 growing_timer.start(GROWING_TIME);
606 player_status->bonus = type;
610 Player::draw(DrawingContext& context)
612 TuxBodyParts* tux_body;
614 if (player_status->bonus == GROWUP_BONUS)
616 else if (player_status->bonus == FIRE_BONUS)
618 else if (player_status->bonus == ICE_BONUS)
621 tux_body = small_tux;
623 int layer = LAYER_OBJECTS + 10;
625 /* Set Tux sprite action */
626 if (duck && is_big())
629 tux_body->set_action("duck-left");
631 tux_body->set_action("duck-right");
633 else if (skidding_timer.started() && !skidding_timer.check())
636 tux_body->set_action("skid-left");
638 tux_body->set_action("skid-right");
640 else if (kick_timer.started() && !kick_timer.check())
643 tux_body->set_action("kick-left");
645 tux_body->set_action("kick-right");
647 else if (butt_jump && is_big())
650 tux_body->set_action("buttjump-left");
652 tux_body->set_action("buttjump-right");
654 else if (physic.get_velocity_y() != 0)
657 tux_body->set_action("jump-left");
659 tux_body->set_action("jump-right");
663 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
666 tux_body->set_action("stand-left");
668 tux_body->set_action("stand-right");
673 tux_body->set_action("walk-left");
675 tux_body->set_action("walk-right");
679 if(idle_timer.check())
684 tux_body->head->set_action("idle-left", 1);
686 tux_body->head->set_action("idle-right", 1);
691 // Tux is holding something
692 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
693 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
698 tux_body->arms->set_action("duck+grab-left");
700 tux_body->arms->set_action("duck+grab-right");
705 tux_body->arms->set_action("grab-left");
707 tux_body->arms->set_action("grab-right");
713 smalltux_gameover->draw(context, get_pos(), layer);
714 } else if(growing_timer.get_timeleft() > 0) {
718 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
719 int((growing_timer.get_timegone() *
720 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
722 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
723 int((growing_timer.get_timegone() *
724 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
729 context.draw_surface(growingtux_right[
730 int((growing_timer.get_timegone() *
731 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
733 context.draw_surface(growingtux_left[
734 int((growing_timer.get_timegone() *
735 GROWING_FRAMES) / GROWING_TIME)],
739 else if (safe_timer.started() && size_t(game_time*40)%2)
742 tux_body->draw(context, get_pos(), layer);
744 // Draw blinking star overlay
745 if (invincible_timer.started() &&
746 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
747 || size_t(game_time*20)%2)
750 if (!is_big() || duck)
751 smalltux_star->draw(context, get_pos(), layer + 5);
753 bigtux_star->draw(context, get_pos(), layer + 5);
758 Player::collision(GameObject& other, const CollisionHit& hit)
760 Portable* portable = dynamic_cast<Portable*> (&other);
761 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
762 && fabsf(hit.normal.x) > .9) {
763 grabbed_object = portable;
767 if(other.get_flags() & FLAG_SOLID) {
768 TileMap* tilemap = dynamic_cast<TileMap*> (&other);
770 const TilemapCollisionHit* thit =
771 static_cast<const TilemapCollisionHit*> (&hit);
772 if(thit->tileflags & Tile::SPIKE)
775 if(! (thit->tileflags & Tile::SOLID))
779 if(hit.normal.y < 0) { // landed on floor?
780 if (physic.get_velocity_y() < 0)
781 physic.set_velocity_y(0);
782 on_ground_flag = true;
783 } else if(hit.normal.y > 0) { // bumped against the roof
784 physic.set_velocity_y(.1);
787 if(fabsf(hit.normal.x) > .9) { // hit on the side?
788 physic.set_velocity_x(0);
794 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
796 if(controller->pressed(Controller::UP))
797 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
804 Player::make_invincible()
806 sound_manager->play("sounds/invincible.wav");
807 invincible_timer.start(TUX_INVINCIBLE_TIME);
808 Sector::current()->play_music(HERRING_MUSIC);
813 Player::kill(HurtMode mode)
815 if(dying || deactivated)
819 safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
822 sound_manager->play("sounds/hurt.wav");
824 physic.set_velocity_x(0);
826 if (mode == SHRINK && is_big())
828 if (player_status->bonus == FIRE_BONUS
829 || player_status->bonus == ICE_BONUS)
831 safe_timer.start(TUX_SAFE_TIME);
832 player_status->bonus = GROWUP_BONUS;
836 growing_timer.start(GROWING_TIME);
837 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
838 bbox.set_height(31.8);
840 player_status->bonus = NO_BONUS;
845 physic.enable_gravity(true);
846 physic.set_acceleration(0, 0);
847 physic.set_velocity(0, 700);
848 player_status->lives -= 1;
849 player_status->bonus = NO_BONUS;
851 dying_timer.start(3.0);
852 flags |= FLAG_NO_COLLDET;
857 Player::move(const Vector& vector)
859 bbox.set_pos(vector);
861 bbox.set_size(31.8, 63.8);
863 bbox.set_size(31.8, 31.8);
864 on_ground_flag = false;
866 last_ground_y = vector.y;
872 Player::check_bounds(Camera* camera)
874 /* Keep tux in bounds: */
876 { // Lock Tux to the size of the level, so that he doesn't fall of
878 bbox.set_pos(Vector(0, get_pos().y));
881 /* Keep in-bounds, vertically: */
882 if (get_pos().y > Sector::current()->solids->get_height() * 32)
889 // can happen if back scrolling is disabled
890 if(get_pos().x < camera->get_translation().x) {
891 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
894 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
897 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
906 if(collision_object_map(bbox)) {
915 Player::bounce(BadGuy& )
917 //Make sure we stopped flapping
919 falling_from_flap = false;
921 if(controller->hold(Controller::JUMP))
922 physic.set_velocity_y(520);
924 physic.set_velocity_y(300);
927 //Scripting Functions Below
933 physic.set_velocity_x(0);
934 physic.set_velocity_y(0);
943 void Player::walk(float speed)
945 physic.set_velocity_x(speed);