Skip to content
Permalink
Browse files

Don't select units that aren't inside the bandbox

  • Loading branch information...
MHeasell committed Jun 13, 2019
1 parent ada8b48 commit 10eb1b5ff06887b0af5693a2db5c1de668628ba0
Showing with 55 additions and 2 deletions.
  1. +9 −0 src/rwe/DiscreteRect.cpp
  2. +4 −0 src/rwe/DiscreteRect.h
  3. +17 −2 src/rwe/GameScene.cpp
  4. +25 −0 test/rwe/DiscreteRect_test.cpp
@@ -170,4 +170,13 @@ namespace rwe

return DiscreteRect(left, top, static_cast<unsigned int>(intersectWidth), static_cast<unsigned int>(intersectHeight));
}

DiscreteRect DiscreteRect::translate(int dx, int dy) const{
return DiscreteRect(x + dx, y + dy, width, height);
}

bool DiscreteRect::contains(const Point& p) const
{
return p.x >= x && p.y >= y && p.x < (x + static_cast<int>(width)) && p.y < (y + static_cast<int>(height));
}
}
@@ -62,6 +62,10 @@ namespace rwe

DiscreteRect expand(unsigned int dx, unsigned int dy) const;

DiscreteRect translate(int dx, int dy) const;

bool contains(const Point& p) const;

/**
* Returns the intersection of this rectangle and the given rectangle.
* If the two rectangles do not intersect, returns None.
@@ -2196,13 +2196,28 @@ namespace rwe

void GameScene::selectUnitsInBandbox(const DiscreteRect& box)
{
const auto& camera = worldRenderService.getCamera();
const auto cameraPos = camera.getPosition();
auto cameraBox = box.translate(-cameraPos.x, -cameraPos.z);
const auto& matrix = camera.getViewProjectionMatrix();
std::vector<UnitId> units;

for (const auto& e : simulation.units)
{
if (e.second.isSelectableBy(localPlayerId))
if (!e.second.isSelectableBy(localPlayerId))
{
units.emplace_back(e.first);
continue;
}

const auto& worldPos = e.second.position;
auto clipPos = matrix * worldPos;
Point viewportPos = worldViewport.toViewportSpace(clipPos.x, clipPos.y);
if (!cameraBox.contains(viewportPos))
{
continue;
}

units.emplace_back(e.first);
}

replaceUnitSelection(units);
@@ -217,5 +217,30 @@ namespace rwe
REQUIRE(actual == DiscreteRect(1, 2, 5, 3));
}
}

SECTION(".contains")
{
SECTION("returns true when point is in the rectangle")
{
REQUIRE(!DiscreteRect(2, 3, 4, 5).contains(Point(1, 3)));
REQUIRE(DiscreteRect(2, 3, 4, 5).contains(Point(2, 3)));
REQUIRE(DiscreteRect(2, 3, 4, 5).contains(Point(5, 3)));
REQUIRE(!DiscreteRect(2, 3, 4, 5).contains(Point(6, 3)));

REQUIRE(!DiscreteRect(2, 3, 4, 5).contains(Point(2, 2)));
REQUIRE(DiscreteRect(2, 3, 4, 5).contains(Point(2, 3)));
REQUIRE(DiscreteRect(2, 3, 4, 5).contains(Point(2, 7)));
REQUIRE(!DiscreteRect(2, 3, 4, 5).contains(Point(2, 8)));
}
}

SECTION(".translate")
{
SECTION("translates the rectangle")
{
REQUIRE(DiscreteRect(2, 3, 4, 5).translate(3, 6) == DiscreteRect(5, 9, 4, 5));
REQUIRE(DiscreteRect(2, 3, 4, 5).translate(-1, -3) == DiscreteRect(1, 0, 4, 5));
}
}
}
}

0 comments on commit 10eb1b5

Please sign in to comment.
You can’t perform that action at this time.