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 Player::Player(PlayerStatus* _player_status)
104 : player_status(_player_status), grabbed_object(0)
106 controller = main_controller;
107 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
108 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
109 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
116 delete smalltux_gameover;
117 delete smalltux_star;
125 bbox.set_size(31.8, 63.8);
127 bbox.set_size(31.8, 31.8);
137 fall_mode = ON_GROUND;
142 backflipping = false;
143 backflip_direction = 0;
146 on_ground_flag = false;
149 floor_normal = Vector(0,-1);
155 Player::expose(HSQUIRRELVM vm, int table_idx)
157 Scripting::Player* interface = static_cast<Scripting::Player*> (this);
158 expose_object(vm, table_idx, interface, "Tux", false);
162 Player::unexpose(HSQUIRRELVM vm, int table_idx)
164 Scripting::unexpose_object(vm, table_idx, "Tux");
168 Player::set_controller(Controller* controller)
170 this->controller = controller;
174 Player::update(float elapsed_time)
176 if(dying && dying_timer.check()) {
181 if(adjust_height != 0) {
182 bbox.move(Vector(0, bbox.get_height() - adjust_height));
183 bbox.set_height(adjust_height);
187 if(!controller->hold(Controller::ACTION) && grabbed_object) {
188 // move the grabbed object a bit away from tux
189 Vector pos = get_pos() +
190 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
191 bbox.get_height()*0.66666 - 32);
192 Rect dest(pos, pos + Vector(32, 32));
193 if(Sector::current()->is_free_space(dest)) {
194 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
196 moving_object->set_pos(pos);
198 msg_debug << "Non MovingObjetc grabbed?!?" << std::endl;
200 grabbed_object->ungrab(*this, dir);
205 if(!dying && !deactivated)
208 movement = physic.get_movement(elapsed_time);
211 // special exception for cases where we're stuck under tiles after
212 // being ducked. In this case we drift out
213 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
214 && collision_object_map(base)) {
215 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
216 previous_base = old_base = base;
220 if(grabbed_object != 0) {
221 Vector pos = get_pos() +
222 Vector(dir == LEFT ? -16 : 16,
223 bbox.get_height()*0.66666 - 32);
224 grabbed_object->grab(*this, pos, dir);
227 on_ground_flag = false;
233 return on_ground_flag;
239 if(player_status->bonus == NO_BONUS)
246 Player::handle_horizontal_input()
248 float vx = physic.get_velocity_x();
249 float vy = physic.get_velocity_y();
250 float ax = physic.get_acceleration_x();
251 float ay = physic.get_acceleration_y();
254 if(!duck || physic.get_velocity_y() != 0) {
255 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
259 } else if(!controller->hold(Controller::LEFT)
260 && controller->hold(Controller::RIGHT)) {
267 if (!controller->hold(Controller::ACTION)) {
268 ax = dirsign * WALK_ACCELERATION_X;
270 if(vx >= MAX_WALK_XM && dirsign > 0) {
273 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
278 ax = dirsign * RUN_ACCELERATION_X;
280 if(vx >= MAX_RUN_XM && dirsign > 0) {
283 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
289 // we can reach WALK_SPEED without any acceleration
290 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
291 vx = dirsign * WALK_SPEED;
294 // changing directions?
295 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
297 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
298 skidding_timer.start(SKID_TIME);
299 sound_manager->play("sounds/skid.wav");
300 // dust some particles
301 Sector::current()->add_object(
303 Vector(dir == RIGHT ? bbox.p2.x : bbox.p1.x, bbox.p2.y),
304 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
305 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
314 // we get slower when not pressing any keys
316 if(fabs(vx) < WALK_SPEED) {
320 ax = WALK_ACCELERATION_X * 1.5;
322 ax = WALK_ACCELERATION_X * -1.5;
327 // if we're on ice slow down acceleration or deceleration
328 if (isice(base.x, base.y + base.height))
330 /* the acceleration/deceleration rate on ice is inversely proportional to
331 * the current velocity.
334 // increasing 1 will increase acceleration/deceleration rate
335 // decreasing 1 will decrease acceleration/deceleration rate
336 // must stay above zero, though
337 if (ax != 0) ax *= 1 / fabs(vx);
341 // extend/shrink tux collision rectangle so that we fall through/walk over 1
343 if(fabsf(vx) > MAX_WALK_XM) {
346 bbox.set_width(31.8);
349 // on downward slopes, adjust vertical velocity to match slope angle
351 if (floor_normal.y != 0) {
352 if ((floor_normal.x * vx) > 0) {
353 // we overdo it a little, just to be on the safe side
354 vy = vx * (floor_normal.x / floor_normal.y) * 2;
359 physic.set_velocity(vx, vy);
360 physic.set_acceleration(ax, ay);
364 Player::handle_vertical_input()
368 fall_mode = ON_GROUND;
369 last_ground_y = get_pos().y;
371 if(get_pos().y > last_ground_y)
373 else if(fall_mode == ON_GROUND)
377 if(on_ground()) { /* Make sure jumping is off. */
380 backflipping = false;
381 backflip_direction = 0;
386 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
388 if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
389 physic.set_velocity_y(300);
390 else { //do a backflip
392 physic.set_velocity_y(580);
393 backflip_timer.start(0.15);
396 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
397 physic.set_velocity_y(580);
399 physic.set_velocity_y(520);
401 //bbox.move(Vector(0, -1));
405 sound_manager->play("sounds/bigjump.wav");
407 sound_manager->play("sounds/jump.wav");
408 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
409 if (!backflipping && jumping && physic.get_velocity_y() > 0) {
411 physic.set_velocity_y(0);
415 /* In case the player has pressed Down while in a certain range of air,
416 enable butt jump action */
417 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
418 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
421 /* When Down is not held anymore, disable butt jump */
422 if(butt_jump && !controller->hold(Controller::DOWN))
427 if (butt_jump && on_ground() && is_big()) {
430 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
432 Sector::current()->add_smoke_cloud(
433 Vector(get_pos().x - 32, get_pos().y + 32));
437 // Break bricks beneath Tux
438 if(Sector::current()->trybreakbrick(
439 Vector(base.x + 1, base.y + base.height), false)
440 || Sector::current()->trybreakbrick(
441 Vector(base.x + base.width - 1, base.y + base.height), false)) {
442 physic.set_velocity_y(2);
446 // Kill nearby badguys
447 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
448 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
449 i != gameobjects.end();
451 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
453 // don't kill when badguys are already dying or in a certain mode
454 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
455 badguy->mode != BadGuy::BOMB_EXPLODE) {
456 if (fabsf(base.x - badguy->base.x) < 96 &&
457 fabsf(base.y - badguy->base.y) < 64)
465 /** jumping is only allowed if we're about to touch ground soon and if the
466 * button has been up in between the last jump
470 if ( (issolid(get_pos().x + bbox.get_width() / 2,
471 get_pos().y + bbox.get_height() + 64) ||
472 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
473 issolid(get_pos().x + bbox.get_width() - 1,
474 get_pos().y + bbox.get_height() + 64))
477 && input.jump && !input.old_jump)
485 Player::handle_input()
487 /* Handle horizontal movement: */
488 if (!backflipping) handle_horizontal_input();
490 if (backflip_direction == 0) {
491 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
493 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
494 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
499 if (on_ground() && !controller->hold(Controller::JUMP))
501 handle_vertical_input();
504 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
505 if(Sector::current()->add_bullet(
506 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
507 : Vector(32, bbox.get_height()/2)),
508 physic.get_velocity_x(), dir))
509 shooting_timer.start(SHOOTING_TIME);
513 if (controller->hold(Controller::DOWN) && is_big() && !duck
514 && physic.get_velocity_y() == 0 && on_ground()) {
516 bbox.move(Vector(0, 32));
517 bbox.set_height(31.8);
518 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
519 // if we have some velocity left then check if there is space for
521 bbox.move(Vector(0, -32));
522 bbox.set_height(63.8);
523 if(Sector::current()->is_free_space(bbox) || (
524 physic.get_velocity_x() > -.01 && physic.get_velocity_x() < .01
525 && physic.get_velocity_y() > -.01 && physic.get_velocity_y() < .01))
529 // undo the ducking changes
530 bbox.move(Vector(0, 32));
531 bbox.set_height(31.8);
537 Player::set_bonus(BonusType type, bool animate)
539 if(player_status->bonus >= type)
542 if(player_status->bonus == NO_BONUS) {
543 adjust_height = 63.8;
545 growing_timer.start(GROWING_TIME);
548 player_status->bonus = type;
552 Player::set_visible(bool visible)
554 this->visible = visible;
558 Player::get_visible()
566 kick_timer.start(KICK_TIME);
570 Player::draw(DrawingContext& context)
575 TuxBodyParts* tux_body;
577 if (player_status->bonus == GROWUP_BONUS)
579 else if (player_status->bonus == FIRE_BONUS)
581 else if (player_status->bonus == ICE_BONUS)
584 tux_body = small_tux;
586 int layer = LAYER_OBJECTS + 10;
588 /* Set Tux sprite action */
589 if (duck && is_big())
592 tux_body->set_action("duck-left");
594 tux_body->set_action("duck-right");
596 else if (skidding_timer.started() && !skidding_timer.check())
599 tux_body->set_action("skid-left");
601 tux_body->set_action("skid-right");
603 else if (kick_timer.started() && !kick_timer.check())
606 tux_body->set_action("kick-left");
608 tux_body->set_action("kick-right");
610 else if (butt_jump && is_big())
613 tux_body->set_action("buttjump-left");
615 tux_body->set_action("buttjump-right");
617 else if (!on_ground())
620 tux_body->set_action("jump-left");
622 tux_body->set_action("jump-right");
626 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
629 tux_body->set_action("stand-left");
631 tux_body->set_action("stand-right");
636 tux_body->set_action("walk-left");
638 tux_body->set_action("walk-right");
642 if(idle_timer.check())
647 tux_body->head->set_action("idle-left", 1);
649 tux_body->head->set_action("idle-right", 1);
654 // Tux is holding something
655 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
656 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
661 tux_body->arms->set_action("duck+grab-left");
663 tux_body->arms->set_action("duck+grab-right");
668 tux_body->arms->set_action("grab-left");
670 tux_body->arms->set_action("grab-right");
676 smalltux_gameover->draw(context, get_pos(), layer);
677 } else if(growing_timer.get_timeleft() > 0) {
679 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
680 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
682 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
683 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
686 else if (safe_timer.started() && size_t(game_time*40)%2)
689 tux_body->draw(context, get_pos(), layer);
691 // Draw blinking star overlay
692 if (invincible_timer.started() &&
693 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
694 || size_t(game_time*20)%2)
697 if (!is_big() || duck)
698 smalltux_star->draw(context, get_pos(), layer + 5);
700 bigtux_star->draw(context, get_pos(), layer + 5);
705 Player::collision_tile(uint32_t tile_attributes)
707 if(tile_attributes & Tile::HURTS)
712 Player::collision(GameObject& other, const CollisionHit& hit)
714 Bullet* bullet = dynamic_cast<Bullet*> (&other);
719 if(other.get_flags() & FLAG_PORTABLE) {
720 Portable* portable = dynamic_cast<Portable*> (&other);
721 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
722 && fabsf(hit.normal.x) > .9) {
723 grabbed_object = portable;
728 if(other.get_flags() & FLAG_SOLID) {
730 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
732 hit.normal.x, hit.normal.y, hit.depth,
733 get_pos().x, get_pos().y,
734 movement.x, movement.y);
737 if(hit.normal.y < 0) { // landed on floor?
738 if(physic.get_velocity_y() < 0)
739 physic.set_velocity_y(0);
741 on_ground_flag = true;
743 // remember normal of this tile
744 if (hit.normal.y > -0.9) {
745 floor_normal.x = hit.normal.x;
746 floor_normal.y = hit.normal.y;
748 // slowly adjust to unisolid tiles.
749 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
750 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
751 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
754 // hack platforms so that we stand normally on them when going down...
755 Platform* platform = dynamic_cast<Platform*> (&other);
756 if(platform != NULL) {
757 if(platform->get_speed().y > 0)
758 physic.set_velocity_y(-platform->get_speed().y);
759 //physic.set_velocity_x(platform->get_speed().x);
761 } else if(hit.normal.y > 0) { // bumped against the roof
762 physic.set_velocity_y(.1);
764 // hack platform so that we are not glued to it from below
765 Platform* platform = dynamic_cast<Platform*> (&other);
766 if(platform != NULL) {
767 physic.set_velocity_y(-platform->get_speed().y);
771 if(fabsf(hit.normal.x) > .9) { // hit on the side?
772 physic.set_velocity_x(0);
775 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
777 Vector mov = movement - omov->get_movement();
779 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",
781 hit.normal.x, hit.normal.y,
783 movement.x, movement.y,
784 dest.p1.x, dest.p1.y,
785 omov->get_movement().x, omov->get_movement().y);
793 assert(dynamic_cast<MovingObject*> (&other) != NULL);
795 MovingObject* moving_object = static_cast<MovingObject*> (&other);
796 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
797 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
799 if(controller->pressed(Controller::UP))
800 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
806 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
808 if(safe_timer.started())
818 Player::make_invincible()
820 sound_manager->play("sounds/invincible.wav");
821 invincible_timer.start(TUX_INVINCIBLE_TIME);
822 Sector::current()->play_music(HERRING_MUSIC);
827 Player::kill(HurtMode mode)
829 if(dying || deactivated)
833 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
836 sound_manager->play("sounds/hurt.wav");
838 physic.set_velocity_x(0);
840 if (mode == SHRINK && is_big())
842 if (player_status->bonus == FIRE_BONUS
843 || player_status->bonus == ICE_BONUS)
845 safe_timer.start(TUX_SAFE_TIME);
846 player_status->bonus = GROWUP_BONUS;
850 //growing_timer.start(GROWING_TIME);
851 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
852 adjust_height = 31.8;
854 player_status->bonus = NO_BONUS;
859 physic.enable_gravity(true);
860 physic.set_acceleration(0, 0);
861 physic.set_velocity(0, 700);
862 player_status->coins -= 25;
863 player_status->bonus = NO_BONUS;
865 dying_timer.start(3.0);
866 set_group(COLGROUP_DISABLED);
868 DisplayEffect* effect = new DisplayEffect();
869 effect->fade_out(3.0);
870 Sector::current()->add_object(effect);
871 sound_manager->stop_music(3.0);
876 Player::move(const Vector& vector)
878 bbox.set_pos(vector);
880 bbox.set_size(31.8, 63.8);
882 bbox.set_size(31.8, 31.8);
884 last_ground_y = vector.y;
890 Player::check_bounds(Camera* camera)
892 /* Keep tux in bounds: */
894 { // Lock Tux to the size of the level, so that he doesn't fall of
896 bbox.set_pos(Vector(0, get_pos().y));
899 /* Keep in-bounds, vertically: */
900 if (get_pos().y > Sector::current()->solids->get_height() * 32)
907 // can happen if back scrolling is disabled
908 if(get_pos().x < camera->get_translation().x) {
909 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
912 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
915 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
924 if(collision_object_map(bbox)) {
933 Player::add_velocity(const Vector& velocity)
935 physic.set_velocity(physic.get_velocity() + velocity);
939 Player::bounce(BadGuy& )
941 if(controller->hold(Controller::JUMP))
942 physic.set_velocity_y(520);
944 physic.set_velocity_y(300);
947 //Scripting Functions Below
953 physic.set_velocity_x(0);
954 physic.set_velocity_y(0);
963 void Player::walk(float speed)
965 physic.set_velocity_x(speed);