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();
// 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<Player*> players = Sector::current()->get_players();
+ for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
+ Player* player = *playerIter;
+ return player;
+ }
+
+ return 0;
+}
{ 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;
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")
{
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)
{
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);
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;
+ }
}
}
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")
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;
+ }
}
}
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()) {
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<YetiStalactite*> (*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;
*/
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();
*/
bool is_free_space(const Rect& rect) const;
+ /**
+ * returns a list of players currently in the sector
+ */
+ std::vector<Player*> get_players() {
+ return std::vector<Player*>(1, this->player);
+ }
+
private:
void collision_object(MovingObject* object1, MovingObject* object2) const;
GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);