我想用 SFML 创建 Catan 游戏的棋盘,我只需要 19 个形状(六边形),每个形状我都可以利用所有 6 个角和 6 个边来建造城市或道路。 对于形状我做这个:
std::vector<sf::CircleShape> shape(19);
int n = 0;
int shape_y = 100;
for (size_t index = 0; index < shape.size(); index++) {
if (index < 3) {
sf::CircleShape sh(80, 6);
sh.setPosition(200 + n, shape_y);
sh.setFillColor(sf::Color::Magenta);
shape[index] = sh;
n += 140;
}
if (index == 3)
n = 0;
if (index < 7 && index >= 3) {
sf::CircleShape sh(80, 6);
sh.setPosition(130 + n, shape_y + 120);
sh.setFillColor(sf::Color::Blue);
shape[index] = sh;
n += 140;
}
if (index == 7)
n = 0;
if (index >= 7 && index < 12) {
sf::CircleShape sh(80, 6);
sh.setPosition(60 + n, shape_y + 240);
sh.setFillColor(sf::Color::Red);
shape[index] = sh;
n += 140;
}
if (index == 12)
n = 0;
if (index >= 12 && index < 16) {
sf::CircleShape sh(80, 6);
sh.setPosition(130 + n, shape_y + 360);
sh.setFillColor(sf::Color::Green);
shape[index] = sh;
n += 140;
}
if (index == 16)
n = 0;
if (index >= 16 && index < 19) {
sf::CircleShape sh(80, 6);
sh.setPosition(200 + n, shape_y + 480);
sh.setFillColor(sf::Color::Yellow);
shape[index] = sh;
n += 140;
}
}
看起来像这样:
但是我如何从形状中得到角和边呢?如果我使用 getPoint(0) 作为角点,它不会绘制其所属的点。 如果这不是一个好主意,我可以用什么来解决这个问题?
我很久以前就实现了该功能,这是实现该功能的简单方法。
我的方法是将每个六边形表示为一个圆圈。绘制的六边形嵌入到该圆圈中。为了检查鼠标是否位于角落或一侧,我做了一个简单的检查:
如果该点同时在 3 个圆内,则它是一个角(这 3 个六边形的交角)
如果该点在 2 个圆内,则它是一条边。
如果该点在 1 个圆内,则它是一个完整的六边形
概念证明:
蓝色六边形符合正确的棋盘,每个都有一个红色圆圈(大于六边形本身)。
绿色六边形位于棋盘之外(它们不是游戏棋盘的一部分),它们有助于了解鼠标是否位于外六边形的边或角上。
完整代码位于我的 Github 存储库,但相当旧,可能已经过时了