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");
112 sound_manager->preload("sounds/bigjump.wav");
113 sound_manager->preload("sounds/jump.wav");
114 sound_manager->preload("sounds/hurt.wav");
115 sound_manager->preload("sounds/skid.wav");
122 delete smalltux_gameover;
123 delete smalltux_star;
131 bbox.set_size(31.8, 62.8);
133 bbox.set_size(31.8, 30.8);
143 fall_mode = ON_GROUND;
148 backflipping = false;
149 backflip_direction = 0;
152 on_ground_flag = false;
155 floor_normal = Vector(0,-1);
161 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
163 Scripting::Player* interface = static_cast<Scripting::Player*> (this);
164 Scripting::expose_object(vm, table_idx, interface, "Tux", false);
168 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
170 Scripting::unexpose_object(vm, table_idx, "Tux");
174 Player::set_controller(Controller* controller)
176 this->controller = controller;
180 Player::update(float elapsed_time)
182 if(dying && dying_timer.check()) {
187 if(adjust_height != 0) {
188 bbox.move(Vector(0, bbox.get_height() - adjust_height));
189 bbox.set_height(adjust_height);
193 if(!dying && !deactivated)
196 movement = physic.get_movement(elapsed_time);
198 if(grabbed_object != 0 && !dying ) {
199 Vector pos = get_pos() +
200 Vector(dir == LEFT ? -16 : 16,
201 bbox.get_height()*0.66666 - 32);
202 grabbed_object->grab(*this, pos, dir);
205 on_ground_flag = false;
211 return on_ground_flag;
217 if(player_status->bonus == NO_BONUS)
224 Player::handle_horizontal_input()
226 float vx = physic.get_velocity_x();
227 float vy = physic.get_velocity_y();
228 float ax = physic.get_acceleration_x();
229 float ay = physic.get_acceleration_y();
232 if(!duck || physic.get_velocity_y() != 0) {
233 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
237 } else if(!controller->hold(Controller::LEFT)
238 && controller->hold(Controller::RIGHT)) {
245 if (!controller->hold(Controller::ACTION)) {
246 ax = dirsign * WALK_ACCELERATION_X;
248 if(vx >= MAX_WALK_XM && dirsign > 0) {
251 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
256 ax = dirsign * RUN_ACCELERATION_X;
258 if(vx >= MAX_RUN_XM && dirsign > 0) {
261 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
267 // we can reach WALK_SPEED without any acceleration
268 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
269 vx = dirsign * WALK_SPEED;
272 // changing directions?
273 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
275 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
276 skidding_timer.start(SKID_TIME);
277 sound_manager->play("sounds/skid.wav");
278 // dust some particles
279 Sector::current()->add_object(
281 Vector(dir == RIGHT ? bbox.p2.x : bbox.p1.x, bbox.p2.y),
282 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
283 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
292 // we get slower when not pressing any keys
294 if(fabs(vx) < WALK_SPEED) {
298 ax = WALK_ACCELERATION_X * 1.5;
300 ax = WALK_ACCELERATION_X * -1.5;
305 // if we're on ice slow down acceleration or deceleration
306 if (isice(base.x, base.y + base.height))
308 /* the acceleration/deceleration rate on ice is inversely proportional to
309 * the current velocity.
312 // increasing 1 will increase acceleration/deceleration rate
313 // decreasing 1 will decrease acceleration/deceleration rate
314 // must stay above zero, though
315 if (ax != 0) ax *= 1 / fabs(vx);
319 // extend/shrink tux collision rectangle so that we fall through/walk over 1
321 if(fabsf(vx) > MAX_WALK_XM) {
324 bbox.set_width(31.8);
327 // on downward slopes, adjust vertical velocity to match slope angle
329 if (floor_normal.y != 0) {
330 if ((floor_normal.x * vx) > 0) {
331 // we overdo it a little, just to be on the safe side
332 vy = vx * (floor_normal.x / floor_normal.y) * 2;
337 physic.set_velocity(vx, vy);
338 physic.set_acceleration(ax, ay);
342 Player::handle_vertical_input()
346 fall_mode = ON_GROUND;
347 last_ground_y = get_pos().y;
349 if(get_pos().y > last_ground_y)
351 else if(fall_mode == ON_GROUND)
355 if(on_ground()) { /* Make sure jumping is off. */
358 backflipping = false;
359 backflip_direction = 0;
364 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
366 if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
367 physic.set_velocity_y(300);
368 else { //do a backflip
370 physic.set_velocity_y(580);
371 backflip_timer.start(0.15);
374 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
375 physic.set_velocity_y(580);
377 physic.set_velocity_y(520);
379 //bbox.move(Vector(0, -1));
383 sound_manager->play("sounds/bigjump.wav");
385 sound_manager->play("sounds/jump.wav");
386 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
387 if (!backflipping && jumping && physic.get_velocity_y() > 0) {
389 physic.set_velocity_y(0);
393 /* In case the player has pressed Down while in a certain range of air,
394 enable butt jump action */
395 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
396 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
399 /* When Down is not held anymore, disable butt jump */
400 if(butt_jump && !controller->hold(Controller::DOWN))
405 if (butt_jump && on_ground() && is_big()) {
408 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
410 Sector::current()->add_smoke_cloud(
411 Vector(get_pos().x - 32, get_pos().y + 32));
415 // Break bricks beneath Tux
416 if(Sector::current()->trybreakbrick(
417 Vector(base.x + 1, base.y + base.height), false)
418 || Sector::current()->trybreakbrick(
419 Vector(base.x + base.width - 1, base.y + base.height), false)) {
420 physic.set_velocity_y(2);
424 // Kill nearby badguys
425 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
426 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
427 i != gameobjects.end();
429 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
431 // don't kill when badguys are already dying or in a certain mode
432 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
433 badguy->mode != BadGuy::BOMB_EXPLODE) {
434 if (fabsf(base.x - badguy->base.x) < 96 &&
435 fabsf(base.y - badguy->base.y) < 64)
443 /** jumping is only allowed if we're about to touch ground soon and if the
444 * button has been up in between the last jump
448 if ( (issolid(get_pos().x + bbox.get_width() / 2,
449 get_pos().y + bbox.get_height() + 64) ||
450 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
451 issolid(get_pos().x + bbox.get_width() - 1,
452 get_pos().y + bbox.get_height() + 64))
455 && input.jump && !input.old_jump)
463 Player::handle_input()
465 if(!controller->hold(Controller::ACTION) && grabbed_object) {
466 // move the grabbed object a bit away from tux
467 Vector pos = get_pos() +
468 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
469 bbox.get_height()*0.66666 - 32);
470 Rect dest(pos, pos + Vector(32, 32));
471 if(Sector::current()->is_free_space(dest)) {
472 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
474 moving_object->set_pos(pos);
476 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
478 grabbed_object->ungrab(*this, dir);
483 /* Handle horizontal movement: */
484 if (!backflipping) handle_horizontal_input();
486 if (backflip_direction == 0) {
487 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
489 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
490 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
495 if (on_ground() && !controller->hold(Controller::JUMP))
497 handle_vertical_input();
500 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
501 if(Sector::current()->add_bullet(
502 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
503 : Vector(32, bbox.get_height()/2)),
504 physic.get_velocity_x(), dir))
505 shooting_timer.start(SHOOTING_TIME);
509 if (controller->hold(Controller::DOWN) && is_big() && !duck
510 && physic.get_velocity_y() == 0 && on_ground()) {
512 bbox.move(Vector(0, 32));
513 bbox.set_height(31.8);
514 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
515 // if we have some velocity left then check if there is space for
517 bbox.move(Vector(0, -32));
518 bbox.set_height(63.8);
519 if(Sector::current()->is_free_space(bbox) || (
520 physic.get_velocity_x() > -.01 && physic.get_velocity_x() < .01
521 && physic.get_velocity_y() > -.01 && physic.get_velocity_y() < .01))
525 // undo the ducking changes
526 bbox.move(Vector(0, 32));
527 bbox.set_height(31.8);
533 Player::add_coins(int count)
535 player_status->add_coins(count);
539 Player::add_bonus(const std::string& bonustype)
541 if(bonustype == "grow") {
542 add_bonus(GROWUP_BONUS);
543 } else if(bonustype == "fireflower") {
544 add_bonus(FIRE_BONUS);
545 } else if(bonustype == "iceflower") {
546 add_bonus(ICE_BONUS);
547 } else if(bonustype == "none") {
550 std::ostringstream msg;
551 msg << "Unknown bonus type " << bonustype;
552 throw std::runtime_error(msg.str());
557 Player::add_bonus(BonusType type, bool animate)
559 // always ignore NO_BONUS
560 if (type == NO_BONUS) {
564 // ignore GROWUP_BONUS if we're already big
565 if (type == GROWUP_BONUS) {
566 if (player_status->bonus == GROWUP_BONUS) return;
567 if (player_status->bonus == FIRE_BONUS) return;
568 if (player_status->bonus == ICE_BONUS) return;
571 set_bonus(type, animate);
575 Player::set_bonus(BonusType type, bool animate)
577 if(player_status->bonus == NO_BONUS) {
578 adjust_height = 62.8;
580 growing_timer.start(GROWING_TIME);
583 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
584 player_status->max_fire_bullets = 0;
585 player_status->max_ice_bullets = 0;
587 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
588 if (type == ICE_BONUS) player_status->max_ice_bullets++;
590 player_status->bonus = type;
594 Player::set_visible(bool visible)
596 this->visible = visible;
600 Player::get_visible()
608 kick_timer.start(KICK_TIME);
612 Player::draw(DrawingContext& context)
617 TuxBodyParts* tux_body;
619 if (player_status->bonus == GROWUP_BONUS)
621 else if (player_status->bonus == FIRE_BONUS)
623 else if (player_status->bonus == ICE_BONUS)
626 tux_body = small_tux;
628 int layer = LAYER_OBJECTS + 1;
630 /* Set Tux sprite action */
631 if (duck && is_big())
634 tux_body->set_action("duck-left");
636 tux_body->set_action("duck-right");
638 else if (skidding_timer.started() && !skidding_timer.check())
641 tux_body->set_action("skid-left");
643 tux_body->set_action("skid-right");
645 else if (kick_timer.started() && !kick_timer.check())
648 tux_body->set_action("kick-left");
650 tux_body->set_action("kick-right");
652 else if (butt_jump && is_big())
655 tux_body->set_action("buttjump-left");
657 tux_body->set_action("buttjump-right");
659 else if (!on_ground())
662 tux_body->set_action("jump-left");
664 tux_body->set_action("jump-right");
668 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
671 tux_body->set_action("stand-left");
673 tux_body->set_action("stand-right");
678 tux_body->set_action("walk-left");
680 tux_body->set_action("walk-right");
684 if(idle_timer.check())
689 tux_body->head->set_action("idle-left", 1);
691 tux_body->head->set_action("idle-right", 1);
696 // Tux is holding something
697 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
698 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
703 tux_body->arms->set_action("duck+grab-left");
705 tux_body->arms->set_action("duck+grab-right");
710 tux_body->arms->set_action("grab-left");
712 tux_body->arms->set_action("grab-right");
718 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
719 } else if(growing_timer.get_timeleft() > 0) {
721 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
722 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
724 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
725 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
728 else if (safe_timer.started() && size_t(game_time*40)%2)
731 tux_body->draw(context, get_pos(), layer);
733 // Draw blinking star overlay
734 if (invincible_timer.started() &&
735 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
736 || size_t(game_time*20)%2)
739 if (!is_big() || duck)
740 smalltux_star->draw(context, get_pos(), layer + 5);
742 bigtux_star->draw(context, get_pos(), layer + 5);
747 Player::collision_tile(uint32_t tile_attributes)
749 if(tile_attributes & Tile::HURTS)
754 Player::collision(GameObject& other, const CollisionHit& hit)
756 Bullet* bullet = dynamic_cast<Bullet*> (&other);
761 if(other.get_flags() & FLAG_PORTABLE) {
762 Portable* portable = dynamic_cast<Portable*> (&other);
763 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
764 && fabsf(hit.normal.x) > .9) {
765 grabbed_object = portable;
770 if(other.get_flags() & FLAG_SOLID) {
772 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
774 hit.normal.x, hit.normal.y, hit.depth,
775 get_pos().x, get_pos().y,
776 movement.x, movement.y);
779 if(hit.normal.y < 0) { // landed on floor?
780 if(physic.get_velocity_y() < 0)
781 physic.set_velocity_y(0);
783 on_ground_flag = true;
785 // remember normal of this tile
786 if (hit.normal.y > -0.9) {
787 floor_normal.x = hit.normal.x;
788 floor_normal.y = hit.normal.y;
790 // slowly adjust to unisolid tiles.
791 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
792 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
793 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
796 // hack platforms so that we stand normally on them when going down...
797 Platform* platform = dynamic_cast<Platform*> (&other);
798 if(platform != NULL) {
799 if(platform->get_speed().y > 0)
800 physic.set_velocity_y(-platform->get_speed().y);
801 //physic.set_velocity_x(platform->get_speed().x);
803 } else if(hit.normal.y > 0) { // bumped against the roof
804 physic.set_velocity_y(.1);
806 // hack platform so that we are not glued to it from below
807 Platform* platform = dynamic_cast<Platform*> (&other);
808 if(platform != NULL) {
809 physic.set_velocity_y(-platform->get_speed().y);
813 if(fabsf(hit.normal.x) > .9) { // hit on the side?
814 physic.set_velocity_x(0);
817 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
819 Vector mov = movement - omov->get_movement();
821 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",
823 hit.normal.x, hit.normal.y,
825 movement.x, movement.y,
826 dest.p1.x, dest.p1.y,
827 omov->get_movement().x, omov->get_movement().y);
835 assert(dynamic_cast<MovingObject*> (&other) != NULL);
837 MovingObject* moving_object = static_cast<MovingObject*> (&other);
838 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
839 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
841 if(controller->pressed(Controller::UP))
842 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
848 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
850 if(safe_timer.started())
860 Player::make_invincible()
862 sound_manager->play("sounds/invincible.wav");
863 invincible_timer.start(TUX_INVINCIBLE_TIME);
864 Sector::current()->play_music(HERRING_MUSIC);
869 Player::kill(bool completely)
871 if(dying || deactivated)
875 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
878 sound_manager->play("sounds/hurt.wav");
880 physic.set_velocity_x(0);
882 if(!completely && is_big()) {
883 if(player_status->bonus == FIRE_BONUS
884 || player_status->bonus == ICE_BONUS) {
885 safe_timer.start(TUX_SAFE_TIME);
886 set_bonus(GROWUP_BONUS);
888 //growing_timer.start(GROWING_TIME);
889 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
890 adjust_height = 30.8;
895 for (int i = 0; (i < 5) && (i < player_status->coins); i++)
897 // the numbers: starting x, starting y, velocity y
898 Sector::current()->add_object(new FallingCoin(get_pos() +
899 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
900 systemRandom.rand(-100,100)));
902 physic.enable_gravity(true);
903 physic.set_acceleration(0, 0);
904 physic.set_velocity(0, 700);
905 player_status->coins -= 25;
908 dying_timer.start(3.0);
909 set_group(COLGROUP_DISABLED);
911 DisplayEffect* effect = new DisplayEffect();
912 effect->fade_out(3.0);
913 Sector::current()->add_object(effect);
914 sound_manager->stop_music(3.0);
919 Player::move(const Vector& vector)
921 bbox.set_pos(vector);
923 bbox.set_size(31.8, 63.8);
925 bbox.set_size(31.8, 31.8);
927 last_ground_y = vector.y;
933 Player::check_bounds(Camera* camera)
935 /* Keep tux in bounds: */
936 if (get_pos().x < 0) {
937 // Lock Tux to the size of the level, so that he doesn't fall of
939 bbox.set_pos(Vector(0, get_pos().y));
942 /* Keep in-bounds, vertically: */
943 if (get_pos().y > Sector::current()->solids->get_height() * 32) {
949 // can happen if back scrolling is disabled
950 if(get_pos().x < camera->get_translation().x) {
951 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
954 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
957 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
966 if(collision_object_map(bbox)) {
975 Player::add_velocity(const Vector& velocity)
977 physic.set_velocity(physic.get_velocity() + velocity);
981 Player::bounce(BadGuy& )
983 if(controller->hold(Controller::JUMP))
984 physic.set_velocity_y(520);
986 physic.set_velocity_y(300);
989 //Scripting Functions Below
995 physic.set_velocity_x(0);
996 physic.set_velocity_y(0);
997 physic.set_acceleration_x(0);
998 physic.set_acceleration_y(0);
1004 deactivated = false;
1007 void Player::walk(float speed)
1009 physic.set_velocity_x(speed);