private void SendPositionToClients() { BouncyBallSyncMessage syncMessage = new BouncyBallSyncMessage { ID = ID, ServerTick = GameServerManager.Instance.Tick, Position = _rb.transform.position, Velocity = _rb.velocity }; using (Message m = Message.Create(NetworkTags.InGame.BouncyBallSyncPos, syncMessage)) { foreach (IClient client in GameServerManager.Instance.ClientManager.GetAllClients()) { // Does this really need to be reliable? client.SendMessage(m, SendMode.Reliable); } } }
private void UpdateFromServer(object sender, DarkRift.Client.MessageReceivedEventArgs e) { if (e.Tag == NetworkTags.InGame.BouncyBallSyncPos) { // Get message data var syncMessage = e.GetMessage().Deserialize <BouncyBallSyncMessage>(); // First time receiving message if (_lastMessage == null) { _rb.velocity = syncMessage.Velocity; _rb.transform.position = syncMessage.Position; _clientTick = syncMessage.ServerTick; _lastMessage = syncMessage; } if (ID == syncMessage.ID && syncMessage.ServerTick > _lastMessage.ServerTick) { _lastMessage = syncMessage; } } }
private void Reconcile() { float threshold = 0.05f; float maxFrames = 50; if (_reconciliationInfo.Count > 0) { BouncyBallSyncMessage clientInfo = null; for (int i = 0; i < _reconciliationInfo.Count; i++) { if (_reconciliationInfo[i].ServerTick == _lastMessage.ServerTick) { clientInfo = _reconciliationInfo[i]; } } // If 50 ticks have passed, reconcile anyway if (_reconciliationInfo.Count > maxFrames) { _rb.velocity = _lastMessage.Velocity; _rb.transform.position = _lastMessage.Position; _clientTick = _lastMessage.ServerTick; clientInfo = _lastMessage; } if (clientInfo != null) { if (Vector3.Distance(clientInfo.Position, _lastMessage.Position) >= threshold) { _rb.velocity = _lastMessage.Velocity; _rb.transform.position = _lastMessage.Position; } _reconciliationInfo.Clear(); } } }