From 759268c7d1da2e60766459157b3015492786db77 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 28 Feb 2010 17:03:20 +0000 Subject: [PATCH] Calculate slope before and after sloped section --- src/Editor.cpp | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/src/Editor.cpp b/src/Editor.cpp index 76056d4..a12bba8 100644 --- a/src/Editor.cpp +++ b/src/Editor.cpp @@ -63,7 +63,7 @@ public: private: void buildGUI(); void drawDraggedTrack(); - bool drawTrackTile(const Point& aPoint, const track::Direction& anAxis); + bool drawTrackTile(Point where, track::Direction axis); void drawDraggedStraight(const track::Direction& anAxis, int aLength); void drawDraggedCurve(int xLength, int yLength); bool canConnect(const Point& aFirstPoint, @@ -219,12 +219,12 @@ bool Editor::canConnect(const Point& aFirstPoint, // Draw a single tile of straight track and check for collisions // Returns `false' if track cannot be placed here -bool Editor::drawTrackTile(const Point& aPoint, const track::Direction& anAxis) +bool Editor::drawTrackTile(Point where, track::Direction axis) { - if (map->isValidTrack(aPoint)) { - ITrackSegmentPtr merged = map->trackAt(aPoint)->mergeExit(aPoint, anAxis); + if (map->isValidTrack(where)) { + ITrackSegmentPtr merged = map->trackAt(where)->mergeExit(where, axis); if (merged) { - map->setTrackAt(aPoint, merged); + map->setTrackAt(where, merged); return true; } else { @@ -234,18 +234,47 @@ bool Editor::drawTrackTile(const Point& aPoint, const track::Direction& anA } else { bool level; - Vector slope = map->slopeAt(aPoint, anAxis, level); + Vector slope = map->slopeAt(where, axis, level); if (level) { const bool flat = abs(slope.y) < 0.001f; if (flat) { - map->setTrackAt(aPoint, makeStraightTrack(anAxis)); + map->setTrackAt(where, makeStraightTrack(axis)); return true; } else { - warn() << "Slopes not supported yet"; - return false; + Point before, after; + + if (axis == axis::X) { + before = where + makePoint(-1, 0); + after = where + makePoint(1, 0); + } + else { + before = where + makePoint(0, -1); + after = where + makePoint(0, 1); + } + + const bool offEdge = + (axis == axis::X + && (before.x < 0 || after.x >= map->width())) + || (axis == axis::Y + && (before.y < 0 || after.y >= map->depth())); + + if (offEdge) { + warn() << "Cannot place track here"; + return false; + } + else { + bool ignored; + Vector slopeBefore = map->slopeAt(before, axis, ignored); + Vector slopeAfter = map->slopeAt(after, axis, ignored); + + debug() << "before=" << slopeBefore << " after=" << slopeAfter; + + warn() << "Slopes not supported yet"; + return false; + } } } else { -- 2.39.2