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.
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 "statistics.hpp"
35 #include "game_session.hpp"
36 #include "object/tilemap.hpp"
37 #include "object/camera.hpp"
38 #include "object/particles.hpp"
39 #include "object/portable.hpp"
40 #include "object/bullet.hpp"
41 #include "trigger/trigger_base.hpp"
42 #include "control/joystickkeyboardcontroller.hpp"
43 #include "scripting/squirrel_util.hpp"
45 #include "platform.hpp"
46 #include "badguy/badguy.hpp"
47 #include "player_status.hpp"
49 #include "falling_coin.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+10);
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, 62.8);
127 bbox.set_size(31.8, 30.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 log_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::add_coins(int count)
539 player_status->add_coins(count);
543 Player::add_bonus(const std::string& bonustype)
545 if(bonustype == "grow")
546 add_bonus(GROWUP_BONUS);
547 else if(bonustype == "fireflower")
548 add_bonus(FIRE_BONUS);
549 else if(bonustype == "iceflower")
550 add_bonus(ICE_BONUS);
551 else if(bonustype == "none")
555 std::ostringstream msg;
556 msg << "Unknown bonus type " << bonustype;
557 throw std::runtime_error(msg.str());
561 Player::add_bonus(BonusType type, bool animate)
563 // always ignore NO_BONUS
564 if (type == NO_BONUS) {
568 // ignore GROWUP_BONUS if we're already big
569 if (type == GROWUP_BONUS) {
570 if (player_status->bonus == GROWUP_BONUS) return;
571 if (player_status->bonus == FIRE_BONUS) return;
572 if (player_status->bonus == ICE_BONUS) return;
575 set_bonus(type, animate);
579 Player::set_bonus(BonusType type, bool animate)
581 if(player_status->bonus == NO_BONUS) {
582 adjust_height = 62.8;
584 growing_timer.start(GROWING_TIME);
587 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
588 player_status->max_fire_bullets = 0;
589 player_status->max_ice_bullets = 0;
591 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
592 if (type == ICE_BONUS) player_status->max_ice_bullets++;
594 player_status->bonus = type;
598 Player::set_visible(bool visible)
600 this->visible = visible;
604 Player::get_visible()
612 kick_timer.start(KICK_TIME);
616 Player::draw(DrawingContext& context)
621 TuxBodyParts* tux_body;
623 if (player_status->bonus == GROWUP_BONUS)
625 else if (player_status->bonus == FIRE_BONUS)
627 else if (player_status->bonus == ICE_BONUS)
630 tux_body = small_tux;
632 int layer = LAYER_OBJECTS + 1;
634 /* Set Tux sprite action */
635 if (duck && is_big())
638 tux_body->set_action("duck-left");
640 tux_body->set_action("duck-right");
642 else if (skidding_timer.started() && !skidding_timer.check())
645 tux_body->set_action("skid-left");
647 tux_body->set_action("skid-right");
649 else if (kick_timer.started() && !kick_timer.check())
652 tux_body->set_action("kick-left");
654 tux_body->set_action("kick-right");
656 else if (butt_jump && is_big())
659 tux_body->set_action("buttjump-left");
661 tux_body->set_action("buttjump-right");
663 else if (!on_ground())
666 tux_body->set_action("jump-left");
668 tux_body->set_action("jump-right");
672 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
675 tux_body->set_action("stand-left");
677 tux_body->set_action("stand-right");
682 tux_body->set_action("walk-left");
684 tux_body->set_action("walk-right");
688 if(idle_timer.check())
693 tux_body->head->set_action("idle-left", 1);
695 tux_body->head->set_action("idle-right", 1);
700 // Tux is holding something
701 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
702 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
707 tux_body->arms->set_action("duck+grab-left");
709 tux_body->arms->set_action("duck+grab-right");
714 tux_body->arms->set_action("grab-left");
716 tux_body->arms->set_action("grab-right");
722 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
723 } else if(growing_timer.get_timeleft() > 0) {
725 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
726 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
728 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
729 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
732 else if (safe_timer.started() && size_t(game_time*40)%2)
735 tux_body->draw(context, get_pos(), layer);
737 // Draw blinking star overlay
738 if (invincible_timer.started() &&
739 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
740 || size_t(game_time*20)%2)
743 if (!is_big() || duck)
744 smalltux_star->draw(context, get_pos(), layer + 5);
746 bigtux_star->draw(context, get_pos(), layer + 5);
751 Player::collision_tile(uint32_t tile_attributes)
753 if(tile_attributes & Tile::HURTS)
758 Player::collision(GameObject& other, const CollisionHit& hit)
760 Bullet* bullet = dynamic_cast<Bullet*> (&other);
765 if(other.get_flags() & FLAG_PORTABLE) {
766 Portable* portable = dynamic_cast<Portable*> (&other);
767 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
768 && fabsf(hit.normal.x) > .9) {
769 grabbed_object = portable;
774 if(other.get_flags() & FLAG_SOLID) {
776 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
778 hit.normal.x, hit.normal.y, hit.depth,
779 get_pos().x, get_pos().y,
780 movement.x, movement.y);
783 if(hit.normal.y < 0) { // landed on floor?
784 if(physic.get_velocity_y() < 0)
785 physic.set_velocity_y(0);
787 on_ground_flag = true;
789 // remember normal of this tile
790 if (hit.normal.y > -0.9) {
791 floor_normal.x = hit.normal.x;
792 floor_normal.y = hit.normal.y;
794 // slowly adjust to unisolid tiles.
795 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
796 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
797 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
800 // hack platforms so that we stand normally on them when going down...
801 Platform* platform = dynamic_cast<Platform*> (&other);
802 if(platform != NULL) {
803 if(platform->get_speed().y > 0)
804 physic.set_velocity_y(-platform->get_speed().y);
805 //physic.set_velocity_x(platform->get_speed().x);
807 } else if(hit.normal.y > 0) { // bumped against the roof
808 physic.set_velocity_y(.1);
810 // hack platform so that we are not glued to it from below
811 Platform* platform = dynamic_cast<Platform*> (&other);
812 if(platform != NULL) {
813 physic.set_velocity_y(-platform->get_speed().y);
817 if(fabsf(hit.normal.x) > .9) { // hit on the side?
818 physic.set_velocity_x(0);
821 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
823 Vector mov = movement - omov->get_movement();
825 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",
827 hit.normal.x, hit.normal.y,
829 movement.x, movement.y,
830 dest.p1.x, dest.p1.y,
831 omov->get_movement().x, omov->get_movement().y);
839 assert(dynamic_cast<MovingObject*> (&other) != NULL);
841 MovingObject* moving_object = static_cast<MovingObject*> (&other);
842 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
843 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
845 if(controller->pressed(Controller::UP))
846 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
852 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
854 if(safe_timer.started())
864 Player::make_invincible()
866 sound_manager->play("sounds/invincible.wav");
867 invincible_timer.start(TUX_INVINCIBLE_TIME);
868 Sector::current()->play_music(HERRING_MUSIC);
873 Player::kill(HurtMode mode)
875 if(dying || deactivated)
879 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
882 sound_manager->play("sounds/hurt.wav");
884 physic.set_velocity_x(0);
886 if (mode == SHRINK && is_big())
888 if (player_status->bonus == FIRE_BONUS
889 || player_status->bonus == ICE_BONUS)
891 safe_timer.start(TUX_SAFE_TIME);
892 set_bonus(GROWUP_BONUS);
896 //growing_timer.start(GROWING_TIME);
897 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
898 adjust_height = 30.8;
907 for (i = 0; (i < 5) && (i < player_status->coins); i++)
909 // the numbers: starting x, starting y, velocity y
910 Sector::current()->add_object(new FallingCoin(get_pos() + Vector(rand()%5, rand()%50 - 32), rand()%200 - 100));
912 physic.enable_gravity(true);
913 physic.set_acceleration(0, 0);
914 physic.set_velocity(0, 700);
915 player_status->coins -= 25;
918 dying_timer.start(3.0);
919 set_group(COLGROUP_DISABLED);
921 DisplayEffect* effect = new DisplayEffect();
922 effect->fade_out(3.0);
923 Sector::current()->add_object(effect);
924 sound_manager->stop_music(3.0);
929 Player::move(const Vector& vector)
931 bbox.set_pos(vector);
933 bbox.set_size(31.8, 63.8);
935 bbox.set_size(31.8, 31.8);
937 last_ground_y = vector.y;
943 Player::check_bounds(Camera* camera)
945 /* Keep tux in bounds: */
947 { // Lock Tux to the size of the level, so that he doesn't fall of
949 bbox.set_pos(Vector(0, get_pos().y));
952 /* Keep in-bounds, vertically: */
953 if (get_pos().y > Sector::current()->solids->get_height() * 32)
960 // can happen if back scrolling is disabled
961 if(get_pos().x < camera->get_translation().x) {
962 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
965 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
968 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
977 if(collision_object_map(bbox)) {
986 Player::add_velocity(const Vector& velocity)
988 physic.set_velocity(physic.get_velocity() + velocity);
992 Player::bounce(BadGuy& )
994 if(controller->hold(Controller::JUMP))
995 physic.set_velocity_y(520);
997 physic.set_velocity_y(300);
1000 //Scripting Functions Below
1003 Player::deactivate()
1006 physic.set_velocity_x(0);
1007 physic.set_velocity_y(0);
1013 deactivated = false;
1016 void Player::walk(float speed)
1018 physic.set_velocity_x(speed);