Skip to content

Commit

Permalink
Combat text (#4)
Browse files Browse the repository at this point in the history
- Added the Combat Text Widget
- Wrapped some player classes in blueprints
- Added OnCollision callbacks during movement
- Added all the necessary event bindings to set the text of the combat
text on hit
  • Loading branch information
tanis2000 authored Sep 13, 2023
1 parent 874d189 commit 569b17a
Show file tree
Hide file tree
Showing 22 changed files with 579 additions and 44 deletions.
98 changes: 64 additions & 34 deletions .idea/.idea.Test2DPlatformer.dir/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Content/Blueprints/BP_PlatformerGameMode.uasset
Binary file not shown.
Binary file added Content/Characters/Player/BP_Hero.uasset
Binary file not shown.
Binary file added Content/Feedback/BP_CombatText.uasset
Binary file not shown.
Binary file added Content/Feedback/MAT_CombatText.uasset
Binary file not shown.
Binary file added Content/Feedback/WBP_CombatText.uasset
Binary file not shown.
Binary file modified Content/Maps/Level.umap
Binary file not shown.
Binary file modified Content/Maps/Main.umap
Binary file not shown.
Binary file modified Content/Sprites/HeroSprite.uasset
Binary file not shown.
Binary file added Content/Weapons/BP_Bullet.uasset
Binary file not shown.
30 changes: 29 additions & 1 deletion Source/Test2DPlatformer/BasePawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ bool ABasePawn::MoveH(float moveH) {
if (entity != nullptr)
{
SubPixelCounter.X = 0.0f;
OnCollide(entity);
if (entity->IsA(ABasePawn::StaticClass()))
{
ABasePawn *BasePawn = Cast<ABasePawn>(entity);
BasePawn->OnCollide(entity);
}
/*
if (onCollide != nullptr)
{
Expand Down Expand Up @@ -79,6 +85,12 @@ bool ABasePawn::MoveV(float moveV) {
if (entity != nullptr)
{
SubPixelCounter.Z = 0.0f;
OnCollide(entity);
if (entity->IsA(ABasePawn::StaticClass()))
{
ABasePawn *BasePawn = Cast<ABasePawn>(entity);
BasePawn->OnCollide(entity);
}
/*
if (onCollide != nullptr)
{
Expand All @@ -102,6 +114,12 @@ bool ABasePawn::MoveV(float moveV) {
if (entity != nullptr)
{
SubPixelCounter.Z = 0.0f;
OnCollide(entity);
if (entity->IsA(ABasePawn::StaticClass()))
{
ABasePawn *BasePawn = Cast<ABasePawn>(entity);
BasePawn->OnCollide(entity);
}
/*
if (onCollide != nullptr)
{
Expand All @@ -113,6 +131,12 @@ bool ABasePawn::MoveV(float moveV) {
if (!IgnoreJumpThrus && ((entity = CollideFirst(TEXT("JumpThru"), GetActorLocation().X, GetActorLocation().Z + 1.0f)) != nullptr))
{
SubPixelCounter.Z = 0.0f;
OnCollide(entity);
if (entity->IsA(ABasePawn::StaticClass()))
{
ABasePawn *BasePawn = Cast<ABasePawn>(entity);
BasePawn->OnCollide(entity);
}
/*
if (onCollide != nullptr)
{
Expand Down Expand Up @@ -220,4 +244,8 @@ FVector ABasePawn::ActualPosition()
{
FVector location = GetActorLocation();
return FVector(location.X + SubPixelCounter.X, location.Y + SubPixelCounter.Y, location.Z + SubPixelCounter.Z);
}
}

void ABasePawn::OnCollide(AActor* Actor)
{
}
1 change: 1 addition & 0 deletions Source/Test2DPlatformer/BasePawn.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ class TEST2DPLATFORMER_API ABasePawn : public APawn

FVector ActualPosition();

virtual void OnCollide(AActor *Actor);
};
91 changes: 91 additions & 0 deletions Source/Test2DPlatformer/Feedback/PopText.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Fill out your copyright notice in the Description page of Project Settings.


#include "PopText.h"

#include "PopTextWidget.h"
#include "Kismet/GameplayStatics.h"


// Sets default values
APopText::APopText()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;

Billboard = CreateDefaultSubobject<UBillboardComponent>(TEXT("Billboard"));
RootComponent = Billboard;

Duration = 1.5f;
ZFinalOffset = 10;
}

// Called when the game starts or when spawned
void APopText::BeginPlay()
{
Super::BeginPlay();
UE_LOG(LogTemp, Display, TEXT("Attempting to create widget"));
CurrentWidget = CreateWidget<UPopTextWidget>(GetWorld(), StartingWidgetClass);
CurrentWidget->AddToViewport();

CurrentDuration = Duration;
CurrentZOffset = 0;
}

// Called every frame
void APopText::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
UpdatePosition(DeltaTime);
}

void APopText::UpdatePosition(float DeltaTime)
{
if (CurrentWidget == nullptr)
{
return;
}

CurrentDuration -= DeltaTime;
if (CurrentDuration <= 0)
{
CurrentWidget->RemoveFromParent();
CurrentWidget = nullptr;
return;
}

FVector WorldLocation = GetActorLocation();
const float Ratio = (Duration - CurrentDuration) / Duration;
CurrentZOffset = ZFinalOffset * Ratio;
WorldLocation.Z += CurrentZOffset;

FVector2d ScreenPosition;
UGameplayStatics::ProjectWorldToScreen(GetWorld()->GetFirstPlayerController(), WorldLocation, ScreenPosition);
CurrentWidget->SetPositionInViewport(ScreenPosition);
}

void APopText::SetContent(FString Content)
{
if (CurrentWidget == nullptr)
{
return;
}
CurrentWidget->SetContent(Content);
}

// void APopText::ChangeWidget(TSubclassOf<UUserWidget> NewWidgetClass)
// {
// if (CurrentWidget != nullptr)
// {
// CurrentWidget->RemoveFromViewport();
// CurrentWidget = nullptr;
// }
// if (NewWidgetClass != nullptr)
// {
// CurrentWidget = CreateWidget<UUserWidget>(GetWorld(), NewWidgetClass);
// if (CurrentWidget != nullptr)
// {
// CurrentWidget->AddToViewport();
// }
// }
// }
53 changes: 53 additions & 0 deletions Source/Test2DPlatformer/Feedback/PopText.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/BillboardComponent.h"
#include "Components/WidgetComponent.h"
#include "GameFramework/Actor.h"
#include "PopText.generated.h"

class UPopTextWidget;

UCLASS()
class TEST2DPLATFORMER_API APopText : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor's properties
APopText();

UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UBillboardComponent> Billboard;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSubclassOf<UPopTextWidget> StartingWidgetClass;

UPROPERTY(EditAnywhere, BlueprintReadOnly)
TObjectPtr<UPopTextWidget> CurrentWidget;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
float Duration;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
float ZFinalOffset;

float CurrentDuration;
float CurrentZOffset;
protected:



// Called when the game starts or when spawned
virtual void BeginPlay() override;

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

void UpdatePosition(float DeltaTime);

void SetContent(FString Content);
};
4 changes: 4 additions & 0 deletions Source/Test2DPlatformer/Feedback/PopTextWidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Fill out your copyright notice in the Description page of Project Settings.


#include "PopTextWidget.h"
21 changes: 21 additions & 0 deletions Source/Test2DPlatformer/Feedback/PopTextWidget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "PopTextWidget.generated.h"

/**
*
*/
UCLASS()
class TEST2DPLATFORMER_API UPopTextWidget : public UUserWidget
{
GENERATED_BODY()

public:
UFUNCTION(BlueprintImplementableEvent, Category="Something")
void SetContent(const FString &Content);

};
Loading

0 comments on commit 569b17a

Please sign in to comment.