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) {
158 // move the grabbed object a bit away from tux
159 Vector pos = get_pos() +
160 Vector(dir == LEFT ? -bbox.get_width() : bbox.get_width(),
161 bbox.get_height()*0.66666 - 32);
162 MovingObject* object = dynamic_cast<MovingObject*> (grabbed_object);
164 object->set_pos(pos);
167 std::cout << "Non MovingObjetc grabbed?!?\n";
172 if(!dying && !deactivated)
175 movement = physic.get_movement(elapsed_time);
176 on_ground_flag = false;
179 // special exception for cases where we're stuck under tiles after
180 // being ducked. In this case we drift out
181 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
182 && collision_object_map(base)) {
183 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
184 previous_base = old_base = base;
188 if(grabbed_object != 0) {
189 Vector pos = get_pos() +
190 Vector(dir == LEFT ? -16 : 16,
191 bbox.get_height()*0.66666 - 32);
192 grabbed_object->grab(*this, pos);
199 return on_ground_flag;
205 if(player_status->bonus == NO_BONUS)
212 Player::handle_horizontal_input()
214 float vx = physic.get_velocity_x();
215 float vy = physic.get_velocity_y();
216 float ax = physic.get_acceleration_x();
217 float ay = physic.get_acceleration_y();
220 if(!duck || physic.get_velocity_y() != 0) {
221 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
225 } else if(!controller->hold(Controller::LEFT)
226 && controller->hold(Controller::RIGHT)) {
233 if (!controller->hold(Controller::ACTION)) {
234 ax = dirsign * WALK_ACCELERATION_X;
236 if(vx >= MAX_WALK_XM && dirsign > 0) {
239 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
244 ax = dirsign * RUN_ACCELERATION_X;
246 if(vx >= MAX_RUN_XM && dirsign > 0) {
249 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
255 // we can reach WALK_SPEED without any acceleration
256 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
257 vx = dirsign * WALK_SPEED;
260 // changing directions?
261 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
263 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
264 skidding_timer.start(SKID_TIME);
265 sound_manager->play("sounds/skid.wav");
266 // dust some particles
267 Sector::current()->add_object(
269 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
271 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
272 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
281 // we get slower when not pressing any keys
283 if(fabs(vx) < WALK_SPEED) {
287 ax = WALK_ACCELERATION_X * 1.5;
289 ax = WALK_ACCELERATION_X * -1.5;
294 // if we're on ice slow down acceleration or deceleration
295 if (isice(base.x, base.y + base.height))
297 /* the acceleration/deceleration rate on ice is inversely proportional to
298 * the current velocity.
301 // increasing 1 will increase acceleration/deceleration rate
302 // decreasing 1 will decrease acceleration/deceleration rate
303 // must stay above zero, though
304 if (ax != 0) ax *= 1 / fabs(vx);
308 // extend/shrink tux collision rectangle so that we fall through/walk over 1
310 if(fabsf(vx) > MAX_WALK_XM) {
313 bbox.set_width(31.8);
316 physic.set_velocity(vx, vy);
317 physic.set_acceleration(ax, ay);
321 Player::handle_vertical_input()
325 fall_mode = ON_GROUND;
326 last_ground_y = get_pos().y;
328 if(get_pos().y > last_ground_y)
330 else if(fall_mode == ON_GROUND)
334 if(on_ground()) { /* Make sure jumping is off. */
337 backflipping = false;
338 backflip_direction = 0;
343 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
345 if (physic.get_velocity_x() != 0) // only jump a little bit when running ducked
346 physic.set_velocity_y(300);
347 else { //do a backflip
349 physic.set_velocity_y(580);
350 backflip_timer.start(0.15);
353 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
354 physic.set_velocity_y(580);
356 physic.set_velocity_y(520);
358 //bbox.move(Vector(0, -1));
362 sound_manager->play("sounds/bigjump.wav");
364 sound_manager->play("sounds/jump.wav");
365 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
366 if (!backflipping && jumping && physic.get_velocity_y() > 0) {
368 physic.set_velocity_y(0);
372 /* In case the player has pressed Down while in a certain range of air,
373 enable butt jump action */
374 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
375 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
378 /* When Down is not held anymore, disable butt jump */
379 if(butt_jump && !controller->hold(Controller::DOWN))
384 if (butt_jump && on_ground() && is_big()) {
387 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
389 Sector::current()->add_smoke_cloud(
390 Vector(get_pos().x - 32, get_pos().y + 32));
394 // Break bricks beneath Tux
395 if(Sector::current()->trybreakbrick(
396 Vector(base.x + 1, base.y + base.height), false)
397 || Sector::current()->trybreakbrick(
398 Vector(base.x + base.width - 1, base.y + base.height), false)) {
399 physic.set_velocity_y(2);
403 // Kill nearby badguys
404 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
405 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
406 i != gameobjects.end();
408 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
410 // don't kill when badguys are already dying or in a certain mode
411 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
412 badguy->mode != BadGuy::BOMB_EXPLODE) {
413 if (fabsf(base.x - badguy->base.x) < 96 &&
414 fabsf(base.y - badguy->base.y) < 64)
422 /** jumping is only allowed if we're about to touch ground soon and if the
423 * button has been up in between the last jump
427 if ( (issolid(get_pos().x + bbox.get_width() / 2,
428 get_pos().y + bbox.get_height() + 64) ||
429 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
430 issolid(get_pos().x + bbox.get_width() - 1,
431 get_pos().y + bbox.get_height() + 64))
434 && input.jump && !input.old_jump)
442 Player::handle_input()
444 /* Handle horizontal movement: */
445 if (!backflipping) handle_horizontal_input();
447 if (backflip_direction == 0) {
448 dir == LEFT ? backflip_direction = 1 : backflip_direction = -1;
450 else backflip_direction == 1 ? dir = LEFT : dir = RIGHT; //prevent player from changing direction when backflipping
451 if (backflip_timer.check()) physic.set_velocity_x(100 * backflip_direction);
456 if (on_ground() && !controller->hold(Controller::JUMP))
458 handle_vertical_input();
461 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
462 if(Sector::current()->add_bullet(
463 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
464 : Vector(32, bbox.get_height()/2)),
465 physic.get_velocity_x(), dir))
466 shooting_timer.start(SHOOTING_TIME);
470 if (controller->hold(Controller::DOWN) && is_big() && !duck
471 && physic.get_velocity_y() == 0 && on_ground()) {
473 bbox.move(Vector(0, 32));
474 bbox.set_height(31.8);
475 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
476 // try if we can really unduck
477 bbox.move(Vector(0, -32));
478 bbox.set_height(63.8);
482 // when unducking in air we need some space to do so
483 if(on_ground() || !collision_object_map(bbox)) {
486 // undo the ducking changes
487 bbox.move(Vector(0, 32));
488 bbox.set_height(31.8);
495 Player::set_bonus(BonusType type, bool animate)
497 if(player_status->bonus >= type)
500 if(player_status->bonus == NO_BONUS) {
501 bbox.set_height(63.8);
502 bbox.move(Vector(0, -32));
504 growing_timer.start(GROWING_TIME);
507 player_status->bonus = type;
511 Player::draw(DrawingContext& context)
513 TuxBodyParts* tux_body;
515 if (player_status->bonus == GROWUP_BONUS)
517 else if (player_status->bonus == FIRE_BONUS)
519 else if (player_status->bonus == ICE_BONUS)
522 tux_body = small_tux;
524 int layer = LAYER_OBJECTS + 10;
526 /* Set Tux sprite action */
527 if (duck && is_big())
530 tux_body->set_action("duck-left");
532 tux_body->set_action("duck-right");
534 else if (skidding_timer.started() && !skidding_timer.check())
537 tux_body->set_action("skid-left");
539 tux_body->set_action("skid-right");
541 else if (kick_timer.started() && !kick_timer.check())
544 tux_body->set_action("kick-left");
546 tux_body->set_action("kick-right");
548 else if (butt_jump && is_big())
551 tux_body->set_action("buttjump-left");
553 tux_body->set_action("buttjump-right");
555 else if (physic.get_velocity_y() != 0)
558 tux_body->set_action("jump-left");
560 tux_body->set_action("jump-right");
564 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
567 tux_body->set_action("stand-left");
569 tux_body->set_action("stand-right");
574 tux_body->set_action("walk-left");
576 tux_body->set_action("walk-right");
580 if(idle_timer.check())
585 tux_body->head->set_action("idle-left", 1);
587 tux_body->head->set_action("idle-right", 1);
592 // Tux is holding something
593 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
594 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
599 tux_body->arms->set_action("duck+grab-left");
601 tux_body->arms->set_action("duck+grab-right");
606 tux_body->arms->set_action("grab-left");
608 tux_body->arms->set_action("grab-right");
614 smalltux_gameover->draw(context, get_pos(), layer);
615 } else if(growing_timer.get_timeleft() > 0) {
619 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
620 int((growing_timer.get_timegone() *
621 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
623 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
624 int((growing_timer.get_timegone() *
625 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
630 context.draw_surface(growingtux_right[
631 int((growing_timer.get_timegone() *
632 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
634 context.draw_surface(growingtux_left[
635 int((growing_timer.get_timegone() *
636 GROWING_FRAMES) / GROWING_TIME)],
640 else if (safe_timer.started() && size_t(game_time*40)%2)
643 tux_body->draw(context, get_pos(), layer);
645 // Draw blinking star overlay
646 if (invincible_timer.started() &&
647 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
648 || size_t(game_time*20)%2)
651 if (!is_big() || duck)
652 smalltux_star->draw(context, get_pos(), layer + 5);
654 bigtux_star->draw(context, get_pos(), layer + 5);
659 Player::collision(GameObject& other, const CollisionHit& hit)
661 Portable* portable = dynamic_cast<Portable*> (&other);
662 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
663 && fabsf(hit.normal.x) > .9) {
664 grabbed_object = portable;
668 if(other.get_flags() & FLAG_SOLID) {
669 TileMap* tilemap = dynamic_cast<TileMap*> (&other);
671 const TilemapCollisionHit* thit =
672 static_cast<const TilemapCollisionHit*> (&hit);
673 if(thit->tileflags & Tile::SPIKE)
676 if(! (thit->tileflags & Tile::SOLID))
680 if(hit.normal.y < 0) { // landed on floor?
681 if (physic.get_velocity_y() < 0)
682 physic.set_velocity_y(0);
683 on_ground_flag = true;
684 } else if(hit.normal.y > 0) { // bumped against the roof
685 physic.set_velocity_y(.1);
688 if(fabsf(hit.normal.x) > .9) { // hit on the side?
689 physic.set_velocity_x(0);
695 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
697 if(controller->pressed(Controller::UP))
698 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
705 Player::make_invincible()
707 sound_manager->play("sounds/invincible.wav");
708 invincible_timer.start(TUX_INVINCIBLE_TIME);
709 Sector::current()->play_music(HERRING_MUSIC);
714 Player::kill(HurtMode mode)
716 if(dying || deactivated)
720 safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
723 sound_manager->play("sounds/hurt.wav");
725 physic.set_velocity_x(0);
727 if (mode == SHRINK && is_big())
729 if (player_status->bonus == FIRE_BONUS
730 || player_status->bonus == ICE_BONUS)
732 safe_timer.start(TUX_SAFE_TIME);
733 player_status->bonus = GROWUP_BONUS;
737 growing_timer.start(GROWING_TIME);
738 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
739 bbox.set_height(31.8);
741 player_status->bonus = NO_BONUS;
746 physic.enable_gravity(true);
747 physic.set_acceleration(0, 0);
748 physic.set_velocity(0, 700);
749 player_status->lives -= 1;
750 player_status->bonus = NO_BONUS;
752 dying_timer.start(3.0);
753 flags |= FLAG_NO_COLLDET;
758 Player::move(const Vector& vector)
760 bbox.set_pos(vector);
762 bbox.set_size(31.8, 63.8);
764 bbox.set_size(31.8, 31.8);
765 on_ground_flag = false;
767 last_ground_y = vector.y;
773 Player::check_bounds(Camera* camera)
775 /* Keep tux in bounds: */
777 { // Lock Tux to the size of the level, so that he doesn't fall of
779 bbox.set_pos(Vector(0, get_pos().y));
782 /* Keep in-bounds, vertically: */
783 if (get_pos().y > Sector::current()->solids->get_height() * 32)
790 // can happen if back scrolling is disabled
791 if(get_pos().x < camera->get_translation().x) {
792 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
795 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
798 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
807 if(collision_object_map(bbox)) {
816 Player::bounce(BadGuy& )
818 if(controller->hold(Controller::JUMP))
819 physic.set_velocity_y(520);
821 physic.set_velocity_y(300);
824 //Scripting Functions Below
830 physic.set_velocity_x(0);
831 physic.set_velocity_y(0);
840 void Player::walk(float speed)
842 physic.set_velocity_x(speed);