plan to implement pathfinding; minor fixes & refactorings

This commit is contained in:
Maximilian Fajnberg 2022-01-12 18:22:16 +01:00
parent 9021763ab7
commit b5c9c661fe
6 changed files with 56 additions and 28 deletions

View File

@ -2,6 +2,8 @@
#include "AdventureCharacter.h" #include "AdventureCharacter.h"
#include "AdventureMap.h"
#include "HexTile.h"
// Sets default values // Sets default values
AAdventureCharacter::AAdventureCharacter() AAdventureCharacter::AAdventureCharacter()
@ -32,3 +34,10 @@ void AAdventureCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInput
} }
void AAdventureCharacter::AStarFindPath(AHexTile*)
{
std::priority_queue<int32> Frontier;
TMap<AHexTile*, AHexTile*> CameFrom;
TMap<AHexTile*, int32> CostSoFar;
}

View File

@ -4,8 +4,13 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "GameFramework/Character.h" #include "GameFramework/Character.h"
#include <queue>
#include "AdventureCharacter.generated.h" #include "AdventureCharacter.generated.h"
class AHexTile;
UCLASS() UCLASS()
class FRAY_API AAdventureCharacter : public ACharacter class FRAY_API AAdventureCharacter : public ACharacter
{ {
@ -15,6 +20,15 @@ public:
// Sets default values for this character's properties // Sets default values for this character's properties
AAdventureCharacter(); AAdventureCharacter();
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "Runtime")
AHexTile* GridLocation;
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "Runtime")
AHexTile* SelectedHex;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime")
TArray<AHexTile*> MovementPath;
UFUNCTION(BlueprintCallable)
void AStarFindPath(AHexTile* Goal);
protected: protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned
virtual void BeginPlay() override; virtual void BeginPlay() override;

View File

@ -24,17 +24,6 @@ void AAdventureMap::BeginPlay()
} }
} }
// Every Hex Tile's index within the Grid Array can be derived from its Q and R coordinates
int32 AAdventureMap::GridIndex(int32 qAxial, int32 rAxial)
{
/*
* The Q axis is (i.e. columns are) oriented diagonally.
* The Hex Grid has a rough square shape, hence the Q coordinates must be offset by -1 every other row.
*/
int32 column = qAxial + FMath::FloorToInt(rAxial / 2);
return (rAxial * GridSize) + column;
}
// Called once on Begin Play // Called once on Begin Play
void AAdventureMap::MakeGrid() void AAdventureMap::MakeGrid()
{ {
@ -83,8 +72,23 @@ void AAdventureMap::MakeGrid()
bHexGridReady = true; bHexGridReady = true;
} }
// Every Hex Tile's index within the Grid Array can be derived from its Q and R coordinates
int32 AAdventureMap::GridIndex(int32 qAxial, int32 rAxial)
{
/*
* The Q axis is (i.e. columns are) oriented diagonally.
* The Hex Grid has a rough square shape, hence the Q coordinates must be offset by -1 every other row.
*/
int32 column = qAxial + FMath::FloorToInt(rAxial / 2);
return (rAxial * GridSize) + column;
}
AHexTile* AAdventureMap::RandomHex() AHexTile* AAdventureMap::RandomHex()
{ {
int32 RandHex = GridIndex(FMath::RandRange(0, GridSize), FMath::RandRange(0, GridSize)); int32 RandHex = GridIndex(FMath::RandRange(0, GridSize), FMath::RandRange(0, GridSize));
while (RandHex > Grid.Num())
{
RandHex = GridIndex(FMath::RandRange(0, GridSize), FMath::RandRange(0, GridSize));
}
return Grid[RandHex]; return Grid[RandHex];
} }

View File

@ -22,19 +22,22 @@ public:
TSubclassOf<AHexTile> BaseTileClass; TSubclassOf<AHexTile> BaseTileClass;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
TSubclassOf<ACharacter> BasePartyCharacterClass; TSubclassOf<ACharacter> BasePartyCharacterClass;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Config") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
int32 GridSize = 100; // squared is the number of Tiles int32 GridSize = 100; // squared is the number of Tiles
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Config") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
int32 TileSize = 100; int32 TileSize = 100;
UFUNCTION(BlueprintCallable, Category = "Generation")
void MakeGrid();
UPROPERTY(BlueprintReadOnly, Category = "Generation") UPROPERTY(BlueprintReadOnly, Category = "Generation")
bool bHexGridReady; bool bHexGridReady;
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "Generation")
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "Runtime")
TArray<AHexTile*> Grid; TArray<AHexTile*> Grid;
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
int32 GridIndex(int32 q, int32 r); int32 GridIndex(int32 q, int32 r);
UFUNCTION(BlueprintCallable)
AHexTile* RandomHex();
// Player spawn section // Player spawn section
UPROPERTY(VisibleAnywhere, BlueprintReadWrite) UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
@ -42,15 +45,7 @@ public:
UPROPERTY(VisibleAnywhere, BlueprintReadWrite) UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
AAdventureCharacter* PlayerCharacter; AAdventureCharacter* PlayerCharacter;
UFUNCTION(BlueprintCallable)
void MakeGrid();
UFUNCTION(BlueprintCallable)
AHexTile* RandomHex();
protected: protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned
virtual void BeginPlay() override; virtual void BeginPlay() override;
}; };

View File

@ -14,8 +14,6 @@ AHexTile::AHexTile()
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Scene")); RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Scene"));
this->FillCornersArray(); this->FillCornersArray();
// OnClicked.AddDynamic(this, )
} }
void AHexTile::BeginPlay() void AHexTile::BeginPlay()
@ -42,3 +40,8 @@ void AHexTile::FillCornersArray()
} }
} }
TArray<AHexTile*> AHexTile::Neighbors(AHexTile* OfHexTile)
{
return TArray<AHexTile*>();
}

View File

@ -28,7 +28,7 @@ public:
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
FVector Corner(int32 i); FVector Corner(int32 i);
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Runtime") UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "debug")
TArray<FVector> Corners; TArray<FVector> Corners;
UFUNCTION() UFUNCTION()
void FillCornersArray(); void FillCornersArray();
@ -40,6 +40,9 @@ public:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Runtime") UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Runtime")
int32 Index; int32 Index;
UFUNCTION(BlueprintCallable)
TArray<AHexTile*> Neighbors(AHexTile* OfHexTile);
protected: protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned
virtual void BeginPlay() override; virtual void BeginPlay() override;