XNA六边形地图平铺

本文关键字:地图 六边形 XNA | 更新日期: 2023-09-27 17:57:47

好了,伙计们,我在这方面遇到了很多麻烦。我根本无法找到在XNA中的六边形映射中实现瓦片拾取的方法。在问这个问题之前,我已经查过了,所有的答案都涉及复杂的算法和图表,我的小脑袋根本无法理解。所以我想问你们的问题是:如果我想的话,我如何才能悬停在瓷砖上并选择它们?

如果你需要任何关于我的程序到目前为止的样子的参考,只需查看这个链接,它实际上是一样的,只是我有一个较小的地图。

http://www.xnaresources.com/default.asp?page=Tutorial:TileEngineSeries:3

谢谢!

XNA六边形地图平铺

这是我存储但从未使用过的代码,它适用于一条边正在查找的十六进制网格,因此通过一些小的调整,它可以在您的示例中工作。这不是我的代码,不确定是谁写的。

Hexagon[][] hexagons = new Hexagon[100][100];
double hexagonHeight = 30;
double hexagonWidth = 40;
double halfWidth = hexagonWidth / 2;

// Find rough coordinates of Hexagon at mousepoint
private Hexagon getSelectedHexagon(MouseEvent mouse)
    {
        // These will represent which box the mouse is in, not which hexagon!
        int row = (int) (mouse.y / hexagonHeight);
        int column;
        boolean rowIsOdd = row % 2 != 0;
        // Is the row an even number?
        if (rowIsOdd) // No: Calculate normally
            column = (int) (mouse.x / hexagonWidth);
        else // Yes: Offset mouse.x to match the offset of the row
            column = (int) ((mouse.x + halfWidth) / hexagonWidth);
        // column is more complex because it has to
        // take into account that every other row
        // is offset by half the width of a hexagon
        return hexagons[row][column];
    }

编辑:我刚找到作者六边形网格,你如何找到一个点在哪个六边形中?