Add Agrarian debug HUD

This commit is contained in:
2026-05-11 01:28:50 -07:00
parent 6e7f74986c
commit 474b92b1ee
5 changed files with 115 additions and 6 deletions
+76
View File
@@ -0,0 +1,76 @@
// Copyright Pacificao. All Rights Reserved.
#include "AgrarianDebugHUD.h"
#include "AgrarianGameCharacter.h"
#include "AgrarianInventoryComponent.h"
#include "AgrarianSurvivalComponent.h"
void AAgrarianDebugHUD::DrawHUD()
{
Super::DrawHUD();
if (!bShowDebugHUD || !Canvas)
{
return;
}
const AAgrarianGameCharacter* AgrarianCharacter = Cast<AAgrarianGameCharacter>(GetOwningPawn());
if (!AgrarianCharacter)
{
return;
}
float Y = 32.0f;
constexpr float X = 32.0f;
DrawLine(TEXT("AGRARIAN DEV HUD"), X, Y, FColor(160, 220, 140));
DrawSurvival(AgrarianCharacter->GetSurvivalComponent(), X, Y);
DrawInventory(AgrarianCharacter->GetInventoryComponent(), X, Y);
}
void AAgrarianDebugHUD::DrawSurvival(const UAgrarianSurvivalComponent* SurvivalComponent, float X, float& Y) const
{
if (!SurvivalComponent)
{
DrawLine(TEXT("Survival: unavailable"), X, Y, FColor::Red);
return;
}
const FAgrarianSurvivalSnapshot& Survival = SurvivalComponent->Survival;
DrawLine(FString::Printf(TEXT("Health: %.0f"), Survival.Health), X, Y);
DrawLine(FString::Printf(TEXT("Stamina: %.0f"), Survival.Stamina), X, Y);
DrawLine(FString::Printf(TEXT("Hunger: %.0f"), Survival.Hunger), X, Y);
DrawLine(FString::Printf(TEXT("Thirst: %.0f"), Survival.Thirst), X, Y);
DrawLine(FString::Printf(TEXT("Temp: %.1f C"), Survival.BodyTemperature), X, Y);
DrawLine(FString::Printf(TEXT("Injury: %.0f"), Survival.InjurySeverity), X, Y);
Y += 10.0f * TextScale;
}
void AAgrarianDebugHUD::DrawInventory(const UAgrarianInventoryComponent* InventoryComponent, float X, float& Y) const
{
if (!InventoryComponent)
{
DrawLine(TEXT("Inventory: unavailable"), X, Y, FColor::Red);
return;
}
DrawLine(FString::Printf(TEXT("Inventory: %d/%d slots"), InventoryComponent->Items.Num(), InventoryComponent->MaxSlots), X, Y, FColor(160, 220, 140));
if (InventoryComponent->Items.IsEmpty())
{
DrawLine(TEXT("- empty"), X, Y, FColor::Silver);
return;
}
for (const FAgrarianItemStack& Stack : InventoryComponent->Items)
{
const FText DisplayName = Stack.DisplayName.IsEmpty() ? FText::FromName(Stack.ItemId) : Stack.DisplayName;
DrawLine(FString::Printf(TEXT("- %s x%d"), *DisplayName.ToString(), Stack.Quantity), X, Y);
}
}
void AAgrarianDebugHUD::DrawLine(const FString& Text, float X, float& Y, const FColor& Color) const
{
DrawText(Text, Color, X, Y, nullptr, TextScale, false);
Y += 18.0f * TextScale;
}
+30
View File
@@ -0,0 +1,30 @@
// Copyright Pacificao. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/HUD.h"
#include "AgrarianDebugHUD.generated.h"
class UAgrarianInventoryComponent;
class UAgrarianSurvivalComponent;
UCLASS()
class AAgrarianDebugHUD : public AHUD
{
GENERATED_BODY()
public:
virtual void DrawHUD() override;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|HUD")
bool bShowDebugHUD = true;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|HUD", meta = (ClampMin = "0.25"))
float TextScale = 1.0f;
protected:
void DrawSurvival(const UAgrarianSurvivalComponent* SurvivalComponent, float X, float& Y) const;
void DrawInventory(const UAgrarianInventoryComponent* InventoryComponent, float X, float& Y) const;
void DrawLine(const FString& Text, float X, float& Y, const FColor& Color = FColor::White) const;
};
@@ -1,9 +1,11 @@
// Copyright Epic Games, Inc. All Rights Reserved.
#include "AgrarianGameGameMode.h"
#include "AgrarianDebugHUD.h"
#include "AgrarianGameState.h"
AAgrarianGameGameMode::AAgrarianGameGameMode()
{
GameStateClass = AAgrarianGameState::StaticClass();
HUDClass = AAgrarianDebugHUD::StaticClass();
}