15#include "Camera/PlayerCameraManager.h"
16#include "GameFramework/PlayerController.h"
18UInteractionSystem::UInteractionSystem()
20 PrimaryComponentTick.bCanEverTick =
true;
21 PrimaryComponentTick.TickGroup = TG_PostUpdateWork;
24void UInteractionSystem::BeginPlay()
29 OwnerPlayer = Cast<APlayerActor>(GetOwner());
32 PRINTLOG( TEXT(
"UInteractionSystem: Owner is not APlayerActor!"));
33 SetComponentTickEnabled(
false);
37void UInteractionSystem::TickComponent(
float DeltaTime, ELevelTick TickType,
38 FActorComponentTickFunction* ThisTickFunction)
40 Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
43 CurrentTarget = DetectInteractableTarget();
46 if (bShowDebugInfo && CurrentTarget)
48 CurrentTarget->ShowDebugInfo(OwnerPlayer);
52void UInteractionSystem::TryInteract()
54 if (!CurrentTarget || !CurrentTarget->bCanInteract)
61 switch (CurrentTarget->InteractionType)
68 case EInteractionType::Button:
70 PRINTLOG(TEXT(
"UInteractionSystem: Interacting with %s, %s"), *GetOwner()->GetName(), *OwnerPlayer->GetName());
71 CurrentTarget->TriggerInteraction(OwnerPlayer);
76 case EInteractionType::Kiosk:
77 PRINTLOG(TEXT(
"UInteractionSystem: Interacting with %s"), *GetOwner()->GetName());
78 if (
APlayerControl* pc = Cast<APlayerControl>(OwnerPlayer->GetController()))
80 pc->Client_InteractKiosk();
89void UInteractionSystem::TryPickUp()
92 if (HoldingInteractable)
98 if (!CurrentTarget || CurrentTarget->InteractionType != EInteractionType::PickUp)
102 if (!IsValid(CurrentTarget))
104 PRINTLOG( TEXT(
"UInteractionSystem: CurrentTarget is invalid!"));
105 CurrentTarget =
nullptr;
109 AActor* TargetOwner = CurrentTarget->GetOwner();
110 if (!TargetOwner || !IsValid(TargetOwner))
112 PRINTLOG( TEXT(
"UInteractionSystem: Target owner is null or invalid!"));
117 PRINTLOG( TEXT(
"UInteractionSystem: Picking up %s"), *TargetOwner->GetName());
119 HoldingInteractable = CurrentTarget;
120 HoldingInteractable->PickUp(OwnerPlayer);
125void UInteractionSystem::TryDrop()
127 if (!HoldingInteractable)
131 if (!IsValid(HoldingInteractable))
133 PRINTLOG( TEXT(
"UInteractionSystem: HoldingInteractable is invalid!"));
134 HoldingInteractable =
nullptr;
139 AActor* DroppedOwner = HoldingInteractable->GetOwner();
140 if (DroppedOwner && IsValid(DroppedOwner))
142 PRINTLOG( TEXT(
"UInteractionSystem: Dropping %s"), *DroppedOwner->GetName());
145 HoldingInteractable->Drop();
146 HoldingInteractable =
nullptr;
148 PRINTLOG( TEXT(
"UInteractionSystem: Drop complete"));
151void UInteractionSystem::RegisterInteractable(UInteractableComponent* Interactable)
153 if (!Interactable || !IsValid(Interactable))
155 PRINTLOG( TEXT(
"UInteractionSystem: Attempted to register null/invalid interactable"));
159 if (NearbyInteractables.Contains(Interactable))
162 NearbyInteractables.Add(Interactable);
165void UInteractionSystem::UnregisterInteractable(UInteractableComponent* Interactable)
170 NearbyInteractables.Remove(Interactable);
173 if (CurrentTarget == Interactable)
175 CurrentTarget =
nullptr;
179USceneComponent* UInteractionSystem::GetHoldPosition()
const
181 return OwnerPlayer ? OwnerPlayer->HoldPosition :
nullptr;
184UInteractableComponent* UInteractionSystem::DetectInteractableTarget()
187 if (NearbyInteractables.Num() == 0)
191 FHitResult HitResult;
192 if (!PerformCenterLineTrace(HitResult))
196 AActor* HitActor = HitResult.GetActor();
200 UInteractableComponent* InteractComp = HitActor->FindComponentByClass<UInteractableComponent>();
205 if (NearbyInteractables.Contains(InteractComp))
213bool UInteractionSystem::PerformCenterLineTrace(FHitResult& OutHit)
219 APlayerController* PC = Cast<APlayerController>(OwnerPlayer->GetController());
223 APlayerCameraManager* CameraManager = PC->PlayerCameraManager;
227 FVector CameraLocation = CameraManager->GetCameraLocation();
228 FVector CameraForward = CameraManager->GetCameraRotation().Vector();
231 FVector TraceStart = CameraLocation;
232 FVector TraceEnd = TraceStart + (CameraForward * InteractionDistance);
235 bool bHit = GetWorld()->LineTraceSingleByChannel(
236 OutHit, TraceStart, TraceEnd, ECC_Visibility
243 GetWorld(), TraceStart, TraceEnd,
244 bHit ? FColor::Green : FColor::
Red,
Declares the player-controlled character actor.
APlayerControl 선언에 대한 Doxygen 주석을 제공합니다.
YiSan 전반에서 사용하는 공용 인터페이스를 선언합니다.
#define PRINTLOG(fmt,...)
UGameSoundManager 클래스를 선언합니다.