응애맘마조

231129 강의 본문

공부/UE강의

231129 강의

TH.Wert 2023. 11. 30. 21:44

델리게이트를 사용하는 것을 강의했습니다.

#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "C_Trigger.generated.h"

DECLARE_DELEGATE(FBoxLightOverlap);

UCLASS()
class SCENEA_API AC_Trigger : public AActor
{
	GENERATED_BODY()
	
	UPROPERTY(VisibleDefaultsOnly)
		class USceneComponent* Scene;

	UPROPERTY(VisibleDefaultsOnly)
		class UBoxComponent* Box;

	UPROPERTY(VisibleDefaultsOnly)
		class UTextRenderComponent* Text;

public:	
	AC_Trigger();

protected:
	virtual void BeginPlay() override;

	UFUNCTION()
		void ComponentBeginOverlap(
			UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult
		);
	UFUNCTION()
		void ComponentEndOverlap(
			UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);           
public:
	FBoxLightOverlap OnBoxLightBeginOverlap;
	FBoxLightOverlap OnBoxLightEndOverlap;
};
#include "Ive/C_Trigger.h"
#include "Global.h"
#include "Components/BoxComponent.h"
#include "Components/TextRenderComponent.h"
#include "Components/PointLightComponent.h"

// Sets default values
AC_Trigger::AC_Trigger()
{

	CHelpers::CreateComponent<USceneComponent>(this, &Scene, "Scene");
	CHelpers::CreateComponent<UBoxComponent>(this, &Box, "Box", Scene);
	CHelpers::CreateComponent<UTextRenderComponent>(this, &Text, "Text", Scene);

	Box->SetRelativeScale3D(FVector(3));
	Box->bHiddenInGame = false;

	Text->SetRelativeLocation(FVector(0, 0, 100));
	Text->SetRelativeRotation(FRotator(0, 180, 0));
	Text->SetRelativeScale3D(FVector(2));

	Text->TextRenderColor = FColor::Red;
	Text->HorizontalAlignment = EHorizTextAligment::EHTA_Center;
	Text->Text = FText::FromString(GetName());
}

// Called when the game starts or when spawned
void AC_Trigger::BeginPlay()
{
	Super::BeginPlay();
	
	Box->OnComponentBeginOverlap.AddDynamic(this, &AC_Trigger::ComponentBeginOverlap);
	Box->OnComponentEndOverlap.AddDynamic(this, &AC_Trigger::ComponentEndOverlap);
}

void AC_Trigger::ComponentBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	if(OnBoxLightBeginOverlap.IsBound())
	OnBoxLightBeginOverlap.Execute();
}

void AC_Trigger::ComponentEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	if (OnBoxLightBeginOverlap.IsBound())
		OnBoxLightBeginOverlap.Execute();
}
#pragma once

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

UCLASS()
class SCENEA_API AC_OverlapAndHit : public AActor
{
	GENERATED_BODY()
	
		UPROPERTY(VisibleDefaultsOnly)
		class USceneComponent* Scene;

	UPROPERTY(VisibleDefaultsOnly)
		class UBoxComponent* Box;

	UPROPERTY(VisibleDefaultsOnly)
		class UTextRenderComponent* Text;
public:	
	// Sets default values for this actor's properties
	AC_OverlapAndHit();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	UFUNCTION()
		void ComponentBeginOverlap(
			UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult
		);
	UFUNCTION()
		void ComponentEndOverlap(
			UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

	UFUNCTION()
		void ComponentHit(
			UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
};
#include "Ive/C_OverlapAndHit.h"
#include "Global.h"
#include "Components/BoxComponent.h"
#include "Components/TextRenderComponent.h"
#include "Components/PointLightComponent.h"

// Sets default values
AC_OverlapAndHit::AC_OverlapAndHit()
{
	CHelpers::CreateComponent<USceneComponent>(this, &Scene, "Scene");
	CHelpers::CreateComponent<UBoxComponent>(this, &Box, "Box", Scene);
	CHelpers::CreateComponent<UTextRenderComponent>(this, &Text, "Text", Scene);

	Box->SetRelativeScale3D(FVector(3));
	Box->bHiddenInGame = false;

	Text->SetRelativeLocation(FVector(0, 0, 100));
	Text->SetRelativeRotation(FRotator(0, 180, 0));
	Text->SetRelativeScale3D(FVector(2));

	Text->TextRenderColor = FColor::Red;
	Text->HorizontalAlignment = EHorizTextAligment::EHTA_Center;
	Text->Text = FText::FromString(GetName());
}

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

	Box->OnComponentBeginOverlap.AddDynamic(this, &AC_OverlapAndHit::ComponentBeginOverlap);
	Box->OnComponentEndOverlap.AddDynamic(this, &AC_OverlapAndHit::ComponentEndOverlap);
	Box->OnComponentHit.AddDynamic(this, &AC_OverlapAndHit::ComponentHit);
}

void AC_OverlapAndHit::ComponentBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	UE_LOG(LogTemp, Log, TEXT("BeginOverlap , %s"), *OtherActor->GetName());
}

void AC_OverlapAndHit::ComponentEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	UE_LOG(LogTemp, Log, TEXT("EndOverlap, %s"), *OtherActor->GetName());

}
void AC_OverlapAndHit::ComponentHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
	UE_LOG(LogTemp, Log, TEXT("Hit, %s"), *OtherActor->GetName());
}
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "C_Light.generated.h"

UCLASS()
class SCENEA_API AC_Light : public AActor
{
	GENERATED_BODY()
    
private:

	UPROPERTY(VisibleDefaultsOnly)
		class USceneComponent* Scene;

	UPROPERTY(VisibleDefaultsOnly)
		class UPointLightComponent* PointLight;

public:	
	// Sets default values for this actor's properties
	AC_Light();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
    
public:
	UFUNCTION()
		void OnLight();
	UFUNCTION()
		void OffLight();
};
#include "Ive/C_Light.h"
#include "Global.h"
#include "C_Trigger.h"
#include "Components/PointLightComponent.h"
#include "Components/TextRenderComponent.h"

// Sets default values
AC_Light::AC_Light()
{
	CHelpers::CreateComponent<USceneComponent>(this, &Scene, "Scene");
	CHelpers::CreateComponent<UPointLightComponent>(this, &PointLight, "Light", Scene);

	PointLight->SetLightColor(FLinearColor::Red);
	PointLight->Intensity = 1000.0f;
	PointLight->AttenuationRadius = 200.0f;
	PointLight->SetVisibility(false);
}

// Called when the game starts or when spawned
void AC_Light::BeginPlay()
{
	Super::BeginPlay();
	
	AC_Trigger* trigger = nullptr;
	for (AActor* actor : GetWorld()->GetCurrentLevel()->Actors)
	{
		if (!!actor && actor->IsA<AC_Trigger>())
		{
			trigger = Cast<AC_Trigger>(actor);
		}
	}
	trigger->OnBoxLightBeginOverlap.BindUFunction(this, "OnLight");
	trigger->OnBoxLightEndOverlap.BindUFunction(this, "OffLight");
}

void AC_Light::OnLight()
{
	PointLight->SetVisibility(true);
}

void AC_Light::OffLight()
{
	PointLight->SetVisibility(false);
}
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "C_Collision.generated.h"

UCLASS()
class SCENEA_API AC_Collision : public AActor
{
	GENERATED_BODY()
private:
	UPROPERTY(VisibleDefaultsOnly)
		class USceneComponent* Scene;

	UPROPERTY(VisibleDefaultsOnly)
		class UBoxComponent* Box;

	UPROPERTY(VisibleDefaultsOnly)
		class UTextRenderComponent* Text;

	UPROPERTY(VisibleDefaultsOnly)
		class UPointLightComponent* Light;

public:	
	// Sets default values for this actor's properties
	AC_Collision();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

private:
	UFUNCTION()
		void ActorBeginOverlap(AActor* OverLappedActor, AActor* OtherActor);
	UFUNCTION()
		void ActorEndOverlap(AActor* OverLappedActor, AActor* OtherActor);

	UFUNCTION()
		void ComponentBeginOverlap(
			UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult
		);

	UFUNCTION()
		void ComponentEndOverlap(
			UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
};
#include "Ive/C_Collision.h"
#include "Global.h"
#include "Components/BoxComponent.h"
#include "Components/TextRenderComponent.h"
#include "Components/PointLightComponent.h"

// Sets default values
AC_Collision::AC_Collision()
{
	CHelpers::CreateComponent<USceneComponent>(this, &Scene, "Scene");
	CHelpers::CreateComponent<UBoxComponent>(this, &Box, "Box", Scene);
	CHelpers::CreateComponent<UTextRenderComponent>(this, &Text, "Text", Scene);
	CHelpers::CreateComponent<UPointLightComponent>(this, &Light, "Light", Scene);

	Box->SetRelativeScale3D(FVector(3));
	Box->bHiddenInGame = false;

	Text->SetRelativeLocation(FVector(0, 0, 100));
	Text->SetRelativeRotation(FRotator(0, 180, 0));
	Text->SetRelativeScale3D(FVector(2));

	Text->TextRenderColor = FColor::Red;
	Text->HorizontalAlignment = EHorizTextAligment::EHTA_Center;
	Text->Text = FText::FromString(GetName());

	Light->SetLightColor(FLinearColor::Red);
}

// Called when the game starts or when spawned
void AC_Collision::BeginPlay()
{
	Super::BeginPlay();
	
	OnActorBeginOverlap.AddDynamic(this, &AC_Collision::ActorBeginOverlap);
	OnActorEndOverlap.AddDynamic(this, &AC_Collision::ActorEndOverlap);

	Box->OnComponentBeginOverlap.AddDynamic(this, &AC_Collision::ComponentBeginOverlap);
	Box->OnComponentEndOverlap.AddDynamic(this, &AC_Collision::ComponentEndOverlap);

	Light->SetVisibility(false);
}

void AC_Collision::ActorBeginOverlap(AActor* OverLappedActor, AActor* OtherActor)
{
	UE_LOG(LogTemp, Log, TEXT("BeginOverlap - %s , %s"), *OverLappedActor->GetName() ,
		*OtherActor->GetName());
}

void AC_Collision::ActorEndOverlap(AActor* OverLappedActor, AActor* OtherActor)
{

	UE_LOG(LogTemp, Log, TEXT("EndOverlap - %s , %s"), *OverLappedActor->GetName(),
		*OtherActor->GetName());
}

void AC_Collision::ComponentBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	Light->SetVisibility(true);
}

void AC_Collision::ComponentEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	Light->SetVisibility(false);
}

각각 트리거, 오버랩, 라이트, 콜리전입니다.

실행 영상

읽어주셔서 감사합니다.

'공부 > UE강의' 카테고리의 다른 글

231212 강의  (0) 2023.12.12
231211 강의  (0) 2023.12.11
231129 강의  (1) 2023.11.29
231128 강의  (0) 2023.11.28
231127 강의  (0) 2023.11.27
Comments