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 "trigger/trigger_base.hpp"
42 #include "control/joystickkeyboardcontroller.hpp"
44 #include "player_status.hpp"
46 static const int TILES_FOR_BUTTJUMP = 3;
47 static const float SHOOTING_TIME = .150;
48 /// time before idle animation starts
49 static const float IDLE_TIME = 2.5;
51 static const float WALK_ACCELERATION_X = 300;
52 static const float RUN_ACCELERATION_X = 400;
53 static const float SKID_XM = 200;
54 static const float SKID_TIME = .3;
55 static const float MAX_WALK_XM = 230;
56 static const float MAX_RUN_XM = 320;
57 static const float WALK_SPEED = 100;
60 Surface* growingtux_left[GROWING_FRAMES];
61 Surface* growingtux_right[GROWING_FRAMES];
63 Surface* tux_life = 0;
65 TuxBodyParts* small_tux = 0;
66 TuxBodyParts* big_tux = 0;
67 TuxBodyParts* fire_tux = 0;
68 TuxBodyParts* ice_tux = 0;
71 TuxBodyParts::set_action(std::string action, int loops)
74 head->set_action(action, loops);
76 body->set_action(action, loops);
78 arms->set_action(action, loops);
80 feet->set_action(action, loops);
84 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
87 head->draw(context, pos, layer-1);
89 body->draw(context, pos, layer-3);
91 arms->draw(context, pos, layer);
93 feet->draw(context, pos, layer-2);
96 Player::Player(PlayerStatus* _player_status)
97 : player_status(_player_status), grabbed_object(0)
99 controller = main_controller;
100 smalltux_gameover = sprite_manager->create("smalltux-gameover");
101 smalltux_star = sprite_manager->create("smalltux-star");
102 bigtux_star = sprite_manager->create("bigtux-star");
108 delete smalltux_gameover;
109 delete smalltux_star;
117 bbox.set_size(31.8, 63.8);
119 bbox.set_size(31.8, 31.8);
128 fall_mode = ON_GROUND;
133 backflipping = false;
134 backflip_direction = 0;
136 on_ground_flag = false;
143 Player::set_controller(Controller* controller)
145 this->controller = controller;
149 Player::update(float elapsed_time)
151 if(dying && dying_timer.check()) {
156 if(!controller->hold(Controller::ACTION) && grabbed_object) {
157 // move the grabbed object a bit away from tux
158 Vector pos = get_pos() +
159 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
160 bbox.get_height()*0.66666 - 32);
161 Rect dest(pos, pos + Vector(32, 32));
162 if(Sector::current()->is_free_space(dest)) {
163 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
165 moving_object->set_pos(pos);
168 std::cout << "Non MovingObjetc grabbed?!?\n";
175 if(!dying && !deactivated)
178 movement = physic.get_movement(elapsed_time);
179 on_ground_flag = false;
182 // special exception for cases where we're stuck under tiles after
183 // being ducked. In this case we drift out
184 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
185 && collision_object_map(base)) {
186 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
187 previous_base = old_base = base;
191 if(grabbed_object != 0) {
192 Vector pos = get_pos() +
193 Vector(dir == LEFT ? -16 : 16,
194 bbox.get_height()*0.66666 - 32);
195 grabbed_object->grab(*this, pos);
202 return on_ground_flag;
208 if(player_status->bonus == NO_BONUS)
215 Player::handle_horizontal_input()
217 float vx = physic.get_velocity_x();
218 float vy = physic.get_velocity_y();
219 float ax = physic.get_acceleration_x();
220 float ay = physic.get_acceleration_y();
223 if(!duck || physic.get_velocity_y() != 0) {
224 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
228 } else if(!controller->hold(Controller::LEFT)
229 && controller->hold(Controller::RIGHT)) {
236 if (!controller->hold(Controller::ACTION)) {
237 ax = dirsign * WALK_ACCELERATION_X;
239 if(vx >= MAX_WALK_XM && dirsign > 0) {
242 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
247 ax = dirsign * RUN_ACCELERATION_X;
249 if(vx >= MAX_RUN_XM && dirsign > 0) {
252 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
258 // we can reach WALK_SPEED without any acceleration
259 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
260 vx = dirsign * WALK_SPEED;
263 // changing directions?
264 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
266 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
267 skidding_timer.start(SKID_TIME);
268 sound_manager->play("sounds/skid.wav");
269 // dust some particles
270 Sector::current()->add_object(
272 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
274 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
275 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
284 // we get slower when not pressing any keys
286 if(fabs(vx) < WALK_SPEED) {
290 ax = WALK_ACCELERATION_X * 1.5;
292 ax = WALK_ACCELERATION_X * -1.5;
297 // if we're on ice slow down acceleration or deceleration
298 if (isice(base.x, base.y + base.height))
300 /* the acceleration/deceleration rate on ice is inversely proportional to
301 * the current velocity.
304 // increasing 1 will increase acceleration/deceleration rate
305 // decreasing 1 will decrease acceleration/deceleration rate
306 // must stay above zero, though
307 if (ax != 0) ax *= 1 / fabs(vx);
311 // extend/shrink tux collision rectangle so that we fall through/walk over 1
313 if(fabsf(vx) > MAX_WALK_XM) {
316 bbox.set_width(31.8);
319 physic.set_velocity(vx, vy);
320 physic.set_acceleration(ax, ay);
324 Player::handle_vertical_input()
328 fall_mode = ON_GROUND;
329 last_ground_y = get_pos().y;
331 if(get_pos().y > last_ground_y)
333 else if(fall_mode == ON_GROUND)
337 if(on_ground()) { /* Make sure jumping is off. */
340 backflipping = false;
341 backflip_direction = 0;
346 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
348 if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
349 physic.set_velocity_y(300);
350 else { //do a backflip
352 physic.set_velocity_y(580);
353 backflip_timer.start(0.15);
356 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
357 physic.set_velocity_y(580);
359 physic.set_velocity_y(520);
361 //bbox.move(Vector(0, -1));
365 sound_manager->play("sounds/bigjump.wav");
367 sound_manager->play("sounds/jump.wav");
368 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
369 if (!backflipping && jumping && physic.get_velocity_y() > 0) {
371 physic.set_velocity_y(0);
375 /* In case the player has pressed Down while in a certain range of air,
376 enable butt jump action */
377 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
378 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
381 /* When Down is not held anymore, disable butt jump */
382 if(butt_jump && !controller->hold(Controller::DOWN))
387 if (butt_jump && on_ground() && is_big()) {
390 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
392 Sector::current()->add_smoke_cloud(
393 Vector(get_pos().x - 32, get_pos().y + 32));
397 // Break bricks beneath Tux
398 if(Sector::current()->trybreakbrick(
399 Vector(base.x + 1, base.y + base.height), false)
400 || Sector::current()->trybreakbrick(
401 Vector(base.x + base.width - 1, base.y + base.height), false)) {
402 physic.set_velocity_y(2);
406 // Kill nearby badguys
407 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
408 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
409 i != gameobjects.end();
411 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
413 // don't kill when badguys are already dying or in a certain mode
414 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
415 badguy->mode != BadGuy::BOMB_EXPLODE) {
416 if (fabsf(base.x - badguy->base.x) < 96 &&
417 fabsf(base.y - badguy->base.y) < 64)
425 /** jumping is only allowed if we're about to touch ground soon and if the
426 * button has been up in between the last jump
430 if ( (issolid(get_pos().x + bbox.get_width() / 2,
431 get_pos().y + bbox.get_height() + 64) ||
432 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
433 issolid(get_pos().x + bbox.get_width() - 1,
434 get_pos().y + bbox.get_height() + 64))
437 && input.jump && !input.old_jump)
445 Player::handle_input()
447 /* Handle horizontal movement: */
448 if (!backflipping) handle_horizontal_input();
450 if (backflip_direction == 0) {
451 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
453 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
454 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
459 if (on_ground() && !controller->hold(Controller::JUMP))
461 handle_vertical_input();
464 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
465 if(Sector::current()->add_bullet(
466 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
467 : Vector(32, bbox.get_height()/2)),
468 physic.get_velocity_x(), dir))
469 shooting_timer.start(SHOOTING_TIME);
473 if (controller->hold(Controller::DOWN) && is_big() && !duck
474 && physic.get_velocity_y() == 0 && on_ground()) {
476 bbox.move(Vector(0, 32));
477 bbox.set_height(31.8);
478 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
479 // try if we can really unduck
480 bbox.move(Vector(0, -32));
481 bbox.set_height(63.8);
485 // when unducking in air we need some space to do so
486 if(on_ground() || !collision_object_map(bbox)) {
489 // undo the ducking changes
490 bbox.move(Vector(0, 32));
491 bbox.set_height(31.8);
498 Player::set_bonus(BonusType type, bool animate)
500 if(player_status->bonus >= type)
503 if(player_status->bonus == NO_BONUS) {
504 bbox.set_height(63.8);
505 bbox.move(Vector(0, -32));
507 growing_timer.start(GROWING_TIME);
510 player_status->bonus = type;
514 Player::draw(DrawingContext& context)
516 TuxBodyParts* tux_body;
518 if (player_status->bonus == GROWUP_BONUS)
520 else if (player_status->bonus == FIRE_BONUS)
522 else if (player_status->bonus == ICE_BONUS)
525 tux_body = small_tux;
527 int layer = LAYER_OBJECTS + 10;
529 /* Set Tux sprite action */
530 if (duck && is_big())
533 tux_body->set_action("duck-left");
535 tux_body->set_action("duck-right");
537 else if (skidding_timer.started() && !skidding_timer.check())
540 tux_body->set_action("skid-left");
542 tux_body->set_action("skid-right");
544 else if (kick_timer.started() && !kick_timer.check())
547 tux_body->set_action("kick-left");
549 tux_body->set_action("kick-right");
551 else if (butt_jump && is_big())
554 tux_body->set_action("buttjump-left");
556 tux_body->set_action("buttjump-right");
558 else if (physic.get_velocity_y() != 0)
561 tux_body->set_action("jump-left");
563 tux_body->set_action("jump-right");
567 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
570 tux_body->set_action("stand-left");
572 tux_body->set_action("stand-right");
577 tux_body->set_action("walk-left");
579 tux_body->set_action("walk-right");
583 if(idle_timer.check())
588 tux_body->head->set_action("idle-left", 1);
590 tux_body->head->set_action("idle-right", 1);
595 // Tux is holding something
596 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
597 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
602 tux_body->arms->set_action("duck+grab-left");
604 tux_body->arms->set_action("duck+grab-right");
609 tux_body->arms->set_action("grab-left");
611 tux_body->arms->set_action("grab-right");
617 smalltux_gameover->draw(context, get_pos(), layer);
618 } else if(growing_timer.get_timeleft() > 0) {
622 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
623 int((growing_timer.get_timegone() *
624 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
626 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
627 int((growing_timer.get_timegone() *
628 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
633 context.draw_surface(growingtux_right[
634 int((growing_timer.get_timegone() *
635 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
637 context.draw_surface(growingtux_left[
638 int((growing_timer.get_timegone() *
639 GROWING_FRAMES) / GROWING_TIME)],
643 else if (safe_timer.started() && size_t(game_time*40)%2)
646 tux_body->draw(context, get_pos(), layer);
648 // Draw blinking star overlay
649 if (invincible_timer.started() &&
650 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
651 || size_t(game_time*20)%2)
654 if (!is_big() || duck)
655 smalltux_star->draw(context, get_pos(), layer + 5);
657 bigtux_star->draw(context, get_pos(), layer + 5);
662 Player::collision_tile(uint32_t tile_attributes)
664 if(tile_attributes & Tile::HURTS)
669 Player::collision(GameObject& other, const CollisionHit& hit)
671 Portable* portable = dynamic_cast<Portable*> (&other);
672 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
673 && fabsf(hit.normal.x) > .9) {
674 grabbed_object = portable;
678 if(other.get_flags() & FLAG_SOLID) {
679 if(hit.normal.y < 0) { // landed on floor?
680 if(physic.get_velocity_y() < 0)
681 physic.set_velocity_y(0);
682 on_ground_flag = true;
683 } else if(hit.normal.y > 0) { // bumped against the roof
684 physic.set_velocity_y(.1);
687 if(fabsf(hit.normal.x) > .9) { // hit on the side?
688 physic.set_velocity_x(0);
694 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
696 if(controller->pressed(Controller::UP))
697 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
702 MovingObject* moving_object = static_cast<MovingObject*> (&other);
703 if(moving_object->get_group() == COLGROUP_TOUCHABLE)
713 Player::make_invincible()
715 sound_manager->play("sounds/invincible.wav");
716 invincible_timer.start(TUX_INVINCIBLE_TIME);
717 Sector::current()->play_music(HERRING_MUSIC);
722 Player::kill(HurtMode mode)
724 if(dying || deactivated)
728 safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
731 sound_manager->play("sounds/hurt.wav");
733 physic.set_velocity_x(0);
735 if (mode == SHRINK && is_big())
737 if (player_status->bonus == FIRE_BONUS
738 || player_status->bonus == ICE_BONUS)
740 safe_timer.start(TUX_SAFE_TIME);
741 player_status->bonus = GROWUP_BONUS;
745 growing_timer.start(GROWING_TIME);
746 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
747 bbox.set_height(31.8);
749 player_status->bonus = NO_BONUS;
754 physic.enable_gravity(true);
755 physic.set_acceleration(0, 0);
756 physic.set_velocity(0, 700);
757 player_status->lives -= 1;
758 player_status->bonus = NO_BONUS;
760 dying_timer.start(3.0);
761 set_group(COLGROUP_DISABLED);
766 Player::move(const Vector& vector)
768 bbox.set_pos(vector);
770 bbox.set_size(31.8, 63.8);
772 bbox.set_size(31.8, 31.8);
773 on_ground_flag = false;
775 last_ground_y = vector.y;
781 Player::check_bounds(Camera* camera)
783 /* Keep tux in bounds: */
785 { // Lock Tux to the size of the level, so that he doesn't fall of
787 bbox.set_pos(Vector(0, get_pos().y));
790 /* Keep in-bounds, vertically: */
791 if (get_pos().y > Sector::current()->solids->get_height() * 32)
798 // can happen if back scrolling is disabled
799 if(get_pos().x < camera->get_translation().x) {
800 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
803 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
806 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
815 if(collision_object_map(bbox)) {
824 Player::bounce(BadGuy& )
826 if(controller->hold(Controller::JUMP))
827 physic.set_velocity_y(520);
829 physic.set_velocity_y(300);
832 //Scripting Functions Below
838 physic.set_velocity_x(0);
839 physic.set_velocity_y(0);
848 void Player::walk(float speed)
850 physic.set_velocity_x(speed);