Badguys rely on new (dummy-)function get_nearest_player. /
authorChristoph Sommer <mail@christoph-sommer.de>
Sun, 19 Mar 2006 23:52:06 +0000 (23:52 +0000)
committerChristoph Sommer <mail@christoph-sommer.de>
Sun, 19 Mar 2006 23:52:06 +0000 (23:52 +0000)
Improved Sleeping Spiky wakeup trigger

SVN-Revision: 3105

13 files changed:
src/badguy/angrystone.cpp
src/badguy/badguy.cpp
src/badguy/badguy.hpp
src/badguy/flyingsnowball.cpp
src/badguy/jumpy.cpp
src/badguy/nolok_01.cpp
src/badguy/plant.cpp
src/badguy/spidermite.cpp
src/badguy/sspiky.cpp
src/badguy/stalactite.cpp
src/badguy/yeti.cpp
src/badguy/zeekling.cpp
src/sector.hpp

index a3eb781..5caee62 100644 (file)
@@ -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();
index 902a12f..c364556 100644 (file)
@@ -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<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;
+}
index 278d3e6..b3ae59f 100644 (file)
@@ -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;
index c25a8cb..1fa4374 100644 (file)
@@ -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")
index 2b450b2..f5511c8 100644 (file)
@@ -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)
   {
index 513f22a..64650af 100644 (file)
@@ -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);
index 2017d92..e37ac6d 100644 (file)
@@ -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;
+      }
     }
   }
 
index 2acd750..e9584f7 100644 (file)
@@ -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")
index da8db48..a0fd8f0 100644 (file)
@@ -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;
+      }
     }
   }
 
index b06a979..ab4fc4d 100644 (file)
@@ -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()) {
index 10a33e0..f11a6e2 100644 (file)
@@ -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<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;
index f6d795d..8861ba8 100644 (file)
@@ -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();
index 99d8746..05d5af4 100644 (file)
@@ -111,6 +111,13 @@ public:
    */
   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);