storing 12 cardinal dir vectors; Diagonals function

This commit is contained in:
Maximilian Fajnberg 2022-01-26 18:09:39 +01:00
parent b7b9963a68
commit 70ef60f08e
2 changed files with 59 additions and 18 deletions

View File

@ -10,6 +10,18 @@
// Sets default values
AAdventureMap::AAdventureMap()
{
NBVectors.Add(NNE);
NBVectors.Add(E);
NBVectors.Add(SSE);
NBVectors.Add(SSW);
NBVectors.Add(W);
NBVectors.Add(NNW);
NBVectorsDiag.Add(N);
NBVectorsDiag.Add(ENE);
NBVectorsDiag.Add(ESE);
NBVectorsDiag.Add(S);
NBVectorsDiag.Add(WSW);
NBVectorsDiag.Add(WNW);
}
// Called when the game starts or when spawned
@ -17,8 +29,8 @@ void AAdventureMap::BeginPlay()
{
Super::BeginPlay();
World = GetWorld();
if (IsValid(BaseTileClass))
{
if (IsValid(BaseTileClass)) {
MakeGrid();
}
}
@ -79,23 +91,46 @@ AHexTile* AAdventureMap::RandomHex()
return Grid[RandHex];
}
/*
* Add two TArray<TPair<int32, int32>> members containing the Cardinal Directions
(one for immediate neighbors, one for diagonals)
{ fill them in AAdventureMap::AAdventureMap }
* This function instead returns
TMap<bool bDiag, AHexTile* Neighbor>
*/
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex)
{
TArray<AHexTile*> Neighbors;
TArray<AHexTile*> Results;
TArray<int32> Indeces;
Indeces.Add(GridIndex(OfHex->Q + NE.Key, OfHex->R + NE.Value));
Indeces.Add(GridIndex(OfHex->Q + E.Key, OfHex->R + E.Value));
Indeces.Add(GridIndex(OfHex->Q + SE.Key, OfHex->R + SE.Value));
Indeces.Add(GridIndex(OfHex->Q + SW.Key, OfHex->R + SW.Value));
Indeces.Add(GridIndex(OfHex->Q + W.Key, OfHex->R + W.Value));
Indeces.Add(GridIndex(OfHex->Q + NW.Key, OfHex->R + NW.Value));
for (auto& I : Indeces) {
if (Grid.IsValidIndex(I)) { if (OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); } }
for (auto& Vec : NBVectors) {
Indeces.Add(GridIndex(OfHex->Q + Vec.Key, OfHex->R + Vec.Value));
}
for (auto& Ind : Indeces) {
if (Grid.IsValidIndex(Ind)) {
if (OfHex->Distance(Grid[Ind]) == 1) {
Results.Add(Grid[Ind]);
}
}
}
return Results;
}
return Neighbors;
TArray<AHexTile*> AAdventureMap::Diagonals(AHexTile* OfHex)
{
TArray<AHexTile*> Results;
TArray<int32> Indeces;
for (auto& Vec : NBVectorsDiag) {
Indeces.Add(GridIndex(OfHex->Q + Vec.Key, OfHex->R + Vec.Value));
}
for (auto& Ind : Indeces) {
if (Grid.IsValidIndex(Ind)) {
if (OfHex->Distance(Grid[Ind]) == 2) {
Results.Add(Grid[Ind]);
}
}
}
return Results;
}
TArray<AHexTile*> AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radius)

View File

@ -4,6 +4,7 @@
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Containers/Map.h"
#include "AdventureMap.generated.h"
class AHexTile;
@ -49,6 +50,8 @@ public:
AHexTile* RandomHex();
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> Neighbors(AHexTile* OfHex);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> Diagonals(AHexTile* OfHex);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> BreadthFirstSearch(AHexTile* Start, int32 Radius);
UFUNCTION(BlueprintCallable, Category = "Runtime")
@ -58,17 +61,20 @@ public:
// Cardinal direction vectors
TPair<int32, int32> N = TPair<int32,int32>(1, -2); //diag
TPair<int32, int32> NE = TPair<int32,int32>(1, -1);
TPair<int32, int32> NNE = TPair<int32,int32>(1, -1);
TPair<int32, int32> ENE = TPair<int32,int32>(2, -1); //diag
TPair<int32, int32> E = TPair<int32,int32>(1, 0);
TPair<int32, int32> ESE = TPair<int32,int32>(1, 1); //diag
TPair<int32, int32> SE = TPair<int32,int32>(0, 1);
TPair<int32, int32> SSE = TPair<int32,int32>(0, 1);
TPair<int32, int32> S = TPair<int32,int32>(-1, 2); //diag
TPair<int32, int32> SW = TPair<int32,int32>(-1, 1);
TPair<int32, int32> SSW = TPair<int32,int32>(-1, 1);
TPair<int32, int32> WSW = TPair<int32,int32>(-2, 1); //diag
TPair<int32, int32> W = TPair<int32,int32>(-1, 0);
TPair<int32, int32> WNW = TPair<int32,int32>(-1, -1); //diag
TPair<int32, int32> NW = TPair<int32,int32>(0, -1);
TPair<int32, int32> NNW = TPair<int32,int32>(0, -1);
TArray<TPair<int32, int32>> NBVectors;
TArray<TPair<int32, int32>> NBVectorsDiag;
protected:
// Called when the game starts or when spawned