This repository was archived by the owner on Dec 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathTurnComponent.cpp
More file actions
executable file
·123 lines (106 loc) · 2.42 KB
/
Copy pathTurnComponent.cpp
File metadata and controls
executable file
·123 lines (106 loc) · 2.42 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
// Fill out your copyright notice in the Description page of Project Settings.
#include "TurnComponent.H"
UTurnComponent::UTurnComponent()
:Super(),
TurnManager(nullptr),
StartingActionPoints(1),
RemainingActionPoints(1),
TurnTimeout(30)
{
}
void UTurnComponent::BeginPlay()
{
Super::BeginPlay();
RegisterWithTurnManager();
}
void UTurnComponent::OnComponentDestroyed(bool bDestroyingHierarchy)
{
Super::OnComponentDestroyed(bDestroyingHierarchy);
UnregisterWithTurnManager();
}
ATurnManager * UTurnComponent::GetTurnManager()
{
if (!IsValid(TurnManager))
{
RegisterWithTurnManager();
}
return TurnManager;
}
void UTurnComponent::EndTurn()
{
if (IsValid(TurnManager))
{
TurnManager->EndTurn(this);
}
}
void UTurnComponent::EndTeamTurn()
{
if (IsValid(TurnManager))
{
TurnManager->EndTeamTurn(FGenericTeamId::GetTeamIdentifier(GetOwner()));
}
}
void UTurnComponent::RequestStartTurn()
{
if (IsValid(TurnManager))
{
TurnManager->RequestStartTurn(this);
}
}
void UTurnComponent::RequestStartNextComponent()
{
if (IsValid(TurnManager))
{
TurnManager->RequestStartNextComponent(this);
}
}
void UTurnComponent::OnTurnTimeout()
{
if (MyTurn())
{
UE_LOG(NavGrid, Warning, TEXT("Turn timeout (%f sec) reached for %s"), TurnTimeout, *GetOwner()->GetName());
RemainingActionPoints = 0;
EndTurn();
}
}
void UTurnComponent::OwnerReadyForInput()
{
if (IsValid(TurnManager) && TurnManager->GetCurrentComponent() == this)
{
TurnManager->OnReadyForInput().Broadcast(this);
}
}
AActor *UTurnComponent::GetCurrentActor() const
{
if (IsValid(TurnManager))
{
return TurnManager->GetCurrentActor();
}
return nullptr;
}
void UTurnComponent::RegisterWithTurnManager()
{
UnregisterWithTurnManager();
ANavGridGameState *GameState = GetWorld()->GetGameState<ANavGridGameState>();
if (IsValid(GameState))
{
TurnManager = GameState->GetTurnManager();
TurnManager->RegisterTurnComponent(this);
}
}
void UTurnComponent::UnregisterWithTurnManager()
{
if (IsValid(TurnManager))
{
TurnManager->UnregisterTurnComponent(this);
}
TurnManager = nullptr;
}
void UTurnComponent::OnTurnStart()
{
GetWorld()->GetTimerManager().SetTimer(TurnTimeoutHandle, this, &UTurnComponent::OnTurnTimeout, TurnTimeout);
}
void UTurnComponent::OnTurnEnd()
{
GetWorld()->GetTimerManager().ClearTimer(TurnTimeoutHandle);
}