88 lines
3.0 KiB
C++
88 lines
3.0 KiB
C++
#include "Pawn/Public/Interaction/PwnTrigger.h"
|
|
|
|
#include "Components/ShapeComponent.h"
|
|
#include "Pawn/Public/Interaction/PwnTriggerRegister.h"
|
|
|
|
UPwnTrigger::UPwnTrigger() {
|
|
PrimaryComponentTick.bCanEverTick = false;
|
|
bAutoActivate = true;
|
|
}
|
|
|
|
void UPwnTrigger::BeginPlay() {
|
|
Super::BeginPlay();
|
|
VolumeComponent = Cast<UShapeComponent>(TriggerVolume.GetComponent(GetOwner()));
|
|
if (ensure(VolumeComponent)) {
|
|
VolumeComponent->OnComponentBeginOverlap.AddDynamic(this, &UPwnTrigger::OnOverlapBegin);
|
|
VolumeComponent->OnComponentEndOverlap.AddDynamic(this, &UPwnTrigger::OnOverlapEnd);
|
|
}
|
|
}
|
|
|
|
void UPwnTrigger::EndPlay(const EEndPlayReason::Type EndPlayReason) {
|
|
Super::EndPlay(EndPlayReason);
|
|
if (VolumeComponent) {
|
|
VolumeComponent->OnComponentBeginOverlap.RemoveDynamic(this, &UPwnTrigger::OnOverlapBegin);
|
|
VolumeComponent->OnComponentEndOverlap.RemoveDynamic(this, &UPwnTrigger::OnOverlapEnd);
|
|
}
|
|
}
|
|
|
|
void UPwnTrigger::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
|
|
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep,
|
|
const FHitResult& SweepResult) {
|
|
if (UPwnTriggerRegister* Register = OtherActor->GetComponentByClass<UPwnTriggerRegister>()) {
|
|
Register->RegisterTrigger(this);
|
|
OnTriggerEnter.Broadcast(OtherActor);
|
|
ExecuteActions(EnterActions);
|
|
}
|
|
}
|
|
|
|
void UPwnTrigger::OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
|
|
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) {
|
|
if (UPwnTriggerRegister* Register = OtherActor->GetComponentByClass<UPwnTriggerRegister>()) {
|
|
Register->UnregisterTrigger(this);
|
|
OnTriggerExit.Broadcast(OtherActor);
|
|
ExecuteActions(ExitActions);
|
|
}
|
|
}
|
|
|
|
void UPwnTrigger::Activate(const bool bReset) {
|
|
if (bReset || ShouldActivate() == true) {
|
|
VolumeComponent->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
|
|
SetActiveFlag(true);
|
|
OnComponentActivated.Broadcast(this, bReset);
|
|
}
|
|
}
|
|
|
|
void UPwnTrigger::Deactivate() {
|
|
if (ShouldActivate() == false) {
|
|
VolumeComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
|
|
SetActiveFlag(false);
|
|
OnComponentDeactivated.Broadcast(this);
|
|
}
|
|
}
|
|
|
|
void UPwnTrigger::Interact_Implementation(const EInteractionType InteractionType) {
|
|
if (InteractionType == Enable) {
|
|
Activate(true);
|
|
} else if (InteractionType == Disable) {
|
|
Deactivate();
|
|
} else if (InteractionType == Toggle) {
|
|
ExecuteActions(ManualActions);
|
|
}
|
|
}
|
|
|
|
FVector UPwnTrigger::GetVolumeCenter() const {
|
|
if (ensure(VolumeComponent)) {
|
|
VolumeComponent->Bounds.Origin;
|
|
}
|
|
return FVector::ZeroVector;
|
|
}
|
|
|
|
void UPwnTrigger::ExecuteActions(const FInteractableActions& InteractableActions) const {
|
|
for (const FInteractableAction &Action : InteractableActions.Actions) {
|
|
Execute_Interact(Action.Interactable.ActorReference.TryLoad(), Action.InteractionType);
|
|
}
|
|
if (InteractableActions.SelfInteract) {
|
|
Execute_Interact(GetOwner(), InteractableActions.SelfInteractionType);
|
|
}
|
|
}
|