// Interface for the road chunk class - base for Intersection and RoadSegment - // and for the class RoadChunkList: a linked list of RoadChunks. // CSE143 Spring '01 // Homework 6 #ifndef _ROAD_SEGMENT_H #define _ROAD_SEGMENT_H #include "road_chunk.h" #include "position.h" // Forward declarations to avoid circular #includes. class Vehicle; class Intersection; const RoadWidth = 20; // RoadSegment models a segment of road between two intersections. // Each segment has an entry end and an exit end, which must be connected // to intersections. Vehicles enter at the entry end of the road and form a // queue. The head of the queue can only advance (be dequeued) if the // exit intersection object allows. // This is a subclass of SimObject, so it supports basic simulation operations. class RoadSegment : public RoadChunk { public: // Construct an uninitialized road segment RoadSegment(); // Destructor - clean up the vehicles in the segment. virtual ~RoadSegment(); // Draw the segment, INCLUDING calling draw() for all the vehicles in it. // Precondition: the segment is set up properly. virtual void draw(GP142Display& gp); // Advance cars that are currently in the virtual void tick(); // Set the given intersection as the entry or exit of this segment // Precondition: no intersection for that side has been set yet. virtual void setIntersection(Intersection *isec, int entryOrExit); // Set the entry or exit corner position for this road segment. virtual void setCorner(Position pos, int entryOrExit); // Add a vehicle at the entry to the segment. // Precondition: vehicle != NULL, and canEnqueue(vehicle). virtual void enqueue(Vehicle *vehicle); // Return true iff a vehicle can be enqueued at this time. // The result might be invalidated if non-const operations are done. virtual bool canEnqueue() const; // Remove the first vehicle from the head of the queue. // Returns a pointer to the dequeued vehicle, NULL if none. virtual Vehicle *dequeue(); // Return true if there's a vehicle waiting to enter the intersection virtual bool vehicleWaiting() const; private: // Compute the orientation given the corners. // Precondition: both corners were set. void findOrientation(); // Return the center of the road at its entry point. // This is the first position for newly enqueued vehicles. Position entryPosition() const; // Returns true if the specified vehicle is waiting at the intersection. // Precondition: v != NULL. bool isVehicleWaiting(Vehicle *v) const; // Private copy constructor: prevents copying road segments. RoadSegment(const RoadSegment& other) {} // Private assinment operator: prevents assignment of road segments. RoadSegment& operator =(const RoadSegment& other) {} Intersection *isec[2]; // Pointers to Entry and Exit intersections Position corner[2]; // corner[Entry] is the bottom-right corner in a // north-bound segment, or the corresponding corner // if the segment is bound otherwise (e.g. bottom- // left if east-bound. corner[Exit] is the opposite // corner. bool cornerSet[2]; // true iff the corresponding corner was set. Direction orientation; // Driving direction on segment. // STARTER IMPLEMENTATION: the segment can hold only one car at a time. Vehicle *vehicle; // Vehicle currently in the segment, or NULL // if no vehicle. }; #endif