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"
50 #include "random_generator.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+10);
101 feet->draw(context, pos, layer-2);
104 Player::Player(PlayerStatus* _player_status)
105 : player_status(_player_status), grabbed_object(0)
107 controller = main_controller;
108 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
109 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
110 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
117 delete smalltux_gameover;
118 delete smalltux_star;
126 bbox.set_size(31.8, 62.8);
128 bbox.set_size(31.8, 30.8);
138 fall_mode = ON_GROUND;
143 backflipping = false;
144 backflip_direction = 0;
147 on_ground_flag = false;
150 floor_normal = Vector(0,-1);
156 Player::expose(HSQUIRRELVM vm, int table_idx)
158 Scripting::Player* interface = static_cast<Scripting::Player*> (this);
159 Scripting::expose_object(vm, table_idx, interface, "Tux", false);
163 Player::unexpose(HSQUIRRELVM vm, int table_idx)
165 Scripting::unexpose_object(vm, table_idx, "Tux");
169 Player::set_controller(Controller* controller)
171 this->controller = controller;
175 Player::update(float elapsed_time)
177 if(dying && dying_timer.check()) {
182 if(adjust_height != 0) {
183 bbox.move(Vector(0, bbox.get_height() - adjust_height));
184 bbox.set_height(adjust_height);
188 if(!controller->hold(Controller::ACTION) && grabbed_object) {
189 // move the grabbed object a bit away from tux
190 Vector pos = get_pos() +
191 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
192 bbox.get_height()*0.66666 - 32);
193 Rect dest(pos, pos + Vector(32, 32));
194 if(Sector::current()->is_free_space(dest)) {
195 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
197 moving_object->set_pos(pos);
199 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
201 grabbed_object->ungrab(*this, dir);
206 if(!dying && !deactivated)
209 movement = physic.get_movement(elapsed_time);
212 // special exception for cases where we're stuck under tiles after
213 // being ducked. In this case we drift out
214 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
215 && collision_object_map(base)) {
216 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
217 previous_base = old_base = base;
221 if(grabbed_object != 0) {
222 Vector pos = get_pos() +
223 Vector(dir == LEFT ? -16 : 16,
224 bbox.get_height()*0.66666 - 32);
225 grabbed_object->grab(*this, pos, dir);
228 on_ground_flag = false;
234 return on_ground_flag;
240 if(player_status->bonus == NO_BONUS)
247 Player::handle_horizontal_input()
249 float vx = physic.get_velocity_x();
250 float vy = physic.get_velocity_y();
251 float ax = physic.get_acceleration_x();
252 float ay = physic.get_acceleration_y();
255 if(!duck || physic.get_velocity_y() != 0) {
256 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
260 } else if(!controller->hold(Controller::LEFT)
261 && controller->hold(Controller::RIGHT)) {
268 if (!controller->hold(Controller::ACTION)) {
269 ax = dirsign * WALK_ACCELERATION_X;
271 if(vx >= MAX_WALK_XM && dirsign > 0) {
274 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
279 ax = dirsign * RUN_ACCELERATION_X;
281 if(vx >= MAX_RUN_XM && dirsign > 0) {
284 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
290 // we can reach WALK_SPEED without any acceleration
291 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
292 vx = dirsign * WALK_SPEED;
295 // changing directions?
296 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
298 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
299 skidding_timer.start(SKID_TIME);
300 sound_manager->play("sounds/skid.wav");
301 // dust some particles
302 Sector::current()->add_object(
304 Vector(dir == RIGHT ? bbox.p2.x : bbox.p1.x, bbox.p2.y),
305 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
306 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
315 // we get slower when not pressing any keys
317 if(fabs(vx) < WALK_SPEED) {
321 ax = WALK_ACCELERATION_X * 1.5;
323 ax = WALK_ACCELERATION_X * -1.5;
328 // if we're on ice slow down acceleration or deceleration
329 if (isice(base.x, base.y + base.height))
331 /* the acceleration/deceleration rate on ice is inversely proportional to
332 * the current velocity.
335 // increasing 1 will increase acceleration/deceleration rate
336 // decreasing 1 will decrease acceleration/deceleration rate
337 // must stay above zero, though
338 if (ax != 0) ax *= 1 / fabs(vx);
342 // extend/shrink tux collision rectangle so that we fall through/walk over 1
344 if(fabsf(vx) > MAX_WALK_XM) {
347 bbox.set_width(31.8);
350 // on downward slopes, adjust vertical velocity to match slope angle
352 if (floor_normal.y != 0) {
353 if ((floor_normal.x * vx) > 0) {
354 // we overdo it a little, just to be on the safe side
355 vy = vx * (floor_normal.x / floor_normal.y) * 2;
360 physic.set_velocity(vx, vy);
361 physic.set_acceleration(ax, ay);
365 Player::handle_vertical_input()
369 fall_mode = ON_GROUND;
370 last_ground_y = get_pos().y;
372 if(get_pos().y > last_ground_y)
374 else if(fall_mode == ON_GROUND)
378 if(on_ground()) { /* Make sure jumping is off. */
381 backflipping = false;
382 backflip_direction = 0;
387 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
389 if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
390 physic.set_velocity_y(300);
391 else { //do a backflip
393 physic.set_velocity_y(580);
394 backflip_timer.start(0.15);
397 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
398 physic.set_velocity_y(580);
400 physic.set_velocity_y(520);
402 //bbox.move(Vector(0, -1));
406 sound_manager->play("sounds/bigjump.wav");
408 sound_manager->play("sounds/jump.wav");
409 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
410 if (!backflipping && jumping && physic.get_velocity_y() > 0) {
412 physic.set_velocity_y(0);
416 /* In case the player has pressed Down while in a certain range of air,
417 enable butt jump action */
418 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
419 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
422 /* When Down is not held anymore, disable butt jump */
423 if(butt_jump && !controller->hold(Controller::DOWN))
428 if (butt_jump && on_ground() && is_big()) {
431 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
433 Sector::current()->add_smoke_cloud(
434 Vector(get_pos().x - 32, get_pos().y + 32));
438 // Break bricks beneath Tux
439 if(Sector::current()->trybreakbrick(
440 Vector(base.x + 1, base.y + base.height), false)
441 || Sector::current()->trybreakbrick(
442 Vector(base.x + base.width - 1, base.y + base.height), false)) {
443 physic.set_velocity_y(2);
447 // Kill nearby badguys
448 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
449 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
450 i != gameobjects.end();
452 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
454 // don't kill when badguys are already dying or in a certain mode
455 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
456 badguy->mode != BadGuy::BOMB_EXPLODE) {
457 if (fabsf(base.x - badguy->base.x) < 96 &&
458 fabsf(base.y - badguy->base.y) < 64)
466 /** jumping is only allowed if we're about to touch ground soon and if the
467 * button has been up in between the last jump
471 if ( (issolid(get_pos().x + bbox.get_width() / 2,
472 get_pos().y + bbox.get_height() + 64) ||
473 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
474 issolid(get_pos().x + bbox.get_width() - 1,
475 get_pos().y + bbox.get_height() + 64))
478 && input.jump && !input.old_jump)
486 Player::handle_input()
488 /* Handle horizontal movement: */
489 if (!backflipping) handle_horizontal_input();
491 if (backflip_direction == 0) {
492 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
494 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
495 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
500 if (on_ground() && !controller->hold(Controller::JUMP))
502 handle_vertical_input();
505 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
506 if(Sector::current()->add_bullet(
507 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
508 : Vector(32, bbox.get_height()/2)),
509 physic.get_velocity_x(), dir))
510 shooting_timer.start(SHOOTING_TIME);
514 if (controller->hold(Controller::DOWN) && is_big() && !duck
515 && physic.get_velocity_y() == 0 && on_ground()) {
517 bbox.move(Vector(0, 32));
518 bbox.set_height(31.8);
519 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
520 // if we have some velocity left then check if there is space for
522 bbox.move(Vector(0, -32));
523 bbox.set_height(63.8);
524 if(Sector::current()->is_free_space(bbox) || (
525 physic.get_velocity_x() > -.01 && physic.get_velocity_x() < .01
526 && physic.get_velocity_y() > -.01 && physic.get_velocity_y() < .01))
530 // undo the ducking changes
531 bbox.move(Vector(0, 32));
532 bbox.set_height(31.8);
538 Player::add_coins(int count)
540 player_status->add_coins(count);
544 Player::add_bonus(const std::string& bonustype)
546 if(bonustype == "grow") {
547 add_bonus(GROWUP_BONUS);
548 } else if(bonustype == "fireflower") {
549 add_bonus(FIRE_BONUS);
550 } else if(bonustype == "iceflower") {
551 add_bonus(ICE_BONUS);
552 } else if(bonustype == "none") {
555 std::ostringstream msg;
556 msg << "Unknown bonus type " << bonustype;
557 throw std::runtime_error(msg.str());
562 Player::add_bonus(BonusType type, bool animate)
564 // always ignore NO_BONUS
565 if (type == NO_BONUS) {
569 // ignore GROWUP_BONUS if we're already big
570 if (type == GROWUP_BONUS) {
571 if (player_status->bonus == GROWUP_BONUS) return;
572 if (player_status->bonus == FIRE_BONUS) return;
573 if (player_status->bonus == ICE_BONUS) return;
576 set_bonus(type, animate);
580 Player::set_bonus(BonusType type, bool animate)
582 if(player_status->bonus == NO_BONUS) {
583 adjust_height = 62.8;
585 growing_timer.start(GROWING_TIME);
588 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
589 player_status->max_fire_bullets = 0;
590 player_status->max_ice_bullets = 0;
592 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
593 if (type == ICE_BONUS) player_status->max_ice_bullets++;
595 player_status->bonus = type;
599 Player::set_visible(bool visible)
601 this->visible = visible;
605 Player::get_visible()
613 kick_timer.start(KICK_TIME);
617 Player::draw(DrawingContext& context)
622 TuxBodyParts* tux_body;
624 if (player_status->bonus == GROWUP_BONUS)
626 else if (player_status->bonus == FIRE_BONUS)
628 else if (player_status->bonus == ICE_BONUS)
631 tux_body = small_tux;
633 int layer = LAYER_OBJECTS + 1;
635 /* Set Tux sprite action */
636 if (duck && is_big())
639 tux_body->set_action("duck-left");
641 tux_body->set_action("duck-right");
643 else if (skidding_timer.started() && !skidding_timer.check())
646 tux_body->set_action("skid-left");
648 tux_body->set_action("skid-right");
650 else if (kick_timer.started() && !kick_timer.check())
653 tux_body->set_action("kick-left");
655 tux_body->set_action("kick-right");
657 else if (butt_jump && is_big())
660 tux_body->set_action("buttjump-left");
662 tux_body->set_action("buttjump-right");
664 else if (!on_ground())
667 tux_body->set_action("jump-left");
669 tux_body->set_action("jump-right");
673 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
676 tux_body->set_action("stand-left");
678 tux_body->set_action("stand-right");
683 tux_body->set_action("walk-left");
685 tux_body->set_action("walk-right");
689 if(idle_timer.check())
694 tux_body->head->set_action("idle-left", 1);
696 tux_body->head->set_action("idle-right", 1);
701 // Tux is holding something
702 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
703 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
708 tux_body->arms->set_action("duck+grab-left");
710 tux_body->arms->set_action("duck+grab-right");
715 tux_body->arms->set_action("grab-left");
717 tux_body->arms->set_action("grab-right");
723 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
724 } else if(growing_timer.get_timeleft() > 0) {
726 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
727 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
729 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
730 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
733 else if (safe_timer.started() && size_t(game_time*40)%2)
736 tux_body->draw(context, get_pos(), layer);
738 // Draw blinking star overlay
739 if (invincible_timer.started() &&
740 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
741 || size_t(game_time*20)%2)
744 if (!is_big() || duck)
745 smalltux_star->draw(context, get_pos(), layer + 5);
747 bigtux_star->draw(context, get_pos(), layer + 5);
752 Player::collision_tile(uint32_t tile_attributes)
754 if(tile_attributes & Tile::HURTS)
759 Player::collision(GameObject& other, const CollisionHit& hit)
761 Bullet* bullet = dynamic_cast<Bullet*> (&other);
766 if(other.get_flags() & FLAG_PORTABLE) {
767 Portable* portable = dynamic_cast<Portable*> (&other);
768 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
769 && fabsf(hit.normal.x) > .9) {
770 grabbed_object = portable;
775 if(other.get_flags() & FLAG_SOLID) {
777 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
779 hit.normal.x, hit.normal.y, hit.depth,
780 get_pos().x, get_pos().y,
781 movement.x, movement.y);
784 if(hit.normal.y < 0) { // landed on floor?
785 if(physic.get_velocity_y() < 0)
786 physic.set_velocity_y(0);
788 on_ground_flag = true;
790 // remember normal of this tile
791 if (hit.normal.y > -0.9) {
792 floor_normal.x = hit.normal.x;
793 floor_normal.y = hit.normal.y;
795 // slowly adjust to unisolid tiles.
796 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
797 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
798 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
801 // hack platforms so that we stand normally on them when going down...
802 Platform* platform = dynamic_cast<Platform*> (&other);
803 if(platform != NULL) {
804 if(platform->get_speed().y > 0)
805 physic.set_velocity_y(-platform->get_speed().y);
806 //physic.set_velocity_x(platform->get_speed().x);
808 } else if(hit.normal.y > 0) { // bumped against the roof
809 physic.set_velocity_y(.1);
811 // hack platform so that we are not glued to it from below
812 Platform* platform = dynamic_cast<Platform*> (&other);
813 if(platform != NULL) {
814 physic.set_velocity_y(-platform->get_speed().y);
818 if(fabsf(hit.normal.x) > .9) { // hit on the side?
819 physic.set_velocity_x(0);
822 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
824 Vector mov = movement - omov->get_movement();
826 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",
828 hit.normal.x, hit.normal.y,
830 movement.x, movement.y,
831 dest.p1.x, dest.p1.y,
832 omov->get_movement().x, omov->get_movement().y);
840 assert(dynamic_cast<MovingObject*> (&other) != NULL);
842 MovingObject* moving_object = static_cast<MovingObject*> (&other);
843 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
844 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
846 if(controller->pressed(Controller::UP))
847 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
853 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
855 if(safe_timer.started())
865 Player::make_invincible()
867 sound_manager->play("sounds/invincible.wav");
868 invincible_timer.start(TUX_INVINCIBLE_TIME);
869 Sector::current()->play_music(HERRING_MUSIC);
874 Player::kill(bool completely)
876 if(dying || deactivated)
880 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
883 sound_manager->play("sounds/hurt.wav");
885 physic.set_velocity_x(0);
887 if(!completely && is_big()) {
888 if(player_status->bonus == FIRE_BONUS
889 || player_status->bonus == ICE_BONUS) {
890 safe_timer.start(TUX_SAFE_TIME);
891 set_bonus(GROWUP_BONUS);
893 //growing_timer.start(GROWING_TIME);
894 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
895 adjust_height = 30.8;
900 for (int i = 0; (i < 5) && (i < player_status->coins); i++)
902 // the numbers: starting x, starting y, velocity y
903 Sector::current()->add_object(new FallingCoin(get_pos() +
904 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
905 systemRandom.rand(-100,100)));
907 physic.enable_gravity(true);
908 physic.set_acceleration(0, 0);
909 physic.set_velocity(0, 700);
910 player_status->coins -= 25;
913 dying_timer.start(3.0);
914 set_group(COLGROUP_DISABLED);
916 DisplayEffect* effect = new DisplayEffect();
917 effect->fade_out(3.0);
918 Sector::current()->add_object(effect);
919 sound_manager->stop_music(3.0);
924 Player::move(const Vector& vector)
926 bbox.set_pos(vector);
928 bbox.set_size(31.8, 63.8);
930 bbox.set_size(31.8, 31.8);
932 last_ground_y = vector.y;
938 Player::check_bounds(Camera* camera)
940 /* Keep tux in bounds: */
941 if (get_pos().x < 0) {
942 // Lock Tux to the size of the level, so that he doesn't fall of
944 bbox.set_pos(Vector(0, get_pos().y));
947 /* Keep in-bounds, vertically: */
948 if (get_pos().y > Sector::current()->solids->get_height() * 32) {
954 // can happen if back scrolling is disabled
955 if(get_pos().x < camera->get_translation().x) {
956 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
959 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
962 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
971 if(collision_object_map(bbox)) {
980 Player::add_velocity(const Vector& velocity)
982 physic.set_velocity(physic.get_velocity() + velocity);
986 Player::bounce(BadGuy& )
988 if(controller->hold(Controller::JUMP))
989 physic.set_velocity_y(520);
991 physic.set_velocity_y(300);
994 //Scripting Functions Below
1000 physic.set_velocity_x(0);
1001 physic.set_velocity_y(0);
1007 deactivated = false;
1010 void Player::walk(float speed)
1012 physic.set_velocity_x(speed);