From: Christoph Sommer Date: Sun, 19 Mar 2006 23:52:06 +0000 (+0000) Subject: Badguys rely on new (dummy-)function get_nearest_player. / X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=0d9d5685bf4d2308364dcff501f188295b71891c;p=supertux.git Badguys rely on new (dummy-)function get_nearest_player. / Improved Sleeping Spiky wakeup trigger SVN-Revision: 3105 --- diff --git a/src/badguy/angrystone.cpp b/src/badguy/angrystone.cpp index a3eb78122..5caee62e7 100644 --- a/src/badguy/angrystone.cpp +++ b/src/badguy/angrystone.cpp @@ -95,7 +95,7 @@ AngryStone::active_update(float elapsed_time) { BadGuy::active_update(elapsed_time); if (state == IDLE) { - MovingObject* player = Sector::current()->player; + MovingObject* player = this->get_nearest_player(); MovingObject* badguy = this; const Vector playerPos = player->get_pos(); const Vector badguyPos = badguy->get_pos(); diff --git a/src/badguy/badguy.cpp b/src/badguy/badguy.cpp index 902a12f86..c364556a2 100644 --- a/src/badguy/badguy.cpp +++ b/src/badguy/badguy.cpp @@ -347,3 +347,17 @@ BadGuy::may_fall_off_platform() // Watch out there buddy, you might take a sticky end! return true; } + +Player* +BadGuy::get_nearest_player() +{ + // FIXME: does not really return nearest player + + std::vector players = Sector::current()->get_players(); + for (std::vector::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) { + Player* player = *playerIter; + return player; + } + + return 0; +} diff --git a/src/badguy/badguy.hpp b/src/badguy/badguy.hpp index 278d3e663..b3ae59f93 100644 --- a/src/badguy/badguy.hpp +++ b/src/badguy/badguy.hpp @@ -131,11 +131,9 @@ protected: { return state; } /** - * returns a pointer to the player, try to avoid this function to avoid - * problems later when we have multiple players or no player in scripted - * sequence. + * returns a pointer to the nearest player or 0 if no player is available */ - Player* get_player(); + Player* get_nearest_player(); Sprite* sprite; Physic physic; diff --git a/src/badguy/flyingsnowball.cpp b/src/badguy/flyingsnowball.cpp index c25a8cbd2..1fa4374ac 100644 --- a/src/badguy/flyingsnowball.cpp +++ b/src/badguy/flyingsnowball.cpp @@ -96,8 +96,12 @@ FlyingSnowBall::active_update(float elapsed_time) timer.start(FLYTIME); } movement=physic.get_movement(elapsed_time); - dir= Sector::current()->player->get_pos().x>get_pos().x?RIGHT:LEFT; - sprite->set_action(dir == LEFT ? "left" : "right"); + + Player* player = this->get_nearest_player(); + if (player) { + dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT; + sprite->set_action(dir == LEFT ? "left" : "right"); + } } IMPLEMENT_FACTORY(FlyingSnowBall, "flyingsnowball") diff --git a/src/badguy/jumpy.cpp b/src/badguy/jumpy.cpp index 2b450b23f..f5511c857 100644 --- a/src/badguy/jumpy.cpp +++ b/src/badguy/jumpy.cpp @@ -83,8 +83,11 @@ Jumpy::active_update(float elapsed_time) { BadGuy::active_update(elapsed_time); - dir = Sector::current()->player->get_pos().x > get_pos().x - ? RIGHT : LEFT; + Player* player = this->get_nearest_player(); + if (player) + { + dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT; + } if (!groundhit_pos_set) { diff --git a/src/badguy/nolok_01.cpp b/src/badguy/nolok_01.cpp index 513f22af1..64650afb3 100644 --- a/src/badguy/nolok_01.cpp +++ b/src/badguy/nolok_01.cpp @@ -147,7 +147,7 @@ Nolok_01::kill_fall() if (bullet_hitpoints <= 0) { hitpoints = 0; sound_manager->play("sounds/fall.wav", this, - Sector::current()->player->get_pos()); + this->get_pos()); physic.set_velocity_y(0); physic.enable_gravity(true); set_state(STATE_FALLING); diff --git a/src/badguy/plant.cpp b/src/badguy/plant.cpp index 2017d9200..e37ac6db4 100644 --- a/src/badguy/plant.cpp +++ b/src/badguy/plant.cpp @@ -89,17 +89,23 @@ Plant::active_update(float elapsed_time) { BadGuy::active_update(elapsed_time); if(state == PLANT_SLEEPING) { - const Vector playerPos = Sector::current()->player->get_pos(); - const Vector myPos = this->get_pos(); - float dx = (playerPos.x - myPos.x); - float dy = (playerPos.y - myPos.y); - - // if we "see" the player - if ((((dir == LEFT) && (dx > -128) && (dx < 0)) || ((dir == RIGHT) && (dx > 0) && (dx < 128))) && (dy > -32) && (dy < 32)) { - // wake up - sprite->set_action(dir == LEFT ? "waking-left" : "waking-right"); - if(!timer.started()) timer.start(WAKE_TIME); - state = PLANT_WAKING; + + Player* player = this->get_nearest_player(); + if (player) { + Rect mb = this->get_bbox(); + Rect pb = player->get_bbox(); + + bool inReach_left = (pb.p2.x >= mb.p2.x-((dir == LEFT) ? 256 : 0)); + bool inReach_right = (pb.p1.x <= mb.p1.x+((dir == RIGHT) ? 256 : 0)); + bool inReach_top = (pb.p2.y >= mb.p2.y); + bool inReach_bottom = (pb.p1.y <= mb.p1.y); + + if (inReach_left && inReach_right && inReach_top && inReach_bottom) { + // wake up + sprite->set_action(dir == LEFT ? "waking-left" : "waking-right"); + if(!timer.started()) timer.start(WAKE_TIME); + state = PLANT_WAKING; + } } } diff --git a/src/badguy/spidermite.cpp b/src/badguy/spidermite.cpp index 2acd750f7..e9584f7d2 100644 --- a/src/badguy/spidermite.cpp +++ b/src/badguy/spidermite.cpp @@ -96,8 +96,12 @@ SpiderMite::active_update(float elapsed_time) timer.start(FLYTIME); } movement=physic.get_movement(elapsed_time); - dir= Sector::current()->player->get_pos().x>get_pos().x?RIGHT:LEFT; - sprite->set_action(dir == LEFT ? "left" : "right"); + + Player* player = this->get_nearest_player(); + if (player) { + dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT; + sprite->set_action(dir == LEFT ? "left" : "right"); + } } IMPLEMENT_FACTORY(SpiderMite, "spidermite") diff --git a/src/badguy/sspiky.cpp b/src/badguy/sspiky.cpp index da8db4866..a0fd8f0ff 100644 --- a/src/badguy/sspiky.cpp +++ b/src/badguy/sspiky.cpp @@ -89,17 +89,23 @@ SSpiky::active_update(float elapsed_time) { BadGuy::active_update(elapsed_time); if(state == SSPIKY_SLEEPING) { - const Vector playerPos = Sector::current()->player->get_pos(); - const Vector myPos = this->get_pos(); - float dx = (playerPos.x - myPos.x); - float dy = (playerPos.y - myPos.y); - - // if we "see" the player - if ((((dir == LEFT) && (dx > -128) && (dx < 0)) || ((dir == RIGHT) && (dx > 0) && (dx < 128))) && (dy > -32) && (dy < 32)) { - // wake up - sprite->set_action(dir == LEFT ? "waking-left" : "waking-right"); - if(!timer.started()) timer.start(WAKE_TIME); - state = SSPIKY_WAKING; + + Player* player = this->get_nearest_player(); + if (player) { + Rect mb = this->get_bbox(); + Rect pb = player->get_bbox(); + + bool inReach_left = (pb.p2.x >= mb.p2.x-((dir == LEFT) ? 256 : 0)); + bool inReach_right = (pb.p1.x <= mb.p1.x+((dir == RIGHT) ? 256 : 0)); + bool inReach_top = (pb.p2.y >= mb.p2.y); + bool inReach_bottom = (pb.p1.y <= mb.p1.y); + + if (inReach_left && inReach_right && inReach_top && inReach_bottom) { + // wake up + sprite->set_action(dir == LEFT ? "waking-left" : "waking-right"); + if(!timer.started()) timer.start(WAKE_TIME); + state = SSPIKY_WAKING; + } } } diff --git a/src/badguy/stalactite.cpp b/src/badguy/stalactite.cpp index b06a979b2..ab4fc4dc3 100644 --- a/src/badguy/stalactite.cpp +++ b/src/badguy/stalactite.cpp @@ -49,12 +49,14 @@ void Stalactite::active_update(float elapsed_time) { if(state == STALACTITE_HANGING) { - Player* player = Sector::current()->player; - if(player->get_bbox().p2.x > bbox.p1.x - SHAKE_RANGE - && player->get_bbox().p1.x < bbox.p2.x + SHAKE_RANGE - && player->get_bbox().p2.y > bbox.p1.y) { - timer.start(SHAKE_TIME); - state = STALACTITE_SHAKING; + Player* player = this->get_nearest_player(); + if (player) { + if(player->get_bbox().p2.x > bbox.p1.x - SHAKE_RANGE + && player->get_bbox().p1.x < bbox.p2.x + SHAKE_RANGE + && player->get_bbox().p2.y > bbox.p1.y) { + timer.start(SHAKE_TIME); + state = STALACTITE_SHAKING; + } } } else if(state == STALACTITE_SHAKING) { if(timer.check()) { diff --git a/src/badguy/yeti.cpp b/src/badguy/yeti.cpp index 10a33e076..f11a6e241 100644 --- a/src/badguy/yeti.cpp +++ b/src/badguy/yeti.cpp @@ -192,13 +192,16 @@ Yeti::drop_stalactite() YetiStalactite* nearest = 0; float dist = FLT_MAX; + Player* player = this->get_nearest_player(); + if (!player) return; + Sector* sector = Sector::current(); for(Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) { YetiStalactite* stalactite = dynamic_cast (*i); if(stalactite && stalactite->is_hanging()) { float sdist - = fabsf(stalactite->get_pos().x - sector->player->get_pos().x); + = fabsf(stalactite->get_pos().x - player->get_pos().x); if(sdist < dist) { nearest = stalactite; dist = sdist; diff --git a/src/badguy/zeekling.cpp b/src/badguy/zeekling.cpp index f6d795d0a..8861ba877 100644 --- a/src/badguy/zeekling.cpp +++ b/src/badguy/zeekling.cpp @@ -129,7 +129,9 @@ Zeekling::collision_solid(GameObject& , const CollisionHit& hit) */ bool Zeekling::should_we_dive() { - const MovingObject* player = Sector::current()->player; + const MovingObject* player = this->get_nearest_player(); + if (!player) return false; + const MovingObject* badguy = this; const Vector playerPos = player->get_pos(); diff --git a/src/sector.hpp b/src/sector.hpp index 99d874611..05d5af47a 100644 --- a/src/sector.hpp +++ b/src/sector.hpp @@ -111,6 +111,13 @@ public: */ bool is_free_space(const Rect& rect) const; + /** + * returns a list of players currently in the sector + */ + std::vector get_players() { + return std::vector(1, this->player); + } + private: void collision_object(MovingObject* object1, MovingObject* object2) const; GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);