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 "app/globals.h"
27 #include "app/gettext.h"
28 #include "sprite/sprite_manager.h"
31 #include "sprite/sprite.h"
33 #include "resources.h"
34 #include "video/screen.h"
35 #include "statistics.h"
36 #include "game_session.h"
37 #include "object/tilemap.h"
38 #include "object/camera.h"
39 #include "object/gameobjs.h"
40 #include "object/portable.h"
41 #include "trigger/trigger_base.h"
42 #include "control/joystickkeyboardcontroller.h"
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,
84 Uint32 drawing_effect)
87 head->draw(context, pos, layer-1, drawing_effect);
89 body->draw(context, pos, layer-3, drawing_effect);
91 arms->draw(context, pos, layer, drawing_effect);
93 feet->draw(context, pos, layer-2, drawing_effect);
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 falling_from_flap = false;
134 enable_hover = false;
137 flapping_velocity = 0;
139 // temporary to help player's choosing a flapping
140 flapping_mode = NO_FLAP;
142 // Ricardo's flapping
145 on_ground_flag = false;
152 Player::set_controller(Controller* controller)
154 this->controller = controller;
158 Player::action(float elapsed_time)
160 if(dying && dying_timer.check()) {
165 if(!controller->hold(Controller::ACTION) && grabbed_object) {
167 // move the grabbed object a bit away from tux
168 Vector pos = get_pos() +
169 Vector(dir == LEFT ? -bbox.get_width() : bbox.get_width(),
170 bbox.get_height()*0.66666 - 32);
171 MovingObject* object = dynamic_cast<MovingObject*> (grabbed_object);
173 object->set_pos(pos);
176 std::cout << "Non MovingObjetc grabbed?!?\n";
184 movement = physic.get_movement(elapsed_time);
185 on_ground_flag = false;
188 // special exception for cases where we're stuck under tiles after
189 // being ducked. In this case we drift out
190 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
191 && collision_object_map(base)) {
192 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
193 previous_base = old_base = base;
197 if(grabbed_object != 0) {
198 Vector pos = get_pos() +
199 Vector(dir == LEFT ? -16 : 16,
200 bbox.get_height()*0.66666 - 32);
201 grabbed_object->grab(*this, pos);
208 return on_ground_flag;
214 if(player_status->bonus == NO_BONUS)
221 Player::handle_horizontal_input()
223 float vx = physic.get_velocity_x();
224 float vy = physic.get_velocity_y();
225 float ax = physic.get_acceleration_x();
226 float ay = physic.get_acceleration_y();
229 if(!duck || physic.get_velocity_y() != 0) {
230 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
234 } else if(!controller->hold(Controller::LEFT)
235 && controller->hold(Controller::RIGHT)) {
242 if (!controller->hold(Controller::ACTION)) {
243 ax = dirsign * WALK_ACCELERATION_X;
245 if(vx >= MAX_WALK_XM && dirsign > 0) {
248 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
253 ax = dirsign * RUN_ACCELERATION_X;
255 if(vx >= MAX_RUN_XM && dirsign > 0) {
258 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
264 // we can reach WALK_SPEED without any acceleration
265 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
266 vx = dirsign * WALK_SPEED;
269 // changing directions?
270 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
272 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
273 skidding_timer.start(SKID_TIME);
274 sound_manager->play_sound("skid");
275 // dust some partcles
276 Sector::current()->add_object(
278 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
280 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
281 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
290 // we get slower when not pressing any keys
292 if(fabs(vx) < WALK_SPEED) {
296 ax = WALK_ACCELERATION_X * 1.5;
298 ax = WALK_ACCELERATION_X * -1.5;
303 // if we're on ice slow down acceleration or deceleration
304 if (isice(base.x, base.y + base.height))
306 /* the acceleration/deceleration rate on ice is inversely proportional to
307 * the current velocity.
310 // increasing 1 will increase acceleration/deceleration rate
311 // decreasing 1 will decrease acceleration/deceleration rate
312 // must stay above zero, though
313 if (ax != 0) ax *= 1 / fabs(vx);
317 // extend/shrink tux collision rectangle so that we fall through/walk over 1
319 if(fabsf(vx) > MAX_WALK_XM) {
322 bbox.set_width(31.8);
325 physic.set_velocity(vx, vy);
326 physic.set_acceleration(ax, ay);
330 Player::handle_vertical_input()
334 fall_mode = ON_GROUND;
335 last_ground_y = get_pos().y;
337 if(get_pos().y > last_ground_y)
339 else if(fall_mode == ON_GROUND)
343 if(on_ground()) { /* Make sure jumping is off. */
346 falling_from_flap = false;
347 if (flapping_timer.started()) {
348 flapping_timer.start(0);
351 physic.set_acceleration_y(0); //for flapping
355 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
356 if(duck) { // only jump a little bit when in duck mode {
357 physic.set_velocity_y(300);
359 // jump higher if we are running
360 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
361 physic.set_velocity_y(580);
363 physic.set_velocity_y(520);
366 //bbox.move(Vector(0, -1));
371 flaps_nb = 0; // Ricardo's flapping
373 sound_manager->play_sound("bigjump");
375 sound_manager->play_sound("jump");
376 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
377 if (!flapping && !duck && !falling_from_flap && !on_ground()) {
380 if (jumping && physic.get_velocity_y() > 0) {
382 physic.set_velocity_y(0);
386 // temporary to help player's choosing a flapping
387 if(flapping_mode == RICARDO_FLAP) {
388 // Flapping, Ricardo's version
389 // similar to SM3 Fox
390 if(controller->pressed(Controller::JUMP) && can_flap && flaps_nb < 3) {
391 physic.set_velocity_y(350);
392 physic.set_velocity_x(physic.get_velocity_x() * 35);
395 } else if(flapping_mode == MAREK_FLAP) {
396 // Flapping, Marek's version
397 if (controller->hold(Controller::JUMP) && can_flap)
399 if (!flapping_timer.started())
401 flapping_timer.start(TUX_FLAPPING_TIME);
402 flapping_velocity = physic.get_velocity_x();
404 if (flapping_timer.check())
407 falling_from_flap = true;
411 if (!flapping_timer.check()) {
412 float cv = flapping_velocity * sqrt(
413 TUX_FLAPPING_TIME - flapping_timer.get_timegone()
414 / TUX_FLAPPING_TIME);
416 //Handle change of direction while flapping
417 if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
420 physic.set_velocity_x(cv);
421 physic.set_velocity_y(
422 flapping_timer.get_timegone()/.850);
425 } else if(flapping_mode == RYAN_FLAP) {
426 // Flapping, Ryan's version
427 if (controller->hold(Controller::JUMP) && can_flap)
429 if (!flapping_timer.started())
431 flapping_timer.start(TUX_FLAPPING_TIME);
433 if (flapping_timer.check())
436 falling_from_flap = true;
440 if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
441 && physic.get_velocity_y() < 0)
443 float gravity = Sector::current()->gravity;
445 float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
447 // XXX: magic numbers. should be a percent of gravity
448 // gravity is (by default) -0.1f
449 physic.set_acceleration_y(12 + 1*xr);
452 // To slow down x-vel when flapping (not working)
453 if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
455 if (physic.get_velocity_x() < 0)
456 physic.set_acceleration_x(1.0f);
457 else if (physic.get_velocity_x() > 0)
458 physic.set_acceleration_x(-1.0f);
463 physic.set_acceleration_y(0);
467 /* In case the player has pressed Down while in a certain range of air,
468 enable butt jump action */
469 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
470 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
473 /* When Down is not held anymore, disable butt jump */
474 if(butt_jump && !controller->hold(Controller::DOWN))
479 if (butt_jump && on_ground() && is_big()) {
482 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
484 Sector::current()->add_smoke_cloud(
485 Vector(get_pos().x - 32, get_pos().y + 32));
489 // Break bricks beneath Tux
490 if(Sector::current()->trybreakbrick(
491 Vector(base.x + 1, base.y + base.height), false)
492 || Sector::current()->trybreakbrick(
493 Vector(base.x + base.width - 1, base.y + base.height), false)) {
494 physic.set_velocity_y(2);
498 // Kill nearby badguys
499 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
500 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
501 i != gameobjects.end();
503 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
505 // don't kill when badguys are already dying or in a certain mode
506 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
507 badguy->mode != BadGuy::BOMB_EXPLODE) {
508 if (fabsf(base.x - badguy->base.x) < 96 &&
509 fabsf(base.y - badguy->base.y) < 64)
517 /** jumping is only allowed if we're about to touch ground soon and if the
518 * button has been up in between the last jump
522 if ( (issolid(get_pos().x + bbox.get_width() / 2,
523 get_pos().y + bbox.get_height() + 64) ||
524 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
525 issolid(get_pos().x + bbox.get_width() - 1,
526 get_pos().y + bbox.get_height() + 64))
529 && input.jump && !input.old_jump)
537 Player::handle_input()
539 /* Handle horizontal movement: */
540 handle_horizontal_input();
543 if (on_ground() && !controller->hold(Controller::JUMP))
545 handle_vertical_input();
548 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
549 if(Sector::current()->add_bullet(
550 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
551 : Vector(32, bbox.get_height()/2)),
552 physic.get_velocity_x(), dir))
553 shooting_timer.start(SHOOTING_TIME);
557 if (controller->hold(Controller::DOWN) && is_big() && !duck
558 && physic.get_velocity_y() == 0 && on_ground()) {
560 bbox.move(Vector(0, 32));
561 bbox.set_height(31.8);
562 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
563 // try if we can really unduck
564 bbox.move(Vector(0, -32));
565 bbox.set_height(63.8);
569 // when unducking in air we need some space to do so
570 if(on_ground() || !collision_object_map(bbox)) {
573 // undo the ducking changes
574 bbox.move(Vector(0, 32));
575 bbox.set_height(31.8);
582 Player::set_bonus(BonusType type, bool animate)
584 if(player_status->bonus == type)
587 if(player_status->bonus == NO_BONUS) {
588 bbox.set_height(63.8);
589 bbox.move(Vector(0, -32));
591 growing_timer.start(GROWING_TIME);
594 player_status->bonus = type;
598 Player::draw(DrawingContext& context)
600 TuxBodyParts* tux_body;
602 if (player_status->bonus == GROWUP_BONUS)
604 else if (player_status->bonus == FIRE_BONUS)
606 else if (player_status->bonus == ICE_BONUS)
609 tux_body = small_tux;
611 int layer = LAYER_OBJECTS + 10;
613 /* Set Tux sprite action */
614 if (duck && is_big())
617 tux_body->set_action("duck-left");
619 tux_body->set_action("duck-right");
621 else if (skidding_timer.started() && !skidding_timer.check())
624 tux_body->set_action("skid-left");
626 tux_body->set_action("skid-right");
628 else if (kick_timer.started() && !kick_timer.check())
631 tux_body->set_action("kick-left");
633 tux_body->set_action("kick-right");
635 else if (butt_jump && is_big())
638 tux_body->set_action("buttjump-left");
640 tux_body->set_action("buttjump-right");
642 else if (physic.get_velocity_y() != 0)
645 tux_body->set_action("jump-left");
647 tux_body->set_action("jump-right");
651 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
654 tux_body->set_action("stand-left");
656 tux_body->set_action("stand-right");
661 tux_body->set_action("walk-left");
663 tux_body->set_action("walk-right");
667 if(idle_timer.check())
672 tux_body->head->set_action("idle-left", 1);
674 tux_body->head->set_action("idle-right", 1);
679 // Tux is holding something
680 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
681 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
686 tux_body->arms->set_action("duck+grab-left");
688 tux_body->arms->set_action("duck+grab-right");
693 tux_body->arms->set_action("grab-left");
695 tux_body->arms->set_action("grab-right");
701 smalltux_gameover->draw(context, get_pos(), layer);
702 } else if(growing_timer.get_timeleft() > 0) {
706 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
707 int((growing_timer.get_timegone() *
708 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
710 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
711 int((growing_timer.get_timegone() *
712 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
717 context.draw_surface(growingtux_right[
718 int((growing_timer.get_timegone() *
719 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
721 context.draw_surface(growingtux_left[
722 int((growing_timer.get_timegone() *
723 GROWING_FRAMES) / GROWING_TIME)],
727 else if (safe_timer.started() && size_t(global_time*40)%2)
730 tux_body->draw(context, get_pos(), layer);
732 // Draw blinking star overlay
733 if (invincible_timer.started() &&
734 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
735 || size_t(global_time*20)%2)
738 if (!is_big() || duck)
739 smalltux_star->draw(context, get_pos(), layer + 5);
741 bigtux_star->draw(context, get_pos(), layer + 5);
746 Player::collision(GameObject& other, const CollisionHit& hit)
748 Portable* portable = dynamic_cast<Portable*> (&other);
749 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
750 && fabsf(hit.normal.x) > .9) {
751 grabbed_object = portable;
755 if(other.get_flags() & FLAG_SOLID) {
756 if(hit.normal.y < 0) { // landed on floor?
757 if (physic.get_velocity_y() < 0)
758 physic.set_velocity_y(0);
759 on_ground_flag = true;
760 } else if(hit.normal.y > 0) { // bumped against the roof
761 physic.set_velocity_y(.1);
764 if(fabsf(hit.normal.x) > .9) { // hit on the side?
765 physic.set_velocity_x(0);
771 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
773 if(controller->pressed(Controller::UP))
774 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
781 Player::make_invincible()
783 sound_manager->play_sound("invincible");
784 invincible_timer.start(TUX_INVINCIBLE_TIME);
785 Sector::current()->play_music(HERRING_MUSIC);
790 Player::kill(HurtMode mode)
796 safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
799 sound_manager->play_sound("hurt");
801 physic.set_velocity_x(0);
803 if (mode == SHRINK && is_big())
805 if (player_status->bonus == FIRE_BONUS
806 || player_status->bonus == ICE_BONUS)
808 safe_timer.start(TUX_SAFE_TIME);
809 player_status->bonus = GROWUP_BONUS;
813 growing_timer.start(GROWING_TIME);
814 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
815 bbox.set_height(31.8);
817 player_status->bonus = NO_BONUS;
822 physic.enable_gravity(true);
823 physic.set_acceleration(0, 0);
824 physic.set_velocity(0, 700);
825 player_status->lives -= 1;
826 player_status->bonus = NO_BONUS;
828 dying_timer.start(3.0);
829 flags |= FLAG_NO_COLLDET;
834 Player::move(const Vector& vector)
836 bbox.set_pos(vector);
838 bbox.set_size(31.8, 63.8);
840 bbox.set_size(31.8, 31.8);
841 on_ground_flag = false;
843 last_ground_y = vector.y;
849 Player::check_bounds(Camera* camera)
851 /* Keep tux in bounds: */
853 { // Lock Tux to the size of the level, so that he doesn't fall of
855 bbox.set_pos(Vector(0, get_pos().y));
858 /* Keep in-bounds, vertically: */
859 if (get_pos().y > Sector::current()->solids->get_height() * 32)
866 // can happen if back scrolling is disabled
867 if(get_pos().x < camera->get_translation().x) {
868 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
871 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
874 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
883 if(collision_object_map(bbox)) {
892 Player::bounce(BadGuy& )
894 //Make sure we stopped flapping
896 falling_from_flap = false;
898 if(controller->hold(Controller::JUMP))
899 physic.set_velocity_y(520);
901 physic.set_velocity_y(200);