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::add_bonus(const std::string& bonustype)
547 if(bonustype == "grow")
548 add_bonus(GROWUP_BONUS);
549 else if(bonustype == "fireflower")
550 add_bonus(FIRE_BONUS);
551 else if(bonustype == "iceflower")
552 add_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::add_bonus(BonusType type, bool animate)
565 // always ignore NO_BONUS
566 if (type == NO_BONUS) {
570 // ignore GROWUP_BONUS if we're already big
571 if (type == GROWUP_BONUS) {
572 if (player_status->bonus == GROWUP_BONUS) return;
573 if (player_status->bonus == FIRE_BONUS) return;
574 if (player_status->bonus == ICE_BONUS) return;
577 set_bonus(type, animate);
581 Player::set_bonus(BonusType type, bool animate)
583 if(player_status->bonus == NO_BONUS) {
584 adjust_height = 62.8;
586 growing_timer.start(GROWING_TIME);
589 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
590 player_status->max_fire_bullets = 0;
591 player_status->max_ice_bullets = 0;
593 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
594 if (type == ICE_BONUS) player_status->max_ice_bullets++;
596 player_status->bonus = type;
600 Player::set_visible(bool visible)
602 this->visible = visible;
606 Player::get_visible()
614 kick_timer.start(KICK_TIME);
618 Player::draw(DrawingContext& context)
623 TuxBodyParts* tux_body;
625 if (player_status->bonus == GROWUP_BONUS)
627 else if (player_status->bonus == FIRE_BONUS)
629 else if (player_status->bonus == ICE_BONUS)
632 tux_body = small_tux;
634 int layer = LAYER_OBJECTS + 10;
636 /* Set Tux sprite action */
637 if (duck && is_big())
640 tux_body->set_action("duck-left");
642 tux_body->set_action("duck-right");
644 else if (skidding_timer.started() && !skidding_timer.check())
647 tux_body->set_action("skid-left");
649 tux_body->set_action("skid-right");
651 else if (kick_timer.started() && !kick_timer.check())
654 tux_body->set_action("kick-left");
656 tux_body->set_action("kick-right");
658 else if (butt_jump && is_big())
661 tux_body->set_action("buttjump-left");
663 tux_body->set_action("buttjump-right");
665 else if (!on_ground())
668 tux_body->set_action("jump-left");
670 tux_body->set_action("jump-right");
674 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
677 tux_body->set_action("stand-left");
679 tux_body->set_action("stand-right");
684 tux_body->set_action("walk-left");
686 tux_body->set_action("walk-right");
690 if(idle_timer.check())
695 tux_body->head->set_action("idle-left", 1);
697 tux_body->head->set_action("idle-right", 1);
702 // Tux is holding something
703 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
704 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
709 tux_body->arms->set_action("duck+grab-left");
711 tux_body->arms->set_action("duck+grab-right");
716 tux_body->arms->set_action("grab-left");
718 tux_body->arms->set_action("grab-right");
724 smalltux_gameover->draw(context, get_pos(), layer);
725 } else if(growing_timer.get_timeleft() > 0) {
727 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
728 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
730 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
731 GROWING_FRAMES) / GROWING_TIME)], get_pos() - Vector(0, 32), layer);
734 else if (safe_timer.started() && size_t(game_time*40)%2)
737 tux_body->draw(context, get_pos(), layer);
739 // Draw blinking star overlay
740 if (invincible_timer.started() &&
741 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
742 || size_t(game_time*20)%2)
745 if (!is_big() || duck)
746 smalltux_star->draw(context, get_pos(), layer + 5);
748 bigtux_star->draw(context, get_pos(), layer + 5);
753 Player::collision_tile(uint32_t tile_attributes)
755 if(tile_attributes & Tile::HURTS)
760 Player::collision(GameObject& other, const CollisionHit& hit)
762 Bullet* bullet = dynamic_cast<Bullet*> (&other);
767 if(other.get_flags() & FLAG_PORTABLE) {
768 Portable* portable = dynamic_cast<Portable*> (&other);
769 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
770 && fabsf(hit.normal.x) > .9) {
771 grabbed_object = portable;
776 if(other.get_flags() & FLAG_SOLID) {
778 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
780 hit.normal.x, hit.normal.y, hit.depth,
781 get_pos().x, get_pos().y,
782 movement.x, movement.y);
785 if(hit.normal.y < 0) { // landed on floor?
786 if(physic.get_velocity_y() < 0)
787 physic.set_velocity_y(0);
789 on_ground_flag = true;
791 // remember normal of this tile
792 if (hit.normal.y > -0.9) {
793 floor_normal.x = hit.normal.x;
794 floor_normal.y = hit.normal.y;
796 // slowly adjust to unisolid tiles.
797 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
798 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
799 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
802 // hack platforms so that we stand normally on them when going down...
803 Platform* platform = dynamic_cast<Platform*> (&other);
804 if(platform != NULL) {
805 if(platform->get_speed().y > 0)
806 physic.set_velocity_y(-platform->get_speed().y);
807 //physic.set_velocity_x(platform->get_speed().x);
809 } else if(hit.normal.y > 0) { // bumped against the roof
810 physic.set_velocity_y(.1);
812 // hack platform so that we are not glued to it from below
813 Platform* platform = dynamic_cast<Platform*> (&other);
814 if(platform != NULL) {
815 physic.set_velocity_y(-platform->get_speed().y);
819 if(fabsf(hit.normal.x) > .9) { // hit on the side?
820 physic.set_velocity_x(0);
823 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
825 Vector mov = movement - omov->get_movement();
827 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",
829 hit.normal.x, hit.normal.y,
831 movement.x, movement.y,
832 dest.p1.x, dest.p1.y,
833 omov->get_movement().x, omov->get_movement().y);
841 assert(dynamic_cast<MovingObject*> (&other) != NULL);
843 MovingObject* moving_object = static_cast<MovingObject*> (&other);
844 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
845 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
847 if(controller->pressed(Controller::UP))
848 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
854 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
856 if(safe_timer.started())
866 Player::make_invincible()
868 sound_manager->play("sounds/invincible.wav");
869 invincible_timer.start(TUX_INVINCIBLE_TIME);
870 Sector::current()->play_music(HERRING_MUSIC);
875 Player::kill(HurtMode mode)
877 if(dying || deactivated)
881 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
884 sound_manager->play("sounds/hurt.wav");
886 physic.set_velocity_x(0);
888 if (mode == SHRINK && is_big())
890 if (player_status->bonus == FIRE_BONUS
891 || player_status->bonus == ICE_BONUS)
893 safe_timer.start(TUX_SAFE_TIME);
894 set_bonus(GROWUP_BONUS);
898 //growing_timer.start(GROWING_TIME);
899 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
900 adjust_height = 30.8;
909 for (i = 0; (i < 5) && (i < player_status->coins); i++)
911 // the numbers: starting x, starting y, velocity y
912 Sector::current()->add_object(new FallingCoin(get_pos() + Vector(rand()%5, rand()%50 - 32), rand()%200 - 100));
914 physic.enable_gravity(true);
915 physic.set_acceleration(0, 0);
916 physic.set_velocity(0, 700);
917 player_status->coins -= 25;
920 dying_timer.start(3.0);
921 set_group(COLGROUP_DISABLED);
923 DisplayEffect* effect = new DisplayEffect();
924 effect->fade_out(3.0);
925 Sector::current()->add_object(effect);
926 sound_manager->stop_music(3.0);
931 Player::move(const Vector& vector)
933 bbox.set_pos(vector);
935 bbox.set_size(31.8, 63.8);
937 bbox.set_size(31.8, 31.8);
939 last_ground_y = vector.y;
945 Player::check_bounds(Camera* camera)
947 /* Keep tux in bounds: */
949 { // Lock Tux to the size of the level, so that he doesn't fall of
951 bbox.set_pos(Vector(0, get_pos().y));
954 /* Keep in-bounds, vertically: */
955 if (get_pos().y > Sector::current()->solids->get_height() * 32)
962 // can happen if back scrolling is disabled
963 if(get_pos().x < camera->get_translation().x) {
964 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
967 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
970 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
979 if(collision_object_map(bbox)) {
988 Player::add_velocity(const Vector& velocity)
990 physic.set_velocity(physic.get_velocity() + velocity);
994 Player::bounce(BadGuy& )
996 if(controller->hold(Controller::JUMP))
997 physic.set_velocity_y(520);
999 physic.set_velocity_y(300);
1002 //Scripting Functions Below
1005 Player::deactivate()
1008 physic.set_velocity_x(0);
1009 physic.set_velocity_y(0);
1015 deactivated = false;
1018 void Player::walk(float speed)
1020 physic.set_velocity_x(speed);