small changes & cleanup

This commit is contained in:
Maximilian Fajnberg 2022-01-29 20:54:35 +01:00
parent 2bf072e3c6
commit 429ce51805
7 changed files with 32 additions and 67 deletions

View File

@ -18,10 +18,11 @@ AAdventureCameraPawn::AAdventureCameraPawn()
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it. // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
ESASize = .01;
BaseScrollSpeed = 20; BaseScrollSpeed = 20;
ScrollAccelleration = BaseScrollSpeed * 3; ScrollAccelleration = BaseScrollSpeed * 3;
ESASize = .01;
ESAMaxBoundSlope = 1 / (1 - (1-ESASize)); ESAMaxBoundSlope = 1 / (1 - (1-ESASize));
ESAMaxBoundIntercept = 1 - ESAMaxBoundSlope; ESAMaxBoundIntercept = 1 - ESAMaxBoundSlope;
@ -176,7 +177,6 @@ void AAdventureCameraPawn::EdgeScrollVert(float AxisValue)
SpeedY = AxisValue * ScrollAccelleration; SpeedY = AxisValue * ScrollAccelleration;
AddActorLocalOffset(FVector(0, -SpeedY, 0)); AddActorLocalOffset(FVector(0, -SpeedY, 0));
} }
} }
} }

View File

@ -31,5 +31,4 @@ void AAdventureCharacter::Tick(float DeltaTime)
void AAdventureCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) void AAdventureCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{ {
Super::SetupPlayerInputComponent(PlayerInputComponent); Super::SetupPlayerInputComponent(PlayerInputComponent);
} }

View File

@ -36,7 +36,6 @@ void AAdventureMap::MakeGrid()
for (int r = 1; r <= GridSize; r++) { for (int r = 1; r <= GridSize; r++) {
float XOffset = 0.f; float XOffset = 0.f;
if (r % 2 != 0) { if (r > 1) { QOffset--; } } if (r % 2 != 0) { if (r > 1) { QOffset--; } }
else { XOffset = HexWidth / 2; } else { XOffset = HexWidth / 2; }
@ -45,23 +44,19 @@ void AAdventureMap::MakeGrid()
NextHexAt.Y = TileSize * 1.5f * r; NextHexAt.Y = TileSize * 1.5f * r;
NextHexAt.Z = 0.f; NextHexAt.Z = 0.f;
FTransform SpawnTransform = FTransform(NextHexAt); FTransform SpawnTransform = FTransform(NextHexAt);
AHexTile* Tile = World->SpawnActor<AHexTile>(BaseTileClass, SpawnTransform); AHexTile* Tile = World->SpawnActor<AHexTile>(BaseTileClass, SpawnTransform);
Grid.Add(Tile); Grid.Add(Tile);
Tile->Q = q - 1 + QOffset; Tile->Q = q - 1 + QOffset;
Tile->R = r - 1; Tile->R = r - 1;
} }
} }
for (auto& tile : Grid) { for (auto& tile : Grid) {
tile->Index = GridIndex(tile->Q, tile->R); tile->Index = GridIndex(tile->Q, tile->R);
} }
bHexGridReady = true; bHexGridReady = true;
} }
// Every Hex Tile's index within the Grid Array can be derived from its Q and R coordinates // Every Hex Tile's index within the Grid Array can be derived from its Axial Q and R coordinates
int32 AAdventureMap::GridIndex(int32 qAxial, int32 rAxial) int32 AAdventureMap::GridIndex(int32 qAxial, int32 rAxial)
{ {
/* /*
@ -78,50 +73,36 @@ AHexTile* AAdventureMap::RandomHex()
return Grid[RandHex]; return Grid[RandHex];
} }
/*
* Add two TArray<TPair<int32, int32>> members containing the Cardinal Directions
(one for immediate neighbors, one for diagonals)
{ fill them in AAdventureMap::AAdventureMap }
* This function instead returns
TMap<bool bDiag, AHexTile* Neighbor>
*/
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex, bool bFreeOnly = false) TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex, bool bFreeOnly = false)
{ {
TArray<AHexTile*> Results; TArray<AHexTile*> Results;
for (auto& V : NeighborUnitVectors) { for (auto& V : NeighborUnitVectors) {
int32 I = GridIndex(OfHex->Q + V.Q, OfHex->R + V.R); int32 I = GridIndex(OfHex->Q + V.Q, OfHex->R + V.R);
if (Grid.IsValidIndex(I)) { if (Grid.IsValidIndex(I)) {
AHexTile* R = Grid[I]; AHexTile* H = Grid[I];
Results.Add(Grid[I]); if (bFreeOnly && !H->bFree) { continue; }
if (bFreeOnly && !R->bFree) { Results.Remove(R); } if (H->Distance(OfHex) == 1) { Results.Add(H); }
} }
} }
for (auto& R : Results) {
if (bFreeOnly && !R->bFree) { Results.Remove(R); }
}
return Results; return Results;
} }
TArray<AHexTile*> AAdventureMap::FreeDiags(AHexTile* OfHex) TArray<AHexTile*> AAdventureMap::FreeDiagonals(AHexTile* OfHex)
{ {
TArray<AHexTile*> Results; TArray<AHexTile*> Results;
for (auto& V : DiagonalUnitVectors) { for (auto& V : DiagonalUnitVectors) {
int32 I = GridIndex(OfHex->Q + V.Q, OfHex->R + V.R); int32 I = GridIndex(OfHex->Q + V.Q, OfHex->R + V.R);
if (!Grid.IsValidIndex(I)) { continue; } if (!Grid.IsValidIndex(I)) { continue; }
// if (!bFreeOnly) { if (Grid[I]->Distance(OfHex) == 1) { Results.Add(Grid[I]); } }
else { else {
bool bReachable = true; bool bReachable = true;
for (auto& PotentialBlock : Neighbors(OfHex)) { for (AHexTile* PotentialBlock : Neighbors(OfHex)) {
if (PotentialBlock->Distance(Grid[I]) != 1) { continue; } if (PotentialBlock->Distance(Grid[I]) != 1) { continue; }
if (!PotentialBlock->bFree) { if (!PotentialBlock->bFree) {
bReachable = false; bReachable = false;
break; break;
} }
} }
if (bReachable) { if (bReachable) { Results.Add(Grid[I]); }
Results.Add(Grid[I]);
}
} }
} }
return Results; return Results;
@ -162,7 +143,7 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal,
AHexTile* Candidate = ToExamine[0]; AHexTile* Candidate = ToExamine[0];
ToExamine.Remove(Candidate); ToExamine.Remove(Candidate);
// try for Hex with lower estimatet (F)cost // try for Hex with lower estimatet (F)cost
for (auto& 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; }
} }
@ -174,6 +155,7 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal,
// expand frontier & adjust path data // expand frontier & adjust path data
for (AHexTile* Neighbor : Neighbors(Candidate, true)) { for (AHexTile* Neighbor : Neighbors(Candidate, true)) {
if (Neighbor->Distance(Candidate) > 1) { continue; } if (Neighbor->Distance(Candidate) > 1) { continue; }
if (!Neighbor->bFree) { continue; }
if (Processed.Contains(Neighbor)) { continue; } if (Processed.Contains(Neighbor)) { continue; }
bool bInToExamine = ToExamine.Contains(Neighbor); bool bInToExamine = ToExamine.Contains(Neighbor);
@ -186,27 +168,25 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal,
if (!bInToExamine) { if (!bInToExamine) {
Neighbor->HCost = Neighbor->Distance(Goal) * 10.f; Neighbor->HCost = Neighbor->Distance(Goal) * 10.f;
ToExamine.Add(Neighbor); ToExamine.Add(Neighbor);
} }
} }
} if (bDiags) {
} for (AHexTile* Diag : FreeDiagonals(Candidate)) {
if (bDiags) { // right now the heuristic for HCost (Distance func) does NOT take diagonals into account
for (AHexTile* Diag : FreeDiags(Candidate)) {
if (Diag->Distance(Candidate) > 2) { continue; } if (Diag->Distance(Candidate) > 2) { continue; }
if (!Diag->bFree) { continue; } if (!Diag->bFree) { continue; }
if (Processed.Contains(Diag)) { continue; } if (Processed.Contains(Diag)) { continue; }
bool bInToExamine = ToExamine.Contains(Diag); bool bInToExamine = ToExamine.Contains(Diag);
float NewGCost = Candidate->GCost + Diag->MoveCost * 10.f; float NewGCost = Candidate->GCost + 1 + Diag->MoveCost * 10.f;
if (NewGCost < Diag->GCost || !bInToExamine) { if (NewGCost < Diag->GCost || !bInToExamine) {
Diag->GCost = NewGCost; Diag->GCost = NewGCost;
Diag->CameFrom = Candidate; // chain Diag->CameFrom = Candidate; // chain
if (!bInToExamine) { if (!bInToExamine) {
Diag->HCost = Diag->Distance(Goal) * 10.f; Diag->HCost = Diag->Distance(Goal) * 10.f; // not accounting for diagonals
ToExamine.Add(Diag); ToExamine.Add(Diag);
} } }
}
} }
} }
} }

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; // squared is the number of Tiles int32 GridSize = 100;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
int32 TileSize = 100; int32 TileSize = 100;
@ -76,12 +76,14 @@ public:
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> Neighbors(AHexTile* OfHex, bool bFreeOnly); TArray<AHexTile*> Neighbors(AHexTile* OfHex, bool bFreeOnly);
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> FreeDiags(AHexTile* OfHex); TArray<AHexTile*> FreeDiagonals(AHexTile* OfHex);
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Runtime")
TSet<AHexTile*> BreadthFirstSearch(AHexTile* Start, int32 Radius); TSet<AHexTile*> BreadthFirstSearch(AHexTile* Start, int32 Radius);
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> FindPathAStar(AHexTile* Start, AHexTile* Goal, bool bDiags); TArray<AHexTile*> FindPathAStar(AHexTile* Start, AHexTile* Goal, bool bDiags);
// considering a MapObjectManager class or moving pathfinding & search to PlayerController
UPROPERTY(BlueprintReadWrite, EditAnywhere) UPROPERTY(BlueprintReadWrite, EditAnywhere)
TMap<AHexTile*, AMapObject*> MapObjects; TMap<AHexTile*, AMapObject*> MapObjects;
UPROPERTY(BlueprintReadOnly) UPROPERTY(BlueprintReadOnly)

View File

@ -60,31 +60,25 @@ TArray<AHexTile*> AAdventurePlayerController::Vision()
void AAdventurePlayerController::TogglePlacing() void AAdventurePlayerController::TogglePlacing()
{ {
bInPlacementMode = !bInPlacementMode; bInPlacementMode = !bInPlacementMode;
if (bInPlacementMode) { if (bInPlacementMode) { PlaceObj = World->SpawnActor<AMapObject>(PlaceObjClass, FTransform()); }
PlaceObj = World->SpawnActor<AMapObject>(PlaceObjClass, FTransform());
}
else { if (IsValid(PlaceObj)) { PlaceObj->Destroy(); } } else { if (IsValid(PlaceObj)) { PlaceObj->Destroy(); } }
} }
void AAdventurePlayerController::FitOnGrid(AMapObject* MapObject) void AAdventurePlayerController::FitOnGrid(AMapObject* MapObject)
{ {
if (!IsValid(HoveredHex)) { return; } if (!IsValid(HoveredHex)) { return; }
if (HoveredHex->bFree) { if (HoveredHex->bFree) { MapObject->SetActorLocation(FVector(HoveredHex->GetActorLocation())); }
MapObject->SetActorLocation(FVector(HoveredHex->GetActorLocation()));
}
} }
// called from BP; generally takes the Hex under the player cursor as argument // called from BP; generally takes the Hex under the player cursor as argument
void AAdventurePlayerController::PlaceObject(TSubclassOf<AMapObject> MapObjClass, AHexTile* OnHex) void AAdventurePlayerController::PlaceObject(TSubclassOf<AMapObject> MapObjClass, AHexTile* OnHex)
{ {
// spawn this Actor at World location of Origin Hex;
AMapObject* SpawnedObj = World->SpawnActor<AMapObject>(MapObjClass, FTransform(OnHex->GetActorTransform().GetLocation())); AMapObject* SpawnedObj = World->SpawnActor<AMapObject>(MapObjClass, FTransform(OnHex->GetActorTransform().GetLocation()));
SpawnedObj->Origin = OnHex; SpawnedObj->Origin = OnHex;
OnHex->bFree = false;
SpawnedObj->Occupy();
// set Hexes to bOccupied according to BlockVectors; MapRef->MapObjects.Add(OnHex, SpawnedObj);
OnHex->bFree = false; // exact Hexes are eventually to be determined by MapObjectClass's BlockingVectors
MapRef->MapObjects.Add(OnHex, SpawnedObj); // maybe this as well
MapRef->IncID++; MapRef->IncID++;
SpawnedObj->ID = MapRef->IncID; SpawnedObj->ID = MapRef->IncID;
MapRef->MapObjectsByID.Add(MapRef->IncID, SpawnedObj); MapRef->MapObjectsByID.Add(MapRef->IncID, SpawnedObj);

View File

@ -10,40 +10,26 @@
AHexTile::AHexTile() AHexTile::AHexTile()
{ {
TileSize = 100.f; TileSize = 100.f;
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Scene")); RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Scene"));
this->FillCornersArray(); this->FillCornersArray();
} }
void AHexTile::BeginPlay() void AHexTile::BeginPlay() { Super::BeginPlay(); }
{
Super::BeginPlay();
}
FVector AHexTile::Corner(int32 i) FVector AHexTile::Corner(int32 i)
{ {
FVector TileCenter = this->GetActorTransform().GetLocation(); FVector TileCenter = this->GetActorTransform().GetLocation();
int32 Angle_Deg = 60 * i - 30; int32 Angle_Deg = 60 * i - 30;
float Angle_Rad = UKismetMathLibrary::GetPI()/180 * Angle_Deg; float Angle_Rad = UKismetMathLibrary::GetPI()/180 * Angle_Deg;
float X = TileCenter.X + TileSize * cos(Angle_Rad); float X = TileCenter.X + TileSize * cos(Angle_Rad);
float Y = TileCenter.Y + TileSize * sin(Angle_Rad); float Y = TileCenter.Y + TileSize * sin(Angle_Rad);
return FVector(X, Y, 0.f); return FVector(X, Y, 0.f);
} }
void AHexTile::FillCornersArray() void AHexTile::FillCornersArray() { for (int32 i = 0 ; i < 6; i++) { Corners.Emplace(Corner(i + 1)); } }
{
for (int32 i = 0 ; i < 6; i++)
{
Corners.Emplace(Corner(i + 1));
}
}
int32 AHexTile::Distance(AHexTile* ToHex) int32 AHexTile::Distance(AHexTile* ToHex)
{ {
int32 CubeS1 = -this->Q - this->R; int32 CubeS1 = -this->Q - this->R;
int32 CubeS2 = -ToHex->Q - ToHex->R; int32 CubeS2 = -ToHex->Q - ToHex->R;
return (abs(this->Q - ToHex->Q) + abs(this->R - ToHex->R) + abs(CubeS1 - CubeS2)) / 2; return (abs(this->Q - ToHex->Q) + abs(this->R - ToHex->R) + abs(CubeS1 - CubeS2)) / 2;
} }

View File

@ -59,6 +59,10 @@ public:
// MapObject Placement // MapObject Placement
UPROPERTY(BlueprintReadWrite, VisibleAnywhere) UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
bool bFree = true; bool bFree = true;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
bool bTouchable = false;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
bool bSteptivatable = false;
FORCEINLINE bool operator == (const AHexTile &Other) FORCEINLINE bool operator == (const AHexTile &Other)
{ {