Hello everyone
I have tried for 8 hours now to get this working, with and without the help of AI and whatever I do, it doesn’t work although it should be easy af. Possibly have been sitting in front of it for too long =.=
I have a hexgrid where north and south are the easy directions (no change in x-coord) and left/right have 2 possibilites each.
The method I want and couldn’t get to work is one that gives me all the HexTiles in a line starting from srcTile via neighborTile. It’s only possible to have one of the 6 neighbors as second argument.
public static class Direction {
public static List<Vector3Int> directionsOffsetOdd = new List<Vector3Int>() {
new Vector3Int(0,0,1), // N
new Vector3Int(0,0,-1), // S
new Vector3Int(-1,0,0), // W1
new Vector3Int(-1,0,-1), // W2
new Vector3Int(1,0,0), // E1
new Vector3Int(1,0,-1), // E2
};
/** muss noch herausfinden, wie ich das einbinden/ändern muss */
public static List<Vector3Int> directionsOffsetEven = new List<Vector3Int>() {
new Vector3Int(0,0,1), // N
new Vector3Int(0,0,-1), // S
new Vector3Int(-1,0,0), // W1
new Vector3Int(-1,0,1), // W2
new Vector3Int(1,0,0), // E1
new Vector3Int(1,0,1), // E2
};
public static List<Vector3Int> getDirectionList(int x) {
return x % 2 == 0 ? directionsOffsetEven : directionsOffsetOdd;
}
}
A small visualisation of how the field looks
Expected results would be
1,3–>2,2 (lower right) should return (2,2),(3,2),(4,1),(5,1) (blue frames)
1,2–>2,1 (lower right) should return (2,1),(3,1),(4,0)
0,2–>1,2 (lower right) should return (1,2),(2,1),(3,1),(4,0)
2,0–>3,1 (upper right) should return (3,1),(4,1),(5,2),(6,2) (orange frames)
public List<HexTile> getTilesInDirection(HexTile sourceTile, HexTile neighborTile, int range) {
List<HexTile> tilesInDirection = new List<HexTile>();
(int deltaX, int deltaZ) = getDelta(sourceTile, neighborTile);
List<Vector3Int> directionOffsets = Direction.getDirectionList(sourceTile.getHexCoords().x); // oder sourceTile.r, je nach deinem Koordinatensystem
int directionIndex = -1;
for (int i = 0; i < directionOffsets.Count; i++) {
if (directionOffsets[i].x == deltaX && directionOffsets[i].z == deltaZ) {
directionIndex = i;
break;
}
}
if (directionIndex == -1) return tilesInDirection; // invalid dir
tilesInDirection.Add(neighborTile);
HexTile currentTile = neighborTile;
for (int i = 1; i < range; i++) {
directionOffsets = Direction.getDirectionList(currentTile.getHexCoords().x);
// Suche die Richtung in der Direction-Liste
directionIndex = -1;
for (int fff = 0; fff < directionOffsets.Count; fff++) {
if (directionOffsets[fff].x == deltaX && directionOffsets[fff].z == deltaZ) {
directionIndex = fff;
break;
}
}
if (directionIndex == -1) return tilesInDirection;
int nextQ = currentTile.getHexCoords().x + directionOffsets[directionIndex].x;
int nextR = currentTile.getHexCoords().z + directionOffsets[directionIndex].z;
HexTile nextTile = GetTileAt(nextQ, nextR);
if (nextTile != null) tilesInDirection.Add(nextTile);
}
return tilesInDirection;
}
private (int deltaX, int deltaZ) getDelta(HexTile sourceTile, HexTile neighborTile) {
int deltaX = neighborTile.getHexCoords().x - sourceTile.getHexCoords().x;
int deltaZ = neighborTile.getHexCoords().z - sourceTile.getHexCoords().z;
return (deltaX, deltaZ);
}
This method is an abomination right now and was mostly written by the AI. I tried to insert a correcting factor and somehow the mistake must be connected with the alternating directionOffsets. Take note of the expected results and how z is diminished only every second time.
Currently, from 1/3 to 2/2, I get 2/2 and 3/1.
I’m sure there’s a few people who already wrote this kind of method, just take care about the orientation (north/south = easy) and coordinate-system. Currently, I’m giving up on this.
Maybe an even more helpful function would get some kind of a “shape” that could also be rotated according to src and neighborTile. Currently, I only started with a straight line, but different shapes are actually already in the back of my head. Did someone already make something like this?
Hope you can help, thank you