3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2003 Tobias Glaesser <tobi.web@gmx.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 "video/screen.hpp"
35 #include "statistics.hpp"
36 #include "game_session.hpp"
37 #include "object/tilemap.hpp"
38 #include "object/camera.hpp"
39 #include "object/particles.hpp"
40 #include "object/portable.hpp"
41 #include "object/bullet.hpp"
42 #include "trigger/trigger_base.hpp"
43 #include "control/joystickkeyboardcontroller.hpp"
45 #include "badguy/badguy.hpp"
46 #include "player_status.hpp"
48 static const int TILES_FOR_BUTTJUMP = 3;
49 static const float SHOOTING_TIME = .150;
50 /// time before idle animation starts
51 static const float IDLE_TIME = 2.5;
53 static const float WALK_ACCELERATION_X = 300;
54 static const float RUN_ACCELERATION_X = 400;
55 static const float SKID_XM = 200;
56 static const float SKID_TIME = .3;
57 static const float MAX_WALK_XM = 230;
58 static const float MAX_RUN_XM = 320;
59 static const float WALK_SPEED = 100;
61 static const float KICK_TIME = .3;
64 Surface* growingtux_left[GROWING_FRAMES];
65 Surface* growingtux_right[GROWING_FRAMES];
67 Surface* tux_life = 0;
69 TuxBodyParts* small_tux = 0;
70 TuxBodyParts* big_tux = 0;
71 TuxBodyParts* fire_tux = 0;
72 TuxBodyParts* ice_tux = 0;
75 TuxBodyParts::set_action(std::string action, int loops)
78 head->set_action(action, loops);
80 body->set_action(action, loops);
82 arms->set_action(action, loops);
84 feet->set_action(action, loops);
88 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
91 head->draw(context, pos, layer-1);
93 body->draw(context, pos, layer-3);
95 arms->draw(context, pos, layer);
97 feet->draw(context, pos, layer-2);
100 Player::Player(PlayerStatus* _player_status)
101 : player_status(_player_status), grabbed_object(0)
103 controller = main_controller;
104 smalltux_gameover = sprite_manager->create("smalltux-gameover");
105 smalltux_star = sprite_manager->create("smalltux-star");
106 bigtux_star = sprite_manager->create("bigtux-star");
112 delete smalltux_gameover;
113 delete smalltux_star;
121 bbox.set_size(31.8, 63.8);
123 bbox.set_size(31.8, 31.8);
132 fall_mode = ON_GROUND;
137 backflipping = false;
138 backflip_direction = 0;
141 on_ground_flag = false;
148 Player::set_controller(Controller* controller)
150 this->controller = controller;
154 Player::update(float elapsed_time)
156 if(dying && dying_timer.check()) {
161 // fixes the "affected even while blinking" bug
162 if (safe_timer.started() && this->get_group() != COLGROUP_MOVING_ONLY_STATIC) {
163 this->set_group(COLGROUP_MOVING_ONLY_STATIC);
165 else if (!safe_timer.started() && this->get_group() == COLGROUP_MOVING_ONLY_STATIC) {
166 this->set_group(COLGROUP_MOVING);
169 if(!controller->hold(Controller::ACTION) && grabbed_object) {
170 // move the grabbed object a bit away from tux
171 Vector pos = get_pos() +
172 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
173 bbox.get_height()*0.66666 - 32);
174 Rect dest(pos, pos + Vector(32, 32));
175 if(Sector::current()->is_free_space(dest)) {
176 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
178 moving_object->set_pos(pos);
181 std::cout << "Non MovingObjetc grabbed?!?\n";
184 grabbed_object->ungrab(*this, dir);
189 if(!dying && !deactivated)
192 movement = physic.get_movement(elapsed_time);
193 on_ground_flag = false;
196 // special exception for cases where we're stuck under tiles after
197 // being ducked. In this case we drift out
198 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
199 && collision_object_map(base)) {
200 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
201 previous_base = old_base = base;
205 if(grabbed_object != 0) {
206 Vector pos = get_pos() +
207 Vector(dir == LEFT ? -16 : 16,
208 bbox.get_height()*0.66666 - 32);
209 grabbed_object->grab(*this, pos, dir);
216 return on_ground_flag;
222 if(player_status->bonus == NO_BONUS)
229 Player::handle_horizontal_input()
231 float vx = physic.get_velocity_x();
232 float vy = physic.get_velocity_y();
233 float ax = physic.get_acceleration_x();
234 float ay = physic.get_acceleration_y();
237 if(!duck || physic.get_velocity_y() != 0) {
238 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
242 } else if(!controller->hold(Controller::LEFT)
243 && controller->hold(Controller::RIGHT)) {
250 if (!controller->hold(Controller::ACTION)) {
251 ax = dirsign * WALK_ACCELERATION_X;
253 if(vx >= MAX_WALK_XM && dirsign > 0) {
256 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
261 ax = dirsign * RUN_ACCELERATION_X;
263 if(vx >= MAX_RUN_XM && dirsign > 0) {
266 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
272 // we can reach WALK_SPEED without any acceleration
273 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
274 vx = dirsign * WALK_SPEED;
277 // changing directions?
278 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
280 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
281 skidding_timer.start(SKID_TIME);
282 sound_manager->play("sounds/skid.wav");
283 // dust some particles
284 Sector::current()->add_object(
286 Vector(dir == RIGHT ? bbox.p2.x : bbox.p1.x, bbox.p2.y),
287 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
288 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
297 // we get slower when not pressing any keys
299 if(fabs(vx) < WALK_SPEED) {
303 ax = WALK_ACCELERATION_X * 1.5;
305 ax = WALK_ACCELERATION_X * -1.5;
310 // if we're on ice slow down acceleration or deceleration
311 if (isice(base.x, base.y + base.height))
313 /* the acceleration/deceleration rate on ice is inversely proportional to
314 * the current velocity.
317 // increasing 1 will increase acceleration/deceleration rate
318 // decreasing 1 will decrease acceleration/deceleration rate
319 // must stay above zero, though
320 if (ax != 0) ax *= 1 / fabs(vx);
324 // extend/shrink tux collision rectangle so that we fall through/walk over 1
326 if(fabsf(vx) > MAX_WALK_XM) {
329 bbox.set_width(31.8);
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 /* Handle horizontal movement: */
461 if (!backflipping) handle_horizontal_input();
463 if (backflip_direction == 0) {
464 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
466 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
467 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
472 if (on_ground() && !controller->hold(Controller::JUMP))
474 handle_vertical_input();
477 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
478 if(Sector::current()->add_bullet(
479 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
480 : Vector(32, bbox.get_height()/2)),
481 physic.get_velocity_x(), dir))
482 shooting_timer.start(SHOOTING_TIME);
486 if (controller->hold(Controller::DOWN) && is_big() && !duck
487 && physic.get_velocity_y() == 0 && on_ground()) {
489 bbox.move(Vector(0, 32));
490 bbox.set_height(31.8);
491 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
492 // if we have some velocity left then check if there is space for
494 bbox.move(Vector(0, -32));
495 bbox.set_height(63.8);
496 if(Sector::current()->is_free_space(bbox) || (
497 physic.get_velocity_x() > -.01 && physic.get_velocity_x() < .01
498 && physic.get_velocity_y() > -.01 && physic.get_velocity_y() < .01))
502 // undo the ducking changes
503 bbox.move(Vector(0, 32));
504 bbox.set_height(31.8);
510 Player::set_bonus(BonusType type, bool animate)
512 if(player_status->bonus >= type)
515 if(player_status->bonus == NO_BONUS) {
516 bbox.set_height(63.8);
517 bbox.move(Vector(0, -32));
519 growing_timer.start(GROWING_TIME);
522 player_status->bonus = type;
526 Player::set_visible(bool visible)
528 this->visible = visible;
532 Player::get_visible()
540 kick_timer.start(KICK_TIME);
544 Player::draw(DrawingContext& context)
549 TuxBodyParts* tux_body;
551 if (player_status->bonus == GROWUP_BONUS)
553 else if (player_status->bonus == FIRE_BONUS)
555 else if (player_status->bonus == ICE_BONUS)
558 tux_body = small_tux;
560 int layer = LAYER_OBJECTS + 10;
562 /* Set Tux sprite action */
563 if (duck && is_big())
566 tux_body->set_action("duck-left");
568 tux_body->set_action("duck-right");
570 else if (skidding_timer.started() && !skidding_timer.check())
573 tux_body->set_action("skid-left");
575 tux_body->set_action("skid-right");
577 else if (kick_timer.started() && !kick_timer.check())
580 tux_body->set_action("kick-left");
582 tux_body->set_action("kick-right");
584 else if (butt_jump && is_big())
587 tux_body->set_action("buttjump-left");
589 tux_body->set_action("buttjump-right");
591 else if (physic.get_velocity_y() != 0)
594 tux_body->set_action("jump-left");
596 tux_body->set_action("jump-right");
600 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
603 tux_body->set_action("stand-left");
605 tux_body->set_action("stand-right");
610 tux_body->set_action("walk-left");
612 tux_body->set_action("walk-right");
616 if(idle_timer.check())
621 tux_body->head->set_action("idle-left", 1);
623 tux_body->head->set_action("idle-right", 1);
628 // Tux is holding something
629 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
630 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
635 tux_body->arms->set_action("duck+grab-left");
637 tux_body->arms->set_action("duck+grab-right");
642 tux_body->arms->set_action("grab-left");
644 tux_body->arms->set_action("grab-right");
650 smalltux_gameover->draw(context, get_pos(), layer);
651 } else if(growing_timer.get_timeleft() > 0) {
655 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
656 int((growing_timer.get_timegone() *
657 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
659 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
660 int((growing_timer.get_timegone() *
661 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
666 context.draw_surface(growingtux_right[
667 int((growing_timer.get_timegone() *
668 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
670 context.draw_surface(growingtux_left[
671 int((growing_timer.get_timegone() *
672 GROWING_FRAMES) / GROWING_TIME)],
676 else if (safe_timer.started() && size_t(game_time*40)%2)
679 tux_body->draw(context, get_pos(), layer);
681 // Draw blinking star overlay
682 if (invincible_timer.started() &&
683 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
684 || size_t(game_time*20)%2)
687 if (!is_big() || duck)
688 smalltux_star->draw(context, get_pos(), layer + 5);
690 bigtux_star->draw(context, get_pos(), layer + 5);
695 Player::collision_tile(uint32_t tile_attributes)
697 if(tile_attributes & Tile::HURTS)
702 Player::collision(GameObject& other, const CollisionHit& hit)
704 Bullet* bullet = dynamic_cast<Bullet*> (&other);
709 if(other.get_flags() & FLAG_PORTABLE) {
710 Portable* portable = dynamic_cast<Portable*> (&other);
711 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
712 && fabsf(hit.normal.x) > .9) {
713 grabbed_object = portable;
718 if(other.get_flags() & FLAG_SOLID) {
719 if(hit.normal.y < 0) { // landed on floor?
720 if(physic.get_velocity_y() < 0)
721 physic.set_velocity_y(0);
722 on_ground_flag = true;
723 } else if(hit.normal.y > 0) { // bumped against the roof
724 physic.set_velocity_y(.1);
727 if(fabsf(hit.normal.x) > .9) { // hit on the side?
728 physic.set_velocity_x(0);
735 assert(dynamic_cast<MovingObject*> (&other) != NULL);
737 MovingObject* moving_object = static_cast<MovingObject*> (&other);
738 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
739 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
741 if(controller->pressed(Controller::UP))
742 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
748 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
756 Player::make_invincible()
758 sound_manager->play("sounds/invincible.wav");
759 invincible_timer.start(TUX_INVINCIBLE_TIME);
760 Sector::current()->play_music(HERRING_MUSIC);
765 Player::kill(HurtMode mode)
767 if(dying || deactivated)
771 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
774 sound_manager->play("sounds/hurt.wav");
776 physic.set_velocity_x(0);
778 if (mode == SHRINK && is_big())
780 if (player_status->bonus == FIRE_BONUS
781 || player_status->bonus == ICE_BONUS)
783 safe_timer.start(TUX_SAFE_TIME);
784 player_status->bonus = GROWUP_BONUS;
788 growing_timer.start(GROWING_TIME);
789 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
790 bbox.set_height(31.8);
792 player_status->bonus = NO_BONUS;
797 physic.enable_gravity(true);
798 physic.set_acceleration(0, 0);
799 physic.set_velocity(0, 700);
800 player_status->lives -= 1;
801 player_status->bonus = NO_BONUS;
803 dying_timer.start(3.0);
804 set_group(COLGROUP_DISABLED);
806 DisplayEffect* effect = new DisplayEffect();
807 effect->fade_out(3.0);
808 Sector::current()->add_object(effect);
809 sound_manager->stop_music(3.0);
814 Player::move(const Vector& vector)
816 bbox.set_pos(vector);
818 bbox.set_size(31.8, 63.8);
820 bbox.set_size(31.8, 31.8);
821 on_ground_flag = false;
823 last_ground_y = vector.y;
829 Player::check_bounds(Camera* camera)
831 /* Keep tux in bounds: */
833 { // Lock Tux to the size of the level, so that he doesn't fall of
835 bbox.set_pos(Vector(0, get_pos().y));
838 /* Keep in-bounds, vertically: */
839 if (get_pos().y > Sector::current()->solids->get_height() * 32)
846 // can happen if back scrolling is disabled
847 if(get_pos().x < camera->get_translation().x) {
848 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
851 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
854 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
863 if(collision_object_map(bbox)) {
872 Player::bounce(BadGuy& )
874 if(controller->hold(Controller::JUMP))
875 physic.set_velocity_y(520);
877 physic.set_velocity_y(300);
880 //Scripting Functions Below
886 physic.set_velocity_x(0);
887 physic.set_velocity_y(0);
896 void Player::walk(float speed)
898 physic.set_velocity_x(speed);