summaryrefslogtreecommitdiff
path: root/Landfall.Network/NetworkManager.cs
blob: 89d790a50667776f1df61f4ec55b9a646dc5ce6f (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
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
using System;
using System.Collections.Generic;
using Lidgren.Network;
using UnityEngine;

namespace Landfall.Network;

public class NetworkManager : MonoBehaviour
{
	[SerializeField]
	private GameObject m_PlayerPrefab;

	private GameObject m_SpawnedPlayer;

	private NetClient m_Client;

	private List<NetPlayer> m_Players = new List<NetPlayer>();

	private NetPlayer m_LocalPlayer;

	private string m_LocalName;

	public bool Active { get; private set; }

	private void Awake()
	{
	}

	private void Start()
	{
		InitNetwork();
	}

	private void InitNetwork()
	{
		if (!StartClient())
		{
			Debug.LogError("Could Not Connect to server!");
			return;
		}
		Debug.Log("Connected To Server!");
		SpawnPlayer();
	}

	public bool StartClient()
	{
		m_LocalName = "Philip " + UnityEngine.Random.Range(0, int.MaxValue);
		LoginInformation loginInformation = new LoginInformation();
		loginInformation.PlayerName = m_LocalName;
		LoginInformation ob = loginInformation;
		NetPeerConfiguration netPeerConfiguration = new NetPeerConfiguration("RobotsNetwork");
		netPeerConfiguration.EnableMessageType(NetIncomingMessageType.Data);
		m_Client = new NetClient(netPeerConfiguration);
		m_Client.Start();
		NetOutgoingMessage netOutgoingMessage = m_Client.CreateMessage();
		netOutgoingMessage.Write((byte)1);
		netOutgoingMessage.WriteAllProperties(ob);
		m_Client.Connect("127.0.0.1", 1337, netOutgoingMessage);
		return EstablishConnection();
	}

	private bool EstablishConnection()
	{
		DateTime now = DateTime.Now;
		do
		{
			NetIncomingMessage netIncomingMessage;
			if ((netIncomingMessage = m_Client.ReadMessage()) != null)
			{
				NetIncomingMessageType messageType = netIncomingMessage.MessageType;
				if (messageType == NetIncomingMessageType.StatusChanged && netIncomingMessage.SenderConnection.Status == NetConnectionStatus.Connected)
				{
					Active = true;
					NetOutgoingMessage netOutgoingMessage = m_Client.CreateMessage();
					netOutgoingMessage.Write((byte)4);
					m_Client.SendMessage(netOutgoingMessage, NetDeliveryMethod.ReliableOrdered);
					return true;
				}
			}
		}
		while (DateTime.Now.Subtract(now).Seconds <= 5);
		return false;
	}

	private void SpawnPlayer()
	{
		m_SpawnedPlayer = UnityEngine.Object.Instantiate(m_PlayerPrefab);
		m_LocalPlayer = new NetPlayer
		{
			PlayerName = m_LocalName,
			PlayerObject = m_SpawnedPlayer
		};
		m_SpawnedPlayer.GetComponent<NetworkController>().Init(this, hasControl: true, m_LocalName);
		m_SpawnedPlayer.SetActive(value: true);
	}

	private void Update()
	{
		if (Active)
		{
			ReadMessages();
		}
	}

	private void ReadMessages()
	{
		NetIncomingMessage netIncomingMessage;
		while ((netIncomingMessage = m_Client.ReadMessage()) != null)
		{
			if (netIncomingMessage.MessageType == NetIncomingMessageType.Data)
			{
				PacketType packetType = (PacketType)netIncomingMessage.ReadByte();
				Debug.Log("Recieved Message: " + packetType);
				switch (packetType)
				{
				case PacketType.Login:
				{
					bool flag = netIncomingMessage.ReadBoolean();
					RecieveAllPlayers(netIncomingMessage);
					break;
				}
				case PacketType.NewPlayer:
					RecieveNewPlayer(netIncomingMessage);
					break;
				case PacketType.AllPlayers:
					RecieveAllPlayers(netIncomingMessage);
					break;
				case PacketType.Ping:
					Debug.Log("Recieved Ping!");
					break;
				case PacketType.PlayerUpdate:
					RecievedPlayerUpdate(netIncomingMessage);
					break;
				}
			}
		}
	}

	private void RecievedPlayerUpdate(NetIncomingMessage msg)
	{
		Vector3 newPos = msg.ReadVector3();
		string playerName = msg.ReadString();
		NetPlayer netPlayer = FindPlayerByName(playerName);
		netPlayer.PlayerObject.GetComponent<NetworkController>().UpdatePosition(newPos);
	}

	private NetPlayer FindPlayerByName(string playerName)
	{
		foreach (NetPlayer player in m_Players)
		{
			if (player.PlayerName == playerName)
			{
				return player;
			}
		}
		throw new Exception("Could not find player woth name: " + playerName);
	}

	private void RecieveNewPlayer(NetIncomingMessage msg)
	{
		Player player = new Player();
		msg.ReadAllProperties(player);
		SpawnRemotePlayer(player);
	}

	private void RecieveAllPlayers(NetIncomingMessage message)
	{
		byte b = message.ReadByte();
		for (byte b2 = 0; b2 < b; b2 = (byte)(b2 + 1))
		{
			Player player = new Player();
			message.ReadAllProperties(player);
			if (IsNewPlayer(player))
			{
				SpawnRemotePlayer(player);
			}
		}
	}

	private bool IsNewPlayer(Player player)
	{
		foreach (NetPlayer player2 in m_Players)
		{
			if (player2.PlayerName == player.Name)
			{
				return false;
			}
		}
		return true;
	}

	private void SpawnRemotePlayer(Player newPlayer)
	{
		GameObject gameObject = UnityEngine.Object.Instantiate(m_PlayerPrefab);
		gameObject.name = newPlayer.Name;
		gameObject.GetComponent<NetworkController>().Init(this, hasControl: false, newPlayer.Name);
		gameObject.SetActive(value: true);
		m_Players.Add(new NetPlayer
		{
			PlayerObject = gameObject,
			PlayerName = newPlayer.Name
		});
	}

	public void UpdateFromLocalClient(Vector3 position)
	{
		NetOutgoingMessage netOutgoingMessage = m_Client.CreateMessage();
		netOutgoingMessage.Write((byte)5);
		netOutgoingMessage.Write(position);
		netOutgoingMessage.Write(m_LocalName);
		m_Client.SendMessage(netOutgoingMessage, NetDeliveryMethod.Unreliable);
	}
}