Add distance_from(Stop)
authorIra W. Snyder <devel@irasnyder.com>
Sat, 6 Oct 2007 18:22:43 +0000 (11:22 -0700)
committerIra W. Snyder <devel@irasnyder.com>
Sat, 6 Oct 2007 18:22:43 +0000 (11:22 -0700)
It would be nice to get the distance from a Stop, not only a position. This
way, direction can also be taken into account.

Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
elevator.cpp
elevator.hpp

index e50ee2e..d4e7cfb 100644 (file)
@@ -92,6 +92,23 @@ float Elevator::distance_from (Position &pos) const
        return pos - position_;
 }
 
+float Elevator::distance_from (Stop &s) const
+{
+       Direction d = s.getDirection();
+       Position  p = s.getPosition ();
+
+       /* If direction doesn't matter, then only position does */
+       if (d == ALL || direction_ == IDLE)
+               return distance_from (p);
+
+       /* If we're not in the same direction, then we're "really far" away */
+       if (d != direction_)
+               return INT_MAX;
+
+       /* We must be in the correct direction, so pure distance is fine */
+       return distance_from (p);
+}
+
 void Elevator::transition_move_up ()
 {
        direction_ = UP;
@@ -145,7 +162,7 @@ void Elevator::transition_open_door ()
        else
                stops_.remove (Stop(position_, direction_));
 
-       // TODO: Call into GUI to open the door
+       // TODO: Call into the GUI to open the door
        std::cout << "Opening Door" << std::endl;
 }
 
index df8a228..de8d71b 100644 (file)
@@ -26,6 +26,7 @@ class Elevator
 
                void stop_at (Stop &stop);
                float distance_from (Position& pos) const;
+               float distance_from (Stop& s) const;
                void move ();
                bool is_idle () const;