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("images/creatures/tux_small/smalltux-gameover.sprite");
105 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
106 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
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;
144 floor_normal = Vector(0,-1);
150 Player::set_controller(Controller* controller)
152 this->controller = controller;
156 Player::update(float elapsed_time)
158 // do we need to enable gravity again?
161 lower.move(Vector(0, 4.0));
162 if(Sector::current()->is_free_space(lower)) {
163 physic.enable_gravity(true);
164 on_ground_flag = false;
168 if(dying && dying_timer.check()) {
173 if(!controller->hold(Controller::ACTION) && grabbed_object) {
174 // move the grabbed object a bit away from tux
175 Vector pos = get_pos() +
176 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
177 bbox.get_height()*0.66666 - 32);
178 Rect dest(pos, pos + Vector(32, 32));
179 if(Sector::current()->is_free_space(dest)) {
180 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
182 moving_object->set_pos(pos);
185 std::cout << "Non MovingObjetc grabbed?!?\n";
188 grabbed_object->ungrab(*this, dir);
193 if(!dying && !deactivated)
196 movement = physic.get_movement(elapsed_time);
199 // special exception for cases where we're stuck under tiles after
200 // being ducked. In this case we drift out
201 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
202 && collision_object_map(base)) {
203 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
204 previous_base = old_base = base;
208 if(grabbed_object != 0) {
209 Vector pos = get_pos() +
210 Vector(dir == LEFT ? -16 : 16,
211 bbox.get_height()*0.66666 - 32);
212 grabbed_object->grab(*this, pos, dir);
219 return on_ground_flag;
225 if(player_status->bonus == NO_BONUS)
232 Player::handle_horizontal_input()
234 float vx = physic.get_velocity_x();
235 float vy = physic.get_velocity_y();
236 float ax = physic.get_acceleration_x();
237 float ay = physic.get_acceleration_y();
240 if(!duck || physic.get_velocity_y() != 0) {
241 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
245 } else if(!controller->hold(Controller::LEFT)
246 && controller->hold(Controller::RIGHT)) {
253 if (!controller->hold(Controller::ACTION)) {
254 ax = dirsign * WALK_ACCELERATION_X;
256 if(vx >= MAX_WALK_XM && dirsign > 0) {
259 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
264 ax = dirsign * RUN_ACCELERATION_X;
266 if(vx >= MAX_RUN_XM && dirsign > 0) {
269 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
275 // we can reach WALK_SPEED without any acceleration
276 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
277 vx = dirsign * WALK_SPEED;
280 // changing directions?
281 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
283 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
284 skidding_timer.start(SKID_TIME);
285 sound_manager->play("sounds/skid.wav");
286 // dust some particles
287 Sector::current()->add_object(
289 Vector(dir == RIGHT ? bbox.p2.x : bbox.p1.x, bbox.p2.y),
290 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
291 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
300 // we get slower when not pressing any keys
302 if(fabs(vx) < WALK_SPEED) {
306 ax = WALK_ACCELERATION_X * 1.5;
308 ax = WALK_ACCELERATION_X * -1.5;
313 // if we're on ice slow down acceleration or deceleration
314 if (isice(base.x, base.y + base.height))
316 /* the acceleration/deceleration rate on ice is inversely proportional to
317 * the current velocity.
320 // increasing 1 will increase acceleration/deceleration rate
321 // decreasing 1 will decrease acceleration/deceleration rate
322 // must stay above zero, though
323 if (ax != 0) ax *= 1 / fabs(vx);
327 // extend/shrink tux collision rectangle so that we fall through/walk over 1
329 if(fabsf(vx) > MAX_WALK_XM) {
332 bbox.set_width(31.8);
335 // on downward slopes, adjust vertical velocity to match slope angle
337 if (floor_normal.y != 0) {
338 if ((floor_normal.x * vx) > 0) {
339 // we overdo it a little, just to be on the safe side
340 vy = vx * (floor_normal.x / floor_normal.y) * 2;
345 physic.set_velocity(vx, vy);
346 physic.set_acceleration(ax, ay);
350 Player::handle_vertical_input()
354 fall_mode = ON_GROUND;
355 last_ground_y = get_pos().y;
357 if(get_pos().y > last_ground_y)
359 else if(fall_mode == ON_GROUND)
363 if(on_ground()) { /* Make sure jumping is off. */
366 backflipping = false;
367 backflip_direction = 0;
372 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
374 if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
375 physic.set_velocity_y(300);
376 else { //do a backflip
378 physic.set_velocity_y(580);
379 backflip_timer.start(0.15);
382 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
383 physic.set_velocity_y(580);
385 physic.set_velocity_y(520);
387 //bbox.move(Vector(0, -1));
391 sound_manager->play("sounds/bigjump.wav");
393 sound_manager->play("sounds/jump.wav");
394 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
395 if (!backflipping && jumping && physic.get_velocity_y() > 0) {
397 physic.set_velocity_y(0);
401 /* In case the player has pressed Down while in a certain range of air,
402 enable butt jump action */
403 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
404 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
407 /* When Down is not held anymore, disable butt jump */
408 if(butt_jump && !controller->hold(Controller::DOWN))
413 if (butt_jump && on_ground() && is_big()) {
416 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
418 Sector::current()->add_smoke_cloud(
419 Vector(get_pos().x - 32, get_pos().y + 32));
423 // Break bricks beneath Tux
424 if(Sector::current()->trybreakbrick(
425 Vector(base.x + 1, base.y + base.height), false)
426 || Sector::current()->trybreakbrick(
427 Vector(base.x + base.width - 1, base.y + base.height), false)) {
428 physic.set_velocity_y(2);
432 // Kill nearby badguys
433 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
434 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
435 i != gameobjects.end();
437 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
439 // don't kill when badguys are already dying or in a certain mode
440 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
441 badguy->mode != BadGuy::BOMB_EXPLODE) {
442 if (fabsf(base.x - badguy->base.x) < 96 &&
443 fabsf(base.y - badguy->base.y) < 64)
451 /** jumping is only allowed if we're about to touch ground soon and if the
452 * button has been up in between the last jump
456 if ( (issolid(get_pos().x + bbox.get_width() / 2,
457 get_pos().y + bbox.get_height() + 64) ||
458 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
459 issolid(get_pos().x + bbox.get_width() - 1,
460 get_pos().y + bbox.get_height() + 64))
463 && input.jump && !input.old_jump)
471 Player::handle_input()
473 /* Handle horizontal movement: */
474 if (!backflipping) handle_horizontal_input();
476 if (backflip_direction == 0) {
477 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
479 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
480 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
485 if (on_ground() && !controller->hold(Controller::JUMP))
487 handle_vertical_input();
490 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
491 if(Sector::current()->add_bullet(
492 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
493 : Vector(32, bbox.get_height()/2)),
494 physic.get_velocity_x(), dir))
495 shooting_timer.start(SHOOTING_TIME);
499 if (controller->hold(Controller::DOWN) && is_big() && !duck
500 && physic.get_velocity_y() == 0 && on_ground()) {
502 bbox.move(Vector(0, 32));
503 bbox.set_height(31.8);
504 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
505 // if we have some velocity left then check if there is space for
507 bbox.move(Vector(0, -32));
508 bbox.set_height(63.8);
509 if(Sector::current()->is_free_space(bbox) || (
510 physic.get_velocity_x() > -.01 && physic.get_velocity_x() < .01
511 && physic.get_velocity_y() > -.01 && physic.get_velocity_y() < .01))
515 // undo the ducking changes
516 bbox.move(Vector(0, 32));
517 bbox.set_height(31.8);
523 Player::set_bonus(BonusType type, bool animate)
525 if(player_status->bonus >= type)
528 if(player_status->bonus == NO_BONUS) {
529 bbox.set_height(63.8);
530 bbox.move(Vector(0, -32));
532 growing_timer.start(GROWING_TIME);
535 player_status->bonus = type;
539 Player::set_visible(bool visible)
541 this->visible = visible;
545 Player::get_visible()
553 kick_timer.start(KICK_TIME);
557 Player::draw(DrawingContext& context)
562 TuxBodyParts* tux_body;
564 if (player_status->bonus == GROWUP_BONUS)
566 else if (player_status->bonus == FIRE_BONUS)
568 else if (player_status->bonus == ICE_BONUS)
571 tux_body = small_tux;
573 int layer = LAYER_OBJECTS + 10;
575 /* Set Tux sprite action */
576 if (duck && is_big())
579 tux_body->set_action("duck-left");
581 tux_body->set_action("duck-right");
583 else if (skidding_timer.started() && !skidding_timer.check())
586 tux_body->set_action("skid-left");
588 tux_body->set_action("skid-right");
590 else if (kick_timer.started() && !kick_timer.check())
593 tux_body->set_action("kick-left");
595 tux_body->set_action("kick-right");
597 else if (butt_jump && is_big())
600 tux_body->set_action("buttjump-left");
602 tux_body->set_action("buttjump-right");
604 else if (physic.get_velocity_y() != 0)
607 tux_body->set_action("jump-left");
609 tux_body->set_action("jump-right");
613 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
616 tux_body->set_action("stand-left");
618 tux_body->set_action("stand-right");
623 tux_body->set_action("walk-left");
625 tux_body->set_action("walk-right");
629 if(idle_timer.check())
634 tux_body->head->set_action("idle-left", 1);
636 tux_body->head->set_action("idle-right", 1);
641 // Tux is holding something
642 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
643 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
648 tux_body->arms->set_action("duck+grab-left");
650 tux_body->arms->set_action("duck+grab-right");
655 tux_body->arms->set_action("grab-left");
657 tux_body->arms->set_action("grab-right");
663 smalltux_gameover->draw(context, get_pos(), layer);
664 } else if(growing_timer.get_timeleft() > 0) {
668 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
669 int((growing_timer.get_timegone() *
670 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
672 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
673 int((growing_timer.get_timegone() *
674 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
679 context.draw_surface(growingtux_right[
680 int((growing_timer.get_timegone() *
681 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
683 context.draw_surface(growingtux_left[
684 int((growing_timer.get_timegone() *
685 GROWING_FRAMES) / GROWING_TIME)],
689 else if (safe_timer.started() && size_t(game_time*40)%2)
692 tux_body->draw(context, get_pos(), layer);
694 // Draw blinking star overlay
695 if (invincible_timer.started() &&
696 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
697 || size_t(game_time*20)%2)
700 if (!is_big() || duck)
701 smalltux_star->draw(context, get_pos(), layer + 5);
703 bigtux_star->draw(context, get_pos(), layer + 5);
708 Player::collision_tile(uint32_t tile_attributes)
710 if(tile_attributes & Tile::HURTS)
715 Player::collision(GameObject& other, const CollisionHit& hit)
717 Bullet* bullet = dynamic_cast<Bullet*> (&other);
722 if(other.get_flags() & FLAG_PORTABLE) {
723 Portable* portable = dynamic_cast<Portable*> (&other);
724 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
725 && fabsf(hit.normal.x) > .9) {
726 grabbed_object = portable;
731 if(other.get_flags() & FLAG_SOLID) {
732 if(hit.normal.y < 0) { // landed on floor?
733 if(physic.get_velocity_y() < 0)
734 physic.set_velocity_y(0);
735 on_ground_flag = true;
737 // remember normal of this tile
738 if (hit.normal.y > -0.9) {
739 floor_normal.x = hit.normal.x;
740 floor_normal.y = hit.normal.y;
742 // slowly adjust to unisolid tiles.
743 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
744 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
745 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
749 physic.enable_gravity(false);
750 } else if(hit.normal.y > 0) { // bumped against the roof
751 physic.set_velocity_y(.1);
754 if(fabsf(hit.normal.x) > .9) { // hit on the side?
755 physic.set_velocity_x(0);
762 assert(dynamic_cast<MovingObject*> (&other) != NULL);
764 MovingObject* moving_object = static_cast<MovingObject*> (&other);
765 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
766 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
768 if(controller->pressed(Controller::UP))
769 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
775 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
777 if(safe_timer.started())
787 Player::make_invincible()
789 sound_manager->play("sounds/invincible.wav");
790 invincible_timer.start(TUX_INVINCIBLE_TIME);
791 Sector::current()->play_music(HERRING_MUSIC);
796 Player::kill(HurtMode mode)
798 if(dying || deactivated)
802 (safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0))
805 sound_manager->play("sounds/hurt.wav");
807 physic.set_velocity_x(0);
809 if (mode == SHRINK && is_big())
811 if (player_status->bonus == FIRE_BONUS
812 || player_status->bonus == ICE_BONUS)
814 safe_timer.start(TUX_SAFE_TIME);
815 player_status->bonus = GROWUP_BONUS;
819 growing_timer.start(GROWING_TIME);
820 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
821 bbox.set_height(31.8);
823 player_status->bonus = NO_BONUS;
828 physic.enable_gravity(true);
829 physic.set_acceleration(0, 0);
830 physic.set_velocity(0, 700);
831 player_status->lives -= 1;
832 player_status->bonus = NO_BONUS;
834 dying_timer.start(3.0);
835 set_group(COLGROUP_DISABLED);
837 DisplayEffect* effect = new DisplayEffect();
838 effect->fade_out(3.0);
839 Sector::current()->add_object(effect);
840 sound_manager->stop_music(3.0);
845 Player::move(const Vector& vector)
847 bbox.set_pos(vector);
849 bbox.set_size(31.8, 63.8);
851 bbox.set_size(31.8, 31.8);
853 last_ground_y = vector.y;
859 Player::check_bounds(Camera* camera)
861 /* Keep tux in bounds: */
863 { // Lock Tux to the size of the level, so that he doesn't fall of
865 bbox.set_pos(Vector(0, get_pos().y));
868 /* Keep in-bounds, vertically: */
869 if (get_pos().y > Sector::current()->solids->get_height() * 32)
876 // can happen if back scrolling is disabled
877 if(get_pos().x < camera->get_translation().x) {
878 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
881 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
884 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
893 if(collision_object_map(bbox)) {
902 Player::bounce(BadGuy& )
904 if(controller->hold(Controller::JUMP))
905 physic.set_velocity_y(520);
907 physic.set_velocity_y(300);
910 //Scripting Functions Below
916 physic.set_velocity_x(0);
917 physic.set_velocity_y(0);
926 void Player::walk(float speed)
928 physic.set_velocity_x(speed);