From b7b9963a687c9ed66226105a65b2dff3d0163663 Mon Sep 17 00:00:00 2001 From: Maximilian Fajnberg Date: Wed, 26 Jan 2022 16:48:58 +0100 Subject: [PATCH] Camera now following Pawn --- AdventureCameraPawn.cpp | 19 +++++++----------- AdventureCameraPawn.h | 21 ++++---------------- AdventureCharacter.h | 2 ++ AdventureMap.cpp | 37 ++++++++++++++++++++--------------- AdventureMap.h | 1 + AdventurePlayerController.cpp | 12 ++++++++++++ AdventurePlayerController.h | 9 +++++++++ 7 files changed, 56 insertions(+), 45 deletions(-) diff --git a/AdventureCameraPawn.cpp b/AdventureCameraPawn.cpp index 69a102b..501edc2 100644 --- a/AdventureCameraPawn.cpp +++ b/AdventureCameraPawn.cpp @@ -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); } } \ No newline at end of file diff --git a/AdventureCameraPawn.h b/AdventureCameraPawn.h index b531dda..3ba6e59 100644 --- a/AdventureCameraPawn.h +++ b/AdventureCameraPawn.h @@ -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); }; diff --git a/AdventureCharacter.h b/AdventureCharacter.h index a472d6f..80022c5 100644 --- a/AdventureCharacter.h +++ b/AdventureCharacter.h @@ -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 diff --git a/AdventureMap.cpp b/AdventureMap.cpp index c502325..8bed685 100644 --- a/AdventureMap.cpp +++ b/AdventureMap.cpp @@ -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 AAdventureMap::Neighbors(AHexTile* OfHex) { TArray 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 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 AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radiu TArray ToExamine; TSet Processed; + Results.Add(Start); ToExamine.Add(Start); while (!ToExamine.IsEmpty()) { @@ -107,19 +114,17 @@ TArray 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 AAdventureMap::AStar(AHexTile* Start, AHexTile* Goal) { TArray ToExamine; @@ -128,15 +133,15 @@ TArray 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; } diff --git a/AdventureMap.h b/AdventureMap.h index baf2ffb..0f5503f 100644 --- a/AdventureMap.h +++ b/AdventureMap.h @@ -8,6 +8,7 @@ class AHexTile; class AMapObject; +class AAdventurePlayerController; UCLASS() class FRAY_API AAdventureMap : public AActor diff --git a/AdventurePlayerController.cpp b/AdventurePlayerController.cpp index 59cdf47..33291b9 100644 --- a/AdventurePlayerController.cpp +++ b/AdventurePlayerController.cpp @@ -45,6 +45,18 @@ void AAdventurePlayerController::LeftClick() if (bInPlacementMode) { PlaceObject(PlaceObjClass, HoveredHex); } } +TArray AAdventurePlayerController::Vision() +{ + TArray Results; + TArray 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; diff --git a/AdventurePlayerController.h b/AdventurePlayerController.h index 390c46b..b42e5d3 100644 --- a/AdventurePlayerController.h +++ b/AdventurePlayerController.h @@ -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 ExploredHexes; + UPROPERTY(EditAnywhere, BlueprintReadWrite) + int32 VisionRadius = 7; + UFUNCTION(BlueprintCallable) + TArray Vision(); + protected: virtual void BeginPlay() override; virtual void SetupInputComponent() override;