-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGamePhase.cs
More file actions
149 lines (117 loc) · 4.53 KB
/
GamePhase.cs
File metadata and controls
149 lines (117 loc) · 4.53 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
144
145
146
147
148
149
// Copyright (c) Meta Platforms, Inc. and affiliates.
// Use of the material below is subject to the terms of the MIT License
// https://github.com/oculus-samples/Unity-Decommissioned/tree/main/Assets/Decommissioned/LICENSE
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Meta.Decommissioned.ScriptableObjects;
using Meta.Multiplayer.Networking;
using Meta.Utilities;
using Meta.XR.Samples;
using UnityEngine;
namespace Meta.Decommissioned.Game
{
/// <summary>
/// A network singleton representing a single phase in the game; determines a phase's duration, callbacks, and state.
/// Specific phases and their unique behavior are encapsulated by a number of unique child classes -- see examples in
/// <see cref="VotePhase"/>,
/// <see cref="PlanningPhase"/>,
/// <see cref="DiscussionPhase"/>
/// and <see cref="NightPhase"/>.
/// </summary>
[MetaCodeSample("Decommissioned")]
public abstract class GamePhase : NetworkSingleton<GamePhase>
{
[SerializeField] protected PhaseConfig m_config;
public GamePhaseEvent OnPhaseBegan;
public GamePhaseEvent OnPhaseEnded;
[SerializeField] private bool m_phaseIsTimed = true;
public bool IsPhaseEnding { get; set; }
[SerializeField] private List<PreGameplayStep> m_preGameplaySteps;
protected virtual float DurationSeconds => m_config.DurationSeconds;
public abstract Phase Phase { get; }
[field: SerializeField, AutoSet]
public NetworkTimer Timer { get; private set; }
private PreGameplayStep m_currentStep;
protected new virtual void Awake() => base.Awake();
protected new void OnDestroy() => base.OnDestroy();
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
Begin();
}
public override void OnNetworkDespawn()
{
base.OnNetworkDespawn();
OnPhaseBegan.RemoveAllListeners();
OnPhaseEnded.RemoveAllListeners();
if (m_phaseIsTimed) { Timer.OnTimerExpiredOnServer.RemoveListener(AdvanceWhenTimerExpires); }
}
private IEnumerator RunBeginStepsRoutine()
{
if (IsServer)
{
foreach (var step in m_preGameplaySteps.Select(s => Instantiate(s)))
{
m_currentStep = step;
step.NetworkObject.Spawn(true);
yield return step.Run();
step.NetworkObject.Despawn();
}
}
Execute();
}
protected virtual void Begin()
{
RaisePhaseBeginEvent();
GamePhaseManager.Instance.RaiseOnPhaseChanged();
GamePhaseManager.Instance.OnPhaseChanged += OnPhaseChanged;
if (TransitionalFade.Instance != null)
{
Timer.OnClientTransitionTimeReached.AddListener(OnTimerTransitionTimeReached);
}
_ = StartCoroutine(RunBeginStepsRoutine());
}
public virtual void End() => RaisePhaseEndEvent();
protected virtual void Execute()
{
if (!m_phaseIsTimed) { return; }
Timer.StartTimer(DurationSeconds);
Timer.OnTimerExpiredOnServer.AddListener(AdvanceWhenTimerExpires);
}
private void RaisePhaseBeginEvent() => OnPhaseBegan.Invoke(this);
private void RaisePhaseEndEvent() => OnPhaseEnded.Invoke(this);
private void OnPhaseChanged(Phase phase)
{
if (phase == Phase) { return; }
if (m_currentStep) { m_currentStep.NetworkObject.Despawn(); }
}
public void SetPhaseAsEnding()
{
if (!IsServer) { return; }
IsPhaseEnding = true;
}
public void ForceSkipPhase()
{
EndPreGameplaySteps();
IsPhaseEnding = true;
}
private void EndPreGameplaySteps()
{
StopAllCoroutines();
if (m_currentStep != null && m_currentStep.IsSpawned)
{
m_currentStep.StopAllCoroutines();
m_currentStep.End();
m_currentStep.NetworkObject.Despawn();
}
Execute();
}
private void OnTimerTransitionTimeReached() => TransitionalFade.Instance.OnPhaseTimerTransition(false);
private void AdvanceWhenTimerExpires(NetworkTimer _)
{
IsPhaseEnding = true;
GamePhaseManager.Instance.AdvancePhase();
}
}
}