4 // Copyright (C) 2006 Matthias Braun <matze@braunis.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 "statistics.hpp"
35 #include "game_session.hpp"
36 #include "object/tilemap.hpp"
37 #include "object/camera.hpp"
38 #include "object/particles.hpp"
39 #include "object/portable.hpp"
40 #include "object/bullet.hpp"
41 #include "trigger/trigger_base.hpp"
42 #include "control/joystickkeyboardcontroller.hpp"
43 #include "scripting/squirrel_util.hpp"
45 #include "platform.hpp"
46 #include "badguy/badguy.hpp"
47 #include "player_status.hpp"
49 #include "falling_coin.hpp"
50 #include "random_generator.hpp"
51 #include "object/sprite_particle.hpp"
53 static const int TILES_FOR_BUTTJUMP = 3;
54 static const float SHOOTING_TIME = .150;
55 /// time before idle animation starts
56 static const float IDLE_TIME = 2.5;
58 static const float WALK_ACCELERATION_X = 300;
59 static const float RUN_ACCELERATION_X = 400;
60 static const float SKID_XM = 200;
61 static const float SKID_TIME = .3;
62 static const float MAX_WALK_XM = 230;
63 static const float MAX_RUN_XM = 320;
64 static const float WALK_SPEED = 100;
66 static const float KICK_TIME = .3;
67 static const float CHEER_TIME = 1;
69 static const float UNDUCK_HURT_TIME = 0.25; /**< if Tux cannot unduck for this long, he will get hurt */
72 Surface* growingtux_left[GROWING_FRAMES];
73 Surface* growingtux_right[GROWING_FRAMES];
75 Surface* tux_life = 0;
77 TuxBodyParts* small_tux = 0;
78 TuxBodyParts* big_tux = 0;
79 TuxBodyParts* fire_tux = 0;
80 TuxBodyParts* ice_tux = 0;
83 TuxBodyParts::set_action(std::string action, int loops)
86 head->set_action(action, loops);
88 body->set_action(action, loops);
90 arms->set_action(action, loops);
92 feet->set_action(action, loops);
96 TuxBodyParts::draw(DrawingContext& context, const Vector& pos, int layer)
99 head->draw(context, pos, layer-1);
101 body->draw(context, pos, layer-3);
103 arms->draw(context, pos, layer+10);
105 feet->draw(context, pos, layer-2);
108 Player::Player(PlayerStatus* _player_status)
109 : player_status(_player_status), grabbed_object(NULL), ghost_mode(false)
111 controller = main_controller;
112 smalltux_gameover = sprite_manager->create("images/creatures/tux_small/smalltux-gameover.sprite");
113 smalltux_star = sprite_manager->create("images/creatures/tux_small/smalltux-star.sprite");
114 bigtux_star = sprite_manager->create("images/creatures/tux_big/bigtux-star.sprite");
116 sound_manager->preload("sounds/bigjump.wav");
117 sound_manager->preload("sounds/jump.wav");
118 sound_manager->preload("sounds/hurt.wav");
119 sound_manager->preload("sounds/skid.wav");
120 sound_manager->preload("sounds/flip.wav");
121 sound_manager->preload("sounds/invincible.wav");
128 delete smalltux_gameover;
129 delete smalltux_star;
137 set_size(31.8, 62.8);
139 set_size(31.8, 30.8);
149 fall_mode = ON_GROUND;
154 backflipping = false;
155 backflip_direction = 0;
158 on_ground_flag = false;
159 grabbed_object = NULL;
161 floor_normal = Vector(0,-1);
167 Player::expose(HSQUIRRELVM vm, SQInteger table_idx)
169 Scripting::Player* interface = static_cast<Scripting::Player*> (this);
170 Scripting::expose_object(vm, table_idx, interface, "Tux", false);
174 Player::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
176 Scripting::unexpose_object(vm, table_idx, "Tux");
180 Player::set_controller(Controller* controller)
182 this->controller = controller;
186 Player::adjust_height(float new_height)
189 bbox2.move(Vector(0, bbox.get_height() - new_height));
190 bbox2.set_height(new_height);
191 if (!Sector::current()->is_free_space(bbox2)) return false;
192 // adjust bbox accordingly
193 // note that we use members of moving_object for this, so we can run this during CD, too
195 set_size(bbox2.get_width(), bbox2.get_height());
200 Player::update(float elapsed_time)
202 if(dying && dying_timer.check()) {
207 if(!dying && !deactivated)
210 // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
211 if (deactivated) apply_friction();
213 // extend/shrink tux collision rectangle so that we fall through/walk over 1
215 if(fabsf(physic.get_velocity_x()) > MAX_WALK_XM) {
221 // on downward slopes, adjust vertical velocity to match slope angle
223 if (floor_normal.y != 0) {
224 if ((floor_normal.x * physic.get_velocity_x()) > 0) {
225 // we overdo it a little, just to be on the safe side
226 physic.set_velocity_y(-physic.get_velocity_x() * (floor_normal.x / floor_normal.y) * 2);
231 // handle backflipping
233 //prevent player from changing direction when backflipping
234 dir = (backflip_direction == 1) ? LEFT : RIGHT;
235 if (backflip_timer.started()) physic.set_velocity_x(100 * backflip_direction);
240 fall_mode = ON_GROUND;
241 last_ground_y = get_pos().y;
243 if(get_pos().y > last_ground_y)
245 else if(fall_mode == ON_GROUND)
249 // check if we landed
252 if (backflipping && (!backflip_timer.started())) {
253 backflipping = false;
254 backflip_direction = 0;
256 // if controls are currently deactivated, we take care of standing up ourselves
257 if (deactivated) do_standup();
263 if (butt_jump && on_ground() && is_big()) {
266 Sector::current()->add_smoke_cloud(Vector(get_pos().x - 32, get_pos().y));
268 Sector::current()->add_smoke_cloud(
269 Vector(get_pos().x - 32, get_pos().y + 32));
273 // Break bricks beneath Tux
274 if(Sector::current()->trybreakbrick(
275 Vector(base.x + 1, base.y + base.height), false)
276 || Sector::current()->trybreakbrick(
277 Vector(base.x + base.width - 1, base.y + base.height), false)) {
278 physic.set_velocity_y(-2);
282 // Kill nearby badguys
283 std::vector<GameObject*> gameobjects = Sector::current()->gameobjects;
284 for (std::vector<GameObject*>::iterator i = gameobjects.begin();
285 i != gameobjects.end();
287 BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
289 // don't kill when badguys are already dying or in a certain mode
290 if(badguy->dying == DYING_NOT && badguy->mode != BadGuy::BOMB_TICKING &&
291 badguy->mode != BadGuy::BOMB_EXPLODE) {
292 if (fabsf(base.x - badguy->base.x) < 96 &&
293 fabsf(base.y - badguy->base.y) < 64)
301 // calculate movement for this frame
302 movement = physic.get_movement(elapsed_time);
304 if(grabbed_object != NULL && !dying) {
305 Vector pos = get_pos() +
306 Vector(dir == LEFT ? -16 : 16, get_bbox().get_height()*0.66666 - 32);
307 grabbed_object->grab(*this, pos, dir);
310 if(grabbed_object != NULL && dying){
311 grabbed_object->ungrab(*this, dir);
312 grabbed_object = NULL;
315 on_ground_flag = false;
317 // when invincible, spawn particles
318 if (invincible_timer.started() && !dying)
320 if (systemRandom.rand(0, 2) == 0) {
321 float px = systemRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
322 float py = systemRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
323 Vector ppos = Vector(px, py);
324 Vector pspeed = Vector(0, 0);
325 Vector paccel = Vector(0, 0);
326 // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
327 if (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) {
328 // make every other a longer sparkle to make trail a bit fuzzy
329 if (size_t(game_time*20)%2) {
330 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "small", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
332 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "medium", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
335 Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite", "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
345 return on_ground_flag;
351 if(player_status->bonus == NO_BONUS)
358 Player::apply_friction()
360 if ((on_ground()) && (fabs(physic.get_velocity_x()) < WALK_SPEED)) {
361 physic.set_velocity_x(0);
362 physic.set_acceleration_x(0);
363 } else if(physic.get_velocity_x() < 0) {
364 physic.set_acceleration_x(WALK_ACCELERATION_X * 1.5);
366 physic.set_acceleration_x(WALK_ACCELERATION_X * -1.5);
370 // if we're on ice slow down acceleration or deceleration
371 if (isice(base.x, base.y + base.height))
373 /* the acceleration/deceleration rate on ice is inversely proportional to
374 * the current velocity.
377 // increasing 1 will increase acceleration/deceleration rate
378 // decreasing 1 will decrease acceleration/deceleration rate
379 // must stay above zero, though
380 if (ax != 0) ax *= 1 / fabs(vx);
387 Player::handle_horizontal_input()
389 float vx = physic.get_velocity_x();
390 float vy = physic.get_velocity_y();
391 float ax = physic.get_acceleration_x();
392 float ay = physic.get_acceleration_y();
395 if(!duck || physic.get_velocity_y() != 0) {
396 if(controller->hold(Controller::LEFT) && !controller->hold(Controller::RIGHT)) {
400 } else if(!controller->hold(Controller::LEFT)
401 && controller->hold(Controller::RIGHT)) {
408 if (!controller->hold(Controller::ACTION)) {
409 ax = dirsign * WALK_ACCELERATION_X;
411 if(vx >= MAX_WALK_XM && dirsign > 0) {
414 } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
419 ax = dirsign * RUN_ACCELERATION_X;
421 if(vx >= MAX_RUN_XM && dirsign > 0) {
424 } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
430 // we can reach WALK_SPEED without any acceleration
431 if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
432 vx = dirsign * WALK_SPEED;
435 // changing directions?
436 if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
438 if(fabs(vx)>SKID_XM && !skidding_timer.started()) {
439 skidding_timer.start(SKID_TIME);
440 sound_manager->play("sounds/skid.wav");
441 // dust some particles
442 Sector::current()->add_object(
444 Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
445 dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
446 Vector(280, -260), Vector(0, 300), 3, Color(.4, .4, .4), 3, .8,
455 physic.set_velocity(vx, vy);
456 physic.set_acceleration(ax, ay);
458 // we get slower when not pressing any keys
476 if (!is_big()) return;
478 if (physic.get_velocity_y() != 0) return;
479 if (!on_ground()) return;
481 if (adjust_height(31.8)) {
483 unduck_hurt_timer.stop();
490 Player::do_standup() {
492 if (!is_big()) return;
493 if (backflipping) return;
495 if (adjust_height(63.8)) {
497 unduck_hurt_timer.stop();
499 // if timer is not already running, start it.
500 if (unduck_hurt_timer.get_period() == 0) {
501 unduck_hurt_timer.start(UNDUCK_HURT_TIME);
503 else if (unduck_hurt_timer.check()) {
511 Player::do_backflip() {
513 if (!on_ground()) return;
515 // TODO: we don't have an animation for firetux backflipping, so let's revert to bigtux
516 set_bonus(GROWUP_BONUS, true);
518 backflip_direction = (dir == LEFT)?(+1):(-1);
521 sound_manager->play("sounds/flip.wav");
522 backflip_timer.start(0.15);
526 Player::do_jump(float yspeed) {
527 if (!on_ground()) return;
529 physic.set_velocity_y(yspeed);
530 //bbox.move(Vector(0, -1));
532 on_ground_flag = false;
537 sound_manager->play("sounds/bigjump.wav");
539 sound_manager->play("sounds/jump.wav");
544 Player::handle_vertical_input()
548 if(controller->pressed(Controller::JUMP) && (can_jump)) {
550 // when running, only jump a little bit; else do a backflip
551 if (physic.get_velocity_x() != 0) do_jump(-300); else do_backflip();
553 // jump a bit higher if we are running; else do a normal jump
554 if (fabs(physic.get_velocity_x()) > MAX_WALK_XM) do_jump(-580); else do_jump(-520);
557 // Let go of jump key
558 else if(!controller->hold(Controller::JUMP)) {
559 if (!backflipping && jumping && physic.get_velocity_y() < 0) {
561 physic.set_velocity_y(0);
565 /* In case the player has pressed Down while in a certain range of air,
566 enable butt jump action */
567 if (controller->hold(Controller::DOWN) && !butt_jump && !duck && is_big() && jumping) {
571 /* When Down is not held anymore, disable butt jump */
572 if(butt_jump && !controller->hold(Controller::DOWN)) butt_jump = false;
574 /** jumping is only allowed if we're about to touch ground soon and if the
575 * button has been up in between the last jump
579 if ( (issolid(get_pos().x + bbox.get_width() / 2,
580 get_pos().y + bbox.get_height() + 64) ||
581 issolid(get_pos().x + 1, get_pos().y + bbox.get_height() + 64) ||
582 issolid(get_pos().x + bbox.get_width() - 1,
583 get_pos().y + bbox.get_height() + 64))
586 && input.jump && !input.old_jump)
594 Player::handle_input()
597 handle_input_ghost();
601 if(!controller->hold(Controller::ACTION) && grabbed_object) {
602 // move the grabbed object a bit away from tux
603 Vector pos = get_pos() +
604 Vector(dir == LEFT ? -bbox.get_width()-1 : bbox.get_width()+1,
605 bbox.get_height()*0.66666 - 32);
606 Rect dest(pos, pos + Vector(32, 32));
607 if(Sector::current()->is_free_space(dest)) {
608 MovingObject* moving_object = dynamic_cast<MovingObject*> (grabbed_object);
610 moving_object->set_pos(pos);
612 log_debug << "Non MovingObjetc grabbed?!?" << std::endl;
614 grabbed_object->ungrab(*this, dir);
615 grabbed_object = NULL;
620 if( controller->released( Controller::PEEK_LEFT ) ) {
623 if( controller->released( Controller::PEEK_RIGHT ) ) {
626 if( controller->pressed( Controller::PEEK_LEFT ) ) {
629 if( controller->pressed( Controller::PEEK_RIGHT ) ) {
633 /* Handle horizontal movement: */
634 if (!backflipping) handle_horizontal_input();
637 if (on_ground() && !controller->hold(Controller::JUMP))
640 /* Handle vertical movement: */
641 handle_vertical_input();
644 if (controller->pressed(Controller::ACTION) && player_status->bonus == FIRE_BONUS) {
645 if(Sector::current()->add_bullet(
646 get_pos() + ((dir == LEFT)? Vector(0, bbox.get_height()/2)
647 : Vector(32, bbox.get_height()/2)),
648 physic.get_velocity_x(), dir))
649 shooting_timer.start(SHOOTING_TIME);
652 /* Duck or Standup! */
653 if (controller->hold(Controller::DOWN)) do_duck(); else do_standup();
658 Player::handle_input_ghost()
662 if (controller->hold(Controller::LEFT)) {
664 vx -= MAX_RUN_XM * 2;
666 if (controller->hold(Controller::RIGHT)) {
668 vx += MAX_RUN_XM * 2;
670 if ((controller->hold(Controller::UP)) || (controller->hold(Controller::JUMP))) {
671 vy -= MAX_RUN_XM * 2;
673 if (controller->hold(Controller::DOWN)) {
674 vy += MAX_RUN_XM * 2;
676 if (controller->hold(Controller::ACTION)) {
677 set_ghost_mode(false);
679 physic.set_velocity(vx, vy);
680 physic.set_acceleration(0, 0);
684 Player::add_coins(int count)
686 player_status->add_coins(count);
690 Player::add_bonus(const std::string& bonustype)
692 if(bonustype == "grow") {
693 add_bonus(GROWUP_BONUS);
694 } else if(bonustype == "fireflower") {
695 add_bonus(FIRE_BONUS);
696 } else if(bonustype == "iceflower") {
697 add_bonus(ICE_BONUS);
698 } else if(bonustype == "none") {
701 std::ostringstream msg;
702 msg << "Unknown bonus type " << bonustype;
703 throw std::runtime_error(msg.str());
708 Player::add_bonus(BonusType type, bool animate)
710 // always ignore NO_BONUS
711 if (type == NO_BONUS) {
715 // ignore GROWUP_BONUS if we're already big
716 if (type == GROWUP_BONUS) {
717 if (player_status->bonus == GROWUP_BONUS) return;
718 if (player_status->bonus == FIRE_BONUS) return;
719 if (player_status->bonus == ICE_BONUS) return;
722 set_bonus(type, animate);
726 Player::set_bonus(BonusType type, bool animate)
728 if(player_status->bonus == NO_BONUS) {
729 if (!adjust_height(62.8)) return;
731 growing_timer.start(GROWING_TIME);
734 if ((type == NO_BONUS) || (type == GROWUP_BONUS)) {
735 if ((player_status->bonus == FIRE_BONUS) && (animate)) {
736 // visually lose helmet
737 Vector ppos = Vector((bbox.p1.x + bbox.p2.x) / 2, bbox.p1.y);
738 Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
739 Vector paccel = Vector(0, 1000);
740 std::string action = (dir==LEFT)?"left":"right";
741 Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
743 player_status->max_fire_bullets = 0;
744 player_status->max_ice_bullets = 0;
746 if (type == FIRE_BONUS) player_status->max_fire_bullets++;
747 if (type == ICE_BONUS) player_status->max_ice_bullets++;
749 player_status->bonus = type;
753 Player::set_visible(bool visible)
755 this->visible = visible;
757 set_group(COLGROUP_MOVING);
759 set_group(COLGROUP_DISABLED);
763 Player::get_visible()
771 kick_timer.start(KICK_TIME);
775 Player::draw(DrawingContext& context)
780 TuxBodyParts* tux_body;
782 if (player_status->bonus == GROWUP_BONUS)
784 else if (player_status->bonus == FIRE_BONUS)
786 else if (player_status->bonus == ICE_BONUS)
789 tux_body = small_tux;
791 int layer = LAYER_OBJECTS + 1;
793 /* Set Tux sprite action */
797 tux_body->set_action("backflip-left");
799 tux_body->set_action("backflip-right");
801 else if (duck && is_big())
804 tux_body->set_action("duck-left");
806 tux_body->set_action("duck-right");
808 else if (skidding_timer.started() && !skidding_timer.check())
811 tux_body->set_action("skid-left");
813 tux_body->set_action("skid-right");
815 else if (kick_timer.started() && !kick_timer.check())
818 tux_body->set_action("kick-left");
820 tux_body->set_action("kick-right");
822 else if (butt_jump && is_big())
825 tux_body->set_action("buttjump-left");
827 tux_body->set_action("buttjump-right");
829 else if (!on_ground())
832 tux_body->set_action("jump-left");
834 tux_body->set_action("jump-right");
838 if (fabsf(physic.get_velocity_x()) < 1.0f) // standing
841 tux_body->set_action("stand-left");
843 tux_body->set_action("stand-right");
848 tux_body->set_action("walk-left");
850 tux_body->set_action("walk-right");
854 if(idle_timer.check())
859 tux_body->head->set_action("idle-left", 1);
861 tux_body->head->set_action("idle-right", 1);
866 // Tux is holding something
867 if ((grabbed_object != 0 && physic.get_velocity_y() == 0) ||
868 (shooting_timer.get_timeleft() > 0 && !shooting_timer.check()))
873 tux_body->arms->set_action("duck+grab-left");
875 tux_body->arms->set_action("duck+grab-right");
880 tux_body->arms->set_action("grab-left");
882 tux_body->arms->set_action("grab-right");
888 smalltux_gameover->draw(context, get_pos(), LAYER_FLOATINGOBJECTS + 1);
890 else if ((growing_timer.get_timeleft() > 0) && (!duck)) {
892 context.draw_surface(growingtux_right[int((growing_timer.get_timegone() *
893 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
895 context.draw_surface(growingtux_left[int((growing_timer.get_timegone() *
896 GROWING_FRAMES) / GROWING_TIME)], get_pos(), layer);
899 else if (safe_timer.started() && size_t(game_time*40)%2)
902 tux_body->draw(context, get_pos(), layer);
907 Player::collision_tile(uint32_t tile_attributes)
909 if(tile_attributes & Tile::HURTS)
914 Player::collision(GameObject& other, const CollisionHit& hit)
916 Bullet* bullet = dynamic_cast<Bullet*> (&other);
921 if(other.get_flags() & FLAG_PORTABLE) {
922 Portable* portable = dynamic_cast<Portable*> (&other);
923 assert(portable != NULL);
924 if(portable && grabbed_object == NULL
925 && controller->hold(Controller::ACTION)
926 && fabsf(hit.normal.x) > .9) {
927 grabbed_object = portable;
928 grabbed_object->grab(*this, get_pos(), dir);
933 if(other.get_flags() & FLAG_SOLID) {
935 printf("Col %p: HN: %3.1f %3.1f D %.1f P: %3.1f %3.1f M: %3.1f %3.1f\n",
937 hit.normal.x, hit.normal.y, hit.depth,
938 get_pos().x, get_pos().y,
939 movement.x, movement.y);
942 if(hit.normal.y < 0) { // landed on floor?
943 if(physic.get_velocity_y() > 0)
944 physic.set_velocity_y(0);
946 on_ground_flag = true;
948 // remember normal of this tile
949 if (hit.normal.y > -0.9) {
950 floor_normal.x = hit.normal.x;
951 floor_normal.y = hit.normal.y;
953 // slowly adjust to unisolid tiles.
954 // Necessary because our bounding box sometimes reaches through slopes and thus hits unisolid tiles
955 floor_normal.x = (floor_normal.x * 0.9) + (hit.normal.x * 0.1);
956 floor_normal.y = (floor_normal.y * 0.9) + (hit.normal.y * 0.1);
959 // hack platforms so that we stand normally on them when going down...
960 Platform* platform = dynamic_cast<Platform*> (&other);
961 if(platform != NULL) {
962 if(platform->get_speed().y > 0)
963 physic.set_velocity_y(platform->get_speed().y);
964 //physic.set_velocity_x(platform->get_speed().x);
966 } else if(hit.normal.y > 0) { // bumped against the roof
967 physic.set_velocity_y(-.1);
969 // hack platform so that we are not glued to it from below
970 Platform* platform = dynamic_cast<Platform*> (&other);
971 if(platform != NULL) {
972 physic.set_velocity_y(platform->get_speed().y);
976 if(fabsf(hit.normal.x) > .9) { // hit on the side?
977 physic.set_velocity_x(0);
980 MovingObject* omov = dynamic_cast<MovingObject*> (&other);
982 Vector mov = movement - omov->get_movement();
984 printf("W %p - HITN: %3.1f %3.1f D:%3.1f TM: %3.1f %3.1f TD: %3.1f %3.1f PM: %3.2f %3.1f\n",
986 hit.normal.x, hit.normal.y,
988 movement.x, movement.y,
989 dest.p1.x, dest.p1.y,
990 omov->get_movement().x, omov->get_movement().y);
998 assert(dynamic_cast<MovingObject*> (&other) != NULL);
1000 MovingObject* moving_object = static_cast<MovingObject*> (&other);
1001 if(moving_object->get_group() == COLGROUP_TOUCHABLE) {
1002 TriggerBase* trigger = dynamic_cast<TriggerBase*> (&other);
1004 if(controller->pressed(Controller::UP))
1005 trigger->event(*this, TriggerBase::EVENT_ACTIVATE);
1011 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
1012 if(badguy != NULL) {
1013 if(safe_timer.started() || invincible_timer.started())
1023 Player::make_invincible()
1025 sound_manager->play("sounds/invincible.wav");
1026 invincible_timer.start(TUX_INVINCIBLE_TIME);
1027 Sector::current()->play_music(HERRING_MUSIC);
1032 Player::kill(bool completely)
1034 if(dying || deactivated)
1037 if(!completely && (safe_timer.started() || invincible_timer.started()))
1040 sound_manager->play("sounds/hurt.wav");
1042 physic.set_velocity_x(0);
1044 if(!completely && is_big()) {
1045 if(player_status->bonus == FIRE_BONUS
1046 || player_status->bonus == ICE_BONUS) {
1047 safe_timer.start(TUX_SAFE_TIME);
1048 set_bonus(GROWUP_BONUS, true);
1050 //growing_timer.start(GROWING_TIME);
1051 safe_timer.start(TUX_SAFE_TIME /* + GROWING_TIME */);
1052 adjust_height(30.8);
1054 set_bonus(NO_BONUS, true);
1057 for (int i = 0; (i < 5) && (i < player_status->coins); i++)
1059 // the numbers: starting x, starting y, velocity y
1060 Sector::current()->add_object(new FallingCoin(get_pos() +
1061 Vector(systemRandom.rand(5), systemRandom.rand(-32,18)),
1062 systemRandom.rand(-100,100)));
1064 physic.enable_gravity(true);
1065 physic.set_acceleration(0, 0);
1066 physic.set_velocity(0, -700);
1067 player_status->coins -= 25;
1068 set_bonus(NO_BONUS, true);
1070 dying_timer.start(3.0);
1071 set_group(COLGROUP_DISABLED);
1073 DisplayEffect* effect = new DisplayEffect();
1074 effect->fade_out(3.0);
1075 Sector::current()->add_object(effect);
1076 sound_manager->stop_music(3.0);
1081 Player::move(const Vector& vector)
1085 // TODO: do we need the following? Seems irrelevant to moving the player
1087 set_size(31.8, 63.8);
1089 set_size(31.8, 31.8);
1091 last_ground_y = vector.y;
1097 Player::check_bounds(Camera* camera)
1099 /* Keep tux in bounds: */
1100 if (get_pos().x < 0) {
1101 // Lock Tux to the size of the level, so that he doesn't fall of
1103 set_pos(Vector(0, get_pos().y));
1106 /* Keep in-bounds, vertically: */
1107 if (get_pos().y > Sector::current()->solids->get_height() * 32) {
1112 bool adjust = false;
1113 // can happen if back scrolling is disabled
1114 if(get_pos().x < camera->get_translation().x) {
1115 set_pos(Vector(camera->get_translation().x, get_pos().y));
1118 if(get_pos().x >= camera->get_translation().x + SCREEN_WIDTH - bbox.get_width())
1121 camera->get_translation().x + SCREEN_WIDTH - bbox.get_width(),
1130 if(collision_object_map(bbox)) {
1139 Player::add_velocity(const Vector& velocity)
1141 physic.set_velocity(physic.get_velocity() + velocity);
1145 Player::add_velocity(const Vector& velocity, const Vector& end_speed)
1147 if (end_speed.x > 0) physic.set_velocity_x(std::min(physic.get_velocity_x() + velocity.x, end_speed.x));
1148 if (end_speed.x < 0) physic.set_velocity_x(std::max(physic.get_velocity_x() + velocity.x, end_speed.x));
1149 if (end_speed.y > 0) physic.set_velocity_y(std::min(physic.get_velocity_y() + velocity.y, end_speed.y));
1150 if (end_speed.y < 0) physic.set_velocity_y(std::max(physic.get_velocity_y() + velocity.y, end_speed.y));
1154 Player::bounce(BadGuy& )
1156 if(controller->hold(Controller::JUMP))
1157 physic.set_velocity_y(-520);
1159 physic.set_velocity_y(-300);
1162 //Scripting Functions Below
1165 Player::deactivate()
1167 if (deactivated) return;
1169 physic.set_velocity_x(0);
1170 physic.set_velocity_y(0);
1171 physic.set_acceleration_x(0);
1172 physic.set_acceleration_y(0);
1178 if (!deactivated) return;
1179 deactivated = false;
1182 void Player::walk(float speed)
1184 physic.set_velocity_x(speed);
1188 Player::set_ghost_mode(bool enable)
1190 if (ghost_mode == enable) return;
1193 set_group(COLGROUP_DISABLED);
1194 physic.enable_gravity(false);
1195 log_debug << "You feel lightheaded. Use movement controls to float around, press ACTION to scare badguys." << std::endl;
1198 set_group(COLGROUP_MOVING);
1199 physic.enable_gravity(true);
1200 log_debug << "You feel solid again." << std::endl;