From b5c9c661fec46cde03a44d8aada41799dd0d4d1d Mon Sep 17 00:00:00 2001 From: Maximilian Fajnberg Date: Wed, 12 Jan 2022 18:22:16 +0100 Subject: [PATCH] plan to implement pathfinding; minor fixes & refactorings --- AdventureCharacter.cpp | 9 +++++++++ AdventureCharacter.h | 14 ++++++++++++++ AdventureMap.cpp | 28 ++++++++++++++++------------ AdventureMap.h | 21 ++++++++------------- HexTile.cpp | 7 +++++-- HexTile.h | 5 ++++- 6 files changed, 56 insertions(+), 28 deletions(-) diff --git a/AdventureCharacter.cpp b/AdventureCharacter.cpp index 4a37af6..ca9180f 100644 --- a/AdventureCharacter.cpp +++ b/AdventureCharacter.cpp @@ -2,6 +2,8 @@ #include "AdventureCharacter.h" +#include "AdventureMap.h" +#include "HexTile.h" // Sets default values AAdventureCharacter::AAdventureCharacter() @@ -32,3 +34,10 @@ void AAdventureCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInput } +void AAdventureCharacter::AStarFindPath(AHexTile*) +{ + std::priority_queue Frontier; + TMap CameFrom; + TMap CostSoFar; + +} \ No newline at end of file diff --git a/AdventureCharacter.h b/AdventureCharacter.h index 42793a8..63c5d33 100644 --- a/AdventureCharacter.h +++ b/AdventureCharacter.h @@ -4,8 +4,13 @@ #include "CoreMinimal.h" #include "GameFramework/Character.h" + +#include + #include "AdventureCharacter.generated.h" +class AHexTile; + UCLASS() class FRAY_API AAdventureCharacter : public ACharacter { @@ -15,6 +20,15 @@ public: // Sets default values for this character's properties AAdventureCharacter(); + UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "Runtime") + AHexTile* GridLocation; + UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "Runtime") + AHexTile* SelectedHex; + UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime") + TArray MovementPath; + UFUNCTION(BlueprintCallable) + void AStarFindPath(AHexTile* Goal); + protected: // Called when the game starts or when spawned virtual void BeginPlay() override; diff --git a/AdventureMap.cpp b/AdventureMap.cpp index 5da4c30..182f4b3 100644 --- a/AdventureMap.cpp +++ b/AdventureMap.cpp @@ -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 void AAdventureMap::MakeGrid() { @@ -83,8 +72,23 @@ void AAdventureMap::MakeGrid() 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() { 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]; -} \ No newline at end of file +} diff --git a/AdventureMap.h b/AdventureMap.h index a06fc7e..22715e6 100644 --- a/AdventureMap.h +++ b/AdventureMap.h @@ -22,19 +22,22 @@ public: TSubclassOf BaseTileClass; UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") TSubclassOf BasePartyCharacterClass; - UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Config") + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") int32 GridSize = 100; // squared is the number of Tiles - UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Config") + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") int32 TileSize = 100; + UFUNCTION(BlueprintCallable, Category = "Generation") + void MakeGrid(); UPROPERTY(BlueprintReadOnly, Category = "Generation") bool bHexGridReady; - - UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "Runtime") + UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "Generation") TArray Grid; + UFUNCTION(BlueprintCallable) int32 GridIndex(int32 q, int32 r); - + UFUNCTION(BlueprintCallable) + AHexTile* RandomHex(); // Player spawn section UPROPERTY(VisibleAnywhere, BlueprintReadWrite) @@ -42,15 +45,7 @@ public: UPROPERTY(VisibleAnywhere, BlueprintReadWrite) AAdventureCharacter* PlayerCharacter; - UFUNCTION(BlueprintCallable) - void MakeGrid(); - UFUNCTION(BlueprintCallable) - AHexTile* RandomHex(); - protected: // Called when the game starts or when spawned virtual void BeginPlay() override; - - - }; \ No newline at end of file diff --git a/HexTile.cpp b/HexTile.cpp index 3856383..0f7a3ef 100644 --- a/HexTile.cpp +++ b/HexTile.cpp @@ -14,8 +14,6 @@ AHexTile::AHexTile() RootComponent = CreateDefaultSubobject(TEXT("Scene")); this->FillCornersArray(); - - // OnClicked.AddDynamic(this, ) } void AHexTile::BeginPlay() @@ -42,3 +40,8 @@ void AHexTile::FillCornersArray() } } +TArray AHexTile::Neighbors(AHexTile* OfHexTile) +{ + return TArray(); +} + diff --git a/HexTile.h b/HexTile.h index 9169796..73c4a8d 100644 --- a/HexTile.h +++ b/HexTile.h @@ -28,7 +28,7 @@ public: UFUNCTION(BlueprintCallable) FVector Corner(int32 i); - UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Runtime") + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "debug") TArray Corners; UFUNCTION() void FillCornersArray(); @@ -40,6 +40,9 @@ public: UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Runtime") int32 Index; + UFUNCTION(BlueprintCallable) + TArray Neighbors(AHexTile* OfHexTile); + protected: // Called when the game starts or when spawned virtual void BeginPlay() override;