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/particles.hpp"
40 #include "object/portable.hpp"
41 #include "object/bullet.hpp"
42 #include "trigger/trigger_base.hpp"
43 #include "control/joystickkeyboardcontroller.hpp"
44 #include "scripting/wrapper_util.hpp"
46 #include "platform.hpp"
47 #include "badguy/badguy.hpp"
48 #include "player_status.hpp"
51 static const int TILES_FOR_BUTTJUMP = 3;
52 static const float SHOOTING_TIME = .150;
53 /// time before idle animation starts
54 static const float IDLE_TIME = 2.5;
56 static const float WALK_ACCELERATION_X = 300;
57 static const float RUN_ACCELERATION_X = 400;
58 static const float SKID_XM = 200;
59 static const float SKID_TIME = .3;
60 static const float MAX_WALK_XM = 230;
61 static const float MAX_RUN_XM = 320;
62 static const float WALK_SPEED = 100;
64 static const float KICK_TIME = .3;
67 Surface* growingtux_left[GROWING_FRAMES];
68 Surface* growingtux_right[GROWING_FRAMES];
70 Surface* tux_life = 0;
72 TuxBodyParts* small_tux = 0;
73 TuxBodyParts* big_tux = 0;
74 TuxBodyParts* fire_tux = 0;
75 TuxBodyParts* ice_tux = 0;
78 TuxBodyParts::set_action(std::string action, int loops)
81 head->set_action(action, loops);
83 body->set_action(action, loops);
85 arms->set_action(action, loops);
87 feet->set_action(action, loops);
91 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
94 head->draw(context, pos, layer-1);
96 body->draw(context, pos, layer-3);
98 arms->draw(context, pos, layer);
100 feet->draw(context, pos, layer-2);
103 FallingCoin::FallingCoin(const Vector& start_position, const int vel_x)
105 pos = start_position;
106 sprite = sprite_manager->create("images/objects/coin/coin.sprite");
107 physic.set_velocity_y(800);
108 physic.set_velocity_x(vel_x);
111 FallingCoin::~FallingCoin()
117 FallingCoin::draw(DrawingContext& context)
119 sprite->draw(context, pos, LAYER_OBJECTS + 5);
123 FallingCoin::update(float elapsed_time)
125 pos += physic.get_movement(elapsed_time);
126 if (pos.y > SCREEN_HEIGHT)
130 Player::Player(PlayerStatus* _player_status)
131 : player_status(_player_status), grabbed_object(0)
133 controller = main_controller;
134 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
135 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
136 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
143 delete smalltux_gameover;
144 delete smalltux_star;
152 bbox.set_size(31.8, 62.8);
154 bbox.set_size(31.8, 30.8);
164 fall_mode = ON_GROUND;
169 backflipping = false;
170 backflip_direction = 0;
173 on_ground_flag = false;
176 floor_normal = Vector(0,-1);
182 Player::expose(HSQUIRRELVM vm, int table_idx)
184 Scripting::Player* interface = static_cast<Scripting::Player*> (this);
185 expose_object(vm, table_idx, interface, "Tux", false);
189 Player::unexpose(HSQUIRRELVM vm, int table_idx)
191 Scripting::unexpose_object(vm, table_idx, "Tux");
195 Player::set_controller(Controller* controller)
197 this->controller = controller;
201 Player::update(float elapsed_time)
203 if(dying && dying_timer.check()) {
208 if(adjust_height != 0) {
209 bbox.move(Vector(0, bbox.get_height() - adjust_height));
210 bbox.set_height(adjust_height);
214 if(!controller->hold(Controller::ACTION) && grabbed_object) {
215 // move the grabbed object a bit away from tux
216 Vector pos = get_pos() +
217 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
218 bbox.get_height()*0.66666 - 32);
219 Rect dest(pos, pos + Vector(32, 32));
220 if(Sector::current()->is_free_space(dest)) {
221 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
223 moving_object->set_pos(pos);
225 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
227 grabbed_object->ungrab(*this, dir);
232 if(!dying && !deactivated)
235 movement = physic.get_movement(elapsed_time);
238 // special exception for cases where we're stuck under tiles after
239 // being ducked. In this case we drift out
240 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
241 && collision_object_map(base)) {
242 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
243 previous_base = old_base = base;
247 if(grabbed_object != 0) {
248 Vector pos = get_pos() +
249 Vector(dir == LEFT ? -16 : 16,
250 bbox.get_height()*0.66666 - 32);
251 grabbed_object->grab(*this, pos, dir);
254 on_ground_flag = false;
260 return on_ground_flag;
266 if(player_status->bonus == NO_BONUS)
273 Player::handle_horizontal_input()
275 float vx = physic.get_velocity_x();
276 float vy = physic.get_velocity_y();
277 float ax = physic.get_acceleration_x();
278 float ay = physic.get_acceleration_y();
281 if(!duck || physic.get_velocity_y() != 0) {
282 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
286 } else if(!controller->hold(Controller::LEFT)
287 && controller->hold(Controller::RIGHT)) {
294 if (!controller->hold(Controller::ACTION)) {
295 ax = dirsign * WALK_ACCELERATION_X;
297 if(vx >= MAX_WALK_XM && dirsign > 0) {
300 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
305 ax = dirsign * RUN_ACCELERATION_X;
307 if(vx >= MAX_RUN_XM && dirsign > 0) {
310 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
316 // we can reach WALK_SPEED without any acceleration
317 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
318 vx = dirsign * WALK_SPEED;
321 // changing directions?
322 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
324 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
325 skidding_timer.start(SKID_TIME);
326 sound_manager->play("sounds/skid.wav");
327 // dust some particles
328 Sector::current()->add_object(
330 Vector(dir == RIGHT ? bbox.p2.x : bbox.p1.x, bbox.p2.y),
331 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
332 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
341 // we get slower when not pressing any keys
343 if(fabs(vx) < WALK_SPEED) {
347 ax = WALK_ACCELERATION_X * 1.5;
349 ax = WALK_ACCELERATION_X * -1.5;
354 // if we're on ice slow down acceleration or deceleration
355 if (isice(base.x, base.y + base.height))
357 /* the acceleration/deceleration rate on ice is inversely proportional to
358 * the current velocity.
361 // increasing 1 will increase acceleration/deceleration rate
362 // decreasing 1 will decrease acceleration/deceleration rate
363 // must stay above zero, though
364 if (ax != 0) ax *= 1 / fabs(vx);
368 // extend/shrink tux collision rectangle so that we fall through/walk over 1
370 if(fabsf(vx) > MAX_WALK_XM) {
373 bbox.set_width(31.8);
376 // on downward slopes, adjust vertical velocity to match slope angle
378 if (floor_normal.y != 0) {
379 if ((floor_normal.x * vx) > 0) {
380 // we overdo it a little, just to be on the safe side
381 vy = vx * (floor_normal.x / floor_normal.y) * 2;
386 physic.set_velocity(vx, vy);
387 physic.set_acceleration(ax, ay);
391 Player::handle_vertical_input()
395 fall_mode = ON_GROUND;
396 last_ground_y = get_pos().y;
398 if(get_pos().y > last_ground_y)
400 else if(fall_mode == ON_GROUND)
404 if(on_ground()) { /* Make sure jumping is off. */
407 backflipping = false;
408 backflip_direction = 0;
413 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
415 if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
416 physic.set_velocity_y(300);
417 else { //do a backflip
419 physic.set_velocity_y(580);
420 backflip_timer.start(0.15);
423 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
424 physic.set_velocity_y(580);
426 physic.set_velocity_y(520);
428 //bbox.move(Vector(0, -1));
432 sound_manager->play("sounds/bigjump.wav");
434 sound_manager->play("sounds/jump.wav");
435 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
436 if (!backflipping && jumping && physic.get_velocity_y() > 0) {
438 physic.set_velocity_y(0);
442 /* In case the player has pressed Down while in a certain range of air,
443 enable butt jump action */
444 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
445 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
448 /* When Down is not held anymore, disable butt jump */
449 if(butt_jump && !controller->hold(Controller::DOWN))
454 if (butt_jump && on_ground() && is_big()) {
457 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
459 Sector::current()->add_smoke_cloud(
460 Vector(get_pos().x - 32, get_pos().y + 32));
464 // Break bricks beneath Tux
465 if(Sector::current()->trybreakbrick(
466 Vector(base.x + 1, base.y + base.height), false)
467 || Sector::current()->trybreakbrick(
468 Vector(base.x + base.width - 1, base.y + base.height), false)) {
469 physic.set_velocity_y(2);
473 // Kill nearby badguys
474 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
475 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
476 i != gameobjects.end();
478 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
480 // don't kill when badguys are already dying or in a certain mode
481 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
482 badguy->mode != BadGuy::BOMB_EXPLODE) {
483 if (fabsf(base.x - badguy->base.x) < 96 &&
484 fabsf(base.y - badguy->base.y) < 64)
492 /** jumping is only allowed if we're about to touch ground soon and if the
493 * button has been up in between the last jump
497 if ( (issolid(get_pos().x + bbox.get_width() / 2,
498 get_pos().y + bbox.get_height() + 64) ||
499 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
500 issolid(get_pos().x + bbox.get_width() - 1,
501 get_pos().y + bbox.get_height() + 64))
504 && input.jump && !input.old_jump)
512 Player::handle_input()
514 /* Handle horizontal movement: */
515 if (!backflipping) handle_horizontal_input();
517 if (backflip_direction == 0) {
518 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
520 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
521 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
526 if (on_ground() && !controller->hold(Controller::JUMP))
528 handle_vertical_input();
531 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
532 if(Sector::current()->add_bullet(
533 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
534 : Vector(32, bbox.get_height()/2)),
535 physic.get_velocity_x(), dir))
536 shooting_timer.start(SHOOTING_TIME);
540 if (controller->hold(Controller::DOWN) && is_big() && !duck
541 && physic.get_velocity_y() == 0 && on_ground()) {
543 bbox.move(Vector(0, 32));
544 bbox.set_height(31.8);
545 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
546 // if we have some velocity left then check if there is space for
548 bbox.move(Vector(0, -32));
549 bbox.set_height(63.8);
550 if(Sector::current()->is_free_space(bbox) || (
551 physic.get_velocity_x() > -.01 && physic.get_velocity_x() < .01
552 && physic.get_velocity_y() > -.01 && physic.get_velocity_y() < .01))
556 // undo the ducking changes
557 bbox.move(Vector(0, 32));
558 bbox.set_height(31.8);
564 Player::add_coins(int count)
566 player_status->add_coins(count);
570 Player::set_bonus(const std::string& bonustype)
572 if(bonustype == "grow")
573 set_bonus(GROWUP_BONUS);
574 else if(bonustype == "fireflower")
575 set_bonus(FIRE_BONUS);
576 else if(bonustype == "iceflower")
577 set_bonus(ICE_BONUS);
578 else if(bonustype == "none")
582 std::ostringstream msg;
583 msg << "Unknown bonus type " << bonustype;
584 throw std::runtime_error(msg.str());
588 Player::set_bonus(BonusType type, bool animate)
590 if(player_status->bonus >= type)
593 if(player_status->bonus == NO_BONUS) {
594 adjust_height = 62.8;
596 growing_timer.start(GROWING_TIME);
599 player_status->bonus = type;
603 Player::set_visible(bool visible)
605 this->visible = visible;
609 Player::get_visible()
617 kick_timer.start(KICK_TIME);
621 Player::draw(DrawingContext& context)
626 TuxBodyParts* tux_body;
628 if (player_status->bonus == GROWUP_BONUS)
630 else if (player_status->bonus == FIRE_BONUS)
632 else if (player_status->bonus == ICE_BONUS)
635 tux_body = small_tux;
637 int layer = LAYER_OBJECTS + 10;
639 /* Set Tux sprite action */
640 if (duck && is_big())
643 tux_body->set_action("duck-left");
645 tux_body->set_action("duck-right");
647 else if (skidding_timer.started() && !skidding_timer.check())
650 tux_body->set_action("skid-left");
652 tux_body->set_action("skid-right");
654 else if (kick_timer.started() && !kick_timer.check())
657 tux_body->set_action("kick-left");
659 tux_body->set_action("kick-right");
661 else if (butt_jump && is_big())
664 tux_body->set_action("buttjump-left");
666 tux_body->set_action("buttjump-right");
668 else if (!on_ground())
671 tux_body->set_action("jump-left");
673 tux_body->set_action("jump-right");
677 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
680 tux_body->set_action("stand-left");
682 tux_body->set_action("stand-right");
687 tux_body->set_action("walk-left");
689 tux_body->set_action("walk-right");
693 if(idle_timer.check())
698 tux_body->head->set_action("idle-left", 1);
700 tux_body->head->set_action("idle-right", 1);
705 // Tux is holding something
706 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
707 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
712 tux_body->arms->set_action("duck+grab-left");
714 tux_body->arms->set_action("duck+grab-right");
719 tux_body->arms->set_action("grab-left");
721 tux_body->arms->set_action("grab-right");
727 smalltux_gameover->draw(context, get_pos(), layer);
728 } else if(growing_timer.get_timeleft() > 0) {
730 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
731 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
733 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
734 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
737 else if (safe_timer.started() && size_t(game_time*40)%2)
740 tux_body->draw(context, get_pos(), layer);
742 // Draw blinking star overlay
743 if (invincible_timer.started() &&
744 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
745 || size_t(game_time*20)%2)
748 if (!is_big() || duck)
749 smalltux_star->draw(context, get_pos(), layer + 5);
751 bigtux_star->draw(context, get_pos(), layer + 5);
756 Player::collision_tile(uint32_t tile_attributes)
758 if(tile_attributes & Tile::HURTS)
763 Player::collision(GameObject& other, const CollisionHit& hit)
765 Bullet* bullet = dynamic_cast<Bullet*> (&other);
770 if(other.get_flags() & FLAG_PORTABLE) {
771 Portable* portable = dynamic_cast<Portable*> (&other);
772 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
773 && fabsf(hit.normal.x) > .9) {
774 grabbed_object = portable;
779 if(other.get_flags() & FLAG_SOLID) {
781 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
783 hit.normal.x, hit.normal.y, hit.depth,
784 get_pos().x, get_pos().y,
785 movement.x, movement.y);
788 if(hit.normal.y < 0) { // landed on floor?
789 if(physic.get_velocity_y() < 0)
790 physic.set_velocity_y(0);
792 on_ground_flag = true;
794 // remember normal of this tile
795 if (hit.normal.y > -0.9) {
796 floor_normal.x = hit.normal.x;
797 floor_normal.y = hit.normal.y;
799 // slowly adjust to unisolid tiles.
800 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
801 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
802 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
805 // hack platforms so that we stand normally on them when going down...
806 Platform* platform = dynamic_cast<Platform*> (&other);
807 if(platform != NULL) {
808 if(platform->get_speed().y > 0)
809 physic.set_velocity_y(-platform->get_speed().y);
810 //physic.set_velocity_x(platform->get_speed().x);
812 } else if(hit.normal.y > 0) { // bumped against the roof
813 physic.set_velocity_y(.1);
815 // hack platform so that we are not glued to it from below
816 Platform* platform = dynamic_cast<Platform*> (&other);
817 if(platform != NULL) {
818 physic.set_velocity_y(-platform->get_speed().y);
822 if(fabsf(hit.normal.x) > .9) { // hit on the side?
823 physic.set_velocity_x(0);
826 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
828 Vector mov = movement - omov->get_movement();
830 printf("W %p - HITN: %3.1f %3.1f D:%3.1f TM: %3.1f %3.1f TD: %3.1f %3.1f PM: %3.2f %3.1f\n",
832 hit.normal.x, hit.normal.y,
834 movement.x, movement.y,
835 dest.p1.x, dest.p1.y,
836 omov->get_movement().x, omov->get_movement().y);
844 assert(dynamic_cast<MovingObject*> (&other) != NULL);
846 MovingObject* moving_object = static_cast<MovingObject*> (&other);
847 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
848 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
850 if(controller->pressed(Controller::UP))
851 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
857 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
859 if(safe_timer.started())
869 Player::make_invincible()
871 sound_manager->play("sounds/invincible.wav");
872 invincible_timer.start(TUX_INVINCIBLE_TIME);
873 Sector::current()->play_music(HERRING_MUSIC);
878 Player::kill(HurtMode mode)
880 if(dying || deactivated)
884 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
887 sound_manager->play("sounds/hurt.wav");
889 physic.set_velocity_x(0);
891 if (mode == SHRINK && is_big())
893 if (player_status->bonus == FIRE_BONUS
894 || player_status->bonus == ICE_BONUS)
896 safe_timer.start(TUX_SAFE_TIME);
897 player_status->bonus = GROWUP_BONUS;
901 //growing_timer.start(GROWING_TIME);
902 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
903 adjust_height = 30.8;
905 player_status->bonus = NO_BONUS;
912 for (i = 0; (i < 5) && (i < player_status->coins); i++)
914 // the numbers: starting x, starting y, velocity y
915 Sector::current()->add_object(new FallingCoin(get_pos() + Vector(rand()%5, rand()%50 - 32), rand()%200 - 100));
917 physic.enable_gravity(true);
918 physic.set_acceleration(0, 0);
919 physic.set_velocity(0, 700);
920 player_status->coins -= 25;
921 player_status->bonus = NO_BONUS;
923 dying_timer.start(3.0);
924 set_group(COLGROUP_DISABLED);
926 DisplayEffect* effect = new DisplayEffect();
927 effect->fade_out(3.0);
928 Sector::current()->add_object(effect);
929 sound_manager->stop_music(3.0);
934 Player::move(const Vector& vector)
936 bbox.set_pos(vector);
938 bbox.set_size(31.8, 63.8);
940 bbox.set_size(31.8, 31.8);
942 last_ground_y = vector.y;
948 Player::check_bounds(Camera* camera)
950 /* Keep tux in bounds: */
952 { // Lock Tux to the size of the level, so that he doesn't fall of
954 bbox.set_pos(Vector(0, get_pos().y));
957 /* Keep in-bounds, vertically: */
958 if (get_pos().y > Sector::current()->solids->get_height() * 32)
965 // can happen if back scrolling is disabled
966 if(get_pos().x < camera->get_translation().x) {
967 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
970 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
973 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
982 if(collision_object_map(bbox)) {
991 Player::add_velocity(const Vector& velocity)
993 physic.set_velocity(physic.get_velocity() + velocity);
997 Player::bounce(BadGuy& )
999 if(controller->hold(Controller::JUMP))
1000 physic.set_velocity_y(520);
1002 physic.set_velocity_y(300);
1005 //Scripting Functions Below
1008 Player::deactivate()
1011 physic.set_velocity_x(0);
1012 physic.set_velocity_y(0);
1018 deactivated = false;
1021 void Player::walk(float speed)
1023 physic.set_velocity_x(speed);