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.
27 #include "sprite/sprite_manager.h"
30 #include "sprite/sprite.h"
32 #include "resources.h"
33 #include "video/screen.h"
34 #include "statistics.h"
35 #include "game_session.h"
36 #include "object/tilemap.h"
37 #include "object/camera.h"
38 #include "object/gameobjs.h"
39 #include "object/portable.h"
40 #include "trigger/trigger_base.h"
41 #include "control/joystickkeyboardcontroller.h"
44 static const int TILES_FOR_BUTTJUMP = 3;
45 static const float SHOOTING_TIME = .150;
46 /// time before idle animation starts
47 static const float IDLE_TIME = 2.5;
49 static const float WALK_ACCELERATION_X = 300;
50 static const float RUN_ACCELERATION_X = 400;
51 static const float SKID_XM = 200;
52 static const float SKID_TIME = .3;
53 static const float MAX_WALK_XM = 230;
54 static const float MAX_RUN_XM = 320;
55 static const float WALK_SPEED = 100;
58 Surface* growingtux_left[GROWING_FRAMES];
59 Surface* growingtux_right[GROWING_FRAMES];
61 Surface* tux_life = 0;
63 TuxBodyParts* small_tux = 0;
64 TuxBodyParts* big_tux = 0;
65 TuxBodyParts* fire_tux = 0;
66 TuxBodyParts* ice_tux = 0;
69 TuxBodyParts::set_action(std::string action, int loops)
72 head->set_action(action, loops);
74 body->set_action(action, loops);
76 arms->set_action(action, loops);
78 feet->set_action(action, loops);
82 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
85 head->draw(context, pos, layer-1);
87 body->draw(context, pos, layer-3);
89 arms->draw(context, pos, layer);
91 feet->draw(context, pos, layer-2);
94 Player::Player(PlayerStatus* _player_status)
95 : player_status(_player_status), grabbed_object(0)
97 controller = main_controller;
98 smalltux_gameover = sprite_manager->create("smalltux-gameover");
99 smalltux_star = sprite_manager->create("smalltux-star");
100 bigtux_star = sprite_manager->create("bigtux-star");
106 delete smalltux_gameover;
107 delete smalltux_star;
115 bbox.set_size(31.8, 63.8);
117 bbox.set_size(31.8, 31.8);
126 fall_mode = ON_GROUND;
131 falling_from_flap = false;
132 enable_hover = false;
135 flapping_velocity = 0;
137 // temporary to help player's choosing a flapping
138 flapping_mode = NO_FLAP;
140 // Ricardo's flapping
143 on_ground_flag = false;
150 Player::set_controller(Controller* controller)
152 this->controller = controller;
156 Player::update(float elapsed_time)
158 if(dying && dying_timer.check()) {
163 if(!controller->hold(Controller::ACTION) && grabbed_object) {
165 // move the grabbed object a bit away from tux
166 Vector pos = get_pos() +
167 Vector(dir == LEFT ? -bbox.get_width() : bbox.get_width(),
168 bbox.get_height()*0.66666 - 32);
169 MovingObject* object = dynamic_cast<MovingObject*> (grabbed_object);
171 object->set_pos(pos);
174 std::cout << "Non MovingObjetc grabbed?!?\n";
182 movement = physic.get_movement(elapsed_time);
183 on_ground_flag = false;
186 // special exception for cases where we're stuck under tiles after
187 // being ducked. In this case we drift out
188 if(!duck && on_ground() && old_base.x == base.x && old_base.y == base.y
189 && collision_object_map(base)) {
190 base.x += elapsed_time * WALK_SPEED * (dir ? 1: -1);
191 previous_base = old_base = base;
195 if(grabbed_object != 0) {
196 Vector pos = get_pos() +
197 Vector(dir == LEFT ? -16 : 16,
198 bbox.get_height()*0.66666 - 32);
199 grabbed_object->grab(*this, pos);
206 return on_ground_flag;
212 if(player_status->bonus == NO_BONUS)
219 Player::handle_horizontal_input()
221 float vx = physic.get_velocity_x();
222 float vy = physic.get_velocity_y();
223 float ax = physic.get_acceleration_x();
224 float ay = physic.get_acceleration_y();
227 if(!duck || physic.get_velocity_y() != 0) {
228 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
232 } else if(!controller->hold(Controller::LEFT)
233 && controller->hold(Controller::RIGHT)) {
240 if (!controller->hold(Controller::ACTION)) {
241 ax = dirsign * WALK_ACCELERATION_X;
243 if(vx >= MAX_WALK_XM && dirsign > 0) {
246 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
251 ax = dirsign * RUN_ACCELERATION_X;
253 if(vx >= MAX_RUN_XM && dirsign > 0) {
256 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
262 // we can reach WALK_SPEED without any acceleration
263 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
264 vx = dirsign * WALK_SPEED;
267 // changing directions?
268 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
270 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
271 skidding_timer.start(SKID_TIME);
272 sound_manager->play_sound("skid");
273 // dust some partcles
274 Sector::current()->add_object(
276 Vector(bbox.p1.x + (dir == RIGHT ? bbox.get_width() : 0),
278 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
279 Vector(280,-260), Vector(0,0.030), 3, Color(100,100,100), 3, .8,
288 // we get slower when not pressing any keys
290 if(fabs(vx) < WALK_SPEED) {
294 ax = WALK_ACCELERATION_X * 1.5;
296 ax = WALK_ACCELERATION_X * -1.5;
301 // if we're on ice slow down acceleration or deceleration
302 if (isice(base.x, base.y + base.height))
304 /* the acceleration/deceleration rate on ice is inversely proportional to
305 * the current velocity.
308 // increasing 1 will increase acceleration/deceleration rate
309 // decreasing 1 will decrease acceleration/deceleration rate
310 // must stay above zero, though
311 if (ax != 0) ax *= 1 / fabs(vx);
315 // extend/shrink tux collision rectangle so that we fall through/walk over 1
317 if(fabsf(vx) > MAX_WALK_XM) {
320 bbox.set_width(31.8);
323 physic.set_velocity(vx, vy);
324 physic.set_acceleration(ax, ay);
328 Player::handle_vertical_input()
332 fall_mode = ON_GROUND;
333 last_ground_y = get_pos().y;
335 if(get_pos().y > last_ground_y)
337 else if(fall_mode == ON_GROUND)
341 if(on_ground()) { /* Make sure jumping is off. */
344 falling_from_flap = false;
345 if (flapping_timer.started()) {
346 flapping_timer.stop();
349 physic.set_acceleration_y(0); //for flapping
353 if(controller->pressed(Controller::JUMP) && can_jump && on_ground()) {
354 if (duck) // only jump a little bit when in duck mode
355 physic.set_velocity_y(300);
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));
366 flaps_nb = 0; // Ricardo's flapping
368 sound_manager->play_sound("bigjump");
370 sound_manager->play_sound("jump");
371 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
372 if (!flapping && !duck && !falling_from_flap && !on_ground()) {
375 if (jumping && physic.get_velocity_y() > 0) {
377 physic.set_velocity_y(0);
381 // temporary to help players choosing a flapping
382 if(flapping_mode == RICARDO_FLAP) {
383 // Flapping, Ricardo's version
384 // similar to SM3 Fox
385 if(controller->pressed(Controller::JUMP) && can_flap && flaps_nb < 3) {
386 physic.set_velocity_y(350);
387 physic.set_velocity_x(physic.get_velocity_x() * 35);
390 } else if(flapping_mode == MAREK_FLAP) {
392 // Flapping, Marek and Ondra's version
393 if (controller->hold(Controller::JUMP) && can_flap)
395 if (flapping_timer.check())
399 falling_from_flap = true;
401 else if (!flapping_timer.started())
403 flapping_timer.start(TUX_FLAPPING_TIME);
404 flapping_velocity = physic.get_velocity_x();
410 float cv = flapping_velocity * sqrt(
411 TUX_FLAPPING_TIME - flapping_timer.get_timegone()
412 / TUX_FLAPPING_TIME);
414 //Handle change of direction while flapping
415 if (((dir == LEFT) && (cv > 0)) || (dir == RIGHT) && (cv < 0)) {
418 physic.set_velocity_x(cv);
419 physic.set_velocity_y(flapping_timer.get_timegone()
420 * TUX_FLAPPING_STRENGTH);
421 //std::cout << "Timegone: " << flapping_timer.get_timegone() << ", Y velocity: " << physic.get_velocity_y() << "\n";
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);
468 /* In case the player has pressed Down while in a certain range of air,
469 enable butt jump action */
470 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
471 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
474 /* When Down is not held anymore, disable butt jump */
475 if(butt_jump && !controller->hold(Controller::DOWN))
480 if (butt_jump && on_ground() && is_big()) {
483 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
485 Sector::current()->add_smoke_cloud(
486 Vector(get_pos().x - 32, get_pos().y + 32));
490 // Break bricks beneath Tux
491 if(Sector::current()->trybreakbrick(
492 Vector(base.x + 1, base.y + base.height), false)
493 || Sector::current()->trybreakbrick(
494 Vector(base.x + base.width - 1, base.y + base.height), false)) {
495 physic.set_velocity_y(2);
499 // Kill nearby badguys
500 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
501 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
502 i != gameobjects.end();
504 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
506 // don't kill when badguys are already dying or in a certain mode
507 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
508 badguy->mode != BadGuy::BOMB_EXPLODE) {
509 if (fabsf(base.x - badguy->base.x) < 96 &&
510 fabsf(base.y - badguy->base.y) < 64)
518 /** jumping is only allowed if we're about to touch ground soon and if the
519 * button has been up in between the last jump
523 if ( (issolid(get_pos().x + bbox.get_width() / 2,
524 get_pos().y + bbox.get_height() + 64) ||
525 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
526 issolid(get_pos().x + bbox.get_width() - 1,
527 get_pos().y + bbox.get_height() + 64))
530 && input.jump && !input.old_jump)
538 Player::handle_input()
540 /* Handle horizontal movement: */
541 handle_horizontal_input();
544 if (on_ground() && !controller->hold(Controller::JUMP))
546 handle_vertical_input();
549 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
550 if(Sector::current()->add_bullet(
551 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
552 : Vector(32, bbox.get_height()/2)),
553 physic.get_velocity_x(), dir))
554 shooting_timer.start(SHOOTING_TIME);
558 if (controller->hold(Controller::DOWN) && is_big() && !duck
559 && physic.get_velocity_y() == 0 && on_ground()) {
561 bbox.move(Vector(0, 32));
562 bbox.set_height(31.8);
563 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
564 // try if we can really unduck
565 bbox.move(Vector(0, -32));
566 bbox.set_height(63.8);
570 // when unducking in air we need some space to do so
571 if(on_ground() || !collision_object_map(bbox)) {
574 // undo the ducking changes
575 bbox.move(Vector(0, 32));
576 bbox.set_height(31.8);
583 Player::set_bonus(BonusType type, bool animate)
585 if(player_status->bonus == type)
588 if(player_status->bonus == NO_BONUS) {
589 bbox.set_height(63.8);
590 bbox.move(Vector(0, -32));
592 growing_timer.start(GROWING_TIME);
595 player_status->bonus = type;
599 Player::draw(DrawingContext& context)
601 TuxBodyParts* tux_body;
603 if (player_status->bonus == GROWUP_BONUS)
605 else if (player_status->bonus == FIRE_BONUS)
607 else if (player_status->bonus == ICE_BONUS)
610 tux_body = small_tux;
612 int layer = LAYER_OBJECTS + 10;
614 /* Set Tux sprite action */
615 if (duck && is_big())
618 tux_body->set_action("duck-left");
620 tux_body->set_action("duck-right");
622 else if (skidding_timer.started() && !skidding_timer.check())
625 tux_body->set_action("skid-left");
627 tux_body->set_action("skid-right");
629 else if (kick_timer.started() && !kick_timer.check())
632 tux_body->set_action("kick-left");
634 tux_body->set_action("kick-right");
636 else if (butt_jump && is_big())
639 tux_body->set_action("buttjump-left");
641 tux_body->set_action("buttjump-right");
643 else if (physic.get_velocity_y() != 0)
646 tux_body->set_action("jump-left");
648 tux_body->set_action("jump-right");
652 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
655 tux_body->set_action("stand-left");
657 tux_body->set_action("stand-right");
662 tux_body->set_action("walk-left");
664 tux_body->set_action("walk-right");
668 if(idle_timer.check())
673 tux_body->head->set_action("idle-left", 1);
675 tux_body->head->set_action("idle-right", 1);
680 // Tux is holding something
681 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
682 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
687 tux_body->arms->set_action("duck+grab-left");
689 tux_body->arms->set_action("duck+grab-right");
694 tux_body->arms->set_action("grab-left");
696 tux_body->arms->set_action("grab-right");
702 smalltux_gameover->draw(context, get_pos(), layer);
703 } else if(growing_timer.get_timeleft() > 0) {
707 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
708 int((growing_timer.get_timegone() *
709 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
711 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
712 int((growing_timer.get_timegone() *
713 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
718 context.draw_surface(growingtux_right[
719 int((growing_timer.get_timegone() *
720 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
722 context.draw_surface(growingtux_left[
723 int((growing_timer.get_timegone() *
724 GROWING_FRAMES) / GROWING_TIME)],
728 else if (safe_timer.started() && size_t(global_time*40)%2)
731 tux_body->draw(context, get_pos(), layer);
733 // Draw blinking star overlay
734 if (invincible_timer.started() &&
735 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
736 || size_t(global_time*20)%2)
739 if (!is_big() || duck)
740 smalltux_star->draw(context, get_pos(), layer + 5);
742 bigtux_star->draw(context, get_pos(), layer + 5);
747 Player::collision(GameObject& other, const CollisionHit& hit)
749 Portable* portable = dynamic_cast<Portable*> (&other);
750 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
751 && fabsf(hit.normal.x) > .9) {
752 grabbed_object = portable;
756 if(other.get_flags() & FLAG_SOLID) {
757 if(hit.normal.y < 0) { // landed on floor?
758 if (physic.get_velocity_y() < 0)
759 physic.set_velocity_y(0);
760 on_ground_flag = true;
761 } else if(hit.normal.y > 0) { // bumped against the roof
762 physic.set_velocity_y(.1);
765 if(fabsf(hit.normal.x) > .9) { // hit on the side?
766 physic.set_velocity_x(0);
772 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
774 if(controller->pressed(Controller::UP))
775 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
782 Player::make_invincible()
784 sound_manager->play_sound("invincible");
785 invincible_timer.start(TUX_INVINCIBLE_TIME);
786 Sector::current()->play_music(HERRING_MUSIC);
791 Player::kill(HurtMode mode)
797 safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
800 sound_manager->play_sound("hurt");
802 physic.set_velocity_x(0);
804 if (mode == SHRINK && is_big())
806 if (player_status->bonus == FIRE_BONUS
807 || player_status->bonus == ICE_BONUS)
809 safe_timer.start(TUX_SAFE_TIME);
810 player_status->bonus = GROWUP_BONUS;
814 growing_timer.start(GROWING_TIME);
815 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
816 bbox.set_height(31.8);
818 player_status->bonus = NO_BONUS;
823 physic.enable_gravity(true);
824 physic.set_acceleration(0, 0);
825 physic.set_velocity(0, 700);
826 player_status->lives -= 1;
827 player_status->bonus = NO_BONUS;
829 dying_timer.start(3.0);
830 flags |= FLAG_NO_COLLDET;
835 Player::move(const Vector& vector)
837 bbox.set_pos(vector);
839 bbox.set_size(31.8, 63.8);
841 bbox.set_size(31.8, 31.8);
842 on_ground_flag = false;
844 last_ground_y = vector.y;
850 Player::check_bounds(Camera* camera)
852 /* Keep tux in bounds: */
854 { // Lock Tux to the size of the level, so that he doesn't fall of
856 bbox.set_pos(Vector(0, get_pos().y));
859 /* Keep in-bounds, vertically: */
860 if (get_pos().y > Sector::current()->solids->get_height() * 32)
867 // can happen if back scrolling is disabled
868 if(get_pos().x < camera->get_translation().x) {
869 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
872 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
875 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
884 if(collision_object_map(bbox)) {
893 Player::bounce(BadGuy& )
895 //Make sure we stopped flapping
897 falling_from_flap = false;
899 if(controller->hold(Controller::JUMP))
900 physic.set_velocity_y(520);
902 physic.set_velocity_y(300);