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"
51 #include "falling_coin.hpp"
53 static const int TILES_FOR_BUTTJUMP = 3;
54 static const float SHOOTING_TIME = .150;
55 /// time before idle animation starts
56 static const float IDLE_TIME = 2.5;
58 static const float WALK_ACCELERATION_X = 300;
59 static const float RUN_ACCELERATION_X = 400;
60 static const float SKID_XM = 200;
61 static const float SKID_TIME = .3;
62 static const float MAX_WALK_XM = 230;
63 static const float MAX_RUN_XM = 320;
64 static const float WALK_SPEED = 100;
66 static const float KICK_TIME = .3;
69 Surface* growingtux_left[GROWING_FRAMES];
70 Surface* growingtux_right[GROWING_FRAMES];
72 Surface* tux_life = 0;
74 TuxBodyParts* small_tux = 0;
75 TuxBodyParts* big_tux = 0;
76 TuxBodyParts* fire_tux = 0;
77 TuxBodyParts* ice_tux = 0;
80 TuxBodyParts::set_action(std::string action, int loops)
83 head->set_action(action, loops);
85 body->set_action(action, loops);
87 arms->set_action(action, loops);
89 feet->set_action(action, loops);
93 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
96 head->draw(context, pos, layer-1);
98 body->draw(context, pos, layer-3);
100 arms->draw(context, pos, layer);
102 feet->draw(context, pos, layer-2);
105 Player::Player(PlayerStatus* _player_status)
106 : player_status(_player_status), grabbed_object(0)
108 controller = main_controller;
109 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
110 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
111 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
118 delete smalltux_gameover;
119 delete smalltux_star;
127 bbox.set_size(31.8, 62.8);
129 bbox.set_size(31.8, 30.8);
139 fall_mode = ON_GROUND;
144 backflipping = false;
145 backflip_direction = 0;
148 on_ground_flag = false;
151 floor_normal = Vector(0,-1);
157 Player::expose(HSQUIRRELVM vm, int table_idx)
159 Scripting::Player* interface = static_cast<Scripting::Player*> (this);
160 expose_object(vm, table_idx, interface, "Tux", false);
164 Player::unexpose(HSQUIRRELVM vm, int table_idx)
166 Scripting::unexpose_object(vm, table_idx, "Tux");
170 Player::set_controller(Controller* controller)
172 this->controller = controller;
176 Player::update(float elapsed_time)
178 if(dying && dying_timer.check()) {
183 if(adjust_height != 0) {
184 bbox.move(Vector(0, bbox.get_height() - adjust_height));
185 bbox.set_height(adjust_height);
189 if(!controller->hold(Controller::ACTION) && grabbed_object) {
190 // move the grabbed object a bit away from tux
191 Vector pos = get_pos() +
192 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
193 bbox.get_height()*0.66666 - 32);
194 Rect dest(pos, pos + Vector(32, 32));
195 if(Sector::current()->is_free_space(dest)) {
196 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
198 moving_object->set_pos(pos);
200 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
202 grabbed_object->ungrab(*this, dir);
207 if(!dying && !deactivated)
210 movement = physic.get_movement(elapsed_time);
213 // special exception for cases where we're stuck under tiles after
214 // being ducked. In this case we drift out
215 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
216 && collision_object_map(base)) {
217 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
218 previous_base = old_base = base;
222 if(grabbed_object != 0) {
223 Vector pos = get_pos() +
224 Vector(dir == LEFT ? -16 : 16,
225 bbox.get_height()*0.66666 - 32);
226 grabbed_object->grab(*this, pos, dir);
229 on_ground_flag = false;
235 return on_ground_flag;
241 if(player_status->bonus == NO_BONUS)
248 Player::handle_horizontal_input()
250 float vx = physic.get_velocity_x();
251 float vy = physic.get_velocity_y();
252 float ax = physic.get_acceleration_x();
253 float ay = physic.get_acceleration_y();
256 if(!duck || physic.get_velocity_y() != 0) {
257 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
261 } else if(!controller->hold(Controller::LEFT)
262 && controller->hold(Controller::RIGHT)) {
269 if (!controller->hold(Controller::ACTION)) {
270 ax = dirsign * WALK_ACCELERATION_X;
272 if(vx >= MAX_WALK_XM && dirsign > 0) {
275 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
280 ax = dirsign * RUN_ACCELERATION_X;
282 if(vx >= MAX_RUN_XM && dirsign > 0) {
285 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
291 // we can reach WALK_SPEED without any acceleration
292 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
293 vx = dirsign * WALK_SPEED;
296 // changing directions?
297 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
299 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
300 skidding_timer.start(SKID_TIME);
301 sound_manager->play("sounds/skid.wav");
302 // dust some particles
303 Sector::current()->add_object(
305 Vector(dir == RIGHT ? bbox.p2.x : bbox.p1.x, bbox.p2.y),
306 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
307 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
316 // we get slower when not pressing any keys
318 if(fabs(vx) < WALK_SPEED) {
322 ax = WALK_ACCELERATION_X * 1.5;
324 ax = WALK_ACCELERATION_X * -1.5;
329 // if we're on ice slow down acceleration or deceleration
330 if (isice(base.x, base.y + base.height))
332 /* the acceleration/deceleration rate on ice is inversely proportional to
333 * the current velocity.
336 // increasing 1 will increase acceleration/deceleration rate
337 // decreasing 1 will decrease acceleration/deceleration rate
338 // must stay above zero, though
339 if (ax != 0) ax *= 1 / fabs(vx);
343 // extend/shrink tux collision rectangle so that we fall through/walk over 1
345 if(fabsf(vx) > MAX_WALK_XM) {
348 bbox.set_width(31.8);
351 // on downward slopes, adjust vertical velocity to match slope angle
353 if (floor_normal.y != 0) {
354 if ((floor_normal.x * vx) > 0) {
355 // we overdo it a little, just to be on the safe side
356 vy = vx * (floor_normal.x / floor_normal.y) * 2;
361 physic.set_velocity(vx, vy);
362 physic.set_acceleration(ax, ay);
366 Player::handle_vertical_input()
370 fall_mode = ON_GROUND;
371 last_ground_y = get_pos().y;
373 if(get_pos().y > last_ground_y)
375 else if(fall_mode == ON_GROUND)
379 if(on_ground()) { /* Make sure jumping is off. */
382 backflipping = false;
383 backflip_direction = 0;
388 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
390 if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
391 physic.set_velocity_y(300);
392 else { //do a backflip
394 physic.set_velocity_y(580);
395 backflip_timer.start(0.15);
398 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
399 physic.set_velocity_y(580);
401 physic.set_velocity_y(520);
403 //bbox.move(Vector(0, -1));
407 sound_manager->play("sounds/bigjump.wav");
409 sound_manager->play("sounds/jump.wav");
410 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
411 if (!backflipping && jumping && physic.get_velocity_y() > 0) {
413 physic.set_velocity_y(0);
417 /* In case the player has pressed Down while in a certain range of air,
418 enable butt jump action */
419 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
420 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
423 /* When Down is not held anymore, disable butt jump */
424 if(butt_jump && !controller->hold(Controller::DOWN))
429 if (butt_jump && on_ground() && is_big()) {
432 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
434 Sector::current()->add_smoke_cloud(
435 Vector(get_pos().x - 32, get_pos().y + 32));
439 // Break bricks beneath Tux
440 if(Sector::current()->trybreakbrick(
441 Vector(base.x + 1, base.y + base.height), false)
442 || Sector::current()->trybreakbrick(
443 Vector(base.x + base.width - 1, base.y + base.height), false)) {
444 physic.set_velocity_y(2);
448 // Kill nearby badguys
449 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
450 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
451 i != gameobjects.end();
453 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
455 // don't kill when badguys are already dying or in a certain mode
456 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
457 badguy->mode != BadGuy::BOMB_EXPLODE) {
458 if (fabsf(base.x - badguy->base.x) < 96 &&
459 fabsf(base.y - badguy->base.y) < 64)
467 /** jumping is only allowed if we're about to touch ground soon and if the
468 * button has been up in between the last jump
472 if ( (issolid(get_pos().x + bbox.get_width() / 2,
473 get_pos().y + bbox.get_height() + 64) ||
474 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
475 issolid(get_pos().x + bbox.get_width() - 1,
476 get_pos().y + bbox.get_height() + 64))
479 && input.jump && !input.old_jump)
487 Player::handle_input()
489 /* Handle horizontal movement: */
490 if (!backflipping) handle_horizontal_input();
492 if (backflip_direction == 0) {
493 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
495 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
496 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
501 if (on_ground() && !controller->hold(Controller::JUMP))
503 handle_vertical_input();
506 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
507 if(Sector::current()->add_bullet(
508 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
509 : Vector(32, bbox.get_height()/2)),
510 physic.get_velocity_x(), dir))
511 shooting_timer.start(SHOOTING_TIME);
515 if (controller->hold(Controller::DOWN) && is_big() && !duck
516 && physic.get_velocity_y() == 0 && on_ground()) {
518 bbox.move(Vector(0, 32));
519 bbox.set_height(31.8);
520 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
521 // if we have some velocity left then check if there is space for
523 bbox.move(Vector(0, -32));
524 bbox.set_height(63.8);
525 if(Sector::current()->is_free_space(bbox) || (
526 physic.get_velocity_x() > -.01 && physic.get_velocity_x() < .01
527 && physic.get_velocity_y() > -.01 && physic.get_velocity_y() < .01))
531 // undo the ducking changes
532 bbox.move(Vector(0, 32));
533 bbox.set_height(31.8);
539 Player::add_coins(int count)
541 player_status->add_coins(count);
545 Player::set_bonus(const std::string& bonustype)
547 if(bonustype == "grow")
548 set_bonus(GROWUP_BONUS);
549 else if(bonustype == "fireflower")
550 set_bonus(FIRE_BONUS);
551 else if(bonustype == "iceflower")
552 set_bonus(ICE_BONUS);
553 else if(bonustype == "none")
557 std::ostringstream msg;
558 msg << "Unknown bonus type " << bonustype;
559 throw std::runtime_error(msg.str());
563 Player::set_bonus(BonusType type, bool animate)
565 if(player_status->bonus >= type)
568 if(player_status->bonus == NO_BONUS) {
569 adjust_height = 62.8;
571 growing_timer.start(GROWING_TIME);
574 player_status->bonus = type;
578 Player::set_visible(bool visible)
580 this->visible = visible;
584 Player::get_visible()
592 kick_timer.start(KICK_TIME);
596 Player::draw(DrawingContext& context)
601 TuxBodyParts* tux_body;
603 if (player_status->bonus == GROWUP_BONUS)
605 else if (player_status->bonus == FIRE_BONUS)
607 else if (player_status->bonus == ICE_BONUS)
610 tux_body = small_tux;
612 int layer = LAYER_OBJECTS + 10;
614 /* Set Tux sprite action */
615 if (duck && is_big())
618 tux_body->set_action("duck-left");
620 tux_body->set_action("duck-right");
622 else if (skidding_timer.started() && !skidding_timer.check())
625 tux_body->set_action("skid-left");
627 tux_body->set_action("skid-right");
629 else if (kick_timer.started() && !kick_timer.check())
632 tux_body->set_action("kick-left");
634 tux_body->set_action("kick-right");
636 else if (butt_jump && is_big())
639 tux_body->set_action("buttjump-left");
641 tux_body->set_action("buttjump-right");
643 else if (!on_ground())
646 tux_body->set_action("jump-left");
648 tux_body->set_action("jump-right");
652 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
655 tux_body->set_action("stand-left");
657 tux_body->set_action("stand-right");
662 tux_body->set_action("walk-left");
664 tux_body->set_action("walk-right");
668 if(idle_timer.check())
673 tux_body->head->set_action("idle-left", 1);
675 tux_body->head->set_action("idle-right", 1);
680 // Tux is holding something
681 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
682 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
687 tux_body->arms->set_action("duck+grab-left");
689 tux_body->arms->set_action("duck+grab-right");
694 tux_body->arms->set_action("grab-left");
696 tux_body->arms->set_action("grab-right");
702 smalltux_gameover->draw(context, get_pos(), layer);
703 } else if(growing_timer.get_timeleft() > 0) {
705 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
706 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
708 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
709 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
712 else if (safe_timer.started() && size_t(game_time*40)%2)
715 tux_body->draw(context, get_pos(), layer);
717 // Draw blinking star overlay
718 if (invincible_timer.started() &&
719 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
720 || size_t(game_time*20)%2)
723 if (!is_big() || duck)
724 smalltux_star->draw(context, get_pos(), layer + 5);
726 bigtux_star->draw(context, get_pos(), layer + 5);
731 Player::collision_tile(uint32_t tile_attributes)
733 if(tile_attributes & Tile::HURTS)
738 Player::collision(GameObject& other, const CollisionHit& hit)
740 Bullet* bullet = dynamic_cast<Bullet*> (&other);
745 if(other.get_flags() & FLAG_PORTABLE) {
746 Portable* portable = dynamic_cast<Portable*> (&other);
747 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
748 && fabsf(hit.normal.x) > .9) {
749 grabbed_object = portable;
754 if(other.get_flags() & FLAG_SOLID) {
756 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
758 hit.normal.x, hit.normal.y, hit.depth,
759 get_pos().x, get_pos().y,
760 movement.x, movement.y);
763 if(hit.normal.y < 0) { // landed on floor?
764 if(physic.get_velocity_y() < 0)
765 physic.set_velocity_y(0);
767 on_ground_flag = true;
769 // remember normal of this tile
770 if (hit.normal.y > -0.9) {
771 floor_normal.x = hit.normal.x;
772 floor_normal.y = hit.normal.y;
774 // slowly adjust to unisolid tiles.
775 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
776 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
777 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
780 // hack platforms so that we stand normally on them when going down...
781 Platform* platform = dynamic_cast<Platform*> (&other);
782 if(platform != NULL) {
783 if(platform->get_speed().y > 0)
784 physic.set_velocity_y(-platform->get_speed().y);
785 //physic.set_velocity_x(platform->get_speed().x);
787 } else if(hit.normal.y > 0) { // bumped against the roof
788 physic.set_velocity_y(.1);
790 // hack platform so that we are not glued to it from below
791 Platform* platform = dynamic_cast<Platform*> (&other);
792 if(platform != NULL) {
793 physic.set_velocity_y(-platform->get_speed().y);
797 if(fabsf(hit.normal.x) > .9) { // hit on the side?
798 physic.set_velocity_x(0);
801 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
803 Vector mov = movement - omov->get_movement();
805 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",
807 hit.normal.x, hit.normal.y,
809 movement.x, movement.y,
810 dest.p1.x, dest.p1.y,
811 omov->get_movement().x, omov->get_movement().y);
819 assert(dynamic_cast<MovingObject*> (&other) != NULL);
821 MovingObject* moving_object = static_cast<MovingObject*> (&other);
822 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
823 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
825 if(controller->pressed(Controller::UP))
826 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
832 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
834 if(safe_timer.started())
844 Player::make_invincible()
846 sound_manager->play("sounds/invincible.wav");
847 invincible_timer.start(TUX_INVINCIBLE_TIME);
848 Sector::current()->play_music(HERRING_MUSIC);
853 Player::kill(HurtMode mode)
855 if(dying || deactivated)
859 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
862 sound_manager->play("sounds/hurt.wav");
864 physic.set_velocity_x(0);
866 if (mode == SHRINK && is_big())
868 if (player_status->bonus == FIRE_BONUS
869 || player_status->bonus == ICE_BONUS)
871 safe_timer.start(TUX_SAFE_TIME);
872 player_status->bonus = GROWUP_BONUS;
876 //growing_timer.start(GROWING_TIME);
877 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
878 adjust_height = 30.8;
880 player_status->bonus = NO_BONUS;
887 for (i = 0; (i < 5) && (i < player_status->coins); i++)
889 // the numbers: starting x, starting y, velocity y
890 Sector::current()->add_object(new FallingCoin(get_pos() + Vector(rand()%5, rand()%50 - 32), rand()%200 - 100));
892 physic.enable_gravity(true);
893 physic.set_acceleration(0, 0);
894 physic.set_velocity(0, 700);
895 player_status->coins -= 25;
896 player_status->bonus = NO_BONUS;
898 dying_timer.start(3.0);
899 set_group(COLGROUP_DISABLED);
901 DisplayEffect* effect = new DisplayEffect();
902 effect->fade_out(3.0);
903 Sector::current()->add_object(effect);
904 sound_manager->stop_music(3.0);
909 Player::move(const Vector& vector)
911 bbox.set_pos(vector);
913 bbox.set_size(31.8, 63.8);
915 bbox.set_size(31.8, 31.8);
917 last_ground_y = vector.y;
923 Player::check_bounds(Camera* camera)
925 /* Keep tux in bounds: */
927 { // Lock Tux to the size of the level, so that he doesn't fall of
929 bbox.set_pos(Vector(0, get_pos().y));
932 /* Keep in-bounds, vertically: */
933 if (get_pos().y > Sector::current()->solids->get_height() * 32)
940 // can happen if back scrolling is disabled
941 if(get_pos().x < camera->get_translation().x) {
942 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
945 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
948 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
957 if(collision_object_map(bbox)) {
966 Player::add_velocity(const Vector& velocity)
968 physic.set_velocity(physic.get_velocity() + velocity);
972 Player::bounce(BadGuy& )
974 if(controller->hold(Controller::JUMP))
975 physic.set_velocity_y(520);
977 physic.set_velocity_y(300);
980 //Scripting Functions Below
986 physic.set_velocity_x(0);
987 physic.set_velocity_y(0);
996 void Player::walk(float speed)
998 physic.set_velocity_x(speed);