From 70ef60f08e6a4fc612085b6fc1a7fdb75e682d96 Mon Sep 17 00:00:00 2001 From: Maximilian Fajnberg Date: Wed, 26 Jan 2022 18:09:39 +0100 Subject: [PATCH] storing 12 cardinal dir vectors; Diagonals function --- AdventureMap.cpp | 63 +++++++++++++++++++++++++++++++++++++----------- AdventureMap.h | 14 ++++++++--- 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/AdventureMap.cpp b/AdventureMap.cpp index 8bed685..640b511 100644 --- a/AdventureMap.cpp +++ b/AdventureMap.cpp @@ -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> members containing the Cardinal Directions + (one for immediate neighbors, one for diagonals) + { fill them in AAdventureMap::AAdventureMap } + +* This function instead returns + TMap +*/ TArray AAdventureMap::Neighbors(AHexTile* OfHex) { - TArray Neighbors; + TArray Results; TArray 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 AAdventureMap::Diagonals(AHexTile* OfHex) +{ + TArray Results; + TArray 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 AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radius) diff --git a/AdventureMap.h b/AdventureMap.h index 0f5503f..2d141c4 100644 --- a/AdventureMap.h +++ b/AdventureMap.h @@ -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 Neighbors(AHexTile* OfHex); + UFUNCTION(BlueprintCallable, Category = "Runtime") + TArray Diagonals(AHexTile* OfHex); UFUNCTION(BlueprintCallable, Category = "Runtime") TArray BreadthFirstSearch(AHexTile* Start, int32 Radius); UFUNCTION(BlueprintCallable, Category = "Runtime") @@ -58,17 +61,20 @@ public: // Cardinal direction vectors TPair N = TPair(1, -2); //diag - TPair NE = TPair(1, -1); + TPair NNE = TPair(1, -1); TPair ENE = TPair(2, -1); //diag TPair E = TPair(1, 0); TPair ESE = TPair(1, 1); //diag - TPair SE = TPair(0, 1); + TPair SSE = TPair(0, 1); TPair S = TPair(-1, 2); //diag - TPair SW = TPair(-1, 1); + TPair SSW = TPair(-1, 1); TPair WSW = TPair(-2, 1); //diag TPair W = TPair(-1, 0); TPair WNW = TPair(-1, -1); //diag - TPair NW = TPair(0, -1); + TPair NNW = TPair(0, -1); + + TArray> NBVectors; + TArray> NBVectorsDiag; protected: // Called when the game starts or when spawned