TriggerActor

This is the actor that the player steps on which triggers the movement component on the transporter that is attached to the Platform Actor
mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.5 kB
2
Indexable
Never
// Sets default values
APressurePlate::APressurePlate()
{
	// 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;

	Activated = false;

	bReplicates = true;
	
	// Root
	RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("RootComp"));
	SetRootComponent(RootComp);

	// Trigger
	TriggerShape = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("TriggerMesh"));
	TriggerShape->SetupAttachment(RootComp);
	TriggerShape->SetIsReplicated(true);
	
	const auto TriggerMeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT(
		"/Game/StarterContent/Shapes/Shape_Cylinder.Shape_Cylinder"));
	if (TriggerMeshAsset.Succeeded())
	{
		TriggerShape->SetStaticMesh(TriggerMeshAsset.Object);
		TriggerShape->SetRelativeScale3D(FVector(3.3f, 3.3f, 0.2f));
		TriggerShape->SetRelativeLocation(FVector(0.0f, 0.0f, 10.0f));
	}

	// Actual Mesh
	Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	Mesh->SetupAttachment(RootComp);
	// Why replicated?
	Mesh->SetIsReplicated(true);
	
	const auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT(
		"/Game/Stylized_Egypt/Meshes/building/SM_building_part_08.SM_building_part_08"));
	if (TriggerMeshAsset.Succeeded())
	{
		Mesh->SetStaticMesh(MeshAsset.Object);
		Mesh->SetRelativeScale3D(FVector(4.0f, 4.0f, 0.5f));
		Mesh->SetRelativeLocation(FVector(0.0f, 0.0f, 7.2f));
	}
}

// Called when the game starts or when spawned
void APressurePlate::BeginPlay()
{
	Super::BeginPlay();

	// Why Replicated? Lol
	SetReplicateMovement(true);

	// Trigger Setup
	TriggerShape->SetVisibility(false);
	TriggerShape->SetCollisionProfileName(FName("OverlapAll"));
	TriggerShape->SetGenerateOverlapEvents(true);

	if (HasAuthority())
	{
		TriggerShape->OnComponentBeginOverlap.AddDynamic(this, &APressurePlate::OnComponentBeginOverlap);
		TriggerShape->OnComponentEndOverlap.AddDynamic(this, &APressurePlate::OnComponentEndOverlap);
		// Make sure that the player isn't already overlapping at game start
		TArray<AActor*> OverlappingActors;
		TriggerShape->GetOverlappingActors(OverlappingActors);
		for (AActor* OtherActor : OverlappingActors)
		{
			HandleBeginOverlappingActor(OtherActor);
			if (Activated)
			{
				break;
			}
		}
	}
}

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

void APressurePlate::OnComponentBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
	UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	if (HasAuthority())
	{
		HandleBeginOverlappingActor(OtherActor);
	}
}

void APressurePlate::OnComponentEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
	UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	// Only the one that first Activated it can DeActivate it
	if (HasAuthority() && OtherActor == CurrentTriggerActor)
	{
		if (Activated)
		{
			Activated = false;
			CurrentTriggerActor = nullptr;
			PressurePlateOnDeactivated.Broadcast();
		}
	}
}

void APressurePlate::HandleBeginOverlappingActor(AActor* OtherActor)
{
	// Only Activate if there isn't a Current Actor Standing on it
	if (OtherActor->ActorHasTag("TriggerActor") && !CurrentTriggerActor)
	{
		if (!Activated)
		{
			CurrentTriggerActor = OtherActor;
			Activated = true;
			PressurePlateOnActivated.Broadcast();
		}
	}
}
Leave a Comment