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);
if (IsValid(FollowPawn)) { if (FollowPawn->bIsMoving) { FollowAdvPawn(DeltaTime); } }
}
// Called to bind functionality to input
@ -69,18 +70,19 @@ void AAdventureCameraPawn::SetupPlayerInputComponent(UInputComponent* PlayerInpu
PlayerInputComponent->BindAxis("Move WS", this, &AAdventureCameraPawn::ScrollVert);
}
}
void AAdventureCameraPawn::ScrollSide(float AxisValue)
{
float DeltaLoc = AxisValue * BaseScrollSpeed * 3;
AddActorLocalOffset(FVector(DeltaLoc, 0, 0));
}
void AAdventureCameraPawn::ScrollVert(float AxisValue)
{
float DeltaLoc = AxisValue * BaseScrollSpeed * -3;
AddActorLocalOffset(FVector(0, DeltaLoc, 0));
}
void AAdventureCameraPawn::EdgeScrollSide(float AxisValue)
{
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..."));
AdvPawnLocationCurrent = FollowPawn->GetActorLocation();
AdvPawnLocationDelta = AdvPawnLocationCurrent - AdvPawnLocationPrevious;
UE_LOG(LogTemp, Warning, TEXT("%s"), *AdvPawnLocationDelta.ToString());
AddActorLocalOffset(AdvPawnLocationDelta);
AdvPawnLocationPrevious = AdvPawnLocationCurrent;
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;
if (MapPawnInViewX && MapPawnInViewY) { AddActorLocalOffset(FollowPawn->GetVelocity() * DeltaSeconds); }
}

View File

@ -28,26 +28,13 @@ public:
class AAdventureCharacter* FollowPawn;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input")
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Input")
float ESASize;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input")
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Input")
float ScrollAccelleration;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input")
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Input")
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:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
@ -77,5 +64,5 @@ public:
void EdgeScrollVert(float AxisValue);
UFUNCTION(BlueprintCallable)
void FollowAdvPawn();
void FollowAdvPawn(float DeltaSeconds);
};

View File

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

View File

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

View File

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

View File

@ -45,6 +45,18 @@ void AAdventurePlayerController::LeftClick()
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()
{
bInPlacementMode = !bInPlacementMode;

View File

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