summaryrefslogtreecommitdiff
path: root/GameCode/NetworkData.cs
blob: 293da46a1774cbe2c7d15a76ff265a442e1ecc95 (plain)
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
using Photon.Pun;
using UnityEngine;

public class NetworkData : MonoBehaviour
{
	private PhotonView photonView;

	private bool inited;

	private void Start()
	{
		photonView = GetComponent<PhotonView>();
	}

	private void Init()
	{
		if (!inited)
		{
			inited = true;
			if (PhotonNetwork.IsMasterClient)
			{
				Debug.Log("Why am i the master?");
			}
		}
	}

	private void Update()
	{
		if (PhotonNetwork.InRoom)
		{
			Init();
		}
	}

	private void RequestJoin()
	{
		photonView.RPC("RequestJoinMaster", RpcTarget.MasterClient);
		Debug.Log("Request join");
	}

	[PunRPC]
	public void RequestJoinMaster()
	{
		string text = JsonUtility.ToJson(new InitPackage
		{
			currentMapID = MapManager.instance.currentLevelID
		});
		photonView.RPC("RequestJoinResponse", RpcTarget.Others, text);
	}

	[PunRPC]
	public void RequestJoinResponse(string jsonResponse)
	{
		InitPackage initPackage = (InitPackage)JsonUtility.FromJson(jsonResponse, typeof(InitPackage));
		MapManager.instance.LoadLevelFromID(initPackage.currentMapID, onlyMaster: false, callInImidetly: true);
		Debug.Log("Got response");
	}
}