Compare commits
No commits in common. "32f4c6a27827eac28d274d26c68a729835126b5a" and "fc10cbfc3efcbe14ae68e1d26eaa3672d95007e5" have entirely different histories.
32f4c6a278
...
fc10cbfc3e
@ -33,3 +33,10 @@ void AAdventureCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInput
|
|||||||
Super::SetupPlayerInputComponent(PlayerInputComponent);
|
Super::SetupPlayerInputComponent(PlayerInputComponent);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AAdventureCharacter::AStarFindPath(AHexTile* Goal)
|
||||||
|
{
|
||||||
|
std::priority_queue<int32> Frontier;
|
||||||
|
TMap<AHexTile*, AHexTile*> CameFrom;
|
||||||
|
TMap<AHexTile*, int32> CostSoFar;
|
||||||
|
}
|
@ -20,10 +20,14 @@ public:
|
|||||||
// Sets default values for this character's properties
|
// Sets default values for this character's properties
|
||||||
AAdventureCharacter();
|
AAdventureCharacter();
|
||||||
|
|
||||||
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime")
|
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "Runtime")
|
||||||
AHexTile* GridLocation;
|
AHexTile* GridLocation;
|
||||||
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime")
|
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "Runtime")
|
||||||
AHexTile* SelectedHex;
|
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
|
||||||
|
119
AdventureMap.cpp
119
AdventureMap.cpp
@ -6,7 +6,7 @@
|
|||||||
#include "AdventureCameraPawn.h"
|
#include "AdventureCameraPawn.h"
|
||||||
#include "AdventureCharacter.h"
|
#include "AdventureCharacter.h"
|
||||||
#include "Kismet/GameplayStatics.h"
|
#include "Kismet/GameplayStatics.h"
|
||||||
#include "Algo/Reverse.h"
|
|
||||||
|
|
||||||
// Sets default values
|
// Sets default values
|
||||||
AAdventureMap::AAdventureMap()
|
AAdventureMap::AAdventureMap()
|
||||||
@ -40,10 +40,13 @@ void AAdventureMap::MakeGrid()
|
|||||||
{
|
{
|
||||||
if (r > 1)
|
if (r > 1)
|
||||||
{
|
{
|
||||||
QOffset--;
|
QOffset--; // The Q axis is (i.e. columns are) oriented diagonally.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else { XOffset = HexWidth / 2; }
|
else
|
||||||
|
{
|
||||||
|
XOffset = HexWidth / 2;
|
||||||
|
}
|
||||||
|
|
||||||
for (int q = 1; q <= GridSize; q++)
|
for (int q = 1; q <= GridSize; q++)
|
||||||
{
|
{
|
||||||
@ -83,111 +86,35 @@ int32 AAdventureMap::GridIndex(int32 qAxial, int32 rAxial)
|
|||||||
AHexTile* AAdventureMap::RandomHex()
|
AHexTile* AAdventureMap::RandomHex()
|
||||||
{
|
{
|
||||||
int32 RandHex = GridIndex(FMath::RandRange(0, GridSize-1), FMath::RandRange(0, GridSize-1));
|
int32 RandHex = GridIndex(FMath::RandRange(0, GridSize-1), FMath::RandRange(0, GridSize-1));
|
||||||
|
//while (RandHex > Grid.Num())
|
||||||
|
//{
|
||||||
|
// RandHex = GridIndex(FMath::RandRange(0, GridSize), FMath::RandRange(0, GridSize));
|
||||||
|
//}
|
||||||
return Grid[RandHex];
|
return Grid[RandHex];
|
||||||
}
|
}
|
||||||
|
|
||||||
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex)
|
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex)
|
||||||
{
|
{
|
||||||
TArray<AHexTile*> Neighbors;
|
TArray<AHexTile*> Neighbors;
|
||||||
int32 I;
|
int32 Index;
|
||||||
|
|
||||||
I = GridIndex(OfHex->Q + 1 , OfHex->R + 0 );
|
Index = GridIndex(OfHex->Q + 1 , OfHex->R + 0 );
|
||||||
if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); }
|
if (Index >= 0 && Index < Grid.Num() && OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
|
||||||
|
|
||||||
I = GridIndex(OfHex->Q + 1 , OfHex->R - 1 );
|
Index = GridIndex(OfHex->Q + 1 , OfHex->R - 1 );
|
||||||
if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); }
|
if (Index >= 0 && Index < Grid.Num() && OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
|
||||||
|
|
||||||
I = GridIndex(OfHex->Q + 0 , OfHex->R - 1 );
|
Index = GridIndex(OfHex->Q + 0 , OfHex->R - 1 );
|
||||||
if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); }
|
if (Index >= 0 && Index < Grid.Num() && OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
|
||||||
|
|
||||||
I = GridIndex(OfHex->Q - 1 , OfHex->R + 0 );
|
Index = GridIndex(OfHex->Q - 1 , OfHex->R + 0 );
|
||||||
if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); }
|
if (Index >= 0 && Index < Grid.Num() && OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
|
||||||
|
|
||||||
I = GridIndex(OfHex->Q - 1 , OfHex->R + 1 );
|
Index = GridIndex(OfHex->Q - 1 , OfHex->R + 1 );
|
||||||
if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); }
|
if (Index >= 0 && Index < Grid.Num() && OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
|
||||||
|
|
||||||
I = GridIndex(OfHex->Q + 0 , OfHex->R + 1 );
|
Index = GridIndex(OfHex->Q + 0 , OfHex->R + 1 );
|
||||||
if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); }
|
if (Index >= 0 && Index < Grid.Num() && OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
|
||||||
|
|
||||||
return Neighbors;
|
return Neighbors;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Be aware that the respective character will become relevant to this function at some point
|
|
||||||
TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal)
|
|
||||||
{
|
|
||||||
TArray<AHexTile*> Priorities;
|
|
||||||
Priorities.Init(Start, 1);
|
|
||||||
|
|
||||||
// Editing Hex->CameFrom pointers, i.e. chaining Hexes
|
|
||||||
while (!Priorities.IsEmpty())
|
|
||||||
{
|
|
||||||
AHexTile* Current = Priorities[0];
|
|
||||||
Priorities.RemoveAt(0);
|
|
||||||
if (*Current == *Goal)
|
|
||||||
{
|
|
||||||
// UE_LOG(LogTemp, Warning, TEXT("Goal found!")); // debug
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Expanding the Frontier
|
|
||||||
for (AHexTile* Next : Neighbors(Current))
|
|
||||||
{
|
|
||||||
int32 NewCost = Current->CostSoFar + Next->MoveCost;
|
|
||||||
|
|
||||||
// UE_LOG(LogTemp, Warning, TEXT("Cost calculated.")); // debug
|
|
||||||
|
|
||||||
if (!Priorities.Contains(Next) || NewCost < Next->CostSoFar)
|
|
||||||
{
|
|
||||||
// UE_LOG(LogTemp, Warning, TEXT("New candidate found.")); // debug
|
|
||||||
|
|
||||||
Next->CostSoFar = NewCost;
|
|
||||||
int32 NewPrio = NewCost + Next->Distance(Goal);
|
|
||||||
|
|
||||||
// Adjust the Priority Queue
|
|
||||||
if (Priorities.Contains(Next)) { Priorities.Remove(Next); }
|
|
||||||
for (AHexTile* Hex : Priorities) // at this point Priorities is empty, need to make sure it's not.
|
|
||||||
{
|
|
||||||
int32 OldPrio = Hex->CostSoFar + Hex->Distance(Goal);
|
|
||||||
int32 Index;
|
|
||||||
Priorities.Find(Hex, Index);
|
|
||||||
|
|
||||||
// UE_LOG(LogTemp, Warning, TEXT("Comparing priorities...")); // debug
|
|
||||||
|
|
||||||
if (OldPrio > NewPrio)
|
|
||||||
{
|
|
||||||
Priorities.Insert(Next, Index);
|
|
||||||
Next->CameFrom = Current;
|
|
||||||
|
|
||||||
// UE_LOG(LogTemp, Warning, TEXT("Looks promising!")); // debug
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (Index == Priorities.Num() - 1 && OldPrio <= NewPrio)
|
|
||||||
{
|
|
||||||
Priorities.Emplace(Next);
|
|
||||||
Next->CameFrom = Current;
|
|
||||||
|
|
||||||
// UE_LOG(LogTemp, Warning, TEXT("Low prio added")); // debug
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (Priorities.IsEmpty())
|
|
||||||
{
|
|
||||||
Priorities.Emplace(Next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TArray<AHexTile*> Path;
|
|
||||||
AHexTile* Hex = Goal;
|
|
||||||
while (*Hex != *Start)
|
|
||||||
{
|
|
||||||
Path.Emplace(Hex);
|
|
||||||
Hex = Hex->CameFrom;
|
|
||||||
}
|
|
||||||
|
|
||||||
Algo::Reverse(Path);
|
|
||||||
return Path;
|
|
||||||
}
|
|
||||||
|
@ -40,8 +40,6 @@ public:
|
|||||||
AHexTile* RandomHex();
|
AHexTile* RandomHex();
|
||||||
UFUNCTION(BlueprintCallable, Category = "Runtime")
|
UFUNCTION(BlueprintCallable, Category = "Runtime")
|
||||||
TArray<AHexTile*> Neighbors(AHexTile* OfHex);
|
TArray<AHexTile*> Neighbors(AHexTile* OfHex);
|
||||||
UFUNCTION(BlueprintCallable, Category = "Runtime")
|
|
||||||
TArray<AHexTile*> FindPathAStar(AHexTile* Start, AHexTile* Goal);
|
|
||||||
|
|
||||||
// Player spawn section
|
// Player spawn section
|
||||||
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
|
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
|
||||||
|
@ -14,30 +14,3 @@ AAdventurePlayerController::AAdventurePlayerController()
|
|||||||
PrimaryActorTick.bStartWithTickEnabled = true;
|
PrimaryActorTick.bStartWithTickEnabled = true;
|
||||||
|
|
||||||
}
|
}
|
||||||
void AAdventurePlayerController::BeginPlay()
|
|
||||||
{
|
|
||||||
Super::BeginPlay();
|
|
||||||
World = GetWorld();
|
|
||||||
}
|
|
||||||
|
|
||||||
void AAdventurePlayerController::SetupInputComponent()
|
|
||||||
{
|
|
||||||
// Always call this.
|
|
||||||
Super::SetupInputComponent();
|
|
||||||
|
|
||||||
// This is initialized on startup, you can go straight to binding
|
|
||||||
InputComponent->BindAction("LeftClick", IE_Pressed, this, &AAdventurePlayerController::AdvClick);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AAdventurePlayerController::AdvClick()
|
|
||||||
{
|
|
||||||
FHitResult Hit;
|
|
||||||
GetHitResultUnderCursor(ECollisionChannel::ECC_Vehicle,false,Hit);
|
|
||||||
|
|
||||||
if (IsValid(Hit.GetActor()))
|
|
||||||
{
|
|
||||||
AHexTile* HitHex = (AHexTile*)Hit.GetActor();
|
|
||||||
// MapRef->FindPathAStar(CurrentHex, HitHex); // this would currently cause a crash...
|
|
||||||
UE_LOG(LogTemp, Warning, TEXT("%d"), HitHex->Index);
|
|
||||||
}
|
|
||||||
}
|
|
@ -23,19 +23,5 @@ class FRAY_API AAdventurePlayerController : public APlayerController
|
|||||||
public:
|
public:
|
||||||
AAdventurePlayerController();
|
AAdventurePlayerController();
|
||||||
|
|
||||||
UPROPERTY()
|
|
||||||
UWorld* World;
|
|
||||||
UPROPERTY()
|
|
||||||
AAdventureMap* MapRef;
|
|
||||||
UPROPERTY()
|
|
||||||
AHexTile* SpawnHex;
|
|
||||||
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime")
|
|
||||||
AHexTile* CurrentHex;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual void BeginPlay() override;
|
|
||||||
virtual void SetupInputComponent() override;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void AdvClick();
|
|
||||||
};
|
};
|
||||||
|
39
HexTile.h
39
HexTile.h
@ -23,48 +23,31 @@ public:
|
|||||||
|
|
||||||
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
|
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
|
||||||
float TileSize;
|
float TileSize;
|
||||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Config")
|
|
||||||
|
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
|
||||||
USceneComponent* SceneComponent;
|
USceneComponent* SceneComponent;
|
||||||
UPROPERTY(BlueprintReadWrite, Category = "Config")
|
|
||||||
AAdventureMap* MapRef;
|
|
||||||
|
|
||||||
UFUNCTION(BlueprintCallable, Category = "debug")
|
UFUNCTION(BlueprintCallable, Category = "debug")
|
||||||
FVector Corner(int32 i);
|
FVector Corner(int32 i);
|
||||||
UFUNCTION()
|
|
||||||
void FillCornersArray();
|
|
||||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "debug")
|
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "debug")
|
||||||
TArray<FVector> Corners;
|
TArray<FVector> Corners;
|
||||||
|
UFUNCTION()
|
||||||
|
void FillCornersArray();
|
||||||
|
|
||||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Coordinates")
|
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Runtime")
|
||||||
int32 Q;
|
int32 Q;
|
||||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Coordinates")
|
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Runtime")
|
||||||
int32 R;
|
int32 R;
|
||||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Coordinates")
|
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Runtime")
|
||||||
int32 Index;
|
int32 Index;
|
||||||
UFUNCTION(BlueprintCallable, Category = "Coordinates")
|
UPROPERTY(BlueprintReadWrite, Category = "Runtime")
|
||||||
|
AAdventureMap* MapRef;
|
||||||
|
UFUNCTION(BlueprintCallable, Category = "Runtime")
|
||||||
int32 Distance(AHexTile* ToHex);
|
int32 Distance(AHexTile* ToHex);
|
||||||
|
|
||||||
// Pathfinding
|
|
||||||
UPROPERTY(BlueprintReadWrite, Category = "Movement")
|
|
||||||
int32 MoveCost = 1;
|
|
||||||
UPROPERTY(VisibleInstanceOnly, Category = "Movement")
|
|
||||||
AHexTile* CameFrom;
|
|
||||||
UPROPERTY(VisibleInstanceOnly, Category = "Movement")
|
|
||||||
int32 CostSoFar = 0;
|
|
||||||
|
|
||||||
FORCEINLINE bool operator == (const AHexTile &Other)
|
|
||||||
{
|
|
||||||
if (this->Q == Other.Q && this->R == Other.R) { return true; }
|
|
||||||
else { return false; }
|
|
||||||
}
|
|
||||||
FORCEINLINE bool operator != (const AHexTile &Other)
|
|
||||||
{
|
|
||||||
if (this->Q == Other.Q && this->R == Other.R) { return false; }
|
|
||||||
else { return true; }
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user