added Distance function, quick fix for Neighbors function

This commit is contained in:
Maximilian Fajnberg 2022-01-13 16:54:37 +01:00
parent 219b99c8d7
commit 33de4f2fa6
4 changed files with 42 additions and 13 deletions

View File

@ -99,22 +99,40 @@ TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex)
int32 Index;
Index = GridIndex(OfHex->Q + 1 , OfHex->R + 0 );
if (Index < Grid.Num() && Index >= 0) { Neighbors.Add(Grid[Index]); }
if ( Index >= 0&& Index < Grid.Num() )
{
if (OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
}
Index = GridIndex(OfHex->Q + 1 , OfHex->R - 1 );
if (Index < Grid.Num() && Index >= 0) { Neighbors.Add(Grid[Index]); }
if (Index >= 0 && Index < Grid.Num())
{
if (OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
}
Index = GridIndex(OfHex->Q + 0 , OfHex->R - 1 );
if (Index < Grid.Num() && Index >= 0) { Neighbors.Add(Grid[Index]); }
if (Index >= 0 && Index < Grid.Num())
{
if (OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
}
Index = GridIndex(OfHex->Q - 1 , OfHex->R + 0 );
if (Index < Grid.Num() && Index >= 0) { Neighbors.Add(Grid[Index]); }
if (Index >= 0 && Index < Grid.Num())
{
if (OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
}
Index = GridIndex(OfHex->Q - 1 , OfHex->R + 1 );
if (Index < Grid.Num() && Index >= 0) { Neighbors.Add(Grid[Index]); }
if (Index >= 0 && Index < Grid.Num())
{
if (OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
}
Index = GridIndex(OfHex->Q + 0 , OfHex->R + 1 );
if (Index < Grid.Num() && Index >= 0) { Neighbors.Add(Grid[Index]); }
return Neighbors; // currently wrapping from corner to corner --> rectify this with a manhattan distance check
if (Index >= 0 && Index < Grid.Num())
{
if (OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
}
return Neighbors;
}

View File

@ -34,12 +34,12 @@ public:
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "Generation")
TArray<AHexTile*> Grid;
UFUNCTION(BlueprintCallable)
UFUNCTION(BlueprintCallable, Category = "Runtime")
int32 GridIndex(int32 q, int32 r);
UFUNCTION(BlueprintCallable)
UFUNCTION(BlueprintCallable, Category = "Runtime")
AHexTile* RandomHex();
UFUNCTION(BlueprintCallable)
TArray<AHexTile*> Neighbors(AHexTile* OfHexTile);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> Neighbors(AHexTile* OfHex);
// Player spawn section
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)

View File

@ -39,3 +39,11 @@ void AHexTile::FillCornersArray()
Corners.Emplace(Corner(i + 1));
}
}
int32 AHexTile::Distance(AHexTile* ToHex)
{
int32 CubeS1 = -this->Q - this->R;
int32 CubeS2 = -ToHex->Q - ToHex->R;
return (abs(this->Q - ToHex->Q) + abs(this->R - ToHex->R) + abs(CubeS1 - CubeS2)) / 2;
}

View File

@ -23,10 +23,11 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
float TileSize;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
USceneComponent* SceneComponent;
UFUNCTION(BlueprintCallable)
UFUNCTION(BlueprintCallable, Category = "debug")
FVector Corner(int32 i);
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "debug")
TArray<FVector> Corners;
@ -39,9 +40,11 @@ public:
int32 R;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Runtime")
int32 Index;
UPROPERTY(BlueprintReadWrite, Category = "Runtime")
AAdventureMap* MapRef;
UFUNCTION(BlueprintCallable, Category = "Runtime")
int32 Distance(AHexTile* ToHex);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;