#pragma once
include “CoreMinimal.h”
include “GameFramework/Actor.h”
include “Components/StaticMeshComponent.h”
include “Components/TimelineComponent.h”
include “MyInterface.h”
include “MyDoor.generated.h”
class AMyCharacter;
UCLASS()
class INTHEFRONT_API AMyDoor : public AActor, public IMyInterface
{
GENERATED_BODY()
public:
// Sets default values for this actor’s properties
AMyDoor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
virtual void OnInteractDoor();
void ToggleDoor();
UPROPERTY(VisibleDefaultsOnly, Category = "Mesh")
UStaticMeshComponent* DoorFrame;
UPROPERTY(VisibleDefaultsOnly, Category = "Mesh")
UStaticMeshComponent* Door;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Animation")
UCurveFloat* CurveFloat;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Door")
bool IsDoorOpen = false;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Door")
float DoorRotateAngle = 90.f;
UPROPERTY()
AMyCharacter* MyCharacter;
protected:
FTimeline Timeline;
bool DoorSameSide;
void OpenDoor(float Value);
void SetDoorOnSameSide();
};
include “MyDoor.h”
include “MyCharacter.h”
// Sets default values
AMyDoor::AMyDoor()
{
// 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;
DoorFrame = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("DoorFrame"));
DoorFrame->SetupAttachment(RootComponent);
Door = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Door"));
Door->SetupAttachment(DoorFrame);
}
// Called when the game starts or when spawned
void AMyDoor::BeginPlay()
{
Super::BeginPlay();
if (CurveFloat)
{
FOnTimelineFloat TimelineProgress;
TimelineProgress.BindUFunction(this, FName("OpenDoor"));
Timeline.AddInterpFloat(CurveFloat, TimelineProgress);
}
if (IsDoorOpen)
{
Timeline.Play();
}
}
// Called every frame
void AMyDoor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
Timeline.TickTimeline(DeltaTime);
}
void AMyDoor::OnInteractDoor()
{
ToggleDoor();
IsDoorOpen = !IsDoorOpen;
}
void AMyDoor::ToggleDoor()
{
if (IsDoorOpen)
{
Timeline.Reverse();
}
else
{
if (!Timeline.IsPlaying())
{
SetDoorOnSameSide();
Timeline.Play();
}
}
}
void AMyDoor::OpenDoor(float Value)
{
float Angle = DoorSameSide ? -DoorRotateAngle : DoorRotateAngle;
FRotator Rot = FRotator(0.f, Angle * Value, 0.f);
Door->SetRelativeRotation(Rot);
}
void AMyDoor::SetDoorOnSameSide()
{
if (MyCharacter)
{
FVector CharacterFV = MyCharacter->GetActorForwardVector();
FVector DoorFV = GetActorForwardVector();
DoorSameSide = (FVector::DotProduct(CharacterFV, DoorFV) >= 0);
}
}
How To REPLICATE the Door code On MULTICAST and SERVER???