-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraManager.cpp
More file actions
143 lines (109 loc) · 4.2 KB
/
CameraManager.cpp
File metadata and controls
143 lines (109 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Fill out your copyright notice in the Description page of Project Settings.
#include "CameraManager.h"
#include "Camera/CameraActor.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
#include "Components/AudioComponent.h"
// Sets default values
ACameraManager::ACameraManager()
{
// 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;
SwitchCameraAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("SwitchCameraAudioComponent"));
SwitchCameraAudioComponent->SetupAttachment(RootComponent);
CameraWidgetClass = nullptr;
WidgetCameraIndex = 0;
}
void ACameraManager::SwitchToNextCamera()
{
if(Cameras.Num() > 0 )
{
if(Cameras.Num() > 0)
{
CurrentCameraIndex = (CurrentCameraIndex + 1) % Cameras.Num();
// 사운드 재생
if (SwitchCameraSoundCue)
{
SwitchCameraAudioComponent->SetSound(SwitchCameraSoundCue);
SwitchCameraAudioComponent->Play();
UE_LOG(LogTemp, Warning, TEXT("Playing camera switch sound"));
}
else
{
UE_LOG(LogTemp, Error, TEXT("SwitchCameraSoundCue is not set"));
return;
}
SwitchToCamera(CurrentCameraIndex);
}
}
}
void ACameraManager::SwitchToCamera(int32 CameraIndex)
{
// Check if the CameraIndex is valid
if (Cameras.IsValidIndex(CameraIndex))
{
ACameraActor* NewCamera = Cast<ACameraActor>(Cameras[CameraIndex]);
if (NewCamera)
{
APlayerController* PlayerController = UGameplayStatics::GetPlayerController(this, 0);
if (!PlayerController)
{
UE_LOG(LogTemp, Error, TEXT("Failed to get PlayerController"));
return; // 에러 발생 시 함수 종료
}
// Log the current and new camera information
UE_LOG(LogTemp, Warning, TEXT("Switching to camera index: %d"), CameraIndex);
UE_LOG(LogTemp, Warning, TEXT("Current Camera: %s"), *PlayerController->GetViewTarget()->GetName());
UE_LOG(LogTemp, Warning, TEXT("New Camera: %s"), *NewCamera->GetName());
// Camera switching with a blend time
PlayerController->SetViewTargetWithBlend(NewCamera, 0.1f);
// Update NowCameraIndex to the current camera index
NowCameraIndex = CameraIndex; // Update the current camera index
AActor* NewViewTarget = PlayerController->GetViewTarget();
if (NewViewTarget != NewCamera)
{
UE_LOG(LogTemp, Error, TEXT("Failed to set new view target"));
}
// Log success message
UE_LOG(LogTemp, Warning, TEXT("Successfully switched to camera: %s"), *NewCamera->GetName());
}
else
{
// Log error if casting failed
UE_LOG(LogTemp, Error, TEXT("Failed to cast to ACameraActor for index: %d"), CameraIndex);
}
}
else
{
// Log error if CameraIndex is invalid
UE_LOG(LogTemp, Error, TEXT("Invalid CameraIndex: %d"), CameraIndex);
}
}
// Called when the game starts or when spawned
void ACameraManager::BeginPlay()
{
Super::BeginPlay();
// 모든 ACameraActor를 찾아서 Cameras 배열에 추가
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ACameraActor::StaticClass(), Cameras);
if (Cameras.Num() > 0)
{
CurrentCameraIndex = 0;
NowCameraIndex = 0;
UE_LOG(LogTemp, Warning, TEXT("Current Camera Index: %d"), CurrentCameraIndex);
UE_LOG(LogTemp, Warning, TEXT("Found %d cameras"), Cameras.Num());
// 시작할 때 첫 번째 카메라로 전환
SwitchToCamera(CurrentCameraIndex);
}
else
{
UE_LOG(LogTemp, Error, TEXT("No cameras found"));
}
}
float ACameraManager::CurrentCamera()
{
return CurrentCameraIndex;
}
// Called every frame
void ACameraManager::Tick(float DeltaTime)
{
}