feat: add interaction interface, component and test item

Added Interactable - interface for interaction system. Implemented in test item (TestBox).
Created interaction component for character
Added interaction input - not implemented.
This commit is contained in:
Kubson96 2025-02-17 14:46:22 +01:00
parent 75e145c193
commit adddc1352c
8 changed files with 102 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,47 @@
#include "Player/InteractionComponent.h"
UInteractionComponent::UInteractionComponent()
{
PrimaryComponentTick.bCanEverTick = true;
}
void UInteractionComponent::BeginPlay()
{
Super::BeginPlay();
}
void UInteractionComponent::CheckForInteractable()
{
AActor* Owner = GetOwner();
FVector LineStart = Owner->GetActorLocation();
FVector ForwardVector = Owner->GetActorForwardVector();
FVector LineEnd = LineStart + (ForwardVector * InteractionDistance);
FHitResult HitResult;
FCollisionQueryParams QueryParams;
QueryParams.AddIgnoredActor(Owner);
bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, LineStart, LineEnd, ECC_Visibility, QueryParams);
if (bHit && HitResult.GetActor())
{
if (HitResult.GetActor()->Implements<UInteractable>())
{
IInteractable::Execute_Interact(HitResult.GetActor());
}
}
if (bShowDebugLine)
DrawDebugLine(GetWorld(), LineStart, LineEnd, FColor::Red, false, 1.0f, 0, 1.0f);
}
void UInteractionComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
CheckForInteractable();
}

View File

@ -0,0 +1,20 @@
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "Interactable.generated.h"
UINTERFACE(MinimalAPI, Blueprintable)
class UInteractable : public UInterface
{
GENERATED_BODY()
};
class EXO_API IInteractable
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Interaction")
void Interact();
};

View File

@ -0,0 +1,35 @@
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Interfaces/Interactable.h"
#include "InteractionComponent.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class EXO_API UInteractionComponent : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UInteractionComponent();
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Settings")
float InteractionDistance = 100.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Debug")
bool bShowDebugLine = false;
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
private:
void CheckForInteractable();
};