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,
83 Uint32 drawing_effect)
86 head->draw(context, pos, layer-1, drawing_effect);
88 body->draw(context, pos, layer-3, drawing_effect);
90 arms->draw(context, pos, layer, drawing_effect);
92 feet->draw(context, pos, layer-2, drawing_effect);
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::action(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_sound("skid");
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.start(0);
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);
358 // jump higher if we are running
359 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM)
360 physic.set_velocity_y(580);
362 physic.set_velocity_y(520);
365 //bbox.move(Vector(0, -1));
370 flaps_nb = 0; // Ricardo's flapping
372 sound_manager->play_sound("bigjump");
374 sound_manager->play_sound("jump");
375 } else if(!controller->hold(Controller::JUMP)) { // Let go of jump key
376 if (!flapping && !duck && !falling_from_flap && !on_ground()) {
379 if (jumping && physic.get_velocity_y() > 0) {
381 physic.set_velocity_y(0);
385 // temporary to help player's choosing a flapping
386 if(flapping_mode == RICARDO_FLAP) {
387 // Flapping, Ricardo's version
388 // similar to SM3 Fox
389 if(controller->pressed(Controller::JUMP) && can_flap && flaps_nb < 3) {
390 physic.set_velocity_y(350);
391 physic.set_velocity_x(physic.get_velocity_x() * 35);
394 } else if(flapping_mode == MAREK_FLAP) {
395 // Flapping, Marek's version
396 if (controller->hold(Controller::JUMP) && can_flap)
398 if (!flapping_timer.started())
400 flapping_timer.start(TUX_FLAPPING_TIME);
401 flapping_velocity = physic.get_velocity_x();
403 if (flapping_timer.check())
406 falling_from_flap = true;
410 if (!flapping_timer.check()) {
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)) {
419 physic.set_velocity_x(cv);
420 physic.set_velocity_y(
421 flapping_timer.get_timegone()/.850);
424 } else if(flapping_mode == RYAN_FLAP) {
425 // Flapping, Ryan's version
426 if (controller->hold(Controller::JUMP) && can_flap)
428 if (!flapping_timer.started())
430 flapping_timer.start(TUX_FLAPPING_TIME);
432 if (flapping_timer.check())
435 falling_from_flap = true;
439 if (flapping && flapping_timer.get_timegone() <= TUX_FLAPPING_TIME
440 && physic.get_velocity_y() < 0)
442 float gravity = Sector::current()->gravity;
444 float xr = (fabsf(physic.get_velocity_x()) / MAX_RUN_XM);
446 // XXX: magic numbers. should be a percent of gravity
447 // gravity is (by default) -0.1f
448 physic.set_acceleration_y(12 + 1*xr);
451 // To slow down x-vel when flapping (not working)
452 if (fabsf(physic.get_velocity_x()) > MAX_WALK_XM)
454 if (physic.get_velocity_x() < 0)
455 physic.set_acceleration_x(1.0f);
456 else if (physic.get_velocity_x() > 0)
457 physic.set_acceleration_x(-1.0f);
462 physic.set_acceleration_y(0);
466 /* In case the player has pressed Down while in a certain range of air,
467 enable butt jump action */
468 if (controller->hold(Controller::DOWN) && !butt_jump && !duck)
469 //if(tiles_on_air(TILES_FOR_BUTTJUMP) && jumping)
472 /* When Down is not held anymore, disable butt jump */
473 if(butt_jump && !controller->hold(Controller::DOWN))
478 if (butt_jump && on_ground() && is_big()) {
481 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
483 Sector::current()->add_smoke_cloud(
484 Vector(get_pos().x - 32, get_pos().y + 32));
488 // Break bricks beneath Tux
489 if(Sector::current()->trybreakbrick(
490 Vector(base.x + 1, base.y + base.height), false)
491 || Sector::current()->trybreakbrick(
492 Vector(base.x + base.width - 1, base.y + base.height), false)) {
493 physic.set_velocity_y(2);
497 // Kill nearby badguys
498 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
499 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
500 i != gameobjects.end();
502 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
504 // don't kill when badguys are already dying or in a certain mode
505 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
506 badguy->mode != BadGuy::BOMB_EXPLODE) {
507 if (fabsf(base.x - badguy->base.x) < 96 &&
508 fabsf(base.y - badguy->base.y) < 64)
516 /** jumping is only allowed if we're about to touch ground soon and if the
517 * button has been up in between the last jump
521 if ( (issolid(get_pos().x + bbox.get_width() / 2,
522 get_pos().y + bbox.get_height() + 64) ||
523 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
524 issolid(get_pos().x + bbox.get_width() - 1,
525 get_pos().y + bbox.get_height() + 64))
528 && input.jump && !input.old_jump)
536 Player::handle_input()
538 /* Handle horizontal movement: */
539 handle_horizontal_input();
542 if (on_ground() && !controller->hold(Controller::JUMP))
544 handle_vertical_input();
547 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
548 if(Sector::current()->add_bullet(
549 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
550 : Vector(32, bbox.get_height()/2)),
551 physic.get_velocity_x(), dir))
552 shooting_timer.start(SHOOTING_TIME);
556 if (controller->hold(Controller::DOWN) && is_big() && !duck
557 && physic.get_velocity_y() == 0 && on_ground()) {
559 bbox.move(Vector(0, 32));
560 bbox.set_height(31.8);
561 } else if(!controller->hold(Controller::DOWN) && is_big() && duck) {
562 // try if we can really unduck
563 bbox.move(Vector(0, -32));
564 bbox.set_height(63.8);
568 // when unducking in air we need some space to do so
569 if(on_ground() || !collision_object_map(bbox)) {
572 // undo the ducking changes
573 bbox.move(Vector(0, 32));
574 bbox.set_height(31.8);
581 Player::set_bonus(BonusType type, bool animate)
583 if(player_status->bonus == type)
586 if(player_status->bonus == NO_BONUS) {
587 bbox.set_height(63.8);
588 bbox.move(Vector(0, -32));
590 growing_timer.start(GROWING_TIME);
593 player_status->bonus = type;
597 Player::draw(DrawingContext& context)
599 TuxBodyParts* tux_body;
601 if (player_status->bonus == GROWUP_BONUS)
603 else if (player_status->bonus == FIRE_BONUS)
605 else if (player_status->bonus == ICE_BONUS)
608 tux_body = small_tux;
610 int layer = LAYER_OBJECTS + 10;
612 /* Set Tux sprite action */
613 if (duck && is_big())
616 tux_body->set_action("duck-left");
618 tux_body->set_action("duck-right");
620 else if (skidding_timer.started() && !skidding_timer.check())
623 tux_body->set_action("skid-left");
625 tux_body->set_action("skid-right");
627 else if (kick_timer.started() && !kick_timer.check())
630 tux_body->set_action("kick-left");
632 tux_body->set_action("kick-right");
634 else if (butt_jump && is_big())
637 tux_body->set_action("buttjump-left");
639 tux_body->set_action("buttjump-right");
641 else if (physic.get_velocity_y() != 0)
644 tux_body->set_action("jump-left");
646 tux_body->set_action("jump-right");
650 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
653 tux_body->set_action("stand-left");
655 tux_body->set_action("stand-right");
660 tux_body->set_action("walk-left");
662 tux_body->set_action("walk-right");
666 if(idle_timer.check())
671 tux_body->head->set_action("idle-left", 1);
673 tux_body->head->set_action("idle-right", 1);
678 // Tux is holding something
679 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
680 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
685 tux_body->arms->set_action("duck+grab-left");
687 tux_body->arms->set_action("duck+grab-right");
692 tux_body->arms->set_action("grab-left");
694 tux_body->arms->set_action("grab-right");
700 smalltux_gameover->draw(context, get_pos(), layer);
701 } else if(growing_timer.get_timeleft() > 0) {
705 context.draw_surface(growingtux_right[GROWING_FRAMES-1 -
706 int((growing_timer.get_timegone() *
707 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
709 context.draw_surface(growingtux_left[GROWING_FRAMES-1 -
710 int((growing_timer.get_timegone() *
711 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
716 context.draw_surface(growingtux_right[
717 int((growing_timer.get_timegone() *
718 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
720 context.draw_surface(growingtux_left[
721 int((growing_timer.get_timegone() *
722 GROWING_FRAMES) / GROWING_TIME)],
726 else if (safe_timer.started() && size_t(global_time*40)%2)
729 tux_body->draw(context, get_pos(), layer);
731 // Draw blinking star overlay
732 if (invincible_timer.started() &&
733 (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING
734 || size_t(global_time*20)%2)
737 if (!is_big() || duck)
738 smalltux_star->draw(context, get_pos(), layer + 5);
740 bigtux_star->draw(context, get_pos(), layer + 5);
745 Player::collision(GameObject& other, const CollisionHit& hit)
747 Portable* portable = dynamic_cast<Portable*> (&other);
748 if(portable && grabbed_object == 0 && controller->hold(Controller::ACTION)
749 && fabsf(hit.normal.x) > .9) {
750 grabbed_object = portable;
754 if(other.get_flags() & FLAG_SOLID) {
755 if(hit.normal.y < 0) { // landed on floor?
756 if (physic.get_velocity_y() < 0)
757 physic.set_velocity_y(0);
758 on_ground_flag = true;
759 } else if(hit.normal.y > 0) { // bumped against the roof
760 physic.set_velocity_y(.1);
763 if(fabsf(hit.normal.x) > .9) { // hit on the side?
764 physic.set_velocity_x(0);
770 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
772 if(controller->pressed(Controller::UP))
773 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
780 Player::make_invincible()
782 sound_manager->play_sound("invincible");
783 invincible_timer.start(TUX_INVINCIBLE_TIME);
784 Sector::current()->play_music(HERRING_MUSIC);
789 Player::kill(HurtMode mode)
795 safe_timer.get_timeleft() > 0 || invincible_timer.get_timeleft() > 0)
798 sound_manager->play_sound("hurt");
800 physic.set_velocity_x(0);
802 if (mode == SHRINK && is_big())
804 if (player_status->bonus == FIRE_BONUS
805 || player_status->bonus == ICE_BONUS)
807 safe_timer.start(TUX_SAFE_TIME);
808 player_status->bonus = GROWUP_BONUS;
812 growing_timer.start(GROWING_TIME);
813 safe_timer.start(TUX_SAFE_TIME + GROWING_TIME);
814 bbox.set_height(31.8);
816 player_status->bonus = NO_BONUS;
821 physic.enable_gravity(true);
822 physic.set_acceleration(0, 0);
823 physic.set_velocity(0, 700);
824 player_status->lives -= 1;
825 player_status->bonus = NO_BONUS;
827 dying_timer.start(3.0);
828 flags |= FLAG_NO_COLLDET;
833 Player::move(const Vector& vector)
835 bbox.set_pos(vector);
837 bbox.set_size(31.8, 63.8);
839 bbox.set_size(31.8, 31.8);
840 on_ground_flag = false;
842 last_ground_y = vector.y;
848 Player::check_bounds(Camera* camera)
850 /* Keep tux in bounds: */
852 { // Lock Tux to the size of the level, so that he doesn't fall of
854 bbox.set_pos(Vector(0, get_pos().y));
857 /* Keep in-bounds, vertically: */
858 if (get_pos().y > Sector::current()->solids->get_height() * 32)
865 // can happen if back scrolling is disabled
866 if(get_pos().x < camera->get_translation().x) {
867 bbox.set_pos(Vector(camera->get_translation().x, get_pos().y));
870 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
873 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
882 if(collision_object_map(bbox)) {
891 Player::bounce(BadGuy& )
893 //Make sure we stopped flapping
895 falling_from_flap = false;
897 if(controller->hold(Controller::JUMP))
898 physic.set_velocity_y(520);
900 physic.set_velocity_y(200);