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, SQInteger 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, SQInteger 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(!dying && !deactivated)
191 movement = physic.get_movement(elapsed_time);
193 if(grabbed_object != 0) {
194 Vector pos = get_pos() +
195 Vector(dir == LEFT ? -16 : 16,
196 bbox.get_height()*0.66666 - 32);
197 grabbed_object->grab(*this, pos, dir);
200 on_ground_flag = false;
206 return on_ground_flag;
212 if(player_status->bonus == NO_BONUS)
219 Player::handle_horizontal_input()
221 float vx = physic.get_velocity_x();
222 float vy = physic.get_velocity_y();
223 float ax = physic.get_acceleration_x();
224 float ay = physic.get_acceleration_y();
227 if(!duck || physic.get_velocity_y() != 0) {
228 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
232 } else if(!controller->hold(Controller::LEFT)
233 && controller->hold(Controller::RIGHT)) {
240 if (!controller->hold(Controller::ACTION)) {
241 ax = dirsign * WALK_ACCELERATION_X;
243 if(vx >= MAX_WALK_XM && dirsign > 0) {
246 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
251 ax = dirsign * RUN_ACCELERATION_X;
253 if(vx >= MAX_RUN_XM && dirsign > 0) {
256 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
262 // we can reach WALK_SPEED without any acceleration
263 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
264 vx = dirsign * WALK_SPEED;
267 // changing directions?
268 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
270 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
271 skidding_timer.start(SKID_TIME);
272 sound_manager->play("sounds/skid.wav");
273 // dust some particles
274 Sector::current()->add_object(
276 Vector(dir == RIGHT ? bbox.p2.x : bbox.p1.x, bbox.p2.y),
277 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
278 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
287 // we get slower when not pressing any keys
289 if(fabs(vx) < WALK_SPEED) {
293 ax = WALK_ACCELERATION_X * 1.5;
295 ax = WALK_ACCELERATION_X * -1.5;
300 // if we're on ice slow down acceleration or deceleration
301 if (isice(base.x, base.y + base.height))
303 /* the acceleration/deceleration rate on ice is inversely proportional to
304 * the current velocity.
307 // increasing 1 will increase acceleration/deceleration rate
308 // decreasing 1 will decrease acceleration/deceleration rate
309 // must stay above zero, though
310 if (ax != 0) ax *= 1 / fabs(vx);
314 // extend/shrink tux collision rectangle so that we fall through/walk over 1
316 if(fabsf(vx) > MAX_WALK_XM) {
319 bbox.set_width(31.8);
322 // on downward slopes, adjust vertical velocity to match slope angle
324 if (floor_normal.y != 0) {
325 if ((floor_normal.x * vx) > 0) {
326 // we overdo it a little, just to be on the safe side
327 vy = vx * (floor_normal.x / floor_normal.y) * 2;
332 physic.set_velocity(vx, vy);
333 physic.set_acceleration(ax, ay);
337 Player::handle_vertical_input()
341 fall_mode = ON_GROUND;
342 last_ground_y = get_pos().y;
344 if(get_pos().y > last_ground_y)
346 else if(fall_mode == ON_GROUND)
350 if(on_ground()) { /* Make sure jumping is off. */
353 backflipping = false;
354 backflip_direction = 0;
359 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
361 if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
362 physic.set_velocity_y(300);
363 else { //do a backflip
365 physic.set_velocity_y(580);
366 backflip_timer.start(0.15);
369 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
370 physic.set_velocity_y(580);
372 physic.set_velocity_y(520);
374 //bbox.move(Vector(0, -1));
378 sound_manager->play("sounds/bigjump.wav");
380 sound_manager->play("sounds/jump.wav");
381 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
382 if (!backflipping && jumping && physic.get_velocity_y() > 0) {
384 physic.set_velocity_y(0);
388 /* In case the player has pressed Down while in a certain range of air,
389 enable butt jump action */
390 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
391 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
394 /* When Down is not held anymore, disable butt jump */
395 if(butt_jump && !controller->hold(Controller::DOWN))
400 if (butt_jump && on_ground() && is_big()) {
403 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
405 Sector::current()->add_smoke_cloud(
406 Vector(get_pos().x - 32, get_pos().y + 32));
410 // Break bricks beneath Tux
411 if(Sector::current()->trybreakbrick(
412 Vector(base.x + 1, base.y + base.height), false)
413 || Sector::current()->trybreakbrick(
414 Vector(base.x + base.width - 1, base.y + base.height), false)) {
415 physic.set_velocity_y(2);
419 // Kill nearby badguys
420 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
421 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
422 i != gameobjects.end();
424 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
426 // don't kill when badguys are already dying or in a certain mode
427 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
428 badguy->mode != BadGuy::BOMB_EXPLODE) {
429 if (fabsf(base.x - badguy->base.x) < 96 &&
430 fabsf(base.y - badguy->base.y) < 64)
438 /** jumping is only allowed if we're about to touch ground soon and if the
439 * button has been up in between the last jump
443 if ( (issolid(get_pos().x + bbox.get_width() / 2,
444 get_pos().y + bbox.get_height() + 64) ||
445 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
446 issolid(get_pos().x + bbox.get_width() - 1,
447 get_pos().y + bbox.get_height() + 64))
450 && input.jump && !input.old_jump)
458 Player::handle_input()
460 if(!controller->hold(Controller::ACTION) && grabbed_object) {
461 // move the grabbed object a bit away from tux
462 Vector pos = get_pos() +
463 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
464 bbox.get_height()*0.66666 - 32);
465 Rect dest(pos, pos + Vector(32, 32));
466 if(Sector::current()->is_free_space(dest)) {
467 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
469 moving_object->set_pos(pos);
471 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
473 grabbed_object->ungrab(*this, dir);
478 /* Handle horizontal movement: */
479 if (!backflipping) handle_horizontal_input();
481 if (backflip_direction == 0) {
482 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
484 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
485 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
490 if (on_ground() && !controller->hold(Controller::JUMP))
492 handle_vertical_input();
495 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
496 if(Sector::current()->add_bullet(
497 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
498 : Vector(32, bbox.get_height()/2)),
499 physic.get_velocity_x(), dir))
500 shooting_timer.start(SHOOTING_TIME);
504 if (controller->hold(Controller::DOWN) && is_big() && !duck
505 && physic.get_velocity_y() == 0 && on_ground()) {
507 bbox.move(Vector(0, 32));
508 bbox.set_height(31.8);
509 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
510 // if we have some velocity left then check if there is space for
512 bbox.move(Vector(0, -32));
513 bbox.set_height(63.8);
514 if(Sector::current()->is_free_space(bbox) || (
515 physic.get_velocity_x() > -.01 && physic.get_velocity_x() < .01
516 && physic.get_velocity_y() > -.01 && physic.get_velocity_y() < .01))
520 // undo the ducking changes
521 bbox.move(Vector(0, 32));
522 bbox.set_height(31.8);
528 Player::add_coins(int count)
530 player_status->add_coins(count);
534 Player::add_bonus(const std::string& bonustype)
536 if(bonustype == "grow") {
537 add_bonus(GROWUP_BONUS);
538 } else if(bonustype == "fireflower") {
539 add_bonus(FIRE_BONUS);
540 } else if(bonustype == "iceflower") {
541 add_bonus(ICE_BONUS);
542 } else if(bonustype == "none") {
545 std::ostringstream msg;
546 msg << "Unknown bonus type " << bonustype;
547 throw std::runtime_error(msg.str());
552 Player::add_bonus(BonusType type, bool animate)
554 // always ignore NO_BONUS
555 if (type == NO_BONUS) {
559 // ignore GROWUP_BONUS if we're already big
560 if (type == GROWUP_BONUS) {
561 if (player_status->bonus == GROWUP_BONUS) return;
562 if (player_status->bonus == FIRE_BONUS) return;
563 if (player_status->bonus == ICE_BONUS) return;
566 set_bonus(type, animate);
570 Player::set_bonus(BonusType type, bool animate)
572 if(player_status->bonus == NO_BONUS) {
573 adjust_height = 62.8;
575 growing_timer.start(GROWING_TIME);
578 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
579 player_status->max_fire_bullets = 0;
580 player_status->max_ice_bullets = 0;
582 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
583 if (type == ICE_BONUS) player_status->max_ice_bullets++;
585 player_status->bonus = type;
589 Player::set_visible(bool visible)
591 this->visible = visible;
595 Player::get_visible()
603 kick_timer.start(KICK_TIME);
607 Player::draw(DrawingContext& context)
612 TuxBodyParts* tux_body;
614 if (player_status->bonus == GROWUP_BONUS)
616 else if (player_status->bonus == FIRE_BONUS)
618 else if (player_status->bonus == ICE_BONUS)
621 tux_body = small_tux;
623 int layer = LAYER_OBJECTS + 1;
625 /* Set Tux sprite action */
626 if (duck && is_big())
629 tux_body->set_action("duck-left");
631 tux_body->set_action("duck-right");
633 else if (skidding_timer.started() && !skidding_timer.check())
636 tux_body->set_action("skid-left");
638 tux_body->set_action("skid-right");
640 else if (kick_timer.started() && !kick_timer.check())
643 tux_body->set_action("kick-left");
645 tux_body->set_action("kick-right");
647 else if (butt_jump && is_big())
650 tux_body->set_action("buttjump-left");
652 tux_body->set_action("buttjump-right");
654 else if (!on_ground())
657 tux_body->set_action("jump-left");
659 tux_body->set_action("jump-right");
663 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
666 tux_body->set_action("stand-left");
668 tux_body->set_action("stand-right");
673 tux_body->set_action("walk-left");
675 tux_body->set_action("walk-right");
679 if(idle_timer.check())
684 tux_body->head->set_action("idle-left", 1);
686 tux_body->head->set_action("idle-right", 1);
691 // Tux is holding something
692 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
693 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
698 tux_body->arms->set_action("duck+grab-left");
700 tux_body->arms->set_action("duck+grab-right");
705 tux_body->arms->set_action("grab-left");
707 tux_body->arms->set_action("grab-right");
713 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
714 } else if(growing_timer.get_timeleft() > 0) {
716 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
717 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
719 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
720 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
723 else if (safe_timer.started() && size_t(game_time*40)%2)
726 tux_body->draw(context, get_pos(), layer);
728 // Draw blinking star overlay
729 if (invincible_timer.started() &&
730 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
731 || size_t(game_time*20)%2)
734 if (!is_big() || duck)
735 smalltux_star->draw(context, get_pos(), layer + 5);
737 bigtux_star->draw(context, get_pos(), layer + 5);
742 Player::collision_tile(uint32_t tile_attributes)
744 if(tile_attributes & Tile::HURTS)
749 Player::collision(GameObject& other, const CollisionHit& hit)
751 Bullet* bullet = dynamic_cast<Bullet*> (&other);
756 if(other.get_flags() & FLAG_PORTABLE) {
757 Portable* portable = dynamic_cast<Portable*> (&other);
758 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
759 && fabsf(hit.normal.x) > .9) {
760 grabbed_object = portable;
765 if(other.get_flags() & FLAG_SOLID) {
767 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
769 hit.normal.x, hit.normal.y, hit.depth,
770 get_pos().x, get_pos().y,
771 movement.x, movement.y);
774 if(hit.normal.y < 0) { // landed on floor?
775 if(physic.get_velocity_y() < 0)
776 physic.set_velocity_y(0);
778 on_ground_flag = true;
780 // remember normal of this tile
781 if (hit.normal.y > -0.9) {
782 floor_normal.x = hit.normal.x;
783 floor_normal.y = hit.normal.y;
785 // slowly adjust to unisolid tiles.
786 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
787 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
788 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
791 // hack platforms so that we stand normally on them when going down...
792 Platform* platform = dynamic_cast<Platform*> (&other);
793 if(platform != NULL) {
794 if(platform->get_speed().y > 0)
795 physic.set_velocity_y(-platform->get_speed().y);
796 //physic.set_velocity_x(platform->get_speed().x);
798 } else if(hit.normal.y > 0) { // bumped against the roof
799 physic.set_velocity_y(.1);
801 // hack platform so that we are not glued to it from below
802 Platform* platform = dynamic_cast<Platform*> (&other);
803 if(platform != NULL) {
804 physic.set_velocity_y(-platform->get_speed().y);
808 if(fabsf(hit.normal.x) > .9) { // hit on the side?
809 physic.set_velocity_x(0);
812 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
814 Vector mov = movement - omov->get_movement();
816 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",
818 hit.normal.x, hit.normal.y,
820 movement.x, movement.y,
821 dest.p1.x, dest.p1.y,
822 omov->get_movement().x, omov->get_movement().y);
830 assert(dynamic_cast<MovingObject*> (&other) != NULL);
832 MovingObject* moving_object = static_cast<MovingObject*> (&other);
833 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
834 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
836 if(controller->pressed(Controller::UP))
837 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
843 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
845 if(safe_timer.started())
855 Player::make_invincible()
857 sound_manager->play("sounds/invincible.wav");
858 invincible_timer.start(TUX_INVINCIBLE_TIME);
859 Sector::current()->play_music(HERRING_MUSIC);
864 Player::kill(bool completely)
866 if(dying || deactivated)
870 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
873 sound_manager->play("sounds/hurt.wav");
875 physic.set_velocity_x(0);
877 if(!completely && is_big()) {
878 if(player_status->bonus == FIRE_BONUS
879 || player_status->bonus == ICE_BONUS) {
880 safe_timer.start(TUX_SAFE_TIME);
881 set_bonus(GROWUP_BONUS);
883 //growing_timer.start(GROWING_TIME);
884 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
885 adjust_height = 30.8;
890 for (int i = 0; (i < 5) && (i < player_status->coins); i++)
892 // the numbers: starting x, starting y, velocity y
893 Sector::current()->add_object(new FallingCoin(get_pos() +
894 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
895 systemRandom.rand(-100,100)));
897 physic.enable_gravity(true);
898 physic.set_acceleration(0, 0);
899 physic.set_velocity(0, 700);
900 player_status->coins -= 25;
903 dying_timer.start(3.0);
904 set_group(COLGROUP_DISABLED);
906 DisplayEffect* effect = new DisplayEffect();
907 effect->fade_out(3.0);
908 Sector::current()->add_object(effect);
909 sound_manager->stop_music(3.0);
914 Player::move(const Vector& vector)
916 bbox.set_pos(vector);
918 bbox.set_size(31.8, 63.8);
920 bbox.set_size(31.8, 31.8);
922 last_ground_y = vector.y;
928 Player::check_bounds(Camera* camera)
930 /* Keep tux in bounds: */
931 if (get_pos().x < 0) {
932 // Lock Tux to the size of the level, so that he doesn't fall of
934 bbox.set_pos(Vector(0, get_pos().y));
937 /* Keep in-bounds, vertically: */
938 if (get_pos().y > Sector::current()->solids->get_height() * 32) {
944 // can happen if back scrolling is disabled
945 if(get_pos().x < camera->get_translation().x) {
946 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
949 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
952 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
961 if(collision_object_map(bbox)) {
970 Player::add_velocity(const Vector& velocity)
972 physic.set_velocity(physic.get_velocity() + velocity);
976 Player::bounce(BadGuy& )
978 if(controller->hold(Controller::JUMP))
979 physic.set_velocity_y(520);
981 physic.set_velocity_y(300);
984 //Scripting Functions Below
990 physic.set_velocity_x(0);
991 physic.set_velocity_y(0);
992 physic.set_acceleration_x(0);
993 physic.set_acceleration_y(0);
1002 void Player::walk(float speed)
1004 physic.set_velocity_x(speed);