-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cs
176 lines (144 loc) · 5.03 KB
/
GameManager.cs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using UnityEngine;
using TMPro;
using Photon.Pun;
using ExitGames.Client.Photon;
using Photon.Realtime;
namespace LoneX.TchilaVirus
{
public class GameManager : MonoBehaviourPunCallbacks
{
#region fields
private int shieldCount = 0;
public TMP_Text scoreText;
public TMP_Text CenterText;
public float maxGameTimer;
public float gameTimer;
public int score;
public static GameManager instance;
public static bool isGameover = false;
#endregion
#region MonoBehaviour Callbacks
private void Start()
{
InitializeRoomProperties();
}
private void Awake()
{
if(instance == null)
instance = this;
else
Destroy(this);
}
public override void OnEnable()
{
base.OnEnable();
CenterText.gameObject.SetActive(false);
CountdownTimer.OnCountdownTimerHasExpired += OnCountdownTimerHasExpired;
isGameover= false;
if(PhotonNetwork.IsMasterClient)
{
CountdownTimer.SetStartTime();
}
}
public override void OnDisable()
{
base.OnDisable();
CountdownTimer.OnCountdownTimerHasExpired -= OnCountdownTimerHasExpired;
}
#endregion
#region CountDownTimer Callbacks
public void OnCountdownTimerHasExpired()
{
if(!PhotonNetwork.InRoom)
return;
DisplayGameOverText();
}
#endregion
#region Photon Callbacks
public override void OnJoinedRoom()
{
//Debug.Log("OnJoinedRoom called in Game Manager");
if(PhotonNetwork.CurrentRoom.CustomProperties["ShieldCount"] != null
&& PhotonNetwork.CurrentRoom.CustomProperties["gameTimer"] != null
&& PhotonNetwork.CurrentRoom.CustomProperties["score"] != null)
{
gameTimer = float.Parse(PhotonNetwork.CurrentRoom.CustomProperties["gameTimer"].ToString());
shieldCount = int.Parse(PhotonNetwork.CurrentRoom.CustomProperties["ShieldCount"].ToString());
score = int.Parse(PhotonNetwork.CurrentRoom.CustomProperties["score"].ToString());
scoreText.text = $"{score}/{shieldCount}";
}else
RoomManager.instance.LeaveRoom();
}
public override void OnRoomPropertiesUpdate(Hashtable _propertiesThatChanged)
{
if( _propertiesThatChanged["score"] != null && _propertiesThatChanged["ShieldCount"] != null)
{
score = int.Parse(_propertiesThatChanged["score"].ToString());
shieldCount = int.Parse(_propertiesThatChanged["ShieldCount"].ToString());
scoreText.text = $"{score}/{shieldCount}";
}
}
#endregion
#region LocalMethods
public void DisplayGameOverText()
{
isGameover = true;
CenterText.gameObject.SetActive(true);
PlayerManager _player = RoomManager.instance.myplayer.GetComponent<PlayerManager>();
if(PlayerWon(_player))
CenterText.text = "Mission Acomplished !";
else
CenterText.text = "Mission Failed !";
}
public bool PlayerWon(PlayerManager _player)
{
if(_player.team == Team.Virus)
if(score < shieldCount)
{
return false;
}
else
{
return true;
}
else
if(score < shieldCount)
{
return true;
}
else
{
return false;
}
}
public void InitializeRoomProperties()
{
if(PhotonNetwork.IsMasterClient)
{
gameTimer = maxGameTimer;
shieldCount = ShieldsManager.instance.transform.childCount;
Hashtable _timeProp = new Hashtable {{"gameTimer", gameTimer} , {"ShieldCount" , shieldCount},{"score" , score}};
InitScoreText();
PhotonNetwork.CurrentRoom.SetCustomProperties(_timeProp);
}
}
public void UpdateScore()
{
if(!isGameover)
{
score++;
scoreText.text = $"{score}/{shieldCount}";
Hashtable _scoreProp = new Hashtable {{"score", score}};
if(score >= shieldCount)
DisplayGameOverText();
PhotonNetwork.CurrentRoom.SetCustomProperties(_scoreProp);
}
}
public void InitScoreText()
{
if(scoreText != null)
scoreText.text = $"{score}/{shieldCount}";
}
#endregion
}
}