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/gameobjs.hpp"
40 #include "object/portable.hpp"
41 #include "object/bullet.hpp"
42 #include "trigger/trigger_base.hpp"
43 #include "control/joystickkeyboardcontroller.hpp"
45 #include "player_status.hpp"
47 static const int TILES_FOR_BUTTJUMP = 3;
48 static const float SHOOTING_TIME = .150;
49 /// time before idle animation starts
50 static const float IDLE_TIME = 2.5;
52 static const float WALK_ACCELERATION_X = 300;
53 static const float RUN_ACCELERATION_X = 400;
54 static const float SKID_XM = 200;
55 static const float SKID_TIME = .3;
56 static const float MAX_WALK_XM = 230;
57 static const float MAX_RUN_XM = 320;
58 static const float WALK_SPEED = 100;
61 Surface* growingtux_left[GROWING_FRAMES];
62 Surface* growingtux_right[GROWING_FRAMES];
64 Surface* tux_life = 0;
66 TuxBodyParts* small_tux = 0;
67 TuxBodyParts* big_tux = 0;
68 TuxBodyParts* fire_tux = 0;
69 TuxBodyParts* ice_tux = 0;
72 TuxBodyParts::set_action(std::string action, int loops)
75 head->set_action(action, loops);
77 body->set_action(action, loops);
79 arms->set_action(action, loops);
81 feet->set_action(action, loops);
85 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
88 head->draw(context, pos, layer-1);
90 body->draw(context, pos, layer-3);
92 arms->draw(context, pos, layer);
94 feet->draw(context, pos, layer-2);
97 Player::Player(PlayerStatus* _player_status)
98 : player_status(_player_status), grabbed_object(0)
100 controller = main_controller;
101 smalltux_gameover = sprite_manager->create("smalltux-gameover");
102 smalltux_star = sprite_manager->create("smalltux-star");
103 bigtux_star = sprite_manager->create("bigtux-star");
109 delete smalltux_gameover;
110 delete smalltux_star;
118 bbox.set_size(31.8, 63.8);
120 bbox.set_size(31.8, 31.8);
129 fall_mode = ON_GROUND;
134 backflipping = false;
135 backflip_direction = 0;
137 on_ground_flag = false;
144 Player::set_controller(Controller* controller)
146 this->controller = controller;
150 Player::update(float elapsed_time)
152 if(dying && dying_timer.check()) {
157 // fixes the "affected even while blinking" bug
158 if (safe_timer.started() && this->get_group() != COLGROUP_MOVING_ONLY_STATIC) {
159 this->set_group(COLGROUP_MOVING_ONLY_STATIC);
161 else if (!safe_timer.started() && this->get_group() == COLGROUP_MOVING_ONLY_STATIC) {
162 this->set_group(COLGROUP_MOVING);
165 if(!controller->hold(Controller::ACTION) && grabbed_object) {
166 // move the grabbed object a bit away from tux
167 Vector pos = get_pos() +
168 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
169 bbox.get_height()*0.66666 - 32);
170 Rect dest(pos, pos + Vector(32, 32));
171 if(Sector::current()->is_free_space(dest)) {
172 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
174 moving_object->set_pos(pos);
177 std::cout << "Non MovingObjetc grabbed?!?\n";
184 if(!dying && !deactivated)
187 movement = physic.get_movement(elapsed_time);
188 on_ground_flag = false;
191 // special exception for cases where we're stuck under tiles after
192 // being ducked. In this case we drift out
193 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
194 && collision_object_map(base)) {
195 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
196 previous_base = old_base = base;
200 if(grabbed_object != 0) {
201 Vector pos = get_pos() +
202 Vector(dir == LEFT ? -16 : 16,
203 bbox.get_height()*0.66666 - 32);
204 grabbed_object->grab(*this, pos);
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(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
283 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
284 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
293 // we get slower when not pressing any keys
295 if(fabs(vx) < WALK_SPEED) {
299 ax = WALK_ACCELERATION_X * 1.5;
301 ax = WALK_ACCELERATION_X * -1.5;
306 // if we're on ice slow down acceleration or deceleration
307 if (isice(base.x, base.y + base.height))
309 /* the acceleration/deceleration rate on ice is inversely proportional to
310 * the current velocity.
313 // increasing 1 will increase acceleration/deceleration rate
314 // decreasing 1 will decrease acceleration/deceleration rate
315 // must stay above zero, though
316 if (ax != 0) ax *= 1 / fabs(vx);
320 // extend/shrink tux collision rectangle so that we fall through/walk over 1
322 if(fabsf(vx) > MAX_WALK_XM) {
325 bbox.set_width(31.8);
328 physic.set_velocity(vx, vy);
329 physic.set_acceleration(ax, ay);
333 Player::handle_vertical_input()
337 fall_mode = ON_GROUND;
338 last_ground_y = get_pos().y;
340 if(get_pos().y > last_ground_y)
342 else if(fall_mode == ON_GROUND)
346 if(on_ground()) { /* Make sure jumping is off. */
349 backflipping = false;
350 backflip_direction = 0;
355 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
357 if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
358 physic.set_velocity_y(300);
359 else { //do a backflip
361 physic.set_velocity_y(580);
362 backflip_timer.start(0.15);
365 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
366 physic.set_velocity_y(580);
368 physic.set_velocity_y(520);
370 //bbox.move(Vector(0, -1));
374 sound_manager->play("sounds/bigjump.wav");
376 sound_manager->play("sounds/jump.wav");
377 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
378 if (!backflipping && jumping && physic.get_velocity_y() > 0) {
380 physic.set_velocity_y(0);
384 /* In case the player has pressed Down while in a certain range of air,
385 enable butt jump action */
386 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
387 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
390 /* When Down is not held anymore, disable butt jump */
391 if(butt_jump && !controller->hold(Controller::DOWN))
396 if (butt_jump && on_ground() && is_big()) {
399 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
401 Sector::current()->add_smoke_cloud(
402 Vector(get_pos().x - 32, get_pos().y + 32));
406 // Break bricks beneath Tux
407 if(Sector::current()->trybreakbrick(
408 Vector(base.x + 1, base.y + base.height), false)
409 || Sector::current()->trybreakbrick(
410 Vector(base.x + base.width - 1, base.y + base.height), false)) {
411 physic.set_velocity_y(2);
415 // Kill nearby badguys
416 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
417 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
418 i != gameobjects.end();
420 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
422 // don't kill when badguys are already dying or in a certain mode
423 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
424 badguy->mode != BadGuy::BOMB_EXPLODE) {
425 if (fabsf(base.x - badguy->base.x) < 96 &&
426 fabsf(base.y - badguy->base.y) < 64)
434 /** jumping is only allowed if we're about to touch ground soon and if the
435 * button has been up in between the last jump
439 if ( (issolid(get_pos().x + bbox.get_width() / 2,
440 get_pos().y + bbox.get_height() + 64) ||
441 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
442 issolid(get_pos().x + bbox.get_width() - 1,
443 get_pos().y + bbox.get_height() + 64))
446 && input.jump && !input.old_jump)
454 Player::handle_input()
456 /* Handle horizontal movement: */
457 if (!backflipping) handle_horizontal_input();
459 if (backflip_direction == 0) {
460 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
462 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
463 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
468 if (on_ground() && !controller->hold(Controller::JUMP))
470 handle_vertical_input();
473 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
474 if(Sector::current()->add_bullet(
475 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
476 : Vector(32, bbox.get_height()/2)),
477 physic.get_velocity_x(), dir))
478 shooting_timer.start(SHOOTING_TIME);
482 if (controller->hold(Controller::DOWN) && is_big() && !duck
483 && physic.get_velocity_y() == 0 && on_ground()) {
485 bbox.move(Vector(0, 32));
486 bbox.set_height(31.8);
487 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
488 // try if we can really unduck
489 bbox.move(Vector(0, -32));
490 bbox.set_height(63.8);
494 // when unducking in air we need some space to do so
495 if(on_ground() || !collision_object_map(bbox)) {
498 // undo the ducking changes
499 bbox.move(Vector(0, 32));
500 bbox.set_height(31.8);
507 Player::set_bonus(BonusType type, bool animate)
509 if(player_status->bonus >= type)
512 if(player_status->bonus == NO_BONUS) {
513 bbox.set_height(63.8);
514 bbox.move(Vector(0, -32));
516 growing_timer.start(GROWING_TIME);
519 player_status->bonus = type;
523 Player::draw(DrawingContext& context)
525 TuxBodyParts* tux_body;
527 if (player_status->bonus == GROWUP_BONUS)
529 else if (player_status->bonus == FIRE_BONUS)
531 else if (player_status->bonus == ICE_BONUS)
534 tux_body = small_tux;
536 int layer = LAYER_OBJECTS + 10;
538 /* Set Tux sprite action */
539 if (duck && is_big())
542 tux_body->set_action("duck-left");
544 tux_body->set_action("duck-right");
546 else if (skidding_timer.started() && !skidding_timer.check())
549 tux_body->set_action("skid-left");
551 tux_body->set_action("skid-right");
553 else if (kick_timer.started() && !kick_timer.check())
556 tux_body->set_action("kick-left");
558 tux_body->set_action("kick-right");
560 else if (butt_jump && is_big())
563 tux_body->set_action("buttjump-left");
565 tux_body->set_action("buttjump-right");
567 else if (physic.get_velocity_y() != 0)
570 tux_body->set_action("jump-left");
572 tux_body->set_action("jump-right");
576 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
579 tux_body->set_action("stand-left");
581 tux_body->set_action("stand-right");
586 tux_body->set_action("walk-left");
588 tux_body->set_action("walk-right");
592 if(idle_timer.check())
597 tux_body->head->set_action("idle-left", 1);
599 tux_body->head->set_action("idle-right", 1);
604 // Tux is holding something
605 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
606 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
611 tux_body->arms->set_action("duck+grab-left");
613 tux_body->arms->set_action("duck+grab-right");
618 tux_body->arms->set_action("grab-left");
620 tux_body->arms->set_action("grab-right");
626 smalltux_gameover->draw(context, get_pos(), layer);
627 } else if(growing_timer.get_timeleft() > 0) {
631 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
632 int((growing_timer.get_timegone() *
633 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
635 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
636 int((growing_timer.get_timegone() *
637 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
642 context.draw_surface(growingtux_right[
643 int((growing_timer.get_timegone() *
644 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
646 context.draw_surface(growingtux_left[
647 int((growing_timer.get_timegone() *
648 GROWING_FRAMES) / GROWING_TIME)],
652 else if (safe_timer.started() && size_t(game_time*40)%2)
655 tux_body->draw(context, get_pos(), layer);
657 // Draw blinking star overlay
658 if (invincible_timer.started() &&
659 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
660 || size_t(game_time*20)%2)
663 if (!is_big() || duck)
664 smalltux_star->draw(context, get_pos(), layer + 5);
666 bigtux_star->draw(context, get_pos(), layer + 5);
671 Player::collision_tile(uint32_t tile_attributes)
673 if(tile_attributes & Tile::HURTS)
678 Player::collision(GameObject& other, const CollisionHit& hit)
680 Bullet* bullet = dynamic_cast<Bullet*> (&other);
685 Portable* portable = dynamic_cast<Portable*> (&other);
686 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
687 && fabsf(hit.normal.x) > .9) {
688 grabbed_object = portable;
692 if(other.get_flags() & FLAG_SOLID) {
693 if(hit.normal.y < 0) { // landed on floor?
694 if(physic.get_velocity_y() < 0)
695 physic.set_velocity_y(0);
696 on_ground_flag = true;
697 } else if(hit.normal.y > 0) { // bumped against the roof
698 physic.set_velocity_y(.1);
701 if(fabsf(hit.normal.x) > .9) { // hit on the side?
702 physic.set_velocity_x(0);
708 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
710 if(controller->pressed(Controller::UP))
711 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
716 MovingObject* moving_object = static_cast<MovingObject*> (&other);
717 if(moving_object->get_group() == COLGROUP_TOUCHABLE)
727 Player::make_invincible()
729 sound_manager->play("sounds/invincible.wav");
730 invincible_timer.start(TUX_INVINCIBLE_TIME);
731 Sector::current()->play_music(HERRING_MUSIC);
736 Player::kill(HurtMode mode)
738 if(dying || deactivated)
742 safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
745 sound_manager->play("sounds/hurt.wav");
747 physic.set_velocity_x(0);
749 if (mode == SHRINK && is_big())
751 if (player_status->bonus == FIRE_BONUS
752 || player_status->bonus == ICE_BONUS)
754 safe_timer.start(TUX_SAFE_TIME);
755 player_status->bonus = GROWUP_BONUS;
759 growing_timer.start(GROWING_TIME);
760 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
761 bbox.set_height(31.8);
763 player_status->bonus = NO_BONUS;
768 physic.enable_gravity(true);
769 physic.set_acceleration(0, 0);
770 physic.set_velocity(0, 700);
771 player_status->lives -= 1;
772 player_status->bonus = NO_BONUS;
774 dying_timer.start(3.0);
775 set_group(COLGROUP_DISABLED);
780 Player::move(const Vector& vector)
782 bbox.set_pos(vector);
784 bbox.set_size(31.8, 63.8);
786 bbox.set_size(31.8, 31.8);
787 on_ground_flag = false;
789 last_ground_y = vector.y;
795 Player::check_bounds(Camera* camera)
797 /* Keep tux in bounds: */
799 { // Lock Tux to the size of the level, so that he doesn't fall of
801 bbox.set_pos(Vector(0, get_pos().y));
804 /* Keep in-bounds, vertically: */
805 if (get_pos().y > Sector::current()->solids->get_height() * 32)
812 // can happen if back scrolling is disabled
813 if(get_pos().x < camera->get_translation().x) {
814 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
817 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
820 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
829 if(collision_object_map(bbox)) {
838 Player::bounce(BadGuy& )
840 if(controller->hold(Controller::JUMP))
841 physic.set_velocity_y(520);
843 physic.set_velocity_y(300);
846 //Scripting Functions Below
852 physic.set_velocity_x(0);
853 physic.set_velocity_y(0);
862 void Player::walk(float speed)
864 physic.set_velocity_x(speed);