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"
45 static const int TILES_FOR_BUTTJUMP = 3;
46 static const float SHOOTING_TIME = .150;
47 /// time before idle animation starts
48 static const float IDLE_TIME = 2.5;
50 static const float WALK_ACCELERATION_X = 300;
51 static const float RUN_ACCELERATION_X = 400;
52 static const float SKID_XM = 200;
53 static const float SKID_TIME = .3;
54 static const float MAX_WALK_XM = 230;
55 static const float MAX_RUN_XM = 320;
56 static const float WALK_SPEED = 100;
59 Surface* growingtux_left[GROWING_FRAMES];
60 Surface* growingtux_right[GROWING_FRAMES];
62 Surface* tux_life = 0;
64 TuxBodyParts* small_tux = 0;
65 TuxBodyParts* big_tux = 0;
66 TuxBodyParts* fire_tux = 0;
67 TuxBodyParts* ice_tux = 0;
70 TuxBodyParts::set_action(std::string action, int loops)
73 head->set_action(action, loops);
75 body->set_action(action, loops);
77 arms->set_action(action, loops);
79 feet->set_action(action, loops);
83 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
86 head->draw(context, pos, layer-1);
88 body->draw(context, pos, layer-3);
90 arms->draw(context, pos, layer);
92 feet->draw(context, pos, layer-2);
95 Player::Player(PlayerStatus* _player_status)
96 : player_status(_player_status), grabbed_object(0)
98 controller = main_controller;
99 smalltux_gameover = sprite_manager->create("smalltux-gameover");
100 smalltux_star = sprite_manager->create("smalltux-star");
101 bigtux_star = sprite_manager->create("bigtux-star");
107 delete smalltux_gameover;
108 delete smalltux_star;
116 bbox.set_size(31.8, 63.8);
118 bbox.set_size(31.8, 31.8);
127 fall_mode = ON_GROUND;
132 falling_from_flap = false;
133 enable_hover = false;
136 flapping_velocity = 0;
138 // temporary to help player's choosing a flapping
139 flapping_mode = NO_FLAP;
141 // Ricardo's flapping
144 on_ground_flag = false;
151 Player::set_controller(Controller* controller)
153 this->controller = controller;
157 Player::update(float elapsed_time)
159 if(dying && dying_timer.check()) {
164 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() : bbox.get_width(),
169 bbox.get_height()*0.66666 - 32);
170 MovingObject* object = dynamic_cast<MovingObject*> (grabbed_object);
172 object->set_pos(pos);
175 std::cout << "Non MovingObjetc grabbed?!?\n";
183 movement = physic.get_movement(elapsed_time);
184 on_ground_flag = false;
187 // special exception for cases where we're stuck under tiles after
188 // being ducked. In this case we drift out
189 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
190 && collision_object_map(base)) {
191 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
192 previous_base = old_base = base;
196 if(grabbed_object != 0) {
197 Vector pos = get_pos() +
198 Vector(dir == LEFT ? -16 : 16,
199 bbox.get_height()*0.66666 - 32);
200 grabbed_object->grab(*this, pos);
207 return on_ground_flag;
213 if(player_status->bonus == NO_BONUS)
220 Player::handle_horizontal_input()
222 float vx = physic.get_velocity_x();
223 float vy = physic.get_velocity_y();
224 float ax = physic.get_acceleration_x();
225 float ay = physic.get_acceleration_y();
228 if(!duck || physic.get_velocity_y() != 0) {
229 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
233 } else if(!controller->hold(Controller::LEFT)
234 && controller->hold(Controller::RIGHT)) {
241 if (!controller->hold(Controller::ACTION)) {
242 ax = dirsign * WALK_ACCELERATION_X;
244 if(vx >= MAX_WALK_XM && dirsign > 0) {
247 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
252 ax = dirsign * RUN_ACCELERATION_X;
254 if(vx >= MAX_RUN_XM && dirsign > 0) {
257 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
263 // we can reach WALK_SPEED without any acceleration
264 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
265 vx = dirsign * WALK_SPEED;
268 // changing directions?
269 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
271 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
272 skidding_timer.start(SKID_TIME);
273 sound_manager->play("sounds/skid.ogg");
274 // dust some partcles
275 Sector::current()->add_object(
277 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
279 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
280 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
289 // we get slower when not pressing any keys
291 if(fabs(vx) < WALK_SPEED) {
295 ax = WALK_ACCELERATION_X * 1.5;
297 ax = WALK_ACCELERATION_X * -1.5;
302 // if we're on ice slow down acceleration or deceleration
303 if (isice(base.x, base.y + base.height))
305 /* the acceleration/deceleration rate on ice is inversely proportional to
306 * the current velocity.
309 // increasing 1 will increase acceleration/deceleration rate
310 // decreasing 1 will decrease acceleration/deceleration rate
311 // must stay above zero, though
312 if (ax != 0) ax *= 1 / fabs(vx);
316 // extend/shrink tux collision rectangle so that we fall through/walk over 1
318 if(fabsf(vx) > MAX_WALK_XM) {
321 bbox.set_width(31.8);
324 physic.set_velocity(vx, vy);
325 physic.set_acceleration(ax, ay);
329 Player::handle_vertical_input()
333 fall_mode = ON_GROUND;
334 last_ground_y = get_pos().y;
336 if(get_pos().y > last_ground_y)
338 else if(fall_mode == ON_GROUND)
342 if(on_ground()) { /* Make sure jumping is off. */
345 falling_from_flap = false;
346 if (flapping_timer.started()) {
347 flapping_timer.stop();
350 physic.set_acceleration_y(0); //for flapping
354 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
355 if (duck) // only jump a little bit when in duck mode
356 physic.set_velocity_y(300);
357 else if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) // jump higher if we are running
358 physic.set_velocity_y(580);
360 physic.set_velocity_y(520);
362 //bbox.move(Vector(0, -1));
367 flaps_nb = 0; // Ricardo's flapping
369 sound_manager->play("sounds/bigjump.ogg");
371 sound_manager->play("sounds/jump.ogg");
372 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
373 if (!flapping && !duck && !falling_from_flap && !on_ground()) {
376 if (jumping && physic.get_velocity_y() > 0) {
378 physic.set_velocity_y(0);
382 // temporary to help players choosing a flapping
383 if(flapping_mode == RICARDO_FLAP) {
384 // Flapping, Ricardo's version
385 // similar to SM3 Fox
386 if(controller->pressed(Controller::JUMP) && can_flap && flaps_nb < 3) {
387 physic.set_velocity_y(350);
388 physic.set_velocity_x(physic.get_velocity_x() * 35);
391 } else if(flapping_mode == MAREK_FLAP) {
393 // Flapping, Marek and Ondra's version
394 if (controller->hold(Controller::JUMP) && can_flap)
396 if (flapping_timer.check())
400 falling_from_flap = true;
402 else if (!flapping_timer.started())
404 flapping_timer.start(TUX_FLAPPING_TIME);
405 flapping_velocity = physic.get_velocity_x();
411 float cv = flapping_velocity * sqrt(
412 TUX_FLAPPING_TIME - flapping_timer.get_timegone()
413 / TUX_FLAPPING_TIME);
415 //Handle change of direction while flapping
416 if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
420 if (controller->hold(Controller::LEFT)) {
421 cv = -TUX_FLAPPING_LEAST_X;
423 else if (controller->hold(Controller::RIGHT)) {
424 cv = TUX_FLAPPING_LEAST_X;
427 physic.set_velocity_x(cv);
428 physic.set_velocity_y(flapping_timer.get_timegone()
429 * TUX_FLAPPING_STRENGTH);
430 //std::cout << "Timegone: " << flapping_timer.get_timegone() << ", Y velocity: " << physic.get_velocity_y() << "\n";
434 } else if(flapping_mode == RYAN_FLAP) {
435 // Flapping, Ryan's version
436 if (controller->hold(Controller::JUMP) && can_flap)
438 if (!flapping_timer.started())
440 flapping_timer.start(TUX_FLAPPING_TIME);
442 if (flapping_timer.check())
445 falling_from_flap = true;
449 if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
450 && physic.get_velocity_y() < 0)
452 float gravity = Sector::current()->gravity;
454 float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
456 // XXX: magic numbers. should be a percent of gravity
457 // gravity is (by default) -0.1f
458 physic.set_acceleration_y(12 + 1*xr);
461 // To slow down x-vel when flapping (not working)
462 if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
464 if (physic.get_velocity_x() < 0)
465 physic.set_acceleration_x(1.0f);
466 else if (physic.get_velocity_x() > 0)
467 physic.set_acceleration_x(-1.0f);
472 physic.set_acceleration_y(0);
477 /* In case the player has pressed Down while in a certain range of air,
478 enable butt jump action */
479 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
480 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
483 /* When Down is not held anymore, disable butt jump */
484 if(butt_jump && !controller->hold(Controller::DOWN))
489 if (butt_jump && on_ground() && is_big()) {
492 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
494 Sector::current()->add_smoke_cloud(
495 Vector(get_pos().x - 32, get_pos().y + 32));
499 // Break bricks beneath Tux
500 if(Sector::current()->trybreakbrick(
501 Vector(base.x + 1, base.y + base.height), false)
502 || Sector::current()->trybreakbrick(
503 Vector(base.x + base.width - 1, base.y + base.height), false)) {
504 physic.set_velocity_y(2);
508 // Kill nearby badguys
509 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
510 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
511 i != gameobjects.end();
513 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
515 // don't kill when badguys are already dying or in a certain mode
516 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
517 badguy->mode != BadGuy::BOMB_EXPLODE) {
518 if (fabsf(base.x - badguy->base.x) < 96 &&
519 fabsf(base.y - badguy->base.y) < 64)
527 /** jumping is only allowed if we're about to touch ground soon and if the
528 * button has been up in between the last jump
532 if ( (issolid(get_pos().x + bbox.get_width() / 2,
533 get_pos().y + bbox.get_height() + 64) ||
534 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
535 issolid(get_pos().x + bbox.get_width() - 1,
536 get_pos().y + bbox.get_height() + 64))
539 && input.jump && !input.old_jump)
547 Player::handle_input()
549 /* Handle horizontal movement: */
550 handle_horizontal_input();
553 if (on_ground() && !controller->hold(Controller::JUMP))
555 handle_vertical_input();
558 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
559 if(Sector::current()->add_bullet(
560 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
561 : Vector(32, bbox.get_height()/2)),
562 physic.get_velocity_x(), dir))
563 shooting_timer.start(SHOOTING_TIME);
567 if (controller->hold(Controller::DOWN) && is_big() && !duck
568 && physic.get_velocity_y() == 0 && on_ground()) {
570 bbox.move(Vector(0, 32));
571 bbox.set_height(31.8);
572 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
573 // try if we can really unduck
574 bbox.move(Vector(0, -32));
575 bbox.set_height(63.8);
579 // when unducking in air we need some space to do so
580 if(on_ground() || !collision_object_map(bbox)) {
583 // undo the ducking changes
584 bbox.move(Vector(0, 32));
585 bbox.set_height(31.8);
592 Player::set_bonus(BonusType type, bool animate)
594 if(player_status->bonus >= type)
597 if(player_status->bonus == NO_BONUS) {
598 bbox.set_height(63.8);
599 bbox.move(Vector(0, -32));
601 growing_timer.start(GROWING_TIME);
604 player_status->bonus = type;
608 Player::draw(DrawingContext& context)
610 TuxBodyParts* tux_body;
612 if (player_status->bonus == GROWUP_BONUS)
614 else if (player_status->bonus == FIRE_BONUS)
616 else if (player_status->bonus == ICE_BONUS)
619 tux_body = small_tux;
621 int layer = LAYER_OBJECTS + 10;
623 /* Set Tux sprite action */
624 if (duck && is_big())
627 tux_body->set_action("duck-left");
629 tux_body->set_action("duck-right");
631 else if (skidding_timer.started() && !skidding_timer.check())
634 tux_body->set_action("skid-left");
636 tux_body->set_action("skid-right");
638 else if (kick_timer.started() && !kick_timer.check())
641 tux_body->set_action("kick-left");
643 tux_body->set_action("kick-right");
645 else if (butt_jump && is_big())
648 tux_body->set_action("buttjump-left");
650 tux_body->set_action("buttjump-right");
652 else if (physic.get_velocity_y() != 0)
655 tux_body->set_action("jump-left");
657 tux_body->set_action("jump-right");
661 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
664 tux_body->set_action("stand-left");
666 tux_body->set_action("stand-right");
671 tux_body->set_action("walk-left");
673 tux_body->set_action("walk-right");
677 if(idle_timer.check())
682 tux_body->head->set_action("idle-left", 1);
684 tux_body->head->set_action("idle-right", 1);
689 // Tux is holding something
690 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
691 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
696 tux_body->arms->set_action("duck+grab-left");
698 tux_body->arms->set_action("duck+grab-right");
703 tux_body->arms->set_action("grab-left");
705 tux_body->arms->set_action("grab-right");
711 smalltux_gameover->draw(context, get_pos(), layer);
712 } else if(growing_timer.get_timeleft() > 0) {
716 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
717 int((growing_timer.get_timegone() *
718 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
720 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
721 int((growing_timer.get_timegone() *
722 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
727 context.draw_surface(growingtux_right[
728 int((growing_timer.get_timegone() *
729 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
731 context.draw_surface(growingtux_left[
732 int((growing_timer.get_timegone() *
733 GROWING_FRAMES) / GROWING_TIME)],
737 else if (safe_timer.started() && size_t(game_time*40)%2)
740 tux_body->draw(context, get_pos(), layer);
742 // Draw blinking star overlay
743 if (invincible_timer.started() &&
744 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
745 || size_t(game_time*20)%2)
748 if (!is_big() || duck)
749 smalltux_star->draw(context, get_pos(), layer + 5);
751 bigtux_star->draw(context, get_pos(), layer + 5);
756 Player::collision(GameObject& other, const CollisionHit& hit)
758 Portable* portable = dynamic_cast<Portable*> (&other);
759 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
760 && fabsf(hit.normal.x) > .9) {
761 grabbed_object = portable;
765 if(other.get_flags() & FLAG_SOLID) {
766 if(hit.normal.y < 0) { // landed on floor?
767 if (physic.get_velocity_y() < 0)
768 physic.set_velocity_y(0);
769 on_ground_flag = true;
770 } else if(hit.normal.y > 0) { // bumped against the roof
771 physic.set_velocity_y(.1);
774 if(fabsf(hit.normal.x) > .9) { // hit on the side?
775 physic.set_velocity_x(0);
781 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
783 if(controller->pressed(Controller::UP))
784 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
791 Player::make_invincible()
793 sound_manager->play("sounds/invincible.ogg");
794 invincible_timer.start(TUX_INVINCIBLE_TIME);
795 Sector::current()->play_music(HERRING_MUSIC);
800 Player::kill(HurtMode mode)
806 safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
809 sound_manager->play("sounds/hurt.ogg");
811 physic.set_velocity_x(0);
813 if (mode == SHRINK && is_big())
815 if (player_status->bonus == FIRE_BONUS
816 || player_status->bonus == ICE_BONUS)
818 safe_timer.start(TUX_SAFE_TIME);
819 player_status->bonus = GROWUP_BONUS;
823 growing_timer.start(GROWING_TIME);
824 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
825 bbox.set_height(31.8);
827 player_status->bonus = NO_BONUS;
832 physic.enable_gravity(true);
833 physic.set_acceleration(0, 0);
834 physic.set_velocity(0, 700);
835 player_status->lives -= 1;
836 player_status->bonus = NO_BONUS;
838 dying_timer.start(3.0);
839 flags |= FLAG_NO_COLLDET;
844 Player::move(const Vector& vector)
846 bbox.set_pos(vector);
848 bbox.set_size(31.8, 63.8);
850 bbox.set_size(31.8, 31.8);
851 on_ground_flag = false;
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 //Make sure we stopped flapping
906 falling_from_flap = false;
908 if(controller->hold(Controller::JUMP))
909 physic.set_velocity_y(520);
911 physic.set_velocity_y(300);