CTimeOfDayHandler.h

더보기


#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CTimeOfDayHandler.generated.h"


//멀티캐스트 델리게이트 파라미터(시그니쳐함수이름,파라미터1,파라미터2)
DECLARE_MULTICAST_DELEGATE_TwoParams(FOnItmeChangedStignature,int32,int32)
UCLASS()
class WILLIAM_API ACTimeOfDayHandler : public AActor
{
GENERATED_BODY()


public:
//시그니쳐로 델리게이트를 만든다.
FOnItmeChangedStignature OnTimeChanged;


UPROPERTY()
int32 TimeScale;
UPROPERTY()
int32 Hours;
UPROPERTY()
int32 Minutes;
UPROPERTY()
float ElapsedSeconds;

public:

ACTimeOfDayHandler();

protected:
virtual void BeginPlay() override;

public:
virtual void Tick(float DeltaTime) override;

};

 

CTimeOfDayHandler.cpp

더보기

#include "CTimeOfDayHandler.h"

ACTimeOfDayHandler::ACTimeOfDayHandler()
{
PrimaryActorTick.bCanEverTick = true;
//프로퍼티 초기화
TimeScale = 60;
Hours = 0;
Minutes = 0;
ElapsedSeconds = 0;
}

void ACTimeOfDayHandler::BeginPlay()
{
Super::BeginPlay();
}

void ACTimeOfDayHandler::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
ElapsedSeconds += (DeltaTime*TimeScale);
if(ElapsedSeconds>60)
{
ElapsedSeconds -= 60;
Minutes++;
if(Minutes>60)
{
Minutes -= 60;
Hours++;
}
}
OnTimeChanged.Broadcast(Hours, Minutes);
}

 

ACClock.h

더보기



#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CClock.generated.h"

UCLASS()
class WILLIAM_API ACClock : public AActor
{
GENERATED_BODY()

public:
UPROPERTY()
USceneComponent* RootSceneComponent;
UPROPERTY()
UStaticMeshComponent* ClockFace;

UPROPERTY()
USceneComponent* HourHandle;
UPROPERTY()
UStaticMeshComponent* HourHand;

UPROPERTY()
USceneComponent* MinuteHandle;
UPROPERTY()
UStaticMeshComponent* MinuteHand;



UFUNCTION()
void TimeChanged(int32 Hours, int32 Minutes);

FDelegateHandle MyDelegateHandle;

public:
ACClock();

protected:
virtual void BeginPlay() override;

public:
virtual void Tick(float DeltaTime) override;

};

ACClock.cpp

 

더보기


#include "CClock.h"

#include "Kismet/GameplayStatics.h"
#include "CTimeOfDayHandler.h"


ACClock::ACClock()
{
  PrimaryActorTick.bCanEverTick = true;

RootSceneComponent = CreateDefaultSubobject<USceneComponent>("RootSceneComponent");
ClockFace = CreateDefaultSubobject<UStaticMeshComponent>("ClockFace");

HourHand = CreateDefaultSubobject<UStaticMeshComponent>("HourHand");
MinuteHand = CreateDefaultSubobject<UStaticMeshComponent>("MinuteHand");

HourHandle = CreateDefaultSubobject<USceneComponent>("HourHandle");
MinuteHandle = CreateDefaultSubobject<USceneComponent>("MinuteHandle");

auto meshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/02/Sphere1.Sphere1'"));

if(meshAsset.Object !=nullptr)
{
ClockFace->SetStaticMesh(meshAsset.Object);
HourHand->SetStaticMesh(meshAsset.Object);
MinuteHand->SetStaticMesh(meshAsset.Object);
}

RootComponent = RootSceneComponent;
HourHand->AttachTo(HourHandle);
MinuteHand->AttachTo(MinuteHandle);

HourHandle->AttachTo(RootSceneComponent);
MinuteHandle->AttachTo(RootSceneComponent);

ClockFace->AttachTo(RootSceneComponent);
ClockFace->SetRelativeTransform(FTransform(FRotator(90, 0, 0), FVector(10, 0, 25), FVector(2, 2, 0.1)));

HourHand->SetRelativeTransform(FTransform(FRotator(0, 0, 0), FVector(0, 0, 25), FVector(0.1, 0.1, 0.5)));
MinuteHand->SetRelativeTransform(FTransform(FRotator(0, 0, 0), FVector(0, 0, 50), FVector(0.1, 0.1, 1)));

}


void ACClock::BeginPlay()
{
Super::BeginPlay();

TArray<AActor*>TimeOfDayHandlers;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ACTimeOfDayHandler::StaticClass(), TimeOfDayHandlers);
if(TimeOfDayHandlers.Num()!=0)
{
auto TimeOfDayHandler = Cast< ACTimeOfDayHandler>(TimeOfDayHandlers[0]);
MyDelegateHandle = TimeOfDayHandler->OnTimeChanged.AddUObject(this, &ACClock::TimeChanged);
}


}

void ACClock::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}

void ACClock::TimeChanged(int32 Hours, int32 Minutes)
{
HourHandle->SetRelativeRotation(FRotator(0, 0, 30 * Hours));
MinuteHandle->SetRelativeRotation(FRotator(0, 0, 6 * Minutes));

}

 

 

실행결과

 

+ Recent posts