From 665ae39b30463dbafcfaae6db681724ea990f805 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 7 Jun 2009 12:38:38 +0100 Subject: [PATCH] Detect when we've arrived at a station --- include/IMap.hpp | 3 +++ include/ITrain.hpp | 4 ++++ src/Game.cpp | 25 +++++++++++++++++++++++++ src/Map.cpp | 6 ++++++ src/Train.cpp | 6 ++++++ 5 files changed, 44 insertions(+) diff --git a/include/IMap.hpp b/include/IMap.hpp index 53ca232..888e066 100644 --- a/include/IMap.hpp +++ b/include/IMap.hpp @@ -39,6 +39,9 @@ public: // first virtual ITrackSegmentPtr trackAt(const Point& aPoint) const = 0; + // Return the station at this track location or a null pointer + virtual IStationPtr stationAt(Point aPoint) const = 0; + // True if the given position is the origin of a track segment virtual bool isValidTrack(const Point& aPoint) const = 0; diff --git a/include/ITrain.hpp b/include/ITrain.hpp index 9ae43ca..8586ea0 100644 --- a/include/ITrain.hpp +++ b/include/ITrain.hpp @@ -21,6 +21,7 @@ #include "Platform.hpp" #include "IRollingStock.hpp" #include "IMap.hpp" +#include "ITrackSegment.hpp" // Interface to managing complete trains struct ITrain { @@ -33,6 +34,9 @@ struct ITrain { // the train virtual Vector front() const = 0; + // Return the track segment occupied by the front of the train + virtual ITrackSegmentPtr trackSegment() const = 0; + // Return the speed of the train virtual double speed() const = 0; diff --git a/src/Game.cpp b/src/Game.cpp index dfd543a..11c7ea7 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -49,6 +49,8 @@ public: void onMouseRelease(IPickBufferPtr aPickBuffer, int x, int y, MouseButton aButton) {} private: + void lookAhead(); + IMapPtr myMap; ITrainPtr myTrain; ILightPtr mySun; @@ -153,6 +155,29 @@ void Game::update(IPickBufferPtr aPickBuffer, int aDelta) myTempLabel->setText("Temp: %.lfdeg", temp); myWaterMeter->setValue(8); + + lookAhead(); +} + +// Look along the track and notify the player of any stations, points, etc. +// that they are approaching +void Game::lookAhead() +{ + ITrackSegmentPtr seg = myTrain->trackSegment(); + + // Are we sitting on a station + typedef list > PointList; + PointList endpoints; + seg->getEndpoints(endpoints); + + IStationPtr station; + for (PointList::const_iterator it = endpoints.begin(); + it != endpoints.end(); ++it) + if ((station = myMap->stationAt(makePoint((*it).x, (*it).y)))) + break; + + if (station) + log() << "Stop here for station " << station->name() << "!"; } void Game::onKeyDown(SDLKey aKey) diff --git a/src/Map.cpp b/src/Map.cpp index 57f3972..d6c0a77 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -81,6 +81,7 @@ public: track::Connection startLocation() const; ITrackSegmentPtr trackAt(const Point& aPoint) const; + IStationPtr stationAt(Point aPoint) const; void setTrackAt(const Point& aPoint, ITrackSegmentPtr aTrack); bool isValidTrack(const Point& aPoint) const; void render(IGraphicsPtr aContext) const; @@ -219,6 +220,11 @@ ITrackSegmentPtr Map::trackAt(const Point& aPoint) const } } +IStationPtr Map::stationAt(Point aPoint) const +{ + return tileAt(aPoint.x, aPoint.y).station; +} + void Map::eraseTile(int x, int y) { Tile& tile = tileAt(x, y); diff --git a/src/Train.cpp b/src/Train.cpp index 1845a4e..9ceff9c 100644 --- a/src/Train.cpp +++ b/src/Train.cpp @@ -38,6 +38,7 @@ public: void update(int aDelta); Vector front() const; + ITrackSegmentPtr trackSegment() const; double speed() const { return myParts.front().vehicle->speed(); } IControllerPtr controller() { return myParts.front().vehicle->controller(); } @@ -230,6 +231,11 @@ void Train::render() const mySmokeTrail->render(); } +ITrackSegmentPtr Train::trackSegment() const +{ + return engine().segment; +} + Vector Train::front() const { return partPosition(engine()); -- 2.39.2