From e4bd5f42dd75205f1071705ef5f12bba384fab72 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 13 Jun 2009 22:26:35 +0100 Subject: [PATCH] Control travel on points --- include/ITrackSegment.hpp | 15 +++++-- maps/points.xml | 2 +- src/CrossoverTrack.cpp | 12 +++--- src/CurvedTrack.cpp | 2 +- src/Game.cpp | 2 +- src/Points.cpp | 89 +++++++++++++++++++++++++++++++-------- src/StraightTrack.cpp | 12 +++--- src/Train.cpp | 6 +-- 8 files changed, 100 insertions(+), 40 deletions(-) diff --git a/include/ITrackSegment.hpp b/include/ITrackSegment.hpp index f5a6b34..54e7db6 100644 --- a/include/ITrackSegment.hpp +++ b/include/ITrackSegment.hpp @@ -43,7 +43,8 @@ namespace track { // Angles for curved track typedef int Angle; - typedef std::tr1::function TransformFunc; + struct TravelToken; + typedef std::tr1::function TransformFunc; // Choices that the player may make for a track segment enum Choice { @@ -64,13 +65,19 @@ namespace track { // Default choice Choice activeChoice; - // Choices available to the player - set choices; - // A function that transforms the location of the train // so it will render in the correct place for this track segment // The functions assumes that it is initially placed at the origin TransformFunc transformer; + + // Wrapper for the above + void transform(double aDelta) const + { + transformer(*this, aDelta); + } + + // Choices available to the player + set choices; }; } diff --git a/maps/points.xml b/maps/points.xml index 548cf48..c3633eb 100644 --- a/maps/points.xml +++ b/maps/points.xml @@ -2,7 +2,7 @@ No Name - + maps/points.bin diff --git a/src/CrossoverTrack.cpp b/src/CrossoverTrack.cpp index 6a2a080..3e3fbed 100644 --- a/src/CrossoverTrack.cpp +++ b/src/CrossoverTrack.cpp @@ -52,7 +52,7 @@ public: xml::element toXml() const; private: - void transform(const track::Direction& aDirection, double aDelta) const; + void transform(const track::TravelToken& aToken, double aDelta) const; int myX, myY; }; @@ -108,25 +108,25 @@ CrossoverTrack::getTravelToken(track::Position aPosition, track::TravelToken tok = { aDirection, aPosition, - track::CHOOSE_STRAIGHT_ON + track::CHOOSE_STRAIGHT_ON, + bind(&CrossoverTrack::transform, this, _1, _2) }; - tok.transformer = bind(&CrossoverTrack::transform, this, aDirection, _1); tok.choices.insert(track::CHOOSE_STRAIGHT_ON); return tok; } -void CrossoverTrack::transform(const track::Direction& aDirection, +void CrossoverTrack::transform(const track::TravelToken& aToken, double aDelta) const { assert(aDelta < 1.0); - bool backwards = aDirection == -axis::X || aDirection == -axis::Y; + bool backwards = aToken.direction== -axis::X || aToken.direction == -axis::Y; if (backwards) { aDelta = 1.0 - aDelta; } - track::Direction dir = backwards ? -aDirection : aDirection; + track::Direction dir = backwards ? -aToken.direction : aToken.direction; const double xTrans = dir == axis::X ? aDelta : 0; const double yTrans = dir == axis::Y ? aDelta : 0; diff --git a/src/CurvedTrack.cpp b/src/CurvedTrack.cpp index 858c0ba..b9a008f 100644 --- a/src/CurvedTrack.cpp +++ b/src/CurvedTrack.cpp @@ -91,8 +91,8 @@ CurvedTrack::getTravelToken(track::Position aPosition, aDirection, aPosition, track::CHOOSE_STRAIGHT_ON, + bind(&CurvedTrack::transform, this, _1, _2) }; - tok.transformer = bind(&CurvedTrack::transform, this, tok, _1); tok.choices.insert(track::CHOOSE_STRAIGHT_ON); return tok; } diff --git a/src/Game.cpp b/src/Game.cpp index 4e24ac2..7d28dc5 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -171,7 +171,7 @@ void Game::update(IPickBufferPtr aPickBuffer, int aDelta) myWaterMeter->setValue(8); - lookAhead(); + //lookAhead(); } // Signal that we are approaching a station diff --git a/src/Points.cpp b/src/Points.cpp index b23590a..790b5e6 100644 --- a/src/Points.cpp +++ b/src/Points.cpp @@ -132,8 +132,8 @@ track::TravelToken Points::getTravelToken(track::Position aPosition, aDirection, aPosition, track::CHOOSE_STRAIGHT_ON, + bind(&Points::transform, this, _1, _2) }; - tok.transformer = bind(&Points::transform, this, tok, _1); tok.choices.insert(track::CHOOSE_STRAIGHT_ON); if (aPosition.x == myX && aPosition.y == myY) @@ -148,7 +148,8 @@ void Points::transform(const track::TravelToken& aToken, double aDelta) const assert(aDelta < len); - if (myX == aToken.position.x && myY == aToken.position.y) { + if (myX == aToken.position.x && myY == aToken.position.y + && aToken.activeChoice == track::CHOOSE_STRAIGHT_ON) { if (aToken.direction == myAxis && (myAxis == -axis::X || myAxis == -axis::Y)) @@ -193,7 +194,8 @@ void Points::transform(const track::TravelToken& aToken, double aDelta) const glTranslated(-0.5, 0.0, 0.0); } - else if (aToken.position == displacedEndpoint()) { + else if (aToken.position == displacedEndpoint() + || aToken.activeChoice != track::CHOOSE_STRAIGHT_ON) { // Curving onto the straight section float xTrans, yTrans, rotate; @@ -202,7 +204,9 @@ void Points::transform(const track::TravelToken& aToken, double aDelta) const // to compress the delta into [0,1] here const float curveDelta = aDelta / len; - const float fValue = 1.0f - curveDelta; + bool backwards = aToken.position == displacedEndpoint(); + + const float fValue = backwards ? 1.0f - curveDelta : curveDelta; const Vector curveValue = myCurve(fValue); // Calculate the angle that the tangent to the curve at this @@ -210,27 +214,43 @@ void Points::transform(const track::TravelToken& aToken, double aDelta) const const Vector deriv = myCurve.deriv(fValue); const float angle = radToDeg(atanf(deriv.y / deriv.x)); - + if (myAxis == -axis::X && aToken.direction == axis::X) { - debug() << "case 1 " << amReflected; + xTrans = 1.0f - curveValue.x; + yTrans = amReflected ? curveValue.y : -curveValue.y; + rotate = amReflected ? angle : -angle; + } + else if (myAxis == -axis::X && aToken.direction == -axis::X) { xTrans = 1.0f - curveValue.x; yTrans = amReflected ? curveValue.y : -curveValue.y; rotate = amReflected ? angle : -angle; } else if (myAxis == axis::X && aToken.direction == -axis::X) { - debug() << "case 2 " << amReflected; + xTrans = curveValue.x; + yTrans = amReflected ? -curveValue.y : curveValue.y; + rotate = amReflected ? angle : -angle; + } + else if (myAxis == axis::X && aToken.direction == axis::X) { xTrans = curveValue.x; yTrans = amReflected ? -curveValue.y : curveValue.y; rotate = amReflected ? angle : -angle; } else if (myAxis == -axis::Y && aToken.direction == axis::Y) { - debug() << "case 3 " << amReflected; + xTrans = amReflected ? -curveValue.y : curveValue.y; + yTrans = 1.0f - curveValue.x; + rotate = amReflected ? angle : -angle; + } + else if (myAxis == -axis::Y && aToken.direction == -axis::Y) { xTrans = amReflected ? -curveValue.y : curveValue.y; yTrans = 1.0f - curveValue.x; rotate = amReflected ? angle : -angle; } else if (myAxis == axis::Y && aToken.direction == -axis::Y) { - debug() << "case 4 " << amReflected; + xTrans = amReflected ? curveValue.y : -curveValue.y; + yTrans = curveValue.x; + rotate = amReflected ? angle : -angle; + } + else if (myAxis == axis::Y && aToken.direction == axis::Y) { xTrans = amReflected ? curveValue.y : -curveValue.y; yTrans = curveValue.x; rotate = amReflected ? angle : -angle; @@ -273,7 +293,12 @@ bool Points::isValidDirection(const track::Direction& aDirection) const } track::Connection Points::nextPosition(const track::TravelToken& aToken) const -{ +{ + bool branching = aToken.activeChoice != track::CHOOSE_STRAIGHT_ON; + + debug() << "axis=" << myAxis << " dir=" << aToken.direction + << " r=" << amReflected; + if (myAxis == axis::X) { if (aToken.direction == -axis::X) { // Two possible entry points @@ -281,16 +306,30 @@ track::Connection Points::nextPosition(const track::TravelToken& aToken) const } else { // Two possible exits - return make_pair(makePoint(myX + 3, myY), axis::X); + if (branching) { + if (amReflected) + return make_pair(makePoint(myX + 3, myY - 1), axis::X); + else + return make_pair(makePoint(myX + 3, myY + 1), axis::X); + } + else + return make_pair(makePoint(myX + 3, myY), axis::X); } } else if (myAxis == -axis::X) { if (aToken.direction == -axis::X) { - // Two possible entry points - return make_pair(makePoint(myX - 3, myY), -axis::X); + // Two possible exits + if (branching) { + if (amReflected) + return make_pair(makePoint(myX - 3, myY + 1), -axis::X); + else + return make_pair(makePoint(myX - 3, myY - 1), -axis::X); + } + else + return make_pair(makePoint(myX - 3, myY), -axis::X); } else { - // Two possible exits + // Two possible entry points return make_pair(makePoint(myX + 1, myY), axis::X); } } @@ -301,16 +340,30 @@ track::Connection Points::nextPosition(const track::TravelToken& aToken) const } else { // Two possible exits - return make_pair(makePoint(myX, myY + 3), axis::Y); + if (branching) { + if (amReflected) + return make_pair(makePoint(myX + 1, myY + 3), axis::Y); + else + return make_pair(makePoint(myX - 1, myY + 3), axis::Y); + } + else + return make_pair(makePoint(myX, myY + 3), axis::Y); } } else if (myAxis == -axis::Y) { if (aToken.direction == -axis::Y) { - // Two possible entry points - return make_pair(makePoint(myX, myY - 3), -axis::Y); + // Two possible exits + if (branching) { + if (amReflected) + return make_pair(makePoint(myX - 1, myY - 3), -axis::Y); + else + return make_pair(makePoint(myX + 1, myY - 3), -axis::Y); + } + else + return make_pair(makePoint(myX, myY - 3), -axis::Y); } else { - // Two possible exits + // Two possible entry points return make_pair(makePoint(myX, myY + 1), axis::Y); } } diff --git a/src/StraightTrack.cpp b/src/StraightTrack.cpp index d92161d..e451379 100644 --- a/src/StraightTrack.cpp +++ b/src/StraightTrack.cpp @@ -54,7 +54,7 @@ public: track::Direction aDirection) const; xml::element toXml() const; private: - void transform(const track::Direction& aDirection, double aDelta) const; + void transform(const track::TravelToken& aToken, double aDelta) const; void ensureValidDirection(const track::Direction& aDirection) const; int myX, myY; // Absolute position @@ -81,19 +81,19 @@ StraightTrack::getTravelToken(track::Position aPosition, track::TravelToken tok = { aDirection, aPosition, - track::CHOOSE_STRAIGHT_ON + track::CHOOSE_STRAIGHT_ON, + bind(&StraightTrack::transform, this, _1, _2) }; - tok.transformer = bind(&StraightTrack::transform, this, aDirection, _1); tok.choices.insert(track::CHOOSE_STRAIGHT_ON); return tok; } -void StraightTrack::transform(const track::Direction& aDirection, +void StraightTrack::transform(const track::TravelToken& aToken, double aDelta) const { assert(aDelta < 1.0); - if (aDirection == -myDirection) + if (aToken.direction == -myDirection) aDelta = 1.0 - aDelta; const double xTrans = myDirection == axis::X ? aDelta : 0; @@ -108,7 +108,7 @@ void StraightTrack::transform(const track::Direction& aDirection, glTranslated(-0.5, 0.0, 0.0); - if (aDirection == -myDirection) + if (aToken.direction == -myDirection) glRotated(-180.0, 0.0, 1.0, 0.0); } diff --git a/src/Train.cpp b/src/Train.cpp index 360c598..98a5d0b 100644 --- a/src/Train.cpp +++ b/src/Train.cpp @@ -153,7 +153,7 @@ void Train::updateSmokePosition(int aDelta) glPushMatrix(); glLoadIdentity(); - e.travelToken.transformer(e.segmentDelta); + e.travelToken.transform(e.segmentDelta); const float smokeOffX = 0.63f; const float smokeOffY = 1.04f; @@ -230,7 +230,7 @@ void Train::render() const it != myParts.end(); ++it) { glPushMatrix(); - (*it).travelToken.transformer((*it).segmentDelta); + (*it).travelToken.transform((*it).segmentDelta); glTranslatef(0.0f, track::RAIL_HEIGHT, 0.0f); (*it).vehicle->render(); @@ -262,7 +262,7 @@ Vector Train::partPosition(const Part& aPart) const glPushMatrix(); glLoadIdentity(); - aPart.travelToken.transformer(aPart.segmentDelta); + aPart.travelToken.transform(aPart.segmentDelta); float matrix[16]; glGetFloatv(GL_MODELVIEW_MATRIX, matrix); -- 2.39.2