port to UE5 and new development environment; consequential leeway

This commit is contained in:
Maximilian Fajnberg 2022-05-25 20:07:51 +02:00
parent 1485621698
commit 21f7dd4e67
5 changed files with 74 additions and 35 deletions

View File

@ -55,6 +55,14 @@ void AAdventureCameraPawn::Tick(float DeltaTime)
{ {
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
// quick fix for edgescrolling after viewport resizing.
//
//TickIncrement++;
//if (TickIncrement >= 60) {
// TickIncrement = 0;
// ViewSize = Viewport->Viewport->GetSizeXY();
//}
if (IsValid(FollowPawn)) { if (FollowPawn->bIsMoving) { FollowAdvPawn(DeltaTime); } } if (IsValid(FollowPawn)) { if (FollowPawn->bIsMoving) { FollowAdvPawn(DeltaTime); } }
} }

View File

@ -19,6 +19,8 @@ public:
UGameViewportClient* Viewport; UGameViewportClient* Viewport;
UPROPERTY() UPROPERTY()
FIntPoint ViewSize; FIntPoint ViewSize;
UPROPERTY()
int32 TickIncrement = 0;
UPROPERTY(BlueprintReadWrite, Category = "Config") UPROPERTY(BlueprintReadWrite, Category = "Config")
class AAdventureMap* AdvMapRef; class AAdventureMap* AdvMapRef;

View File

@ -63,7 +63,7 @@ int32 AAdventureMap::GridIndex(int32 qAxial, int32 rAxial)
* The Q axis is (i.e. columns are) oriented diagonally. * 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. * 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); int32 column = qAxial + FMath::FloorToInt(rAxial / 2.);
return (rAxial * GridSize) + column; return (rAxial * GridSize) + column;
} }
@ -142,7 +142,7 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal,
while (!ToExamine.IsEmpty()) { while (!ToExamine.IsEmpty()) {
AHexTile* Candidate = ToExamine[0]; AHexTile* Candidate = ToExamine[0];
ToExamine.Remove(Candidate); ToExamine.Remove(Candidate);
// try for Hex with lower estimatet (F)cost // find Hex with lower estimatet F-cost
for (AHexTile* t : ToExamine) { for (AHexTile* t : ToExamine) {
t->FCost = t->GCost + t->HCost; t->FCost = t->GCost + t->HCost;
if (t->FCost < Candidate->FCost || t->FCost == Candidate->FCost && t->HCost < Candidate->HCost) { Candidate = t; } if (t->FCost < Candidate->FCost || t->FCost == Candidate->FCost && t->HCost < Candidate->HCost) { Candidate = t; }
@ -181,64 +181,88 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal,
Algo::Reverse(Path); Algo::Reverse(Path);
if (bDiags) { if (bDiags) {
TArray<AHexTile*> PathS = ShortcutAStar(Path); Path = ShortcutAStar(Path);
} }
return Path; return Path;
} }
// very bro-sciency approach to pathfinding for diagonal Hex-movement
TArray<AHexTile*> AAdventureMap::ShortcutAStar(TArray<AHexTile*> Path) TArray<AHexTile*> AAdventureMap::ShortcutAStar(TArray<AHexTile*> Path)
{ {
TArray<AHexTile*> Shortcut; TArray<AHexTile*> Shortcut;
int32 Len = Path.Num(); int32 Len = Path.Num();
TArray<AHexTile*> WorkingSegment; TArray<AHexTile*> WorkingSegment;
AHexTile* Start = PCRef->CurrentHex; AHexTile* CurrentHex = PCRef->CurrentHex;
WorkingSegment.Add(Start); WorkingSegment.Add(CurrentHex);
int32 i = 0; int32 h = 0;
FHexVector PrevDir = FHexVector(Path[0], Start); // create segments for each bend
FHexVector PrevDir = FHexVector(Path[0], CurrentHex);
FHexVector DirASave = PrevDir;
FHexVector DirA; FHexVector DirA;
int32 HexesBeforeBend = 1; int32 HexesBeforeBend = 1;
for (i; i < Len-1; i++) { for (h; h < Len-1; h++) {
WorkingSegment.Add(Path[i]); WorkingSegment.Add(Path[h]);
DirA = FHexVector(Path[i+1], Path[i]); DirA = FHexVector(Path[h+1], Path[h]);
if (DirA != PrevDir) { break; } if (DirA != PrevDir) { break; } // save Path[h] into Array of Bends
HexesBeforeBend++; HexesBeforeBend++;
PrevDir = DirA; PrevDir = DirA;
} }
PrevDir = DirA; PrevDir = DirA;
FHexVector DirB; FHexVector DirB;
int32 HexesAfterBend = 0; int32 HexesAfterBend = 0;
for (i; i < Len - 1; i++) { for (h; h < Len - 1; h++) {
WorkingSegment.Add(Path[i+1]); WorkingSegment.Add(Path[h+1]);
DirB = FHexVector(Path[i+1], Path[i]); DirB = FHexVector(Path[h+1], Path[h]);
if (DirB != PrevDir) { break; } if (DirB != PrevDir) { break; }
HexesAfterBend++; HexesAfterBend++;
PrevDir = DirB; PrevDir = DirB;
} }
if (HexesAfterBend == 0) { return Path; }
// debug if (HexesAfterBend == 0)
UE_LOG(LogTemp, Warning, TEXT("Before bend: %d"), HexesBeforeBend); { return Path; }
UE_LOG(LogTemp, Warning, TEXT("After bend: %d"), HexesAfterBend); FHexVector UnitDiag = UnitDiagFromUnitNB(DirASave, DirB);
UE_LOG(LogTemp, Warning, TEXT("Working segment length: %d"), WorkingSegment.Num());
for (AHexTile* HexDebug : WorkingSegment) { UE_LOG(LogTemp, Warning, TEXT("HexID: %d"), HexDebug->Index); }
FHexVector UnitDiag = UnitDiagFromUnitNB(DirA, DirB);
AHexTile* Milestone = WorkingSegment.Last(); AHexTile* Milestone = WorkingSegment.Last();
int32 NumDiags = (HexesBeforeBend >= HexesAfterBend) ? HexesAfterBend : HexesBeforeBend;
int32 NumTries = (HexesBeforeBend >= HexesAfterBend) ? HexesBeforeBend : HexesAfterBend;
// Try adding diagonal Hexes to Shortcut from Start until Milestone (if BeforeBend>=AferBend: do this AfterBend times, else: BeforeBend) bool bDiagAdded = false;
// Set a bool to 'true' as soon as the first Hex is added; Set CurrentHex to that; for (int i = 0; i < NumTries; i++) {
// if the path is blocked and the bool is 'false': Set CurrentHex to the next in WorkingSegment; Retry. if (NumDiags == 0) {
// if the path is blocked and the bool is 'true': Append the rest recursively calling A*(Shortcut.Last(), Path.Last(), true); Shortcut.Append(FindPathAStar(CurrentHex, Milestone, false));
// when iterated AfterBend (or BeforeBend) times: Append the rest recursively calling A*(Shortcut.Last(), Path.Last(), true); break;
}
if (DiagIsReachable(CurrentHex, UnitDiag)) {
int32 CanIndex = GridIndex(CurrentHex->Q + UnitDiag.Q, CurrentHex->R + UnitDiag.R);
if (Grid.IsValidIndex(CanIndex)) {
AHexTile* Candidate = Grid[CanIndex];
if (Candidate->bFree) {
Shortcut.Add(Candidate);
bDiagAdded = true;
CurrentHex = Candidate;
NumDiags--;
continue;
} } }
if (!bDiagAdded && !DiagIsReachable(CurrentHex, UnitDiag)) {
Shortcut.Add(CurrentHex);
CurrentHex = WorkingSegment[i + 1];
NumDiags--;
continue;
}
if (bDiagAdded && !DiagIsReachable(CurrentHex, UnitDiag)) {
Shortcut.Append(FindPathAStar(CurrentHex, Milestone, true));
break;
}
}
if (Milestone != Path.Last()) { Shortcut.Append(FindPathAStar(Milestone, Path.Last(), true)); }
UE_LOG(LogTemp, Warning, TEXT("Hexes before bend: %d"), HexesBeforeBend);
UE_LOG(LogTemp, Warning, TEXT("Hexes after bend: %d"), HexesAfterBend);
return Shortcut; return Shortcut;
} }
FHexVector AAdventureMap::UnitDiagFromUnitNB(FHexVector InVecA, FHexVector InVecB) { FHexVector AAdventureMap::UnitDiagFromUnitNB(FHexVector InVecA, FHexVector InVecB) {
if (InVecA == NNW && InVecB == NNE||InVecB == NNW && InVecA == NNE) { return N; } if (InVecA == NNW && InVecB == NNE||InVecB == NNW && InVecA == NNE) { return N; }
if (InVecA == NNE && InVecB == E ||InVecB == NNE && InVecA == E) { return ENE; } if (InVecA == NNE && InVecB == E ||InVecB == NNE && InVecA == E) { return ENE; }
@ -248,6 +272,7 @@ FHexVector AAdventureMap::UnitDiagFromUnitNB(FHexVector InVecA, FHexVector InVec
if (InVecA == W && InVecB == NNW||InVecB == W && InVecA == NNW) { return WNW; } if (InVecA == W && InVecB == NNW||InVecB == W && InVecA == NNW) { return WNW; }
return FHexVector(); return FHexVector();
} }
bool AAdventureMap::DiagIsReachable(AHexTile* InStart, FHexVector InDiagUnitVec) { bool AAdventureMap::DiagIsReachable(AHexTile* InStart, FHexVector InDiagUnitVec) {
FHexVector BlockA; FHexVector BlockA;
FHexVector BlockB; FHexVector BlockB;
@ -257,7 +282,10 @@ bool AAdventureMap::DiagIsReachable(AHexTile* InStart, FHexVector InDiagUnitVec)
if (InDiagUnitVec == S) { BlockA = SSE, BlockB = SSW; } if (InDiagUnitVec == S) { BlockA = SSE, BlockB = SSW; }
if (InDiagUnitVec == WSW) { BlockA = SSW, BlockB = W; } if (InDiagUnitVec == WSW) { BlockA = SSW, BlockB = W; }
if (InDiagUnitVec == WNW) { BlockA = W, BlockB = NNW; } if (InDiagUnitVec == WNW) { BlockA = W, BlockB = NNW; }
AHexTile* HexA = Grid[GridIndex(InStart->Q + BlockA.Q, InStart->R + BlockA.R)]; int32 IndexA = GridIndex(InStart->Q + BlockA.Q, InStart->R + BlockA.R);
AHexTile* HexB = Grid[GridIndex(InStart->Q + BlockB.Q, InStart->R + BlockB.R)]; int32 IndexB = GridIndex(InStart->Q + BlockB.Q, InStart->R + BlockB.R);
if (!Grid.IsValidIndex(IndexA) || !Grid.IsValidIndex(IndexB)) { return false; }
AHexTile* HexA = Grid[IndexA];
AHexTile* HexB = Grid[IndexB];
return (HexA->bFree && HexB->bFree); return (HexA->bFree && HexB->bFree);
} }

View File

@ -26,7 +26,7 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
TSubclassOf<AHexTile> BaseTileClass; TSubclassOf<AHexTile> BaseTileClass;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
int32 GridSize = 100; int32 GridSize = 60;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
int32 TileSize = 100; int32 TileSize = 100;
@ -90,6 +90,7 @@ public:
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> ShortcutAStar(TArray<AHexTile*> Path); TArray<AHexTile*> ShortcutAStar(TArray<AHexTile*> Path);
// considering a MapObjectManager class or moving pathfinding & search to PlayerController // considering a MapObjectManager class or moving pathfinding & search to PlayerController
UPROPERTY(BlueprintReadWrite, EditAnywhere) UPROPERTY(BlueprintReadWrite, EditAnywhere)

View File

@ -26,9 +26,9 @@ public:
USceneComponent* SceneComponent; USceneComponent* SceneComponent;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Config") UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Config")
UStaticMeshComponent* OrientHexMesh; UStaticMeshComponent* OrientHexMesh;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Config") UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Config")
bool bCollectable; bool bCollectable;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Config") UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Config")
bool bActivatable; bool bActivatable;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Generation") UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Generation")