106 lines
3.0 KiB
C++
106 lines
3.0 KiB
C++
// Project Lab - NHTV Igad
|
|
|
|
#include "UnrealProject.h"
|
|
#include "CombatTextWidget.h"
|
|
#include "CombatTextText.h"
|
|
#include "WidgetLayoutLibrary.h"
|
|
|
|
#define TEXT_LIFETIME 1.0f
|
|
|
|
void UCombatTextWidget::NativeConstruct()
|
|
{
|
|
Super::NativeConstruct();
|
|
|
|
|
|
}
|
|
void UCombatTextWidget::NativeDestruct()
|
|
{
|
|
Super::NativeDestruct();
|
|
}
|
|
|
|
void UCombatTextWidget::NativeTick(const FGeometry& geometry, float deltaTime)
|
|
{
|
|
Super::NativeTick(geometry, deltaTime);
|
|
|
|
UWorld* const world = GetWorld();
|
|
if (!IsValid(world)) return;
|
|
APlayerController* const controller = world->GetFirstPlayerController();
|
|
if (!IsValid(controller)) return;
|
|
|
|
const FVector2D screenSize = FVector2D(world->GetGameViewport()->Viewport->GetSizeXY().X, world->GetGameViewport()->Viewport->GetSizeXY().Y);
|
|
const float viewportScale = UWidgetLayoutLibrary::GetViewportScale(this);
|
|
|
|
|
|
for (int32 i = 0; i < m_combatTexts.Num();)
|
|
{
|
|
// Check the lifetime
|
|
CombatText& text = m_combatTexts[i];
|
|
text.lifeTime += deltaTime;
|
|
if (text.lifeTime >= TEXT_LIFETIME)
|
|
{
|
|
text.textObject->RemoveFromParent();
|
|
m_combatTexts.RemoveAt(i);
|
|
continue;
|
|
}
|
|
|
|
// Set the color
|
|
UCanvasPanelSlot* asSlot = Cast<UCanvasPanelSlot>(text.textObject->Slot);
|
|
if (asSlot)
|
|
{
|
|
const float progress = text.lifeTime / TEXT_LIFETIME;
|
|
text.textObject->SetColor(FLinearColor(text.color.R, text.color.G, text.color.B, text.color.A * (1.0f - progress)));
|
|
|
|
// Update position
|
|
FVector2D position;
|
|
if (controller->ProjectWorldLocationToScreen(text.position + FVector(0, 0, progress * 200), position))
|
|
{
|
|
if (text.arc)
|
|
{
|
|
position -= FVector2D(progress * text.arcDir.X, FMath::Sin(progress * PI) * text.arcDir.Y) * 100;
|
|
asSlot->SetPosition(position / viewportScale);
|
|
}
|
|
else
|
|
asSlot->SetPosition(position / viewportScale);
|
|
}
|
|
}
|
|
|
|
i++;
|
|
}
|
|
}
|
|
|
|
|
|
void UCombatTextWidget::CreateCombatText(const FVector& position, const FString& text, const FLinearColor& color)
|
|
{
|
|
CombatText combatText(position, text, color, false);
|
|
|
|
combatText.textObject = m_CreateTextObject(text, color);
|
|
|
|
m_combatTexts.Add(combatText);
|
|
}
|
|
void UCombatTextWidget::CreateArcingCombatText(const FVector& position, const FString& text, const FLinearColor& color)
|
|
{
|
|
CombatText combatText(position, text, color, true);
|
|
|
|
combatText.arcDir.X = FMath::SRand() * 0.5f + 0.3f;
|
|
combatText.arcDir.Y = FMath::SRand() * 0.5f + 0.3f;
|
|
if (FMath::Rand() % 2 == 0)
|
|
combatText.arcDir.X = -combatText.arcDir.X;
|
|
|
|
combatText.textObject = m_CreateTextObject(text, color);
|
|
|
|
m_combatTexts.Add(combatText);
|
|
}
|
|
|
|
UCombatTextText* UCombatTextWidget::m_CreateTextObject(const FString& text, const FLinearColor& color)
|
|
{
|
|
UCombatTextText* newObject = CreateWidget<UCombatTextText>(GetWorld(), textWidget);
|
|
canvas->AddChild(newObject);
|
|
UCanvasPanelSlot* asSlot = Cast<UCanvasPanelSlot>(newObject->Slot);
|
|
asSlot->SetSize(FVector2D(1000, 500));
|
|
asSlot->SetAlignment(FVector2D(0.5f, 0.05f));
|
|
|
|
newObject->SetText(text);
|
|
newObject->SetColor(color);
|
|
|
|
return newObject;
|
|
} |