-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastupdated.cs
281 lines (249 loc) · 7.81 KB
/
lastupdated.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class NetworkManager : MonoBehaviourPunCallbacks
{
[SerializeField]
private Text connectionText;
[SerializeField]
private Transform[] spawnPoints;
[SerializeField]
private Camera sceneCamera;
[SerializeField]
private GameObject[] playerModel;
[SerializeField]
private GameObject serverWindow;
[SerializeField]
private GameObject messageWindow;
[SerializeField]
private GameObject sightImage;
[SerializeField]
private InputField username;
[SerializeField]
private InputField roomName;
[SerializeField]
private InputField roomList;
[SerializeField]
private InputField messagesLog;
// timmer
[SerializeField]
private Text timerText;
// Kills & death
[SerializeField] private Text killCountText;
[SerializeField] private Text deathCountText;
private GameObject player;
private Queue<string> messages;
private const int messageCount = 10;
private string nickNamePrefKey = "PlayerName";
private PhotonView photonView;
// Timer variables
public float GameDuration = 20f; // Game duration in seconds (20 seconds)
private float elapsedTime = 0f;
private bool isGameOver = false;
// Kill & Death Count..!
private Dictionary<string, int> killCounts = new Dictionary<string, int>();
private Dictionary<string, int> deathCounts = new Dictionary<string, int>();
void Start()
{
messages = new Queue<string>(messageCount);
photonView = GetComponent<PhotonView>();
if (PlayerPrefs.HasKey(nickNamePrefKey))
{
username.text = PlayerPrefs.GetString(nickNamePrefKey);
}
PhotonNetwork.AutomaticallySyncScene = true;
PhotonNetwork.ConnectUsingSettings();
connectionText.text = "Connecting to lobby...";
//Initial kill and death
string localPlayerName = PhotonNetwork.LocalPlayer.NickName;
killCounts.Add(localPlayerName, 0);
deathCounts.Add(localPlayerName, 0);
}
public override void OnConnectedToMaster()
{
PhotonNetwork.JoinLobby();
}
public override void OnDisconnected(DisconnectCause cause)
{
connectionText.text = cause.ToString();
}
public override void OnJoinedLobby()
{
serverWindow.SetActive(true);
connectionText.text = "";
}
public override void OnRoomListUpdate(List<RoomInfo> rooms)
{
roomList.text = "";
foreach (RoomInfo room in rooms)
{
roomList.text += room.Name + "\n";
}
}
public void JoinRoom()
{
serverWindow.SetActive(false);
connectionText.text = "Joining room...";
PhotonNetwork.LocalPlayer.NickName = username.text;
PlayerPrefs.SetString(nickNamePrefKey, username.text);
RoomOptions roomOptions = new RoomOptions()
{
IsVisible = true,
MaxPlayers = 8
};
if (PhotonNetwork.IsConnectedAndReady)
{
PhotonNetwork.JoinOrCreateRoom(roomName.text, roomOptions, TypedLobby.Default);
}
else
{
connectionText.text = "PhotonNetwork connection is not ready, try restarting it.";
}
}
public override void OnJoinedRoom()
{
connectionText.text = "";
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
StartGame();
}
void StartGame()
{
Debug.Log("Starting game with GameDuration: " + GameDuration);
Respawn(0.0f);
if (PhotonNetwork.IsMasterClient)
{
StartCoroutine(GameTimer());
}
}
void Respawn(float spawnTime)
{
sightImage.SetActive(false);
sceneCamera.enabled = true;
StartCoroutine(RespawnCoroutine(spawnTime));
}
// Method to update UI with kill and death counts
private void UpdatePlayerUI(string playerName)
{
int kills = killCounts.ContainsKey(playerName) ? killCounts[playerName] : 0;
int deaths = deathCounts.ContainsKey(playerName) ? deathCounts[playerName] : 0;
killCountText.text = "Kills: " + kills.ToString();
deathCountText.text = "Deaths: " + deaths.ToString();
}
// Method to handle kill event
public void HandleKill(string playerName)
{
if (killCounts.ContainsKey(playerName))
{
killCounts[playerName]++;
}
UpdatePlayerUI(playerName);
}
// Method to handle death event
public void HandleDeath(string playerName)
{
if (deathCounts.ContainsKey(playerName))
{
deathCounts[playerName]++;
}
UpdatePlayerUI(playerName);
}
IEnumerator RespawnCoroutine(float spawnTime)
{
yield return new WaitForSeconds(spawnTime);
messageWindow.SetActive(true);
sightImage.SetActive(true);
int playerIndex = Random.Range(0, playerModel.Length);
int spawnIndex = Random.Range(0, spawnPoints.Length);
player = PhotonNetwork.Instantiate(playerModel[playerIndex].name, spawnPoints[spawnIndex].position, spawnPoints[spawnIndex].rotation, 0);
PlayerHealth playerHealth = player.GetComponent<PlayerHealth>();
playerHealth.RespawnEvent += Respawn;
playerHealth.AddMessageEvent += AddMessage;
sceneCamera.enabled = false;
if (spawnTime == 0)
{
AddMessage("Player " + PhotonNetwork.LocalPlayer.NickName + " Joined Game.");
}
else
{
AddMessage("Player " + PhotonNetwork.LocalPlayer.NickName + " Respawned.");
}
}
void AddMessage(string message)
{
photonView.RPC("AddMessage_RPC", RpcTarget.All, message);
}
[PunRPC]
void AddMessage_RPC(string message)
{
messages.Enqueue(message);
if (messages.Count > messageCount)
{
messages.Dequeue();
}
messagesLog.text = "";
foreach (string m in messages)
{
messagesLog.text += m + "\n";
}
}
public override void OnPlayerLeftRoom(Player other)
{
if (PhotonNetwork.IsMasterClient)
{
AddMessage("Player " + other.NickName + " Left Game.");
}
}
IEnumerator GameTimer()
{
while (elapsedTime < GameDuration && !isGameOver)
{
yield return new WaitForSeconds(1f);
elapsedTime += 1f;
photonView.RPC("UpdateTimer", RpcTarget.All, elapsedTime);
}
if (!isGameOver)
{
photonView.RPC("EndGame", RpcTarget.All);
}
}
[PunRPC]
void UpdateTimer(float time)
{
elapsedTime = time;
UpdateTimerUI();
}
void UpdateTimerUI()
{
float remainingTime = GameDuration - elapsedTime;
int minutes = Mathf.FloorToInt(remainingTime / 60);
int seconds = Mathf.FloorToInt(remainingTime % 60);
timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
[PunRPC]
void EndGame()
{
isGameOver = true;
Debug.Log("Game Over!");
HandleGameEnd();
}
void HandleGameEnd()
{
// Stop all player actions and show end-game screen or results
AddMessage("Game Over!");
// Print kill and death data for all players
Debug.Log("Kill and Death Data:");
foreach (var kvp in killCounts)
{
string playerName = kvp.Key;
int kills = kvp.Value;
int deaths = deathCounts.ContainsKey(playerName) ? deathCounts[playerName] : 0;
Debug.Log(playerName + ": Kills - " + kills + ", Deaths - " + deaths);
AddMessage(playerName + ": Kills - " + kills + ", Deaths - " + deaths);
}
// Implement additional end-game logic here
}
}