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 m_Players = new List(); 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().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().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().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); } }