4 // Copyright (C) 2006 Matthias Braun <matze@braunis.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.
27 #include "gettext.hpp"
28 #include "sprite/sprite_manager.hpp"
29 #include "audio/sound_manager.hpp"
32 #include "sprite/sprite.hpp"
34 #include "resources.hpp"
35 #include "video/screen.hpp"
36 #include "statistics.hpp"
37 #include "game_session.hpp"
38 #include "object/tilemap.hpp"
39 #include "object/camera.hpp"
40 #include "object/particles.hpp"
41 #include "object/portable.hpp"
42 #include "object/bullet.hpp"
43 #include "trigger/trigger_base.hpp"
44 #include "control/joystickkeyboardcontroller.hpp"
45 #include "scripting/wrapper_util.hpp"
47 #include "platform.hpp"
48 #include "badguy/badguy.hpp"
49 #include "player_status.hpp"
52 static const int TILES_FOR_BUTTJUMP = 3;
53 static const float SHOOTING_TIME = .150;
54 /// time before idle animation starts
55 static const float IDLE_TIME = 2.5;
57 static const float WALK_ACCELERATION_X = 300;
58 static const float RUN_ACCELERATION_X = 400;
59 static const float SKID_XM = 200;
60 static const float SKID_TIME = .3;
61 static const float MAX_WALK_XM = 230;
62 static const float MAX_RUN_XM = 320;
63 static const float WALK_SPEED = 100;
65 static const float KICK_TIME = .3;
68 Surface* growingtux_left[GROWING_FRAMES];
69 Surface* growingtux_right[GROWING_FRAMES];
71 Surface* tux_life = 0;
73 TuxBodyParts* small_tux = 0;
74 TuxBodyParts* big_tux = 0;
75 TuxBodyParts* fire_tux = 0;
76 TuxBodyParts* ice_tux = 0;
79 TuxBodyParts::set_action(std::string action, int loops)
82 head->set_action(action, loops);
84 body->set_action(action, loops);
86 arms->set_action(action, loops);
88 feet->set_action(action, loops);
92 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
95 head->draw(context, pos, layer-1);
97 body->draw(context, pos, layer-3);
99 arms->draw(context, pos, layer);
101 feet->draw(context, pos, layer-2);
104 FallingCoin::FallingCoin(const Vector& start_position, const int vel_x)
106 pos = start_position;
107 sprite = sprite_manager->create("images/objects/coin/coin.sprite");
108 physic.set_velocity_y(800);
109 physic.set_velocity_x(vel_x);
112 FallingCoin::~FallingCoin()
118 FallingCoin::draw(DrawingContext& context)
120 sprite->draw(context, pos, LAYER_OBJECTS + 5);
124 FallingCoin::update(float elapsed_time)
126 pos += physic.get_movement(elapsed_time);
127 if (pos.y > SCREEN_HEIGHT)
131 Player::Player(PlayerStatus* _player_status)
132 : player_status(_player_status), grabbed_object(0)
134 controller = main_controller;
135 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
136 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
137 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
144 delete smalltux_gameover;
145 delete smalltux_star;
153 bbox.set_size(31.8, 62.8);
155 bbox.set_size(31.8, 30.8);
165 fall_mode = ON_GROUND;
170 backflipping = false;
171 backflip_direction = 0;
174 on_ground_flag = false;
177 floor_normal = Vector(0,-1);
183 Player::expose(HSQUIRRELVM vm, int table_idx)
185 Scripting::Player* interface = static_cast<Scripting::Player*> (this);
186 expose_object(vm, table_idx, interface, "Tux", false);
190 Player::unexpose(HSQUIRRELVM vm, int table_idx)
192 Scripting::unexpose_object(vm, table_idx, "Tux");
196 Player::set_controller(Controller* controller)
198 this->controller = controller;
202 Player::update(float elapsed_time)
204 if(dying && dying_timer.check()) {
209 if(adjust_height != 0) {
210 bbox.move(Vector(0, bbox.get_height() - adjust_height));
211 bbox.set_height(adjust_height);
215 if(!controller->hold(Controller::ACTION) && grabbed_object) {
216 // move the grabbed object a bit away from tux
217 Vector pos = get_pos() +
218 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
219 bbox.get_height()*0.66666 - 32);
220 Rect dest(pos, pos + Vector(32, 32));
221 if(Sector::current()->is_free_space(dest)) {
222 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
224 moving_object->set_pos(pos);
226 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
228 grabbed_object->ungrab(*this, dir);
233 if(!dying && !deactivated)
236 movement = physic.get_movement(elapsed_time);
239 // special exception for cases where we're stuck under tiles after
240 // being ducked. In this case we drift out
241 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
242 && collision_object_map(base)) {
243 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
244 previous_base = old_base = base;
248 if(grabbed_object != 0) {
249 Vector pos = get_pos() +
250 Vector(dir == LEFT ? -16 : 16,
251 bbox.get_height()*0.66666 - 32);
252 grabbed_object->grab(*this, pos, dir);
255 on_ground_flag = false;
261 return on_ground_flag;
267 if(player_status->bonus == NO_BONUS)
274 Player::handle_horizontal_input()
276 float vx = physic.get_velocity_x();
277 float vy = physic.get_velocity_y();
278 float ax = physic.get_acceleration_x();
279 float ay = physic.get_acceleration_y();
282 if(!duck || physic.get_velocity_y() != 0) {
283 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
287 } else if(!controller->hold(Controller::LEFT)
288 && controller->hold(Controller::RIGHT)) {
295 if (!controller->hold(Controller::ACTION)) {
296 ax = dirsign * WALK_ACCELERATION_X;
298 if(vx >= MAX_WALK_XM && dirsign > 0) {
301 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
306 ax = dirsign * RUN_ACCELERATION_X;
308 if(vx >= MAX_RUN_XM && dirsign > 0) {
311 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
317 // we can reach WALK_SPEED without any acceleration
318 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
319 vx = dirsign * WALK_SPEED;
322 // changing directions?
323 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
325 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
326 skidding_timer.start(SKID_TIME);
327 sound_manager->play("sounds/skid.wav");
328 // dust some particles
329 Sector::current()->add_object(
331 Vector(dir == RIGHT ? bbox.p2.x : bbox.p1.x, bbox.p2.y),
332 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
333 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
342 // we get slower when not pressing any keys
344 if(fabs(vx) < WALK_SPEED) {
348 ax = WALK_ACCELERATION_X * 1.5;
350 ax = WALK_ACCELERATION_X * -1.5;
355 // if we're on ice slow down acceleration or deceleration
356 if (isice(base.x, base.y + base.height))
358 /* the acceleration/deceleration rate on ice is inversely proportional to
359 * the current velocity.
362 // increasing 1 will increase acceleration/deceleration rate
363 // decreasing 1 will decrease acceleration/deceleration rate
364 // must stay above zero, though
365 if (ax != 0) ax *= 1 / fabs(vx);
369 // extend/shrink tux collision rectangle so that we fall through/walk over 1
371 if(fabsf(vx) > MAX_WALK_XM) {
374 bbox.set_width(31.8);
377 // on downward slopes, adjust vertical velocity to match slope angle
379 if (floor_normal.y != 0) {
380 if ((floor_normal.x * vx) > 0) {
381 // we overdo it a little, just to be on the safe side
382 vy = vx * (floor_normal.x / floor_normal.y) * 2;
387 physic.set_velocity(vx, vy);
388 physic.set_acceleration(ax, ay);
392 Player::handle_vertical_input()
396 fall_mode = ON_GROUND;
397 last_ground_y = get_pos().y;
399 if(get_pos().y > last_ground_y)
401 else if(fall_mode == ON_GROUND)
405 if(on_ground()) { /* Make sure jumping is off. */
408 backflipping = false;
409 backflip_direction = 0;
414 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
416 if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
417 physic.set_velocity_y(300);
418 else { //do a backflip
420 physic.set_velocity_y(580);
421 backflip_timer.start(0.15);
424 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
425 physic.set_velocity_y(580);
427 physic.set_velocity_y(520);
429 //bbox.move(Vector(0, -1));
433 sound_manager->play("sounds/bigjump.wav");
435 sound_manager->play("sounds/jump.wav");
436 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
437 if (!backflipping && jumping && physic.get_velocity_y() > 0) {
439 physic.set_velocity_y(0);
443 /* In case the player has pressed Down while in a certain range of air,
444 enable butt jump action */
445 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
446 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
449 /* When Down is not held anymore, disable butt jump */
450 if(butt_jump && !controller->hold(Controller::DOWN))
455 if (butt_jump && on_ground() && is_big()) {
458 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
460 Sector::current()->add_smoke_cloud(
461 Vector(get_pos().x - 32, get_pos().y + 32));
465 // Break bricks beneath Tux
466 if(Sector::current()->trybreakbrick(
467 Vector(base.x + 1, base.y + base.height), false)
468 || Sector::current()->trybreakbrick(
469 Vector(base.x + base.width - 1, base.y + base.height), false)) {
470 physic.set_velocity_y(2);
474 // Kill nearby badguys
475 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
476 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
477 i != gameobjects.end();
479 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
481 // don't kill when badguys are already dying or in a certain mode
482 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
483 badguy->mode != BadGuy::BOMB_EXPLODE) {
484 if (fabsf(base.x - badguy->base.x) < 96 &&
485 fabsf(base.y - badguy->base.y) < 64)
493 /** jumping is only allowed if we're about to touch ground soon and if the
494 * button has been up in between the last jump
498 if ( (issolid(get_pos().x + bbox.get_width() / 2,
499 get_pos().y + bbox.get_height() + 64) ||
500 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
501 issolid(get_pos().x + bbox.get_width() - 1,
502 get_pos().y + bbox.get_height() + 64))
505 && input.jump && !input.old_jump)
513 Player::handle_input()
515 /* Handle horizontal movement: */
516 if (!backflipping) handle_horizontal_input();
518 if (backflip_direction == 0) {
519 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
521 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
522 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
527 if (on_ground() && !controller->hold(Controller::JUMP))
529 handle_vertical_input();
532 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
533 if(Sector::current()->add_bullet(
534 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
535 : Vector(32, bbox.get_height()/2)),
536 physic.get_velocity_x(), dir))
537 shooting_timer.start(SHOOTING_TIME);
541 if (controller->hold(Controller::DOWN) && is_big() && !duck
542 && physic.get_velocity_y() == 0 && on_ground()) {
544 bbox.move(Vector(0, 32));
545 bbox.set_height(31.8);
546 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
547 // if we have some velocity left then check if there is space for
549 bbox.move(Vector(0, -32));
550 bbox.set_height(63.8);
551 if(Sector::current()->is_free_space(bbox) || (
552 physic.get_velocity_x() > -.01 && physic.get_velocity_x() < .01
553 && physic.get_velocity_y() > -.01 && physic.get_velocity_y() < .01))
557 // undo the ducking changes
558 bbox.move(Vector(0, 32));
559 bbox.set_height(31.8);
565 Player::add_coins(int count)
567 player_status->add_coins(count);
571 Player::set_bonus(const std::string& bonustype)
573 if(bonustype == "grow")
574 set_bonus(GROWUP_BONUS);
575 else if(bonustype == "fireflower")
576 set_bonus(FIRE_BONUS);
577 else if(bonustype == "iceflower")
578 set_bonus(ICE_BONUS);
579 else if(bonustype == "none")
583 std::ostringstream msg;
584 msg << "Unknown bonus type " << bonustype;
585 throw std::runtime_error(msg.str());
589 Player::set_bonus(BonusType type, bool animate)
591 if(player_status->bonus >= type)
594 if(player_status->bonus == NO_BONUS) {
595 adjust_height = 62.8;
597 growing_timer.start(GROWING_TIME);
600 player_status->bonus = type;
604 Player::set_visible(bool visible)
606 this->visible = visible;
610 Player::get_visible()
618 kick_timer.start(KICK_TIME);
622 Player::draw(DrawingContext& context)
627 TuxBodyParts* tux_body;
629 if (player_status->bonus == GROWUP_BONUS)
631 else if (player_status->bonus == FIRE_BONUS)
633 else if (player_status->bonus == ICE_BONUS)
636 tux_body = small_tux;
638 int layer = LAYER_OBJECTS + 10;
640 /* Set Tux sprite action */
641 if (duck && is_big())
644 tux_body->set_action("duck-left");
646 tux_body->set_action("duck-right");
648 else if (skidding_timer.started() && !skidding_timer.check())
651 tux_body->set_action("skid-left");
653 tux_body->set_action("skid-right");
655 else if (kick_timer.started() && !kick_timer.check())
658 tux_body->set_action("kick-left");
660 tux_body->set_action("kick-right");
662 else if (butt_jump && is_big())
665 tux_body->set_action("buttjump-left");
667 tux_body->set_action("buttjump-right");
669 else if (!on_ground())
672 tux_body->set_action("jump-left");
674 tux_body->set_action("jump-right");
678 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
681 tux_body->set_action("stand-left");
683 tux_body->set_action("stand-right");
688 tux_body->set_action("walk-left");
690 tux_body->set_action("walk-right");
694 if(idle_timer.check())
699 tux_body->head->set_action("idle-left", 1);
701 tux_body->head->set_action("idle-right", 1);
706 // Tux is holding something
707 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
708 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
713 tux_body->arms->set_action("duck+grab-left");
715 tux_body->arms->set_action("duck+grab-right");
720 tux_body->arms->set_action("grab-left");
722 tux_body->arms->set_action("grab-right");
728 smalltux_gameover->draw(context, get_pos(), layer);
729 } else if(growing_timer.get_timeleft() > 0) {
731 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
732 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
734 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
735 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
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_tile(uint32_t tile_attributes)
759 if(tile_attributes & Tile::HURTS)
764 Player::collision(GameObject& other, const CollisionHit& hit)
766 Bullet* bullet = dynamic_cast<Bullet*> (&other);
771 if(other.get_flags() & FLAG_PORTABLE) {
772 Portable* portable = dynamic_cast<Portable*> (&other);
773 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
774 && fabsf(hit.normal.x) > .9) {
775 grabbed_object = portable;
780 if(other.get_flags() & FLAG_SOLID) {
782 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
784 hit.normal.x, hit.normal.y, hit.depth,
785 get_pos().x, get_pos().y,
786 movement.x, movement.y);
789 if(hit.normal.y < 0) { // landed on floor?
790 if(physic.get_velocity_y() < 0)
791 physic.set_velocity_y(0);
793 on_ground_flag = true;
795 // remember normal of this tile
796 if (hit.normal.y > -0.9) {
797 floor_normal.x = hit.normal.x;
798 floor_normal.y = hit.normal.y;
800 // slowly adjust to unisolid tiles.
801 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
802 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
803 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
806 // hack platforms so that we stand normally on them when going down...
807 Platform* platform = dynamic_cast<Platform*> (&other);
808 if(platform != NULL) {
809 if(platform->get_speed().y > 0)
810 physic.set_velocity_y(-platform->get_speed().y);
811 //physic.set_velocity_x(platform->get_speed().x);
813 } else if(hit.normal.y > 0) { // bumped against the roof
814 physic.set_velocity_y(.1);
816 // hack platform so that we are not glued to it from below
817 Platform* platform = dynamic_cast<Platform*> (&other);
818 if(platform != NULL) {
819 physic.set_velocity_y(-platform->get_speed().y);
823 if(fabsf(hit.normal.x) > .9) { // hit on the side?
824 physic.set_velocity_x(0);
827 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
829 Vector mov = movement - omov->get_movement();
831 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",
833 hit.normal.x, hit.normal.y,
835 movement.x, movement.y,
836 dest.p1.x, dest.p1.y,
837 omov->get_movement().x, omov->get_movement().y);
845 assert(dynamic_cast<MovingObject*> (&other) != NULL);
847 MovingObject* moving_object = static_cast<MovingObject*> (&other);
848 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
849 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
851 if(controller->pressed(Controller::UP))
852 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
858 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
860 if(safe_timer.started())
870 Player::make_invincible()
872 sound_manager->play("sounds/invincible.wav");
873 invincible_timer.start(TUX_INVINCIBLE_TIME);
874 Sector::current()->play_music(HERRING_MUSIC);
879 Player::kill(HurtMode mode)
881 if(dying || deactivated)
885 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
888 sound_manager->play("sounds/hurt.wav");
890 physic.set_velocity_x(0);
892 if (mode == SHRINK && is_big())
894 if (player_status->bonus == FIRE_BONUS
895 || player_status->bonus == ICE_BONUS)
897 safe_timer.start(TUX_SAFE_TIME);
898 player_status->bonus = GROWUP_BONUS;
902 //growing_timer.start(GROWING_TIME);
903 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
904 adjust_height = 30.8;
906 player_status->bonus = NO_BONUS;
913 for (i = 0; (i < 5) && (i < player_status->coins); i++)
915 // the numbers: starting x, starting y, velocity y
916 Sector::current()->add_object(new FallingCoin(get_pos() + Vector(rand()%5, rand()%50 - 32), rand()%200 - 100));
918 physic.enable_gravity(true);
919 physic.set_acceleration(0, 0);
920 physic.set_velocity(0, 700);
921 player_status->coins -= 25;
922 player_status->bonus = NO_BONUS;
924 dying_timer.start(3.0);
925 set_group(COLGROUP_DISABLED);
927 DisplayEffect* effect = new DisplayEffect();
928 effect->fade_out(3.0);
929 Sector::current()->add_object(effect);
930 sound_manager->stop_music(3.0);
935 Player::move(const Vector& vector)
937 bbox.set_pos(vector);
939 bbox.set_size(31.8, 63.8);
941 bbox.set_size(31.8, 31.8);
943 last_ground_y = vector.y;
949 Player::check_bounds(Camera* camera)
951 /* Keep tux in bounds: */
953 { // Lock Tux to the size of the level, so that he doesn't fall of
955 bbox.set_pos(Vector(0, get_pos().y));
958 /* Keep in-bounds, vertically: */
959 if (get_pos().y > Sector::current()->solids->get_height() * 32)
966 // can happen if back scrolling is disabled
967 if(get_pos().x < camera->get_translation().x) {
968 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
971 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
974 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
983 if(collision_object_map(bbox)) {
992 Player::add_velocity(const Vector& velocity)
994 physic.set_velocity(physic.get_velocity() + velocity);
998 Player::bounce(BadGuy& )
1000 if(controller->hold(Controller::JUMP))
1001 physic.set_velocity_y(520);
1003 physic.set_velocity_y(300);
1006 //Scripting Functions Below
1009 Player::deactivate()
1012 physic.set_velocity_x(0);
1013 physic.set_velocity_y(0);
1019 deactivated = false;
1022 void Player::walk(float speed)
1024 physic.set_velocity_x(speed);