HAxis sos
This commit is contained in:
22
Source/UnrealProject/GUI/CombatText/CombatTextText.cpp
Normal file
22
Source/UnrealProject/GUI/CombatText/CombatTextText.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
// Project Lab - NHTV Igad
|
||||
|
||||
#include "UnrealProject.h"
|
||||
#include "CombatTextText.h"
|
||||
|
||||
|
||||
|
||||
void UCombatTextText::NativeConstruct()
|
||||
{
|
||||
Super::NativeConstruct();
|
||||
lifeTime = 0;
|
||||
}
|
||||
|
||||
|
||||
void UCombatTextText::SetText_Implementation(const FString& text)
|
||||
{
|
||||
|
||||
}
|
||||
void UCombatTextText::SetColor_Implementation(const FLinearColor& color)
|
||||
{
|
||||
|
||||
}
|
||||
29
Source/UnrealProject/GUI/CombatText/CombatTextText.h
Normal file
29
Source/UnrealProject/GUI/CombatText/CombatTextText.h
Normal file
@@ -0,0 +1,29 @@
|
||||
// Project Lab - NHTV Igad
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Blueprint/UserWidget.h"
|
||||
#include "CombatTextText.generated.h"
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
UCLASS()
|
||||
class UNREALPROJECT_API UCombatTextText : public UUserWidget
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
virtual void NativeConstruct();
|
||||
|
||||
UFUNCTION(BlueprintNativeEvent, Category = "CombatText")
|
||||
void SetText(const FString& text);
|
||||
|
||||
UFUNCTION(BlueprintNativeEvent, Category = "CombatText")
|
||||
void SetColor(const FLinearColor& color);
|
||||
|
||||
|
||||
float lifeTime;
|
||||
FVector position;
|
||||
};
|
||||
106
Source/UnrealProject/GUI/CombatText/CombatTextWidget.cpp
Normal file
106
Source/UnrealProject/GUI/CombatText/CombatTextWidget.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
// 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;
|
||||
}
|
||||
54
Source/UnrealProject/GUI/CombatText/CombatTextWidget.h
Normal file
54
Source/UnrealProject/GUI/CombatText/CombatTextWidget.h
Normal file
@@ -0,0 +1,54 @@
|
||||
// Project Lab - NHTV Igad
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Blueprint/UserWidget.h"
|
||||
#include "CombatTextWidget.generated.h"
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
UCLASS()
|
||||
class UNREALPROJECT_API UCombatTextWidget : public UUserWidget
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
virtual void NativeConstruct();
|
||||
virtual void NativeDestruct();
|
||||
virtual void NativeTick(const FGeometry& geometry, float deltaTime) override;
|
||||
|
||||
UPROPERTY(EditAnywhere, Category = "CombatText")
|
||||
TSubclassOf<class UCombatTextText> textWidget;
|
||||
|
||||
UPROPERTY(BlueprintReadWrite, Category = "CombatText")
|
||||
class UCanvasPanel* canvas;
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "CombatText")
|
||||
void CreateCombatText(const FVector& position, const FString& text, const FLinearColor& color);
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "CombatText")
|
||||
void CreateArcingCombatText(const FVector& position, const FString& text, const FLinearColor& color);
|
||||
|
||||
|
||||
private:
|
||||
struct CombatText
|
||||
{
|
||||
CombatText(const FVector& position, const FString& text, const FLinearColor& color, bool arc) : position(position), text(text), color(color), lifeTime(0), arc(arc) {}
|
||||
FVector position;
|
||||
FString text;
|
||||
FLinearColor color;
|
||||
float lifeTime;
|
||||
bool arc;
|
||||
FVector2D arcDir;
|
||||
|
||||
UCombatTextText* textObject;
|
||||
};
|
||||
|
||||
TArray<CombatText> m_combatTexts;
|
||||
|
||||
UCombatTextText* m_CreateTextObject(const FString& text, const FLinearColor& color);
|
||||
};
|
||||
Reference in New Issue
Block a user