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::set_bonus(BonusType type, bool animate)
566 if(player_status->bonus >= type)
569 if(player_status->bonus == NO_BONUS) {
570 adjust_height = 62.8;
572 growing_timer.start(GROWING_TIME);
575 player_status->bonus = type;
579 Player::set_visible(bool visible)
581 this->visible = visible;
585 Player::get_visible()
593 kick_timer.start(KICK_TIME);
597 Player::draw(DrawingContext& context)
602 TuxBodyParts* tux_body;
604 if (player_status->bonus == GROWUP_BONUS)
606 else if (player_status->bonus == FIRE_BONUS)
608 else if (player_status->bonus == ICE_BONUS)
611 tux_body = small_tux;
613 int layer = LAYER_OBJECTS + 10;
615 /* Set Tux sprite action */
616 if (duck && is_big())
619 tux_body->set_action("duck-left");
621 tux_body->set_action("duck-right");
623 else if (skidding_timer.started() && !skidding_timer.check())
626 tux_body->set_action("skid-left");
628 tux_body->set_action("skid-right");
630 else if (kick_timer.started() && !kick_timer.check())
633 tux_body->set_action("kick-left");
635 tux_body->set_action("kick-right");
637 else if (butt_jump && is_big())
640 tux_body->set_action("buttjump-left");
642 tux_body->set_action("buttjump-right");
644 else if (!on_ground())
647 tux_body->set_action("jump-left");
649 tux_body->set_action("jump-right");
653 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
656 tux_body->set_action("stand-left");
658 tux_body->set_action("stand-right");
663 tux_body->set_action("walk-left");
665 tux_body->set_action("walk-right");
669 if(idle_timer.check())
674 tux_body->head->set_action("idle-left", 1);
676 tux_body->head->set_action("idle-right", 1);
681 // Tux is holding something
682 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
683 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
688 tux_body->arms->set_action("duck+grab-left");
690 tux_body->arms->set_action("duck+grab-right");
695 tux_body->arms->set_action("grab-left");
697 tux_body->arms->set_action("grab-right");
703 smalltux_gameover->draw(context, get_pos(), layer);
704 } else if(growing_timer.get_timeleft() > 0) {
706 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
707 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
709 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
710 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
713 else if (safe_timer.started() && size_t(game_time*40)%2)
716 tux_body->draw(context, get_pos(), layer);
718 // Draw blinking star overlay
719 if (invincible_timer.started() &&
720 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
721 || size_t(game_time*20)%2)
724 if (!is_big() || duck)
725 smalltux_star->draw(context, get_pos(), layer + 5);
727 bigtux_star->draw(context, get_pos(), layer + 5);
732 Player::collision_tile(uint32_t tile_attributes)
734 if(tile_attributes & Tile::HURTS)
739 Player::collision(GameObject& other, const CollisionHit& hit)
741 Bullet* bullet = dynamic_cast<Bullet*> (&other);
746 if(other.get_flags() & FLAG_PORTABLE) {
747 Portable* portable = dynamic_cast<Portable*> (&other);
748 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
749 && fabsf(hit.normal.x) > .9) {
750 grabbed_object = portable;
755 if(other.get_flags() & FLAG_SOLID) {
757 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
759 hit.normal.x, hit.normal.y, hit.depth,
760 get_pos().x, get_pos().y,
761 movement.x, movement.y);
764 if(hit.normal.y < 0) { // landed on floor?
765 if(physic.get_velocity_y() < 0)
766 physic.set_velocity_y(0);
768 on_ground_flag = true;
770 // remember normal of this tile
771 if (hit.normal.y > -0.9) {
772 floor_normal.x = hit.normal.x;
773 floor_normal.y = hit.normal.y;
775 // slowly adjust to unisolid tiles.
776 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
777 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
778 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
781 // hack platforms so that we stand normally on them when going down...
782 Platform* platform = dynamic_cast<Platform*> (&other);
783 if(platform != NULL) {
784 if(platform->get_speed().y > 0)
785 physic.set_velocity_y(-platform->get_speed().y);
786 //physic.set_velocity_x(platform->get_speed().x);
788 } else if(hit.normal.y > 0) { // bumped against the roof
789 physic.set_velocity_y(.1);
791 // hack platform so that we are not glued to it from below
792 Platform* platform = dynamic_cast<Platform*> (&other);
793 if(platform != NULL) {
794 physic.set_velocity_y(-platform->get_speed().y);
798 if(fabsf(hit.normal.x) > .9) { // hit on the side?
799 physic.set_velocity_x(0);
802 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
804 Vector mov = movement - omov->get_movement();
806 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",
808 hit.normal.x, hit.normal.y,
810 movement.x, movement.y,
811 dest.p1.x, dest.p1.y,
812 omov->get_movement().x, omov->get_movement().y);
820 assert(dynamic_cast<MovingObject*> (&other) != NULL);
822 MovingObject* moving_object = static_cast<MovingObject*> (&other);
823 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
824 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
826 if(controller->pressed(Controller::UP))
827 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
833 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
835 if(safe_timer.started())
845 Player::make_invincible()
847 sound_manager->play("sounds/invincible.wav");
848 invincible_timer.start(TUX_INVINCIBLE_TIME);
849 Sector::current()->play_music(HERRING_MUSIC);
854 Player::kill(HurtMode mode)
856 if(dying || deactivated)
860 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
863 sound_manager->play("sounds/hurt.wav");
865 physic.set_velocity_x(0);
867 if (mode == SHRINK && is_big())
869 if (player_status->bonus == FIRE_BONUS
870 || player_status->bonus == ICE_BONUS)
872 safe_timer.start(TUX_SAFE_TIME);
873 player_status->bonus = GROWUP_BONUS;
877 //growing_timer.start(GROWING_TIME);
878 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
879 adjust_height = 30.8;
881 player_status->bonus = NO_BONUS;
888 for (i = 0; (i < 5) && (i < player_status->coins); i++)
890 // the numbers: starting x, starting y, velocity y
891 Sector::current()->add_object(new FallingCoin(get_pos() + Vector(rand()%5, rand()%50 - 32), rand()%200 - 100));
893 physic.enable_gravity(true);
894 physic.set_acceleration(0, 0);
895 physic.set_velocity(0, 700);
896 player_status->coins -= 25;
897 player_status->bonus = NO_BONUS;
899 dying_timer.start(3.0);
900 set_group(COLGROUP_DISABLED);
902 DisplayEffect* effect = new DisplayEffect();
903 effect->fade_out(3.0);
904 Sector::current()->add_object(effect);
905 sound_manager->stop_music(3.0);
910 Player::move(const Vector& vector)
912 bbox.set_pos(vector);
914 bbox.set_size(31.8, 63.8);
916 bbox.set_size(31.8, 31.8);
918 last_ground_y = vector.y;
924 Player::check_bounds(Camera* camera)
926 /* Keep tux in bounds: */
928 { // Lock Tux to the size of the level, so that he doesn't fall of
930 bbox.set_pos(Vector(0, get_pos().y));
933 /* Keep in-bounds, vertically: */
934 if (get_pos().y > Sector::current()->solids->get_height() * 32)
941 // can happen if back scrolling is disabled
942 if(get_pos().x < camera->get_translation().x) {
943 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
946 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
949 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
958 if(collision_object_map(bbox)) {
967 Player::add_velocity(const Vector& velocity)
969 physic.set_velocity(physic.get_velocity() + velocity);
973 Player::bounce(BadGuy& )
975 if(controller->hold(Controller::JUMP))
976 physic.set_velocity_y(520);
978 physic.set_velocity_y(300);
981 //Scripting Functions Below
987 physic.set_velocity_x(0);
988 physic.set_velocity_y(0);
997 void Player::walk(float speed)
999 physic.set_velocity_x(speed);