Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.6 kB
10
Indexable
Never
// Fill out your copyright notice in the Description page of Project Settings.


#include "World/Blueprints/Components/Transporter.h"
#include "World/Blueprints/PressurePlate.h"


// Sets default values for this component's properties
UTransporter::UTransporter()
{
	// 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;

	SetIsReplicatedByDefault(true);

	MoveTime = 3.0f;
	ActivatedTriggerCount = 0;

	ArePointsSet = false;
	StartPoint = FVector::Zero();
	EndPoint = FVector::Zero();
	
	TimerRate = 0.01f;
}


// Called when the game starts
void UTransporter::BeginPlay()
{
	Super::BeginPlay();

	OwningActorRef = GetOwner();
	if (OwningActorRef->HasAuthority())
	{
		for (AActor* TA : TriggerActors)
		{
			APressurePlate* PressurePlateActor = Cast<APressurePlate>(TA);
			if (PressurePlateActor)
			{
				PressurePlateActor->PressurePlateOnActivated.AddDynamic(this, &UTransporter::OnPressurePlateActivated);
				PressurePlateActor->PressurePlateOnDeactivated.AddDynamic(this, &UTransporter::OnPressurePlateDeactivated);
			}
		}
	}
}

void UTransporter::SetPoints(const FVector& Point1, const FVector& Point2)
{
	if (Point1.Equals(Point2))
	{
		return;
	}
	
	StartPoint = Point1;
	EndPoint = Point2;
	ArePointsSet = true;
}

void UTransporter::OnPressurePlateActivated()
{
	if (OwningActorRef && OwningActorRef->HasAuthority())
	{
		ActivatedTriggerCount++;

		AllTriggerActorsTriggered = ActivatedTriggerCount >= TriggerActors.Num();
		FString Msg = FString::Printf(TEXT("Transporter Activated: %d"), ActivatedTriggerCount);
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::White, Msg);

		if (AllTriggerActorsTriggered)
		{
			// if (!OwningActorRef->GetWorldTimerManager().IsTimerActive(TriggerMovementHandle))
			// Distance Divide by Time Equals Speed (Rate of Travel)
			Speed = FVector::Distance(StartPoint, EndPoint) / MoveTime;
			
			OwningActorRef->GetWorldTimerManager().SetTimer(
				TriggerMovementHandle, this,
				&UTransporter::ActivateTransporterMovement,
				TimerRate, true, -1);
		}
	}
}

void UTransporter::OnPressurePlateDeactivated()
{
	if (OwningActorRef && OwningActorRef->HasAuthority())
	{
		ActivatedTriggerCount--;

		AllTriggerActorsTriggered = ActivatedTriggerCount >= TriggerActors.Num();
		FString Msg = FString::Printf(TEXT("Transporter Activated: %d"), ActivatedTriggerCount);
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::White, Msg);

		if (!AllTriggerActorsTriggered)
		{
			// Distance Divide by Time Equals Speed (Rate of Travel)
			Speed = FVector::Distance(StartPoint, EndPoint) / MoveTime;
			
			OwningActorRef->GetWorldTimerManager().SetTimer(
				TriggerMovementHandle, this,
				&UTransporter::ActivateTransporterMovement,
				TimerRate, true, -1);
		}
	}
}

void UTransporter::ActivateTransporterMovement()
{
	const FVector CurrentLocation = OwningActorRef->GetActorLocation();
	
	// Ternary
	const FVector TargetLocation = AllTriggerActorsTriggered ? EndPoint : StartPoint;
	if (!CurrentLocation.Equals(TargetLocation))
	{
		const float DeltaTime = OwningActorRef->GetWorld()->DeltaTimeSeconds;
		const FVector NewLocation = FMath::VInterpConstantTo(CurrentLocation, TargetLocation, DeltaTime, Speed);

		OwningActorRef->SetActorLocation(NewLocation);
	}
	else
	{
		OwningActorRef->GetWorldTimerManager().ClearTimer(TriggerMovementHandle);
	}
}
Leave a Comment