Files
WizardingHub/Source/WizardingCentral/StickySlime/StickySlimeComponent.cpp
T
2025-05-05 22:06:18 -04:00

126 lines
3.1 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "StickySlimeComponent.h"
#include "SlimableInterface.h"
#include "SlimeAnchorInterface.h"
// Sets default values for this component's properties
UStickySlimeComponent::UStickySlimeComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = false;
// ...
// Initialize
AttachedActor1 = nullptr;
AttachedActor2 = nullptr;
bIsActivated = false;
}
// Called when the game starts
void UStickySlimeComponent::BeginPlay()
{
Super::BeginPlay();
// ...
}
void UStickySlimeComponent::AttachToActor(AActor* Target)
{
if (!Target || this->AttachedActor1 == Target || this->AttachedActor2 == Target)
{
return;
}
if (bIsActivated)
{
return;
}
if (this->AttachedActor1)
{
UE_LOG(LogTemp, Warning, TEXT("Attaching to actor"));
this->ActivateIfAnchored(Target);
}
UE_LOG(LogTemp, Warning, TEXT("Attaching to actor"));
this->AttachedActor1 = Target;
this->GetOwner()->AttachToComponent(Target->GetRootComponent(),
FAttachmentTransformRules::KeepWorldTransform);
}
void UStickySlimeComponent::ActivateIfAnchored(AActor* Target)
{
if (!Target || this->AttachedActor1 == Target || this->AttachedActor2 == Target)
{
return;
}
if (bIsActivated)
{
return;
}
this->AttachedActor2 = Target;
const bool bA1IsAnchor = this->AttachedActor1->GetClass()->
ImplementsInterface(USlimeAnchorInterface::StaticClass());
const bool bA2IsAnchor = this->AttachedActor2->GetClass()->
ImplementsInterface(USlimeAnchorInterface::StaticClass());
if (!bA1IsAnchor && !bA2IsAnchor)
{
this->AttachedActor2 = nullptr;
return;
}
bIsActivated = true;
if (bA1IsAnchor && this->AttachedActor2->GetClass()->ImplementsInterface(USlimableInterface::StaticClass()))
{
ISlimableInterface::Execute_OnSlimed(this->AttachedActor2, this->GetOwner());
}
else if (bA2IsAnchor && this->AttachedActor1->GetClass()->ImplementsInterface(USlimableInterface::StaticClass()))
{
ISlimableInterface::Execute_OnSlimed(this->AttachedActor1, this->GetOwner());
}
else
{
this->BreakSlime();
}
}
void UStickySlimeComponent::BreakSlime()
{
if (!bIsActivated)
{
return;
}
if (this->AttachedActor1->GetClass()->ImplementsInterface(USlimableInterface::StaticClass()))
{
ISlimableInterface::Execute_OnUnslimed(this->AttachedActor1);
}
if (this->AttachedActor2->GetClass()->ImplementsInterface(USlimableInterface::StaticClass()))
{
ISlimableInterface::Execute_OnUnslimed(this->AttachedActor2);
}
this->AttachedActor1 = nullptr;
this->AttachedActor2 = nullptr;
this->bIsActivated = false;
}
// // Called every frame
// void UStickySlimeComponent::TickComponent(float DeltaTime, ELevelTick TickType,
// FActorComponentTickFunction* ThisTickFunction)
// {
// Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
//
// // ...
// }