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
|
using System.Collections;
using Photon.Pun;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MapManager : MonoBehaviour
{
[SerializeField]
public string[] levels;
public int currentLevelID;
public static MapManager instance;
[HideInInspector]
public bool isTestingMap;
public MapWrapper currentMap;
private PhotonView view;
internal int otherPlayersMostRecentlyLoadedLevel = -1;
public string forceMap = "";
private bool callInNextMap;
private void Awake()
{
instance = this;
view = GetComponent<PhotonView>();
}
internal void ReportMapLoaded(int levelID)
{
view.RPC("RPCA_ReportMapLoaded", RpcTarget.Others, levelID);
}
[PunRPC]
internal void RPCA_ReportMapLoaded(int levelID)
{
otherPlayersMostRecentlyLoadedLevel = levelID;
}
private string GetRandomMap()
{
if (forceMap != "")
{
return forceMap;
}
int num = Random.Range(0, levels.Length);
while (num == currentLevelID && levels.Length > 1)
{
num = Random.Range(0, levels.Length);
}
return levels[num];
}
public void LoadNextLevel(bool callInImidetly = false, bool forceLoad = false)
{
if (forceLoad || PhotonNetwork.IsMasterClient || PhotonNetwork.OfflineMode)
{
view.RPC("RPCA_SetCallInNextMap", RpcTarget.All, callInImidetly);
view.RPC("RPCA_LoadLevel", RpcTarget.All, GetRandomMap());
}
}
[PunRPC]
private void RPCA_SetCallInNextMap(bool toSet)
{
callInNextMap = toSet;
}
public void LoadLevelFromID(int ID, bool onlyMaster = false, bool callInImidetly = false)
{
if (!(!PhotonNetwork.IsMasterClient && onlyMaster))
{
callInNextMap = callInImidetly;
RPCA_LoadLevel(levels[ID]);
}
}
[PunRPC]
public void RPCA_CallInNewMapAndMovePlayers(int mapID)
{
StartCoroutine(WaitForMapToBeLoaded(mapID));
}
private IEnumerator WaitForMapToBeLoaded(int mapID)
{
while (currentLevelID != mapID)
{
yield return null;
}
Debug.Log("CALL IN NEW MAP AND MOVE PLAYERS");
if (currentMap != null)
{
MapTransition.instance.Enter(currentMap.Map);
}
MapTransition.instance.ClearObjects();
PlayerManager.instance.RPCA_MovePlayers();
}
public void CallInNewMapAndMovePlayers(int mapID)
{
if (PhotonNetwork.IsMasterClient || PhotonNetwork.OfflineMode)
{
view.RPC("RPCA_CallInNewMapAndMovePlayers", RpcTarget.All, mapID);
}
}
[PunRPC]
public void RPCA_CallInNewMap()
{
if (currentMap != null)
{
MapTransition.instance.Enter(currentMap.Map);
}
MapTransition.instance.ClearObjects();
}
public void CallInNewMap()
{
if (PhotonNetwork.IsMasterClient || PhotonNetwork.OfflineMode)
{
view.RPC("RPCA_CallInNewMap", RpcTarget.All);
}
}
public SpawnPoint[] GetSpawnPoints()
{
return currentMap.Map.GetComponentsInChildren<SpawnPoint>();
}
private void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
{
Map map = null;
for (int i = 0; i < scene.GetRootGameObjects().Length; i++)
{
map = scene.GetRootGameObjects()[i].GetComponent<Map>();
if ((bool)map)
{
break;
}
}
if (!map)
{
Debug.LogError("NO MAP WAS FOUND WHEN LOADING NEW MAP");
}
map.wasSpawned = true;
SceneManager.sceneLoaded -= OnLevelFinishedLoading;
if (currentMap != null)
{
StartCoroutine(UnloadAfterSeconds(currentMap.Scene));
MapTransition.instance.Exit(currentMap.Map);
}
MapTransition.instance.SetStartPos(map);
map.levelID = currentLevelID;
currentMap = new MapWrapper(map, scene);
currentLevelID = GetIDFromScene(scene);
if (callInNextMap)
{
CallInNewMap();
callInNextMap = false;
}
Debug.Log("FINISHED LOADING SCENE");
}
private int GetIDFromScene(Scene scene)
{
int result = -1;
for (int i = 0; i < levels.Length; i++)
{
if (levels[i] == scene.name)
{
result = i;
}
}
return result;
}
[PunRPC]
public void RPCA_LoadLevel(string sceneName)
{
Debug.Log("LOADING SCENE");
SceneManager.LoadScene(sceneName, LoadSceneMode.Additive);
SceneManager.sceneLoaded += OnLevelFinishedLoading;
}
private IEnumerator UnloadAfterSeconds(Scene scene)
{
yield return new WaitForSecondsRealtime(2f);
SceneManager.UnloadSceneAsync(scene);
}
public void UnloadScene(Scene scene)
{
SceneManager.UnloadSceneAsync(scene);
}
}
|