Add building ghost preview
This commit is contained in:
@@ -2,13 +2,14 @@
|
||||
|
||||
#include "AgrarianBuildingPlacementComponent.h"
|
||||
#include "AgrarianInventoryComponent.h"
|
||||
#include "DrawDebugHelpers.h"
|
||||
#include "GameFramework/Pawn.h"
|
||||
#include "GameFramework/Controller.h"
|
||||
#include "Engine/World.h"
|
||||
|
||||
UAgrarianBuildingPlacementComponent::UAgrarianBuildingPlacementComponent()
|
||||
{
|
||||
PrimaryComponentTick.bCanEverTick = false;
|
||||
PrimaryComponentTick.bCanEverTick = true;
|
||||
SetIsReplicatedByDefault(true);
|
||||
}
|
||||
|
||||
@@ -55,6 +56,16 @@ bool UAgrarianBuildingPlacementComponent::GetPlacementPreview(FTransform& OutTra
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UAgrarianBuildingPlacementComponent::GetPlacementPreviewState(FTransform& OutTransform, FText& FailureReason) const
|
||||
{
|
||||
if (!GetPlacementPreview(OutTransform, FailureReason))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return CanPlaceAtTransform(ActiveBuildClass, OutTransform, FailureReason);
|
||||
}
|
||||
|
||||
bool UAgrarianBuildingPlacementComponent::CanPlaceAtTransform(TSubclassOf<AActor> BuildClass, const FTransform& PlacementTransform, FText& FailureReason) const
|
||||
{
|
||||
if (!BuildClass)
|
||||
@@ -148,6 +159,38 @@ void UAgrarianBuildingPlacementComponent::ServerPlaceBuildable_Implementation(TS
|
||||
OnBuildPlaced.Broadcast(PlacedActor);
|
||||
}
|
||||
|
||||
void UAgrarianBuildingPlacementComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
|
||||
{
|
||||
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
|
||||
|
||||
if (!bShowGhostPreview || !ActiveBuildClass || !GetWorld())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
FTransform PreviewTransform;
|
||||
FText FailureReason;
|
||||
const bool bCanPlace = GetPlacementPreviewState(PreviewTransform, FailureReason);
|
||||
const FColor PreviewColor = bCanPlace ? ValidGhostPreviewColor : InvalidGhostPreviewColor;
|
||||
const FVector PreviewExtent(
|
||||
FMath::Max(PlacementProbeRadius, 25.0f),
|
||||
FMath::Max(PlacementProbeRadius, 25.0f),
|
||||
FMath::Max(PlacementProbeRadius * 0.5f, 25.0f));
|
||||
|
||||
DrawDebugBox(
|
||||
GetWorld(),
|
||||
PreviewTransform.GetLocation() + FVector(0.0f, 0.0f, PreviewExtent.Z),
|
||||
PreviewExtent,
|
||||
PreviewTransform.GetRotation(),
|
||||
PreviewColor,
|
||||
false,
|
||||
GhostPreviewLifetimeSeconds,
|
||||
0,
|
||||
GhostPreviewLineThickness);
|
||||
|
||||
OnBuildPreviewUpdated.Broadcast(bCanPlace, PreviewTransform, FailureReason);
|
||||
}
|
||||
|
||||
bool UAgrarianBuildingPlacementComponent::HasPlacementCost(FText& FailureReason) const
|
||||
{
|
||||
const UAgrarianInventoryComponent* Inventory = GetOwner() ? GetOwner()->FindComponentByClass<UAgrarianInventoryComponent>() : nullptr;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FAgrarianBuildPlacedSignature, AActor*, PlacedActor);
|
||||
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FAgrarianBuildPlacementFailedSignature, FText, Reason);
|
||||
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FAgrarianBuildPreviewUpdatedSignature, bool, bCanPlace, FTransform, PreviewTransform, FText, FailureReason);
|
||||
|
||||
UCLASS(ClassGroup = (Agrarian), BlueprintType, Blueprintable, meta = (BlueprintSpawnableComponent))
|
||||
class UAgrarianBuildingPlacementComponent : public UActorComponent
|
||||
@@ -24,6 +25,9 @@ public:
|
||||
UPROPERTY(BlueprintAssignable, Category = "Agrarian|Building")
|
||||
FAgrarianBuildPlacementFailedSignature OnBuildPlacementFailed;
|
||||
|
||||
UPROPERTY(BlueprintAssignable, Category = "Agrarian|Building")
|
||||
FAgrarianBuildPreviewUpdatedSignature OnBuildPreviewUpdated;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building")
|
||||
TSubclassOf<AActor> ActiveBuildClass;
|
||||
|
||||
@@ -45,12 +49,30 @@ public:
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building", meta = (EditCondition = "bSnapToGrid", ClampMin = "1"))
|
||||
float GridSize = 50.0f;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building|Preview")
|
||||
bool bShowGhostPreview = true;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building|Preview", meta = (ClampMin = "0"))
|
||||
float GhostPreviewLifetimeSeconds = 0.0f;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building|Preview", meta = (ClampMin = "0"))
|
||||
float GhostPreviewLineThickness = 2.0f;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building|Preview")
|
||||
FColor ValidGhostPreviewColor = FColor(70, 220, 120);
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Building|Preview")
|
||||
FColor InvalidGhostPreviewColor = FColor(230, 80, 65);
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "Agrarian|Building")
|
||||
void SetActiveBuildable(TSubclassOf<AActor> BuildClass, const TArray<FAgrarianItemStack>& Cost);
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "Agrarian|Building")
|
||||
bool GetPlacementPreview(FTransform& OutTransform, FText& FailureReason) const;
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "Agrarian|Building")
|
||||
bool GetPlacementPreviewState(FTransform& OutTransform, FText& FailureReason) const;
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "Agrarian|Building")
|
||||
bool CanPlaceAtTransform(TSubclassOf<AActor> BuildClass, const FTransform& PlacementTransform, FText& FailureReason) const;
|
||||
|
||||
@@ -60,6 +82,8 @@ public:
|
||||
UFUNCTION(Server, Reliable, BlueprintCallable, Category = "Agrarian|Building")
|
||||
void ServerPlaceBuildable(TSubclassOf<AActor> BuildClass, FTransform PlacementTransform);
|
||||
|
||||
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
|
||||
|
||||
protected:
|
||||
bool HasPlacementCost(FText& FailureReason) const;
|
||||
void ConsumePlacementCost();
|
||||
|
||||
Reference in New Issue
Block a user