Camera now following Pawn

This commit is contained in:
Maximilian Fajnberg 2022-01-26 16:48:58 +01:00
parent 986233d960
commit b7b9963a68
7 changed files with 56 additions and 45 deletions

View File

@ -54,6 +54,7 @@ void AAdventureCameraPawn::Tick(float DeltaTime)
{ {
Super::Tick(DeltaTime); Super::Tick(DeltaTime);
if (IsValid(FollowPawn)) { if (FollowPawn->bIsMoving) { FollowAdvPawn(DeltaTime); } }
} }
// Called to bind functionality to input // Called to bind functionality to input
@ -69,18 +70,19 @@ void AAdventureCameraPawn::SetupPlayerInputComponent(UInputComponent* PlayerInpu
PlayerInputComponent->BindAxis("Move WS", this, &AAdventureCameraPawn::ScrollVert); PlayerInputComponent->BindAxis("Move WS", this, &AAdventureCameraPawn::ScrollVert);
} }
} }
void AAdventureCameraPawn::ScrollSide(float AxisValue) void AAdventureCameraPawn::ScrollSide(float AxisValue)
{ {
float DeltaLoc = AxisValue * BaseScrollSpeed * 3; float DeltaLoc = AxisValue * BaseScrollSpeed * 3;
AddActorLocalOffset(FVector(DeltaLoc, 0, 0)); AddActorLocalOffset(FVector(DeltaLoc, 0, 0));
} }
void AAdventureCameraPawn::ScrollVert(float AxisValue) void AAdventureCameraPawn::ScrollVert(float AxisValue)
{ {
float DeltaLoc = AxisValue * BaseScrollSpeed * -3; float DeltaLoc = AxisValue * BaseScrollSpeed * -3;
AddActorLocalOffset(FVector(0, DeltaLoc, 0)); AddActorLocalOffset(FVector(0, DeltaLoc, 0));
} }
void AAdventureCameraPawn::EdgeScrollSide(float AxisValue) void AAdventureCameraPawn::EdgeScrollSide(float AxisValue)
{ {
float mouseX; float mouseX;
@ -178,16 +180,9 @@ void AAdventureCameraPawn::EdgeScrollVert(float AxisValue)
} }
} }
void AAdventureCameraPawn::FollowAdvPawn() void AAdventureCameraPawn::FollowAdvPawn(float DeltaSeconds)
{ {
UE_LOG(LogTemp, Warning, TEXT("following...")); bool MapPawnInViewX = (FMath::Abs(FollowPawn->GetActorTransform().GetLocation().X - this->GetActorLocation().X)) < ViewSize.X;
bool MapPawnInViewY = (FMath::Abs(FollowPawn->GetActorTransform().GetLocation().Y - this->GetActorLocation().Y)) < ViewSize.Y;
AdvPawnLocationCurrent = FollowPawn->GetActorLocation(); if (MapPawnInViewX && MapPawnInViewY) { AddActorLocalOffset(FollowPawn->GetVelocity() * DeltaSeconds); }
AdvPawnLocationDelta = AdvPawnLocationCurrent - AdvPawnLocationPrevious;
UE_LOG(LogTemp, Warning, TEXT("%s"), *AdvPawnLocationDelta.ToString());
AddActorLocalOffset(AdvPawnLocationDelta);
AdvPawnLocationPrevious = AdvPawnLocationCurrent;
} }

View File

@ -28,26 +28,13 @@ public:
class AAdventureCharacter* FollowPawn; class AAdventureCharacter* FollowPawn;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Input")
float ESASize; float ESASize;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Input")
float ScrollAccelleration; float ScrollAccelleration;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input") UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Input")
float BaseScrollSpeed; float BaseScrollSpeed;
UPROPERTY()
FVector AdvPawnLocationPrevious;
UPROPERTY()
FVector AdvPawnLocationCurrent;
UPROPERTY()
FVector AdvPawnLocationDelta;
UPROPERTY(BlueprintReadWrite, Category = "Runtime")
bool bAdvPawnIsMoving = false;
UPROPERTY(BlueprintReadWrite, Category = "Runtime")
class AHexTile* SelectedHex;
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;
@ -77,5 +64,5 @@ public:
void EdgeScrollVert(float AxisValue); void EdgeScrollVert(float AxisValue);
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
void FollowAdvPawn(); void FollowAdvPawn(float DeltaSeconds);
}; };

View File

@ -24,6 +24,8 @@ public:
class AAdventureMap* MapRef; class AAdventureMap* MapRef;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime") UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime")
AHexTile* Location; AHexTile* Location;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime")
bool bIsMoving;
protected: protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned

View File

@ -3,6 +3,7 @@
#include "AdventureMap.h" #include "AdventureMap.h"
#include "HexTile.h" #include "HexTile.h"
#include "AdventurePlayerController.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "Algo/Reverse.h" #include "Algo/Reverse.h"
@ -81,14 +82,19 @@ AHexTile* AAdventureMap::RandomHex()
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex) TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex)
{ {
TArray<AHexTile*> Neighbors; TArray<AHexTile*> Neighbors;
int32 Arr[] = { GridIndex(OfHex->Q+1, OfHex->R), GridIndex(OfHex->Q+1, OfHex->R-1),
GridIndex(OfHex->Q, OfHex->R-1), GridIndex(OfHex->Q-1, OfHex->R),
GridIndex(OfHex->Q-1, OfHex->R+1), GridIndex(OfHex->Q, OfHex->R+1) };
TArray<int32> Indeces; TArray<int32> Indeces;
Indeces.Append(Arr, UE_ARRAY_COUNT(Arr));
Neighbors.Add(RandomHex()); // pathetic but necessary Indeces.Add(GridIndex(OfHex->Q + NE.Key, OfHex->R + NE.Value));
for (auto& I : Indeces) { if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); } } Indeces.Add(GridIndex(OfHex->Q + E.Key, OfHex->R + E.Value));
Indeces.Add(GridIndex(OfHex->Q + SE.Key, OfHex->R + SE.Value));
Indeces.Add(GridIndex(OfHex->Q + SW.Key, OfHex->R + SW.Value));
Indeces.Add(GridIndex(OfHex->Q + W.Key, OfHex->R + W.Value));
Indeces.Add(GridIndex(OfHex->Q + NW.Key, OfHex->R + NW.Value));
for (auto& I : Indeces) {
if (Grid.IsValidIndex(I)) { if (OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); } }
}
return Neighbors; return Neighbors;
} }
@ -98,6 +104,7 @@ TArray<AHexTile*> AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radiu
TArray<AHexTile*> ToExamine; TArray<AHexTile*> ToExamine;
TSet<AHexTile*> Processed; TSet<AHexTile*> Processed;
Results.Add(Start);
ToExamine.Add(Start); ToExamine.Add(Start);
while (!ToExamine.IsEmpty()) { while (!ToExamine.IsEmpty()) {
@ -107,19 +114,17 @@ TArray<AHexTile*> AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radiu
ToExamine.Remove(Candidate); ToExamine.Remove(Candidate);
for (AHexTile* Neighbor : Neighbors(Candidate)) { for (AHexTile* Neighbor : Neighbors(Candidate)) {
if (Neighbor->Distance(Candidate) > 1) { continue; } if (Neighbor->Distance(Candidate) > 1) { continue; }
if (Processed.Contains(Neighbor)) { continue; } if (Processed.Contains(Neighbor)) { continue; }
if (Neighbor->Distance(Start) > Radius) { continue; }
if (Neighbor->Distance(Start) <= Radius) { ToExamine.Add(Neighbor);
ToExamine.Add(Neighbor); Results.Add(Neighbor);
Results.Add(Neighbor);
}
} }
} }
Results.Add(Start);
return Results; return Results;
} }
TArray<AHexTile*> AAdventureMap::AStar(AHexTile* Start, AHexTile* Goal) TArray<AHexTile*> AAdventureMap::AStar(AHexTile* Start, AHexTile* Goal)
{ {
TArray<AHexTile*> ToExamine; TArray<AHexTile*> ToExamine;
@ -128,15 +133,15 @@ TArray<AHexTile*> AAdventureMap::AStar(AHexTile* Start, AHexTile* Goal)
while (!ToExamine.IsEmpty()) { while (!ToExamine.IsEmpty()) {
AHexTile* Candidate = ToExamine[0]; AHexTile* Candidate = ToExamine[0];
ToExamine.Remove(Candidate);
// estimate closest known Hex to Goal // estimate closest known Hex to Goal
for (auto& t : ToExamine) { for (auto& t : ToExamine) {
int32 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; }
} }
Processed.Add(Candidate); Processed.Add(Candidate);
ToExamine.Remove(Candidate);
// exit // exit
if (Candidate == Goal) { break; } if (Candidate == Goal) { break; }

View File

@ -8,6 +8,7 @@
class AHexTile; class AHexTile;
class AMapObject; class AMapObject;
class AAdventurePlayerController;
UCLASS() UCLASS()
class FRAY_API AAdventureMap : public AActor class FRAY_API AAdventureMap : public AActor

View File

@ -45,6 +45,18 @@ void AAdventurePlayerController::LeftClick()
if (bInPlacementMode) { PlaceObject(PlaceObjClass, HoveredHex); } if (bInPlacementMode) { PlaceObject(PlaceObjClass, HoveredHex); }
} }
TArray<AHexTile*> AAdventurePlayerController::Vision()
{
TArray<AHexTile*> Results;
TArray<AHexTile*> Visible = MapRef->BreadthFirstSearch(CurrentHex, 7);
for (auto& Hex : Visible) {
if (ExploredHexes.Contains(Hex)) { continue; }
Results.Add(Hex);
ExploredHexes.Add(Hex);
}
return Results;
}
void AAdventurePlayerController::TogglePlacing() void AAdventurePlayerController::TogglePlacing()
{ {
bInPlacementMode = !bInPlacementMode; bInPlacementMode = !bInPlacementMode;

View File

@ -28,6 +28,8 @@ public:
UWorld* World; UWorld* World;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite) UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
AAdventureMap* MapRef; AAdventureMap* MapRef;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
AAdventureCameraPawn* CamRef;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite) UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
AHexTile* SpawnHex; AHexTile* SpawnHex;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime") UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime")
@ -37,6 +39,13 @@ public:
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
void LeftClick(); void LeftClick();
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSet<AHexTile*> ExploredHexes;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 VisionRadius = 7;
UFUNCTION(BlueprintCallable)
TArray<AHexTile*> Vision();
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
virtual void SetupInputComponent() override; virtual void SetupInputComponent() override;